Skip to content

Commit

Permalink
Changed the way the GroupDialog description is displayed. It now uses…
Browse files Browse the repository at this point in the history
… a TextFlow Node to display the stylised text. The string with html tags is parsed and the sections of text within html tags are put in stylised Text Nodes. Rearranged the GroupDialog window, since the window grew larger then the display size. GroupDialog window is no longer resizable. fixes JabRef#4610
  • Loading branch information
LeonhardWolz committed Feb 17, 2019
1 parent 891a93b commit 40138d6
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/gui/groups/GroupDescriptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ private GroupDescriptions() {
public static String getDescriptionForPreview(String field, String expr, boolean caseSensitive, boolean regExp) {
String header = regExp ? Localization.lang(
"This group contains entries whose <b>%0</b> field contains the regular expression <b>%1</b>",
field, StringUtil.quoteForHTML(expr)) : Localization.lang(
field, expr) : Localization.lang(
"This group contains entries whose <b>%0</b> field contains the keyword <b>%1</b>",
field, StringUtil.quoteForHTML(expr));
field, expr);
String caseSensitiveText = caseSensitive ? Localization.lang("case sensitive") : Localization
.lang("case insensitive");
String footer = regExp ? Localization
Expand All @@ -30,7 +30,7 @@ public static String getDescriptionForPreview(String field, String expr, boolean
+ "then using the context menu. "
+ "This process removes the term <b>%1</b> from "
+ "each entry's <b>%0</b> field.",
field, StringUtil.quoteForHTML(expr));
field, expr);
return String.format("%s (%s). %s", header, caseSensitiveText, footer);
}

Expand Down
103 changes: 77 additions & 26 deletions src/main/java/org/jabref/gui/groups/GroupDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@

import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.ButtonType;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
Expand All @@ -26,7 +29,6 @@
import javafx.scene.text.FontPosture;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.scene.web.WebView;

import org.jabref.Globals;
import org.jabref.JabRefGUI;
Expand Down Expand Up @@ -104,7 +106,7 @@ class GroupDialog extends BaseDialog<AbstractGroup> {
private final TextField texGroupFilePath = new TextField();

// for all types
private final WebView descriptionWebView = new WebView();
private final TextFlow descriptionTextFlow = new TextFlow();
private final StackPane optionsPanel = new StackPane();


Expand All @@ -120,7 +122,10 @@ public GroupDialog(JabRefFrame jabrefFrame, AbstractGroup editedGroup) {

explicitRadioButton.setSelected(true);

descriptionWebView.setPrefWidth(585);
descriptionTextFlow.setMinWidth(585);
descriptionTextFlow.setPrefWidth(585);
descriptionTextFlow.setMinHeight(180);
descriptionTextFlow.setPrefHeight(180);

// set default values (overwritten if editedGroup != null)
keywordGroupSearchField.setText(jabrefFrame.prefs().get(JabRefPreferences.GROUPS_DEFAULT_FIELD));
Expand Down Expand Up @@ -196,26 +201,31 @@ public GroupDialog(JabRefFrame jabrefFrame, AbstractGroup editedGroup) {
selectPanel.setPadding(new Insets(0, 0, 0, 10));

// Description panel
ScrollPane descriptionPane = new ScrollPane(descriptionWebView);
ScrollPane descriptionPane = new ScrollPane(descriptionTextFlow);
descriptionPane.setHbarPolicy(ScrollBarPolicy.AS_NEEDED);
descriptionPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);

// create layout
VBox mainPanel = new VBox(15);
HBox mainPanel = new HBox(15);
getDialogPane().setContent(mainPanel);
mainPanel.setPadding(new Insets(5, 15, 5, 15));
mainPanel.getChildren().setAll(
generalPanel,
new VBox(5,
new Label(Localization.lang("Type")),
selectPanel
),
new VBox(
new Label(Localization.lang("Options")),
optionsPanel
generalPanel,
new VBox(5,
new Label(Localization.lang("Type")),
selectPanel
)
),
new Label(Localization.lang("Description")),
descriptionPane
new Separator(Orientation.VERTICAL),
new VBox(5,
new VBox(
new Label(Localization.lang("Options")),
optionsPanel
),
new Label(Localization.lang("Description")),
descriptionPane
)
);

updateComponents();
Expand Down Expand Up @@ -393,6 +403,9 @@ groupName, getContext(),
texGroupFilePath.setText(group.getFilePath().toString());
}
}

setResizable(false);
getDialogPane().getScene().getWindow().sizeToScene();
}

