p2izing the editor
[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.controller.PreferencesController;
32 import eu.etaxonomy.taxeditor.editor.ContextMenu;
33 import eu.etaxonomy.taxeditor.editor.GroupedComposite;
34 import eu.etaxonomy.taxeditor.operations.description.AddElementOperation;
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 edit the label for this description";
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
93 if (text.length() == 0) {
94 initEmptyText();
95 } else {
96 textViewer.getTextWidget().setText(text);
97 }
98 } else {
99 logger.warn("TextViewer is null.");
100 }
101 }
102
103 // protected String getEmptyTextPrompt() {
104 // return "Click to edit the label for this description";
105 // }
106
107 public TaxonDescription getDescription() {
108 return description;
109 }
110
111 private void createMenu() {
112
113 MenuManager featuresMenu = new MenuManager("Add description element");
114 featuresMenu.setRemoveAllWhenShown(true);
115 featuresMenu.addMenuListener(new IMenuListener() {
116 public void menuAboutToShow(IMenuManager manager) {
117 for (final Feature feature : PreferencesController.getPreferredFeatures()) {
118 String text = feature.getLabel();
119 manager.add(new Action(text){
120
121 public void run() {
122 IUndoableOperation operation = new AddElementOperation
123 (this.getText(), undoContext, taxon, description, feature);
124 GlobalController.executeOperation(operation);
125 }
126 });
127 }
128 }
129 });
130
131 // Important: add an empty action. Otherwise, when the main menu sees that
132 // there are no actions in the submenu, it will not be shown
133 featuresMenu.add(new Action() {});
134
135 ContextMenu contextMenu = createContextMenu();
136 contextMenu.addSubmenu(featuresMenu);
137 }
138
139 protected void parse(String text) {
140 getDescription().setTitleCache(text);
141
142 // Manually refresh the property sheet to reflect changes
143 setSelection();
144
145 setDirty(true);
146 }
147
148 public IPropertySource getPropertySource() {
149 return new TaxonDescriptionPropertySource(getDescription());
150 }
151
152 @Override
153 protected Font getViewerFont() {
154 return null;
155 }
156
157 public Object getData () {
158 return getDescription();
159 }
160 }