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
35 public class LanguageEditorPreferencePage
extends CdmPreferencePage
{
38 PreferencesUtil preferencesUtil
= new PreferencesUtil();
39 private boolean isSelectionChanged
= false;
40 private int initalSelectionIndex
;
43 protected Control
createContents(Composite parent
) {
44 Composite container
= new Composite(parent
, SWT
.NULL
);
45 final GridLayout gridLayout
= new GridLayout();
46 container
.setLayout(gridLayout
);
47 createEditorDefaultLanguage(container
);
54 private void createEditorDefaultLanguage(Composite container
) {
55 // TODO Auto-generated method stub
56 final Label description
= new Label(container
, SWT
.NONE
);
57 description
.setText(Messages
.LanguageEditorPreferencePage_RestartRequired
);
59 final Label label
= new Label(container
, SWT
.NONE
);
60 label
.setText(Messages
.LanguageEditorPreferencePage_ChooseDefaultLanguage
);
62 GridData oneLine
= new GridData();
63 oneLine
.grabExcessHorizontalSpace
= true;
64 oneLine
.horizontalAlignment
= GridData
.FILL
;
66 combo
= new CCombo(container
, SWT
.NONE
);
67 combo
.setLayoutData(oneLine
);
69 combo
.add(Language
.GERMAN
.getLabel(), 0);
70 combo
.add(Language
.ENGLISH
.getLabel(), 1);
71 restoreSavedSelection();
73 combo
.addSelectionListener(new SelectionListener() {
76 public void widgetSelected(SelectionEvent e
) {
79 // preferencesUtil.writePropertyToConfigFile(combo.getSelectionIndex());
80 if(initalSelectionIndex
!= combo
.getSelectionIndex()) {
81 isSelectionChanged
= true;
83 isSelectionChanged
= false;
85 // } catch (IOException e1) {
86 // MessagingUtils.messageDialog("Failed to write Config.ini", LanguageEditorPreferencePage.class,
87 // "Language switch failed, because could not write to Folder. No writing permissions!", null);
92 public void widgetDefaultSelected(SelectionEvent e
) {
93 // TODO Auto-generated method stub
100 * TODO: This method is not taking advantages of the enum field yet
102 private void restoreSavedSelection() {
103 String rememberedValue
= PreferencesUtil
.getStringValue(IPreferenceKeys
.DEFAULT_LANGUAGE_EDITOR
);
104 if(StringUtils
.isNotEmpty(rememberedValue
)&& StringUtils
.isNotBlank(rememberedValue
)){
105 if(rememberedValue
.equalsIgnoreCase("en")){ //$NON-NLS-1$
106 initalSelectionIndex
= 1;
108 }else if(rememberedValue
.equalsIgnoreCase("de")){ //$NON-NLS-1$
109 initalSelectionIndex
= 0;
113 Locale locale
= Locale
.getDefault();
114 if(locale
.getLanguage().equals(new Locale("de").getLanguage())){
115 initalSelectionIndex
= 0;
117 }else if(locale
.getLanguage().equals(new Locale("en").getLanguage())){
118 initalSelectionIndex
= 1;
124 private enum Language
{
126 GERMAN("Deutsch"), ENGLISH("English"); //$NON-NLS-1$ //$NON-NLS-2$
127 private final String label
;
128 private Language(String label
){
135 public String
getLabel() {
141 public boolean performOk() {
143 if(isSelectionChanged
){
144 preferencesUtil
.writePropertyToConfigFile(combo
.getSelectionIndex());
145 boolean result
= MessageDialog
.openConfirm(getShell(), Messages
.LanguageEditorPreferencePage_PleaseRestart
, Messages
.LanguageEditorPreferencePage_EditorHasToRestart
);
148 PlatformUI
.getWorkbench().restart();
151 } catch (IOException e
) {
154 return super.performOk();