3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.ui
.element
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Arrays
;
15 import java
.util
.Collections
;
16 import java
.util
.List
;
19 import org
.eclipse
.core
.runtime
.IStatus
;
20 import org
.eclipse
.jface
.util
.PropertyChangeEvent
;
21 import org
.eclipse
.jface
.wizard
.WizardDialog
;
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
.LanguageString
;
30 import eu
.etaxonomy
.cdm
.model
.common
.MultilanguageTextHelper
;
31 import eu
.etaxonomy
.taxeditor
.model
.DefaultTermComparator
;
32 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
33 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
34 import eu
.etaxonomy
.taxeditor
.ui
.combo
.TermComboElement
;
35 import eu
.etaxonomy
.taxeditor
.ui
.translation
.TranslationWizard
;
39 * MultilanguageTextElement class.
43 * @created Sep 29, 2010
46 public class MultilanguageTextElement
extends AbstractCdmFormElement
implements
49 private class OpenTranslationWizard
extends SelectionAdapter
{
50 private final Map
<Language
, LanguageString
> multilanguageText
;
52 public OpenTranslationWizard(
53 Map
<Language
, LanguageString
> multilanguageText
) {
55 this.multilanguageText
= multilanguageText
;
62 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
63 * .swt.events.SelectionEvent)
66 public void widgetSelected(SelectionEvent e
) {
67 TranslationWizard wizard
= new TranslationWizard(multilanguageText
);
68 WizardDialog dialog
= new WizardDialog(getLayoutComposite()
71 if (dialog
.open() == IStatus
.OK
) {
72 combo_language
.setTerms(getLanguages());
79 private TermComboElement
<Language
> combo_language
;
81 protected LanguageStringWithLabelElement element_languageString
;
83 private Button button
;
85 private Map
<Language
, LanguageString
> multilanguageText
;
89 * Constructor for MultilanguageTextElement.
93 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
96 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
99 * a {@link java.lang.String} object.
100 * @param multilanguageText
101 * a {@link java.util.Map} object.
103 * a {@link java.lang.Integer} object.
107 public MultilanguageTextElement(CdmFormFactory formFactory
,
108 ICdmFormElement formElement
, String labelString
,
109 Map
<Language
, LanguageString
> multilanguageText
,
110 Integer textHeight
, int style
) {
111 super(formFactory
, formElement
);
113 formFactory
.addPropertyChangeListener(this);
115 element_languageString
= this.formFactory
116 .createLanguageStringWithLabelElement(formElement
, labelString
,
117 null, textHeight
, style
);
119 if (PreferencesUtil
.isMultilanguageTextEditingCapability()) {
120 createMultilanguageTextEditingElements(formElement
,
121 multilanguageText
, style
);
123 setMultilanguageText(multilanguageText
);
126 private void createMultilanguageTextEditingElements(
127 ICdmFormElement formElement
,
128 Map
<Language
, LanguageString
> multilanguageText
, int style
) {
129 combo_language
= formFactory
130 .createTermComboElement(Language
.class, formElement
,
132 combo_language
.addSelectionListener(this);
133 addControl(formFactory
.createLabel(getLayoutComposite(), ""));
134 button
= formFactory
.createButton(getLayoutComposite(),
135 "Open In Translation Editor", SWT
.PUSH
);
137 button
.setLayoutData(CdmFormFactory
.RIGHT());
138 button
.addSelectionListener(new OpenTranslationWizard(
142 private List
<Language
> getLanguages() {
144 ArrayList
<Language
> languageList
= new ArrayList
<Language
>(
145 getMultilanguageText().keySet());
147 Collections
.sort(languageList
, new DefaultTermComparator
<Language
>());
156 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt
157 * .events.SelectionEvent)
161 public void widgetSelected(SelectionEvent e
) {
162 Language selectedLanguage
= combo_language
.getSelection();
163 if (selectedLanguage
!= null) {
164 LanguageString selectedLanguageString
= getMultilanguageText().get(
167 element_languageString
.setLanguageString(selectedLanguageString
);
175 * org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse
176 * .swt.events.SelectionEvent)
180 public void widgetDefaultSelected(SelectionEvent e
) {
185 * Setter for the field <code>multilanguageText</code>.
188 * @param multilanguageText
189 * the multilanguageText to set
191 public void setMultilanguageText(
192 Map
<Language
, LanguageString
> multilanguageText
) {
193 this.multilanguageText
= multilanguageText
;
195 if (multilanguageText
!= null) {
196 if (multilanguageText
.isEmpty()) {
197 multilanguageText
.put(
198 CdmStore
.getDefaultLanguage(),
199 LanguageString
.NewInstance("",
200 CdmStore
.getDefaultLanguage()));
202 if (PreferencesUtil
.isMultilanguageTextEditingCapability()) {
203 combo_language
.setTerms(getLanguages());
212 private void updateControls() {
213 LanguageString preferredLanguageString
= MultilanguageTextHelper
214 .getPreferredLanguageString(getMultilanguageText(),
215 Arrays
.asList(new Language
[] { CdmStore
216 .getDefaultLanguage() }));
218 element_languageString
.setLanguageString(preferredLanguageString
);
219 if (PreferencesUtil
.isMultilanguageTextEditingCapability()) {
220 combo_language
.setSelection(preferredLanguageString
.getLanguage());
226 * Getter for the field <code>multilanguageText</code>.
229 * @return the multilanguageText
231 public Map
<Language
, LanguageString
> getMultilanguageText() {
232 return multilanguageText
;
239 * eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#propertyChange(org
240 * .eclipse.jface.util.PropertyChangeEvent)
244 public void propertyChange(PropertyChangeEvent event
) {
248 Object eventSource
= event
.getSource();
249 if (eventSource
== element_languageString
) {
250 LanguageString languageString
= ((LanguageStringWithLabelElement
) eventSource
)
251 .getLanguageString();
252 multilanguageText
.put(languageString
.getLanguage(), languageString
);
253 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));