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
.forms
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Arrays
;
15 import java
.util
.List
;
18 import org
.eclipse
.core
.runtime
.IStatus
;
19 import org
.eclipse
.jface
.util
.PropertyChangeEvent
;
20 import org
.eclipse
.jface
.wizard
.WizardDialog
;
21 import org
.eclipse
.swt
.SWT
;
22 import org
.eclipse
.swt
.events
.SelectionAdapter
;
23 import org
.eclipse
.swt
.events
.SelectionEvent
;
24 import org
.eclipse
.swt
.events
.SelectionListener
;
25 import org
.eclipse
.swt
.widgets
.Button
;
27 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
28 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
29 import eu
.etaxonomy
.cdm
.model
.common
.MultilanguageTextHelper
;
30 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
31 import eu
.etaxonomy
.taxeditor
.editor
.translation
.TranslationWizard
;
32 import eu
.etaxonomy
.taxeditor
.forms
.CdmFormFactory
.TermComboType
;
33 import eu
.etaxonomy
.taxeditor
.forms
.term
.EmptyComboElement
;
34 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
38 * @created Sep 29, 2010
41 public class MultilanguageTextElement
extends AbstractCdmFormElement
implements SelectionListener
{
43 private class OpenTranslationWizard
extends SelectionAdapter
{
44 private Map
<Language
, LanguageString
> multilanguageText
;
46 public OpenTranslationWizard(Map
<Language
, LanguageString
> multilanguageText
){
48 this.multilanguageText
= multilanguageText
;
52 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
55 public void widgetSelected(SelectionEvent e
) {
56 TranslationWizard wizard
= new TranslationWizard(multilanguageText
);
57 WizardDialog dialog
= new WizardDialog(EditorUtil
.getShell(), wizard
);
59 if (dialog
.open() == IStatus
.OK
){
60 combo_language
.setTerms(getLanguages());
68 private EmptyComboElement
<Language
> combo_language
;
70 private LanguageStringWithLabelElement element_languageString
;
72 private Button button
;
76 private Map
<Language
, LanguageString
> multilanguageText
;
82 public MultilanguageTextElement(CdmFormFactory formFactory
,
83 ICdmFormElement formElement
, String labelString
, Map
<Language
, LanguageString
> multilanguageText
, Integer textHeight
, int style
) {
84 super(formFactory
, formElement
);
86 formFactory
.addPropertyChangeListener(this);
88 element_languageString
= formFactory
.createLanguageStringWithLabelElement(formElement
, "", null, textHeight
, style
);
90 combo_language
= (EmptyComboElement
<Language
>) formFactory
.createTermComboElement(TermComboType
.EMPTY
, formElement
, "", null, style
);
91 combo_language
.addSelectionListener(this);
92 combo_language
.setEnabled(true);
94 addControl(formFactory
.createLabel(getLayoutComposite(), ""));
96 button
= formFactory
.createButton(getLayoutComposite(), "Open In Translation Editor", SWT
.PUSH
);
98 button
.setLayoutData(CdmFormFactory
.RIGHT());
99 button
.addSelectionListener(new OpenTranslationWizard(multilanguageText
));
100 button
.setEnabled(true);
102 setMultilanguageText(multilanguageText
);
106 * @see eu.etaxonomy.taxeditor.forms.ISelectable#setSelected(boolean)
109 public void setSelected(boolean selected
) {
113 private List
<Language
> getLanguages(){
115 ArrayList
<Language
> languageList
= new ArrayList
<Language
>(getMultilanguageText().keySet());
123 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
126 public void widgetSelected(SelectionEvent e
) {
127 Language selectedLanguage
= combo_language
.getSelection();
128 if(selectedLanguage
!= null){
129 LanguageString selectedLanguageString
= getMultilanguageText().get(selectedLanguage
);
131 element_languageString
.setLanguageString(selectedLanguageString
);
136 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
139 public void widgetDefaultSelected(SelectionEvent e
) {}
142 * @param multilanguageText the multilanguageText to set
144 public void setMultilanguageText(Map
<Language
, LanguageString
> multilanguageText
) {
145 this.multilanguageText
= multilanguageText
;
147 if(multilanguageText
.isEmpty()){
148 multilanguageText
.put(CdmStore
.getDefaultLanguage(), LanguageString
.NewInstance("", CdmStore
.getDefaultLanguage()));
150 combo_language
.setTerms(getLanguages());
158 private void updateControls() {
159 LanguageString preferredLanguageString
= MultilanguageTextHelper
.getPreferredLanguageString(getMultilanguageText(), Arrays
.asList(new Language
[]{CdmStore
.getDefaultLanguage()}));
161 element_languageString
.setLanguageString(preferredLanguageString
);
162 combo_language
.setSelection(preferredLanguageString
.getLanguage());
166 * @return the multilanguageText
168 public Map
<Language
, LanguageString
> getMultilanguageText() {
169 return multilanguageText
;
173 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
176 public void propertyChange(PropertyChangeEvent event
) {
180 Object eventSource
= event
.getSource();
181 if(eventSource
== element_languageString
){
182 LanguageString languageString
= ((LanguageStringWithLabelElement
) eventSource
).getLanguageString();
183 multilanguageText
.put(languageString
.getLanguage(), languageString
);