Merge branch 'release/4.0.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / description / DescriptionElementDetailSection.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.ui.section.description;
12
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.jface.action.ToolBarManager;
15 import org.eclipse.jface.viewers.ISelectionProvider;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.widgets.Control;
18
19 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
20 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
21 import eu.etaxonomy.cdm.model.description.Feature;
22 import eu.etaxonomy.cdm.model.description.TextData;
23 import eu.etaxonomy.taxeditor.model.MessagingUtils;
24 import eu.etaxonomy.taxeditor.store.CdmStore;
25 import eu.etaxonomy.taxeditor.store.StoreUtil;
26 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
27 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
28 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
29 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection;
30 import eu.etaxonomy.taxeditor.ui.section.description.operation.ChangeDescriptionElementType;
31
32 /**
33 * <p>DescriptionElementDetailSection class.</p>
34 *
35 * @author n.hoffmann
36 * @created Mar 5, 2010
37 * @version 1.0
38 */
39 public class DescriptionElementDetailSection extends
40 AbstractCdmDetailSection<DescriptionElementBase> {
41
42 /**
43 * <p>Constructor for DescriptionElementDetailSection.</p>
44 *
45 * @param cdmFormFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
46 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
47 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
48 * @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
49 * @param style a int.
50 */
51 public DescriptionElementDetailSection(CdmFormFactory cdmFormFactory, ConversationHolder conversation,
52 ICdmFormElement parentElement, ISelectionProvider selectionProvider, int style) {
53 super(cdmFormFactory, conversation, parentElement, selectionProvider, style);
54 }
55
56
57 @Override
58 protected Control createToolbar() {
59
60 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
61
62 final String destinationString = (getEntity().isInstanceOf(TextData.class)) ? "the specific element" : "free text";
63
64 final String label = "Change to " + destinationString;
65
66 Action addAction = new Action("Change Type", Action.AS_PUSH_BUTTON) {
67 /* (non-Javadoc)
68 * @see org.eclipse.jface.action.Action#run()
69 */
70 @Override
71 public void run() {
72 boolean confirmed = MessagingUtils.confirmDialog("Confirmation", "Do you really want to change to "
73 + destinationString + "? Current data will be lost.");
74 if (confirmed) {
75 // FIXME
76 ChangeDescriptionElementType operation = new ChangeDescriptionElementType(label,
77 getEntity(), StoreUtil.getDetailsView(), StoreUtil.getUndoContext());
78 StoreUtil.executeOperation(operation);
79 }
80 }
81 };
82 // TODO enable this once a proper icon has been found
83 // ticket #5043
84 // addAction.setImageDescriptor(new ImageDescriptor() {
85 //
86 // @Override
87 // public ImageData getImageData() {
88 // return ImageResources.getImage(ImageResources.ADD_ICON).getImageData();
89 // }
90 // });
91 addAction.setToolTipText(label);
92
93 toolBarManager.add(addAction);
94
95 return toolBarManager.createControl(this);
96 }
97
98 /** {@inheritDoc} */
99 @Override
100 public String getHeading() {
101 return "Description Element";
102 }
103
104 /** {@inheritDoc} */
105 @Override
106 protected void setSectionTitle() {
107 this.setText(getHeading() + ": " + getEntity().getFeature().getLabel(CdmStore.getDefaultLanguage()));
108
109 if(IsMoreThanOneTypeSupported()){
110 setTextClient(createToolbar());
111 }
112 }
113
114 private boolean IsMoreThanOneTypeSupported(){
115 int count = 0;
116 Feature feature = getEntity().getFeature();
117 if(feature.isSupportsCategoricalData()){count++;}
118 if(feature.isSupportsCommonTaxonName()){count++;}
119 if(feature.isSupportsDistribution()){count++;}
120 if(feature.isSupportsIndividualAssociation()){count++;}
121 if(feature.isSupportsQuantitativeData()){count++;}
122 if(feature.isSupportsTaxonInteraction()){count++;}
123 if(feature.isSupportsTextData()){count++;}
124 return count > 1;
125 }
126
127 /* (non-Javadoc)
128 * @see eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection#createCdmDetailElement(eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection, int)
129 */
130 @Override
131 protected AbstractCdmDetailElement<DescriptionElementBase> createCdmDetailElement(AbstractCdmDetailSection<DescriptionElementBase> parentElement, int style) {
132 return formFactory.createDescriptionElementDetailElement(parentElement, style);
133 }
134 }