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
.ICdmDataSource
;
41 import eu
.etaxonomy
.taxeditor
.ITaxEditorConstants
;
42 import eu
.etaxonomy
.taxeditor
.TaxEditorPlugin
;
43 import eu
.etaxonomy
.taxeditor
.UiUtil
;
44 import eu
.etaxonomy
.taxeditor
.actions
.ui
.OpenTaxonEditorAction
;
45 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceDialog
;
46 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
49 * The panel containing the taxonomic tree.
55 public class TaxonomicTreeView
extends ViewPart
{
56 private static final Logger logger
= Logger
.getLogger(TaxonomicTreeView
.class);
58 private Composite dbComposite
;
59 private Composite taxTreeComposite
= null;
60 private Composite top
= null;
61 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.taxonomictreeview"; //$NON-NLS-1$
63 private TaxonomicTreeViewer treeViewer
;
65 private IManagedForm linkForm
;
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 (TaxEditorPlugin
.getDefault().getSessionRootTaxa().size() == 0) {
132 linkForm
= new ManagedForm(taxTreeComposite
);
133 Composite linkBody
= linkForm
.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
= linkForm
.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 // Get current datasource and list of all data sources
165 ICdmDataSource sessionDataSource
= CdmDataSourceRepository
.getDefault().
166 getCurrentDataSource();
167 List
<ICdmDataSource
> dataSources
=
168 CdmDataSourceRepository
.getDefault().getAll();
170 // Create menu items, putting a check next to the current one
171 for (ICdmDataSource dataSource
: dataSources
) {
172 MenuItem item
= new MenuItem(dataSourceMenu
, SWT
.CHECK
);
174 // If this is the current data source, put a check by it
175 if (dataSource
.equals(sessionDataSource
)) {
176 item
.setSelection(true);
179 // Make a nice little name string out of this mutha
180 String text
= dataSource
.getName();
181 if (dataSource
.getDatabase() != null) {
182 text
+= " (" + dataSource
.getDatabaseType().getName() + ")";
185 item
.setData(dataSource
);
187 // On selection, make this the current data source
188 item
.addSelectionListener(new SelectionAdapter() {
189 public void widgetSelected(SelectionEvent e
) {
191 // Make sure widget has a datasource in its data field
192 if (!(e
.widget
.getData() instanceof ICdmDataSource
)) {
196 // Get the datasource from the widget's data field
197 ICdmDataSource dataSource
= (ICdmDataSource
) e
.widget
.getData();
199 // Make sure this isn't already selected
200 if (dataSource
.equals(CdmDataSourceRepository
.getDefault().getCurrentDataSource())) {
204 // Set datasource in widget's data to current datasource
205 CdmDataSourceRepository
.getDefault().setCurrentDataSource(dataSource
);
207 // Deselect all other datasources
208 for (MenuItem item
: dataSourceMenu
.getItems()) {
209 if (item
.getData() != null && !item
.getData().equals(dataSource
)) {
210 item
.setSelection(false);
217 // Create entry to open data source dialog
218 new MenuItem(dataSourceMenu
, SWT
.SEPARATOR
);
219 final MenuItem newDataSourceMenuItem
= new MenuItem(dataSourceMenu
, SWT
.NONE
);
220 newDataSourceMenuItem
.setText("Edit data sources ...");
221 newDataSourceMenuItem
.addSelectionListener(new SelectionAdapter() {
222 public void widgetSelected(SelectionEvent event
) {
223 Dialog dialog
= new CdmDataSourceDialog(UiUtil
.getShell());
229 public TaxonomicTreeViewer
createTreeViewer() {
231 // Dispose link to new taxon as necessary
232 if (linkForm
!= null) {
233 linkForm
.getForm().dispose();
237 treeViewer
= new TaxonomicTreeViewer(taxTreeComposite
);
239 ((TaxonomicTreeViewer
) treeViewer
).createDoubleClickListener();
240 ((TaxonomicTreeViewer
) treeViewer
).createMenu();
241 ((TaxonomicTreeViewer
) treeViewer
).createQuickAdd();
242 ((TaxonomicTreeViewer
) treeViewer
).createDragAndDrop();
244 taxTreeComposite
.layout();
249 private void createActions() {
250 // Create the actions
254 * Initialize the toolbar
256 private void initializeToolBar() {
257 IToolBarManager toolbarManager
= getViewSite().getActionBars()
258 .getToolBarManager();
262 * Initialize the menu
264 private void initializeMenu() {
265 IMenuManager menuManager
= getViewSite().getActionBars()
269 public void setFocus() {
274 * Expose TreeViewer, i.e. to manipulate tree nodes
277 public TaxonomicTreeViewer
getTreeViewer() {