ref #7086 Open WS editor via selection dialog
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / AbstractCdmDetailSection.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.ui.section;
11
12 import org.eclipse.jface.action.ToolBarManager;
13 import org.eclipse.jface.viewers.ISelectionProvider;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.graphics.Color;
16 import org.eclipse.swt.widgets.Control;
17 import org.eclipse.ui.forms.events.ExpansionEvent;
18 import org.eclipse.ui.forms.events.IExpansionListener;
19 import org.eclipse.ui.forms.widgets.ExpandableComposite;
20 import org.eclipse.ui.forms.widgets.TableWrapLayout;
21
22 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
23 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
24 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
25 import eu.etaxonomy.cdm.model.common.Language;
26 import eu.etaxonomy.cdm.model.description.TextData;
27 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
28 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
29 import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
30 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
31 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
32 import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
33 import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
34
35 /**
36 * This class visualizes an CDM entity of type ENTITY.
37 *
38 * @param <ENTITY> A CDM entity which should be visualized by this section.
39 *
40 * @author n.hoffmann
41 * @created Feb 26, 2010
42 */
43 public abstract class AbstractCdmDetailSection<ENTITY>
44 extends AbstractFormSection<ENTITY>
45 implements IEnableableFormElement, IExpansionListener {
46
47 protected ICdmDetailElement<ENTITY> detailElement;
48
49 public AbstractCdmDetailSection(CdmFormFactory formFactory,
50 ConversationHolder conversation, ICdmFormElement parentElement,
51 ISelectionProvider selectionProvider, int style) {
52 this(formFactory, null, conversation, parentElement, selectionProvider, style);
53 }
54
55
56 public AbstractCdmDetailSection(CdmFormFactory formFactory, Class<ENTITY> clazz,
57 ConversationHolder conversation, ICdmFormElement parentElement,
58 ISelectionProvider selectionProvider, int style) {
59 super(formFactory, parentElement, selectionProvider,
60 ExpandableComposite.CLIENT_INDENT | style);
61
62 setText(getHeading());
63
64 addExpansionListener(this);
65
66 if(clazz==null){
67 createControls(this, style);
68 }
69 else{
70 createControlsByType(this, clazz, SWT.NULL);
71 }
72 }
73
74 protected void createControlsByType(AbstractCdmDetailSection<ENTITY> formElement, Class<ENTITY> entityClass, int style) {
75 TableWrapLayout layout = (TableWrapLayout) getLayoutComposite().getLayout();
76 layout.topMargin = 10;
77 layout.numColumns = DEFAULT_NUM_COLUMNS;
78
79 getLayoutComposite().setLayout(layout);
80 if(entityClass==null){
81 detailElement = createCdmDetailElement(formElement, style);
82 }
83 else{
84 detailElement = createCdmDetailElementByType(formElement, entityClass, style);
85 }
86 }
87
88 protected void createControls(AbstractCdmDetailSection<ENTITY> formElement, int style) {
89 createControlsByType(formElement, null, style);
90 }
91
92 protected abstract ICdmDetailElement<ENTITY> createCdmDetailElement(AbstractCdmDetailSection<ENTITY> parentElement, int style);
93
94 protected ICdmDetailElement<ENTITY> createCdmDetailElementByType(AbstractCdmDetailSection<ENTITY> parentElement, Class<ENTITY> entityClass, int style){
95 return createCdmDetailElement(parentElement, style);
96 }
97
98 public abstract String getHeading();
99
100 /** {@inheritDoc} */
101 @Override
102 public void dispose() {
103 if (detailElement instanceof ISelectableElement) {
104 ISelectableElement selectableElement = (ISelectableElement) detailElement;
105 if (selectableElement.getSelectionArbitrator() != null) {
106 formFactory.destroySelectionArbitrator(selectableElement
107 .getSelectionArbitrator());
108 }
109 }
110 super.dispose();
111 }
112
113 /** {@inheritDoc} */
114 @Override
115 public void setBackground(Color color) {
116 if (detailElement != null) {
117 // detailComposite.setBackground(color);
118 }
119 super.setBackground(color);
120 }
121
122 @Override
123 public void setEntity(ENTITY entity) {
124 if (detailElement != null) {
125 detailElement.setEntity(entity);
126 }
127 super.setEntity(entity);
128 setSectionTitle();
129 layout();
130
131 }
132
133 protected void setSectionTitle() {
134 String title = "";
135 String label = "";
136 if (getEntity() != null && (getEntity() instanceof IdentifiableEntity) && !(getEntity() instanceof SpecimenOrObservationBase)) {
137 if (getEntity() instanceof DefinedTermBase<?> ){
138 label = ((DefinedTermBase<?>)getEntity()).getLabel(PreferencesUtil.getGlobalLanguage());
139 if (label == null){
140 label = ((DefinedTermBase<?>)getEntity()).getLabel();
141 }
142
143 } else if (getEntity() instanceof TextData){
144 label = ((TextData)getEntity()).getLanguageText(PreferencesUtil.getGlobalLanguage()).getText();
145 if (label == null){
146 label = ((TextData)getEntity()).getLanguageText(Language.DEFAULT()).getText();
147 }
148 } else{
149 label =((IdentifiableEntity) getEntity()).getTitleCache();
150 }
151 title = ": " + label;
152
153 // we have to duplicate ampersands otherwise they are treated as
154 // mnenomic (see Label.setText() documentation)
155 // see also #4302
156 title = title.replace("&", "&&");
157 }
158 this.setText(String.format("%s%s", getHeading(), title));
159 setTextClient(createToolbar());
160 }
161
162 protected Control createToolbar() {
163 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
164 return toolBarManager.createControl(this);
165 }
166
167 public void updateTitle() {
168 if (!isDisposed()) {
169 setSectionTitle();
170 layout();
171 }
172 }
173
174 /** {@inheritDoc} */
175 @Override
176 public void setIrrelevant(boolean irrelevant) {
177 if (detailElement != null) {
178
179 detailElement.setIrrelevant(irrelevant);
180 }
181 }
182
183 /** {@inheritDoc} */
184 @Override
185 public void expansionStateChanging(ExpansionEvent e) {
186 // logger.warn("Expansion State Changing");
187 }
188
189 /** {@inheritDoc} */
190 @Override
191 public void expansionStateChanged(ExpansionEvent e) {
192 // logger.warn("Expansion State Changed");
193 }
194 }