ref #7981: implement aggregation configuration wizard
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / RemotingLoginDialog.java
index ae7c71cde58e12c0ff530197db97b81d4edfae15..004a26e2f656ec53caa5b831cf24105cddc3781c 100644 (file)
@@ -17,13 +17,16 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.log4j.Logger;
 import org.eclipse.core.runtime.IProgressMonitor;
 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.jface.layout.GridDataFactory;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.SWTException;
 import org.eclipse.swt.custom.StyledText;
 import org.eclipse.swt.events.KeyAdapter;
 import org.eclipse.swt.events.KeyEvent;
@@ -58,7 +61,8 @@ import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteConfiguration;
 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
 import eu.etaxonomy.cdm.api.service.IUserService;
 import eu.etaxonomy.cdm.model.metadata.CdmMetaData;
-import eu.etaxonomy.taxeditor.Messages;
+import eu.etaxonomy.cdm.persistence.hibernate.permission.Role;
+import eu.etaxonomy.taxeditor.l10n.Messages;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
@@ -83,6 +87,10 @@ import eu.etaxonomy.taxeditor.webapp.ICDMServerError;
  */
 public class RemotingLoginDialog extends Dialog implements ICDMServerError {
 
+    private Logger logger = Logger.getLogger(getClass());
+
+    private static final String DEFAULT_PASS = "00000"; //$NON-NLS-1$
+    private static final String DEFAULT_USER = "admin"; //$NON-NLS-1$
     private static final String UBUNTU = "Ubuntu"; //$NON-NLS-1$
        protected Object result;
     protected Shell shlConnect;
@@ -101,8 +109,6 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
     private final static String STATUS_RETRIEVING = Messages.RemotingLoginDialog_STATUS_RETRIEVING;
     private final static String STATUS_CHECKING_AVAILABILITY = Messages.RemotingLoginDialog_STATUS_CHECKING;
     private final static String STATUS_NO_INSTANCES = Messages.RemotingLoginDialog_STATUS_NO_INSTANCES_FOUND;
-    private final static String STATUS_ERROR = Messages.RemotingLoginDialog_STATUS_ERROR;
-    private final static String STATUS_REMOTING_NOT_ACTIVATED = Messages.RemotingLoginDialog_STATUS_REMOTING_NOT_ACTIVATED;
     private final static String STATUS_NOT_COMPATIBLE = Messages.RemotingLoginDialog_STATUS_NOT_COMPATIBLE;
 
     private final static String MESG_COMPATIBLE_EDITOR_OLD = Messages.RemotingLoginDialog_UPDATE_EDITOR;
@@ -118,7 +124,6 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
     private final static String LAST_SERVER_KEY = "lastServerKey"; //$NON-NLS-1$
     private final static String LAST_INSTANCE_KEY = "lastInstanceKey"; //$NON-NLS-1$
 
-    private final static String REFRESH_LABEL = Messages.RemotingLoginDialog_LABEL_REFRESH;
 
     private Composite remotingComposite;
     private CdmServerInfo selectedCsii;
@@ -144,7 +149,7 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
     private final int MIN_WIDTH = 530;
     private final int MIN_HEIGHT = 220;
     private final int MIN_EXP_HEIGHT = 380;
-    private final int MESSAGE_HEIGHT = 25;
+    private final int MESSAGE_HEIGHT = 50;
     private Label lblEditorVersion;
     private Text txtEditorVersion;
     private Label lblServerCDMVersion;
@@ -205,8 +210,10 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
         Display display = getParent().getDisplay();
 
         while (!shlConnect.isDisposed()) {
-            if (!display.readAndDispatch()) {
-                display.sleep();
+            if (!display.isDisposed()){
+                if (!display.readAndDispatch()) {
+                    display.sleep();
+                }
             }
         }
 
@@ -217,7 +224,7 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
      * Create contents of the dialog.
      */
     private void createContents() {
-        shlConnect = new Shell(getParent(), SWT.DIALOG_TRIM);
+        shlConnect = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
         shlConnect.setMinimumSize(new Point(MIN_WIDTH, MIN_HEIGHT));
         shlConnect.setSize(MIN_WIDTH, MIN_HEIGHT);
         shlConnect.setText(Messages.RemotingLoginDialog_LABEL_CONNECT);
@@ -255,6 +262,7 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
         txtCdmServerStatus.setEditable(false);
         GridData gd_txtCdmServerStatus = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
         gd_txtCdmServerStatus.widthHint = 100;
+
         txtCdmServerStatus.setLayoutData(gd_txtCdmServerStatus);
 
         btnCdmServerRefresh = new Button(cdmServerComposite, SWT.NONE);
@@ -365,10 +373,10 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
         GridData gd_btnRememberMe = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
         btnRememberMe.setLayoutData(gd_btnRememberMe);
         btnRememberMe.setText(Messages.RemotingLoginDialog_LABEL_REMEMBER_ME);
-        new Label(loginComposite, SWT.NONE);
-        new Label(loginComposite, SWT.NONE);
-        new Label(loginComposite, SWT.NONE);
-        new Label(loginComposite, SWT.NONE);
+
+        Label lblDefaultLogin = new Label(loginComposite, SWT.NONE);
+        GridDataFactory.fillDefaults().span(4, 1).align(SWT.TRAIL, SWT.CENTER).applyTo(lblDefaultLogin);
+        lblDefaultLogin.setText(String.format(Messages.RemotingLoginDialog_DEFAULT_LOGIN, DEFAULT_USER, DEFAULT_PASS));
 
         styledTxtMessage = new StyledText(remotingComposite, SWT.NONE);
         styledTxtMessage.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
@@ -378,20 +386,24 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
         styledTxtMessage.setSelectionForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
         styledTxtMessage.setDoubleClickEnabled(false);
         styledTxtMessage.setEditable(false);
+        styledTxtMessage.setWordWrap(true);
+
         GridData gd_styledTxtMessage = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
         gd_styledTxtMessage.exclude = true;
         gd_styledTxtMessage.minimumHeight = MESSAGE_HEIGHT;
         gd_styledTxtMessage.heightHint = MESSAGE_HEIGHT;
+        gd_styledTxtMessage.widthHint = MIN_WIDTH - 5;
+
         styledTxtMessage.setLayoutData(gd_styledTxtMessage);
 
         xpndblcmpstAdvanced = new ExpandableComposite(remotingComposite, SWT.NONE, ExpandableComposite.TWISTIE);
         GridData gd_xpndblcmpstAdvanced = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
         gd_xpndblcmpstAdvanced.heightHint = 19;
+        gd_xpndblcmpstAdvanced.widthHint = MIN_WIDTH - 10;
         xpndblcmpstAdvanced.setLayoutData(gd_xpndblcmpstAdvanced);
         xpndblcmpstAdvanced.addExpansionListener(new IExpansionListener() {
             @Override
             public void expansionStateChanged(ExpansionEvent e) {
-                GridData gridData = (GridData) xpndblcmpstAdvanced.getLayoutData();
                 if(e.getState()) {
                     shlConnect.setSize(MIN_WIDTH, MIN_EXP_HEIGHT);
                 } else {
@@ -478,7 +490,7 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
                        startManagedServer();
                } else {
                        connect();
-                       if (CdmStore.isActive()){
+                       if (CdmStore.isActive() && CdmStore.currentAuthentiationHasOneOfRoles(Role.ROLE_REMOTING)){
                            PreferencesUtil.setNomenclaturalCodePreferences();
                        }
 
@@ -529,17 +541,22 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
 
 
     private void refreshCdmServer() {
-        txtCdmServerStatus.setText(STATUS_CHECKING_AVAILABILITY);
-        clearOnServerChange();
-        emptyCredentials();
-        updateSelectedCdmServer();
-        Display.getDefault().asyncExec(new Runnable() {
-            @Override
-            public void run() {
-                checkSelectedCdmServer();
-            }
-        });
-
+        try{
+            txtCdmServerStatus.setText(STATUS_CHECKING_AVAILABILITY);
+            clearOnServerChange();
+            emptyCredentials();
+            updateSelectedCdmServer();
+            Display.getDefault().asyncExec(new Runnable() {
+                @Override
+                public void run() {
+                    checkSelectedCdmServer();
+                }
+            });
+        }
+        catch(SWTException e){
+            //catch widget is disposed exception which may occurr if
+            //dialog is closed but the runnable tries to update
+        }
     }
 
     private void updateSelectedCdmServer() {
@@ -550,7 +567,7 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
     }
 
     private void updatePort() {
-        txtPort.setText("");
+        txtPort.setText(""); //$NON-NLS-1$
         if(selectedCsii != null) {
             int port = selectedCsii.getPort();
             if(port == CdmServerInfo.NULL_PORT) {
@@ -574,23 +591,36 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
     }
 
     private void checkSelectedCdmServer() {
-        if(selectedCsii != null) {
-            if(selectedCsii.isLocalhost()) {
-                txtPort.setEditable(true);
-                txtPort.setEnabled(true);
-            }
-            if(selectedCsii.pingServer()) {
-                txtCdmServerStatus.setText(STATUS_AVAILABLE);
-                populateCdmInstanceCombo(true);
-                String serverVersionTimestamp = generateLastModifiedTooltip(selectedCsii.getCdmlibLastModified());
-                txtServerVersion.setText(selectedCsii.getCdmlibServicesVersion() + ":" + serverVersionTimestamp); //$NON-NLS-1$
-            } else {
-                txtCdmServerStatus.setText(STATUS_NOT_AVAILABLE);
-                comboCdmInstance.removeAll();
-                disableCdmInstanceControls("", "");
+        try{
+            if(selectedCsii != null) {
+                if(selectedCsii.isLocalhost()) {
+                    txtPort.setEditable(true);
+                    txtPort.setEnabled(true);
+                }
+                try {
+                    if(selectedCsii.pingServer()) {
+                        txtCdmServerStatus.setText(STATUS_AVAILABLE);
+                        populateCdmInstanceCombo(true);
+                        String serverVersionTimestamp = generateLastModifiedTooltip(selectedCsii.getCdmlibLastModified());
+                        txtServerVersion.setText(selectedCsii.getCdmlibServicesVersion() + ":" + serverVersionTimestamp); //$NON-NLS-1$
+                    } else {
+                        txtCdmServerStatus.setText(STATUS_NOT_AVAILABLE);
+                        comboCdmInstance.removeAll();
+                        disableCdmInstanceControls("", ""); //$NON-NLS-1$ //$NON-NLS-2$
+                    }
+                } catch (CDMServerException | IOException e) {
+                    txtCdmServerStatus.setText(STATUS_NOT_AVAILABLE);
+                    MessagingUtils.warningDialog(Messages.RemotingLoginDialog_CONNECTION_FAILED_TITLE, this,
+                            Messages.RemotingLoginDialog_CONNECTION_FAILED_MESSAGE);
+                    logger.warn(Messages.RemotingLoginDialog_CONNECTION_FAILED_TITLE, e);
+                }
             }
+            updatePort();
+        }
+        catch(SWTException e){
+            //catch widget is disposed exception which may occurr if
+            //dialog is closed but the runnable tries to update
         }
-        updatePort();
     }
 
 
@@ -599,7 +629,7 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
         comboCdmInstance.setEnabled(false);
         btnConnect.setEnabled(false);
         txtCdmInstanceStatus.setText(STATUS_RETRIEVING);
-        txtCdmInstanceStatus.setToolTipText("");
+        txtCdmInstanceStatus.setToolTipText(""); //$NON-NLS-1$
 
         serverJob = new Job(Messages.RemotingLoginDialog_RETRIEVE_SERVER_INSTANCES) {
             @Override
@@ -679,7 +709,7 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
         if(selIndex != -1) {
             selectedCdmInstance = selectedCsii.getInstanceFromName(comboCdmInstance.getItem(selIndex));
 
-            if(loadLoginPrefs && !isDevRemoteSource) {
+            if(loadLoginPrefs) {
                 readPrefCredentials();
             }
         }
@@ -837,7 +867,7 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
                     int compareDbSchemaVersion = selectedCsii.compareDbSchemaVersion(selectedCdmInstance, getPort());
                     int compareCdmlibServicesVersion = 0;
                     boolean disableServicesApiTimestampCheck =
-                            PreferencesUtil.getPreferenceStore().getBoolean((IPreferenceKeys.DISABLE_SERVICES_API_TIMESTAMP_CHECK));
+                            PreferencesUtil.getBooleanValue((IPreferenceKeys.DISABLE_SERVICES_API_TIMESTAMP_CHECK));
                     if(!disableServicesApiTimestampCheck) {
                         compareCdmlibServicesVersion = selectedCsii.compareCdmlibServicesVersion();
                     }
@@ -913,6 +943,20 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
          flushPreferences(credentialsPrefs);
     }
 
+    private void removePrefCredentials() {
+        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
+        Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
+        credentialsPrefs.put(getUsernamePrefKey(), txtLogin.getText());
+        credentialsPrefs.put(getPasswordPrefKey(), txtPassword.getText());
+        removePreferences(credentialsPrefs);
+   }
+    private void removePreferences(Preferences prefs) {
+        try {
+            prefs.removeNode();
+        } catch (BackingStoreException bse) {
+            setMessage(bse.getMessage());
+        }
+    }
     private void flushPreferences(Preferences prefs) {
         try {
             prefs.flush();
@@ -921,6 +965,8 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
         }
     }
 
+
+
     private void readPrefCredentials() {
         String username, password;
         IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
@@ -943,16 +989,16 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
     }
 
     private void emptyCredentials() {
-        txtLogin.setText("");
-        txtPassword.setText("");
+        txtLogin.setText(""); //$NON-NLS-1$
+        txtPassword.setText(""); //$NON-NLS-1$
     }
 
     private String getUsernamePrefKey() {
-        return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + USERNAME_SUFFIX;
+        return selectedCsii.toString(selectedCdmInstance.getName(), isDevRemoteSource?getPort():-1) + USERNAME_SUFFIX;
     }
 
     private String getPasswordPrefKey() {
-        return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + PASSWORD_SUFFIX;
+        return selectedCsii.toString(selectedCdmInstance.getName(), isDevRemoteSource?getPort():-1) + PASSWORD_SUFFIX;
     }
 
     private boolean validateLogin(ICdmRemoteSource remoteSource) {
@@ -993,23 +1039,29 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
 
     public void setMessage(String message) {
         if(message != null && !message.isEmpty()) {
-            if(message.length() > 60) {
-                styledTxtMessage.setToolTipText(message);
-                message = message.substring(0, 60) + "..."; //$NON-NLS-1$
-            }
+//            if(message.length() > 50) {
+//                styledTxtMessage.setToolTipText(message);
+//                message = message.substring(0,50) + "..."; //$NON-NLS-1$
+//            }
             styledTxtMessage.setText(message);
             styledTxtMessage.setVisible(true);
             ((GridData)styledTxtMessage.getLayoutData()).exclude = false;
             shlConnect.setSize(MIN_WIDTH, getHeightWithoutMessage() + MESSAGE_HEIGHT);
             shlConnect.setMinimumSize(MIN_WIDTH, getHeightWithoutMessage() + MESSAGE_HEIGHT);
         } else {
-            styledTxtMessage.setText(""); //$NON-NLS-1$
-            styledTxtMessage.setVisible(false);
-            ((GridData)styledTxtMessage.getLayoutData()).exclude = true;
-            shlConnect.setSize(MIN_WIDTH, getHeightWithoutMessage());
-            shlConnect.setMinimumSize(MIN_WIDTH, getHeightWithoutMessage());
+            if (!styledTxtMessage.isDisposed()){
+                styledTxtMessage.setText(""); //$NON-NLS-1$
+                styledTxtMessage.setVisible(false);
+                ((GridData)styledTxtMessage.getLayoutData()).exclude = true;
+            }
+            if(!shlConnect.isDisposed()){
+                shlConnect.setSize(MIN_WIDTH, getHeightWithoutMessage());
+                shlConnect.setMinimumSize(MIN_WIDTH, getHeightWithoutMessage());
+            }
+        }
+        if (!remotingComposite.isDisposed()){
+            remotingComposite.layout();
         }
-        remotingComposite.layout();
     }
 
     private int getHeightWithoutMessage() {
@@ -1022,13 +1074,17 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
 
 
     public void hide(boolean isHidden) {
-        if(shlConnect != null && shlConnect.getDisplay() != null) {
-            shlConnect.setVisible(!isHidden);
+        if (!shlConnect.isDisposed()){
+            if(shlConnect != null && shlConnect.getDisplay() != null) {
+                shlConnect.setVisible(!isHidden);
+            }
         }
     }
     public void dispose() {
-        if(shlConnect != null && shlConnect.getDisplay() != null) {
-            shlConnect.dispose();
+        if (!shlConnect.isDisposed()){
+            if(shlConnect != null && shlConnect.getDisplay() != null) {
+                shlConnect.dispose();
+            }
         }
     }
 
@@ -1036,9 +1092,11 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
         Display.getDefault().asyncExec(new Runnable() {
             @Override
             public void run() {
-                if(!isDevRemoteSource) {
+                if(selectedCdmInstance!=null){
                     if(isRememberMe()) {
                         persistPrefCredentials();
+                    }else{
+                        removePrefCredentials();
                     }
                     persistPrefLastServerInstance();
                 }