Project

General

Profile

Download (10 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2016 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
package eu.etaxonomy.taxeditor.ui.element;
10

    
11
/**
12
 * @author k.luther
13
 * @date 22.06.2016
14
 *
15
 */
16

    
17
import java.util.ArrayList;
18
import java.util.Collections;
19
import java.util.List;
20

    
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;
26

    
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;
36

    
37

    
38
public class RepresentationElement extends AbstractCdmFormElement {
39

    
40
        protected TermComboElement<Language> combo_language;
41

    
42
        protected TextWithLabelElement element_Label;
43
        protected TextWithLabelElement element_abbrevLabel;
44
        protected TextWithLabelElement element_plural;
45
        protected TextWithLabelElement element_text;
46

    
47
        protected Button button;
48

    
49
        protected Representation selectedRepresentation;
50
        protected TermBase term;
51

    
52
		private Button removeRepresentation;
53

    
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);
58
        }
59

    
60
        public RepresentationElement(CdmFormFactory formFactory,
61
                ICdmFormElement formElement, Representation representation, TermBase term,
62
                Integer textHeight, int style, boolean fill) {
63
            super(formFactory, formElement);
64

    
65
            formFactory.addPropertyChangeListener(this);
66

    
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);
73
            }
74
            setTerm(term, fill);
75
            if(representation!=null){
76
                setSelectedRepresentation(representation);
77
            }
78

    
79
        }
80

    
81
        protected void createRepresentationEditingElements(
82
                ICdmFormElement formElement, int style) {
83
        	removeRepresentation = formFactory.createButton(getLayoutComposite(), null,
84
    				SWT.PUSH);
85
    		removeRepresentation.setImage(ImageResources
86
    				.getImage(ImageResources.TRASH_ICON));
87
    		removeRepresentation.setToolTipText("Remove representation");
88
    		removeRepresentation.addSelectionListener(new DeleteListener(this));
89
    		addControl(removeRepresentation);
90

    
91
            combo_language = formFactory.createDefinedTermComboElement(TermType.Language, formElement,
92
                            "", null, false, style);
93

    
94
       }
95

    
96
        public List<Language> getLanguages() {
97

    
98
            ArrayList<Language> languageList = new ArrayList<Language>();
99
            for (Representation rep: term.getRepresentations()){
100
                languageList.add(rep.getLanguage());
101
            }
102

    
103
            Collections.sort(languageList, new DefaultTermComparator<Language>());
104

    
105
            return languageList;
106
        }
107

    
108
        public void setTerm(
109
                TermBase term, boolean update) {
110
            this.term = term;
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);
115
            }
116

    
117
            if (selectedRepresentation != null) {
118
               combo_language.setTerms(getLanguages());
119

    
120
            } else{
121
            	 setEnabledControls(false);
122
                 if (PreferencesUtil.isMultilanguageTextEditingCapability()) {
123
                	 removeRepresentation.setEnabled(false);
124
                 }
125
            }
126
            if (update){
127
            	updateControls();
128
            }
129
        }
130

    
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);
136
        }
137

    
138
        protected void updateControls() {
139
            Representation preferredRepresentation = term
140
                    .getPreferredRepresentation(PreferencesUtil.getGlobalLanguage() );
141

    
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);
151

    
152
            }
153
        }
154

    
155
        public TermBase getTerm() {
156
            return term;
157
        }
158

    
159
        @Override
160
        public void propertyChange(PropertyChangeEvent event) {
161
            if (event == null) {
162
                return;
163
            }
164
            Object eventSource = event.getSource();
165

    
166
            if (eventSource == element_abbrevLabel) {
167
                String abbrevLabel = ((TextWithLabelElement) eventSource)
168
                        .getText();
169
                selectedRepresentation.setAbbreviatedLabel(abbrevLabel);
170
                firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event));
171
            }
172
            else if (eventSource == element_plural){
173
                String plural = ((TextWithLabelElement) eventSource)
174
                        .getText();
175
                selectedRepresentation.setPlural(plural);
176
                firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event));
177
            } else if (eventSource == element_Label){
178
                String label = ((TextWithLabelElement) eventSource)
179
                        .getText();
180
                selectedRepresentation.setLabel(label);
181
                firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event));
182
            } else if (eventSource == element_text){
183
                String text = ((TextWithLabelElement) eventSource)
184
                        .getText();
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);
194

    
195
                    Language selectedLanguage = combo_language.getSelection();
196
                    if (selectedLanguage != null) {
197
                        selectedRepresentation = getTerm().getRepresentation(selectedLanguage);
198
                        if (selectedRepresentation == null){
199
                            selectedRepresentation = Representation.NewInstance("", "", "", selectedLanguage);
200
                        }
201
                        element_Label.setText(selectedRepresentation.getLabel());
202
                        element_abbrevLabel.setText(selectedRepresentation.getAbbreviatedLabel());
203
                        element_plural.setText(selectedRepresentation.getPlural());
204
                        element_text.setText(selectedRepresentation.getDescription());
205

    
206
                    }else {
207
                        setEnabledControls(false);
208
                    }
209
                } else{
210
                    setEnabledControls(false);
211
                    removeRepresentation.setEnabled(false);
212
                }
213
                firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event));
214
            }
215
        }
216

    
217
        public Representation getSelectedRepresentation() {
218
            return selectedRepresentation;
219
        }
220

    
221
        public void setSelectedRepresentation(Representation selectedRepresentation) {
222
            if (selectedRepresentation == null){
223
                selectedRepresentation = Representation.NewInstance("", "", "", null);
224
            }
225
            this.selectedRepresentation = selectedRepresentation;
226
        }
227

    
228
        private class DeleteListener extends SelectionAdapter {
229

    
230
    		private final RepresentationElement element;
231

    
232
    		public DeleteListener(RepresentationElement element) {
233
    			this.element = element;
234
    		}
235

    
236
    		@Override
237
    		public void widgetSelected(SelectionEvent e) {
238
    			term.removeRepresentation(selectedRepresentation);
239
    			firePropertyChangeEvent(new CdmPropertyChangeEvent(
240
    					this, e));
241
    			updateControls();
242
    		}
243
    	}
244
    }
245

    
246

    
(38-38/49)