Moving editor sources back into trunk
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / description / LanguageDialog.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.editor.description;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.apache.log4j.Logger;
17 import org.eclipse.jface.dialogs.Dialog;
18 import org.eclipse.jface.dialogs.IDialogConstants;
19 import org.eclipse.jface.dialogs.InputDialog;
20 import org.eclipse.jface.window.Window;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.custom.CCombo;
23 import org.eclipse.swt.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Shell;
31
32 import eu.etaxonomy.cdm.model.common.Language;
33 import eu.etaxonomy.taxeditor.editor.EditorUtil;
34 import eu.etaxonomy.taxeditor.store.CdmStore;
35 import eu.etaxonomy.taxeditor.store.model.LanguageUtil;
36
37 /**
38 * @author p.ciardelli
39 * @created 02.04.2009
40 * @version 1.0
41 */
42 public class LanguageDialog extends InputDialog {
43 private static final Logger logger = Logger.getLogger(LanguageDialog.class);
44
45 private static final int VISIBLE_ITEMS = 20;
46
47 private String message;
48
49 private Language language;
50 protected Language languageTemp;
51
52 /**
53 * Language selected last time the dialog was used.
54 */
55 private static Language lastLanguage;
56
57 private CCombo combo;
58
59 private List<Language> languages;
60
61
62 /**
63 * @param shell
64 * @param dialogTitle
65 * @param dialogMessage
66 */
67 public LanguageDialog(Shell shell, String dialogTitle, String dialogMessage) {
68 super(shell, dialogTitle,
69 dialogMessage, null, null);
70
71 message = dialogMessage;
72
73 // Set selection menu to language chosen last time Dialog was opened
74 if (lastLanguage != null) {
75 language = lastLanguage;
76 }
77 }
78
79 /**
80 * Lifted from Dialog.
81 *
82 * @param parent
83 * @return
84 * @see Dialog
85 */
86 protected Control createDialogComposite(Composite parent) {
87 // create a composite with standard margins and spacing
88 Composite composite = new Composite(parent, SWT.NONE);
89 GridLayout layout = new GridLayout();
90 layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
91 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
92 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
93 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
94 composite.setLayout(layout);
95 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
96 applyDialogFont(composite);
97 return composite;
98 }
99
100 /*
101 * (non-Javadoc) Method declared on Dialog.
102 */
103 protected Control createDialogArea(Composite parent) {
104 // create composite
105 Composite composite = (Composite) createDialogComposite(parent);
106 // create message
107 if (message != null) {
108 Label label = new Label(composite, SWT.WRAP);
109 label.setText(message);
110 GridData data = new GridData(GridData.GRAB_HORIZONTAL
111 | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL
112 | GridData.VERTICAL_ALIGN_CENTER);
113 data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
114 label.setLayoutData(data);
115 label.setFont(parent.getFont());
116 }
117 // create combo
118 combo = new CCombo(composite, SWT.BORDER);
119 combo.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
120 | GridData.HORIZONTAL_ALIGN_FILL));
121 combo.setVisibleItemCount(VISIBLE_ITEMS);
122 combo.addSelectionListener(new SelectionAdapter() {
123 @Override
124 public void widgetSelected(SelectionEvent e) {
125 int i = combo.getSelectionIndex();
126 languageTemp = languages.get(i);
127 }
128 });
129
130 populateLanguages();
131
132 applyDialogFont(composite);
133 return composite;
134 }
135
136 /**
137 *
138 */
139 private void populateLanguages() {
140 languages = new ArrayList<Language>();
141 int i = 0;
142 int index = 0;
143 for (Language language : CdmStore.getLanguages()) {
144
145 String description = LanguageUtil.getDescription(language);
146 if (description == null) {
147 logger.warn("No description for " + language + " - not added to language menu");
148 continue;
149 }
150 combo.add(description);
151 languages.add(language);
152
153 if (this.language != null && this.language.equals(language)) {
154 index = i;
155 }
156 i++;
157 }
158 combo.select(index);
159 }
160
161 /**
162 * @return
163 */
164 private Language getLanguage() {
165 return language;
166 }
167
168 /**
169 * @param dialogTitle
170 * @param dialogMessage
171 * @return
172 */
173 public static Language getLanguage(String dialogTitle,
174 String dialogMessage) {
175 LanguageDialog dialog = new LanguageDialog(EditorUtil.getShell(),
176 dialogTitle,
177 dialogMessage);
178 if (dialog.open() == Window.CANCEL) {
179 return null;
180 }
181 return dialog.getLanguage();
182 }
183
184 protected void createButtonsForButtonBar(Composite parent) {
185 createButton(parent, IDialogConstants.OK_ID,
186 IDialogConstants.OK_LABEL, true);
187 createButton(parent, IDialogConstants.CANCEL_ID,
188 IDialogConstants.CANCEL_LABEL, false);
189 }
190
191 protected void buttonPressed(int buttonId) {
192 if (IDialogConstants.OK_ID == buttonId) {
193 okPressed();
194 } else if (IDialogConstants.CANCEL_ID == buttonId) {
195 cancelPressed();
196 }
197 }
198
199 protected void okPressed() {
200 language = getLanguageSelection();
201 lastLanguage = language;
202 super.okPressed();
203 }
204
205 protected void cancelPressed() {
206 language = null;
207 super.cancelPressed();
208 }
209
210 private Language getLanguageSelection() {
211 int i = combo.getSelectionIndex();
212 return languages.get(i);
213 }
214 }