Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid recreation of the EntryEditor #3187

Merged
merged 6 commits into from
Oct 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is getVisibleTabName used somewhere outside of this class? if not then it can be converted to just a field and don't need to be exposed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used in the BasePanel and the main table, so it needs to be visible.


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