Added FeaturePreferences to enable user to choose which Features to display in the...
[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.MenuManager;
16 import org.eclipse.jface.text.Document;
17 import org.eclipse.jface.text.source.SourceViewer;
18 import org.eclipse.swt.SWT;
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.forms.widgets.TableWrapData;
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.UiUtil;
30 import eu.etaxonomy.taxeditor.actions.ui.description.AddDescriptionElementCompositeAction;
31 import eu.etaxonomy.taxeditor.editor.ContextMenu;
32 import eu.etaxonomy.taxeditor.editor.EditorGroupedComposite;
33 import eu.etaxonomy.taxeditor.editor.ParseListener;
34
35 /**
36 * Composite for editing a description's label / Title Cache information
37 *
38 * @author p.ciardelli
39 * @created 09.09.2008
40 * @version 1.0
41 */
42 public class DescriptionLabelComposite extends EditorGroupedComposite {
43 private static final Logger logger = Logger
44 .getLogger(DescriptionLabelComposite.class);
45
46 /**
47 * ************ FONTS ************
48 */
49 public static final Font DESCRIPTION_LABEL_FONT = TaxEditorPlugin.getDefault()
50 .getFont(ITaxEditorConstants.ACCEPTED_TAXON_FONT);
51
52 /**
53 * ************ ICONS ************
54 */
55 public static final Image DESCRIPTION_LABEL_ICON = TaxEditorPlugin.getDefault()
56 .getImage(ITaxEditorConstants.BLACK_SQUARE_ICON);
57
58 private static final String EMPTY_NAME_PROMPT = "Click to add description label";
59
60 public DescriptionLabelComposite(Composite parent, IManagedForm managedForm, TaxonDescription taxonDescription) {
61 super(parent, managedForm);
62
63 createLabelViewer();
64 createBorderSupport();
65 createLineWrapSupport();
66
67 setData(taxonDescription);
68 transform(null);
69
70 createEmptyViewerPrompt(EMPTY_NAME_PROMPT);
71 setFocus();
72 }
73
74 @Override
75 public void transform(String transformation) {
76 setDraggable(false);
77 setFont(DESCRIPTION_LABEL_FONT);
78 setIcon(DESCRIPTION_LABEL_ICON);
79
80 createMenu();
81 }
82
83 public void setData(Object data) {
84 super.setData(data);
85
86 Assert.isTrue((data instanceof TaxonDescription),
87 "DescriptionLabelComposite's data field must contain a TaxonDescription object");
88
89 String label = ((TaxonDescription) data).getTitleCache();
90 getTextViewer().getTextWidget().setText(label);
91 }
92
93 private void createMenu() {
94 ContextMenu contextMenu = createContextMenu();
95
96 MenuManager menuManager = new MenuManager("Add description element");
97
98 Composite parentComposite = this.getParent();
99
100 for (Feature feature : UiUtil.getPreferredFeatures()) {
101 Action action = new AddDescriptionElementCompositeAction(parentComposite, managedForm, feature);
102 menuManager.add(action);
103 }
104 contextMenu.addSubmenu(menuManager);
105 }
106
107 private void createLabelViewer() {
108 this.textViewer = new SourceViewer(this, null, SWT.WRAP | SWT.MULTI | SWT.RESIZE);
109 this.textViewer.setDocument(new Document(""));
110 this.textViewer.getTextWidget().setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
111 this.textViewer.getTextWidget().addModifyListener(new ParseListener() {
112
113 @Override
114 public void parse(String text) {
115 Assert.isTrue((getData() instanceof TaxonDescription),
116 "DescriptionLabelComposite's data field must contain a TaxonDescription object");
117
118 ((TaxonDescription) getData()).setTitleCache(text);
119 setDirty(true);
120 }
121
122 });
123 }
124
125 }