#4073 Add new project for lanuching embedded jetty server using cdmlib webapp
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / RemotingLoginDialog.java
index 37ba28d86ba7edae1b14a592515c83f391f6062a..b8cca7c84b4af373ecf93ec9a0487200c5d1f176 100644 (file)
@@ -9,19 +9,28 @@
  */
 package eu.etaxonomy.taxeditor.ui.dialog;
 
+import java.io.File;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.commons.lang.StringUtils;
 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.swt.SWT;
+import org.eclipse.swt.custom.StyledText;
 import org.eclipse.swt.events.MouseAdapter;
 import org.eclipse.swt.events.MouseEvent;
 import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.layout.FillLayout;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
@@ -33,26 +42,41 @@ import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Text;
+import org.eclipse.ui.forms.events.ExpansionEvent;
+import org.eclipse.ui.forms.events.IExpansionListener;
+import org.eclipse.ui.forms.widgets.ExpandableComposite;
+import org.eclipse.ui.progress.IProgressConstants;
 import org.eclipse.wb.swt.SWTResourceManager;
+import org.osgi.service.prefs.BackingStoreException;
+import org.osgi.service.prefs.Preferences;
 
-import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
+import eu.etaxonomy.cdm.api.application.CdmApplicationState;
+import eu.etaxonomy.cdm.model.metadata.CdmMetaData;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.remoting.server.CDMServerException;
+import eu.etaxonomy.taxeditor.remoting.server.CDMServerUtils;
+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;
 import eu.etaxonomy.taxeditor.store.CdmStore;
+import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
+import eu.etaxonomy.taxeditor.webapp.CDMEmbeddedServerException;
+import eu.etaxonomy.taxeditor.webapp.CDMServer;
+import eu.etaxonomy.taxeditor.webapp.ICDMServerError;
+
 
 /**
  * @author cmathew
  * @date 20 Jan 2015
  *
  */
