4f1f5071812d1aeb7bf5e5d5e4d8e26e03fcef43
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / translation / TranslationWizardPage.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.ui.translation;
12
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18
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;
27
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;
39
40 /**
41 * <p>TranslationWizardPage class.</p>
42 *
43 * @author n.hoffmann
44 * @created Sep 29, 2010
45 * @version 1.0
46 */
47 public class TranslationWizardPage extends WizardPage{
48
49 private static int TEXT_HEIGHT = 400;
50
51 private Map<Language, LanguageString> multilanguageText;
52
53 MultilanguageTextElement source;
54
55 private CdmFormFactory formFactory;
56
57 private TermComboElement combo_sourceLanguage;
58
59 private LanguageStringWithLabelElement text_source;
60
61 private TermComboElement<Language> combo_targetLanguage;
62
63 private LanguageStringWithLabelElement text_target;
64
65 /**
66 * <p>Constructor for TranslationWizardPage.</p>
67 *
68 * @param multilanguageText a {@link java.util.Map} object.
69 */
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());
76 }
77
78 /* (non-Javadoc)
79 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
80 */
81 /** {@inheritDoc} */
82 @Override
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));
87
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);
93
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);
99
100 createLeftControls(leftRoot);
101 createRightControls(rightRoot);
102
103 // initialize with default language
104 setSource(CdmStore.getDefaultLanguage());
105
106
107 setControl(control);
108 }
109
110 /**
111 * @param defaultLanguage
112 */
113 private void setSource(Language language) {
114
115 List<Language> languages = Arrays.asList(new Language[]{language});
116
117 LanguageString preferredLanguageString = MultilanguageTextHelper.getPreferredLanguageString(multilanguageText, languages);
118
119 if(preferredLanguageString != null){
120 combo_sourceLanguage.setSelection(preferredLanguageString.getLanguage());
121 text_source.setLanguageString(preferredLanguageString);
122 }
123 }
124
125
126 /**
127 *
128 * @param element
129 */
130 private void createLeftControls(ICdmFormElement element){
131
132 combo_sourceLanguage = formFactory.createTermComboElement(null, element, null, null, SWT.NULL);
133 combo_sourceLanguage.setTerms(getLanguages());
134
135 combo_sourceLanguage.addSelectionListener(new SelectionAdapter() {
136 /* (non-Javadoc)
137 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
138 */
139 @Override
140 public void widgetSelected(SelectionEvent e) {
141 text_source.setLanguageString(multilanguageText.get(combo_sourceLanguage.getSelection()));
142 }
143 });
144
145 LanguageString preferredLanguageString = MultilanguageTextHelper.getPreferredLanguageString(multilanguageText, getLanguages());
146
147 text_source = formFactory.createLanguageStringWithLabelElement(element, null, preferredLanguageString, TEXT_HEIGHT, SWT.NULL);
148
149 // text_source.setEnabled(false);
150
151 text_source.setText(preferredLanguageString.getText());
152 }
153
154 /**
155 *
156 * @param element
157 */
158 private void createRightControls(ICdmFormElement element){
159 combo_targetLanguage = formFactory.createTermComboElement(Language.class, element, null, null, SWT.NULL);
160
161 combo_targetLanguage.addSelectionListener(new SelectionAdapter() {
162 /* (non-Javadoc)
163 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
164 */
165 @Override
166 public void widgetSelected(SelectionEvent e) {
167 Language language = combo_targetLanguage.getSelection();
168 LanguageString languageString;
169
170 if(multilanguageText.containsKey(language)){
171 languageString = multilanguageText.get(language);
172 }else{
173 languageString = LanguageString.NewInstance("", language);
174 multilanguageText.put(language, languageString);
175 }
176
177 text_target.setLanguageString(languageString);
178 }
179 });
180
181 text_target = formFactory.createLanguageStringWithLabelElement(element, null, null, TEXT_HEIGHT, SWT.NULL);
182
183 Button button_translationService = formFactory.createButton(element.getLayoutComposite(), "Translate", SWT.PUSH);
184 button_translationService.addSelectionListener(new SelectionAdapter() {
185 /* (non-Javadoc)
186 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
187 */
188 @Override
189 public void widgetSelected(SelectionEvent event) {
190 ITranslationServiceWrapper service = new GoogleLanguageApiWrapper();
191 String translatedText;
192 try {
193 String text = text_source.getText();
194 Language sourceLanguage = text_source.getLanguageString().getLanguage();
195 Language targetLanguage = combo_targetLanguage.getSelection();
196
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());
203 }
204
205 }
206 });
207 }
208
209
210 /**
211 *
212 * @return
213 */
214 private List<Language> getLanguages(){
215 Set<Language> languageSet = MultilanguageTextHelper.getLanguages(multilanguageText);
216
217 List<Language> languageList = new ArrayList<Language>(languageSet);
218
219 return languageList;
220 }
221
222 /* (non-Javadoc)
223 * @see org.eclipse.jface.dialogs.DialogPage#dispose()
224 */
225 /** {@inheritDoc} */
226 @Override
227 public void dispose() {
228 super.dispose();
229 }
230 }