migrating to cdmlib-plugin 2.0.0.20 including new term loading
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / editor / description / DescriptionLabelComposite.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.action.IMenuListener;
17 import org.eclipse.jface.action.IMenuManager;
18 import org.eclipse.jface.action.MenuManager;
19 import org.eclipse.swt.graphics.Font;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.ui.forms.IManagedForm;
23 import org.eclipse.ui.views.properties.IPropertySource;
24
25 import eu.etaxonomy.cdm.model.description.Feature;
26 import eu.etaxonomy.cdm.model.description.TaxonDescription;
27 import eu.etaxonomy.taxeditor.ITaxEditorConstants;
28 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
29 import eu.etaxonomy.taxeditor.controller.EditorController;
30 import eu.etaxonomy.taxeditor.controller.GlobalController;
31 import eu.etaxonomy.taxeditor.editor.ContextMenu;
32 import eu.etaxonomy.taxeditor.editor.GroupedComposite;
33 import eu.etaxonomy.taxeditor.operations.description.AddElementOperation;
34 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
35 import eu.etaxonomy.taxeditor.propertysheet.description.TaxonDescriptionPropertySource;
36
37 /**
38 * Composite for editing a description's label / Title Cache information.
39 *
40 * @author p.ciardelli
41 * @created 09.09.2008
42 * @version 1.0
43 */
44 public class DescriptionLabelComposite extends GroupedComposite {
45 private static final Logger logger = Logger
46 .getLogger(DescriptionLabelComposite.class);
47
48 /**
49 * ************ FONTS ************
50 */
51 public static final Font DESCRIPTION_LABEL_FONT = TaxEditorPlugin.getDefault()
52 .getFont(ITaxEditorConstants.ACCEPTED_TAXON_FONT);
53
54 /**
55 * ************ ICONS ************
56 */
57 public static final Image DESCRIPTION_LABEL_ICON = TaxEditorPlugin.getDefault()
58 .getImage(ITaxEditorConstants.BLACK_SQUARE_ICON);
59
60 private static final String EMPTY_NAME_PROMPT = "Click to add description label";
61
62 private TaxonDescription description;
63
64 private IUndoContext undoContext;
65
66 public DescriptionLabelComposite(Composite parent, IManagedForm managedForm, TaxonDescription description) {
67 super(parent, managedForm);
68
69 this.description = description;
70
71 this.undoContext = EditorController.getUndoContext(taxon);
72
73 createTextViewer();
74 createBorderSupport();
75 createLineWrapSupport();
76
77 createParser();
78 createEmptyViewerPrompt(EMPTY_NAME_PROMPT);
79
80 setIsDraggable(false);
81 setFont(DESCRIPTION_LABEL_FONT);
82 setIcon(DESCRIPTION_LABEL_ICON);
83
84 createMenu();
85
86 initTextViewer();
87 }
88
89 private void initTextViewer() {
90 if (textViewer != null) {
91 String text = getDescription().getTitleCache();
92 textViewer.getTextWidget().setText(text);
93 } else {
94 logger.warn("TextViewer is null.");
95 }
96 }
97
98 public TaxonDescription getDescription() {
99 return description;
100 }
101
102 private void createMenu() {
103
104 MenuManager featuresMenu = new MenuManager("Add description element");
105 featuresMenu.setRemoveAllWhenShown(true);
106 featuresMenu.addMenuListener(new IMenuListener() {
107 public void menuAboutToShow(IMenuManager manager) {
108 for (final Feature feature : PreferencesUtil.getPreferredFeatures()) {
109
110 String text = feature.getLabel();
111 manager.add(new Action(text){
112
113 public void run() {
114 IUndoableOperation operation = new AddElementOperation
115 (this.getText(), undoContext, taxon, description, feature);
116 GlobalController.executeOperation(operation);
117 }
118 });
119 }
120 }
121 });
122
123 // Important: add an empty action. Otherwise, when the main menu sees that
124 // there are no actions in the submenu, it will not be shown
125 featuresMenu.add(new Action() {});
126
127 ContextMenu contextMenu = createContextMenu();
128 contextMenu.addSubmenu(featuresMenu);
129 }
130
131 protected void parse(String text) {
132 getDescription().setTitleCache(text);
133 setDirty(true);
134 }
135
136 public IPropertySource getPropertySource() {
137 return new TaxonDescriptionPropertySource(getDescription());
138 }
139
140 @Override
141 protected Font getViewerFont() {
142 return null;
143 }
144
145 public Object getData () {
146 return getDescription();
147 }
148 }