import java.util.ArrayList;\r
\r
import org.apache.log4j.Logger;\r
+import org.eclipse.jface.action.Action;\r
+import org.eclipse.jface.action.MenuManager;\r
import org.eclipse.jface.dialogs.Dialog;\r
import org.eclipse.jface.dialogs.IDialogConstants;\r
import org.eclipse.jface.dialogs.MessageDialog;\r
import org.eclipse.jface.viewers.IDoubleClickListener;\r
import org.eclipse.jface.viewers.IStructuredSelection;\r
import org.eclipse.jface.viewers.LabelProvider;\r
-import org.eclipse.jface.viewers.ListViewer;\r
+import org.eclipse.jface.viewers.TableViewer;\r
import org.eclipse.swt.SWT;\r
import org.eclipse.swt.custom.CCombo;\r
import org.eclipse.swt.events.MouseAdapter;\r
import org.eclipse.swt.widgets.Control;\r
import org.eclipse.swt.widgets.Group;\r
import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.Menu;\r
import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.swt.widgets.Table;\r
import org.eclipse.swt.widgets.Text;\r
\r
+import eu.etaxonomy.cdm.common.CdmUtils;\r
+import eu.etaxonomy.cdm.database.CdmPersistentDataSource;\r
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;\r
import eu.etaxonomy.cdm.database.ICdmDataSource;\r
-import eu.etaxonomy.taxeditor.TaxEditorPlugin;\r
import eu.etaxonomy.taxeditor.UiUtil;\r
/**\r
* List of database types comes from {@link eu.etaxonomy.cdm.database.DatabaseTypeEnum}.\r
\r
private Text databaseText;\r
private Text portText;\r
- private Text hostText;\r
+ private Text serverText;\r
private Text passwordText;\r
private Text databaseNameText;\r
private Text datasourceNameText;\r
private CCombo databaseTypeCombo;\r
- private Composite editDatasourceComposite;\r
+ private Text usernameText;\r
+ \r
private Composite container;\r
-\r
+ private Composite editDatasourceComposite;\r
private Group authenticationGroup;\r
private Group locationGroup;\r
- \r
- private ArrayList<DatabaseTypeEnum> databaseTypes;\r
-\r
- private ICdmDataSource dataSource = null;\r
-\r
private Button saveButton;\r
+ private Composite dbConfigComposite;\r
\r
- private Composite connectionDataComposite;\r
-\r
+ private ArrayList<DatabaseTypeEnum> databaseTypes;\r
+ private ICdmDataSource dataSource = null;\r
\r
+ private TableViewer dataSourceViewer;\r
\r
+ \r
/**\r
* Create the dialog\r
* @param parentShell\r
gridLayout.numColumns = 2;\r
container.setLayout(gridLayout);\r
\r
- // Create and style list of datasources\r
- ListViewer dataSourceViewer = new ListViewer(container, SWT.BORDER);\r
- GridData listLayout = new GridData(SWT.LEFT, SWT.FILL, false, true);\r
- listLayout.widthHint = 162;\r
- dataSourceViewer.getList().setLayoutData(listLayout);\r
+ // Create viewer for datasource list\r
+ dataSourceViewer = new TableViewer(container, SWT.BORDER);\r
+ GridData viewerLayout = new GridData(SWT.LEFT, SWT.FILL, false, true);\r
+ viewerLayout.widthHint = 162;\r
+ dataSourceViewer.getTable().setLayoutData(viewerLayout);\r
\r
- // Set providers for dataSource list\r
+ // Set providers for datasource list\r
dataSourceViewer.setContentProvider(new ArrayContentProvider());\r
dataSourceViewer.setLabelProvider(new LabelProvider() {\r
public String getText(Object element) {\r
}\r
});\r
\r
- // Populate the list with saved datasources and an entry for "new dataSource" \r
- dataSourceViewer.setInput(CdmDataSourceRepository.getDefault().getAll());\r
- dataSourceViewer.add("<< Add data source >>");\r
+ // Add data sources to viewer \r
+ refreshDataSourceViewer();\r
\r
// Double-clicking on a dataSource in the viewer puts it in the editable area\r
dataSourceViewer.addDoubleClickListener(new IDoubleClickListener() {\r
}\r
}\r
});\r
+ \r
+ // Create "remove datasource" rt-click menu\r
+ Table dataSourceControl = dataSourceViewer.getTable();\r
+ final MenuManager manager = new MenuManager();\r
+ final Menu menu = manager.createContextMenu(dataSourceControl);\r
+ dataSourceControl.setMenu(menu);\r
+ \r
+ // Add right-click menu to delete datasources\r
+ dataSourceControl.addSelectionListener(new SelectionAdapter() {\r
+ public void widgetSelected(SelectionEvent e) {\r
+ \r
+ // Get content of widget's data field\r
+ Object data = e.item.getData();\r
+ \r
+ // Clear menu\r
+ manager.removeAll();\r
+ \r
+ // Only persistent data sources can be removed -\r
+ // H2 database and "New datasource" entry remain untouched\r
+ if (data instanceof CdmPersistentDataSource) {\r
+ manager.add(new RemoveDataSourceAction((CdmPersistentDataSource) data));\r
+ }\r
+ }\r
+ });\r
\r
// Create editDatasourceComposite to display a dataSource's name and type\r
editDatasourceComposite = new Composite(container, SWT.NONE);\r
// Make editable fields visible\r
editDatasourceComposite.setVisible(true);\r
\r
+ // Remove any existing db config data\r
+ removeDbConfigFields();\r
+ \r
// Populate name and database type if this is a non-null dataSource\r
if (dataSource != null) {\r
datasourceNameText.setText(dataSource.getName());\r
} else {\r
datasourceNameText.setText("");\r
datasourceNameText.setEnabled(true);\r
- databaseTypeCombo.deselectAll();\r
- databaseTypeCombo.clearSelection();\r
+ databaseTypeCombo.setText(""); // for some reason, .deselectAll and .clearSelection don't work as advertised\r
databaseTypeCombo.setEnabled(true);\r
}\r
\r
\r
private void populateComboBoxItems() {\r
\r
- // Init DB types; if already init'ed, clear\r
+ // Init DB types\r
if (databaseTypes == null) {\r
databaseTypes = new ArrayList<DatabaseTypeEnum>();\r
- } else {\r
- databaseTypes.clear();\r
}\r
\r
// Add types to the type drop-down and to the types collection\r
protected Point getInitialSize() {\r
return new Point(500, 375);\r
}\r
+\r
+ private void removeDbConfigFields() {\r
+ // Init db config area by removing all children\r
+ if (dbConfigComposite != null) {\r
+ for (Control child : dbConfigComposite.getChildren()) {\r
+ child.dispose();\r
+ }\r
+ }\r
+ }\r
+ \r
+ private void createH2Form() {\r
\r
+ }\r
+ \r
private void createMySqlForm(final ICdmDataSource dataSource) {\r
\r
- // Dispose previous form\r
-// if (authenticationGroup != null) {\r
-// authenticationGroup.dispose();\r
-// }\r
-// if (locationGroup != null) {\r
-// locationGroup.dispose();\r
-// }\r
-// if (saveButton != null) {\r
-// saveButton.dispose();\r
-// }\r
- if (connectionDataComposite != null) {\r
- connectionDataComposite.dispose();\r
- editDatasourceComposite.layout();\r
+ // Create composite for db config as necessary\r
+ if (dbConfigComposite == null) {\r
+ dbConfigComposite = new Composite(editDatasourceComposite, SWT.NONE);\r
+ dbConfigComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));\r
+ GridLayout formLayout = new GridLayout();\r
+ formLayout.numColumns = 2;\r
+ dbConfigComposite.setLayout(formLayout);\r
}\r
- connectionDataComposite = new Composite(editDatasourceComposite, SWT.NONE);\r
- connectionDataComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));\r
- GridLayout formLayout = new GridLayout();\r
- formLayout.numColumns = 2;\r
- connectionDataComposite.setLayout(formLayout);\r
\r
// Create group composite for authentication data\r
- authenticationGroup = new Group(connectionDataComposite, SWT.NONE);\r
+ authenticationGroup = new Group(dbConfigComposite, SWT.NONE);\r
authenticationGroup.setText("Authentication");\r
authenticationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));\r
-// authenticationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));\r
GridLayout authenticationLayout = new GridLayout();\r
authenticationLayout.numColumns = 2;\r
authenticationGroup.setLayout(authenticationLayout);\r
// Create database name input\r
databaseNameText = new Text(authenticationGroup, SWT.BORDER);\r
databaseNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
+\r
+ // Create username label\r
+ Label usernameLabel = new Label(authenticationGroup, SWT.NONE);\r
+ usernameLabel.setText("User Name:");\r
+\r
+ // Create username input\r
+ usernameText = new Text(authenticationGroup, SWT.BORDER);\r
+ usernameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
\r
// Create password label\r
Label passwordLabel = new Label(authenticationGroup, SWT.NONE);\r
Button button = new Button(authenticationGroup, SWT.CHECK);\r
\r
// Create group composite for location data \r
- locationGroup = new Group(connectionDataComposite, SWT.NONE);\r
+ locationGroup = new Group(dbConfigComposite, SWT.NONE);\r
locationGroup.setText("Location");\r
locationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));\r
-// locationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));\r
GridLayout locationLayout = new GridLayout();\r
locationLayout.numColumns = 2;\r
locationGroup.setLayout(locationLayout);\r
\r
// Create host label\r
- Label hostLabel = new Label(locationGroup, SWT.NONE);\r
- hostLabel.setText("Host:");\r
+ Label serverLabel = new Label(locationGroup, SWT.NONE);\r
+ serverLabel.setText("Host:");\r
\r
// Create host input\r
- hostText = new Text(locationGroup, SWT.BORDER);\r
- hostText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
+ serverText = new Text(locationGroup, SWT.BORDER);\r
+ serverText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
\r
// Create port label\r
Label portLabel = new Label(locationGroup, SWT.NONE);\r
portText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
\r
// Create save button\r
- saveButton = new Button(connectionDataComposite, SWT.NONE);\r
+ saveButton = new Button(dbConfigComposite, SWT.NONE);\r
final GridData saveButtonLayout = new GridData(SWT.RIGHT, SWT.CENTER, false, false);\r
saveButtonLayout.widthHint = 76;\r
saveButton.setLayoutData(saveButtonLayout);\r
// Save data to datasource on mouse click\r
saveButton.addMouseListener(new MouseAdapter() {\r
public void mouseUp(MouseEvent e) {\r
- \r
- \r
+ saveDbConfig();\r
}\r
});\r
\r
// Populate fields with datasource data\r
if (dataSource != null) {\r
- databaseNameText.setText(dataSource.getDatabase());\r
+ databaseNameText.setText(CdmUtils.Nz(dataSource.getDatabase()));\r
// passwordText.setText(dataSource.getP);\r
- hostText.setText(dataSource.getServer());\r
+ serverText.setText(CdmUtils.Nz(dataSource.getServer()));\r
portText.setText(String.valueOf(dataSource.getPort())); \r
}\r
\r
// Redraw\r
- container.layout();\r
+ container.layout(true, true);\r
+ }\r
+ \r
+\r
+ private void saveDbConfig() {\r
+ \r
+ // Get field values\r
+ DatabaseTypeEnum databaseType = databaseTypes.get(databaseTypeCombo.getSelectionIndex());\r
+ String dataSourceName = datasourceNameText.getText();\r
+ String server = serverText.getText();\r
+ String database = databaseNameText.getText();\r
+ String username = usernameText.getText(); \r
+ String password = passwordText.getText();\r
+ \r
+ try {\r
+ \r
+ // Try casting port to an integer\r
+ Integer port = new Integer(portText.getText());\r
+ \r
+ logger.warn(port);\r
+ \r
+ // Save with port\r
+ CdmDataSourceRepository.getDefault().save (\r
+ dataSourceName, \r
+ databaseType, \r
+ server, \r
+ database, \r
+ port, \r
+ username, \r
+ password);\r
+ \r
+ } catch (ClassCastException e) {\r
+ \r
+ // If cast failed, save without port\r
+ CdmDataSourceRepository.getDefault().save (\r
+ dataSourceName, \r
+ databaseType, \r
+ server, \r
+ database,\r
+ username, \r
+ password);\r
+ }\r
+ \r
+ // Refresh list of data sources to reflect changes\r
+ refreshDataSourceViewer();\r
+ }\r
+ \r
+ private void refreshDataSourceViewer() {\r
+ \r
+ // Populate the list with saved datasources and an entry for "new dataSource"\r
+ dataSourceViewer.setInput(\r
+ CdmDataSourceRepository.getDefault().getAll());\r
+ dataSourceViewer.add("<< Add data source >>");\r
+ }\r
+\r
+ private class RemoveDataSourceAction extends Action {\r
+ \r
+ private CdmPersistentDataSource dataSource;\r
+\r
+ RemoveDataSourceAction(CdmPersistentDataSource dataSource) {\r
+ setText("Remove '" + dataSource.getName() + "' from saved data sources.");\r
+ this.dataSource = dataSource;\r
+ }\r
+ \r
+ public void run() {\r
+ \r
+ // Delete from persistent data sources\r
+ CdmDataSourceRepository.getDefault().delete(dataSource);\r
+ \r
+ // Refresh list of data sources to reflect changes\r
+ refreshDataSourceViewer();\r
+ }\r
}\r
}\r
import eu.etaxonomy.cdm.api.application.CdmApplicationController;\r
import eu.etaxonomy.cdm.database.CdmDataSource;\r
import eu.etaxonomy.cdm.database.CdmPersistentDataSource;\r
+import eu.etaxonomy.cdm.database.DatabaseTypeEnum;\r
import eu.etaxonomy.cdm.database.ICdmDataSource;\r
import eu.etaxonomy.cdm.model.common.init.TermNotFoundException;\r
import eu.etaxonomy.taxeditor.TaxEditorPlugin;\r
if (repository == null) {\r
repository = new CdmDataSourceRepository();\r
\r
- // Create test data sources\r
- CdmDataSource h2DataSource = CdmDataSource.NewH2EmbeddedInstance(\r
+ // Create datasource for local H2 database\r
+ ICdmDataSource h2DataSource = CdmDataSource.NewH2EmbeddedInstance(\r
"cdm", "sa", "");\r
- CdmDataSource mySqlDataSource = CdmDataSource.NewMySqlInstance(\r
- "87.106.88.177", "cdm_edit_cichorieae", 80, "edit", "R3m0teAt80");\r
- \r
- // Add test data sources to repository\r
repository.add(h2DataSource);\r
- repository.add(mySqlDataSource);\r
+ \r
+ // Add stored datasources to repository\r
+ for (ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()) {\r
+ repository.add(dataSource);\r
+ }\r
+ \r
+ // Get default datasource\r
+ ICdmDataSource defaultDataSource = CdmPersistentDataSource.NewDefaultInstance();\r
+ \r
+ // If none specified, use local H2 database \r
+ if (defaultDataSource == null) {\r
+ defaultDataSource = h2DataSource;\r
+ }\r
\r
- // Set current data source\r
-// repository.setCurrentDataSource(h2DataSource);\r
- repository.setCurrentDataSource(mySqlDataSource);\r
+ // Set repository's current datasource\r
+ repository.setCurrentDataSource(defaultDataSource);\r
+ \r
+// defaultDataSource = CdmPersistentDataSource.save("mysql_cichorieae", DatabaseTypeEnum.MySQL, "87.106.88.177", "cdm_edit_cichorieae", 80, "edit", "R3m0teAt80");\r
+// CdmDataSource mySqlDataSource = CdmDataSource.NewMySqlInstance(\r
+// "87.106.88.177", "cdm_edit_cichorieae", 80, "edit", "R3m0teAt80");\r
}\r
return repository;\r
}\r
return getDataSources().add(dataSource);\r
}\r
\r
+\r
@Override\r
- public List<ICdmDataSource> getAll() {\r
- return getDataSources();\r
+ public boolean delete(CdmPersistentDataSource dataSource) {\r
+ CdmPersistentDataSource.delete(dataSource);\r
+ return remove(dataSource);\r
}\r
\r
@Override\r
- public boolean save(CdmPersistentDataSource dataSource) {\r
- // TODO Auto-generated method stub\r
- return false;\r
+ public boolean remove(ICdmDataSource dataSource) {\r
+ return getDataSources().remove(dataSource);\r
}\r
\r
-\r
@Override\r
- public void setCdmApplicationController(\r
- CdmApplicationController cdmAppController) {\r
- this.cdmAppController = cdmAppController; \r
+ public List<ICdmDataSource> getAll() {\r
+ return getDataSources();\r
}\r
- \r
+ \r
@Override\r
public ICdmDataSource getCurrentDataSource() {\r
if (currentDataSource == null) {\r
return true;\r
}\r
\r
+ @Override\r
+ public void setCdmApplicationController(\r
+ CdmApplicationController cdmAppController) {\r
+ this.cdmAppController = cdmAppController; \r
+ }\r
+ \r
private CdmApplicationController getCdmAppController() {\r
if (cdmAppController == null) {\r
throw new IllegalStateException("CdmApplicationController not set.");\r
}\r
\r
private boolean changeDataSource(final ICdmDataSource dataSource) {\r
+\r
+ // Close all open editors before showing progress monitor\r
+ if (!UiUtil.closeOpenTaxonEditors()) {\r
+ \r
+ // User has canceled operation \r
+ return false;\r
+ }\r
+ \r
try {\r
IRunnableWithProgress op = new IRunnableWithProgress() {\r
\r
public void run(IProgressMonitor monitor)\r
throws InvocationTargetException, InterruptedException {\r
\r
+ // Start the progress bar with 25 steps\r
monitor.beginTask("Changing to a new datasource ...", 25);\r
- \r
- // Close all open editors\r
\r
// Clear all session variables\r
CdmUtil.clearLibraryObjects();\r
try {\r
getCdmAppController().changeDataSource(dataSource);\r
} catch (TermNotFoundException e) {\r
- // TODO Auto-generated catch block\r
e.printStackTrace();\r
+ throw new InterruptedException();\r
}\r
monitor.worked(10);\r
\r
}\r
return true;\r
}\r
+\r
+ @Override\r
+ public CdmPersistentDataSource save(String dataSourceName, DatabaseTypeEnum databaseType,\r
+ String server, String database, int port, String username,\r
+ String password) {\r
+ CdmPersistentDataSource dataSource = CdmPersistentDataSource.save(\r
+ dataSourceName, databaseType, server, database, port, username, password);\r
+ add(dataSource);\r
+ return dataSource;\r
+ }\r
+ \r
+ @Override\r
+ public CdmPersistentDataSource save(String dataSourceName, DatabaseTypeEnum databaseType,\r
+ String server, String database, String username, String password) {\r
+ CdmPersistentDataSource dataSource = CdmPersistentDataSource.save(\r
+ dataSourceName, databaseType, server, database, username, password);\r
+ add(dataSource);\r
+ return dataSource;\r
+ }\r
}
\ No newline at end of file