ada7e6e08f146cb3f4ac73c590a383533ab1fc15
[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.events.FocusAdapter;
18 import org.eclipse.swt.events.FocusEvent;
19 import org.eclipse.swt.graphics.Color;
20 import org.eclipse.swt.graphics.Font;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.ui.forms.IManagedForm;
24 import org.eclipse.ui.forms.widgets.TableWrapData;
25 import org.eclipse.ui.forms.widgets.TableWrapLayout;
26 import org.eclipse.ui.views.properties.IPropertySource;
27
28 import eu.etaxonomy.cdm.model.common.Language;
29 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
30 import eu.etaxonomy.cdm.model.description.TextData;
31 import eu.etaxonomy.taxeditor.ITaxEditorConstants;
32 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
33 import eu.etaxonomy.taxeditor.controller.EditorController;
34 import eu.etaxonomy.taxeditor.controller.GlobalController;
35 import eu.etaxonomy.taxeditor.editor.ContextMenu;
36 import eu.etaxonomy.taxeditor.editor.GroupedComposite;
37 import eu.etaxonomy.taxeditor.editor.LineWrapSupport;
38 import eu.etaxonomy.taxeditor.editor.ParseListener;
39 import eu.etaxonomy.taxeditor.editor.name.NameViewer;
40 import eu.etaxonomy.taxeditor.operations.description.RemoveElementOperation;
41 import eu.etaxonomy.taxeditor.propertysheet.description.DescriptionElementPropertySource;
42
43 /**
44 * @author p.ciardelli
45 * @created 02.06.2008
46 * @version 1.0
47 */
48 public class DescriptionElementComposite extends GroupedComposite {
49 private static final Logger logger = Logger.getLogger(DescriptionElementComposite.class);
50
51 /**
52 * ************ COMPOSITE TYPES ************
53 */
54 public static final String TEXT_DATA = "text_data";
55 public static final String COMMON_TAXON_NAME = "common_taxon_name";
56 public static final String DISTRIBUTION = "distribution";
57
58 /**
59 * ************ FONTS ************
60 */
61 public static final Font ELEMENT_FONT = TaxEditorPlugin.getDefault()
62 .getFont(ITaxEditorConstants.SYNONYM_FONT);
63
64 /**
65 * ************ ICONS ************
66 */
67 public static final Image BLACK_SQUARE_ICON = TaxEditorPlugin.getDefault()
68 .getImage(ITaxEditorConstants.BLACK_SQUARE_ICON);
69
70 private DescriptionElementBase element;
71
72 private IUndoContext undoContext;
73
74 private static final String EMPTY_NAME_PROMPT = "Click to add descriptive element text";
75
76 public DescriptionElementComposite(Composite parent, IManagedForm managedForm,
77 DescriptionElementBase element) {
78 super(parent, managedForm);
79
80 this.element = element;
81
82 this.undoContext = EditorController.getUndoContext(taxon);
83
84 createTextViewer();
85 createBorderSupport();
86 createLineWrapSupport();
87
88 createEmptyViewerPrompt(EMPTY_NAME_PROMPT);
89 setFocus();
90
91 setIsDraggable(false);
92 setIcon(BLACK_SQUARE_ICON);
93 setFont(getViewerFont());
94
95 createMenu();
96
97 initTextViewer();
98 }
99
100 protected void createContent() {
101 // Stolen from NameComposite
102 setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
103 TableWrapLayout layout = new TableWrapLayout();
104 layout.leftMargin = 0;
105 layout.topMargin = 0;
106 layout.bottomMargin = 0;
107 layout.verticalSpacing = 0;
108 setLayout(layout);
109
110 Color groupBackgroundColor = TaxEditorPlugin.getDefault().
111 getColor(ITaxEditorConstants.GROUP_GRAY_BKG_COLOR);
112 setBackground(groupBackgroundColor);
113 }
114
115 public void setIcon(Image icon) {
116 // Adapted from NameComposite
117 if (getTextViewer() instanceof NameViewer) {
118 ((NameViewer) getTextViewer()).setIcon(icon);
119 }
120 }
121
122 protected ContextMenu createContextMenu() {
123 // Adapted from NameComposite
124
125 NameViewer nameViewer = (NameViewer) getTextViewer();
126
127 if (nameViewer != null) {
128 ContextMenu contextMenu = new ContextMenu(nameViewer.getRulerControl());
129 nameViewer.getTextWidget().setMenu(contextMenu.getMenu());
130 return contextMenu;
131 } else {
132 logger.warn("Can't create menu because nameViewer has not been initalized.");
133 return null;
134 }
135 }
136
137 private void initTextViewer() {
138
139 String text = null;
140 if (element instanceof TextData) {
141 text = ((TextData) element).getText(Language.DEFAULT());
142 }
143 if (text == null) {
144 text = "";
145 }
146
147 getTextViewer().getTextWidget().setText(text);
148 }
149
150 public DescriptionElementBase getElement() {
151 return element;
152 }
153
154 protected void createLineWrapSupport() {
155 new LineWrapSupport(getTextViewer(), managedForm);
156 }
157
158
159
160 private void createTextViewer() {
161 this.textViewer = new NameViewer(this);
162 this.textViewer.getTextWidget().addModifyListener(new ParseListener() {
163
164 @Override
165 public void parse(String text) {
166
167 if (getElement() instanceof TextData) {
168 ((TextData) getElement()).putText(text, Language.DEFAULT());
169 }
170
171 setDirty(true);
172 }
173 });
174 this.textViewer.getTextWidget().addFocusListener(new FocusAdapter() {
175 public void focusGained(FocusEvent e) {
176 setFocus();
177 }
178 });
179 }
180
181 private void createMenu() {
182 ContextMenu contextMenu = createContextMenu();
183
184 // Delete this description element
185 String text = "Delete description element";
186 ImageDescriptor image = TaxEditorPlugin.getDefault()
187 .getImageDescriptor(ITaxEditorConstants.ACTIVE_DELETE_ICON);
188 contextMenu.addAction(new Action(text, image){
189
190 public void run() {
191 IUndoableOperation operation = new RemoveElementOperation
192 (this.getText(), undoContext, taxon, element);
193
194 GlobalController.executeOperation(operation);
195 }
196 });
197 }
198
199 @Override
200 public IPropertySource getPropertySource() {
201 return new DescriptionElementPropertySource(getElement());
202 }
203
204 @Override
205 protected Font getViewerFont() {
206 return ELEMENT_FONT;
207 }
208 }