First stabs at JFace Text functionality. Test TextViewer on NameEditorView divided...
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor.prototype2 / src / eu / etaxonomy / taxeditor / prototype2 / view / CarduusEditorView.java
1 package eu.etaxonomy.taxeditor.prototype2.view;
2
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
5
6 import org.eclipse.core.runtime.IProgressMonitor;
7 import org.eclipse.jface.action.Action;
8 import org.eclipse.jface.text.Document;
9 import org.eclipse.jface.text.TextViewer;
10 import org.eclipse.jface.text.source.SourceViewer;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.custom.PopupList;
13 import org.eclipse.swt.custom.StyledText;
14 import org.eclipse.swt.events.ControlAdapter;
15 import org.eclipse.swt.events.ControlEvent;
16 import org.eclipse.swt.events.FocusEvent;
17 import org.eclipse.swt.events.FocusListener;
18 import org.eclipse.swt.events.KeyEvent;
19 import org.eclipse.swt.events.KeyListener;
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.events.SelectionListener;
25 import org.eclipse.swt.graphics.Point;
26 import org.eclipse.swt.graphics.Rectangle;
27 import org.eclipse.swt.layout.FillLayout;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.CoolBar;
33 import org.eclipse.swt.widgets.CoolItem;
34 import org.eclipse.swt.widgets.Display;
35 import org.eclipse.swt.widgets.Event;
36 import org.eclipse.swt.widgets.Label;
37 import org.eclipse.swt.widgets.Listener;
38 import org.eclipse.swt.widgets.Menu;
39 import org.eclipse.swt.widgets.MenuItem;
40 import org.eclipse.swt.widgets.Shell;
41 import org.eclipse.swt.widgets.Table;
42 import org.eclipse.swt.widgets.TableColumn;
43 import org.eclipse.swt.widgets.TableItem;
44 import org.eclipse.swt.widgets.Text;
45 import org.eclipse.swt.widgets.ToolBar;
46 import org.eclipse.swt.widgets.ToolItem;
47 import org.eclipse.ui.IEditorInput;
48 import org.eclipse.ui.IEditorSite;
49 import org.eclipse.ui.PartInitException;
50 import org.eclipse.ui.part.EditorPart;
51 import com.swtdesigner.ResourceManager;
52 import com.swtdesigner.SWTResourceManager;
53
54 //import org.eclipse.ui.texteditor.AbstractTextEditor;
55
56 //import eu.etaxonomy.cdm.event.ICdmEventListener;
57 //import eu.etaxonomy.cdm.event.ICdmEventListenerRegistration;
58 //import eu.etaxonomy.cdm.model.name.TaxonName;
59 import eu.etaxonomy.cdm.model.name.TaxonName;
60 import eu.etaxonomy.taxeditor.prototype2.view.MySourceViewerConfig;
61 import eu.etaxonomy.taxeditor.prototype2.controller.SaveNameAction;
62 import eu.etaxonomy.taxeditor.prototype2.Activator;
63 import eu.etaxonomy.taxeditor.prototype2.controller.OpenNameEditorAction;
64
65 //public class CarduusEditorView extends EditorPart implements ICdmEventListener {
66 public class CarduusEditorView extends EditorPart {
67
68 private Table table_1;
69 private Text carduusLSpText;
70 private StyledText styledText;
71 private TaxonName taxonname;
72
73 public static final String ID = "eu.etaxonomy.taxeditor.prototype2.view.carduuseditorview"; //$NON-NLS-1$
74
75 /**
76 * Create contents of the editor part
77 * @param parent
78 */
79 @Override
80 public void createPartControl(Composite parent) {
81 //
82
83
84 // txtPlay = new Text(container, SWT.BORDER);
85 // txtPlay.setCapture(true);
86 // final GridData gd_txtPlay = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
87 // gd_txtPlay.heightHint = 96;
88 // txtPlay.setLayoutData(gd_txtPlay);
89 //// txtPlay.addVerifyListener(new VerifyListener() {
90 //// @Override
91 //// public void verifyText(VerifyEvent e) {
92 //// e.doit = Character.isDigit(e.character);
93 //// }
94 //// });
95 // txtPlay.addSelectionListener(new SelectionListener() {
96 //
97 // @Override
98 // public void widgetDefaultSelected(SelectionEvent e) {
99 // // TODO Auto-generated method stub
100 //
101 // }
102 //
103 // @Override
104 // public void widgetSelected(SelectionEvent e) {
105 // System.out.println(e.toString());
106 // }
107 //
108 //
109 // });
110 //
111 //
112 // txtPlay.addKeyListener(new KeyListener() {
113 //
114 // public void keyPressed(KeyEvent e) {
115 // System.out.println(e.character);
116 // if (e.character == '=') {
117 // Shell shell = Activator.getDefault().getWorkbench().
118 // getActiveWorkbenchWindow().getShell();
119 // PopupList list = new PopupList(shell, SWT.H_SCROLL | SWT.V_SCROLL);
120 //
121 //// Combo list = new Combo(shell, SWT.H_SCROLL | SWT.V_SCROLL);
122 //
123 // // Add the items to the list
124 // String[] OPTIONS = { "Create homo. syn.",
125 // "Create hetero. syn.",
126 // "Create mis. name",
127 // "Create homonym"};
128 //
129 // list.setItems(OPTIONS);
130 //
131 // // Open the list and get the selected item
132 // String selected = list.open(txtPlay.getBounds());
133 //
134 // // Print the item to the console
135 // System.out.println(selected);
136 // }
137 //
138 // }
139 //
140 // public void keyReleased(KeyEvent e) {
141 // // TODO Auto-generated method stub
142 //
143 // }
144 // });
145 parent.setLayout(new GridLayout());
146 Document doc = new Document("");
147
148 final Label taxonomicallyIncludedInLabel = new Label(parent, SWT.NONE);
149 taxonomicallyIncludedInLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
150 taxonomicallyIncludedInLabel.setText("Higher taxon: \"Cardueae Cass.\"");
151
152 final ToolBar toolBar = new ToolBar(parent, SWT.NONE);
153 // toolBar.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_GRAY));
154
155 final ToolItem itemSave = new ToolItem(toolBar, SWT.PUSH);
156 itemSave.setToolTipText("Save taxon \"Pilosella fuscoatra (Nägeli & Peter) Soják\"");
157 itemSave.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/save.gif"));
158 itemSave.addSelectionListener(new SelectionListener() {
159 public void mouseDown(final MouseEvent e) {
160
161 // taxonname.setGenus(txtGenus.getText());
162 // taxonname.setSpecificEpithet(txtSpEpi.getText());
163
164 new SaveNameAction(taxonname).run();
165 }
166
167 public void widgetDefaultSelected(SelectionEvent e) {
168 new SaveNameAction(taxonname).run();
169 }
170
171 public void widgetSelected(SelectionEvent e) {
172 new SaveNameAction(taxonname).run();
173 }
174 });
175
176 final ToolItem itemMove = new ToolItem(toolBar, SWT.DROP_DOWN);
177 itemMove.setToolTipText("Move taxon \"Pilosella fuscoatra (Nägeli & Peter) Soják\"");
178 itemMove.addSelectionListener(new SelectionAdapter() {
179 public void widgetSelected(final SelectionEvent e) {
180 }
181 });
182
183 final Menu menuMove = new Menu(toolBar);
184 menuMove.setData("x", null);
185 addDropDown(itemMove, menuMove);
186
187 final MenuItem moveTaxonpilosellaMenuItem = new MenuItem(menuMove, SWT.NONE);
188 moveTaxonpilosellaMenuItem.setText("Move taxon \"Pilosella fuscoatra (Nägeli & Peter) Soják\" to another higher taxon");
189
190 final MenuItem turnTaxonpilosellaMenuItem = new MenuItem(menuMove, SWT.NONE);
191 turnTaxonpilosellaMenuItem.setText("Turn taxon \"Pilosella fuscoatra (Nägeli & Peter) Soják\" into a synonym");
192
193 // });
194 // newItemToolItem.setText("New item");
195 itemMove.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/move_no_bg.gif"));
196
197 final ToolItem itemDelete = new ToolItem(toolBar, SWT.NONE);
198 itemDelete.setToolTipText("Delete taxon \"Pilosella fuscoatra (Nägeli & Peter) Soják\"");
199 itemDelete.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/delete.gif"));
200
201 final ToolItem separator1 = new ToolItem(toolBar, SWT.SEPARATOR);
202
203 final ToolItem itemAddRelationship = new ToolItem(toolBar, SWT.DROP_DOWN);
204 itemAddRelationship.setToolTipText("Add nom. or tax. relation to \"Pilosella fuscoatra (Nägeli & Peter) Soják\"");
205 itemAddRelationship.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/rel_no_bg.gif"));
206
207 final Menu menuAddRel = new Menu(toolBar);
208 menuAddRel.setData("x", null);
209 addDropDown(itemAddRelationship, menuAddRel);
210
211 final MenuItem testMenuItem = new MenuItem(menuAddRel, SWT.NONE);
212 testMenuItem.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/unknown_no_bg.gif"));
213 testMenuItem.setText("Add synonymy (type unknown)");
214
215 final MenuItem addHomotypicSynonymMenuItem = new MenuItem(menuAddRel, SWT.NONE);
216 addHomotypicSynonymMenuItem.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/homosyn_no_bg.gif"));
217 addHomotypicSynonymMenuItem.setText("Add homotypic synonym");
218
219 final MenuItem addHeterotypicSynoynmMenuItem = new MenuItem(menuAddRel, SWT.NONE);
220 addHeterotypicSynoynmMenuItem.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/heterosyn_no_bg.gif"));
221 addHeterotypicSynoynmMenuItem.setText("Add heterotypic synoynm");
222
223 final MenuItem addBasionymMenuItem = new MenuItem(menuAddRel, SWT.NONE);
224 addBasionymMenuItem.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/basionym_no_bg.gif"));
225 addBasionymMenuItem.setText("Add basionym");
226
227 final MenuItem addReplacedSynonymMenuItem = new MenuItem(menuAddRel, SWT.NONE);
228 addReplacedSynonymMenuItem.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/repsyn_no_bg.gif"));
229 addReplacedSynonymMenuItem.setText("Add replaced synonym");
230
231 final MenuItem addHomonymMenuItem = new MenuItem(menuAddRel, SWT.NONE);
232 addHomonymMenuItem.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/homonym_no_bg.gif"));
233 addHomonymMenuItem.setText("Add homonym");
234
235 final MenuItem addOrthographicVariantMenuItem = new MenuItem(menuAddRel, SWT.NONE);
236 addOrthographicVariantMenuItem.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/orthovariant_no_bg.gif"));
237 addOrthographicVariantMenuItem.setText("Add orthographic variant");
238
239 final MenuItem addMisappliedNameMenuItem = new MenuItem(menuAddRel, SWT.NONE);
240 addMisappliedNameMenuItem.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/misapplied_no_bg.gif"));
241 addMisappliedNameMenuItem.setText("Add misapplied name");
242
243 final MenuItem addConceptRelationMenuItem_1 = new MenuItem(menuAddRel, SWT.CASCADE);
244 addConceptRelationMenuItem_1.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/concept_no_bg.gif"));
245 addConceptRelationMenuItem_1.setText("Add concept relation");
246
247 final Menu menu = new Menu(addConceptRelationMenuItem_1);
248 addConceptRelationMenuItem_1.setMenu(menu);
249
250 final MenuItem checkboxMenuItem = new MenuItem(menu, SWT.CHECK);
251 checkboxMenuItem.setText("congruent");
252
253 final MenuItem checkboxMenuItem_1 = new MenuItem(menu, SWT.CHECK);
254 checkboxMenuItem_1.setSelection(true);
255 checkboxMenuItem_1.setText("included in");
256
257 final MenuItem checkboxMenuItem_2 = new MenuItem(menu, SWT.CHECK);
258 checkboxMenuItem_2.setText("includes");
259
260 final MenuItem checkboxMenuItem_3 = new MenuItem(menu, SWT.CHECK);
261 checkboxMenuItem_3.setText("overlaps");
262
263 final MenuItem checkboxMenuItem_4 = new MenuItem(menu, SWT.CHECK);
264 checkboxMenuItem_4.setText("excludes");
265
266 final MenuItem checkboxMenuItem_5 = new MenuItem(menu, SWT.CHECK);
267 checkboxMenuItem_5.setSelection(true);
268 checkboxMenuItem_5.setText("doubtful");
269
270 final ToolItem separator2 = new ToolItem(toolBar, SWT.SEPARATOR);
271
272 final ToolItem itemAddTaxon = new ToolItem(toolBar, SWT.DROP_DOWN);
273 itemAddTaxon.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/add_no_bg2.gif"));
274 itemAddTaxon.setToolTipText("Add new taxon");
275
276 final Menu menuAdd = new Menu(toolBar);
277 menuAdd.setData("x", null);
278 addDropDown(itemAddTaxon, menuAdd);
279
280 final MenuItem addTaxonToMenuItem = new MenuItem(menuAdd, SWT.NONE);
281 addTaxonToMenuItem.setText("Add new taxon to \"Pilosella fuscoatra (Nägeli & Peter) Soják\"");
282
283 final MenuItem addNewTaxonMenuItem = new MenuItem(menuAdd, SWT.NONE);
284 addNewTaxonMenuItem.setText("Add new taxon to \"Pilosella fuscoatra coll.\"");
285
286 final Composite composite = new Composite(parent, SWT.NONE);
287 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
288 composite.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
289 final GridLayout gridLayout_2 = new GridLayout();
290 gridLayout_2.horizontalSpacing = 3;
291 gridLayout_2.numColumns = 2;
292 composite.setLayout(gridLayout_2);
293
294 carduusLSpText = new Text(composite, SWT.WRAP);
295 carduusLSpText.setText("Carduus L., Sp. Pl.: 820. 1753");
296 carduusLSpText.setFont(SWTResourceManager.getFont("Georgia", 12, SWT.NONE));
297 final GridData gd_carduusLSpText = new GridData(SWT.FILL, SWT.CENTER, true, false);
298 carduusLSpText.setLayoutData(gd_carduusLSpText);
299 carduusLSpText.addFocusListener(new FocusListener() {
300
301 public void focusGained(FocusEvent e) {
302 carduusLSpText.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
303 }
304
305 public void focusLost(FocusEvent e) {
306 carduusLSpText.setBackground(null);
307 }
308
309 });
310
311 table_1 = new Table(composite, SWT.BORDER);
312 table_1.setLinesVisible(true);
313 table_1.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, true, true, 1, 2));
314
315 final TableColumn newColumnTableColumn = new TableColumn(table_1, SWT.NONE);
316 newColumnTableColumn.setWidth(100);
317 newColumnTableColumn.setText("New column");
318
319 final TableColumn newColumnTableColumn_1 = new TableColumn(table_1, SWT.NONE);
320 newColumnTableColumn_1.setWidth(100);
321 newColumnTableColumn_1.setText("New column");
322
323 final TableItem newItemTableItem = new TableItem(table_1, SWT.BORDER);
324 newItemTableItem.setText(0, "Rank");
325 newItemTableItem.setText(1, "genus");
326 newItemTableItem.setText("Rank");
327
328 final TableItem newItemTableItem_1 = new TableItem(table_1, SWT.BORDER);
329 newItemTableItem_1.setText(1, "Carduus");
330 newItemTableItem_1.setText(0, "Genus");
331
332 final TableItem newItemTableItem_2 = new TableItem(table_1, SWT.BORDER);
333 newItemTableItem_2.setText(0, "Species epithet");
334
335 final TableItem newItemTableItem_3 = new TableItem(table_1, SWT.BORDER);
336 newItemTableItem_3.setText(1, "L.");
337 newItemTableItem_3.setText(0, "Author team");
338
339 final TableItem newItemTableItem_4 = new TableItem(table_1, SWT.BORDER);
340 newItemTableItem_4.setText("...");
341 // newItemTableItem_2.setText("Species epithet");
342 // newItemTableItem_1.setText("Genus");
343 //container.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_GRAY));
344
345 SourceViewer sourceviewer = new SourceViewer(composite, null, SWT.WRAP | SWT.FULL_SELECTION);
346 sourceviewer.setAnnotationHover(null);
347 styledText = sourceviewer.getTextWidget();
348 final GridData gd_styledText = new GridData(SWT.FILL, SWT.FILL, true, true);
349 // gd_styledText.heightHint = 41;
350 // gd_styledText.widthHint = 450;
351 styledText.setLayoutData(gd_styledText);
352 sourceviewer.setDocument(doc);
353 sourceviewer.configure(new MySourceViewerConfig());
354 sourceviewer.setTabsToSpacesConverter(null);
355
356
357 // final Menu menu = new Menu(toolBar);
358 // addDropDown(newItemToolItem, menu);
359 // );
360
361 // Action testAction = new OpenNameEditorAction();
362 // testAction.setImageDescriptor(ResourceManager.getPluginImageDescriptor(Activator.getDefault(), "icons/edit_16x16.gif"));
363 // testAction.setToolTipText("Add relation");
364 // toolBar.add(testAction);
365
366 populateForm();
367 }
368
369 // private void populateForm() {
370 //
371 // String fulltext = "";
372 //
373 // if (taxonname.getGenus() != null) {
374 // fulltext = taxonname.getGenus();
375 // }
376 //
377 // if (taxonname.getSpecificEpithet() != null) {
378 // fulltext += " " + taxonname.getSpecificEpithet();
379 // }
380 //
381 // if (taxonname.getAuthorship() != null) {
382 // fulltext += " " + taxonname.getAuthorship();
383 // }
384 //
385 //// txtPlay.setText(fulltext);
386 //// txtPlay.setToolTipText("Joe mama\njoe daddy\njoe gramammy");
387 //
388 // this.setPartName(taxonname.getName());
389 // }
390
391 private void populateForm() {
392 this.setPartName("Pilosella fuscoatra (Nägeli & Peter) Soják");
393 }
394
395 @Override
396 public void setFocus() {
397 // Set the focus
398 }
399
400 @Override
401 public void doSave(IProgressMonitor monitor) {
402 // Do the Save operation
403 }
404
405 @Override
406 public void doSaveAs() {
407 // Do the Save As operation
408 }
409
410 @Override
411 public void init(IEditorSite site, IEditorInput input) throws PartInitException {
412
413 if (!(input instanceof IEditorInput))
414 throw new PartInitException(
415 "Invalid Input: Must be IFileEditorInput");
416
417 if (input.getAdapter(TaxonName.class) != null) {
418 taxonname = (TaxonName) input.getAdapter(TaxonName.class);
419 // taxonname.addCdmEventListener(this);
420 } else {
421
422 taxonname = null;
423 }
424
425 setSite(site);
426 setInput(input);
427 }
428
429 @Override
430 public boolean isDirty() {
431 return false;
432 }
433
434 @Override
435 public boolean isSaveAsAllowed() {
436 return false;
437 }
438
439 public void propertyChange(PropertyChangeEvent evt) {
440 // TaxonName taxonname = (TaxonName)evt.getNewValue();
441 this.taxonname = (TaxonName)evt.getNewValue();
442 System.out.println(((TaxonName)evt.getNewValue()).getName());
443 populateForm();
444
445 // for individual fields, do something like:
446 // if (evt.getPropertyName().equalsIgnoreCase("genus"))
447 // txtGenus.setText ...
448 }
449
450 // public void onDelete(ICdmEventListenerRegistration arg0) {
451 // // TODO Auto-generated method stub
452 //
453 // }
454
455 public void onInsert(Object arg0) {
456 // TODO Auto-generated method stub
457
458 }
459
460 public void onLoad(Object arg0) {
461 // TODO Auto-generated method stub
462
463 }
464
465 // public void onUpdate(ICdmEventListenerRegistration arg0) {
466 // // TODO Auto-generated method stub
467 //
468 // }
469
470 private static void addDropDown(final ToolItem item, final Menu menu) {
471 item.addListener(SWT.Selection, new Listener() {
472 public void handleEvent(Event event) {
473 if (event.detail == SWT.ARROW) {
474 Rectangle rect = item.getBounds();
475 Point pt = new Point(rect.x, rect.y + rect.height);
476 pt = item.getParent().toDisplay(pt);
477 menu.setLocation(pt.x, pt.y);
478 menu.setVisible(true);
479 }
480 }
481 });
482 }
483 }