2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.taxeditor
.preference
;
11 import java
.io
.IOException
;
12 import java
.util
.Locale
;
14 import org
.apache
.commons
.lang3
.StringUtils
;
15 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
16 import org
.eclipse
.swt
.SWT
;
17 import org
.eclipse
.swt
.custom
.CCombo
;
18 import org
.eclipse
.swt
.events
.SelectionEvent
;
19 import org
.eclipse
.swt
.events
.SelectionListener
;
20 import org
.eclipse
.swt
.layout
.GridData
;
21 import org
.eclipse
.swt
.layout
.GridLayout
;
22 import org
.eclipse
.swt
.widgets
.Composite
;
23 import org
.eclipse
.swt
.widgets
.Control
;
24 import org
.eclipse
.swt
.widgets
.Label
;
25 import org
.eclipse
.ui
.PlatformUI
;
27 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
28 import eu
.etaxonomy
.taxeditor
.preference
.menu
.CdmPreferencePage
;
32 * @created Dec 3, 2010
34 public class LanguageEditorPreferencePage
extends CdmPreferencePage
{
37 PreferencesUtil preferencesUtil
= new PreferencesUtil();
38 private boolean isSelectionChanged
= false;
39 private int initalSelectionIndex
;
42 protected Control
createContents(Composite parent
) {
43 Composite container
= new Composite(parent
, SWT
.NULL
);
44 final GridLayout gridLayout
= new GridLayout();
45 container
.setLayout(gridLayout
);
46 createEditorDefaultLanguage(container
);
53 private void createEditorDefaultLanguage(Composite container
) {
54 // TODO Auto-generated method stub
55 final Label description
= new Label(container
, SWT
.NONE
);
56 description
.setText(Messages
.LanguageEditorPreferencePage_RestartRequired
);
58 final Label label
= new Label(container
, SWT
.NONE
);
59 label
.setText(Messages
.LanguageEditorPreferencePage_ChooseDefaultLanguage
);
61 GridData oneLine
= new GridData();
62 oneLine
.grabExcessHorizontalSpace
= true;
63 oneLine
.horizontalAlignment
= GridData
.FILL
;
65 combo
= new CCombo(container
, SWT
.NONE
);
66 combo
.setLayoutData(oneLine
);
68 combo
.add(Language
.GERMAN
.getLabel(), 0);
69 combo
.add(Language
.ENGLISH
.getLabel(), 1);
70 restoreSavedSelection();
72 combo
.addSelectionListener(new SelectionListener() {
75 public void widgetSelected(SelectionEvent e
) {
78 // preferencesUtil.writePropertyToConfigFile(combo.getSelectionIndex());
79 if(initalSelectionIndex
!= combo
.getSelectionIndex()) {
80 isSelectionChanged
= true;
82 isSelectionChanged
= false;
84 // } catch (IOException e1) {
85 // MessagingUtils.messageDialog("Failed to write Config.ini", LanguageEditorPreferencePage.class,
86 // "Language switch failed, because could not write to Folder. No writing permissions!", null);
91 public void widgetDefaultSelected(SelectionEvent e
) {
92 // TODO Auto-generated method stub
99 * TODO: This method is not taking advantages of the enum field yet
101 private void restoreSavedSelection() {
102 String rememberedValue
= PreferencesUtil
.getStringValue(IPreferenceKeys
.DEFAULT_LANGUAGE_EDITOR
);
103 if(StringUtils
.isNotEmpty(rememberedValue
)&& StringUtils
.isNotBlank(rememberedValue
)){
104 if(rememberedValue
.equalsIgnoreCase("en")){ //$NON-NLS-1$
105 initalSelectionIndex
= 1;
107 }else if(rememberedValue
.equalsIgnoreCase("de")){ //$NON-NLS-1$
108 initalSelectionIndex
= 0;
112 Locale locale
= Locale
.getDefault();
113 if(locale
.getLanguage().equals(new Locale("de").getLanguage())){
114 initalSelectionIndex
= 0;
116 }else if(locale
.getLanguage().equals(new Locale("en").getLanguage())){
117 initalSelectionIndex
= 1;
123 private enum Language
{
125 GERMAN("Deutsch"), ENGLISH("English"); //$NON-NLS-1$ //$NON-NLS-2$
126 private final String label
;
127 private Language(String label
){
134 public String
getLabel() {
140 public boolean performOk() {
142 if(isSelectionChanged
){
143 preferencesUtil
.writePropertyToConfigFile(combo
.getSelectionIndex());
144 boolean result
= MessageDialog
.openConfirm(getShell(), Messages
.LanguageEditorPreferencePage_PleaseRestart
, Messages
.LanguageEditorPreferencePage_EditorHasToRestart
);
147 PlatformUI
.getWorkbench().restart();
150 } catch (IOException e
) {
153 return super.performOk();