Skip to content

Commit

Permalink
Add session restor functionality for shared database (#2014)
Browse files Browse the repository at this point in the history
- Restoration of the last shared database tab which was sucessfully connected
- Popup of the connection dialog for the case that the new connection wasn't sucessfull
- Reorganisation of DBMSConnectionProperties & OpenSharedDatabaseDialog (simplification)
  • Loading branch information
obraliar authored and koppor committed Sep 22, 2016
1 parent 0195fa2 commit e1b2aa3
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
## [Unreleased]

### Changed
- Add session restoring functionality for shared database. Related to [#1703](https://github.com/JabRef/jabref/issues/1703)
- Implementation of LiveUpdate for PostgreSQL & Oracle systems. Related to [#970](https://github.com/JabRef/jabref/issues/970).
- Added fetcher for [MathSciNet](http://www.ams.org/mathscinet), [zbMATH](https://www.zbmath.org/) and [Astrophysics Data System](http://www.adsabs.harvard.edu/)
- Improved search:
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/net/sf/jabref/JabRefGUI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.sf.jabref;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
Expand All @@ -21,6 +22,8 @@
import net.sf.jabref.gui.importer.ParserResultWarningDialog;
import net.sf.jabref.gui.importer.actions.OpenDatabaseAction;
import net.sf.jabref.gui.importer.worker.AutosaveStartupPrompter;
import net.sf.jabref.gui.shared.OpenSharedDatabaseDialog;
import net.sf.jabref.gui.shared.SharedDatabaseUIManager;
import net.sf.jabref.gui.worker.VersionWorker;
import net.sf.jabref.logic.importer.OpenDatabase;
import net.sf.jabref.logic.importer.ParserResult;
Expand All @@ -30,6 +33,7 @@
import net.sf.jabref.migrations.PreferencesMigrations;
import net.sf.jabref.preferences.JabRefPreferences;
import net.sf.jabref.preferences.VersionPreferences;
import net.sf.jabref.shared.exception.DatabaseNotSupportedException;

import com.jgoodies.looks.plastic.Plastic3DLookAndFeel;
import com.jgoodies.looks.plastic.theme.SkyBluer;
Expand Down Expand Up @@ -192,6 +196,19 @@ private void openWindow() {
if (!bibDatabases.isEmpty()) {
JabRefGUI.getMainFrame().getCurrentBasePanel().getMainTable().requestFocus();
}

boolean isSharedDatabaseEdited = Globals.prefs.getBoolean(JabRefPreferences.SHARED_DATABASE_LAST_EDITED);
if (isSharedDatabaseEdited) {
boolean isFocused = Globals.prefs.getBoolean(JabRefPreferences.SHARED_DATABASE_LAST_FOCUSED);
String keywordSeparator = Globals.prefs.get(JabRefPreferences.KEYWORD_SEPARATOR);

try {
new SharedDatabaseUIManager(mainFrame, keywordSeparator).openLastSharedDatabaseTab(isFocused);
} catch (SQLException | DatabaseNotSupportedException e) {
LOGGER.info("Failed to restore shared database. Use connection dialog to connect.");
new OpenSharedDatabaseDialog(mainFrame).setVisible(true);
}
}
}

private void openLastEditedDatabases() {
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/net/sf/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ public void windowClosing(WindowEvent e) {
} else {
String content = "";
SearchQuery currentSearchQuery = currentBasePanel.getCurrentSearchQuery();
if (currentSearchQuery != null && !currentSearchQuery.getQuery().trim().isEmpty()) {
if ((currentSearchQuery != null) && !currentSearchQuery.getQuery().trim().isEmpty()) {
content = currentSearchQuery.getQuery();
}
globalSearchBar.setSearchTerm(content, true);
Expand Down Expand Up @@ -789,6 +789,8 @@ private void tearDownJabRef(List<String> filenames) {
prefs.putStringList(JabRefPreferences.LAST_EDITED, filenames);
File focusedDatabase = getCurrentBasePanel().getBibDatabaseContext().getDatabaseFile().orElse(null);
new LastFocusedTabPreferences(prefs).setLastFocusedTab(focusedDatabase);
prefs.putBoolean(JabRefPreferences.SHARED_DATABASE_LAST_FOCUSED,
getCurrentBasePanel().getBibDatabaseContext().getLocation() == DatabaseLocation.SHARED);
}

}
Expand Down Expand Up @@ -828,6 +830,9 @@ public boolean quit() {
// Ask here if the user really wants to close, if the base
// has not been saved since last save.
boolean close = true;

prefs.putBoolean(JabRefPreferences.SHARED_DATABASE_LAST_EDITED, Boolean.FALSE);

List<String> filenames = new ArrayList<>();
if (tabbedPane.getTabCount() > 0) {
for (int i = 0; i < tabbedPane.getTabCount(); i++) {
Expand Down Expand Up @@ -862,6 +867,7 @@ public boolean quit() {
}
}
} else if (context.getLocation() == DatabaseLocation.SHARED) {
prefs.putBoolean(JabRefPreferences.SHARED_DATABASE_LAST_EDITED, Boolean.TRUE);
context.convertToLocalDatabase();
context.getDBMSSynchronizer().closeSharedDatabase();
context.clearDBMSSynchronizer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@
import net.sf.jabref.gui.help.HelpAction;
import net.sf.jabref.logic.help.HelpFile;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.Defaults;
import net.sf.jabref.model.database.BibDatabaseContext;
import net.sf.jabref.model.database.BibDatabaseMode;
import net.sf.jabref.model.database.DatabaseLocation;
import net.sf.jabref.preferences.JabRefPreferences;
import net.sf.jabref.shared.DBMSConnection;
import net.sf.jabref.shared.DBMSConnectionProperties;
Expand Down Expand Up @@ -83,7 +79,6 @@ public class OpenSharedDatabaseDialog extends JDialog {
private final SharedDatabasePreferences prefs = new SharedDatabasePreferences();

private DBMSConnectionProperties connectionProperties;
private BibDatabaseContext bibDatabaseContext;

/**
* @param frame the JabRef Frame
Expand All @@ -102,13 +97,8 @@ public void openSharedDatabase() {
setLoadingConnectButtonText(true);

try {
bibDatabaseContext.getDBMSSynchronizer().openSharedDatabase(connectionProperties);
frame.addTab(bibDatabaseContext, true);
new SharedDatabaseUIManager(frame, Globals.prefs.get(JabRefPreferences.KEYWORD_SEPARATOR)).openNewSharedDatabaseTab(connectionProperties);
setPreferences();
bibDatabaseContext.getDBMSSynchronizer()
.registerListener(
new SharedDatabaseUIManager(frame, Globals.prefs.get(JabRefPreferences.KEYWORD_SEPARATOR)));
frame.output(Localization.lang("Connection_to_%0_server_established.", connectionProperties.getType().toString()));
dispose();
return; // setLoadingConnectButtonText(false) should not be reached regularly.
} catch (SQLException exception) {
Expand All @@ -131,10 +121,6 @@ private void setupActions() {
public void actionPerformed(ActionEvent e) {
try {
checkFields();
BibDatabaseMode selectedMode = Globals.prefs.getDefaultBibDatabaseMode();

bibDatabaseContext = new BibDatabaseContext(new Defaults(selectedMode),
DatabaseLocation.SHARED, Globals.prefs.get(JabRefPreferences.KEYWORD_SEPARATOR));

connectionProperties = new DBMSConnectionProperties();
connectionProperties.setType((DBMSType) dbmsTypeDropDown.getSelectedItem());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
package net.sf.jabref.gui.shared;

import java.sql.SQLException;

import java.util.Objects;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import net.sf.jabref.Globals;
import net.sf.jabref.JabRefGUI;
import net.sf.jabref.gui.BasePanel;
import net.sf.jabref.gui.JabRefFrame;
import net.sf.jabref.gui.entryeditor.EntryEditor;
import net.sf.jabref.gui.undo.UndoableRemoveEntry;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.Defaults;
import net.sf.jabref.model.database.BibDatabaseContext;
import net.sf.jabref.model.database.BibDatabaseMode;
import net.sf.jabref.model.database.DatabaseLocation;
import net.sf.jabref.shared.DBMSConnectionProperties;
import net.sf.jabref.shared.DBMSSynchronizer;
import net.sf.jabref.shared.event.ConnectionLostEvent;
import net.sf.jabref.shared.event.SharedEntryNotPresentEvent;
import net.sf.jabref.shared.event.UpdateRefusedEvent;
import net.sf.jabref.shared.exception.DatabaseNotSupportedException;
import net.sf.jabref.shared.prefs.SharedDatabasePreferences;

import com.google.common.eventbus.Subscribe;

public class SharedDatabaseUIManager {

private final JabRefFrame jabRefFrame;
private final DBMSSynchronizer dbmsSynchronizer;
private final String keywordSeparator;
private DBMSSynchronizer dbmsSynchronizer;

public SharedDatabaseUIManager(JabRefFrame jabRefFrame, String keywordSeparator) {
this.jabRefFrame = jabRefFrame;
this.dbmsSynchronizer = jabRefFrame.getCurrentBasePanel().getBibDatabaseContext().getDBMSSynchronizer();
this.keywordSeparator = keywordSeparator;
}

Expand Down Expand Up @@ -82,4 +92,44 @@ public void listen(SharedEntryNotPresentEvent event) {
SwingUtilities.invokeLater(() -> panel.hideBottomComponent());
}
}

/**
* Opens a new shared database tab with the given {@link DBMSConnectionProperties}.
*
* @param dbmsConnectionProperties Connection data
* @param raiseTab If <code>true</code> the new tab gets selected.
*/
private void openNewSharedDatabaseTab(DBMSConnectionProperties dbmsConnectionProperties, boolean raiseTab)
throws SQLException, DatabaseNotSupportedException {
JabRefFrame frame = JabRefGUI.getMainFrame();
BibDatabaseMode selectedMode = Globals.prefs.getDefaultBibDatabaseMode();
BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(new Defaults(selectedMode), DatabaseLocation.SHARED,
keywordSeparator);

dbmsSynchronizer = bibDatabaseContext.getDBMSSynchronizer();
dbmsSynchronizer.openSharedDatabase(dbmsConnectionProperties);
frame.addTab(bibDatabaseContext, raiseTab);
dbmsSynchronizer.registerListener(this);
frame.output(Localization.lang("Connection_to_%0_server_established.", dbmsConnectionProperties.getType().toString()));
}

/**
* Opens a new shared database tab with the given {@link DBMSConnectionProperties}.
*
* @param dbmsConnectionProperties Connection data
*/
public void openNewSharedDatabaseTab(DBMSConnectionProperties dbmsConnectionProperties)
throws SQLException, DatabaseNotSupportedException {
openNewSharedDatabaseTab(dbmsConnectionProperties, true);
}

/**
* Opens a new shared database tab with the already set {@link DBMSConnectionProperties}.
*
* @param raiseTab If <code>true</code> the new tab gets selected.
*/
public void openLastSharedDatabaseTab(boolean raiseTab)
throws SQLException, DatabaseNotSupportedException {
openNewSharedDatabaseTab(new DBMSConnectionProperties(new SharedDatabasePreferences()), raiseTab);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ public class JabRefPreferences {
public static final String LAST_EDITED = "lastEdited";
public static final String OPEN_LAST_EDITED = "openLastEdited";
public static final String LAST_FOCUSED = "lastFocused";
public static final String SHARED_DATABASE_LAST_EDITED = "sharedDatabaseLastEdited";
public static final String SHARED_DATABASE_LAST_FOCUSED = "sharedDatabaseLastFocused";
public static final String BACKUP = "backup";
public static final String AUTO_OPEN_FORM = "autoOpenForm";
public static final String FILE_WORKING_DIRECTORY = "fileWorkingDirectory";
Expand Down Expand Up @@ -599,6 +601,8 @@ private JabRefPreferences() {
defaults.put(BACKUP, Boolean.TRUE);
defaults.put(OPEN_LAST_EDITED, Boolean.TRUE);
defaults.put(LAST_EDITED, "");
defaults.put(SHARED_DATABASE_LAST_EDITED, Boolean.FALSE);
defaults.put(SHARED_DATABASE_LAST_FOCUSED, Boolean.FALSE);
defaults.put(LAST_FOCUSED, "");
defaults.put(STRINGS_POS_X, 0);
defaults.put(STRINGS_POS_Y, 0);
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/net/sf/jabref/shared/DBMSConnectionProperties.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
package net.sf.jabref.shared;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Optional;

import net.sf.jabref.shared.prefs.SharedDatabasePreferences;
import net.sf.jabref.shared.security.Password;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* Keeps all essential data for establishing a new connection to a DBMS using {@link DBMSConnector}.
*/
public class DBMSConnectionProperties {

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

private DBMSType type;
private String host;
private int port;
Expand All @@ -17,6 +29,10 @@ public DBMSConnectionProperties() {
// no data
}

public DBMSConnectionProperties(SharedDatabasePreferences prefs) {
setFromPreferences(prefs);
}

public DBMSConnectionProperties(DBMSType type, String host, int port, String database, String user,
String password) {
this.type = type;
Expand Down Expand Up @@ -75,4 +91,35 @@ public void setPassword(String password) {
this.password = password;
}

/**
* Gets all required data from {@link SharedDatabasePreferences} and sets them if present.
*/
private void setFromPreferences(SharedDatabasePreferences prefs) {
if (prefs.getType().isPresent()) {
Optional<DBMSType> dbmsType = DBMSType.fromString(prefs.getType().get());
if (dbmsType.isPresent()) {
this.type = dbmsType.get();
}
}

prefs.getHost().ifPresent(theHost -> this.host = theHost);
prefs.getPort().ifPresent(thePort -> this.port = Integer.parseInt(thePort));
prefs.getName().ifPresent(theDatabase -> this.database = theDatabase);

if (prefs.getUser().isPresent()) {
this.user = prefs.getUser().get();
if (prefs.getPassword().isPresent()) {
try {
this.password = new Password(prefs.getPassword().get().toCharArray(), prefs.getUser().get()).decrypt();
} catch (UnsupportedEncodingException | GeneralSecurityException e) {
LOGGER.error("Could not decrypt password", e);
}
}
}

if (!prefs.getPassword().isPresent()) {
// Some DBMS require a non-null value as a password (in case of using an empty string).
this.password = "";
}
}
}

0 comments on commit e1b2aa3

Please # to comment.