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