refactored wizard dialog and pages to allow for easy in place editing and cloning...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / datasource / wizard / CdmDataSourceMySQLWizardPage.java
index 264e7be3533e4210d9ec70b61e1d36ce0fcef572..48d8dc87b6478b2e934cfff44fb92d6deb95c912 100644 (file)
@@ -37,16 +37,30 @@ public class CdmDataSourceMySQLWizardPage extends CdmDataSourceCredentialsWizard
 
        private int port;
 
+       
+
        /**
         * <p>Constructor for CdmDataSourceMySQLWizardPage.</p>
         *
         * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
         */
        protected CdmDataSourceMySQLWizardPage(ICdmDataSource dataSource) {
-               super("MySQL");
+               super("MySQL", dataSource, CdmDataSourceWizard.Mode.CREATE);
                setTitle("MySQL Server");
                setDescription("Enter credentials for MySQL database");
-               this.setDataSource(dataSource);
+
+       }
+       
+       /**
+        * <p>Constructor for CdmDataSourceMySQLWizardPage.</p>
+        *
+        * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
+        */
+       protected CdmDataSourceMySQLWizardPage(ICdmDataSource dataSource, CdmDataSourceWizard.Mode mode) {
+               super("MySQL", dataSource, mode);
+               setTitle("MySQL Server");
+               setDescription("Enter credentials for MySQL database");
+
        }
        
        /* (non-Javadoc)
@@ -58,7 +72,7 @@ public class CdmDataSourceMySQLWizardPage extends CdmDataSourceCredentialsWizard
                // Create group composite for location data 
                locationGroup = new Group(composite, SWT.NONE);
                locationGroup.setText("Location");
-               locationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
+               locationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 2));
                GridLayout locationLayout = new GridLayout();
                locationLayout.numColumns = 2;
                locationGroup.setLayout(locationLayout);
@@ -70,7 +84,7 @@ public class CdmDataSourceMySQLWizardPage extends CdmDataSourceCredentialsWizard
                // Create host input
                text_server = new Text(locationGroup, SWT.BORDER);
                text_server.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
-               text_server.addModifyListener(this);
+               
 
                // Create port label
                Label portLabel = new Label(locationGroup, SWT.NONE);
@@ -79,7 +93,7 @@ public class CdmDataSourceMySQLWizardPage extends CdmDataSourceCredentialsWizard
                // Create port input
                text_port = new Text(locationGroup, SWT.BORDER);
                text_port.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
-               text_port.addModifyListener(this);
+               
                
        }
 
@@ -105,13 +119,25 @@ public class CdmDataSourceMySQLWizardPage extends CdmDataSourceCredentialsWizard
         */
        /** {@inheritDoc} */
        @Override
-       public void updateDataSource() {
-               setDataSource(CdmDataSource.NewMySqlInstance(server,
-                                                                                                       database,
-                                                                                                       port,
-                                                                                                       username,
-                                                                                                       password,
-                                                                                                       nomenclaturalCode));
+       public void updateDataSource() {        
+               ICdmDataSource dataSource = getDataSource();
+
+               if(dataSource == null) {
+                       setDataSource(CdmDataSource.NewMySqlInstance(server,
+                                       database,
+                                       port,
+                                       username,
+                                       password,
+                                       nomenclaturalCode));
+               } else {
+                       dataSource.setName(name);
+                       dataSource.setServer(server);
+                       dataSource.setDatabase(database);
+                       dataSource.setPort(port);
+                       dataSource.setUsername(username);
+                       dataSource.setPassword(password);
+                       dataSource.setNomenclaturalCode(nomenclaturalCode);
+               }
        }
        
        /* (non-Javadoc)
@@ -140,10 +166,25 @@ public class CdmDataSourceMySQLWizardPage extends CdmDataSourceCredentialsWizard
        /** {@inheritDoc} */
        @Override
        public void init() {
+               super.init();
                if(getDataSource() != null){
-                       modifyTextWithoutTriggeringListeners(text_server, this, getDataSource().getServer());
-                       modifyTextWithoutTriggeringListeners(text_port, this, getDataSource().getPort()+"");                    
-                       super.init();
+                       removeListeners();
+                       text_server.setText(getDataSource().getServer());
+                       text_port.setText(String.valueOf(getDataSource().getPort()));           
+                       // add listeners after setting text to avoid the modify event being called
+                       // for the initial value
+                       addListeners();
                }
+
+       }
+       
+       private void addListeners() {
+               text_server.addModifyListener(this);
+               text_port.addModifyListener(this);
+       }
+       
+       private void removeListeners() {
+               text_server.removeModifyListener(this);
+               text_port.removeModifyListener(this);
        }
 }