1) Separated out transaction stuff to CdmTransactionController, 2) Put data source...
[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.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;
47
48 /**
49 * The panel containing the taxonomic tree.
50 *
51 * @author p.ciardelli
52 * @created 27.05.2008
53 * @version 1.0
54 */
55 public class TaxonomicTreeView extends ViewPart {
56 private static final Logger logger = Logger.getLogger(TaxonomicTreeView.class);
57
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$
62
63 private TaxonomicTreeViewer treeViewer;
64
65 private IManagedForm linkForm;
66
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 (TaxEditorPlugin.getDefault().getSessionRootTaxa().size() == 0) {
131
132 linkForm = new ManagedForm(taxTreeComposite);
133 Composite linkBody = linkForm.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 = linkForm.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 // Get current datasource and list of all data sources
165 ICdmDataSource sessionDataSource = CdmDataSourceRepository.getDefault().
166 getCurrentDataSource();
167 List<ICdmDataSource> dataSources =
168 CdmDataSourceRepository.getDefault().getAll();
169
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);
173
174 // If this is the current data source, put a check by it
175 if (dataSource.equals(sessionDataSource)) {
176 item.setSelection(true);
177 }
178
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() + ")";
183 }
184 item.setText(text);
185 item.setData(dataSource);
186
187 // On selection, make this the current data source
188 item.addSelectionListener(new SelectionAdapter() {
189 public void widgetSelected(SelectionEvent e) {
190
191 // Make sure widget has a datasource in its data field
192 if (!(e.widget.getData() instanceof ICdmDataSource)) {
193 return;
194 }
195
196 // Get the datasource from the widget's data field
197 ICdmDataSource dataSource = (ICdmDataSource) e.widget.getData();
198
199 // Make sure this isn't already selected
200 if (dataSource.equals(CdmDataSourceRepository.getDefault().getCurrentDataSource())) {
201 return;
202 }
203
204 // Set datasource in widget's data to current datasource
205 CdmDataSourceRepository.getDefault().setCurrentDataSource(dataSource);
206
207 // Deselect all other datasources
208 for (MenuItem item : dataSourceMenu.getItems()) {
209 if (item.getData() != null && !item.getData().equals(dataSource)) {
210 item.setSelection(false);
211 }
212 }
213 }
214 });
215 }
216
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());
224 dialog.open();
225 }
226 });
227 }
228
229 public TaxonomicTreeViewer createTreeViewer() {
230
231 // Dispose link to new taxon as necessary
232 if (linkForm != null) {
233 linkForm.getForm().dispose();
234 }
235
236 // Create tree
237 treeViewer = new TaxonomicTreeViewer(taxTreeComposite);
238
239 ((TaxonomicTreeViewer) treeViewer).createDoubleClickListener();
240 ((TaxonomicTreeViewer) treeViewer).createMenu();
241 ((TaxonomicTreeViewer) treeViewer).createQuickAdd();
242 ((TaxonomicTreeViewer) treeViewer).createDragAndDrop();
243
244 taxTreeComposite.layout();
245
246 return treeViewer;
247 }
248
249 private void createActions() {
250 // Create the actions
251 }
252
253 /**
254 * Initialize the toolbar
255 */
256 private void initializeToolBar() {
257 IToolBarManager toolbarManager = getViewSite().getActionBars()
258 .getToolBarManager();
259 }
260
261 /**
262 * Initialize the menu
263 */
264 private void initializeMenu() {
265 IMenuManager menuManager = getViewSite().getActionBars()
266 .getMenuManager();
267 }
268
269 public void setFocus() {
270 // Set the focus
271 }
272
273 /**
274 * Expose TreeViewer, i.e. to manipulate tree nodes
275 * @return
276 */
277 public TaxonomicTreeViewer getTreeViewer() {
278 return treeViewer;
279 }
280 }