Skip to content

Commit

Permalink
Fix NPE on tab switching in EntryEditor between Mr Dlib and File Anno…
Browse files Browse the repository at this point in the history
…tations (#2567)

Adjust fomratting
  • Loading branch information
Siedlerchr authored and koppor committed Feb 20, 2017
1 parent e36a99e commit 4da74e5
Showing 1 changed file with 41 additions and 35 deletions.
76 changes: 41 additions & 35 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import javafx.scene.layout.StackPane;

import org.jabref.Globals;

import org.jabref.gui.BasePanel;
import org.jabref.gui.EntryContainer;
import org.jabref.gui.GUIGlobals;
Expand Down Expand Up @@ -115,7 +114,6 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
* GUI component that allows editing of the fields of a BibEntry (i.e. the
* one that shows up, when you double click on an entry in the table)
Expand All @@ -127,6 +125,7 @@
* update themselves if the change is made from somewhere else.
*/
public class EntryEditor extends JPanel implements EntryContainer {

private static final Log LOGGER = LogFactory.getLog(EntryEditor.class);

/** A reference to the entry this object works on. */
Expand Down Expand Up @@ -217,7 +216,6 @@ public class EntryEditor extends JPanel implements EntryContainer {

private final List<SearchQueryHighlightListener> searchListeners = new ArrayList<>();


public EntryEditor(JabRefFrame frame, BasePanel panel, BibEntry entry) {
this.frame = frame;
this.panel = panel;
Expand All @@ -242,7 +240,7 @@ public EntryEditor(JabRefFrame frame, BasePanel panel, BibEntry entry) {
}

updateAllFields();
if (this.fileListEditor != null){
if (this.fileListEditor != null) {
this.fileListEditor.adjustColumnWidth();
}
}
Expand All @@ -260,8 +258,6 @@ private void setupFieldPanels() {
Set<String> deprecatedFields = new HashSet<>(EntryConverter.FIELD_ALIASES_TEX_TO_LTX.keySet());
Set<String> usedOptionalFieldsDeprecated = new HashSet<>(deprecatedFields);



if ((type.getOptionalFields() != null) && !type.getOptionalFields().isEmpty()) {
if (!frame.getCurrentBasePanel().getBibDatabaseContext().isBiblatexMode()) {
addOptionalTab(type);
Expand Down Expand Up @@ -379,7 +375,8 @@ private void addSpecialTabs() {

private void addSourceTab() {
String panelName = Localization.lang("%0 source", panel.getBibDatabaseContext().getMode().getFormattedName());
String toolTip = Localization.lang("Show/edit %0 source", panel.getBibDatabaseContext().getMode().getFormattedName());
String toolTip = Localization.lang("Show/edit %0 source",
panel.getBibDatabaseContext().getMode().getFormattedName());
srcPanel.setName(panelName);
tabbed.addTab(panelName, IconTheme.JabRefIcon.SOURCE.getSmallIcon(), srcPanel, toolTip);
tabs.add(srcPanel);
Expand All @@ -401,7 +398,8 @@ private void addOtherTab(List<String> otherFields) {
private List<String> addRequiredTab(EntryType type) {
List<String> requiredFields = type.getRequiredFieldsFlat();

EntryEditorTab requiredPanel = new EntryEditorTab(frame, panel, requiredFields, this, true, false, Localization.lang("Required fields"));
EntryEditorTab requiredPanel = new EntryEditorTab(frame, panel, requiredFields, this, true, false,
Localization.lang("Required fields"));
if (requiredPanel.fileListEditor != null) {
fileListEditor = requiredPanel.fileListEditor;
}
Expand All @@ -411,7 +409,6 @@ private List<String> addRequiredTab(EntryType type) {
return requiredFields;
}


/**
* Creates the related Article Tab
*/
Expand Down Expand Up @@ -443,7 +440,8 @@ private void addOptionalTab(EntryType type) {
}
tabbed.addTab(Localization.lang("Optional fields"), IconTheme.JabRefIcon.OPTIONAL.getSmallIcon(),
optionalPanel
.getPane(), Localization.lang("Show optional fields"));
.getPane(),
Localization.lang("Show optional fields"));
tabs.add(optionalPanel);
}

Expand All @@ -456,7 +454,8 @@ private void addPdfTab() {
Optional<String> field = entry.getField(FieldName.FILE);
if (field.isPresent()) {
fileAnnotationTab = new FileAnnotationTab(this, panel, tabbed);
tabbed.addTab(Localization.lang("File annotations"), IconTheme.JabRefIcon.COMMENT.getSmallIcon(), fileAnnotationTab,
tabbed.addTab(Localization.lang("File annotations"), IconTheme.JabRefIcon.COMMENT.getSmallIcon(),
fileAnnotationTab,
Localization.lang("Show file annotations"));
tabs.add(fileAnnotationTab);
}
Expand Down Expand Up @@ -533,7 +532,8 @@ private void setupToolBar() {
toolBar.add(writeXmp);

JPopupMenu fetcherPopup = new JPopupMenu();
for(EntryBasedFetcher fetcher : EntryFetchers.getEntryBasedFetchers(Globals.prefs.getImportFormatPreferences())) {
for (EntryBasedFetcher fetcher : EntryFetchers
.getEntryBasedFetchers(Globals.prefs.getImportFormatPreferences())) {
fetcherPopup.add(new JMenuItem(new AbstractAction(fetcher.getName()) {

@Override
Expand Down Expand Up @@ -630,7 +630,8 @@ public Optional<JComponent> getExtra(final FieldEditor editor) {
} else if (fieldExtras.contains(FieldProperty.YES_NO)) {
return FieldExtraComponents.getYesNoExtraComponent(editor, this);
} else if (fieldExtras.contains(FieldProperty.MONTH)) {
return FieldExtraComponents.getMonthExtraComponent(editor, this, frame.getCurrentBasePanel().getBibDatabaseContext().getMode());
return FieldExtraComponents.getMonthExtraComponent(editor, this,
frame.getCurrentBasePanel().getBibDatabaseContext().getMode());
} else if (fieldExtras.contains(FieldProperty.GENDER)) {
return FieldExtraComponents.getGenderExtraComponent(editor, this);
} else if (fieldExtras.contains(FieldProperty.EDITOR_TYPE)) {
Expand Down Expand Up @@ -757,7 +758,7 @@ private void activateVisible() {
if (activeTab instanceof EntryEditorTab) {
((EntryEditorTab) activeTab).focus();
} else if (activeTab instanceof FileAnnotationTab) {
((FileAnnotationTab)activeTab).requestFocus();
((FileAnnotationTab) activeTab).requestFocus();
} else {
source.requestFocus();
}
Expand Down Expand Up @@ -1007,6 +1008,7 @@ private void unregisterListeners() {
}

private class TypeButton extends JButton {

public TypeButton() {
super(IconTheme.JabRefIcon.EDIT.getIcon());
setToolTipText(Localization.lang("Change entry type"));
Expand All @@ -1020,6 +1022,7 @@ private void showChangeEntryTypePopupMenu() {
}

private class TypeLabel extends JLabel {

public TypeLabel(String type) {
super(type);
setUI(new VerticalLabelUI(false));
Expand All @@ -1044,14 +1047,12 @@ public void mouseClicked(MouseEvent e) {
}
}


private void handleTypeChange() {
showChangeEntryTypePopupMenu();
}
});
}


@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Expand All @@ -1064,6 +1065,7 @@ public void paintComponent(Graphics g) {
* Focus listener that fires the storeFieldAction when a TextArea loses focus.
*/
private class FieldListener extends FocusAdapter {

@Override
public void focusLost(FocusEvent event) {
if (!event.isTemporary()) {
Expand All @@ -1073,13 +1075,14 @@ public void focusLost(FocusEvent event) {
}

private class TabListener implements ChangeListener {

@Override
public void stateChanged(ChangeEvent event) {
// We tell the editor tab to update all its fields. This makes sure they are updated even if the tab we
// just left contained one or more of the same fields as this one:
SwingUtilities.invokeLater(() -> {
Object activeTab = tabs.get(tabbed.getSelectedIndex());
if (activeTab instanceof FileAnnotationTab && !((FileAnnotationTab) activeTab).isInitialized()) {
if ((activeTab instanceof FileAnnotationTab) && !((FileAnnotationTab) activeTab).isInitialized()) {
//Initialize by getting notes from cache if they are cached
FileAnnotationTab.initializeTab((FileAnnotationTab) activeTab,
panel.getAnnotationCache().getFromCache(Optional.of(entry)));
Expand All @@ -1092,16 +1095,15 @@ public void stateChanged(ChangeEvent event) {
}

// When the tab "Related articles" gets selected, the request to get the recommendations is started.
if (((JTabbedPane) event.getSource()).getSelectedComponent().getName()
.equals(Localization.lang("Related articles"))) {
if (activeTab instanceof EntryEditorTabRelatedArticles) {
relatedArticlesTab.focus();
}
});
}
}


class DeleteAction extends AbstractAction {

public DeleteAction() {
super(Localization.lang("Delete"), IconTheme.JabRefIcon.DELETE_ENTRY.getIcon());
putValue(Action.SHORT_DESCRIPTION, Localization.lang("Delete entry"));
Expand Down Expand Up @@ -1144,6 +1146,7 @@ public void close() {
}

class CloseAction extends AbstractAction {

public CloseAction() {
super(Localization.lang("Close window"), IconTheme.JabRefIcon.CLOSE.getSmallIcon());
putValue(Action.SHORT_DESCRIPTION, Localization.lang("Close window"));
Expand Down Expand Up @@ -1322,6 +1325,7 @@ public void actionPerformed(ActionEvent event) {
}

class SwitchLeftAction extends AbstractAction {

public SwitchLeftAction() {
super("Switch to the panel to the left");
}
Expand All @@ -1336,6 +1340,7 @@ public void actionPerformed(ActionEvent e) {
}

class SwitchRightAction extends AbstractAction {

public SwitchRightAction() {
super("Switch to the panel to the right");
}
Expand All @@ -1350,6 +1355,7 @@ public void actionPerformed(ActionEvent e) {
}

class NextEntryAction extends AbstractAction {

public NextEntryAction() {
super(Localization.lang("Next entry"), IconTheme.JabRefIcon.DOWN.getIcon());

Expand All @@ -1363,6 +1369,7 @@ public void actionPerformed(ActionEvent e) {
}

class PrevEntryAction extends AbstractAction {

public PrevEntryAction() {
super(Localization.lang("Previous entry"), IconTheme.JabRefIcon.UP.getIcon());

Expand Down Expand Up @@ -1399,10 +1406,12 @@ public void actionPerformed(ActionEvent e) {

if (oldValue.isPresent()) {
if (Globals.prefs.getBoolean(JabRefPreferences.AVOID_OVERWRITING_KEY)) {
panel.output(Localization.lang("Not overwriting existing key. To change this setting, open Options -> Prefererences -> BibTeX key generator"));
panel.output(Localization.lang(
"Not overwriting existing key. To change this setting, open Options -> Prefererences -> BibTeX key generator"));
return;
} else if (Globals.prefs.getBoolean(JabRefPreferences.WARN_BEFORE_OVERWRITING_KEY)) {
CheckBoxMessage cbm = new CheckBoxMessage(Localization.lang("The current BibTeX key will be overwritten. Continue?"),
CheckBoxMessage cbm = new CheckBoxMessage(
Localization.lang("The current BibTeX key will be overwritten. Continue?"),
Localization.lang("Disable this confirmation dialog"), false);
int answer = JOptionPane.showConfirmDialog(frame, cbm, Localization.lang("Overwrite key"),
JOptionPane.YES_NO_OPTION);
Expand All @@ -1417,7 +1426,8 @@ public void actionPerformed(ActionEvent e) {
}

BibtexKeyPatternUtil.makeAndSetLabel(panel.getBibDatabaseContext().getMetaData()
.getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()), panel.getDatabase(), entry,
.getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()),
panel.getDatabase(), entry,
Globals.prefs.getBibtexKeyPatternPreferences());

// Store undo information:
Expand All @@ -1434,6 +1444,7 @@ public void actionPerformed(ActionEvent e) {
}

class UndoAction extends AbstractAction {

public UndoAction() {
super("Undo", IconTheme.JabRefIcon.UNDO.getIcon());
putValue(Action.SHORT_DESCRIPTION, "Undo");
Expand All @@ -1446,6 +1457,7 @@ public void actionPerformed(ActionEvent e) {
}

class RedoAction extends AbstractAction {

public RedoAction() {
super("Redo", IconTheme.JabRefIcon.REDO.getIcon());
putValue(Action.SHORT_DESCRIPTION, "Redo");
Expand All @@ -1458,6 +1470,7 @@ public void actionPerformed(ActionEvent e) {
}

class SaveDatabaseAction extends AbstractAction {

public SaveDatabaseAction() {
super("Save library");
}
Expand Down Expand Up @@ -1488,54 +1501,48 @@ private void warnDuplicateBibtexkey() {
}

private void warnEmptyBibtexkey() {
panel.output(Localization.lang("Empty BibTeX key") + ". " + Localization.lang("Grouping may not work for this entry."));
panel.output(Localization.lang("Empty BibTeX key") + ". "
+ Localization.lang("Grouping may not work for this entry."));
}


public AbstractAction getNextEntryAction() {
return nextEntryAction;
}


public AbstractAction getPrevEntryAction() {
return prevEntryAction;
}


public SwitchLeftAction getSwitchLeftAction() {
return switchLeftAction;
}


public SwitchRightAction getSwitchRightAction() {
return switchRightAction;
}


public SaveDatabaseAction getSaveDatabaseAction() {
return saveDatabaseAction;
}


public HelpAction getHelpAction() {
return helpAction;
}


public GenerateKeyAction getGenerateKeyAction() {
return generateKeyAction;
}


public StoreFieldAction getStoreFieldAction() {
return storeFieldAction;
}


private class AutoLinkAction extends AbstractAction {

public AutoLinkAction() {
putValue(Action.SMALL_ICON, IconTheme.JabRefIcon.AUTO_FILE_LINK.getIcon());
putValue(Action.SHORT_DESCRIPTION, Localization.lang("Automatically set file links for this entry") + " (Alt-F)");
putValue(Action.SHORT_DESCRIPTION,
Localization.lang("Automatically set file links for this entry") + " (Alt-F)");
}

@Override
Expand All @@ -1549,7 +1556,6 @@ public void actionPerformed(ActionEvent event) {
}
}


private boolean updateTimeStampIsSet() {
return Globals.prefs.getBoolean(JabRefPreferences.USE_TIME_STAMP)
&& Globals.prefs.getBoolean(JabRefPreferences.UPDATE_TIMESTAMP);
Expand Down

0 comments on commit 4da74e5

Please sign in to comment.