Skip to content

Commit

Permalink
Refactor JabRef
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-kolb committed Apr 5, 2016
1 parent 5312d56 commit cb1a3e6
Show file tree
Hide file tree
Showing 22 changed files with 64 additions and 79 deletions.
77 changes: 31 additions & 46 deletions src/main/java/net/sf/jabref/JabRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@
* JabRef Main Class - The application gets started here.
*/
public class JabRef {

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

public static JabRefFrame jrf;
public static JabRefFrame mainFrame;

private JabRefCLI cli;

Expand Down Expand Up @@ -100,25 +99,20 @@ public void start(String[] args) {
// Check for running JabRef
RemotePreferences remotePreferences = new RemotePreferences(Globals.prefs);
if (remotePreferences.useRemoteServer()) {

Globals.REMOTE_LISTENER.open(new JabRefMessageHandler(this), remotePreferences.getPort());

if (Globals.REMOTE_LISTENER.isOpen()) {
Globals.REMOTE_LISTENER.start(); // we are alone, we start the server
} else {
if (!Globals.REMOTE_LISTENER.isOpen()) {
// we are not alone, there is already a server out there, try to contact already running JabRef:
if (RemoteListenerClient.sendToActiveJabRefInstance(args, remotePreferences.getPort())) {
/*
* We have successfully sent our command line options
* through the socket to another JabRef instance. So we
* assume it's all taken care of, and quit.
*/
System.out.println(
Localization.lang("Arguments passed on to running JabRef instance. Shutting down."));
// We have successfully sent our command line options through the socket to another JabRef instance.
// So we assume it's all taken care of, and quit.
System.out.println(Localization.lang("Arguments passed on to running JabRef instance. Shutting down."));
JabRefExecutorService.INSTANCE.shutdownEverything();
return;
}
}
// we are alone, we start the server
Globals.REMOTE_LISTENER.start();
}

// override used newline character with the one stored in the preferences
Expand All @@ -136,7 +130,6 @@ public void start(String[] args) {
}

public Optional<Vector<ParserResult>> processArguments(String[] args, boolean initialStartup) {

cli = new JabRefCLI(args);

if (!cli.isBlank() && cli.isDebugLogging()) {
Expand Down Expand Up @@ -217,7 +210,6 @@ public Optional<Vector<ParserResult>> processArguments(String[] args, boolean in
} else if (pr != ParserResult.FILE_LOCKED) {
loaded.add(pr);
}

}
}

Expand Down Expand Up @@ -266,7 +258,7 @@ public Optional<Vector<ParserResult>> processArguments(String[] args, boolean in
System.err.println(Localization.lang("Output file missing").concat(". \n \t ")
.concat(Localization.lang("Usage")).concat(": ") + JabRefCLI.getExportMatchesSyntax());
return Optional.empty();
} //end switch
}

//export new database
IExportFormat format = ExportFormats.getExportFormat(formatName);
Expand All @@ -283,12 +275,12 @@ public Optional<Vector<ParserResult>> processArguments(String[] args, boolean in
+ ex.getMessage());
}
}
} /*end if newBase != null*/ else {
} else {
System.err.println(Localization.lang("No search matches."));
}
} else {
System.err.println(Localization.lang("The output option depends on a valid input option."));
} //end if(loaded.size > 0)
}
}

