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
;
14 import org
.apache
.log4j
.Logger
;
15 import org
.eclipse
.jface
.action
.IMenuManager
;
16 import org
.eclipse
.jface
.action
.IToolBarManager
;
17 import org
.eclipse
.jface
.dialogs
.Dialog
;
18 import org
.eclipse
.swt
.SWT
;
19 import org
.eclipse
.swt
.events
.SelectionAdapter
;
20 import org
.eclipse
.swt
.events
.SelectionEvent
;
21 import org
.eclipse
.swt
.layout
.FillLayout
;
22 import org
.eclipse
.swt
.layout
.GridData
;
23 import org
.eclipse
.swt
.layout
.GridLayout
;
24 import org
.eclipse
.swt
.widgets
.Composite
;
25 import org
.eclipse
.swt
.widgets
.Display
;
26 import org
.eclipse
.swt
.widgets
.Label
;
27 import org
.eclipse
.swt
.widgets
.Menu
;
28 import org
.eclipse
.swt
.widgets
.MenuItem
;
29 import org
.eclipse
.ui
.forms
.IManagedForm
;
30 import org
.eclipse
.ui
.forms
.ManagedForm
;
31 import org
.eclipse
.ui
.forms
.events
.HyperlinkAdapter
;
32 import org
.eclipse
.ui
.forms
.events
.HyperlinkEvent
;
33 import org
.eclipse
.ui
.forms
.widgets
.Hyperlink
;
34 import org
.eclipse
.ui
.forms
.widgets
.TableWrapLayout
;
35 import org
.eclipse
.ui
.part
.ViewPart
;
37 import com
.swtdesigner
.ResourceManager
;
38 import com
.swtdesigner
.SWTResourceManager
;
40 import eu
.etaxonomy
.cdm
.database
.DatabaseTypeEnum
;
41 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
42 import eu
.etaxonomy
.taxeditor
.ITaxEditorConstants
;
43 import eu
.etaxonomy
.taxeditor
.TaxEditorPlugin
;
44 import eu
.etaxonomy
.taxeditor
.UiUtil
;
45 import eu
.etaxonomy
.taxeditor
.actions
.ui
.OpenTaxonEditorAction
;
46 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceDialog
;
47 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
48 import eu
.etaxonomy
.taxeditor
.model
.CdmSessionDataRepository
;
51 * The panel containing the taxonomic tree.
57 public class TaxonomicTreeView
extends ViewPart
{
58 private static final Logger logger
= Logger
.getLogger(TaxonomicTreeView
.class);
60 private Composite dbComposite
;
61 private Composite taxTreeComposite
= null;
62 private Composite top
= null;
63 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.taxonomictreeview"; //$NON-NLS-1$
65 private TaxonomicTreeViewer treeViewer
;
66 private IManagedForm hyperlinkForm
;
67 private Menu dataSourceMenu
;
70 * Create contents of the view part
74 public void createPartControl(Composite parent
) {
76 // Layout top composite
77 parent
.setLayout(new FillLayout());
78 GridLayout gridLayout
= new GridLayout();
79 gridLayout
.horizontalSpacing
= 0;
80 gridLayout
.marginWidth
= 0;
81 gridLayout
.marginHeight
= 0;
82 gridLayout
.verticalSpacing
= 0;
84 // Create top composite
85 top
= new Composite(parent
, SWT
.NONE
);
86 top
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
87 top
.setLayout(gridLayout
);
89 // Create composite for DB
90 dbComposite
= new Composite(top
, SWT
.NONE
);
91 dbComposite
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
92 final GridLayout gridLayout_1
= new GridLayout();
93 gridLayout_1
.numColumns
= 2;
94 dbComposite
.setLayout(gridLayout_1
);
95 final Label lblDataSourceTitle
= new Label(dbComposite
, SWT
.NONE
);
96 lblDataSourceTitle
.setFont(SWTResourceManager
.getFont("Arial", 14, SWT
.BOLD
));
97 lblDataSourceTitle
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
98 lblDataSourceTitle
.setAlignment(SWT
.CENTER
);
99 lblDataSourceTitle
.setText(" Local");
102 final Label lblDatasourceMenu
= new Label(dbComposite
, SWT
.NONE
);
103 lblDatasourceMenu
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
104 lblDatasourceMenu
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
105 lblDatasourceMenu
.setImage(ResourceManager
.getPluginImage(TaxEditorPlugin
.getDefault(), "icons/db.gif"));
106 lblDatasourceMenu
.setImage(TaxEditorPlugin
.getDefault().getImageRegistry().get(ITaxEditorConstants
.DB_ICON
));
108 // Create menu to hang from DB label
109 dataSourceMenu
= new Menu(lblDatasourceMenu
);
110 lblDatasourceMenu
.setMenu(dataSourceMenu
);
111 createDataSourceMenu();
114 final Label lblDataSource
= new Label(dbComposite
, SWT
.NONE
);
115 final GridData gd_lblDataSource
= new GridData(SWT
.LEFT
, SWT
.CENTER
, false, false, 2, 1);
116 gd_lblDataSource
.heightHint
= 16;
117 lblDataSource
.setLayoutData(gd_lblDataSource
);
118 lblDataSource
.setFont(SWTResourceManager
.getFont("Arial", 8, SWT
.NONE
));
119 lblDataSource
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
120 lblDataSource
.setAlignment(SWT
.CENTER
);
121 lblDataSource
.setText(" Datasource: local h2 database");
123 // Create composite for taxonomic tree
124 taxTreeComposite
= new Composite(top
, SWT
.NONE
);
125 taxTreeComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
126 taxTreeComposite
.setLayout(new FillLayout());
127 taxTreeComposite
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
129 // If there are no root taxa, provide a link to create this tree's initial root taxon
130 if (CdmSessionDataRepository
.getDefault().getRootTaxa().size() == 0) {
132 hyperlinkForm
= new ManagedForm(taxTreeComposite
);
133 Composite linkBody
= hyperlinkForm
.getForm().getBody();
135 TableWrapLayout linkLayout
= new TableWrapLayout();
136 linkLayout
.leftMargin
= 20;
137 linkLayout
.rightMargin
= 20;
138 linkBody
.setLayout(linkLayout
);
139 linkBody
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
141 Hyperlink link
= hyperlinkForm
.getToolkit().createHyperlink(linkBody
,
142 "Your data source is empty. Click here to create a root taxon.", SWT
.WRAP
);
143 link
.addHyperlinkListener(new HyperlinkAdapter() {
146 public void linkActivated(HyperlinkEvent e
) {
147 new OpenTaxonEditorAction().run();
162 private void createDataSourceMenu() {
164 // Create entry to open data source dialog
165 new MenuItem(dataSourceMenu
, SWT
.SEPARATOR
);
166 final MenuItem newDataSourceMenuItem
= new MenuItem(dataSourceMenu
, SWT
.NONE
);
167 newDataSourceMenuItem
.setText("Edit data sources ...");
168 newDataSourceMenuItem
.addSelectionListener(new SelectionAdapter() {
169 public void widgetSelected(SelectionEvent event
) {
170 Dialog dialog
= new CdmDataSourceDialog(UiUtil
.getShell());
173 // Refresh menu to reflect any changes in data
174 refreshDataSources();
178 // Add data sources to menu
179 refreshDataSources();
182 private void refreshDataSources() {
184 // First dispose of any entries that are already there
185 for (MenuItem menuItem
: dataSourceMenu
.getItems()) {
186 if (menuItem
.getData() instanceof ICdmDataSource
) {
191 // Get current datasource and list of all data sources
192 ICdmDataSource sessionDataSource
= CdmDataSourceRepository
.getDefault().
193 getCurrentDataSource();
194 List
<ICdmDataSource
> dataSources
=
195 CdmDataSourceRepository
.getDefault().getAll();
197 // Create menu items, putting a check next to the current one
198 for (ICdmDataSource dataSource
: dataSources
) {
200 // Always add menu item to index 0, i.e. at the beginning of the menu
201 MenuItem item
= new MenuItem(dataSourceMenu
, SWT
.CHECK
, 0);
203 // If this is the current data source, put a check by it
204 if (dataSource
.equals(sessionDataSource
)) {
205 item
.setSelection(true);
208 // Make a nice little name string out of this mutha
209 String text
= dataSource
.getName();
210 if (!dataSource
.getDatabaseType().equals(DatabaseTypeEnum
.H2
)) {
211 if (dataSource
.getDatabase() != null) {
212 text
+= " (" + dataSource
.getDatabaseType().getName() + ")";
216 item
.setData(dataSource
);
218 // On selection, make this the current data source
219 item
.addSelectionListener(new SelectionAdapter() {
220 public void widgetSelected(SelectionEvent e
) {
222 // Make sure widget has a datasource in its data field
223 if (!(e
.widget
.getData() instanceof ICdmDataSource
)) {
227 // Get the datasource from the widget's data field
228 ICdmDataSource dataSource
= (ICdmDataSource
) e
.widget
.getData();
230 // Make sure this isn't already selected
231 if (dataSource
.equals(CdmDataSourceRepository
.getDefault().getCurrentDataSource())) {
233 // Make sure it remains checked, then return
234 if (e
.widget
instanceof MenuItem
) {
235 ((MenuItem
) e
.widget
).setSelection(true);
240 // Set datasource in widget's data to current datasource
241 CdmDataSourceRepository
.getDefault().setCurrentDataSource(dataSource
);
243 // Deselect all other datasources
244 for (MenuItem item
: dataSourceMenu
.getItems()) {
245 if (item
.getData() != null && !item
.getData().equals(dataSource
)) {
246 item
.setSelection(false);
254 public TaxonomicTreeViewer
createTreeViewer() {
256 // Dispose link to new taxon as necessary
257 if (hyperlinkForm
!= null) {
258 hyperlinkForm
.getForm().dispose();
262 treeViewer
= new TaxonomicTreeViewer(taxTreeComposite
);
264 ((TaxonomicTreeViewer
) treeViewer
).createDoubleClickListener();
265 ((TaxonomicTreeViewer
) treeViewer
).createMenu();
266 ((TaxonomicTreeViewer
) treeViewer
).createQuickAdd();
267 ((TaxonomicTreeViewer
) treeViewer
).createDragAndDrop();
269 taxTreeComposite
.layout();
274 private void createActions() {
275 // Create the actions
279 * Initialize the toolbar
281 private void initializeToolBar() {
282 IToolBarManager toolbarManager
= getViewSite().getActionBars()
283 .getToolBarManager();
287 * Initialize the menu
289 private void initializeMenu() {
290 IMenuManager menuManager
= getViewSite().getActionBars()
294 public void setFocus() {
299 * Expose TreeViewer, i.e. to manipulate tree nodes
302 public TaxonomicTreeViewer
getTreeViewer() {