public GroupDialog() {
Expand Down Expand Up @@ -517,7 +530,7 @@ private void updateComponents() {
boolean okEnabled = !nameField.getText().trim().isEmpty();
if (!okEnabled) {
setDescription(Localization.lang("Please enter a name for the group."));
//TODO: okButton.setDisable(true);
getDialogPane().lookupButton(ButtonType.OK).setDisable(true);
return;
}
String s1;
Expand All @@ -532,27 +545,27 @@ private void updateComponents() {
try {
Pattern.compile(s2);
setDescription(GroupDescriptions.getDescriptionForPreview(s1, s2, keywordGroupCaseSensitive.isSelected(),
keywordGroupRegExp.isSelected()));
keywordGroupRegExp.isSelected()));
} catch (PatternSyntaxException e) {
okEnabled = false;
setDescription(formatRegExException(s2, e));
}
} else {
setDescription(GroupDescriptions.getDescriptionForPreview(s1, s2, keywordGroupCaseSensitive.isSelected(),
keywordGroupRegExp.isSelected()));
keywordGroupRegExp.isSelected()));
}
} else {
setDescription(Localization.lang(
"Please enter the field to search (e.g. <b>keywords</b>) and the keyword to search it for (e.g. <b>electrical</b>)."));
"Please enter the field to search (e.g. <b>keywords</b>) and the keyword to search it for (e.g. <b>electrical</b>)."));
}
setNameFontItalic(true);
} else if (searchRadioButton.isSelected()) {
s1 = searchGroupSearchExpression.getText().trim();
okEnabled = okEnabled & !s1.isEmpty();
if (okEnabled) {
setDescription(fromTextFlowToHTMLString(SearchDescribers.getSearchDescriberFor(
new SearchQuery(s1, isCaseSensitive(), isRegex()))
.getDescription()));
new SearchQuery(s1, isCaseSensitive(), isRegex()))
.getDescription()));

if (isRegex()) {
try {
Expand All @@ -564,17 +577,17 @@ private void updateComponents() {
}
} else {
setDescription(Localization
.lang("Please enter a search term. For example, to search all fields for <b>Smith</b>, enter:<p>"
+ "<tt>smith</tt><p>"
+ "To search the field <b>Author</b> for <b>Smith</b> and the field <b>Title</b> for <b>electrical</b>, enter:<p>"
+ "<tt>author=smith and title=electrical</tt>"));
.lang("Please enter a search term. For example, to search all fields for <b>Smith</b>, enter:<p>"
+ "<tt>smith</tt><p>"
+ "To search the field <b>Author</b> for <b>Smith</b> and the field <b>Title</b> for <b>electrical</b>, enter:<p>"
+ "<tt>author=smith and title=electrical</tt>"));
}
setNameFontItalic(true);
} else if (explicitRadioButton.isSelected()) {
setDescription(GroupDescriptions.getDescriptionForPreview());
setNameFontItalic(false);
}
//TODO: okButton.setDisable(!okEnabled);
getDialogPane().lookupButton(ButtonType.OK).setDisable(!okEnabled);
}

private String fromTextFlowToHTMLString(TextFlow textFlow) {
Expand All @@ -596,7 +609,45 @@ private boolean isCaseSensitive() {
}

private void setDescription(String description) {
descriptionWebView.getEngine().loadContent("<html>" + description + "</html>");
descriptionTextFlow.getChildren().setAll(createFormattedDescription(description));
}

private ArrayList<Node> createFormattedDescription(String description) {
ArrayList<Node> nodes = new ArrayList<>();

description = description.replaceAll("<p>|<br>", "\n");

String[] boldSplit = description.split("(?=<b>)|(?<=</b>)|(?=<i>)|(?<=</i>)|(?=<tt>)|(?<=</tt>)|(?=<kbd>)|(?<=</kbd>)");

for (String bs : boldSplit) {

if (bs.matches("<b>[^<>]*</b>")) {

bs = bs.replaceAll("<b>|</b>", "");
Text textElement = new Text(bs);
textElement.setStyle("-fx-font-weight: bold");
nodes.add(textElement);

} else if (bs.matches("<i>[^<>]*</i>")) {

bs = bs.replaceAll("<i>|</i>", "");
Text textElement = new Text(bs);
textElement.setStyle("-fx-font-style: italic");
nodes.add(textElement);

} else if (bs.matches("<tt>[^<>]*</tt>|<kbd>[^<>]*</kbd>")) {

bs = bs.replaceAll("<tt>|</tt>|<kbd>|</kbd>", "");
Text textElement = new Text(bs);
textElement.setStyle("-fx-font-family: 'Courier New', Courier, monospace");
nodes.add(textElement);

} else {
nodes.add(new Text(bs));
}
}

return nodes;
}

/**
Expand Down

0 comments on commit 40138d6

Please sign in to comment.