2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.editor
.description
;
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
.core
.runtime
.Assert
;
16 import org
.eclipse
.jface
.action
.Action
;
17 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
18 import org
.eclipse
.jface
.text
.Document
;
19 import org
.eclipse
.jface
.text
.source
.SourceViewer
;
20 import org
.eclipse
.swt
.SWT
;
21 import org
.eclipse
.swt
.events
.FocusAdapter
;
22 import org
.eclipse
.swt
.events
.FocusEvent
;
23 import org
.eclipse
.swt
.graphics
.Font
;
24 import org
.eclipse
.swt
.graphics
.Image
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
26 import org
.eclipse
.ui
.forms
.IManagedForm
;
27 import org
.eclipse
.ui
.forms
.widgets
.TableWrapData
;
28 import org
.eclipse
.ui
.views
.properties
.IPropertySource
;
30 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
31 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
32 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
33 import eu
.etaxonomy
.taxeditor
.ITaxEditorConstants
;
34 import eu
.etaxonomy
.taxeditor
.TaxEditorPlugin
;
35 import eu
.etaxonomy
.taxeditor
.controller
.EditorController
;
36 import eu
.etaxonomy
.taxeditor
.controller
.GlobalController
;
37 import eu
.etaxonomy
.taxeditor
.editor
.ContextMenu
;
38 import eu
.etaxonomy
.taxeditor
.editor
.GroupedComposite
;
39 import eu
.etaxonomy
.taxeditor
.editor
.LineWrapSupport
;
40 import eu
.etaxonomy
.taxeditor
.editor
.ParseListener
;
41 import eu
.etaxonomy
.taxeditor
.operations
.description
.RemoveElementOperation
;
42 import eu
.etaxonomy
.taxeditor
.propertysheet
.description
.DescriptionElementPropertySource
;
49 public class DescriptionElementComposite
extends GroupedComposite
{
50 private static final Logger logger
= Logger
.getLogger(DescriptionElementComposite
.class);
53 * ************ COMPOSITE TYPES ************
55 public static final String TEXT_DATA
= "text_data";
56 public static final String COMMON_TAXON_NAME
= "common_taxon_name";
57 public static final String DISTRIBUTION
= "distribution";
60 * ************ INDENTATIONS ************
62 // public static final int ACCEPTED_INDENT = 0;
65 * ************ FONTS ************
67 public static final Font ELEMENT_FONT
= TaxEditorPlugin
.getDefault()
68 .getFont(ITaxEditorConstants
.SYNONYM_FONT
);
71 * ************ ICONS ************
73 public static final Image BLACK_SQUARE_ICON
= TaxEditorPlugin
.getDefault()
74 .getImage(ITaxEditorConstants
.BLACK_SQUARE_ICON
);
77 * ************ TRANSFORMATIONS ************
79 // public static final String ADD_GROUP_BASIONYM = "add_group_basionym";
82 * ************ MENU ACTIONS ************
84 public Action CHANGE_TAXON_TO_SYNONYM_ACTION
;
86 private DescriptionElementBase element
;
88 private IUndoContext undoContext
;
90 private static final String EMPTY_NAME_PROMPT
= "Click to add descriptive element text";
93 * The constructor for a <code>DescriptionElementComposite</code>. Takes a parent
94 * <code>Composite</code> on which to create itself, and an <code>IManagedForm</code> for
95 * <code>Composite</code> life cycle methods, i.e. drawing borders, creating other
96 * <code>Composite</code>s, creating line wrap support, etc.
101 public DescriptionElementComposite(Composite parent
, IManagedForm managedForm
,
102 DescriptionElementBase element
) {
103 super(parent
, managedForm
);
105 this.element
= element
;
107 this.undoContext
= EditorController
.getUndoContext(taxon
);
110 createBorderSupport();
111 createLineWrapSupport();
116 createEmptyViewerPrompt(EMPTY_NAME_PROMPT
);
120 public DescriptionElementBase
getElement() {
124 protected void createLineWrapSupport() {
125 new LineWrapSupport(getTextViewer(), managedForm
);
129 * All cosmetic - non-data-related, i.e. icons, fonts, etc. -
130 * transformations take place in this method.
132 * @param transformation
134 public void transform(String transformation
) {
135 setIsDraggable(true);
136 setFont(ELEMENT_FONT
);
137 setIcon(BLACK_SQUARE_ICON
);
141 managedForm
.getForm().getBody().layout();
145 * Override of Composite.setData() which passes data along to
146 * DescriptionElementComposite's TextViewer where appropriate.
148 * @see org.eclipse.swt.widgets.Widget#setData(java.lang.Object)
150 public void setData(Object data
) {
153 Assert
.isTrue((data
instanceof DescriptionElementBase
),
154 "DescriptionElementComposite's data field must contain a DescriptionElementBase object");
156 DescriptionElementBase element
= (DescriptionElementBase
) data
;
159 if (element
instanceof TextData
) {
160 text
= ((TextData
) element
).getText(Language
.DEFAULT());
166 getTextViewer().getTextWidget().setText(text
);
169 private void createTextViewer() {
170 this.textViewer
= new SourceViewer(this, null, SWT
.WRAP
| SWT
.MULTI
| SWT
.RESIZE
);
171 this.textViewer
.setDocument(new Document(""));
172 this.textViewer
.getTextWidget().setLayoutData(new TableWrapData(TableWrapData
.FILL_GRAB
));
173 this.textViewer
.getTextWidget().addModifyListener(new ParseListener() {
176 public void parse(String text
) {
177 Assert
.isTrue((getData() instanceof DescriptionElementBase
),
178 "DescriptionElementComposite's data field must contain a DescriptionElementBase object");
180 DescriptionElementBase element
= (DescriptionElementBase
) getData();
182 if (element
instanceof TextData
) {
183 ((TextData
) element
).putText(text
, Language
.DEFAULT());
189 this.textViewer
.getTextWidget().addFocusListener(new FocusAdapter() {
190 public void focusGained(FocusEvent e
) {
196 private void createMenu() {
197 ContextMenu contextMenu
= new ContextMenu(getDraggableLabel());
199 // Delete this description element
200 String text
= "Delete description element";
201 ImageDescriptor image
= TaxEditorPlugin
.getDefault()
202 .getImageDescriptor(ITaxEditorConstants
.ACTIVE_DELETE_ICON
);
203 contextMenu
.addAction(new Action(text
, image
){
206 IUndoableOperation operation
= new RemoveElementOperation
207 (this.getText(), undoContext
, taxon
, element
);
209 GlobalController
.executeOperation(operation
);
215 public void dispose() {
217 Composite featureComponent
= this.getParent();
221 // If feature only has 1 child that is not DescriptionElementComposite,
222 // composite is empty
223 if (!(featureComponent
instanceof DescriptionFeatureGroupComposite
)) {
227 if (featureComponent
.getChildren().length
== 1 &&
228 !(featureComponent
.getChildren()[0] instanceof
229 DescriptionElementComposite
)) {
230 Composite grandParent
= featureComponent
.getParent();
231 featureComponent
.dispose();
232 grandParent
.layout();
235 managedForm
.getForm().getBody().layout();
239 public IPropertySource
getPropertySource() {
240 return new DescriptionElementPropertySource((DescriptionElementBase
) getData());
244 protected Font
getViewerFont() {
245 // TODO Auto-generated method stub