p2izing the editor
[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 import java.util.Set;
14
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;
41
42 import com.swtdesigner.ResourceManager;
43 import com.swtdesigner.SWTResourceManager;
44
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;
56
57 /**
58 * The panel containing the taxonomic tree.
59 *
60 * @author p.ciardelli
61 * @created 27.05.2008
62 * @version 1.0
63 */
64 public class TaxonomicTreeView extends ViewPart {
65 private static final Logger logger = Logger.getLogger(TaxonomicTreeView.class);
66
67 private Composite dbComposite;
68 private Composite taxTreeComposite = null;
69 public static final String ID = "eu.etaxonomy.taxeditor.navigation.taxonomictreeview"; //$NON-NLS-1$
70
71 private TaxonomicTreeViewer treeViewer;
72 private IManagedForm hyperlinkForm;
73 private Menu dataSourceMenu;
74
75 private Label lblDataSource;
76
77 private Label lblDataSourceTitle;
78
79 private Composite dbCompositeSub;
80
81 /**
82 * Create contents of the view part
83 * @param parent
84 */
85 @Override
86 public void createPartControl(Composite parent) {
87
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);
94
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));
105
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);
112
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);
117
118 // Create DB label
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));
124
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);
131 }
132 });
133 createDataSourceMenu();
134
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));
142
143 setDataSourceText();
144
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));
150
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) {
153
154 hyperlinkForm = new ManagedForm(taxTreeComposite);
155 Composite linkBody = hyperlinkForm.getForm().getBody();
156
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));
162
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() {
166 @Override
167 public void linkActivated(HyperlinkEvent e) {
168 EditorController.openUnitializedRootTaxon();
169 }
170 });
171
172 treeViewer = null;
173
174 } else {
175 createTreeViewer();
176 }
177
178 // createActions();
179 // initializeToolBar();
180 // initializeMenu();
181
182 }
183
184 /**
185 *
186 */
187 private void setDataSourceText() {
188 ICdmDataSource dataSource = CdmDataSourceRepository.getDefault().getCurrentDataSource();
189
190 try{
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);
194 }
195
196 lblDataSourceTitle.setText(getDataSourceName(dataSource));
197
198 dbCompositeSub.layout();
199 dbComposite.layout();
200 }
201
202 private void createDataSourceMenu() {
203
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();
211 }
212 });
213
214 // Add data sources to menu
215 refreshDataSources();
216 }
217
218 private void showDataSourceDialog() {
219 Dialog dialog = new CdmDataSourceDialog(GlobalController.getShell());
220 dialog.open();
221
222 // Refresh menu to reflect any changes in data
223 refreshDataSources();
224 }
225
226 private void refreshDataSources() {
227
228 // First dispose of any entries that are already there
229 for (MenuItem menuItem : dataSourceMenu.getItems()) {
230 if (menuItem.getData() instanceof ICdmDataSource) {
231 menuItem.dispose();
232 }
233 }
234
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();
240
241 // Create menu items, putting a check next to the current one
242 for (ICdmDataSource dataSource : dataSources) {
243
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);
246
247 // If this is the current data source, put a check by it
248 if (dataSource.equals(sessionDataSource)) {
249 item.setSelection(true);
250 }
251
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() + ")";
257 // }
258 // }
259 String text = getDataSourceName(dataSource);
260 item.setText(text);
261 item.setData(dataSource);
262
263 // On selection, make this the current data source
264 item.addSelectionListener(new SelectionAdapter() {
265 public void widgetSelected(SelectionEvent e) {
266
267 // Make sure widget has a datasource in its data field
268 if (!(e.widget.getData() instanceof ICdmDataSource)) {
269 return;
270 }
271
272 // Get the datasource from the widget's data field
273 ICdmDataSource dataSource = (ICdmDataSource) e.widget.getData();
274
275 // Make sure this isn't already selected
276 if (dataSource.equals(CdmDataSourceRepository.getDefault().getCurrentDataSource())) {
277
278 // Make sure it remains checked, then return
279 if (e.widget instanceof MenuItem) {
280 ((MenuItem) e.widget).setSelection(true);
281 }
282 return;
283 }
284
285 // Set datasource in widget's data to current datasource
286 CdmDataSourceRepository.getDefault().setCurrentDataSource(dataSource);
287
288 // Deselect all other datasources
289 for (MenuItem item : dataSourceMenu.getItems()) {
290 if (item.getData() != null && !item.getData().equals(dataSource)) {
291 item.setSelection(false);
292 }
293 }
294
295 setDataSourceText();
296 }
297 });
298 }
299 }
300
301 /**
302 * @param dataSource
303 * @return
304 */
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() + ")";
310 }
311 }
312 return text;
313 }
314
315 public TaxonomicTreeViewer createTreeViewer() {
316
317 // Dispose link to new taxon as necessary
318 if (hyperlinkForm != null) {
319 hyperlinkForm.getForm().dispose();
320 }
321
322 // Create tree
323 treeViewer = new TaxonomicTreeViewer(taxTreeComposite);
324
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();
330
331 taxTreeComposite.layout();
332
333 // register context menu
334 MenuManager menuMgr = new MenuManager();
335 menuMgr.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
336 getSite().registerContextMenu(menuMgr, treeViewer);
337
338 Control control = treeViewer.getControl();
339 Menu menu = menuMgr.createContextMenu(control);
340 control.setMenu(menu);
341
342 return treeViewer;
343 }
344
345 public void setFocus() {
346 // Set the focus
347 }
348
349 /**
350 * Expose TreeViewer, i.e. to manipulate tree nodes
351 * @return
352 */
353 public TaxonomicTreeViewer getTreeViewer() {
354 return treeViewer;
355 }
356 }
357