if (cli.isGenerateBibtexKeys()) {
Expand Down Expand Up @@ -385,8 +377,7 @@ public Optional<Vector<ParserResult>> processArguments(String[] args, boolean in
if (!cli.isBlank() && cli.isAuxImport()) {
boolean usageMsg = false;

if (!loaded.isEmpty()) // bibtex file loaded
{
if (!loaded.isEmpty()) {
String[] data = cli.getAuxImport().split(",");

if (data.length == 2) {
Expand Down Expand Up @@ -547,7 +538,7 @@ private void setLookAndFeel() {
// Metal L&F
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
// notify the user
JOptionPane.showMessageDialog(jrf,
JOptionPane.showMessageDialog(mainFrame,
"There seem to be problems with OpenJDK and the default GTK Look&Feel. Using Metal L&F instead. Change to another L&F with caution.",
Localization.lang("Warning"), JOptionPane.WARNING_MESSAGE);
} else if (UIManager.getCrossPlatformLookAndFeelClassName().equals(lookFeel)) {
Expand All @@ -566,7 +557,7 @@ private void setLookAndFeel() {
// also set system l&f as default
Globals.prefs.put(JabRefPreferences.WIN_LOOK_AND_FEEL, systemLookFeel);
// notify the user
JOptionPane.showMessageDialog(JabRef.jrf,
JOptionPane.showMessageDialog(JabRef.mainFrame,
Localization
.lang("Unable to find the requested Look & Feel and thus the default one is used."),
Localization.lang("Warning"), JOptionPane.WARNING_MESSAGE);
Expand Down Expand Up @@ -601,8 +592,7 @@ private void setLookAndFeel() {
}

private void openWindow(List<ParserResult> loaded) {
// Perform checks and changes for users with a preference set from an older
// JabRef version.
// Perform checks and changes for users with a preference set from an older JabRef version.
PreferencesMigrations.replaceAbstractField();
PreferencesMigrations.upgradeSortOrder();
PreferencesMigrations.upgradeFaultyEncodingStrings();
Expand All @@ -620,12 +610,8 @@ private void openWindow(List<ParserResult> loaded) {
// "lcd" instead of "on" because of http://wiki.netbeans.org/FaqFontRendering and http://docs.oracle.com/javase/6/docs/technotes/guides/2d/flags.html#aaFonts
System.setProperty("awt.useSystemAAFontSettings", "lcd");

// Set the Look & Feel for Swing.
try {
setLookAndFeel();
} catch (Throwable e) {
LOGGER.error("Swing look and feel could not be loaded.", e);
}
// Look & Feel. This MUST be the first thing to do before loading any Swing-specific code!
setLookAndFeel();

// If the option is enabled, open the last edited databases, if any.
if (!cli.isBlank() && Globals.prefs.getBoolean(JabRefPreferences.OPEN_LAST_EDITED)
Expand Down Expand Up @@ -666,10 +652,9 @@ private void openWindow(List<ParserResult> loaded) {
Globals.prefs.getInt(JabRefPreferences.FONT_STYLE), Globals.prefs.getInt(JabRefPreferences.FONT_SIZE));

LOGGER.debug("Initializing frame");
JabRef.jrf = new JabRefFrame(this);
JabRef.mainFrame = new JabRefFrame(this);

// Add all loaded databases to the frame:

boolean first = true;
List<File> postponed = new ArrayList<>();
List<ParserResult> failed = new ArrayList<>();
Expand All @@ -691,7 +676,7 @@ private void openWindow(List<ParserResult> loaded) {
// add them to the list
toOpenTab.add(pr);
} else {
JabRef.jrf.addParserResult(pr, first);
JabRef.mainFrame.addParserResult(pr, first);
first = false;
}
} else {
Expand All @@ -703,39 +688,39 @@ private void openWindow(List<ParserResult> loaded) {

// finally add things to the currently opened tab
for (ParserResult pr : toOpenTab) {
JabRef.jrf.addParserResult(pr, first);
JabRef.mainFrame.addParserResult(pr, first);
first = false;
}

// Start auto save timer:
if (Globals.prefs.getBoolean(JabRefPreferences.AUTO_SAVE)) {
Globals.startAutoSaveManager(JabRef.jrf);
Globals.startAutoSaveManager(JabRef.mainFrame);
}

// If we are set to remember the window location, we also remember the maximised
// state. This needs to be set after the window has been made visible, so we
// do it here:
if (Globals.prefs.getBoolean(JabRefPreferences.WINDOW_MAXIMISED)) {
JabRef.jrf.setExtendedState(JFrame.MAXIMIZED_BOTH);
JabRef.mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}

JabRef.jrf.setVisible(true);
JabRef.mainFrame.setVisible(true);

if (Globals.prefs.getBoolean(JabRefPreferences.WINDOW_MAXIMISED)) {
JabRef.jrf.setExtendedState(JFrame.MAXIMIZED_BOTH);
JabRef.mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}

for (ParserResult pr : failed) {
String message = "<html>" + Localization.lang("Error opening file '%0'.", pr.getFile().getName()) + "<p>"
+ pr.getErrorMessage() + "</html>";

JOptionPane.showMessageDialog(JabRef.jrf, message, Localization.lang("Error opening file"),
JOptionPane.showMessageDialog(JabRef.mainFrame, message, Localization.lang("Error opening file"),
JOptionPane.ERROR_MESSAGE);
}

for (int i = 0; i < loaded.size(); i++) {
if (Globals.prefs.getBoolean(JabRefPreferences.DISPLAY_KEY_WARNING_DIALOG_AT_STARTUP)) {
ParserResultWarningDialog.showParserResultWarningDialog(loaded.get(i), JabRef.jrf, i);
ParserResultWarningDialog.showParserResultWarningDialog(loaded.get(i), mainFrame, i);
}
}

Expand All @@ -747,9 +732,9 @@ private void openWindow(List<ParserResult> loaded) {
// Note that we have to check whether i does not go over getBasePanelCount().
// This is because importToOpen might have been used, which adds to
// loaded, but not to getBasePanelCount()
for (int i = 0; (i < loaded.size()) && (i < JabRef.jrf.getBasePanelCount()); i++) {
for (int i = 0; (i < loaded.size()) && (i < JabRef.mainFrame.getBasePanelCount()); i++) {
ParserResult pr = loaded.get(i);
BasePanel panel = JabRef.jrf.getBasePanelAt(i);
BasePanel panel = JabRef.mainFrame.getBasePanelAt(i);
OpenDatabaseAction.performPostOpenActions(panel, pr, true);
}

Expand All @@ -758,12 +743,12 @@ private void openWindow(List<ParserResult> loaded) {
// If any database loading was postponed due to an autosave, schedule them
// for handing now:
if (!postponed.isEmpty()) {
AutosaveStartupPrompter asp = new AutosaveStartupPrompter(JabRef.jrf, postponed);
AutosaveStartupPrompter asp = new AutosaveStartupPrompter(JabRef.mainFrame, postponed);
SwingUtilities.invokeLater(asp);
}

if (!loaded.isEmpty()) {
new FocusRequester(JabRef.jrf.getCurrentBasePanel().mainTable);
new FocusRequester(JabRef.mainFrame.getCurrentBasePanel().mainTable);
}
}

Expand Down Expand Up @@ -835,10 +820,10 @@ private static Optional<ParserResult> importFile(String argument) {
try {
List<BibEntry> entries;
if (OS.WINDOWS) {
entries = Globals.IMPORT_FORMAT_READER.importFromFile(data[1], data[0], JabRef.jrf);
entries = Globals.IMPORT_FORMAT_READER.importFromFile(data[1], data[0], JabRef.mainFrame);
} else {
entries = Globals.IMPORT_FORMAT_READER.importFromFile(data[1],
data[0].replace("~", System.getProperty("user.home")), JabRef.jrf);
data[0].replace("~", System.getProperty("user.home")), JabRef.mainFrame);
}
return Optional.of(new ParserResult(entries));
} catch (IllegalArgumentException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public Component getTreeCellRendererComponent(JTree tree, Object value, boolean
sb.append(" [").append(((ExplicitGroup) group).getNumEntries()).append(']');
} else if ((group instanceof KeywordGroup) || (group instanceof SearchGroup)) {
int hits = 0;
BasePanel currentBasePanel = JabRef.jrf.getCurrentBasePanel();
BasePanel currentBasePanel = JabRef.mainFrame.getCurrentBasePanel();
if(currentBasePanel != null) {
for (BibEntry entry : currentBasePanel.getDatabase().getEntries()) {
if (group.contains(entry)) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/gui/DuplicateSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void run() {
askAboutExact = true;
}

DuplicateCallBack cb = new DuplicateCallBack(JabRef.jrf, be[0], be[1],
DuplicateCallBack cb = new DuplicateCallBack(JabRef.mainFrame, be[0], be[1],
askAboutExact ? DuplicateResolverType.DUPLICATE_SEARCH_WITH_EXACT : DuplicateResolverType.DUPLICATE_SEARCH);
((CallBack) Spin.over(cb)).update();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ public void mousePressed(MouseEvent e) {
if ((userObject instanceof FileNodeWrapper) && node.isLeaf()) {
FileNodeWrapper fnw = (FileNodeWrapper) userObject;
try {
JabRefDesktop.openExternalViewer(JabRef.jrf.getCurrentBasePanel().getBibDatabaseContext().getMetaData(), fnw.file.getAbsolutePath(), "pdf");
JabRefDesktop.openExternalViewer(JabRef.mainFrame.getCurrentBasePanel().getBibDatabaseContext().getMetaData(), fnw.file.getAbsolutePath(), "pdf");
} catch (IOException e1) {
LOGGER.info("Error opening file", e1);
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/net/sf/jabref/gui/actions/AutoLinkFilesAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,26 @@ public AutoLinkFilesAction() {

@Override
public void actionPerformed(ActionEvent event) {
List<BibEntry> entries = JabRef.jrf.getCurrentBasePanel().getSelectedEntries();
List<BibEntry> entries = JabRef.mainFrame.getCurrentBasePanel().getSelectedEntries();
if (entries.isEmpty()) {
JabRef.jrf.getCurrentBasePanel()
JabRef.mainFrame.getCurrentBasePanel()
.output(Localization.lang("This operation requires one or more entries to be selected."));
return;
}
JDialog diag = new JDialog(JabRef.jrf, true);
JDialog diag = new JDialog(JabRef.mainFrame, true);
final NamedCompound nc = new NamedCompound(Localization.lang("Automatically set file links"));
Runnable runnable = Util.autoSetLinks(entries, nc, null, null,
JabRef.jrf.getCurrentBasePanel().getBibDatabaseContext().getMetaData(), e -> {
JabRef.mainFrame.getCurrentBasePanel().getBibDatabaseContext().getMetaData(), e -> {
if (e.getID() > 0) {
// entry has been updated in Util.autoSetLinks, only treat nc and status message
if (nc.hasEdits()) {
nc.end();
JabRef.jrf.getCurrentBasePanel().undoManager.addEdit(nc);
JabRef.jrf.getCurrentBasePanel().markBaseChanged();
JabRef.mainFrame.getCurrentBasePanel().undoManager.addEdit(nc);
JabRef.mainFrame.getCurrentBasePanel().markBaseChanged();
}
JabRef.jrf.output(Localization.lang("Finished automatically setting external links."));
JabRef.mainFrame.output(Localization.lang("Finished automatically setting external links."));
} else {
JabRef.jrf.output(Localization.lang("Finished automatically setting external links.") + " "
JabRef.mainFrame.output(Localization.lang("Finished automatically setting external links.") + " "
+ Localization.lang("No files found."));
}
} , diag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public void keyPressed(KeyEvent arg0) {
}
});

AutoCompleter<String> autoComp = JabRef.jrf.getCurrentBasePanel().getAutoCompleters().get("keywords");
AutoCompleter<String> autoComp = JabRef.mainFrame.getCurrentBasePanel().getAutoCompleters().get("keywords");
AutoCompleteListener acl = new AutoCompleteListener(autoComp);
keyword.addKeyListener(acl);
keyword.addFocusListener(acl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class HtmlTransferable implements Transferable {
private static final int HTML = 0;
private static final int STRING = 1;

private static final DataFlavor HTML_FLAVOR = new DataFlavor("text/html;charset=utf-8;class=java.lang.String", "HTML Format"); // charset could be read via JabRef.jrf.getCurrentBasePanel().getEncoding()
private static final DataFlavor HTML_FLAVOR = new DataFlavor("text/html;charset=utf-8;class=java.lang.String", "HTML Format"); // charset could be read via JabRef.frame.getCurrentBasePanel().getEncoding()

private static final DataFlavor[] FLAVORS = {HtmlTransferable.HTML_FLAVOR, DataFlavor.stringFlavor};

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/gui/help/HelpAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void actionPerformed(ActionEvent e) {
JabRefDesktop.openBrowser("http://help.jabref.org/" + Globals.prefs.get(JabRefPreferences.LANGUAGE) + "/" + urlPart);
} catch (IOException ex) {
LOGGER.warn("Could not open browser", ex);
JabRef.jrf.getCurrentBasePanel().output(Localization.lang("Could not open browser."));
JabRef.mainFrame.getCurrentBasePanel().output(Localization.lang("Could not open browser."));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ private void processPopupTrigger(MouseEvent e, int row) {
if ((selRow == -1) || !table.isRowSelected(table.rowAtPoint(e.getPoint()))) {
table.setRowSelectionInterval(row, row);
}
RightClickMenu rightClickMenu = new RightClickMenu(JabRef.jrf, panel);
RightClickMenu rightClickMenu = new RightClickMenu(JabRef.mainFrame, panel);
rightClickMenu.show(table, e.getX(), e.getY());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void actionPerformed(ActionEvent e) {
JabRefDesktop.openBrowser(DONATION_LINK);
} catch (IOException ex) {
LOGGER.warn("Could not open browser", ex);
JabRef.jrf.getCurrentBasePanel().output(Localization.lang("Could not open browser."));
JabRef.mainFrame.getCurrentBasePanel().output(Localization.lang("Could not open browser."));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void actionPerformed(ActionEvent e) {
JabRefDesktop.openBrowser("https://github.com/JabRef/jabref");
} catch (IOException ex) {
LOGGER.warn("Could not open browser", ex);
JabRef.jrf.getCurrentBasePanel().output(Localization.lang("Could not open browser.") + " "
JabRef.mainFrame.getCurrentBasePanel().output(Localization.lang("Could not open browser.") + " "
+ Localization.lang("Please open http://github.com/JabRef/jabref manually."));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void handleMessage(String message) {

for (int i = 0; i < loaded.get().size(); i++) {
ParserResult pr = loaded.get().elementAt(i);
JabRef.jrf.addParserResult(pr, i == 0);
JabRef.mainFrame.addParserResult(pr, i == 0);
}
}
}
Loading

0 comments on commit cb1a3e6

Please sign in to comment.