migrating to cdmlib-plugin 2.0.0.20 including new term loading
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / editor / description / DescriptionElementComposite.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.editor.description;
11
12 import org.apache.log4j.Logger;
13 import org.eclipse.core.commands.operations.IUndoContext;
14 import org.eclipse.core.commands.operations.IUndoableOperation;
15 import org.eclipse.jface.action.Action;
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.swt.graphics.Font;
18 import org.eclipse.swt.graphics.Image;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.ui.forms.IManagedForm;
21 import org.eclipse.ui.views.properties.IPropertySource;
22
23 import eu.etaxonomy.cdm.model.common.Language;
24 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
25 import eu.etaxonomy.cdm.model.description.TextData;
26 import eu.etaxonomy.taxeditor.ITaxEditorConstants;
27 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
28 import eu.etaxonomy.taxeditor.controller.EditorController;
29 import eu.etaxonomy.taxeditor.controller.GlobalController;
30 import eu.etaxonomy.taxeditor.editor.ContextMenu;
31 import eu.etaxonomy.taxeditor.editor.GroupedComposite;
32 import eu.etaxonomy.taxeditor.operations.description.RemoveElementOperation;
33 import eu.etaxonomy.taxeditor.propertysheet.description.DescriptionElementPropertySource;
34
35 /**
36 * @author p.ciardelli
37 * @created 02.06.2008
38 * @version 1.0
39 */
40 public class DescriptionElementComposite extends GroupedComposite {
41 private static final Logger logger = Logger.getLogger(DescriptionElementComposite.class);
42
43 /**
44 * ************ COMPOSITE TYPES ************
45 */
46 public static final String TEXT_DATA = "text_data";
47 public static final String COMMON_TAXON_NAME = "common_taxon_name";
48 public static final String DISTRIBUTION = "distribution";
49
50 /**
51 * ************ FONTS ************
52 */
53 public static final Font ELEMENT_FONT = TaxEditorPlugin.getDefault()
54 .getFont(ITaxEditorConstants.SYNONYM_FONT);
55
56 /**
57 * ************ ICONS ************
58 */
59 public static final Image BLACK_SQUARE_ICON = TaxEditorPlugin.getDefault()
60 .getImage(ITaxEditorConstants.BLACK_SQUARE_ICON);
61
62 private DescriptionElementBase element;
63
64 private IUndoContext undoContext;
65
66 private static final String EMPTY_NAME_PROMPT = "Click to add descriptive element text";
67
68 public DescriptionElementComposite(Composite parent, IManagedForm managedForm,
69 DescriptionElementBase element) {
70 super(parent, managedForm);
71
72 this.element = element;
73
74 this.undoContext = EditorController.getUndoContext(taxon);
75
76 createTextViewer();
77 createBorderSupport();
78 createLineWrapSupport();
79
80 createParser();
81 createEmptyViewerPrompt(EMPTY_NAME_PROMPT);
82
83 setIsDraggable(false);
84 setIcon(BLACK_SQUARE_ICON);
85 setFont(getViewerFont());
86
87 createMenu();
88
89 initTextViewer();
90 }
91
92 private void initTextViewer() {
93
94 String text = null;
95 if (element instanceof TextData) {
96 text = ((TextData) element).getText(Language.DEFAULT());
97 }
98 if (text == null) {
99 text = "";
100 }
101
102 textViewer.getTextWidget().setText(text);
103 }
104
105 public DescriptionElementBase getElement() {
106 return element;
107 }
108
109 protected void parse(String text) {
110
111 if (getElement() instanceof TextData) {
112 ((TextData) getElement()).putText(text, Language.DEFAULT());
113 }
114
115 setDirty(true);
116 }
117
118 private void createMenu() {
119 ContextMenu contextMenu = createContextMenu();
120
121 // Delete this description element
122 String text = "Delete description element";
123 ImageDescriptor image = TaxEditorPlugin.getDefault()
124 .getImageDescriptor(ITaxEditorConstants.ACTIVE_DELETE_ICON);
125 contextMenu.addAction(new Action(text, image){
126
127 public void run() {
128 IUndoableOperation operation = new RemoveElementOperation
129 (this.getText(), undoContext, taxon, element);
130
131 GlobalController.executeOperation(operation);
132 }
133 });
134 }
135
136
137 public IPropertySource getPropertySource() {
138 return new DescriptionElementPropertySource(getElement());
139 }
140
141 @Override
142 protected Font getViewerFont() {
143 return ELEMENT_FONT;
144 }
145
146 public Object getData () {
147 return getElement();
148 }
149 }