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
.widgets
.Button
;
27 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
28 import eu
.etaxonomy
.cdm
.model
.term
.Representation
;
29 import eu
.etaxonomy
.cdm
.model
.term
.TermBase
;
30 import eu
.etaxonomy
.cdm
.model
.term
.TermType
;
31 import eu
.etaxonomy
.taxeditor
.model
.DefaultTermComparator
;
32 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
33 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
34 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
35 import eu
.etaxonomy
.taxeditor
.ui
.combo
.term
.TermComboElement
;
38 public class RepresentationElement
extends AbstractCdmFormElement
{
40 protected TermComboElement
<Language
> combo_language
;
42 protected TextWithLabelElement element_Label
;
43 protected TextWithLabelElement element_abbrevLabel
;
44 protected TextWithLabelElement element_plural
;
45 protected TextWithLabelElement element_text
;
47 protected Button button
;
49 protected Representation selectedRepresentation
;
50 protected TermBase term
;
52 private Button removeRepresentation
;
54 public RepresentationElement(CdmFormFactory formFactory
,
55 ICdmFormElement formElement
, TermBase term
,
56 Integer textHeight
, int style
, boolean fill
) {
57 this(formFactory
, formElement
, null, term
, textHeight
, style
, fill
);
60 public RepresentationElement(CdmFormFactory formFactory
,
61 ICdmFormElement formElement
, Representation representation
, TermBase term
,
62 Integer textHeight
, int style
, boolean fill
) {
63 super(formFactory
, formElement
);
65 formFactory
.addPropertyChangeListener(this);
67 element_Label
= this.formFactory
.createTextWithLabelElement(formElement
, "Label", null, style
);
68 element_abbrevLabel
= this.formFactory
.createTextWithLabelElement(formElement
, "abbrev. Label", null, style
);
69 element_plural
= this.formFactory
.createTextWithLabelElement(formElement
, "Plural", null, style
);
70 element_text
= this.formFactory
.createMultiLineTextWithLabel(formElement
, "Description", textHeight
, style
);
71 if (PreferencesUtil
.isMultilanguageTextEditingCapability()) {
72 createRepresentationEditingElements(formElement
, style
);
75 if(representation
!=null){
76 setSelectedRepresentation(representation
);
81 protected void createRepresentationEditingElements(
82 ICdmFormElement formElement
, int style
) {
83 removeRepresentation
= formFactory
.createButton(getLayoutComposite(), null,
85 removeRepresentation
.setImage(ImageResources
86 .getImage(ImageResources
.TRASH_ICON
));
87 removeRepresentation
.setToolTipText("Remove representation");
88 removeRepresentation
.addSelectionListener(new DeleteListener(this));
89 addControl(removeRepresentation
);
91 combo_language
= formFactory
.createDefinedTermComboElement(TermType
.Language
, formElement
,
92 "", null, false, style
);
96 public List
<Language
> getLanguages() {
98 ArrayList
<Language
> languageList
= new ArrayList
<Language
>();
99 for (Representation rep
: term
.getRepresentations()){
100 languageList
.add(rep
.getLanguage());
103 Collections
.sort(languageList
, new DefaultTermComparator
<Language
>());
109 TermBase term
, boolean update
) {
111 if (term
.getRepresentations().isEmpty()){
112 //if the term has no representation at all, create a default one.
113 Representation rep
= Representation
.NewInstance("", "empty representation", "", PreferencesUtil
.getGlobalLanguage());
114 term
.addRepresentation(rep
);
117 if (selectedRepresentation
!= null) {
118 combo_language
.setTerms(getLanguages());
121 setEnabledControls(false);
122 if (PreferencesUtil
.isMultilanguageTextEditingCapability()) {
123 removeRepresentation
.setEnabled(false);
131 private void setEnabledControls(boolean enabled
){
132 element_Label
.setEnabled(enabled
);
133 element_abbrevLabel
.setEnabled(enabled
);
134 element_plural
.setEnabled(enabled
);
135 element_text
.setEnabled(enabled
);
138 protected void updateControls() {
139 Representation preferredRepresentation
= term
140 .getPreferredRepresentation(PreferencesUtil
.getGlobalLanguage() );
142 element_Label
.setText(preferredRepresentation
!= null? preferredRepresentation
.getLabel(): null);
143 element_abbrevLabel
.setText(preferredRepresentation
!= null? preferredRepresentation
.getAbbreviatedLabel(): null);
144 element_plural
.setText(preferredRepresentation
!= null? preferredRepresentation
.getPlural(): null);
145 element_text
.setText(preferredRepresentation
!= null? preferredRepresentation
.getDescription(): null);
146 selectedRepresentation
= preferredRepresentation
;
147 if (PreferencesUtil
.isMultilanguageTextEditingCapability()) {
148 combo_language
.setSelection(preferredRepresentation
== null?CdmStore
.getDefaultLanguage():preferredRepresentation
.getLanguage());
149 boolean removePossible
= term
.getRepresentations().size() > 1 ;
150 removeRepresentation
.setEnabled(removePossible
);
155 public TermBase
getTerm() {
160 public void propertyChange(PropertyChangeEvent event
) {
164 Object eventSource
= event
.getSource();
166 if (eventSource
== element_abbrevLabel
) {
167 String abbrevLabel
= ((TextWithLabelElement
) eventSource
)
169 selectedRepresentation
.setAbbreviatedLabel(abbrevLabel
);
170 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
172 else if (eventSource
== element_plural
){
173 String plural
= ((TextWithLabelElement
) eventSource
)
175 selectedRepresentation
.setPlural(plural
);
176 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
177 } else if (eventSource
== element_Label
){
178 String label
= ((TextWithLabelElement
) eventSource
)
180 selectedRepresentation
.setLabel(label
);
181 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
182 } else if (eventSource
== element_text
){
183 String text
= ((TextWithLabelElement
) eventSource
)
185 selectedRepresentation
.setText(text
);
186 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
187 } else if (eventSource
== button
){
188 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
189 } else if (eventSource
== combo_language
){
190 if (combo_language
.getSelection() != null){
191 setEnabledControls(true);
192 boolean removePossible
= term
.getRepresentations().size() > 1 ;
193 removeRepresentation
.setEnabled(removePossible
);
195 Language selectedLanguage
= combo_language
.getSelection();
196 if (selectedLanguage
!= null) {
197 selectedRepresentation
= getTerm().getRepresentation(selectedLanguage
);
198 if (selectedRepresentation
== null){
199 selectedRepresentation
= Representation
.NewInstance("", "", "", selectedLanguage
);
201 element_Label
.setText(selectedRepresentation
.getLabel());
202 element_abbrevLabel
.setText(selectedRepresentation
.getAbbreviatedLabel());
203 element_plural
.setText(selectedRepresentation
.getPlural());
204 element_text
.setText(selectedRepresentation
.getDescription());
207 setEnabledControls(false);
210 setEnabledControls(false);
211 removeRepresentation
.setEnabled(false);
213 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
217 public Representation
getSelectedRepresentation() {
218 return selectedRepresentation
;
221 public void setSelectedRepresentation(Representation selectedRepresentation
) {
222 if (selectedRepresentation
== null){
223 selectedRepresentation
= Representation
.NewInstance("", "", "", null);
225 this.selectedRepresentation
= selectedRepresentation
;
228 private class DeleteListener
extends SelectionAdapter
{
230 private final RepresentationElement element
;
232 public DeleteListener(RepresentationElement element
) {
233 this.element
= element
;
237 public void widgetSelected(SelectionEvent e
) {
238 term
.removeRepresentation(selectedRepresentation
);
239 firePropertyChangeEvent(new CdmPropertyChangeEvent(