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
.runtime
.Assert
;
14 import org
.eclipse
.jface
.action
.Action
;
15 import org
.eclipse
.jface
.action
.IMenuListener
;
16 import org
.eclipse
.jface
.action
.IMenuManager
;
17 import org
.eclipse
.jface
.action
.MenuManager
;
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
.description
.Feature
;
31 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
32 import eu
.etaxonomy
.taxeditor
.ITaxEditorConstants
;
33 import eu
.etaxonomy
.taxeditor
.TaxEditorPlugin
;
34 import eu
.etaxonomy
.taxeditor
.actions
.ui
.description
.AddDescriptionElementCompositeAction
;
35 import eu
.etaxonomy
.taxeditor
.editor
.ContextMenu
;
36 import eu
.etaxonomy
.taxeditor
.editor
.GroupedComposite
;
37 import eu
.etaxonomy
.taxeditor
.editor
.ParseListener
;
38 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
39 import eu
.etaxonomy
.taxeditor
.propertysheet
.description
.TaxonDescriptionPropertySource
;
42 * Composite for editing a description's label / Title Cache information.
48 public class DescriptionLabelComposite
extends GroupedComposite
{
49 private static final Logger logger
= Logger
50 .getLogger(DescriptionLabelComposite
.class);
53 * ************ FONTS ************
55 public static final Font DESCRIPTION_LABEL_FONT
= TaxEditorPlugin
.getDefault()
56 .getFont(ITaxEditorConstants
.ACCEPTED_TAXON_FONT
);
59 * ************ ICONS ************
61 public static final Image DESCRIPTION_LABEL_ICON
= TaxEditorPlugin
.getDefault()
62 .getImage(ITaxEditorConstants
.BLACK_SQUARE_ICON
);
64 private static final String EMPTY_NAME_PROMPT
= "Click to add description label";
66 public DescriptionLabelComposite(Composite parent
, IManagedForm managedForm
, TaxonDescription taxonDescription
) {
67 super(parent
, managedForm
);
70 createBorderSupport();
71 createLineWrapSupport();
73 setData(taxonDescription
);
76 createEmptyViewerPrompt(EMPTY_NAME_PROMPT
);
80 public void transform(String transformation
) {
82 setFont(DESCRIPTION_LABEL_FONT
);
83 setIcon(DESCRIPTION_LABEL_ICON
);
88 public void setData(Object data
) {
91 Assert
.isTrue((data
instanceof TaxonDescription
),
92 "DescriptionLabelComposite's data field must contain a TaxonDescription object");
94 String label
= ((TaxonDescription
) data
).getTitleCache();
95 getTextViewer().getTextWidget().setText(label
);
98 private void createMenu() {
100 final Composite parentComposite
= this.getParent();
102 MenuManager featuresMenu
= new MenuManager("Add description element");
103 featuresMenu
.setRemoveAllWhenShown(true);
104 featuresMenu
.addMenuListener(new IMenuListener() {
105 public void menuAboutToShow(IMenuManager manager
) {
106 for (Feature feature
: PreferencesUtil
.getPreferredFeatures()) {
107 Action action
= new AddDescriptionElementCompositeAction(
108 parentComposite
, managedForm
, feature
);
114 // Important: add an empty action. Otherwise, when the main menu sees that
115 // there are no actions in the submenu, it will not be shown
116 featuresMenu
.add(new Action() {});
118 ContextMenu contextMenu
= createContextMenu();
119 contextMenu
.addSubmenu(featuresMenu
);
122 private void createLabelViewer() {
123 this.textViewer
= new SourceViewer(this, null, SWT
.WRAP
| SWT
.MULTI
| SWT
.RESIZE
);
124 this.textViewer
.setDocument(new Document(""));
125 this.textViewer
.getTextWidget().setLayoutData(new TableWrapData(TableWrapData
.FILL_GRAB
));
126 this.textViewer
.getTextWidget().addModifyListener(new ParseListener() {
129 public void parse(String text
) {
130 Assert
.isTrue((getData() instanceof TaxonDescription
),
131 "DescriptionLabelComposite's data field must contain a TaxonDescription object");
133 ((TaxonDescription
) getData()).setTitleCache(text
);
138 this.textViewer
.getTextWidget().addFocusListener(new FocusAdapter() {
139 public void focusGained(FocusEvent e
) {
146 public IPropertySource
getPropertySource() {
147 return new TaxonDescriptionPropertySource((TaxonDescription
) getData());
151 protected Font
getViewerFont() {
152 // TODO Auto-generated method stub