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
.translation
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Arrays
;
15 import java
.util
.List
;
19 import org
.eclipse
.core
.runtime
.Assert
;
20 import org
.eclipse
.jface
.wizard
.WizardPage
;
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
.widgets
.Composite
;
25 import org
.eclipse
.swt
.widgets
.Display
;
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
.cdm
.model
.common
.TermType
;
31 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
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
.element
.CdmFormFactory
;
36 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
37 import eu
.etaxonomy
.taxeditor
.ui
.element
.LanguageStringWithLabelElement
;
38 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
39 import eu
.etaxonomy
.taxeditor
.ui
.element
.MultilanguageTextElement
;
40 import eu
.etaxonomy
.taxeditor
.ui
.element
.RootElement
;
43 * <p>TranslationWizardPage class.</p>
46 * @created Sep 29, 2010
49 public class TranslationWizardPage
extends WizardPage
{
51 private static int TEXT_HEIGHT
= 400;
53 private final Map
<Language
, LanguageString
> multilanguageText
;
55 MultilanguageTextElement source
;
57 private final CdmFormFactory formFactory
;
59 private TermComboElement
<Language
> combo_sourceLanguage
;
61 private LanguageStringWithLabelElement text_source
;
63 private TermComboElement
<Language
> combo_targetLanguage
;
65 private LanguageStringWithLabelElement text_target
;
68 * <p>Constructor for TranslationWizardPage.</p>
70 * @param multilanguageText a {@link java.util.Map} object.
72 protected TranslationWizardPage(Map
<Language
, LanguageString
> multilanguageText
) {
73 super("TranslationWizardPage");
74 Assert
.isNotNull(multilanguageText
, "Multilanguage text may not be null");
75 setTitle("Add or edit translations.");
76 this.multilanguageText
= multilanguageText
;
77 formFactory
= new CdmFormFactory(Display
.getCurrent());
78 setPageComplete(false);
82 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
86 public void createControl(Composite parent
) {
87 Composite control
= formFactory
.createComposite(parent
);
88 // control.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_GREEN));
89 control
.setLayout(LayoutConstants
.LAYOUT(2, true));
91 Composite leftContainer
= formFactory
.createComposite(control
);
92 leftContainer
.setLayoutData(LayoutConstants
.FILL());
93 leftContainer
.setLayout(LayoutConstants
.LAYOUT(1, false));
94 // leftContainer.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE));
95 RootElement leftRoot
= new RootElement(formFactory
, leftContainer
);
97 Composite rightContainer
= formFactory
.createComposite(control
);
98 rightContainer
.setLayoutData(LayoutConstants
.FILL());
99 rightContainer
.setLayout(LayoutConstants
.LAYOUT(1, false));
100 // rightContainer.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
101 RootElement rightRoot
= new RootElement(formFactory
, rightContainer
);
103 createLeftControls(leftRoot
);
104 createRightControls(rightRoot
);
106 // initialize with default language
107 setSource(CdmStore
.getDefaultLanguage());
114 * @param defaultLanguage
116 private void setSource(Language language
) {
118 List
<Language
> languages
= Arrays
.asList(new Language
[]{language
});
120 LanguageString preferredLanguageString
= MultilanguageTextHelper
.getPreferredLanguageString(multilanguageText
, languages
);
122 if(preferredLanguageString
!= null){
123 combo_sourceLanguage
.setSelection(preferredLanguageString
.getLanguage());
124 text_source
.setLanguageString(preferredLanguageString
);
133 private void createLeftControls(ICdmFormElement element
){
135 combo_sourceLanguage
= formFactory
.createDefinedTermComboElement(TermType
.Language
, element
, null, null, SWT
.NULL
);
137 combo_sourceLanguage
.setTerms(getLanguages());
139 combo_sourceLanguage
.addSelectionListener(new SelectionAdapter() {
141 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
144 public void widgetSelected(SelectionEvent e
) {
145 text_source
.setLanguageString(multilanguageText
.get(combo_sourceLanguage
.getSelection()));
149 LanguageString preferredLanguageString
= MultilanguageTextHelper
.getPreferredLanguageString(multilanguageText
, getLanguages());
150 if (preferredLanguageString
== null){
151 preferredLanguageString
= LanguageString
.NewInstance("", PreferencesUtil
.getGlobalLanguage());
153 text_source
= formFactory
.createLanguageStringWithLabelElement(element
, null, preferredLanguageString
, TEXT_HEIGHT
, false, SWT
.NULL
);
155 // text_source.setEnabled(false);
157 text_source
.setText(preferredLanguageString
.getText());
164 private void createRightControls(ICdmFormElement element
){
165 combo_targetLanguage
= formFactory
.createDefinedTermComboElement(TermType
.Language
, element
, null, null, SWT
.NULL
);
167 combo_targetLanguage
.addSelectionListener(new SelectionAdapter() {
169 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
172 public void widgetSelected(SelectionEvent e
) {
173 Language language
= combo_targetLanguage
.getSelection();
175 if (language
== null){
176 setPageComplete(false);
180 setPageComplete(true);
181 LanguageString languageString
;
183 if(multilanguageText
.containsKey(language
)){
184 languageString
= multilanguageText
.get(language
);
186 languageString
= LanguageString
.NewInstance("", language
);
189 text_target
.setLanguageString(languageString
);
193 text_target
= formFactory
.createLanguageStringWithLabelElement(element
, null, null, TEXT_HEIGHT
, false, SWT
.NULL
);
196 // The automated translation did rely on the Google Translate API. Unfortunately this service is not free anymore.
197 // See http://code.google.com/apis/language/translate/overview.html
199 // Button button_translationService = formFactory.createButton(element.getLayoutComposite(), "Translate", SWT.PUSH);
200 // button_translationService.addSelectionListener(new SelectionAdapter() {
202 // * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
205 // public void widgetSelected(SelectionEvent event) {
206 // ITranslationServiceWrapper service = new GoogleLanguageApiWrapper();
207 // String translatedText;
209 // String text = text_source.getText();
210 // Language sourceLanguage = text_source.getLanguageString().getLanguage();
211 // Language targetLanguage = combo_targetLanguage.getSelection();
213 // if(targetLanguage == null){
214 // StoreUtil.warningDialog("Please select a target language", TranslationWizardPage.class, "Please select a language from the combo box at the right.");
218 // translatedText = service.translate(text, sourceLanguage, targetLanguage);
219 // text_target.setText(translatedText);
220 // } catch (LanguageNotSupportedException e) {
221 // StoreUtil.warningDialog("Language not supported", TranslationWizardPage.class, "The chosen language is not supported by the translation service.");
222 // } catch (Exception e){
223 // StoreUtil.warningDialog("Error translating text", TranslationWizardPage.class, e.getMessage());
235 private List
<Language
> getLanguages(){
236 Set
<Language
> languageSet
= MultilanguageTextHelper
.getLanguages(multilanguageText
);
238 List
<Language
> languageList
= new ArrayList
<Language
>();
239 // protect against corrupt language sets
240 for (Language language
: languageSet
){
241 if (language
!= null){
242 languageList
.add(language
);
244 MessagingUtils
.warningDialog("Empty languages", getClass(), "The multilanguage string contains invalid data. Please contact the administrator of you datasource.");
252 * @see org.eclipse.jface.dialogs.DialogPage#dispose()
256 public void dispose() {
264 public LanguageString
getTargetLanguageString() {
265 return text_target
.getLanguageString();