p2izing the editor
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / 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.controller.GlobalController;
34 import eu.etaxonomy.taxeditor.model.CdmSessionDataRepository;
35 import eu.etaxonomy.taxeditor.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 String message;
46
47 private Language language;
48 protected Language languageTemp;
49
50 /**
51 * Language selected last time the dialog was used.
52 */
53 private static Language lastLanguage;
54
55 private CCombo combo;
56
57 private List<Language> languages;
58
59
60 /**
61 * @param shell
62 * @param dialogTitle
63 * @param dialogMessage
64 */
65 public LanguageDialog(Shell shell, String dialogTitle, String dialogMessage) {
66 super(shell, dialogTitle,
67 dialogMessage, null, null);
68
69 message = dialogMessage;
70
71 // Set selection menu to language chosen last time Dialog was opened
72 if (lastLanguage != null) {
73 language = lastLanguage;
74 }
75 }
76
77 /**
78 * Lifted from Dialog.
79 *
80 * @param parent
81 * @return
82 * @see Dialog
83 */
84 protected Control createDialogComposite(Composite parent) {
85 // create a composite with standard margins and spacing
86 Composite composite = new Composite(parent, SWT.NONE);
87 GridLayout layout = new GridLayout();
88 layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
89 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
90 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
91 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
92 composite.setLayout(layout);
93 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
94 applyDialogFont(composite);
95 return composite;
96 }
97
98 /*
99 * (non-Javadoc) Method declared on Dialog.
100 */
101 protected Control createDialogArea(Composite parent) {
102 // create composite
103 Composite composite = (Composite) createDialogComposite(parent);
104 // create message
105 if (message != null) {
106 Label label = new Label(composite, SWT.WRAP);
107 label.setText(message);
108 GridData data = new GridData(GridData.GRAB_HORIZONTAL
109 | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL
110 | GridData.VERTICAL_ALIGN_CENTER);
111 data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
112 label.setLayoutData(data);
113 label.setFont(parent.getFont());
114 }
115 // create combo
116 combo = new CCombo(composite, SWT.BORDER);
117 combo.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
118 | GridData.HORIZONTAL_ALIGN_FILL));
119 combo.addSelectionListener(new SelectionAdapter() {
120 @Override
121 public void widgetSelected(SelectionEvent e) {
122 int i = combo.getSelectionIndex();
123 languageTemp = languages.get(i);
124 }
125 });
126
127 populateLanguages();
128
129 applyDialogFont(composite);
130 return composite;
131 }
132
133 /**
134 *
135 */
136 private void populateLanguages() {
137 languages = new ArrayList<Language>();
138 int i = 0;
139 int index = 0;
140 for (Language language : CdmSessionDataRepository.getDefault().getLanguages()) {
141
142 combo.add(LanguageUtil.getDescription(language));
143 languages.add(language);
144
145 if (this.language != null && this.language.equals(language)) {
146 index = i;
147 }
148 i++;
149 }
150 combo.select(index);
151 }
152
153 /**
154 * @return
155 */
156 private Language getLanguage() {
157 return language;
158 }
159
160 /**
161 * @param dialogTitle
162 * @param dialogMessage
163 * @return
164 */
165 public static Language getLanguage(String dialogTitle,
166 String dialogMessage) {
167 LanguageDialog dialog = new LanguageDialog(GlobalController.getShell(),
168 dialogTitle,
169 dialogMessage);
170 if (dialog.open() == Window.CANCEL) {
171 return null;
172 }
173 return dialog.getLanguage();
174 }
175
176 protected void createButtonsForButtonBar(Composite parent) {
177 createButton(parent, IDialogConstants.OK_ID,
178 IDialogConstants.OK_LABEL, true);
179 createButton(parent, IDialogConstants.CANCEL_ID,
180 IDialogConstants.CANCEL_LABEL, false);
181 }
182
183 protected void buttonPressed(int buttonId) {
184 if (IDialogConstants.OK_ID == buttonId) {
185 okPressed();
186 } else if (IDialogConstants.CANCEL_ID == buttonId) {
187 cancelPressed();
188 }
189 }
190
191 protected void okPressed() {
192 language = getLanguageSelection();
193 lastLanguage = language;
194 super.okPressed();
195 }
196
197 protected void cancelPressed() {
198 language = null;
199 super.cancelPressed();
200 }
201
202 private Language getLanguageSelection() {
203 int i = combo.getSelectionIndex();
204 return languages.get(i);
205 }
206 }