4c4578719796d02838d45ea76fde4f4d018e27e1
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / MultilanguageTextElement.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.element;
12
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Map;
18
19 import org.eclipse.jface.util.PropertyChangeEvent;
20 import org.eclipse.jface.window.Window;
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;
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.cdm.model.common.TermType;
32 import eu.etaxonomy.taxeditor.model.DefaultTermComparator;
33 import eu.etaxonomy.taxeditor.model.MessagingUtils;
34 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
35 import eu.etaxonomy.taxeditor.store.CdmStore;
36 import eu.etaxonomy.taxeditor.ui.combo.TermComboElement;
37 import eu.etaxonomy.taxeditor.ui.translation.TranslationWizard;
38
39 /**
40 * <p>
41 * MultilanguageTextElement class.
42 * </p>
43 *
44 * @author n.hoffmann
45 * @created Sep 29, 2010
46 * @version 1.0
47 */
48 public class MultilanguageTextElement extends AbstractCdmFormElement implements
49 SelectionListener {
50
51 private class OpenTranslationWizard extends SelectionAdapter {
52
53 public OpenTranslationWizard() {
54 super();
55 }
56
57 /*
58 * (non-Javadoc)
59 *
60 * @see
61 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
62 * .swt.events.SelectionEvent)
63 */
64 @Override
65 public void widgetSelected(SelectionEvent e) {
66 Map<Language, LanguageString> multilanguageText = getMultilanguageText();
67
68 if (multilanguageText == null){
69 MessagingUtils.warningDialog("No text to transalte", getClass(), "The text is empty and therefore can not be translated");
70 }
71
72 TranslationWizard wizard = new TranslationWizard(multilanguageText);
73 WizardDialog dialog = new WizardDialog(getLayoutComposite()
74 .getShell(), wizard);
75
76 if (dialog.open() == Window.OK) {
77 combo_language.setTerms(getLanguages());
78 updateControls();
79 }
80
81 }
82 }
83
84 private TermComboElement<Language> combo_language;
85
86 protected LanguageStringWithLabelElement element_languageString;
87
88 private Button button;
89
90 private Map<Language, LanguageString> multilanguageText;
91
92 /**
93 * <p>
94 * Constructor for MultilanguageTextElement.
95 * </p>
96 *
97 * @param formFactory
98 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
99 * object.
100 * @param formElement
101 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
102 * object.
103 * @param labelString
104 * a {@link java.lang.String} object.
105 * @param multilanguageText
106 * a {@link java.util.Map} object.
107 * @param textHeight
108 * a {@link java.lang.Integer} object.
109 * @param style
110 * a int.
111 */
112 public MultilanguageTextElement(CdmFormFactory formFactory,
113 ICdmFormElement formElement, String labelString,
114 Map<Language, LanguageString> multilanguageText,
115 Integer textHeight, int style) {
116 super(formFactory, formElement);
117
118 formFactory.addPropertyChangeListener(this);
119
120 element_languageString = this.formFactory
121 .createLanguageStringWithLabelElement(formElement, labelString,
122 null, textHeight, true, style);
123
124 if (PreferencesUtil.isMultilanguageTextEditingCapability()) {
125 createMultilanguageTextEditingElements(formElement,
126 multilanguageText, style);
127 }
128 setMultilanguageText(multilanguageText);
129 }
130
131 private void createMultilanguageTextEditingElements(
132 ICdmFormElement formElement,
133 Map<Language, LanguageString> multilanguageText, int style) {
134 combo_language = formFactory.createDefinedTermComboElement(TermType.Language, formElement,
135 "", null, style);
136 combo_language.addSelectionListener(this);
137 addControl(formFactory.createLabel(getLayoutComposite(), ""));
138 button = formFactory.createButton(getLayoutComposite(),
139 "Open In Translation Editor", SWT.PUSH);
140 addControl(button);
141 button.setLayoutData(LayoutConstants.RIGHT());
142 button.addSelectionListener(new OpenTranslationWizard());
143 }
144
145 private List<Language> getLanguages() {
146
147 ArrayList<Language> languageList = new ArrayList<Language>(
148 getMultilanguageText().keySet());
149
150 Collections.sort(languageList, new DefaultTermComparator<Language>());
151
152 return languageList;
153 }
154
155 /*
156 * (non-Javadoc)
157 *
158 * @see
159 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt
160 * .events.SelectionEvent)
161 */
162 /** {@inheritDoc} */
163 @Override
164 public void widgetSelected(SelectionEvent e) {
165 Language selectedLanguage = combo_language.getSelection();
166 if (selectedLanguage != null) {
167 LanguageString selectedLanguageString = getMultilanguageText().get(
168 selectedLanguage);
169
170 element_languageString.setLanguageString(selectedLanguageString);
171 }
172 }
173
174 /*
175 * (non-Javadoc)
176 *
177 * @see
178 * org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse
179 * .swt.events.SelectionEvent)
180 */
181 /** {@inheritDoc} */
182 @Override
183 public void widgetDefaultSelected(SelectionEvent e) {
184 }
185
186 /**
187 * <p>
188 * Setter for the field <code>multilanguageText</code>.
189 * </p>
190 *
191 * @param multilanguageText
192 * the multilanguageText to set
193 */
194 public void setMultilanguageText(
195 Map<Language, LanguageString> multilanguageText) {
196 this.multilanguageText = multilanguageText;
197
198 if (multilanguageText != null) {
199 if (PreferencesUtil.isMultilanguageTextEditingCapability()) {
200 combo_language.setTerms(getLanguages());
201 button.setEnabled(false);
202 }
203 updateControls();
204 }
205 }
206
207 /**
208 *
209 */
210 private void updateControls() {
211 LanguageString preferredLanguageString = MultilanguageTextHelper
212 .getPreferredLanguageString(getMultilanguageText(),
213 Arrays.asList(new Language[] { CdmStore
214 .getDefaultLanguage() }));
215
216 element_languageString.setLanguageString(preferredLanguageString);
217 if (PreferencesUtil.isMultilanguageTextEditingCapability()) {
218 combo_language.setSelection(preferredLanguageString.getLanguage());
219 button.setEnabled(true);
220 }
221 }
222
223 /**
224 * <p>
225 * Getter for the field <code>multilanguageText</code>.
226 * </p>
227 *
228 * @return the multilanguageText
229 */
230 public Map<Language, LanguageString> getMultilanguageText() {
231 return multilanguageText;
232 }
233
234 /*
235 * (non-Javadoc)
236 *
237 * @see
238 * eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#propertyChange(org
239 * .eclipse.jface.util.PropertyChangeEvent)
240 */
241 /** {@inheritDoc} */
242 @Override
243 public void propertyChange(PropertyChangeEvent event) {
244 if (event == null) {
245 return;
246 }
247 Object eventSource = event.getSource();
248 if (eventSource == element_languageString) {
249 LanguageString languageString = ((LanguageStringWithLabelElement) eventSource)
250 .getLanguageString();
251 multilanguageText.put(languageString.getLanguage(), languageString);
252 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event));
253 }
254 }
255 }