c3cfc39bf57ef5fa345d41d546d448629dfeae72
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / navigation / TaxonomicTreeView.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.navigation;
11
12 import java.util.List;
13
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;
36
37 import com.swtdesigner.ResourceManager;
38 import com.swtdesigner.SWTResourceManager;
39
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;
49
50 /**
51 * The panel containing the taxonomic tree.
52 *
53 * @author p.ciardelli
54 * @created 27.05.2008
55 * @version 1.0
56 */
57 public class TaxonomicTreeView extends ViewPart {
58 private static final Logger logger = Logger.getLogger(TaxonomicTreeView.class);
59
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$
64
65 private TaxonomicTreeViewer treeViewer;
66 private IManagedForm hyperlinkForm;
67 private Menu dataSourceMenu;
68
69 /**
70 * Create contents of the view part
71 * @param parent
72 */
73 @Override
74 public void createPartControl(Composite parent) {
75
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;
83
84 // Create top composite
85 top = new Composite(parent, SWT.NONE);
86 top.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
87 top.setLayout(gridLayout);
88
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");
100
101 // Create DB label
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));
107
108 // Create menu to hang from DB label
109 dataSourceMenu = new Menu(lblDatasourceMenu);
110 lblDatasourceMenu.setMenu(dataSourceMenu);
111 createDataSourceMenu();
112
113 // Show current DB
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");
122
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));
128
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) {
131
132 hyperlinkForm = new ManagedForm(taxTreeComposite);
133 Composite linkBody = hyperlinkForm.getForm().getBody();
134
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));
140
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() {
144
145 @Override
146 public void linkActivated(HyperlinkEvent e) {
147 new OpenTaxonEditorAction().run();
148 }
149 });
150
151 treeViewer = null;
152
153 } else {
154 createTreeViewer();
155 }
156
157 createActions();
158 initializeToolBar();
159 initializeMenu();
160 }
161
162 private void createDataSourceMenu() {
163
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());
171 dialog.open();
172
173 // Refresh menu to reflect any changes in data
174 refreshDataSources();
175 }
176 });
177
178 // Add data sources to menu
179 refreshDataSources();
180 }
181
182 private void refreshDataSources() {
183
184 // First dispose of any entries that are already there
185 for (MenuItem menuItem : dataSourceMenu.getItems()) {
186 if (menuItem.getData() instanceof ICdmDataSource) {
187 menuItem.dispose();
188 }
189 }
190
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();
196
197 // Create menu items, putting a check next to the current one
198 for (ICdmDataSource dataSource : dataSources) {
199
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);
202
203 // If this is the current data source, put a check by it
204 if (dataSource.equals(sessionDataSource)) {
205 item.setSelection(true);
206 }
207
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() + ")";
213 }
214 }
215 item.setText(text);
216 item.setData(dataSource);
217
218 // On selection, make this the current data source
219 item.addSelectionListener(new SelectionAdapter() {
220 public void widgetSelected(SelectionEvent e) {
221
222 // Make sure widget has a datasource in its data field
223 if (!(e.widget.getData() instanceof ICdmDataSource)) {
224 return;
225 }
226
227 // Get the datasource from the widget's data field
228 ICdmDataSource dataSource = (ICdmDataSource) e.widget.getData();
229
230 // Make sure this isn't already selected
231 if (dataSource.equals(CdmDataSourceRepository.getDefault().getCurrentDataSource())) {
232
233 // Make sure it remains checked, then return
234 if (e.widget instanceof MenuItem) {
235 ((MenuItem) e.widget).setSelection(true);
236 }
237 return;
238 }
239
240 // Set datasource in widget's data to current datasource
241 CdmDataSourceRepository.getDefault().setCurrentDataSource(dataSource);
242
243 // Deselect all other datasources
244 for (MenuItem item : dataSourceMenu.getItems()) {
245 if (item.getData() != null && !item.getData().equals(dataSource)) {
246 item.setSelection(false);
247 }
248 }
249 }
250 });
251 }
252 }
253
254 public TaxonomicTreeViewer createTreeViewer() {
255
256 // Dispose link to new taxon as necessary
257 if (hyperlinkForm != null) {
258 hyperlinkForm.getForm().dispose();
259 }
260
261 // Create tree
262 treeViewer = new TaxonomicTreeViewer(taxTreeComposite);
263
264 ((TaxonomicTreeViewer) treeViewer).createDoubleClickListener();
265 ((TaxonomicTreeViewer) treeViewer).createMenu();
266 ((TaxonomicTreeViewer) treeViewer).createQuickAdd();
267 ((TaxonomicTreeViewer) treeViewer).createDragAndDrop();
268
269 taxTreeComposite.layout();
270
271 return treeViewer;
272 }
273
274 private void createActions() {
275 // Create the actions
276 }
277
278 /**
279 * Initialize the toolbar
280 */
281 private void initializeToolBar() {
282 IToolBarManager toolbarManager = getViewSite().getActionBars()
283 .getToolBarManager();
284 }
285
286 /**
287 * Initialize the menu
288 */
289 private void initializeMenu() {
290 IMenuManager menuManager = getViewSite().getActionBars()
291 .getMenuManager();
292 }
293
294 public void setFocus() {
295 // Set the focus
296 }
297
298 /**
299 * Expose TreeViewer, i.e. to manipulate tree nodes
300 * @return
301 */
302 public TaxonomicTreeViewer getTreeViewer() {
303 return treeViewer;
304 }
305 }