-public class RemotingLoginDialog extends Dialog {
+public class RemotingLoginDialog extends Dialog implements ICDMServerError {
 
     protected Object result;
     protected Shell shlConnect;
-    private Text txtPort;
     private Text txtCdmServerStatus;
     private Text txtCdmInstanceStatus;
     private Combo comboCdmServer;
@@ -61,19 +85,69 @@ public class RemotingLoginDialog extends Dialog {
 
     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_STARTED = "Started";
+    private final static String STATUS_NOT_STARTED = "Not Started";
+    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 STATUS_NOT_COMPATIBLE = "Not Compatible";
+
+    private final static String MESG_COMPATIBLE_EDITOR_OLD = "Please update the Taxonomic Editor (Help->Check for Updates) or choose a compatible cdm-server";
+    private final static String MESG_COMPATIBLE_SERVER_OLD = "Please choose a compatible cdm-server or update the chosen cdm-server";
+
+    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 final static String REFRESH_LABEL = "Refresh";
+
+    private Composite remotingComposite;
     private CdmServerInfo selectedCsii;
     private CdmInstanceInfo selectedCdmInstance;
     private Button btnCdmServerRefresh;
+    private Button btnCdmInstanceRefresh;
+    private Button btnStopServer;
+    private Composite loginComposite;
+    private Label lblLogin;
+    private Text txtLogin;
+    private Label lblPassword;
+    private Text txtPassword;
+    private Button btnRememberMe;
+    private Composite compAdvanced;
+    private Label lblPort;
+    private Text txtPort;
+    private Label lblServerVersion;
+    private Text txtServerVersion;
+    private ExpandableComposite xpndblcmpstAdvanced;
+    private StyledText styledTxtMessage;
 
 
+    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 Label lblEditorVersion;
+    private Text txtEditorVersion;
+    private Label lblServerCDMVersion;
+    private Text txtServerCDMVersion;
+    private Label lblEditorCDMVersion;
+    private Text txtEditorCDMVersion;
+
+    private String serverName, instanceName;
+    private boolean autoConnect = false;
+    private boolean loadLoginPrefs = true;
+    private boolean isDevRemoteSource = false;
+    private Job serverJob;
     /**
      * Create the dialog.
      * @param parent
@@ -84,28 +158,49 @@ public class RemotingLoginDialog extends Dialog {
         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
      */
     public Object open() {
-        ICdmRemoteSource devRemoteSource = CdmServerInfo.getDevServerRemoteSource();
-        if(devRemoteSource != null) {
-            connect(devRemoteSource);
-        } else {
-            createContents();
-            populateCdmServerCombo();
-            shlConnect.open();
-            shlConnect.layout();
 
-            Display display = getParent().getDisplay();
+        createContents();
+        if(serverName == null && instanceName == null) {
+            readPrefLastServerInstance();
+        }
 
-            while (!shlConnect.isDisposed()) {
-                if (!display.readAndDispatch()) {
-                    display.sleep();
-                }
+        setEditorInfo();
+        populateCdmServerCombo();
+        shlConnect.open();
+        shlConnect.layout();
+
+        xpndblcmpstAdvanced.setExpanded(false);
+
+        Display display = getParent().getDisplay();
+
+        while (!shlConnect.isDisposed()) {
+            if (!display.readAndDispatch()) {
+                display.sleep();
             }
         }
+
         return result;
     }
 
@@ -113,23 +208,20 @@ public class RemotingLoginDialog extends Dialog {
      * Create contents of the dialog.
      */
     private void createContents() {
-        shlConnect = new Shell(getParent(), getStyle());
-        shlConnect.setSize(490, 157);
+        shlConnect = new Shell(getParent(), SWT.DIALOG_TRIM);
+        shlConnect.setMinimumSize(new Point(MIN_WIDTH, MIN_HEIGHT));
+        shlConnect.setSize(MIN_WIDTH, MIN_HEIGHT);
         shlConnect.setText("Connect");
         shlConnect.setLayout(new FillLayout(SWT.HORIZONTAL));
 
-        Composite composite = new Composite(shlConnect, SWT.NONE);
-        FillLayout fl_composite = new FillLayout(SWT.VERTICAL);
-        fl_composite.spacing = 5;
-        composite.setLayout(fl_composite);
-
-        Composite remotingComposite = new Composite(composite, SWT.NONE);
-        FillLayout fl_remotingComposite = new FillLayout(SWT.VERTICAL);
-        fl_remotingComposite.spacing = 5;
-        remotingComposite.setLayout(fl_remotingComposite);
+        remotingComposite = new Composite(shlConnect, SWT.NONE);
+        remotingComposite.setLayout(new GridLayout(1, false));
 
         Composite cdmServerComposite = new Composite(remotingComposite, SWT.NONE);
-        cdmServerComposite.setLayout(new GridLayout(6, false));
+        GridData gd_cdmServerComposite = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
+        gd_cdmServerComposite.heightHint = 68;
+        cdmServerComposite.setLayoutData(gd_cdmServerComposite);
+        cdmServerComposite.setLayout(new GridLayout(4, false));
 
         Label lblCdmServer = new Label(cdmServerComposite, SWT.NONE);
         lblCdmServer.setText("CDM Server : ");
@@ -141,6 +233,7 @@ public class RemotingLoginDialog extends Dialog {
             @Override
             public void widgetSelected(SelectionEvent e) {
                 refreshCdmServer();
+                updatePort();
             }
         });
         GridData gd_comboCdmServer = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
@@ -148,14 +241,6 @@ public class RemotingLoginDialog extends Dialog {
         comboCdmServer.setLayoutData(gd_comboCdmServer);
         comboCdmServer.select(0);
 
-        Label lblPortSeparator = new Label(cdmServerComposite, SWT.NONE);
-        lblPortSeparator.setText(" :  ");
-
-        txtPort = new Text(cdmServerComposite, SWT.BORDER);
-        GridData gd_txtPort = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
-        gd_txtPort.widthHint = 50;
-        txtPort.setLayoutData(gd_txtPort);
-
         txtCdmServerStatus = new Text(cdmServerComposite, SWT.BORDER);
         txtCdmServerStatus.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
         txtCdmServerStatus.setEditable(false);
@@ -184,8 +269,7 @@ public class RemotingLoginDialog extends Dialog {
         comboCdmInstance.addSelectionListener(new SelectionAdapter() {
             @Override
             public void widgetSelected(SelectionEvent e) {
-                updateSelectedCdmInstance();
-                checkSelectedCdmServerInstance();
+                refreshCdmInstance();
             }
         });
         GridData gd_comboCdmInstance = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
@@ -196,11 +280,11 @@ public class RemotingLoginDialog extends Dialog {
         txtCdmInstanceStatus = new Text(cdmServerComposite, SWT.BORDER);
         txtCdmInstanceStatus.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
         txtCdmInstanceStatus.setEditable(false);
-        GridData gd_txtCdmInstanceStatus = new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1);
+        GridData gd_txtCdmInstanceStatus = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
         gd_txtCdmInstanceStatus.widthHint = 100;
         txtCdmInstanceStatus.setLayoutData(gd_txtCdmInstanceStatus);
 
-        Button btnCdmInstanceRefresh = new Button(cdmServerComposite, SWT.FLAT);
+        btnCdmInstanceRefresh = new Button(cdmServerComposite, SWT.FLAT);
         btnCdmInstanceRefresh.addSelectionListener(new SelectionAdapter() {
             @Override
             public void widgetSelected(SelectionEvent e) {
@@ -212,42 +296,210 @@ public class RemotingLoginDialog extends Dialog {
         gd_btnCdmInstanceRefresh.heightHint = 30;
         btnCdmInstanceRefresh.setLayoutData(gd_btnCdmInstanceRefresh);
         btnCdmInstanceRefresh.setText("Refresh");
-        new Label(cdmServerComposite, SWT.NONE);
 
-        btnConnect = new Button(cdmServerComposite, SWT.FLAT);
+        loginComposite = new Composite(remotingComposite, SWT.NONE);
+        GridData gd_loginComposite = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
+        gd_loginComposite.widthHint = 487;
+        gd_loginComposite.heightHint = 70;
+        loginComposite.setLayoutData(gd_loginComposite);
+        GridLayout gl_loginComposite = new GridLayout(6, false);
+        gl_loginComposite.marginTop = 5;
+        loginComposite.setLayout(gl_loginComposite);
+
+        lblLogin = new Label(loginComposite, SWT.CENTER);
+        GridData gd_lblLogin = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
+        gd_lblLogin.widthHint = 50;
+        lblLogin.setLayoutData(gd_lblLogin);
+        lblLogin.setText("Login : ");
+        lblLogin.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
+
+        txtLogin = new Text(loginComposite, SWT.BORDER);
+        GridData gd_txtLogin = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
+        gd_txtLogin.minimumWidth = 80;
+        gd_txtLogin.widthHint = 80;
+        gd_txtLogin.heightHint = 15;
+        txtLogin.setLayoutData(gd_txtLogin);
+
+        lblPassword = new Label(loginComposite, SWT.CENTER);
+        lblPassword.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
+        lblPassword.setText("Password : ");
+        lblPassword.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
+
+        txtPassword = new Text(loginComposite, SWT.BORDER | SWT.PASSWORD);
+        GridData gd_txtPassword = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
+        gd_txtPassword.minimumWidth = 80;
+        gd_txtPassword.widthHint = 80;
+        gd_txtPassword.heightHint = 15;
+        txtPassword.setLayoutData(gd_txtPassword);
+        new Label(loginComposite, SWT.NONE);
+
+        btnConnect = new Button(loginComposite, SWT.FLAT);
+        btnConnect.setLayoutData(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));
+                if(selectedCsii.isLocalhostMgd() && !isSelectedCdmInstanceRunningInManagedServer()) {
+                    startManagedServer();
+                } else {
+                    connect();
+                }
             }
         });
-        GridData gd_btnConnect = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
-        gd_btnConnect.heightHint = 30;
-        btnConnect.setLayoutData(gd_btnConnect);
         btnConnect.setText("Connect");
-        new Label(cdmServerComposite, SWT.NONE);
-        new Label(cdmServerComposite, SWT.NONE);
-        new Label(cdmServerComposite, SWT.NONE);
-        new Label(cdmServerComposite, SWT.NONE);
+
+        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.setText("Remember Me");
+        new Label(loginComposite, SWT.NONE);
+        new Label(loginComposite, SWT.NONE);
+        new Label(loginComposite, SWT.NONE);
+        new Label(loginComposite, SWT.NONE);
+
+        styledTxtMessage = new StyledText(remotingComposite, SWT.NONE);
+        styledTxtMessage.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
+        styledTxtMessage.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
+        styledTxtMessage.setFont(SWTResourceManager.getFont("Ubuntu", 12, SWT.BOLD));
+        styledTxtMessage.setSelectionBackground(SWTResourceManager.getColor(SWT.COLOR_LIST_SELECTION_TEXT));
+        styledTxtMessage.setSelectionForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
+        styledTxtMessage.setDoubleClickEnabled(false);
+        styledTxtMessage.setEditable(false);
+        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;
+        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;
+        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 {
+                    shlConnect.setSize(MIN_WIDTH, MIN_HEIGHT);
+                }
+
+            }
+            @Override
+            public void expansionStateChanging(ExpansionEvent e) {
+            }
+        });
+        xpndblcmpstAdvanced.setText("advanced");
+        xpndblcmpstAdvanced.setExpanded(true);
+
+        compAdvanced = new Composite(xpndblcmpstAdvanced, SWT.NONE);
+        xpndblcmpstAdvanced.setClient(compAdvanced);
+        compAdvanced.setLayout(new GridLayout(4, false));
+
+        lblPort = new Label(compAdvanced, SWT.CENTER);
+        lblPort.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
+        lblPort.setSize(0, 0);
+        lblPort.setText("Port : ");
+        lblPort.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
+
+        txtPort = new Text(compAdvanced, SWT.BORDER);
+        GridData gd_txtPort = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
+        gd_txtPort.minimumWidth = 50;
+        gd_txtPort.widthHint = 50;
+        txtPort.setLayoutData(gd_txtPort);
+
+        lblServerVersion = new Label(compAdvanced, SWT.CENTER);
+        lblServerVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
+        lblServerVersion.setText("Server Cdmlib Version :");
+        lblServerVersion.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
+
+        txtServerVersion = new Text(compAdvanced, SWT.BORDER);
+        txtServerVersion.setEditable(false);
+        txtServerVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
+        btnStopServer = new Button(compAdvanced, SWT.FLAT);
+        btnStopServer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
+        btnStopServer.addMouseListener(new MouseAdapter() {
+            @Override
+            public void mouseUp(MouseEvent e) {
+                stopManagedServer();
+            }
+        });
+        btnStopServer.setText("Stop Managed Server");
+
+        lblEditorVersion = new Label(compAdvanced, SWT.CENTER);
+        lblEditorVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
+        lblEditorVersion.setText("Editor Cdmlib Version :");
+        lblEditorVersion.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
+
+        txtEditorVersion = new Text(compAdvanced, SWT.BORDER);
+        txtEditorVersion.setEditable(false);
+        txtEditorVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
+        new Label(compAdvanced, SWT.NONE);
+        new Label(compAdvanced, SWT.NONE);
+
+        lblServerCDMVersion = new Label(compAdvanced, SWT.CENTER);
+        lblServerCDMVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
+        lblServerCDMVersion.setText("Server CDM Version :");
+        lblServerCDMVersion.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
+
+        txtServerCDMVersion = new Text(compAdvanced, SWT.BORDER);
+        txtServerCDMVersion.setEditable(false);
+        txtServerCDMVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
+        new Label(compAdvanced, SWT.NONE);
+        new Label(compAdvanced, SWT.NONE);
+
+        lblEditorCDMVersion = new Label(compAdvanced, SWT.CENTER);
+        lblEditorCDMVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
+        lblEditorCDMVersion.setText("Editor CDM Version :");
+        lblEditorCDMVersion.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
+
+        txtEditorCDMVersion = new Text(compAdvanced, SWT.BORDER);
+        txtEditorCDMVersion.setEditable(false);
+        txtEditorCDMVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
 
     }
 
+
+
     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);
+                        }
+                        CdmRemoteSource devRemoteSource = CdmServerInfo.getDevServerRemoteSource();
+                        if(devRemoteSource != null) {
+                            isDevRemoteSource = true;
+                        }
                         refreshCdmServer();
+                        updatePort();
+
+                        if(devRemoteSource != null) {
+                            String username = System.getProperty("cdm.server.dev.username");
+                            String password = System.getProperty("cdm.server.dev.password");
+                            if(username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
+                                txtLogin.setText(username);
+                                txtPassword.setText(password);
+                                CdmStore.connect(devRemoteSource, RemotingLoginDialog.this);
+                            }
+                        }
                     }
                 });
                 return Status.OK_STATUS;
