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
.store
.StoreUtil
;
35 import eu
.etaxonomy
.taxeditor
.ui
.combo
.TermComboElement
;
36 import eu
.etaxonomy
.taxeditor
.ui
.translation
.TranslationWizard
;
40 * MultilanguageTextElement class.
44 * @created Sep 29, 2010
47 public class MultilanguageTextElement
extends AbstractCdmFormElement
implements
50 private class OpenTranslationWizard
extends SelectionAdapter
{
52 public OpenTranslationWizard() {
60 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
61 * .swt.events.SelectionEvent)
64 public void widgetSelected(SelectionEvent e
) {
65 Map
<Language
, LanguageString
> multilanguageText
= getMultilanguageText();
67 if (multilanguageText
== null){
68 StoreUtil
.warningDialog("No text to transalte", getClass(), "The text is empty and therefore can not be translated");
71 TranslationWizard wizard
= new TranslationWizard(multilanguageText
);
72 WizardDialog dialog
= new WizardDialog(getLayoutComposite()
75 if (dialog
.open() == IStatus
.OK
) {
76 combo_language
.setTerms(getLanguages());
83 private TermComboElement
<Language
> combo_language
;
85 protected LanguageStringWithLabelElement element_languageString
;
87 private Button button
;
89 private Map
<Language
, LanguageString
> multilanguageText
;
93 * Constructor for MultilanguageTextElement.
97 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
100 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
103 * a {@link java.lang.String} object.
104 * @param multilanguageText
105 * a {@link java.util.Map} object.
107 * a {@link java.lang.Integer} object.
111 public MultilanguageTextElement(CdmFormFactory formFactory
,
112 ICdmFormElement formElement
, String labelString
,
113 Map
<Language
, LanguageString
> multilanguageText
,
114 Integer textHeight
, int style
) {
115 super(formFactory
, formElement
);
117 formFactory
.addPropertyChangeListener(this);
119 element_languageString
= this.formFactory
120 .createLanguageStringWithLabelElement(formElement
, labelString
,
121 null, textHeight
, style
);
123 if (PreferencesUtil
.isMultilanguageTextEditingCapability()) {
124 createMultilanguageTextEditingElements(formElement
,
125 multilanguageText
, style
);
127 setMultilanguageText(multilanguageText
);
130 private void createMultilanguageTextEditingElements(
131 ICdmFormElement formElement
,
132 Map
<Language
, LanguageString
> multilanguageText
, int style
) {
133 combo_language
= formFactory
134 .createTermComboElement(Language
.class, formElement
,
136 combo_language
.addSelectionListener(this);
137 addControl(formFactory
.createLabel(getLayoutComposite(), ""));
138 button
= formFactory
.createButton(getLayoutComposite(),
139 "Open In Translation Editor", SWT
.PUSH
);
141 button
.setLayoutData(LayoutConstants
.RIGHT());
142 button
.addSelectionListener(new OpenTranslationWizard());
145 private List
<Language
> getLanguages() {
147 ArrayList
<Language
> languageList
= new ArrayList
<Language
>(
148 getMultilanguageText().keySet());
150 Collections
.sort(languageList
, new DefaultTermComparator
<Language
>());
159 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt
160 * .events.SelectionEvent)
164 public void widgetSelected(SelectionEvent e
) {
165 Language selectedLanguage
= combo_language
.getSelection();
166 if (selectedLanguage
!= null) {
167 LanguageString selectedLanguageString
= getMultilanguageText().get(
170 element_languageString
.setLanguageString(selectedLanguageString
);
178 * org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse
179 * .swt.events.SelectionEvent)
183 public void widgetDefaultSelected(SelectionEvent e
) {
188 * Setter for the field <code>multilanguageText</code>.
191 * @param multilanguageText
192 * the multilanguageText to set
194 public void setMultilanguageText(
195 Map
<Language
, LanguageString
> multilanguageText
) {
196 this.multilanguageText
= multilanguageText
;
198 if (multilanguageText
!= null) {
199 if (multilanguageText
.isEmpty()) {
200 multilanguageText
.put(
201 CdmStore
.getDefaultLanguage(),
202 LanguageString
.NewInstance("",
203 CdmStore
.getDefaultLanguage()));
205 if (PreferencesUtil
.isMultilanguageTextEditingCapability()) {
206 combo_language
.setTerms(getLanguages());
215 private void updateControls() {
216 LanguageString preferredLanguageString
= MultilanguageTextHelper
217 .getPreferredLanguageString(getMultilanguageText(),
218 Arrays
.asList(new Language
[] { CdmStore
219 .getDefaultLanguage() }));
221 element_languageString
.setLanguageString(preferredLanguageString
);
222 if (PreferencesUtil
.isMultilanguageTextEditingCapability()) {
223 combo_language
.setSelection(preferredLanguageString
.getLanguage());
229 * Getter for the field <code>multilanguageText</code>.
232 * @return the multilanguageText
234 public Map
<Language
, LanguageString
> getMultilanguageText() {
235 return multilanguageText
;
242 * eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#propertyChange(org
243 * .eclipse.jface.util.PropertyChangeEvent)
247 public void propertyChange(PropertyChangeEvent event
) {
251 Object eventSource
= event
.getSource();
252 if (eventSource
== element_languageString
) {
253 LanguageString languageString
= ((LanguageStringWithLabelElement
) eventSource
)
254 .getLanguageString();
255 multilanguageText
.put(languageString
.getLanguage(), languageString
);
256 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));