2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.navigation
;
12 import java
.util
.List
;
15 import org
.apache
.log4j
.Logger
;
16 import org
.eclipse
.jface
.action
.GroupMarker
;
17 import org
.eclipse
.jface
.action
.MenuManager
;
18 import org
.eclipse
.jface
.dialogs
.Dialog
;
19 import org
.eclipse
.swt
.SWT
;
20 import org
.eclipse
.swt
.events
.MouseAdapter
;
21 import org
.eclipse
.swt
.events
.MouseEvent
;
22 import org
.eclipse
.swt
.events
.SelectionAdapter
;
23 import org
.eclipse
.swt
.events
.SelectionEvent
;
24 import org
.eclipse
.swt
.layout
.FillLayout
;
25 import org
.eclipse
.swt
.layout
.GridData
;
26 import org
.eclipse
.swt
.layout
.GridLayout
;
27 import org
.eclipse
.swt
.widgets
.Composite
;
28 import org
.eclipse
.swt
.widgets
.Control
;
29 import org
.eclipse
.swt
.widgets
.Display
;
30 import org
.eclipse
.swt
.widgets
.Label
;
31 import org
.eclipse
.swt
.widgets
.Menu
;
32 import org
.eclipse
.swt
.widgets
.MenuItem
;
33 import org
.eclipse
.ui
.IWorkbenchActionConstants
;
34 import org
.eclipse
.ui
.forms
.IManagedForm
;
35 import org
.eclipse
.ui
.forms
.ManagedForm
;
36 import org
.eclipse
.ui
.forms
.events
.HyperlinkAdapter
;
37 import org
.eclipse
.ui
.forms
.events
.HyperlinkEvent
;
38 import org
.eclipse
.ui
.forms
.widgets
.Hyperlink
;
39 import org
.eclipse
.ui
.forms
.widgets
.TableWrapLayout
;
40 import org
.eclipse
.ui
.part
.ViewPart
;
42 import com
.swtdesigner
.ResourceManager
;
43 import com
.swtdesigner
.SWTResourceManager
;
45 import eu
.etaxonomy
.cdm
.database
.DatabaseTypeEnum
;
46 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
47 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
48 import eu
.etaxonomy
.taxeditor
.ITaxEditorConstants
;
49 import eu
.etaxonomy
.taxeditor
.TaxEditorPlugin
;
50 import eu
.etaxonomy
.taxeditor
.controller
.EditorController
;
51 import eu
.etaxonomy
.taxeditor
.controller
.GlobalController
;
52 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceDialog
;
53 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
54 import eu
.etaxonomy
.taxeditor
.model
.CdmSessionDataRepository
;
55 import eu
.etaxonomy
.taxeditor
.model
.ICdmTaxonSetListener
;
58 * The panel containing the taxonomic tree.
64 public class TaxonomicTreeView
extends ViewPart
{
65 private static final Logger logger
= Logger
.getLogger(TaxonomicTreeView
.class);
67 private Composite dbComposite
;
68 private Composite taxTreeComposite
= null;
69 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.taxonomictreeview"; //$NON-NLS-1$
71 private TaxonomicTreeViewer treeViewer
;
72 private IManagedForm hyperlinkForm
;
73 private Menu dataSourceMenu
;
75 private Label lblDataSource
;
77 private Label lblDataSourceTitle
;
79 private Composite dbCompositeSub
;
82 * Create contents of the view part
86 public void createPartControl(Composite parent
) {
88 // Create top composite
89 FillLayout fillLayout
= new FillLayout();
90 fillLayout
.marginWidth
= 0;
91 fillLayout
.marginHeight
= 0;
92 fillLayout
.type
= SWT
.VERTICAL
;
93 parent
.setLayout(fillLayout
);
95 // Create composite for DB
96 dbComposite
= new Composite(parent
, SWT
.NONE
);
97 dbComposite
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
98 GridLayout gridLayout
= new GridLayout();
99 gridLayout
.horizontalSpacing
= 0;
100 gridLayout
.marginWidth
= 0;
101 gridLayout
.marginHeight
= 0;
102 gridLayout
.verticalSpacing
= 5;
103 dbComposite
.setLayout(gridLayout
);
104 dbComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
106 dbCompositeSub
= new Composite(dbComposite
, SWT
.NULL
);
107 dbCompositeSub
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
108 dbCompositeSub
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
109 GridLayout gridLayout1
= new GridLayout();
110 gridLayout1
.numColumns
= 2;
111 dbCompositeSub
.setLayout(gridLayout1
);
113 lblDataSourceTitle
= new Label(dbCompositeSub
, SWT
.NONE
);
114 lblDataSourceTitle
.setFont(SWTResourceManager
.getFont("Arial", 14, SWT
.BOLD
));
115 lblDataSourceTitle
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
116 lblDataSourceTitle
.setAlignment(SWT
.CENTER
);
119 final Label lblDatasourceMenu
= new Label(dbCompositeSub
, SWT
.NONE
);
120 lblDatasourceMenu
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, true, false));
121 lblDatasourceMenu
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
122 lblDatasourceMenu
.setImage(ResourceManager
.getPluginImage(TaxEditorPlugin
.getDefault(), "icons/db.gif"));
123 lblDatasourceMenu
.setImage(TaxEditorPlugin
.getDefault().getImageRegistry().get(ITaxEditorConstants
.DB_ICON
));
125 // Create menu to hang from DB label
126 dataSourceMenu
= new Menu(lblDatasourceMenu
);
127 lblDatasourceMenu
.setMenu(dataSourceMenu
);
128 lblDatasourceMenu
.addMouseListener(new MouseAdapter() {
129 public void mouseDown(MouseEvent e
) {
130 dataSourceMenu
.setVisible(true);
133 createDataSourceMenu();
135 // Create string "jdbc:mysql://192. ..."
136 lblDataSource
= new Label(dbCompositeSub
, SWT
.WRAP
);
137 final GridData gd_lblDataSource
= new GridData(SWT
.LEFT
, SWT
.CENTER
, true, false, 2, 1);
138 gd_lblDataSource
.horizontalIndent
= 5;
139 lblDataSource
.setLayoutData(gd_lblDataSource
);
140 lblDataSource
.setFont(SWTResourceManager
.getFont("Arial", 8, SWT
.NONE
));
141 lblDataSource
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
145 // Create composite for taxonomic tree
146 taxTreeComposite
= new Composite(dbComposite
, SWT
.NONE
);
147 taxTreeComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
148 taxTreeComposite
.setLayout(new FillLayout());
149 taxTreeComposite
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
151 // If there are no root taxa, provide a link to create this tree's initial root taxon
152 if (CdmSessionDataRepository
.getDefault().getRootTaxa().size() == 0) {
154 hyperlinkForm
= new ManagedForm(taxTreeComposite
);
155 Composite linkBody
= hyperlinkForm
.getForm().getBody();
157 TableWrapLayout linkLayout
= new TableWrapLayout();
158 linkLayout
.leftMargin
= 20;
159 linkLayout
.rightMargin
= 20;
160 linkBody
.setLayout(linkLayout
);
161 linkBody
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
163 Hyperlink link
= hyperlinkForm
.getToolkit().createHyperlink(linkBody
,
164 "Your data source is empty. Click here to create a root taxon.", SWT
.WRAP
);
165 link
.addHyperlinkListener(new HyperlinkAdapter() {
167 public void linkActivated(HyperlinkEvent e
) {
168 EditorController
.openUnitializedRootTaxon();
179 // initializeToolBar();
187 private void setDataSourceText() {
188 ICdmDataSource dataSource
= CdmDataSourceRepository
.getDefault().getCurrentDataSource();
191 lblDataSource
.setText(dataSource
.getDatabaseType().getConnectionString(dataSource
));
192 }catch(NullPointerException e
){
193 logger
.error("Unable to set the connection string for the current dataSource. Reason:", e
);
196 lblDataSourceTitle
.setText(getDataSourceName(dataSource
));
198 dbCompositeSub
.layout();
199 dbComposite
.layout();
202 private void createDataSourceMenu() {
204 // Create entry to open data source dialog
205 new MenuItem(dataSourceMenu
, SWT
.SEPARATOR
);
206 final MenuItem newDataSourceMenuItem
= new MenuItem(dataSourceMenu
, SWT
.NONE
);
207 newDataSourceMenuItem
.setText("Edit data sources ...");
208 newDataSourceMenuItem
.addSelectionListener(new SelectionAdapter() {
209 public void widgetSelected(SelectionEvent event
) {
210 showDataSourceDialog();
214 // Add data sources to menu
215 refreshDataSources();
218 private void showDataSourceDialog() {
219 Dialog dialog
= new CdmDataSourceDialog(GlobalController
.getShell());
222 // Refresh menu to reflect any changes in data
223 refreshDataSources();
226 private void refreshDataSources() {
228 // First dispose of any entries that are already there
229 for (MenuItem menuItem
: dataSourceMenu
.getItems()) {
230 if (menuItem
.getData() instanceof ICdmDataSource
) {
235 // Get current datasource and list of all data sources
236 ICdmDataSource sessionDataSource
= CdmDataSourceRepository
.getDefault().
237 getCurrentDataSource();
238 List
<ICdmDataSource
> dataSources
=
239 CdmDataSourceRepository
.getDefault().getAll();
241 // Create menu items, putting a check next to the current one
242 for (ICdmDataSource dataSource
: dataSources
) {
244 // Always add menu item to index 0, i.e. at the beginning of the menu
245 MenuItem item
= new MenuItem(dataSourceMenu
, SWT
.CHECK
, 0);
247 // If this is the current data source, put a check by it
248 if (dataSource
.equals(sessionDataSource
)) {
249 item
.setSelection(true);
252 // Make a nice little name string out of this mutha
253 // String text = dataSource.getName();
254 // if (!dataSource.getDatabaseType().equals(DatabaseTypeEnum.H2)) {
255 // if (dataSource.getDatabase() != null) {
256 // text += " (" + dataSource.getDatabaseType().getName() + ")";
259 String text
= getDataSourceName(dataSource
);
261 item
.setData(dataSource
);
263 // On selection, make this the current data source
264 item
.addSelectionListener(new SelectionAdapter() {
265 public void widgetSelected(SelectionEvent e
) {
267 // Make sure widget has a datasource in its data field
268 if (!(e
.widget
.getData() instanceof ICdmDataSource
)) {
272 // Get the datasource from the widget's data field
273 ICdmDataSource dataSource
= (ICdmDataSource
) e
.widget
.getData();
275 // Make sure this isn't already selected
276 if (dataSource
.equals(CdmDataSourceRepository
.getDefault().getCurrentDataSource())) {
278 // Make sure it remains checked, then return
279 if (e
.widget
instanceof MenuItem
) {
280 ((MenuItem
) e
.widget
).setSelection(true);
285 // Set datasource in widget's data to current datasource
286 CdmDataSourceRepository
.getDefault().setCurrentDataSource(dataSource
);
288 // Deselect all other datasources
289 for (MenuItem item
: dataSourceMenu
.getItems()) {
290 if (item
.getData() != null && !item
.getData().equals(dataSource
)) {
291 item
.setSelection(false);
305 private String
getDataSourceName(ICdmDataSource dataSource
) {
306 String text
= dataSource
.getName();
307 if (!dataSource
.getDatabaseType().equals(DatabaseTypeEnum
.H2
)) {
308 if (dataSource
.getDatabase() != null) {
309 text
+= " (" + dataSource
.getDatabaseType().getName() + ")";
315 public TaxonomicTreeViewer
createTreeViewer() {
317 // Dispose link to new taxon as necessary
318 if (hyperlinkForm
!= null) {
319 hyperlinkForm
.getForm().dispose();
323 treeViewer
= new TaxonomicTreeViewer(taxTreeComposite
);
325 ((TaxonomicTreeViewer
) treeViewer
).createDoubleClickListener();
326 // TODO old menu implementation remove when the new one works well
327 // ((TaxonomicTreeViewer) treeViewer).createMenu();
328 ((TaxonomicTreeViewer
) treeViewer
).createQuickAdd();
329 ((TaxonomicTreeViewer
) treeViewer
).createDragAndDrop();
331 taxTreeComposite
.layout();
333 // register context menu
334 MenuManager menuMgr
= new MenuManager();
335 menuMgr
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
336 getSite().registerContextMenu(menuMgr
, treeViewer
);
338 Control control
= treeViewer
.getControl();
339 Menu menu
= menuMgr
.createContextMenu(control
);
340 control
.setMenu(menu
);
345 public void setFocus() {
350 * Expose TreeViewer, i.e. to manipulate tree nodes
353 public TaxonomicTreeViewer
getTreeViewer() {