Skip to content

Commit

Permalink
Editor tab goes JavaFX (#2813)
Browse files Browse the repository at this point in the history
* Convert entry tab to JavaFX

* Style entry editor tab

* Fix checkstyle....

* Implement feedback
  • Loading branch information
tobiasdiez authored May 9, 2017
1 parent 4832e5d commit 7818515
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 257 deletions.
14 changes: 7 additions & 7 deletions src/main/java/org/jabref/gui/PreambleEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ protected boolean accept(Component c) {
// TODO: Reenable this
//setupJTextComponent((TextArea) editor);

gbl.setConstraints(editor.getLabel(), con);
pan.add(editor.getLabel());
//gbl.setConstraints(editor.getLabel(), con);
//pan.add(editor.getLabel());

con.weightx = 1;

Expand Down Expand Up @@ -156,11 +156,11 @@ public void actionPerformed(ActionEvent e) {
panel.getUndoManager().addEdit(
new UndoablePreambleChange(database, panel, database.getPreamble().orElse(null), toSet));
database.setPreamble(toSet);
if ((toSet == null) || toSet.isEmpty()) {
editor.setLabelColor(GUIGlobals.NULL_FIELD_COLOR);
} else {
editor.setLabelColor(GUIGlobals.ENTRY_EDITOR_LABEL_COLOR);
}
//if ((toSet == null) || toSet.isEmpty()) {
// editor.setLabelColor(GUIGlobals.NULL_FIELD_COLOR);
//} else {
// editor.setLabelColor(GUIGlobals.ENTRY_EDITOR_LABEL_COLOR);
//}
editor.setValidBackgroundColor();
if (editor.hasFocus()) {
editor.setActiveBackgroundColor();
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.text-area {
-fx-text-fill: text-area-foreground;
-fx-control-inner-background: text-area-background;
}

.text-area *.text {
-fx-alignment: center-left;
}

.editorPane {
-fx-hgap: 10;
-fx-vgap: 6;
-fx-background-color: text-area-background;
-fx-padding: 0 0 0 5;
}
179 changes: 89 additions & 90 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditorTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,29 @@
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;

import javafx.embed.swing.JFXPanel;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.RowConstraints;

import org.jabref.Globals;
import org.jabref.gui.BasePanel;
Expand All @@ -34,7 +40,6 @@
import org.jabref.gui.fieldeditors.FieldEditorFX;
import org.jabref.gui.fieldeditors.FieldEditors;
import org.jabref.gui.fieldeditors.FieldNameLabel;
import org.jabref.gui.fieldeditors.TextField;
import org.jabref.gui.keyboard.KeyBinding;
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.logic.l10n.Localization;
Expand All @@ -43,20 +48,15 @@
import org.jabref.model.entry.FieldProperty;
import org.jabref.model.entry.InternalBibtexFields;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

/**
* A single tab displayed in the EntryEditor holding several FieldEditors.
*/
class EntryEditorTab {

private final JPanel panel = new JPanel();
private final JScrollPane scrollPane = new JScrollPane(panel,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
private final JFXPanel panel = new JFXPanel();
private final List<String> fields;
private final EntryEditor parent;
private final Map<String, FieldEditorFX> editors = new HashMap<>();
private final Map<String, FieldEditorFX> editors = new LinkedHashMap<>();
private final FocusListener fieldListener = new EntryEditorTabFocusListener(this);
private final String tabTitle;
private final JabRefFrame frame;
Expand All @@ -66,59 +66,67 @@ class EntryEditorTab {
private boolean updating;


public EntryEditorTab(JabRefFrame frame, BasePanel panel, List<String> fields, EntryEditor parent,
boolean addKeyField, boolean compressed, String tabTitle) {
public EntryEditorTab(JabRefFrame frame, BasePanel basePanel, List<String> fields, EntryEditor parent,
boolean addKeyField, boolean compressed, String tabTitle) {
if (fields == null) {
this.fields = new ArrayList<>();
} else {
this.fields = new ArrayList<>(fields);
}
// Add the edit field for Bibtex-key.
if (addKeyField) {
this.fields.add(BibEntry.KEY_FIELD);
}

this.parent = parent;
this.tabTitle = tabTitle;
this.frame = frame;
this.basePanel = panel;
this.basePanel = basePanel;

// Execute on JavaFX Application Thread
DefaultTaskExecutor.runInJavaFXThread(() -> {
Region root = setupPanel(frame, basePanel, addKeyField, compressed, tabTitle);

if (GUIGlobals.currentFont != null) {
root.setStyle(
"text-area-background: " + convertToHex(GUIGlobals.validFieldBackgroundColor) + ";"
+ "text-area-foreground: " + convertToHex(GUIGlobals.editorTextColor) + ";"
+ "text-area-highlight: " + convertToHex(GUIGlobals.activeBackgroundColor) + ";"
);
}

setupPanel(frame, panel, addKeyField, compressed, tabTitle);
root.getStylesheets().add("org/jabref/gui/entryeditor/EntryEditor.css");

panel.setScene(new Scene(root));
});

// The following line makes sure focus cycles inside tab instead of being lost to other parts of the frame:
scrollPane.setFocusCycleRoot(true);
panel.setFocusCycleRoot(true);
}

private void setupPanel(JabRefFrame frame, BasePanel bPanel, boolean addKeyField,
boolean compressed, String title) {
private static void addColumn(GridPane gridPane, int columnIndex, List<Label> nodes) {
gridPane.addColumn(columnIndex, nodes.toArray(new Node[nodes.size()]));
}

setupKeyBindings(panel.getInputMap(JComponent.WHEN_FOCUSED), panel.getActionMap());
private static void addColumn(GridPane gridPane, int columnIndex, Stream<Parent> nodes) {
gridPane.addColumn(columnIndex, nodes.toArray(Node[]::new));
}

panel.setName(title);
private String convertToHex(java.awt.Color color) {
return String.format("#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue());
}

// Use the title for the scrollPane, too.
// This enables the correct execution of EntryEditor.setVisiblePanel(String name).
scrollPane.setName(title);
private Region setupPanel(JabRefFrame frame, BasePanel bPanel, boolean addKeyField,
boolean compressed, String title) {

int fieldsPerRow = compressed ? 2 : 1;
setupKeyBindings(panel.getInputMap(JComponent.WHEN_FOCUSED), panel.getActionMap());

String colSpec = compressed ? "fill:pref, 1dlu, fill:10dlu:grow, 1dlu, fill:pref, "
+ "8dlu, fill:pref, 1dlu, fill:10dlu:grow, 1dlu, fill:pref"
: "fill:pref, 1dlu, fill:pref:grow, 1dlu, fill:pref";
StringBuilder stringBuilder = new StringBuilder();
int rows = (int) Math.ceil((double) fields.size() / fieldsPerRow);
for (int i = 0; i < rows; i++) {
stringBuilder.append("fill:pref:grow, ");
}
if (addKeyField) {
stringBuilder.append("4dlu, fill:pref");
} else if (stringBuilder.length() >= 2) {
stringBuilder.delete(stringBuilder.length() - 2, stringBuilder.length());
}
String rowSpec = stringBuilder.toString();
panel.setName(title);

DefaultFormBuilder builder = new DefaultFormBuilder
(new FormLayout(colSpec, rowSpec), panel);
editors.clear();
List<Label> labels = new ArrayList<>();

// BibTex edit fields are defined here
for (int i = 0; i < fields.size(); i++) {
String fieldName = fields.get(i);
for (String fieldName : fields) {

// TODO: Reenable/migrate this
// Store the editor for later reference:
Expand Down Expand Up @@ -181,55 +189,46 @@ private void setupPanel(JabRefFrame frame, BasePanel bPanel, boolean addKeyField
//return FieldExtraComponents.getSelectorExtraComponent(frame, panel, editor, contentSelectors, storeFieldAction);
*/

builder.append(new FieldNameLabel(fieldName));

JFXPanel swingPanel = new JFXPanel();
swingPanel.setBackground(GUIGlobals.activeBackgroundColor);
DefaultTaskExecutor.runInJavaFXThread(
() -> {
Scene scene = new Scene(fieldEditor.getNode());
swingPanel.setScene(scene);
}
);
builder.append(swingPanel, 3);
/*
// TODO: Delete when no longer required
if (extra.isPresent()) {
builder.append(fieldEditor.getPane());
JPanel pan = new JPanel();
pan.setLayout(new BorderLayout());
pan.add(extra.get(), BorderLayout.NORTH);
builder.append(pan);
} else {
builder.append(fieldEditor.getPane(), 3);
}
*/
if (((i + 1) % fieldsPerRow) == 0) {
builder.nextLine();
}
labels.add(new FieldNameLabel(fieldName));
}

// Add the edit field for Bibtex-key.
if (addKeyField) {
final TextField textField = new TextField(BibEntry.KEY_FIELD,
parent.getEntry().getCiteKeyOptional().orElse(""), true);
setupJTextComponent(textField, null);
GridPane gridPane = new GridPane();
gridPane.setPrefSize(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
gridPane.setMaxSize(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
gridPane.getStyleClass().add("editorPane");

// TODO: Reenable this
//editors.put(BibEntry.KEY_FIELD, textField);
fields.add(BibEntry.KEY_FIELD);
/*
* If the key field is the only field, we should have only one
* editor, and this one should be set as active initially:
*/
if (editors.size() == 1) {
// TODO: Reenable this
//activeField = textField;
}
builder.nextLine();
builder.append(textField.getLabel());
builder.append(textField, 3);
ColumnConstraints columnExpand = new ColumnConstraints();
columnExpand.setHgrow(Priority.ALWAYS);

ColumnConstraints columnDoNotContract = new ColumnConstraints();
columnDoNotContract.setMinWidth(Region.USE_PREF_SIZE);
int rows;
if (compressed) {
rows = (int) Math.ceil((double) fields.size() / 2);

addColumn(gridPane, 0, labels.subList(0, rows));
addColumn(gridPane, 3, labels.subList(rows, labels.size()));
addColumn(gridPane, 1, editors.values().stream().map(FieldEditorFX::getNode).limit(rows));
addColumn(gridPane, 4, editors.values().stream().map(FieldEditorFX::getNode).skip(rows));

gridPane.getColumnConstraints().addAll(columnDoNotContract, columnExpand, new ColumnConstraints(10), columnDoNotContract, columnExpand);
} else {
rows = fields.size();

addColumn(gridPane, 0, labels);
addColumn(gridPane, 1, editors.values().stream().map(FieldEditorFX::getNode));

gridPane.getColumnConstraints().addAll(columnDoNotContract, columnExpand);
}

RowConstraints rowExpand = new RowConstraints();
rowExpand.setVgrow(Priority.ALWAYS);
rowExpand.setPercentHeight(100 / rows);
for (int i = 0; i < rows; i++) {
gridPane.getRowConstraints().add(rowExpand);
}

return gridPane;
}

private String getPrompt(String field) {
Expand Down Expand Up @@ -367,7 +366,7 @@ public void setEnabled(boolean enabled) {
}

public Component getPane() {
return scrollPane;
return panel;
}

public EntryEditor getParent() {
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/org/jabref/gui/fieldeditors/EditorTextArea.css

This file was deleted.

15 changes: 0 additions & 15 deletions src/main/java/org/jabref/gui/fieldeditors/EditorTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import javafx.fxml.Initializable;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.text.Font;

import org.jabref.gui.GUIGlobals;

import com.sun.javafx.scene.control.skin.TextAreaSkin;

Expand All @@ -27,18 +24,6 @@ public EditorTextArea(String text) {

// Hide horizontal scrollbar and always wrap text
setWrapText(true);

if (GUIGlobals.currentFont != null) {
setFont(Font.font(GUIGlobals.currentFont.getFontName(), GUIGlobals.currentFont.getSize()));

setStyle(
"text-area-background: " + convertToHex(GUIGlobals.validFieldBackgroundColor) + ";"
+ "text-area-foreground: " + convertToHex(GUIGlobals.editorTextColor) + ";"
+ "text-area-highlight: " + convertToHex(GUIGlobals.activeBackgroundColor) + ";"
);
}

getStylesheets().add("org/jabref/gui/fieldeditors/EditorTextArea.css");
}

/**
Expand Down
Loading

0 comments on commit 7818515

Please sign in to comment.