Skip to content

Commit

Permalink
Add sort subgroups recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
Siedlerchr committed Apr 6, 2017
1 parent 2ed5e38 commit 2e86aa0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/main/java/org/jabref/gui/groups/GroupTreeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,17 @@ private ContextMenu createContextMenuForGroup(GroupNodeViewModel group) {
MenuItem removeEntries = new MenuItem(Localization.lang("Remove selected entries from this group"));
removeSubgroups.setOnAction(event -> viewModel.removeSelectedEntries(group));

MenuItem sortAlphabetically = new MenuItem(Localization.lang("Sort all subgroups (recursively)"));
sortAlphabetically.setOnAction(event -> viewModel.sortAlphabeticallyRecursive(group));

menu.getItems().add(editGroup);
menu.getItems().add(new SeparatorMenuItem());
menu.getItems().addAll(addSubgroup, removeSubgroups, removeGroupAndSubgroups, removeGroupKeepSubgroups);
menu.getItems().add(new SeparatorMenuItem());
menu.getItems().addAll(addEntries, removeEntries);
menu.getItems().add(new SeparatorMenuItem());
menu.getItems().add(sortAlphabetically);

return menu;
}

Expand Down
33 changes: 22 additions & 11 deletions src/main/java/org/jabref/gui/groups/GroupTreeViewModel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.gui.groups;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -33,18 +34,23 @@ public class GroupTreeViewModel extends AbstractViewModel {
private final ObjectProperty<Predicate<GroupNodeViewModel>> filterPredicate = new SimpleObjectProperty<>();
private final StringProperty filterText = new SimpleStringProperty();
private Optional<BibDatabaseContext> currentDatabase;
private final Comparator<GroupTreeNode> compAlphabetIgnoreCase = (GroupTreeNode v1, GroupTreeNode v2) -> v1
.getName()
.compareToIgnoreCase(v2.getName());

public GroupTreeViewModel(StateManager stateManager, DialogService dialogService, TaskExecutor taskExecutor) {
this.stateManager = Objects.requireNonNull(stateManager);
this.dialogService = Objects.requireNonNull(dialogService);
this.taskExecutor = Objects.requireNonNull(taskExecutor);

// Register listener
stateManager.activeDatabaseProperty().addListener((observable, oldValue, newValue) -> onActiveDatabaseChanged(newValue));
stateManager.activeDatabaseProperty()
.addListener((observable, oldValue, newValue) -> onActiveDatabaseChanged(newValue));
selectedGroup.addListener((observable, oldValue, newValue) -> onSelectedGroupChanged(newValue));

// Set-up bindings
filterPredicate.bind(Bindings.createObjectBinding(() -> group -> group.isMatchedBy(filterText.get()), filterText));
filterPredicate
.bind(Bindings.createObjectBinding(() -> group -> group.isMatchedBy(filterText.get()), filterText));

// Init
onActiveDatabaseChanged(stateManager.activeDatabaseProperty().getValue());
Expand Down Expand Up @@ -106,7 +112,8 @@ private void onActiveDatabaseChanged(Optional<BibDatabaseContext> newDatabase) {

rootGroup.setValue(newRoot);
stateManager.getSelectedGroup(newDatabase.get()).ifPresent(
selectedGroup -> this.selectedGroup.setValue(new GroupNodeViewModel(newDatabase.get(), stateManager, taskExecutor, selectedGroup)));
selectedGroup -> this.selectedGroup.setValue(
new GroupNodeViewModel(newDatabase.get(), stateManager, taskExecutor, selectedGroup)));
}

currentDatabase = newDatabase;
Expand Down Expand Up @@ -135,19 +142,19 @@ public void addNewSubgroup(GroupNodeViewModel parent) {
* Opens "Edit Group Dialog" and changes the given group to the edited one.
*/
public void editGroup(GroupNodeViewModel oldGroup) {
Optional<AbstractGroup> newGroup = dialogService.showCustomDialogAndWait(new GroupDialog(oldGroup.getGroupNode().getGroup()));
Optional<AbstractGroup> newGroup = dialogService
.showCustomDialogAndWait(new GroupDialog(oldGroup.getGroupNode().getGroup()));
newGroup.ifPresent(group -> {

// TODO: Keep assignments
boolean keepPreviousAssignments = dialogService.showConfirmationDialogAndWait(
Localization.lang("Change of Grouping Method"),
Localization.lang("Assign the original group's entries to this group?"));
// WarnAssignmentSideEffects.warnAssignmentSideEffects(newGroup, panel.frame());
boolean removePreviousAssignents =
(oldGroup.getGroupNode().getGroup() instanceof ExplicitGroup) && (group instanceof ExplicitGroup);
boolean removePreviousAssignents = (oldGroup.getGroupNode().getGroup() instanceof ExplicitGroup)
&& (group instanceof ExplicitGroup);

List<FieldChange> addChange = oldGroup.
getGroupNode().setGroup(
List<FieldChange> addChange = oldGroup.getGroupNode().setGroup(
group,
keepPreviousAssignments,
removePreviousAssignents,
Expand Down Expand Up @@ -197,15 +204,14 @@ public void removeGroupKeepSubgroups(GroupNodeViewModel group) {
//final UndoableAddOrRemoveGroup undo = new UndoableAddOrRemoveGroup(groupsRoot, node, UndoableAddOrRemoveGroup.REMOVE_NODE_KEEP_CHILDREN);
//panel.getUndoManager().addEdit(undo);
GroupTreeNode groupNode = group.getGroupNode();
groupNode.getParent().ifPresent(parent ->
groupNode.moveAllChildrenTo(parent, parent.getIndexOfChild(groupNode).get()));
groupNode.getParent()
.ifPresent(parent -> groupNode.moveAllChildrenTo(parent, parent.getIndexOfChild(groupNode).get()));
groupNode.removeFromParent();

dialogService.notify(Localization.lang("Removed group \"%0\".", group.getDisplayName()));
}
}


/**
* Removes the specified group and its subgroups (after asking for confirmation).
*/
Expand Down Expand Up @@ -249,4 +255,9 @@ public void removeSelectedEntries(GroupNodeViewModel group) {
// if (!undo.isEmpty()) {
// mPanel.getUndoManager().addEdit(UndoableChangeEntriesOfGroup.getUndoableEdit(mNode, undo));
}

public void sortAlphabeticallyRecursive(GroupNodeViewModel group) {
group.getGroupNode().sortChildren(compAlphabetIgnoreCase, true);

}
}

0 comments on commit 2e86aa0

Please sign in to comment.