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
.Button
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
26 import org
.eclipse
.swt
.widgets
.Display
;
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
.store
.CdmStore
;
32 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
33 import eu
.etaxonomy
.taxeditor
.ui
.combo
.TermComboElement
;
34 import eu
.etaxonomy
.taxeditor
.ui
.forms
.CdmFormFactory
;
35 import eu
.etaxonomy
.taxeditor
.ui
.forms
.ICdmFormElement
;
36 import eu
.etaxonomy
.taxeditor
.ui
.forms
.LanguageStringWithLabelElement
;
37 import eu
.etaxonomy
.taxeditor
.ui
.forms
.MultilanguageTextElement
;
38 import eu
.etaxonomy
.taxeditor
.ui
.forms
.RootElement
;
41 * <p>TranslationWizardPage class.</p>
44 * @created Sep 29, 2010
47 public class TranslationWizardPage
extends WizardPage
{
49 private static int TEXT_HEIGHT
= 400;
51 private Map
<Language
, LanguageString
> multilanguageText
;
53 MultilanguageTextElement source
;
55 private CdmFormFactory formFactory
;
57 private TermComboElement combo_sourceLanguage
;
59 private LanguageStringWithLabelElement text_source
;
61 private TermComboElement
<Language
> combo_targetLanguage
;
63 private LanguageStringWithLabelElement text_target
;
66 * <p>Constructor for TranslationWizardPage.</p>
68 * @param multilanguageText a {@link java.util.Map} object.
70 protected TranslationWizardPage(Map
<Language
, LanguageString
> multilanguageText
) {
71 super("TranslationWizardPage");
72 Assert
.isNotNull(multilanguageText
, "Multilanguage text may not be null");
73 setTitle("Add or edit translations.");
74 this.multilanguageText
= multilanguageText
;
75 formFactory
= new CdmFormFactory(Display
.getCurrent());
79 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
83 public void createControl(Composite parent
) {
84 Composite control
= formFactory
.createComposite(parent
);
85 // control.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_GREEN));
86 control
.setLayout(CdmFormFactory
.LAYOUT(2, true));
88 Composite leftContainer
= formFactory
.createComposite(control
);
89 leftContainer
.setLayoutData(CdmFormFactory
.FILL());
90 leftContainer
.setLayout(CdmFormFactory
.LAYOUT(1, false));
91 // leftContainer.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE));
92 RootElement leftRoot
= new RootElement(formFactory
, leftContainer
);
94 Composite rightContainer
= formFactory
.createComposite(control
);
95 rightContainer
.setLayoutData(CdmFormFactory
.FILL());
96 rightContainer
.setLayout(CdmFormFactory
.LAYOUT(1, false));
97 // rightContainer.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
98 RootElement rightRoot
= new RootElement(formFactory
, rightContainer
);
100 createLeftControls(leftRoot
);
101 createRightControls(rightRoot
);
103 // initialize with default language
104 setSource(CdmStore
.getDefaultLanguage());
111 * @param defaultLanguage
113 private void setSource(Language language
) {
115 List
<Language
> languages
= Arrays
.asList(new Language
[]{language
});
117 LanguageString preferredLanguageString
= MultilanguageTextHelper
.getPreferredLanguageString(multilanguageText
, languages
);
119 if(preferredLanguageString
!= null){
120 combo_sourceLanguage
.setSelection(preferredLanguageString
.getLanguage());
121 text_source
.setLanguageString(preferredLanguageString
);
130 private void createLeftControls(ICdmFormElement element
){
132 combo_sourceLanguage
= formFactory
.createTermComboElement(null, element
, null, null, SWT
.NULL
);
133 combo_sourceLanguage
.setTerms(getLanguages());
135 combo_sourceLanguage
.addSelectionListener(new SelectionAdapter() {
137 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
140 public void widgetSelected(SelectionEvent e
) {
141 text_source
.setLanguageString(multilanguageText
.get(combo_sourceLanguage
.getSelection()));
145 LanguageString preferredLanguageString
= MultilanguageTextHelper
.getPreferredLanguageString(multilanguageText
, getLanguages());
147 text_source
= formFactory
.createLanguageStringWithLabelElement(element
, null, preferredLanguageString
, TEXT_HEIGHT
, SWT
.NULL
);
149 // text_source.setEnabled(false);
151 text_source
.setText(preferredLanguageString
.getText());
158 private void createRightControls(ICdmFormElement element
){
159 combo_targetLanguage
= formFactory
.createTermComboElement(Language
.class, element
, null, null, SWT
.NULL
);
161 combo_targetLanguage
.addSelectionListener(new SelectionAdapter() {
163 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
166 public void widgetSelected(SelectionEvent e
) {
167 Language language
= combo_targetLanguage
.getSelection();
168 LanguageString languageString
;
170 if(multilanguageText
.containsKey(language
)){
171 languageString
= multilanguageText
.get(language
);
173 languageString
= LanguageString
.NewInstance("", language
);
174 multilanguageText
.put(language
, languageString
);
177 text_target
.setLanguageString(languageString
);
181 text_target
= formFactory
.createLanguageStringWithLabelElement(element
, null, null, TEXT_HEIGHT
, SWT
.NULL
);
183 Button button_translationService
= formFactory
.createButton(element
.getLayoutComposite(), "Translate", SWT
.PUSH
);
184 button_translationService
.addSelectionListener(new SelectionAdapter() {
186 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
189 public void widgetSelected(SelectionEvent event
) {
190 ITranslationServiceWrapper service
= new GoogleLanguageApiWrapper();
191 String translatedText
;
193 String text
= text_source
.getText();
194 Language sourceLanguage
= text_source
.getLanguageString().getLanguage();
195 Language targetLanguage
= combo_targetLanguage
.getSelection();
197 translatedText
= service
.translate(text
, sourceLanguage
, targetLanguage
);
198 text_target
.setText(translatedText
);
199 } catch (LanguageNotSupportedException e
) {
200 StoreUtil
.warningDialog("Language not supported", TranslationWizardPage
.class, e
.getMessage());
201 } catch (Exception e
){
202 StoreUtil
.warningDialog("Error translating text", TranslationWizardPage
.class, e
.getMessage());
214 private List
<Language
> getLanguages(){
215 Set
<Language
> languageSet
= MultilanguageTextHelper
.getLanguages(multilanguageText
);
217 List
<Language
> languageList
= new ArrayList
<Language
>(languageSet
);
223 * @see org.eclipse.jface.dialogs.DialogPage#dispose()
227 public void dispose() {