2 * Copyright (C) 2016 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.ui
.element
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Collections
;
19 import java
.util
.List
;
21 import org
.eclipse
.jface
.util
.PropertyChangeEvent
;
22 import org
.eclipse
.swt
.SWT
;
23 import org
.eclipse
.swt
.events
.SelectionAdapter
;
24 import org
.eclipse
.swt
.events
.SelectionEvent
;
25 import org
.eclipse
.swt
.events
.SelectionListener
;
26 import org
.eclipse
.swt
.widgets
.Button
;
28 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
29 import eu
.etaxonomy
.cdm
.model
.common
.Representation
;
30 import eu
.etaxonomy
.cdm
.model
.common
.TermBase
;
31 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
32 import eu
.etaxonomy
.taxeditor
.model
.DefaultTermComparator
;
33 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
34 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
35 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
36 import eu
.etaxonomy
.taxeditor
.ui
.combo
.TermComboElement
;
39 public class RepresentationElement
extends AbstractCdmFormElement
implements
42 protected TermComboElement
<Language
> combo_language
;
44 protected TextWithLabelElement element_Label
;
45 protected TextWithLabelElement element_abbrevLabel
;
46 protected TextWithLabelElement element_text
;
48 protected Button button
;
50 protected Representation selectedRepresentation
;
51 protected TermBase term
;
53 private Button removeRepresentation
;
55 public RepresentationElement(CdmFormFactory formFactory
,
56 ICdmFormElement formElement
, TermBase term
,
57 Integer textHeight
, int style
, boolean fill
) {
58 super(formFactory
, formElement
);
60 formFactory
.addPropertyChangeListener(this);
62 element_Label
= this.formFactory
.createTextWithLabelElement(formElement
, "Label", null, style
);
63 element_abbrevLabel
= this.formFactory
64 .createTextWithLabelElement(formElement
, "abbrev. Label",
66 element_text
= this.formFactory
.createMultiLineTextWithLabel(formElement
, "Description", textHeight
, style
);
67 if (PreferencesUtil
.isMultilanguageTextEditingCapability()) {
68 createRepresentationEditingElements(formElement
, style
);
74 public RepresentationElement(CdmFormFactory formFactory
,
75 ICdmFormElement formElement
, Representation representation
, TermBase term
,
76 Integer textHeight
, int style
, boolean fill
) {
77 super(formFactory
, formElement
);
79 formFactory
.addPropertyChangeListener(this);
81 element_Label
= this.formFactory
.createTextWithLabelElement(formElement
, "Label", null, style
);
82 element_abbrevLabel
= this.formFactory
83 .createTextWithLabelElement(formElement
, "abbrev. Label",
85 element_text
= this.formFactory
.createMultiLineTextWithLabel(formElement
, "Description", textHeight
, style
);
86 if (PreferencesUtil
.isMultilanguageTextEditingCapability()) {
87 createRepresentationEditingElements(formElement
, style
);
90 setSelectedRepresentation(representation
);
95 protected void createRepresentationEditingElements(
96 ICdmFormElement formElement
, int style
) {
97 removeRepresentation
= formFactory
.createButton(getLayoutComposite(), null,
99 removeRepresentation
.setImage(ImageResources
100 .getImage(ImageResources
.TRASH_ICON
));
101 removeRepresentation
.setToolTipText("Remove representation");
102 removeRepresentation
.addSelectionListener(new DeleteListener(this));
103 addControl(removeRepresentation
);
105 combo_language
= formFactory
.createDefinedTermComboElement(TermType
.Language
, formElement
,
106 "", null, false, style
);
108 combo_language
.addSelectionListener(this);
112 public List
<Language
> getLanguages() {
114 ArrayList
<Language
> languageList
= new ArrayList
<Language
>();
115 for (Representation rep
: term
.getRepresentations()){
116 languageList
.add(rep
.getLanguage());
119 Collections
.sort(languageList
, new DefaultTermComparator
<Language
>());
125 public void widgetSelected(SelectionEvent e
) {
126 Language selectedLanguage
= combo_language
.getSelection();
127 if (selectedLanguage
!= null) {
128 selectedRepresentation
= getTerm().getRepresentation(
130 if (selectedRepresentation
== null){
131 selectedRepresentation
= Representation
.NewInstance("", "", "", selectedLanguage
);
133 element_Label
.setText(selectedRepresentation
.getLabel());
134 element_abbrevLabel
.setText(selectedRepresentation
.getAbbreviatedLabel());
135 element_text
.setText(selectedRepresentation
.getDescription());
139 element_Label
.setEnabled(false);
140 element_abbrevLabel
.setEnabled(false);
141 element_text
.setEnabled(false);
147 public void widgetDefaultSelected(SelectionEvent e
) {
151 TermBase term
, boolean update
) {
153 if (term
.getRepresentations().isEmpty()){
154 //if the term has no representation at all, create a default one.
155 Representation rep
= Representation
.NewInstance("", "empty representation", "", PreferencesUtil
.getGlobalLanguage());
156 term
.addRepresentation(rep
);
159 if (selectedRepresentation
!= null) {
160 combo_language
.setTerms(getLanguages());
163 element_Label
.setEnabled(false);
164 element_abbrevLabel
.setEnabled(false);
165 element_text
.setEnabled(false);
166 if (PreferencesUtil
.isMultilanguageTextEditingCapability()) {
167 removeRepresentation
.setEnabled(false);
175 protected void updateControls() {
176 Representation preferredRepresentation
= term
177 .getPreferredRepresentation(PreferencesUtil
.getGlobalLanguage() );
179 element_Label
.setText(preferredRepresentation
.getLabel());
180 element_abbrevLabel
.setText(preferredRepresentation
.getAbbreviatedLabel());
181 element_text
.setText(preferredRepresentation
.getDescription());
182 selectedRepresentation
= preferredRepresentation
;
183 if (PreferencesUtil
.isMultilanguageTextEditingCapability()) {
184 combo_language
.setSelection(preferredRepresentation
== null?CdmStore
.getDefaultLanguage():preferredRepresentation
.getLanguage());
185 boolean removePossible
= term
.getRepresentations().size() > 1 ;
186 removeRepresentation
.setEnabled(removePossible
);
191 public TermBase
getTerm() {
196 public void propertyChange(PropertyChangeEvent event
) {
200 Object eventSource
= event
.getSource();
202 if (eventSource
== element_abbrevLabel
) {
203 String abbrevLabel
= ((TextWithLabelElement
) eventSource
)
205 selectedRepresentation
.setAbbreviatedLabel(abbrevLabel
);
206 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
207 } else if (eventSource
== element_Label
){
208 String label
= ((TextWithLabelElement
) eventSource
)
210 selectedRepresentation
.setLabel(label
);
211 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
212 } else if (eventSource
== element_text
){
213 String text
= ((TextWithLabelElement
) eventSource
)
215 selectedRepresentation
.setText(text
);
216 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
217 } else if (eventSource
== button
){
218 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
219 } else if (eventSource
== combo_language
){
220 if (combo_language
.getSelection() != null){
221 element_Label
.setEnabled(true);
222 element_abbrevLabel
.setEnabled(true);
223 element_text
.setEnabled(true);
224 boolean removePossible
= term
.getRepresentations().size() > 1 ;
225 removeRepresentation
.setEnabled(removePossible
);
227 element_Label
.setEnabled(false);
228 element_abbrevLabel
.setEnabled(false);
229 element_text
.setEnabled(false);
230 removeRepresentation
.setEnabled(false);
232 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
236 public Representation
getSelectedRepresentation() {
237 return selectedRepresentation
;
240 public void setSelectedRepresentation(Representation selectedRepresentation
) {
241 if (selectedRepresentation
== null){
242 selectedRepresentation
= Representation
.NewInstance("", "", "", null);
244 this.selectedRepresentation
= selectedRepresentation
;
247 private class DeleteListener
extends SelectionAdapter
{
249 private final RepresentationElement element
;
251 public DeleteListener(RepresentationElement element
) {
252 this.element
= element
;
256 public void widgetSelected(SelectionEvent e
) {
257 term
.removeRepresentation(selectedRepresentation
);
258 firePropertyChangeEvent(new CdmPropertyChangeEvent(