Skip to content

Commit

Permalink
Avoid recreation of the EntryEditor (#3187)
Browse files Browse the repository at this point in the history
  • Loading branch information
lenhard authored and LinusDietz committed Oct 18, 2017
1 parent 8840da5 commit 81ff8f2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 58 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- Crossreferenced entries are now used when a BibTex key is generated for an entry with empty fields. [#2811](https://github.com/JabRef/jabref/issues/2811)
- We now set the WM_CLASS of the UI to org-jabref-JabRefMain to allow certain Un*x window managers to properly identify its windows
- We changed the default paths for the OpenOffice/LibreOffice binaries to the default path for LibreOffice

- We no longer create a new entry editor when selecting a new entry to increase performance. [#3187](https://github.com/JabRef/jabref/pull/3187)

### Fixed
- We fixed the translation of \textendash in the entry preview [#3307](https://github.com/JabRef/jabref/issues/3307)
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -1486,11 +1486,14 @@ public void showEntry(final BibEntry bibEntry) {
* @return A suitable entry editor.
*/
public EntryEditor getEntryEditor(BibEntry entry) {
String lastTabName = "";
if (currentEditor != null) {
lastTabName = currentEditor.getVisibleTabName();
currentEditor.setEntry(entry);
return currentEditor;
} else {
EntryEditor editor = new EntryEditor(this);
editor.setEntry(entry);
return editor;
}
return new EntryEditor(frame, BasePanel.this, entry, lastTabName);
}

public EntryEditor getCurrentEditor() {
Expand Down
121 changes: 67 additions & 54 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ public class EntryEditor extends JPanel implements EntryContainer {
/**
* A reference to the entry this object works on.
*/
private final BibEntry entry;
private BibEntry entry;
/**
* The currently displayed type
*/
private final String displayedBibEntryType;
private String displayedBibEntryType;

/**
* The action concerned with closing the window.
Expand Down Expand Up @@ -157,71 +157,57 @@ public class EntryEditor extends JPanel implements EntryContainer {
* Indicates that we are about to go to the next or previous entry
*/
private final BooleanProperty movingToDifferentEntry = new SimpleBooleanProperty();
private final EntryType entryType;
private EntryType entryType;
private SourceTab sourceTab;
private final BorderLayout layout;
private TypeLabel typeLabel;

public EntryEditor(JabRefFrame frame, BasePanel panel, BibEntry entry, String lastTabName) {
this.frame = frame;
public EntryEditor(BasePanel panel) {
this.frame = panel.frame();
this.panel = panel;
this.entry = Objects.requireNonNull(entry);
entry.registerListener(this);
entryType = EntryTypes.getTypeOrDefault(entry.getType(),
this.frame.getCurrentBasePanel().getBibDatabaseContext().getMode());

displayedBibEntryType = entry.getType();

writeXmp = new WriteXMPEntryEditorAction(panel, this);

BorderLayout borderLayout = new BorderLayout();
setLayout(borderLayout);
setupToolBar();
layout = new BorderLayout();
setLayout(layout);

container = OS.LINUX ? new CustomJFXPanel() : new JFXPanel();
// Create type-label
typeLabel = new TypeLabel("");
setupToolBar();
DefaultTaskExecutor.runInJavaFXThread(() ->
container.setScene(new Scene(tabbed))
);
add(container, BorderLayout.CENTER);

container.addKeyListener(new KeyAdapter() {
DefaultTaskExecutor.runInJavaFXThread(() -> {
EasyBind.subscribe(tabbed.getSelectionModel().selectedItemProperty(), tab -> {
EntryEditorTab activeTab = (EntryEditorTab) tab;
if (activeTab != null) {
activeTab.notifyAboutFocus();
}
});
});

@Override
public void keyPressed(java.awt.event.KeyEvent e) {
setupKeyBindings();
}

public void setEntry(BibEntry entry) {
this.entry = Objects.requireNonNull(entry);
entry.registerListener(this);
entryType = EntryTypes.getTypeOrDefault(entry.getType(),
this.frame.getCurrentBasePanel().getBibDatabaseContext().getMode());

displayedBibEntryType = entry.getType();

//We need to consume this event here to prevent the propgation of keybinding events back to the JFrame
Optional<KeyBinding> keyBinding = Globals.getKeyPrefs().mapToKeyBinding(e);
if (keyBinding.isPresent()) {
switch (keyBinding.get()) {
case CUT:
case COPY:
case PASTE:
case CLOSE_ENTRY_EDITOR:
case DELETE_ENTRY:
case SELECT_ALL:
case ENTRY_EDITOR_NEXT_PANEL:
case ENTRY_EDITOR_NEXT_PANEL_2:
case ENTRY_EDITOR_PREVIOUS_PANEL:
case ENTRY_EDITOR_PREVIOUS_PANEL_2:
e.consume();
break;
default:
//do nothing
}
}
}
});
DefaultTaskExecutor.runInJavaFXThread(() -> {
addTabs(lastTabName);
addTabs(this.getVisibleTabName());

tabbed.setStyle("-fx-font-size: " + Globals.prefs.getFontSizeFX() + "pt;");

container.setScene(new Scene(tabbed));
});
add(container, BorderLayout.CENTER);

EasyBind.subscribe(tabbed.getSelectionModel().selectedItemProperty(), tab -> {
EntryEditorTab activeTab = (EntryEditorTab) tab;
if (activeTab != null) {
activeTab.notifyAboutFocus();
}
});

setupKeyBindings();
TypedBibEntry typedEntry = new TypedBibEntry(entry, panel.getBibDatabaseContext().getMode());
typeLabel.setText(typedEntry.getTypeForDisplay());
}

@Subscribe
Expand Down Expand Up @@ -288,6 +274,34 @@ private void selectLastUsedTab(String lastTabName) {
* Set-up key bindings specific for the entry editor.
*/
private void setupKeyBindings() {
container.addKeyListener(new KeyAdapter() {

@Override
public void keyPressed(java.awt.event.KeyEvent e) {

//We need to consume this event here to prevent the propgation of keybinding events back to the JFrame
Optional<KeyBinding> keyBinding = Globals.getKeyPrefs().mapToKeyBinding(e);
if (keyBinding.isPresent()) {
switch (keyBinding.get()) {
case CUT:
case COPY:
case PASTE:
case CLOSE_ENTRY_EDITOR:
case DELETE_ENTRY:
case SELECT_ALL:
case ENTRY_EDITOR_NEXT_PANEL:
case ENTRY_EDITOR_NEXT_PANEL_2:
case ENTRY_EDITOR_PREVIOUS_PANEL:
case ENTRY_EDITOR_PREVIOUS_PANEL_2:
e.consume();
break;
default:
//do nothing
}
}
}
});

tabbed.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
Optional<KeyBinding> keyBinding = Globals.getKeyPrefs().mapToKeyBinding(event);
if (keyBinding.isPresent()) {
Expand Down Expand Up @@ -387,6 +401,7 @@ public BibDatabase getDatabase() {
}

private void setupToolBar() {

JPanel leftPan = new JPanel();
leftPan.setLayout(new BorderLayout());
JToolBar toolBar = new OSXCompatibleToolbar(SwingConstants.VERTICAL);
Expand Down Expand Up @@ -426,9 +441,7 @@ private void setupToolBar() {
closeBut.setMargin(new Insets(8, 0, 8, 0));
leftPan.add(closeBut, BorderLayout.NORTH);

// Create type-label
TypedBibEntry typedEntry = new TypedBibEntry(entry, panel.getBibDatabaseContext().getMode());
leftPan.add(new TypeLabel(typedEntry.getTypeForDisplay()), BorderLayout.CENTER);
leftPan.add(typeLabel, BorderLayout.CENTER);
TypeButton typeButton = new TypeButton();

toolBar.add(typeButton);
Expand Down

0 comments on commit 81ff8f2

Please sign in to comment.