Skip to content

Commit

Permalink
ref torakiki#127: added a way to disable move buttons from selection …
Browse files Browse the repository at this point in the history
…table and

toolbar
  • Loading branch information
torakiki committed Dec 14, 2015
1 parent 7e6dbbb commit 6039000
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,12 @@ public class MultipleSelectionPane extends BorderPane implements ModuleOwned, Re
private String ownerModule = StringUtils.EMPTY;
private SelectionTable table;

public MultipleSelectionPane(String ownerModule, SelectionTableColumn<?>... columns) {
this(ownerModule, false, columns);
}

public MultipleSelectionPane(String ownerModule, boolean canDuplicate, SelectionTableColumn<?>... columns) {
public MultipleSelectionPane(String ownerModule, boolean canDuplicate, boolean canMove,
SelectionTableColumn<?>... columns) {
require(columns.length > 0, "No column has been selected");
this.ownerModule = defaultString(ownerModule);
setTop(new SelectionTableToolbar(ownerModule));
table = new SelectionTable(ownerModule, canDuplicate, columns);
setTop(new SelectionTableToolbar(ownerModule, canMove));
table = new SelectionTable(ownerModule, canDuplicate, canMove, columns);
setCenter(table);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,15 @@
* @author Andrea Vacondio
*
*/
public class SelectionTable extends TableView<SelectionTableRowData>implements ModuleOwned, RestorableView {
public class SelectionTable extends TableView<SelectionTableRowData> implements ModuleOwned, RestorableView {
private static final Logger LOG = LoggerFactory.getLogger(SelectionTable.class);
private String ownerModule = StringUtils.EMPTY;
private Label placeHolder = new Label(DefaultI18nContext.getInstance().i18n("Drag and drop PDF files here"));
private PasswordFieldPopup passwordPopup;
private Consumer<SelectionChangedEvent> selectionChangedConsumer;

public SelectionTable(String ownerModule, SelectionTableColumn<?>... columns) {
this(ownerModule, false, columns);
}

public SelectionTable(String ownerModule, boolean canDuplicateItems, SelectionTableColumn<?>... columns) {
public SelectionTable(String ownerModule, boolean canDuplicateItems, boolean canMove,
SelectionTableColumn<?>... columns) {
this.ownerModule = defaultString(ownerModule);
setEditable(true);
getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
Expand All @@ -126,44 +124,84 @@ public SelectionTable(String ownerModule, boolean canDuplicateItems, SelectionTa
setOnDragExited(this::onDragExited);
setOnDragDropped(e -> dragConsume(e, this.onDragDropped()));
passwordPopup = new PasswordFieldPopup(this.ownerModule);
initContextMenu(canDuplicateItems);
eventStudio().addAnnotatedListeners(this);
}

private void initContextMenu(boolean canDuplicateItems) {
MenuItem infoItem = createMenuItem(DefaultI18nContext.getInstance().i18n("Document properties"),
MaterialDesignIcon.INFORMATION_OUTLINE);
infoItem.setOnAction(e -> Platform.runLater(() -> eventStudio()
.broadcast(new ShowPdfDescriptorRequest(getSelectionModel().getSelectedItem().descriptor()))));

MenuItem setDestinationItem = createMenuItem(DefaultI18nContext.getInstance().i18n("Set destination"),
MaterialIcon.FLIGHT_LAND);
setDestinationItem.setOnAction(e -> eventStudio().broadcast(
requestDestination(getSelectionModel().getSelectedItem().descriptor().getFile(), getOwnerModule()),
getOwnerModule()));
setDestinationItem.setAccelerator(new KeyCodeCombination(KeyCode.O, KeyCombination.ALT_DOWN));
selectionChangedConsumer = e -> setDestinationItem.setDisable(!e.isSingleSelection());
ContextMenu contextMenu = new ContextMenu(setDestinationItem, new SeparatorMenuItem());
initItemsSectionContextMenu(contextMenu, canDuplicateItems, canMove);
initBottomSectionContextMenu(contextMenu);
setContextMenu(contextMenu);
eventStudio().addAnnotatedListeners(this);
eventStudio().add(SelectionChangedEvent.class, e -> selectionChangedConsumer.accept(e), ownerModule);
}

private void initItemsSectionContextMenu(ContextMenu contextMenu, boolean canDuplicate, boolean canMove) {

MenuItem removeSelected = createMenuItem(DefaultI18nContext.getInstance().i18n("Remove"),
MaterialDesignIcon.MINUS);
removeSelected.setOnAction(e -> eventStudio().broadcast(new RemoveSelectedEvent(), getOwnerModule()));
removeSelected.setAccelerator(new KeyCodeCombination(KeyCode.DELETE));
contextMenu.getItems().add(removeSelected);
selectionChangedConsumer = selectionChangedConsumer
.andThen(e -> removeSelected.setDisable(e.isClearSelection()));
if (canMove) {
MenuItem moveTopSelected = createMenuItem(DefaultI18nContext.getInstance().i18n("Move to Top"),
MaterialDesignIcon.CHEVRON_DOUBLE_UP);
moveTopSelected
.setOnAction(e -> eventStudio().broadcast(new MoveSelectedEvent(MoveType.TOP), getOwnerModule()));

MenuItem moveUpSelected = createMenuItem(DefaultI18nContext.getInstance().i18n("Move Up"),
MaterialDesignIcon.CHEVRON_UP);
moveUpSelected
.setOnAction(e -> eventStudio().broadcast(new MoveSelectedEvent(MoveType.UP), getOwnerModule()));

MenuItem moveDownSelected = createMenuItem(DefaultI18nContext.getInstance().i18n("Move Down"),
MaterialDesignIcon.CHEVRON_DOWN);
moveDownSelected
.setOnAction(e -> eventStudio().broadcast(new MoveSelectedEvent(MoveType.DOWN), getOwnerModule()));

MenuItem moveBottomSelected = createMenuItem(DefaultI18nContext.getInstance().i18n("Move to Bottom"),
MaterialDesignIcon.CHEVRON_DOUBLE_DOWN);
moveBottomSelected.setOnAction(
e -> eventStudio().broadcast(new MoveSelectedEvent(MoveType.BOTTOM), getOwnerModule()));

contextMenu.getItems().addAll(moveTopSelected, moveUpSelected, moveDownSelected, moveBottomSelected);

moveBottomSelected.setAccelerator(new KeyCodeCombination(KeyCode.END, KeyCombination.ALT_DOWN));
moveDownSelected.setAccelerator(new KeyCodeCombination(KeyCode.DOWN, KeyCombination.ALT_DOWN));
moveUpSelected.setAccelerator(new KeyCodeCombination(KeyCode.UP, KeyCombination.ALT_DOWN));
moveTopSelected.setAccelerator(new KeyCodeCombination(KeyCode.HOME, KeyCombination.ALT_DOWN));

selectionChangedConsumer = selectionChangedConsumer.andThen(e -> {
moveTopSelected.setDisable(!e.canMove(MoveType.TOP));
moveUpSelected.setDisable(!e.canMove(MoveType.UP));
moveDownSelected.setDisable(!e.canMove(MoveType.DOWN));
moveBottomSelected.setDisable(!e.canMove(MoveType.BOTTOM));
});
}
if (canDuplicate) {
MenuItem duplicateItem = createMenuItem(DefaultI18nContext.getInstance().i18n("Duplicate"),
MaterialDesignIcon.CONTENT_COPY);
duplicateItem.setOnAction(e -> eventStudio().broadcast(new DuplicateSelectedEvent(), getOwnerModule()));
duplicateItem.setAccelerator(new KeyCodeCombination(KeyCode.DIGIT2, KeyCombination.ALT_DOWN));

MenuItem moveTopSelected = createMenuItem(DefaultI18nContext.getInstance().i18n("Move to Top"),
MaterialDesignIcon.CHEVRON_DOUBLE_UP);
moveTopSelected
.setOnAction(e -> eventStudio().broadcast(new MoveSelectedEvent(MoveType.TOP), getOwnerModule()));
contextMenu.getItems().add(duplicateItem);

MenuItem moveUpSelected = createMenuItem(DefaultI18nContext.getInstance().i18n("Move Up"),
MaterialDesignIcon.CHEVRON_UP);
moveUpSelected.setOnAction(e -> eventStudio().broadcast(new MoveSelectedEvent(MoveType.UP), getOwnerModule()));
selectionChangedConsumer = selectionChangedConsumer
.andThen(e -> duplicateItem.setDisable(e.isClearSelection()));
}
}

MenuItem moveDownSelected = createMenuItem(DefaultI18nContext.getInstance().i18n("Move Down"),
MaterialDesignIcon.CHEVRON_DOWN);
moveDownSelected
.setOnAction(e -> eventStudio().broadcast(new MoveSelectedEvent(MoveType.DOWN), getOwnerModule()));
private void initBottomSectionContextMenu(ContextMenu contextMenu) {

MenuItem moveBottomSelected = createMenuItem(DefaultI18nContext.getInstance().i18n("Move to Bottom"),
MaterialDesignIcon.CHEVRON_DOUBLE_DOWN);
moveBottomSelected
.setOnAction(e -> eventStudio().broadcast(new MoveSelectedEvent(MoveType.BOTTOM), getOwnerModule()));
MenuItem infoItem = createMenuItem(DefaultI18nContext.getInstance().i18n("Document properties"),
MaterialDesignIcon.INFORMATION_OUTLINE);
infoItem.setOnAction(e -> Platform.runLater(() -> eventStudio()
.broadcast(new ShowPdfDescriptorRequest(getSelectionModel().getSelectedItem().descriptor()))));

MenuItem openFileItem = createMenuItem(DefaultI18nContext.getInstance().i18n("Open"),
MaterialDesignIcon.FILE_PDF_BOX);
Expand All @@ -175,46 +213,18 @@ private void initContextMenu(boolean canDuplicateItems) {
openFolderItem.setOnAction(e -> eventStudio().broadcast(
new OpenFileRequest(getSelectionModel().getSelectedItem().descriptor().getFile().getParentFile())));

setDestinationItem.setAccelerator(new KeyCodeCombination(KeyCode.O, KeyCombination.ALT_DOWN));
removeSelected.setAccelerator(new KeyCodeCombination(KeyCode.DELETE));
moveBottomSelected.setAccelerator(new KeyCodeCombination(KeyCode.END, KeyCombination.ALT_DOWN));
moveDownSelected.setAccelerator(new KeyCodeCombination(KeyCode.DOWN, KeyCombination.ALT_DOWN));
moveUpSelected.setAccelerator(new KeyCodeCombination(KeyCode.UP, KeyCombination.ALT_DOWN));
moveTopSelected.setAccelerator(new KeyCodeCombination(KeyCode.HOME, KeyCombination.ALT_DOWN));
infoItem.setAccelerator(new KeyCodeCombination(KeyCode.P, KeyCombination.ALT_DOWN));
openFileItem.setAccelerator(new KeyCodeCombination(KeyCode.O, KeyCombination.SHORTCUT_DOWN));
openFolderItem.setAccelerator(
new KeyCodeCombination(KeyCode.O, KeyCombination.SHORTCUT_DOWN, KeyCombination.ALT_DOWN));

eventStudio().add(SelectionChangedEvent.class, e -> {
setDestinationItem.setDisable(!e.isSingleSelection());
contextMenu.getItems().addAll(new SeparatorMenuItem(), infoItem, openFileItem, openFolderItem);

selectionChangedConsumer = selectionChangedConsumer.andThen(e -> {
infoItem.setDisable(!e.isSingleSelection());
openFileItem.setDisable(!e.isSingleSelection());
openFolderItem.setDisable(!e.isSingleSelection());
removeSelected.setDisable(e.isClearSelection());
moveTopSelected.setDisable(!e.canMove(MoveType.TOP));
moveUpSelected.setDisable(!e.canMove(MoveType.UP));
moveDownSelected.setDisable(!e.canMove(MoveType.DOWN));
moveBottomSelected.setDisable(!e.canMove(MoveType.BOTTOM));

} , getOwnerModule());

ContextMenu context = new ContextMenu(setDestinationItem, new SeparatorMenuItem(), removeSelected,
moveTopSelected, moveUpSelected, moveDownSelected, moveBottomSelected);

if (canDuplicateItems) {
MenuItem duplicateItem = createMenuItem(DefaultI18nContext.getInstance().i18n("Duplicate"),
MaterialDesignIcon.CONTENT_COPY);
duplicateItem.setOnAction(e -> eventStudio().broadcast(new DuplicateSelectedEvent(), getOwnerModule()));

duplicateItem.setAccelerator(new KeyCodeCombination(KeyCode.DIGIT2, KeyCombination.ALT_DOWN));
eventStudio().add(SelectionChangedEvent.class, e -> duplicateItem.setDisable(e.isClearSelection()),
getOwnerModule());
context.getItems().add(duplicateItem);
}

context.getItems().addAll(new SeparatorMenuItem(), infoItem, openFileItem, openFolderItem);
setContextMenu(context);
});
}

private MenuItem createMenuItem(String text, GlyphIcons icon) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ class SelectionTableToolbar extends ToolBar implements ModuleOwned {

private String ownerModule = StringUtils.EMPTY;

public SelectionTableToolbar(String ownerModule) {
public SelectionTableToolbar(String ownerModule, boolean canMove) {
this.ownerModule = defaultString(ownerModule);
getItems().addAll(new AddButton(ownerModule), new ClearButton(ownerModule), new RemoveButton(ownerModule),
new MoveUpButton(ownerModule), new MoveDownButton(ownerModule));
getItems().addAll(new AddButton(ownerModule), new ClearButton(ownerModule), new RemoveButton(ownerModule));
if (canMove) {
getItems().addAll(new MoveUpButton(ownerModule), new MoveDownButton(ownerModule));
}
getStyleClass().add("selection-tool-bar");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class SelectionTableTest extends GuiTest {

@Override
protected Parent getRootNode() {
SelectionTable victim = new SelectionTable(MODULE, true,
SelectionTable victim = new SelectionTable(MODULE, true, true,
new SelectionTableColumn<?>[] { new LoadingColumn(MODULE), FileColumn.NAME, LongColumn.SIZE,
IntColumn.PAGES, LongColumn.LAST_MODIFIED, StringColumn.PAGE_SELECTION });
victim.setId("victim");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class SelectionTableToolbarTest extends GuiTest {

@Override
protected Parent getRootNode() {
SelectionTableToolbar victim = new SelectionTableToolbar(MODULE);
SelectionTableToolbar victim = new SelectionTableToolbar(MODULE, true);
victim.setId("victim");
return victim;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class MergeSelectionPane extends MultipleSelectionPane
private static final Logger LOG = LoggerFactory.getLogger(MergeSelectionPane.class);

public MergeSelectionPane(String ownerModule) {
super(ownerModule, true, new SelectionTableColumn<?>[] { new LoadingColumn(ownerModule), FileColumn.NAME,
super(ownerModule, true, true, new SelectionTableColumn<?>[] { new LoadingColumn(ownerModule), FileColumn.NAME,
LongColumn.SIZE, IntColumn.PAGES, LongColumn.LAST_MODIFIED, StringColumn.PAGE_SELECTION });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public class RotateSelectionPane extends MultipleSelectionPane
implements TaskParametersBuildStep<RotateParametersBuilder> {

public RotateSelectionPane(String ownerModule) {
super(ownerModule, new SelectionTableColumn<?>[] { new LoadingColumn(ownerModule), FileColumn.NAME,
LongColumn.SIZE, IntColumn.PAGES, LongColumn.LAST_MODIFIED });
super(ownerModule, false, false, new SelectionTableColumn<?>[] { new LoadingColumn(ownerModule),
FileColumn.NAME, LongColumn.SIZE, IntColumn.PAGES, LongColumn.LAST_MODIFIED });
}

public void apply(RotateParametersBuilder builder, Consumer<String> onError) {
Expand Down

0 comments on commit 6039000

Please sign in to comment.