@@ -260,7 +512,6 @@ public class RemotingLoginDialog extends Dialog {
     private void refreshCdmServer() {
         txtCdmServerStatus.setText(STATUS_CHECKING_AVAILABILITY);
         updateSelectedCdmServer();
-        updatePort();
         checkSelectedCdmServer();
     }
 
@@ -274,17 +525,42 @@ public class RemotingLoginDialog extends Dialog {
     private void updatePort() {
         txtPort.setText("");
         if(selectedCsii != null) {
-            txtPort.setText(String.valueOf(selectedCsii.getPort()));
+            int port = selectedCsii.getPort();
+            if(port == CdmServerInfo.NULL_PORT) {
+                txtPort.setText(CdmServerInfo.NULL_PORT_STRING);
+            } else {
+                txtPort.setText(String.valueOf(port));
+            }
         }
     }
 
+    private int getPort() {
+        int port = CdmServerInfo.NULL_PORT;
+        try {
+            port = Integer.valueOf(txtPort.getText());
+        } catch (NumberFormatException nfe) {
+            if(!CdmServerInfo.NULL_PORT_STRING.equals(txtPort.getText())) {
+                setMessage("Port should be an integer");
+            }
+        }
+        return port;
+    }
+
     private void checkSelectedCdmServer() {
-        int selIndex = comboCdmServer.getSelectionIndex();
-        txtCdmInstanceStatus.setText("");
+
+        clearOnServerChange();
+        emptyCredentials();
+
         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);
             } else {
                 txtCdmServerStatus.setText(STATUS_NOT_AVAILABLE);
                 comboCdmInstance.removeAll();
@@ -298,7 +574,9 @@ public class RemotingLoginDialog extends Dialog {
         comboCdmInstance.setEnabled(false);
         btnConnect.setEnabled(false);
         txtCdmInstanceStatus.setText(STATUS_RETRIEVING);
-        Job job = new Job("Retrieve Server Instances") {
+        txtCdmInstanceStatus.setToolTipText("");
+
+        serverJob = new Job("Retrieve Server Instances") {
             @Override
             protected IStatus run(IProgressMonitor monitor) {
                 try {
@@ -314,10 +592,22 @@ public class RemotingLoginDialog extends Dialog {
                                     for(CdmInstanceInfo cdmInstance : instances) {
                                         comboCdmInstance.add(cdmInstance.getName());
                                     }
-                                    comboCdmInstance.select(0);
-                                    updateSelectedCdmInstance();
-                                    checkSelectedCdmServerInstance();
+                                    int instanceIndex = -1;
+                                    if(instanceName != null) {
+                                        instanceIndex = comboCdmInstance.indexOf(instanceName);
+                                    }
+                                    if(instanceIndex == -1) {
+                                        comboCdmInstance.select(0);
+                                        autoConnect = false;
+                                    } else {
+                                        comboCdmInstance.select(instanceIndex);
+                                    }
+                                    refreshCdmInstance();
                                     comboCdmInstance.setEnabled(true);
+                                    if(autoConnect) {
+                                        connect();
+                                    }
+
                                 } else {
                                     txtCdmInstanceStatus.setText(STATUS_NO_INSTANCES);
                                     btnConnect.setEnabled(false);
@@ -325,12 +615,13 @@ public class RemotingLoginDialog extends Dialog {
                             }
                         });
                     }
-                } catch (CDMServerException e) {
+                } catch (final CDMServerException e) {
                     MessagingUtils.warn(getClass(), e);
                     Display.getDefault().asyncExec(new Runnable() {
                         @Override
                         public void run() {
-                            txtCdmInstanceStatus.setText(STATUS_REMOTING_NOT_ACTIVATED);
+                            txtCdmInstanceStatus.setText(STATUS_NOT_AVAILABLE);
+                            txtCdmInstanceStatus.setToolTipText(e.getMessage());
                             comboCdmInstance.setEnabled(false);
                             btnConnect.setEnabled(false);
                         }
@@ -340,62 +631,445 @@ public class RemotingLoginDialog extends Dialog {
             }
         };
 
-        if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE)) {
+        if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE) && !isDevRemoteSource) {
             // Start the Job
-            job.schedule();
+            serverJob.schedule();
         }
 
+
     }
 
     private void refreshCdmInstance() {
         txtCdmInstanceStatus.setText(STATUS_CHECKING_AVAILABILITY);
+        clearOnInstanceChange();
         updateSelectedCdmInstance();
-        checkSelectedCdmServerInstance();
+        checkSelectedCdmInstance();
+        updateManagedServerControls();
     }
 
     private void updateSelectedCdmInstance() {
         int selIndex = comboCdmInstance.getSelectionIndex();
         if(selIndex != -1) {
             selectedCdmInstance = selectedCsii.getInstanceFromName(comboCdmInstance.getItem(selIndex));
+
+            if(loadLoginPrefs && !isDevRemoteSource) {
+                readPrefCredentials();
+            }
+        }
+    }
+
+    private void updateManagedServerControls() {
+        if(selectedCsii.isLocalhostMgd()) {
+            if(isSelectedCdmInstanceRunningInManagedServer()) {
+                txtCdmInstanceStatus.setText(STATUS_STARTED);
+            } else {
+                txtCdmInstanceStatus.setText(STATUS_NOT_STARTED);
+            }
+            btnConnect.setEnabled(true);
+            selectedCsii.setPort(getManagedServerPort());
+            updatePort();
+        }
+
+
+        if(isManagedServerRunning()) {
+            btnStopServer.setEnabled(true);
+        } else {
+            btnStopServer.setEnabled(false);
+        }
+    }
+
+    private boolean isManagedServerRunning() {
+        return CdmStore.getManagedServer() != null && CdmStore.getManagedServer().isAlive();
+    }
+
+    private boolean isSelectedCdmInstanceRunningInManagedServer() {
+        return CdmStore.getManagedServer() != null &&
+                CdmStore.getManagedServer().isAlive() &&
+                selectedCsii.isLocalhostMgd() &&
+                CdmStore.getManagedServer().getDataSourceName().equals(selectedCdmInstance.getName());
+    }
+
+    private boolean isActiveCdmInstanceRunningInManagedServer() {
+        return CdmStore.getManagedServer() != null &&
+                CdmStore.getManagedServer().isAlive() &&
+                CdmStore.isActive() &&
+                CdmServerInfo.NAME_LOCALHOST_MGD.equals(CdmStore.getActiveCdmSource().getName());
+    }
+
+
+    private void startManagedServer() {
+        if(isManagedServerRunning()) {
+            if(CdmStore.getManagedServer().getDataSourceName().equals(selectedCdmInstance.getName())) {
+                return;
+            } else {
+                Display.getDefault().syncExec(new Runnable() {
+                    @Override
+                    public void run() {
+                        stopManagedServer();
+                    }
+                });
+            }
         }
+
+
+        Job job = new Job("Managed CDM Server Launch") {
+
+            @Override
+            public IStatus run(IProgressMonitor monitor) {
+                String mgdServerConfigFileName = "mgd.datasources.xml";
+                String config = CDMServerUtils.convertEditorToServerConfig();
+                File managedServerConfigFile;
+                int maxUnits = 50;
+                monitor.beginTask("Launching Managed CDM Server", maxUnits);
+                try {
+                    monitor.subTask("Generating datasources config file for " + selectedCdmInstance.getName());
+                    managedServerConfigFile = CDMServerUtils.writeManagedServerConfig(config, mgdServerConfigFileName);
+                    monitor.worked(1);
+                    CdmStore.setManagedServer(new CDMServer(selectedCdmInstance.getName(), managedServerConfigFile));
+                    monitor.subTask("Starting Managed CDM Server. This may take a while.");
+                    CdmStore.getManagedServer().start(false, RemotingLoginDialog.this);
+                    int serverUnits = 0;
+
+                    // the following loop is a 'fake' progress monitoring where the progress
+                    // bar is advanced by one unit every second until maxUnits -2
+                    while(!CdmStore.getManagedServer().isStarted()) {
+                        if(serverUnits < maxUnits - 2) {
+                            try {
+                                Thread.sleep(1000);
+                            } catch (InterruptedException e) {
+                            }
+                            monitor.worked(1);
+                            serverUnits++;
+                        }
+                    }
+                    Display.getDefault().asyncExec(new Runnable() {
+                        @Override
+                        public void run() {
+                            hide(false);
+                            updateManagedServerControls();
+                            connect();
+                        }
+                    });
+                } catch (IOException ioe) {
+                    MessagingUtils.errorDialog("Error generating server config file",
+                            this,
+                            ioe.getMessage(),
+                            TaxeditorStorePlugin.PLUGIN_ID,
+                            ioe,
+                            true);
+                } catch (CDMEmbeddedServerException cse) {
+                    MessagingUtils.errorDialog("Error starting managed server",
+                            this,
+                            cse.getMessage(),
+                            TaxeditorStorePlugin.PLUGIN_ID,
+                            cse,
+                            true);
+                } finally {
+                    monitor.done();
+                }
+                return Status.OK_STATUS;
+            }
+        };
+
+        // configure the job
+        job.setProperty(IProgressConstants.KEEP_PROPERTY, true);
+        job.setUser(true);
+        // schedule job
+        hide(true);
+        job.schedule();
+    }
+
+    private void stopManagedServer() {
+        try {
+            CdmStore.getManagedServer().stop();
+        } catch (Exception e) {
+            MessagingUtils.errorDialog("Error stopping managed server",
+                    this,
+                    "Could not stop managed server running at port " + CdmStore.getManagedServer().getPort() + ". Please stop it manually",
+                    TaxeditorStorePlugin.PLUGIN_ID,
+                    e,
+                    true);
+        }
+        CdmStore.setManagedServer(null);
+        updateManagedServerControls();
+    }
+
+    private int getManagedServerPort() {
+        return CdmStore.getManagedServer() == null ? CdmServerInfo.NULL_PORT : CdmStore.getManagedServer().getPort();
     }
 
-    private void checkSelectedCdmServerInstance() {
+    private void checkSelectedCdmInstance() {
+        boolean available = false;
+        String status = STATUS_NOT_AVAILABLE;
+        String message = null;
+
         if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE)) {
             try {
-                if(selectedCsii.pingInstance(selectedCdmInstance)) {
-                    txtCdmInstanceStatus.setText(STATUS_AVAILABLE);
-                    btnConnect.setEnabled(true);
+                if(selectedCsii.pingInstance(selectedCdmInstance, getPort())) {
+                    status = STATUS_AVAILABLE;
+                    available = true;
                 } else {
-                    txtCdmInstanceStatus.setText(STATUS_NOT_AVAILABLE);
-                    btnConnect.setEnabled(false);
+                    status = STATUS_NOT_AVAILABLE;
+                    available = false;
+                }
+
+                if(available) {
+                    txtServerCDMVersion.setText(selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort()).getDbSchemaVersion());
+                    int compareDbSchemaVersion = selectedCsii.compareDbSchemaVersion(selectedCdmInstance, getPort());
+                    int compareCdmlibServicesVersion = 0;
+                    boolean disableServicesApiTimestampCheck =
+                            PreferencesUtil.getPreferenceStore().getBoolean((IPreferenceKeys.DISABLE_SERVICES_API_TIMESTAMP_CHECK));
+                    if(!disableServicesApiTimestampCheck) {
+                        compareCdmlibServicesVersion = selectedCsii.compareCdmlibServicesVersion();
+                    }
+                    if(compareDbSchemaVersion > 0 || compareCdmlibServicesVersion > 0) {
+                        status =  STATUS_NOT_COMPATIBLE;
+                        available = false;
+                        message = MESG_COMPATIBLE_EDITOR_OLD;
+                    } else if(compareDbSchemaVersion < 0 || compareCdmlibServicesVersion < 0) {
+                        status = STATUS_NOT_COMPATIBLE;
+                        available = false;
+                        message = MESG_COMPATIBLE_SERVER_OLD;
+                    } else {
+                        status =  STATUS_AVAILABLE;
+                        available = true;
+                        message = "";
+                    }
                 }
             } catch (Exception e) {
-                txtCdmInstanceStatus.setText(STATUS_NOT_AVAILABLE);
-                MessagingUtils.warn(this.getClass(), e);
+                txtCdmInstanceStatus.setToolTipText(e.getMessage());
+            } finally {
+                btnConnect.setEnabled(available);
+                txtCdmInstanceStatus.setText(status);
+                if(!StringUtils.isBlank(message)) {
+                    setMessage(message);
+                }
             }
         }
-
     }
 
-    private void connect(ICdmRemoteSource source) {
-        if(CdmStore.isConnecting()){
-            MessagingUtils.warningDialog("Already connecting", this, "You are currently connecting to a different CDM Instance already.");
+    private void connect() {
+        checkSelectedCdmInstance();
+
+        if(!txtCdmInstanceStatus.getText().equals(STATUS_AVAILABLE)) {
+            return;
+        }
+
+        ICdmRemoteSource source = selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort());
+
+        if(!validateLogin()) {
             return;
         }
 
         try {
-            CdmDataSourceRepository.changeDataSource(source);
+            CdmStore.connect(source, this);
         } catch (Exception e) {
             // Do not expect anything to go wrong at this point, so we throw a runtime exception
             // if any problems
             throw new RuntimeException(e);
         }
 
-        if(shlConnect != null) {
+    }
+
+
+    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() {
+        String username, password;
+        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
+        Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
+        username = credentialsPrefs.get(getUsernamePrefKey(), "");
+        txtLogin.setText(username);
+        password = credentialsPrefs.get(getPasswordPrefKey(),"");
+        txtPassword.setText(password);
+        if(username.isEmpty() || password.isEmpty()) {
+            autoConnect = false;
+        }
+    }
+
+    private void readPrefLastServerInstance() {
+        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
+        Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
+
+        serverName = lastServerInstancePrefs.get(LAST_SERVER_KEY, null);
+        instanceName = lastServerInstancePrefs.get(LAST_INSTANCE_KEY, null);
+    }
+
+    private void emptyCredentials() {
+        txtLogin.setText("");
+        txtPassword.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");
+            return false;
+        }
+        if(getPassword() == null || getPassword().isEmpty()) {
+            setMessage("Password cannot be empty");
+            return false;
+        }
+        return true;
+    }
+    public String getUsername() {
+        return txtLogin.getText();
+    }
+
+    public String getPassword() {
+        return txtPassword.getText();
+    }
+
+    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;
+            shlConnect.setSize(MIN_WIDTH, getHeightWithoutMessage() + MESSAGE_HEIGHT);
+            shlConnect.setMinimumSize(MIN_WIDTH, getHeightWithoutMessage() + MESSAGE_HEIGHT);
+        } else {
+            styledTxtMessage.setText("");
+            styledTxtMessage.setVisible(false);
+            ((GridData)styledTxtMessage.getLayoutData()).exclude = true;
+            shlConnect.setSize(MIN_WIDTH, getHeightWithoutMessage());
+            shlConnect.setMinimumSize(MIN_WIDTH, getHeightWithoutMessage());
+        }
+        remotingComposite.layout();
+    }
+
+    private int getHeightWithoutMessage() {
+        if(xpndblcmpstAdvanced.isExpanded()) {
+            return MIN_EXP_HEIGHT;
+        } else {
+            return MIN_HEIGHT;
+        }
+    }
+
+
+    public void hide(boolean isHidden) {
+        if(shlConnect != null && shlConnect.getDisplay() != null) {
+            shlConnect.setVisible(!isHidden);
+        }
+    }
+    public void dispose() {
+        if(shlConnect != null && shlConnect.getDisplay() != null) {
             shlConnect.dispose();
         }
     }
 
