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
.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
;
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
;
48 public class DescriptionElementComposite
extends GroupedComposite
{
49 private static final Logger logger
= Logger
.getLogger(DescriptionElementComposite
.class);
52 * ************ COMPOSITE TYPES ************
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";
59 * ************ FONTS ************
61 public static final Font ELEMENT_FONT
= TaxEditorPlugin
.getDefault()
62 .getFont(ITaxEditorConstants
.SYNONYM_FONT
);
65 * ************ ICONS ************
67 public static final Image BLACK_SQUARE_ICON
= TaxEditorPlugin
.getDefault()
68 .getImage(ITaxEditorConstants
.BLACK_SQUARE_ICON
);
70 private DescriptionElementBase element
;
72 private IUndoContext undoContext
;
74 private static final String EMPTY_NAME_PROMPT
= "Click to add descriptive element text";
76 public DescriptionElementComposite(Composite parent
, IManagedForm managedForm
,
77 DescriptionElementBase element
) {
78 super(parent
, managedForm
);
80 this.element
= element
;
82 this.undoContext
= EditorController
.getUndoContext(taxon
);
85 createBorderSupport();
86 createLineWrapSupport();
88 createEmptyViewerPrompt(EMPTY_NAME_PROMPT
);
91 setIsDraggable(false);
92 setIcon(BLACK_SQUARE_ICON
);
93 setFont(getViewerFont());
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;
110 Color groupBackgroundColor
= TaxEditorPlugin
.getDefault().
111 getColor(ITaxEditorConstants
.GROUP_GRAY_BKG_COLOR
);
112 setBackground(groupBackgroundColor
);
115 public void setIcon(Image icon
) {
116 // Adapted from NameComposite
117 if (getTextViewer() instanceof NameViewer
) {
118 ((NameViewer
) getTextViewer()).setIcon(icon
);
122 protected ContextMenu
createContextMenu() {
123 // Adapted from NameComposite
125 NameViewer nameViewer
= (NameViewer
) getTextViewer();
127 if (nameViewer
!= null) {
128 ContextMenu contextMenu
= new ContextMenu(nameViewer
.getRulerControl());
129 nameViewer
.getTextWidget().setMenu(contextMenu
.getMenu());
132 logger
.warn("Can't create menu because nameViewer has not been initalized.");
137 private void initTextViewer() {
140 if (element
instanceof TextData
) {
141 text
= ((TextData
) element
).getText(Language
.DEFAULT());
147 getTextViewer().getTextWidget().setText(text
);
150 public DescriptionElementBase
getElement() {
154 protected void createLineWrapSupport() {
155 new LineWrapSupport(getTextViewer(), managedForm
);
160 private void createTextViewer() {
161 this.textViewer
= new NameViewer(this);
162 this.textViewer
.getTextWidget().addModifyListener(new ParseListener() {
165 public void parse(String text
) {
167 if (getElement() instanceof TextData
) {
168 ((TextData
) getElement()).putText(text
, Language
.DEFAULT());
174 this.textViewer
.getTextWidget().addFocusListener(new FocusAdapter() {
175 public void focusGained(FocusEvent e
) {
181 private void createMenu() {
182 ContextMenu contextMenu
= createContextMenu();
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
){
191 IUndoableOperation operation
= new RemoveElementOperation
192 (this.getText(), undoContext
, taxon
, element
);
194 GlobalController
.executeOperation(operation
);
200 public IPropertySource
getPropertySource() {
201 return new DescriptionElementPropertySource(getElement());
205 protected Font
getViewerFont() {