Skip to content

Commit

Permalink
Groups button and context menu for adding groups (#2508)
Browse files Browse the repository at this point in the history
* Add button for adding a new group

* Add context menu for adding a new subgroup

* Update localization

* Rest of upadet
  • Loading branch information
tobiasdiez authored Feb 2, 2017
1 parent 99223c4 commit 6bd8f4c
Show file tree
Hide file tree
Showing 29 changed files with 523 additions and 465 deletions.
7 changes: 7 additions & 0 deletions src/main/java/net/sf/jabref/gui/Dialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.sf.jabref.gui;

import java.util.Optional;

public interface Dialog<R> {
Optional<R> showAndWait();
}
8 changes: 8 additions & 0 deletions src/main/java/net/sf/jabref/gui/DialogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ Optional<ButtonType> showCustomButtonDialogAndWait(Alert.AlertType type, String
*/
Optional<ButtonType> showCustomDialogAndWait(String title, DialogPane contentPane, ButtonType... buttonTypes);

/**
* Shows a custom dialog and returns the result.
*
* @param dialog dialog to show
* @param <R> type of result
*/
<R> Optional<R> showCustomDialogAndWait(Dialog<R> dialog);

/**
* Notify the user in an non-blocking way (i.e., update status message instead of showing a dialog).
* @param message the message to show.
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/sf/jabref/gui/FXDialogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public Optional<ButtonType> showCustomDialogAndWait(String title, DialogPane con
return alert.showAndWait();
}

@Override
public <R> Optional<R> showCustomDialogAndWait(Dialog<R> dialog) {
return dialog.showAndWait();
}

@Override
public void notify(String message) {
JabRefGUI.getMainFrame().output(message);
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/net/sf/jabref/gui/groups/GroupDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

Expand All @@ -28,6 +29,8 @@
import javax.swing.event.CaretListener;

import net.sf.jabref.Globals;
import net.sf.jabref.JabRefGUI;
import net.sf.jabref.gui.Dialog;
import net.sf.jabref.gui.JabRefFrame;
import net.sf.jabref.gui.fieldeditors.TextField;
import net.sf.jabref.gui.keyboard.KeyBinding;
Expand All @@ -51,7 +54,7 @@
* Dialog for creating or modifying groups. Operates directly on the Vector
* containing group information.
*/
class GroupDialog extends JDialog {
class GroupDialog extends JDialog implements Dialog<AbstractGroup> {

private static final int INDEX_EXPLICIT_GROUP = 0;
private static final int INDEX_KEYWORD_GROUP = 1;
Expand Down Expand Up @@ -337,6 +340,10 @@ public void actionPerformed(ActionEvent e) {
}
}

public GroupDialog() {
this(JabRefGUI.getMainFrame(), null);
}

private static String formatRegExException(String regExp, Exception e) {
String[] sa = e.getMessage().split("\\n");
StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -492,4 +499,15 @@ private void setContext(GroupHierarchyType context) {
independentButton.setSelected(true);
}
}

@Override
public Optional<AbstractGroup> showAndWait() {
this.setVisible(true);
if (this.okPressed()) {
AbstractGroup newGroup = getResultingGroup();
return Optional.of(newGroup);
} else {
return Optional.empty();
}
}
}
30 changes: 17 additions & 13 deletions src/main/java/net/sf/jabref/gui/groups/GroupNodeViewModel.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package net.sf.jabref.gui.groups;

import java.util.Objects;
import java.util.stream.Collectors;

import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import net.sf.jabref.logic.l10n.Localization;
Expand All @@ -16,17 +16,18 @@
import net.sf.jabref.model.groups.GroupTreeNode;

import com.google.common.eventbus.Subscribe;
import org.fxmisc.easybind.EasyBind;

public class GroupNodeViewModel {

private final String name;
private final boolean isRoot;
private final String iconCode;
private final boolean isLeaf;
private final ObservableList<GroupNodeViewModel> children = FXCollections.observableArrayList();
private final ObservableList<GroupNodeViewModel> children;
private final BibDatabaseContext databaseContext;
private final GroupTreeNode groupNode;
private final SimpleIntegerProperty hits;
private final SimpleBooleanProperty hasChildren;

public GroupNodeViewModel(BibDatabaseContext databaseContext, GroupTreeNode groupNode) {
this.databaseContext = Objects.requireNonNull(databaseContext);
Expand All @@ -35,15 +36,17 @@ public GroupNodeViewModel(BibDatabaseContext databaseContext, GroupTreeNode grou
name = groupNode.getName();
isRoot = groupNode.isRoot();
iconCode = "";
isLeaf = groupNode.isLeaf();
children.addAll(groupNode.getChildren().stream().map(child -> new GroupNodeViewModel(databaseContext, child)).collect(Collectors.toList()));
children = EasyBind.map(groupNode.getChildren(), child -> new GroupNodeViewModel(databaseContext, child));
hasChildren = new SimpleBooleanProperty();
hasChildren.bind(Bindings.isNotEmpty(children));
hits = new SimpleIntegerProperty(0);
calculateNumberOfMatches();

// Register listener
databaseContext.getDatabase().registerListener(this);
}


public GroupNodeViewModel(BibDatabaseContext databaseContext, AbstractGroup group) {
this(databaseContext, new GroupTreeNode(group));
}
Expand All @@ -52,6 +55,10 @@ static GroupNodeViewModel getAllEntriesGroup(BibDatabaseContext newDatabase) {
return new GroupNodeViewModel(newDatabase, new AllEntriesGroup(Localization.lang("All entries")));
}

public SimpleBooleanProperty hasChildrenProperty() {
return hasChildren;
}

public String getName() {
return name;
}
Expand All @@ -76,7 +83,6 @@ public boolean equals(Object o) {
GroupNodeViewModel that = (GroupNodeViewModel) o;

if (isRoot != that.isRoot) return false;
if (isLeaf != that.isLeaf) return false;
if (!name.equals(that.name)) return false;
if (!iconCode.equals(that.iconCode)) return false;
if (!children.equals(that.children)) return false;
Expand All @@ -91,7 +97,6 @@ public String toString() {
"name='" + name + '\'' +
", isRoot=" + isRoot +
", iconCode='" + iconCode + '\'' +
", isLeaf=" + isLeaf +
", children=" + children +
", databaseContext=" + databaseContext +
", groupNode=" + groupNode +
Expand All @@ -104,7 +109,6 @@ public int hashCode() {
int result = name.hashCode();
result = 31 * result + (isRoot ? 1 : 0);
result = 31 * result + iconCode.hashCode();
result = 31 * result + (isLeaf ? 1 : 0);
result = 31 * result + children.hashCode();
result = 31 * result + databaseContext.hashCode();
result = 31 * result + groupNode.hashCode();
Expand All @@ -116,10 +120,6 @@ public String getIconCode() {
return iconCode;
}

public boolean isLeaf() {
return isLeaf;
}

public ObservableList<GroupNodeViewModel> getChildren() {
return children;
}
Expand All @@ -145,4 +145,8 @@ private void calculateNumberOfMatches() {
Platform.runLater(() -> hits.setValue(newHits));
}).start();
}

public GroupTreeNode addSubgroup(AbstractGroup subgroup) {
return groupNode.addSubgroup(subgroup);
}
}
Loading

0 comments on commit 6bd8f4c

Please sign in to comment.