+    public void onComplete() {
+        Display.getDefault().asyncExec(new Runnable() {
+            @Override
+            public void run() {
+                if(!isDevRemoteSource) {
+                    if(isRememberMe()) {
+                        persistPrefCredentials();
+                    }
+                    persistPrefLastServerInstance();
+                }
+                dispose();
+            }
+        });
+    }
+
+    private String generateLastModifiedTooltip(String cdmlibLastModified) {
+        if(StringUtils.isBlank(cdmlibLastModified)) {
+            return "";
+        }
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.z");
+        Date cdmlibLastModifiedDate;
+        String cdmlibLastModifiedTimestamp = "";
+
+        cdmlibLastModifiedDate = new Date(Long.valueOf(cdmlibLastModified));
+        cdmlibLastModifiedTimestamp = sdf.format(cdmlibLastModifiedDate);
+
+        return cdmlibLastModifiedTimestamp;
+    }
+
+    private void setEditorInfo() {
+        txtEditorCDMVersion.setText(CdmMetaData.getDbSchemaVersion());
+        String editorVersionTimestamp = generateLastModifiedTooltip(CdmApplicationState.getCdmlibLastModified());
+        txtEditorVersion.setText(CdmApplicationState.getCdmlibVersion() + ":" + editorVersionTimestamp);
+    }
+
+    private void clearOnServerChange() {
+        setMessage("");
+        txtServerCDMVersion.setText("");
+        txtServerVersion.setText("");
+        txtServerVersion.setToolTipText("");
+        txtServerCDMVersion.setText("");
+        txtPort.setEditable(false);
+        txtPort.setEnabled(false);
+    }
+
+    private void clearOnInstanceChange() {
+        setMessage("");
+        txtServerCDMVersion.setText("");
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void handleError(final Throwable t) {
+
+        Display.getDefault().syncExec(new Runnable() {
+            @Override
+            public void run() {
+                serverJob.cancel();
+
+                String title = "CDM Server launch error";
+                String  message = t.getMessage();
+
+
+                MessagingUtils.errorDialog(title,
+                        this,
+                        message,
+                        TaxeditorStorePlugin.PLUGIN_ID,
+                        t,
+                        true);
+            }
+        });
+    }
+
 
 }