3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.editor
.description
;
13 import java
.util
.ArrayList
;
14 import java
.util
.List
;
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
;
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
;
42 public class LanguageDialog
extends InputDialog
{
43 private static final Logger logger
= Logger
.getLogger(LanguageDialog
.class);
45 private static final int VISIBLE_ITEMS
= 20;
47 private String message
;
49 private Language language
;
50 protected Language languageTemp
;
53 * Language selected last time the dialog was used.
55 private static Language lastLanguage
;
59 private List
<Language
> languages
;
65 * @param dialogMessage
67 public LanguageDialog(Shell shell
, String dialogTitle
, String dialogMessage
) {
68 super(shell
, dialogTitle
,
69 dialogMessage
, null, null);
71 message
= dialogMessage
;
73 // Set selection menu to language chosen last time Dialog was opened
74 if (lastLanguage
!= null) {
75 language
= lastLanguage
;
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
);
101 * (non-Javadoc) Method declared on Dialog.
103 protected Control
createDialogArea(Composite parent
) {
105 Composite composite
= (Composite
) createDialogComposite(parent
);
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());
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() {
124 public void widgetSelected(SelectionEvent e
) {
125 int i
= combo
.getSelectionIndex();
126 languageTemp
= languages
.get(i
);
132 applyDialogFont(composite
);
139 private void populateLanguages() {
140 languages
= new ArrayList
<Language
>();
143 for (Language language
: CdmStore
.getLanguages()) {
145 String description
= LanguageUtil
.getDescription(language
);
146 if (description
== null) {
147 logger
.warn("No description for " + language
+ " - not added to language menu");
150 combo
.add(description
);
151 languages
.add(language
);
153 if (this.language
!= null && this.language
.equals(language
)) {
164 private Language
getLanguage() {
170 * @param dialogMessage
173 public static Language
getLanguage(String dialogTitle
,
174 String dialogMessage
) {
175 LanguageDialog dialog
= new LanguageDialog(EditorUtil
.getShell(),
178 if (dialog
.open() == Window
.CANCEL
) {
181 return dialog
.getLanguage();
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);
191 protected void buttonPressed(int buttonId
) {
192 if (IDialogConstants
.OK_ID
== buttonId
) {
194 } else if (IDialogConstants
.CANCEL_ID
== buttonId
) {
199 protected void okPressed() {
200 language
= getLanguageSelection();
201 lastLanguage
= language
;
205 protected void cancelPressed() {
207 super.cancelPressed();
210 private Language
getLanguageSelection() {
211 int i
= combo
.getSelectionIndex();
212 return languages
.get(i
);