ref #7981: implement aggregation configuration wizard
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / RemotingLoginDialog.java
index 060ac6292ca7e75c5eb71c65cdd3e402e5b58171..004a26e2f656ec53caa5b831cf24105cddc3781c 100644 (file)
@@ -17,6 +17,7 @@ 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;
@@ -60,6 +61,7 @@ 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.cdm.persistence.hibernate.permission.Role;
 import eu.etaxonomy.taxeditor.l10n.Messages;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
@@ -85,14 +87,10 @@ import eu.etaxonomy.taxeditor.webapp.ICDMServerError;
  */
 public class RemotingLoginDialog extends Dialog implements ICDMServerError {
 
-    /**
-     *
-     */
-    private static final String DEFAULT_PASS = "00000";
-    /**
-     *
-     */
-    private static final String DEFAULT_USER = "admin";
+    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;
@@ -151,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;
@@ -212,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();
+                }
             }
         }
 
@@ -224,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);
@@ -262,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);
@@ -385,15 +386,20 @@ 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
@@ -484,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();
                        }
 
@@ -591,15 +597,22 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
                     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 {
+                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);
-                    comboCdmInstance.removeAll();
-                    disableCdmInstanceControls("", "");
+                    MessagingUtils.warningDialog(Messages.RemotingLoginDialog_CONNECTION_FAILED_TITLE, this,
+                            Messages.RemotingLoginDialog_CONNECTION_FAILED_MESSAGE);
+                    logger.warn(Messages.RemotingLoginDialog_CONNECTION_FAILED_TITLE, e);
                 }
             }
             updatePort();
@@ -696,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();
             }
         }
@@ -854,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();
                     }
@@ -981,11 +994,11 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
     }
 
     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) {
@@ -1026,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() {
@@ -1055,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();
+            }
         }
     }
 
@@ -1069,11 +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();
+                        removePrefCredentials();
                     }
                     persistPrefLastServerInstance();
                 }