Nothing works.
[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.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;
29
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;
40
41 /**
42 * Composite for editing a description's label / Title Cache information.
43 *
44 * @author p.ciardelli
45 * @created 09.09.2008
46 * @version 1.0
47 */
48 public class DescriptionLabelComposite extends GroupedComposite {
49 private static final Logger logger = Logger
50 .getLogger(DescriptionLabelComposite.class);
51
52 /**
53 * ************ FONTS ************
54 */
55 public static final Font DESCRIPTION_LABEL_FONT = TaxEditorPlugin.getDefault()
56 .getFont(ITaxEditorConstants.ACCEPTED_TAXON_FONT);
57
58 /**
59 * ************ ICONS ************
60 */
61 public static final Image DESCRIPTION_LABEL_ICON = TaxEditorPlugin.getDefault()
62 .getImage(ITaxEditorConstants.BLACK_SQUARE_ICON);
63
64 private static final String EMPTY_NAME_PROMPT = "Click to add description label";
65
66 public DescriptionLabelComposite(Composite parent, IManagedForm managedForm, TaxonDescription taxonDescription) {
67 super(parent, managedForm);
68
69 createLabelViewer();
70 createBorderSupport();
71 createLineWrapSupport();
72
73 setData(taxonDescription);
74 transform(null);
75
76 createEmptyViewerPrompt(EMPTY_NAME_PROMPT);
77 setFocus();
78 }
79
80 public void transform(String transformation) {
81 setDraggable(false);
82 setFont(DESCRIPTION_LABEL_FONT);
83 setIcon(DESCRIPTION_LABEL_ICON);
84
85 createMenu();
86 }
87
88 public void setData(Object data) {
89 super.setData(data);
90
91 Assert.isTrue((data instanceof TaxonDescription),
92 "DescriptionLabelComposite's data field must contain a TaxonDescription object");
93
94 String label = ((TaxonDescription) data).getTitleCache();
95 getTextViewer().getTextWidget().setText(label);
96 }
97
98 private void createMenu() {
99
100 final Composite parentComposite = this.getParent();
101
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);
109 manager.add(action);
110 }
111 }
112 });
113
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() {});
117
118 ContextMenu contextMenu = createContextMenu();
119 contextMenu.addSubmenu(featuresMenu);
120 }
121
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() {
127
128 @Override
129 public void parse(String text) {
130 Assert.isTrue((getData() instanceof TaxonDescription),
131 "DescriptionLabelComposite's data field must contain a TaxonDescription object");
132
133 ((TaxonDescription) getData()).setTitleCache(text);
134 setDirty(true);
135 }
136
137 });
138 this.textViewer.getTextWidget().addFocusListener(new FocusAdapter() {
139 public void focusGained(FocusEvent e) {
140 setFocus();
141 }
142 });
143 }
144
145 @Override
146 public IPropertySource getPropertySource() {
147 return new TaxonDescriptionPropertySource((TaxonDescription) getData());
148 }
149
150 @Override
151 protected Font getViewerFont() {
152 // TODO Auto-generated method stub
153 return null;
154 }
155 }