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
;
22 import org
.eclipse
.jface
.util
.PropertyChangeEvent
;
23 import org
.eclipse
.swt
.SWT
;
24 import org
.eclipse
.swt
.events
.SelectionAdapter
;
25 import org
.eclipse
.swt
.events
.SelectionEvent
;
26 import org
.eclipse
.swt
.widgets
.Button
;
28 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
29 import eu
.etaxonomy
.cdm
.model
.term
.Representation
;
30 import eu
.etaxonomy
.cdm
.model
.term
.TermBase
;
31 import eu
.etaxonomy
.cdm
.model
.term
.TermType
;
32 import eu
.etaxonomy
.cdm
.persistence
.dto
.TermDto
;
33 import eu
.etaxonomy
.taxeditor
.model
.DefaultTermComparator
;
34 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
35 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
36 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
37 import eu
.etaxonomy
.taxeditor
.ui
.combo
.term
.TermComboElement
;
40 public class RepresentationElement
extends AbstractCdmFormElement
{
42 protected TermComboElement
<Language
> combo_language
;
44 protected TextWithLabelElement element_Label
;
45 protected TextWithLabelElement element_abbrevLabel
;
46 protected TextWithLabelElement element_plural
;
47 protected TextWithLabelElement element_text
;
49 protected Button button
;
51 protected Representation selectedRepresentation
;
52 protected TermBase term
;
53 protected TermDto termDto
;
55 private Button removeRepresentation
;
57 public RepresentationElement(CdmFormFactory formFactory
,
58 ICdmFormElement formElement
, TermBase term
,
59 Integer textHeight
, int style
, boolean fill
) {
60 this(formFactory
, formElement
, null, term
, textHeight
, style
, fill
);
63 public RepresentationElement(CdmFormFactory formFactory
,
64 ICdmFormElement formElement
, Representation representation
, TermBase term
,
65 Integer textHeight
, int style
, boolean fill
) {
66 super(formFactory
, formElement
);
68 formFactory
.addPropertyChangeListener(this);
70 element_Label
= this.formFactory
.createTextWithLabelElement(formElement
, "Label", null, style
);
71 element_abbrevLabel
= this.formFactory
.createTextWithLabelElement(formElement
, "abbrev. Label", null, style
);
72 element_plural
= this.formFactory
.createTextWithLabelElement(formElement
, "Plural", null, style
);
73 element_text
= this.formFactory
.createMultiLineTextWithLabel(formElement
, "Description", textHeight
, style
);
74 if (PreferencesUtil
.isMultilanguageTextEditingCapability()) {
75 createRepresentationEditingElements(formElement
, style
);
78 if(representation
!=null){
79 setSelectedRepresentation(representation
);
84 public RepresentationElement(CdmFormFactory formFactory
,
85 ICdmFormElement formElement
, Representation representation
, TermDto term
,
86 Integer textHeight
, int style
, boolean fill
) {
87 super(formFactory
, formElement
);
89 formFactory
.addPropertyChangeListener(this);
91 element_Label
= this.formFactory
.createTextWithLabelElement(formElement
, "Label", null, style
);
92 element_abbrevLabel
= this.formFactory
.createTextWithLabelElement(formElement
, "abbrev. Label", null, style
);
93 element_plural
= this.formFactory
.createTextWithLabelElement(formElement
, "Plural", null, style
);
94 element_text
= this.formFactory
.createMultiLineTextWithLabel(formElement
, "Description", textHeight
, style
);
95 if (PreferencesUtil
.isMultilanguageTextEditingCapability()) {
96 createRepresentationEditingElements(formElement
, style
);
98 setTermDto(term
, fill
);
99 if(representation
!=null){
100 setSelectedRepresentation(representation
);
105 protected void createRepresentationEditingElements(
106 ICdmFormElement formElement
, int style
) {
107 removeRepresentation
= formFactory
.createButton(getLayoutComposite(), null,
109 removeRepresentation
.setImage(ImageResources
110 .getImage(ImageResources
.TRASH_ICON
));
111 removeRepresentation
.setToolTipText("Remove representation");
112 removeRepresentation
.addSelectionListener(new DeleteListener(this));
113 addControl(removeRepresentation
);
115 combo_language
= formFactory
.createDefinedTermComboElement(TermType
.Language
, formElement
,
116 "", null, false, style
);
120 public List
<Language
> getLanguages() {
122 ArrayList
<Language
> languageList
= new ArrayList
<Language
>();
123 Set
<Representation
> reps
= null;
125 reps
= term
.getRepresentations();
127 reps
= termDto
.getRepresentations();
130 for (Representation rep
: reps
){
131 languageList
.add(rep
.getLanguage());
134 Collections
.sort(languageList
, new DefaultTermComparator
<Language
>());
140 TermBase term
, boolean update
) {
142 if (term
.getRepresentations().isEmpty()){
143 //if the term has no representation at all, create a default one.
144 Representation rep
= Representation
.NewInstance("", "empty representation", "", PreferencesUtil
.getGlobalLanguage());
145 term
.addRepresentation(rep
);
148 if (selectedRepresentation
!= null) {
149 combo_language
.setTerms(getLanguages());
152 setEnabledControls(false);
153 if (PreferencesUtil
.isMultilanguageTextEditingCapability()) {
154 removeRepresentation
.setEnabled(false);
162 public void setTermDto(
163 TermDto term
, boolean update
) {
165 if (term
.getRepresentations().isEmpty()){
166 //if the term has no representation at all, create a default one.
167 Representation rep
= Representation
.NewInstance("", "empty representation", "", PreferencesUtil
.getGlobalLanguage());
168 term
.addRepresentation(rep
);
171 if (selectedRepresentation
!= null) {
172 combo_language
.setTerms(getLanguages());
175 setEnabledControls(false);
176 if (PreferencesUtil
.isMultilanguageTextEditingCapability()) {
177 removeRepresentation
.setEnabled(false);
185 private void setEnabledControls(boolean enabled
){
186 element_Label
.setEnabled(enabled
);
187 element_abbrevLabel
.setEnabled(enabled
);
188 element_plural
.setEnabled(enabled
);
189 element_text
.setEnabled(enabled
);
192 protected void updateControls() {
193 Representation preferredRepresentation
= null;
195 preferredRepresentation
= term
196 .getPreferredRepresentation(PreferencesUtil
.getGlobalLanguage() );
198 preferredRepresentation
= termDto
199 .getPreferredRepresentation(PreferencesUtil
.getGlobalLanguage() );
202 element_Label
.setText(preferredRepresentation
!= null? preferredRepresentation
.getLabel(): null);
203 element_abbrevLabel
.setText(preferredRepresentation
!= null? preferredRepresentation
.getAbbreviatedLabel(): null);
204 element_plural
.setText(preferredRepresentation
!= null? preferredRepresentation
.getPlural(): null);
205 element_text
.setText(preferredRepresentation
!= null? preferredRepresentation
.getDescription(): null);
206 selectedRepresentation
= preferredRepresentation
;
207 if (PreferencesUtil
.isMultilanguageTextEditingCapability()) {
208 combo_language
.setSelection(preferredRepresentation
== null?CdmStore
.getDefaultLanguage():preferredRepresentation
.getLanguage());
209 boolean removePossible
= false;
211 removePossible
= term
.getRepresentations().size() > 1 ;
213 removePossible
= termDto
.getRepresentations().size() > 1 ;
215 removeRepresentation
.setEnabled(removePossible
);
220 public TermBase
getTerm() {
225 public void propertyChange(PropertyChangeEvent event
) {
229 Object eventSource
= event
.getSource();
231 if (eventSource
== element_abbrevLabel
) {
232 String abbrevLabel
= ((TextWithLabelElement
) eventSource
)
234 selectedRepresentation
.setAbbreviatedLabel(abbrevLabel
);
235 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
237 else if (eventSource
== element_plural
){
238 String plural
= ((TextWithLabelElement
) eventSource
)
240 selectedRepresentation
.setPlural(plural
);
241 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
242 } else if (eventSource
== element_Label
){
243 String label
= ((TextWithLabelElement
) eventSource
)
245 selectedRepresentation
.setLabel(label
);
246 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
247 } else if (eventSource
== element_text
){
248 String text
= ((TextWithLabelElement
) eventSource
)
250 selectedRepresentation
.setText(text
);
251 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
252 } else if (eventSource
== button
){
253 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
254 } else if (eventSource
== combo_language
){
255 if (combo_language
.getSelection() != null){
256 setEnabledControls(true);
257 boolean removePossible
= false;
259 removePossible
= term
.getRepresentations().size() > 1 ;
261 removePossible
= termDto
.getRepresentations().size() > 1 ;
263 removeRepresentation
.setEnabled(removePossible
);
265 Language selectedLanguage
= combo_language
.getSelection();
266 if (selectedLanguage
!= null) {
267 selectedRepresentation
= getTerm().getRepresentation(selectedLanguage
);
268 if (selectedRepresentation
== null){
269 selectedRepresentation
= Representation
.NewInstance("", "", "", selectedLanguage
);
271 element_Label
.setText(selectedRepresentation
.getLabel());
272 element_abbrevLabel
.setText(selectedRepresentation
.getAbbreviatedLabel());
273 element_plural
.setText(selectedRepresentation
.getPlural());
274 element_text
.setText(selectedRepresentation
.getDescription());
277 setEnabledControls(false);
280 setEnabledControls(false);
281 removeRepresentation
.setEnabled(false);
283 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
287 public Representation
getSelectedRepresentation() {
288 return selectedRepresentation
;
291 public void setSelectedRepresentation(Representation selectedRepresentation
) {
292 if (selectedRepresentation
== null){
293 selectedRepresentation
= Representation
.NewInstance("", "", "", null);
295 this.selectedRepresentation
= selectedRepresentation
;
298 private class DeleteListener
extends SelectionAdapter
{
300 private final RepresentationElement element
;
302 public DeleteListener(RepresentationElement element
) {
303 this.element
= element
;
307 public void widgetSelected(SelectionEvent e
) {
309 term
.removeRepresentation(selectedRepresentation
);
311 termDto
.getRepresentations().remove(selectedRepresentation
);
313 firePropertyChangeEvent(new CdmPropertyChangeEvent(