import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.core.runtime.preferences.ConfigurationScope;
+import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.ui.forms.events.IExpansionListener;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.wb.swt.SWTResourceManager;
+import org.osgi.service.prefs.BackingStoreException;
+import org.osgi.service.prefs.Preferences;
import eu.etaxonomy.taxeditor.model.MessagingUtils;
import eu.etaxonomy.taxeditor.remoting.server.CDMServerException;
+import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSource;
import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo;
import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo.CdmInstanceInfo;
import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
private final Map<String, CdmServerInfo> csiiMap = new HashMap<String, CdmServerInfo>();
- private final String STATUS_AVAILABLE = "Available";
- private final String STATUS_NOT_AVAILABLE = "Not Available";
- private final String STATUS_RETRIEVING = "Retrieving ...";
- private final String STATUS_CHECKING_AVAILABILITY = "Checking ...";
- private final String STATUS_NO_INSTANCES = "No Instances Found";
- private final String STATUS_ERROR = "Error";
- private final String STATUS_REMOTING_NOT_ACTIVATED = "Remoting not activated";
+ private final static String STATUS_AVAILABLE = "Available";
+ private final static String STATUS_NOT_AVAILABLE = "Not Available";
+ private final static String STATUS_RETRIEVING = "Retrieving ...";
+ private final static String STATUS_CHECKING_AVAILABILITY = "Checking ...";
+ private final static String STATUS_NO_INSTANCES = "No Instances Found";
+ private final static String STATUS_ERROR = "Error";
+ private final static String STATUS_REMOTING_NOT_ACTIVATED = "Remoting not activated";
+ private final static String STORE_PREFERENCES_NODE = "eu.etaxonomy.taxeditor.store";
+
+ private final static String LOGIN_NODE = "login";
+ private final static String USERNAME_SUFFIX = "_username";
+ private final static String PASSWORD_SUFFIX = "_password";
+
+ private final static String LAST_SERVER_INSTANCE_NODE = "lastServerInstance";
+ private final static String LAST_SERVER_KEY = "lastServerKey";
+ private final static String LAST_INSTANCE_KEY = "lastInstanceKey";
+
private Composite remotingComposite;
private CdmServerInfo selectedCsii;
private Label lblEditorCDMVersion;
private Text txtEditorCDMVersion;
+ private String serverName, instanceName;
+ private boolean autoConnect = false;
+ private boolean loadLoginPrefs = true;
+
/**
* Create the dialog.
* @param parent
setText("Login");
}
+ public Object open(CdmRemoteSource source, boolean loadLoginPrefs, boolean autoConnect) {
+ this.loadLoginPrefs = loadLoginPrefs;
+ this.serverName = source.getName();
+ String contextPath = source.getContextPath();
+ this.instanceName = contextPath.substring(contextPath.lastIndexOf("/") + 1);
+ return open(serverName, instanceName, loadLoginPrefs, autoConnect);
+ }
+
+
+ public Object open(String serverName, String instanceName, boolean loadLoginPrefs, boolean autoConnect) {
+ this.serverName = serverName;
+ this.instanceName = instanceName;
+ this.loadLoginPrefs = loadLoginPrefs;
+ this.autoConnect = autoConnect;
+ return open();
+ }
+
/**
* Open the dialog.
* @return the result
@Override
public void widgetSelected(SelectionEvent e) {
updateSelectedCdmInstance();
- checkSelectedCdmServerInstance();
+ checkSelectedCdmInstance();
}
});
GridData gd_comboCdmInstance = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
btnConnect.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
- int selInstanceIndex = comboCdmInstance.getSelectionIndex();
- String instance = comboCdmInstance.getItem(selInstanceIndex);
- checkSelectedCdmServerInstance();
- connect(selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort()));
+ connect();
}
});
btnConnect.setText("Connect");
btnRememberMe = new Button(loginComposite, SWT.CHECK);
+ btnRememberMe.setSelection(true);
GridData gd_btnRememberMe = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
gd_btnRememberMe.widthHint = 107;
btnRememberMe.setLayoutData(gd_btnRememberMe);
- btnRememberMe.setEnabled(false);
btnRememberMe.setText("Remember Me");
new Label(loginComposite, SWT.NONE);
new Label(loginComposite, SWT.NONE);
}
+
+
private void populateCdmServerCombo() {
Job job = new Job("Retrieve Server Instances") {
@Override
protected IStatus run(IProgressMonitor monitor) {
- Display.getDefault().asyncExec(new Runnable() {
+ Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
for(CdmServerInfo csii : CdmServerInfo.getCdmServers()) {
csiiMap.put(csii.getName(), csii);
comboCdmServer.add(csii.getName());
}
- comboCdmServer.select(0);
+ int serverIndex = -1;
+ if(serverName != null) {
+ serverIndex = comboCdmServer.indexOf(serverName);
+ }
+ if(serverIndex == -1) {
+ comboCdmServer.select(0);
+ autoConnect = false;
+ } else {
+ comboCdmServer.select(serverIndex);
+ }
refreshCdmServer();
updatePort();
}
}
private void checkSelectedCdmServer() {
- int selIndex = comboCdmServer.getSelectionIndex();
+
txtCdmInstanceStatus.setText("");
txtPort.setEditable(false);
txtPort.setEnabled(false);
+ emptyCredentials();
+
if(selectedCsii != null) {
if(selectedCsii.isLocalhost()) {
txtPort.setEditable(true);
txtPort.setEnabled(true);
- } else {
- txtPort.setEditable(false);
- txtPort.setEnabled(false);
}
if(selectedCsii.pingServer()) {
txtCdmServerStatus.setText(STATUS_AVAILABLE);
comboCdmInstance.setEnabled(false);
btnConnect.setEnabled(false);
txtCdmInstanceStatus.setText(STATUS_RETRIEVING);
+
Job job = new Job("Retrieve Server Instances") {
@Override
protected IStatus run(IProgressMonitor monitor) {
for(CdmInstanceInfo cdmInstance : instances) {
comboCdmInstance.add(cdmInstance.getName());
}
- comboCdmInstance.select(0);
+ int instanceIndex = -1;
+ if(instanceName != null) {
+ instanceIndex = comboCdmInstance.indexOf(instanceName);
+ }
+ if(instanceIndex == -1) {
+ comboCdmInstance.select(0);
+ autoConnect = false;
+ } else {
+ comboCdmInstance.select(instanceIndex);
+ }
updateSelectedCdmInstance();
- checkSelectedCdmServerInstance();
+ checkSelectedCdmInstance();
comboCdmInstance.setEnabled(true);
+ if(autoConnect) {
+ connect();
+ }
} else {
txtCdmInstanceStatus.setText(STATUS_NO_INSTANCES);
private void refreshCdmInstance() {
txtCdmInstanceStatus.setText(STATUS_CHECKING_AVAILABILITY);
updateSelectedCdmInstance();
- checkSelectedCdmServerInstance();
+ checkSelectedCdmInstance();
}
private void updateSelectedCdmInstance() {
int selIndex = comboCdmInstance.getSelectionIndex();
if(selIndex != -1) {
selectedCdmInstance = selectedCsii.getInstanceFromName(comboCdmInstance.getItem(selIndex));
+ if(loadLoginPrefs) {
+ readPrefCredentials();
+ }
}
}
- private void checkSelectedCdmServerInstance() {
+ private void checkSelectedCdmInstance() {
if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE)) {
try {
if(selectedCsii.pingInstance(selectedCdmInstance, getPort())) {
}
} catch (Exception e) {
txtCdmInstanceStatus.setText(STATUS_NOT_AVAILABLE);
- //MessagingUtils.warn(this.getClass(), e);
+ txtCdmInstanceStatus.setToolTipText(e.getMessage());
}
}
}
- private void connect(ICdmRemoteSource source) {
-
+ private void connect() {
+ checkSelectedCdmInstance();
- if(!validateLogin()) {
+ if(!txtCdmInstanceStatus.getText().equals(STATUS_AVAILABLE)) {
return;
}
- if(CdmStore.isConnecting()){
- MessagingUtils.warningDialog("Already connecting", this, "You are currently connecting to a different CDM Instance already.");
+ ICdmRemoteSource source = selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort());
+
+ if(!validateLogin()) {
return;
}
+//
+// if(CdmStore.isConnecting()){
+// MessagingUtils.warningDialog("Already connecting", this, "You are currently connecting to a different CDM Instance already.");
+// return;
+// }
try {
CdmStore.connect(source, this);
}
+ public boolean isRememberMe() {
+ return btnRememberMe.getSelection();
+ }
+
+ private void persistPrefLastServerInstance() {
+ IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
+ Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
+
+ lastServerInstancePrefs.put(LAST_SERVER_KEY, selectedCsii.getName());
+ lastServerInstancePrefs.put(LAST_INSTANCE_KEY, selectedCdmInstance.getName());
+
+ flushPreferences(lastServerInstancePrefs);
+ }
+
+ private void persistPrefCredentials() {
+ IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
+ Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
+ credentialsPrefs.put(getUsernamePrefKey(), txtLogin.getText());
+ credentialsPrefs.put(getPasswordPrefKey(), txtPassword.getText());
+ flushPreferences(credentialsPrefs);
+ }
+
+ private void flushPreferences(Preferences prefs) {
+ try {
+ prefs.flush();
+ } catch (BackingStoreException bse) {
+ setMessage(bse.getMessage());
+ }
+ }
+
+ private void readPrefCredentials() {
+ IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
+ Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
+ String username = credentialsPrefs.get(getUsernamePrefKey(), "");
+ txtLogin.setText(username);
+ String password = credentialsPrefs.get(getPasswordPrefKey(), "");
+ txtPassword.setText(password);
+ if(username.isEmpty() || password.isEmpty()) {
+ autoConnect = false;
+ }
+ }
+
+ private void emptyCredentials() {
+ txtLogin.setText("");
+ txtLogin.setText("");
+ }
+
+ private String getUsernamePrefKey() {
+ return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + USERNAME_SUFFIX;
+ }
+
+ private String getPasswordPrefKey() {
+ return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + PASSWORD_SUFFIX;
+ }
+
private boolean validateLogin() {
if(getUsername() == null || getUsername().isEmpty()) {
setMessage("User login cannot be empty");
public void setMessage(String message) {
if(message != null && !message.isEmpty()) {
+ if(message.length() > 60) {
+ styledTxtMessage.setToolTipText(message);
+ message = message.substring(0, 60) + "...";
+ }
styledTxtMessage.setText(message);
styledTxtMessage.setVisible(true);
((GridData)styledTxtMessage.getLayoutData()).exclude = false;
remotingComposite.layout();
}
+
+
public void hide(boolean isHidden) {
if(shlConnect != null) {
shlConnect.setVisible(!isHidden);
shlConnect.dispose();
}
}
+
+ public void onComplete() {
+ Display.getDefault().asyncExec(new Runnable() {
+ @Override
+ public void run() {
+ if(isRememberMe()) {
+ persistPrefCredentials();
+ }
+ persistPrefLastServerInstance();
+ dispose();
+ }
+ });
+ }
}