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.
10 package eu
.etaxonomy
.taxeditor
.preference
;
12 import java
.io
.IOException
;
13 import java
.util
.Locale
;
15 import org
.apache
.commons
.lang
.StringUtils
;
16 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
17 import org
.eclipse
.swt
.SWT
;
18 import org
.eclipse
.swt
.custom
.CCombo
;
19 import org
.eclipse
.swt
.events
.SelectionEvent
;
20 import org
.eclipse
.swt
.events
.SelectionListener
;
21 import org
.eclipse
.swt
.layout
.GridData
;
22 import org
.eclipse
.swt
.layout
.GridLayout
;
23 import org
.eclipse
.swt
.widgets
.Composite
;
24 import org
.eclipse
.swt
.widgets
.Control
;
25 import org
.eclipse
.swt
.widgets
.Label
;
26 import org
.eclipse
.ui
.PlatformUI
;
28 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
29 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
30 import eu
.etaxonomy
.taxeditor
.preference
.menu
.CdmPreferencePage
;
34 * @created Dec 3, 2010
37 public class LanguageEditorPreferencePage
extends CdmPreferencePage
{
40 PreferencesUtil preferencesUtil
= new PreferencesUtil();
41 private boolean isSelectionChanged
= false;
42 private int initalSelectionIndex
;
45 protected Control
createContents(Composite parent
) {
46 Composite container
= new Composite(parent
, SWT
.NULL
);
47 final GridLayout gridLayout
= new GridLayout();
48 container
.setLayout(gridLayout
);
49 createEditorDefaultLanguage(container
);
56 private void createEditorDefaultLanguage(Composite container
) {
57 // TODO Auto-generated method stub
58 final Label description
= new Label(container
, SWT
.NONE
);
59 description
.setText(Messages
.LanguageEditorPreferencePage_RestartRequired
);
61 final Label label
= new Label(container
, SWT
.NONE
);
62 label
.setText(Messages
.LanguageEditorPreferencePage_ChooseDefaultLanguage
);
64 GridData oneLine
= new GridData();
65 oneLine
.grabExcessHorizontalSpace
= true;
66 oneLine
.horizontalAlignment
= GridData
.FILL
;
68 combo
= new CCombo(container
, SWT
.NONE
);
69 combo
.setLayoutData(oneLine
);
71 combo
.add(Language
.GERMAN
.getLabel(), 0);
72 combo
.add(Language
.ENGLISH
.getLabel(), 1);
73 restoreSavedSelection();
75 combo
.addSelectionListener(new SelectionListener() {
78 public void widgetSelected(SelectionEvent e
) {
80 preferencesUtil
.writePropertyToConfigFile(combo
.getSelectionIndex());
81 if(initalSelectionIndex
!= combo
.getSelectionIndex()) {
82 isSelectionChanged
= true;
84 isSelectionChanged
= false;
86 } catch (IOException e1
) {
87 MessagingUtils
.messageDialog("Failed to write Config.ini", LanguageEditorPreferencePage
.class,
88 "Language switch failed, because could not write to Folder. No writing permissions!", null);
93 public void widgetDefaultSelected(SelectionEvent e
) {
94 // TODO Auto-generated method stub
101 * TODO: This method is not taking advantages of the enum field yet
103 private void restoreSavedSelection() {
104 String rememberedValue
= PreferencesUtil
.getPreferenceStore().getString(IPreferenceKeys
.DEFAULT_LANGUAGE_EDITOR
);
105 if(StringUtils
.isNotEmpty(rememberedValue
)&& StringUtils
.isNotBlank(rememberedValue
)){
106 if(rememberedValue
.equalsIgnoreCase("en")){ //$NON-NLS-1$
107 initalSelectionIndex
= 1;
109 }else if(rememberedValue
.equalsIgnoreCase("de")){ //$NON-NLS-1$
110 initalSelectionIndex
= 0;
114 Locale locale
= Locale
.getDefault();
115 if(locale
.getLanguage().equals(new Locale("de").getLanguage())){
116 initalSelectionIndex
= 0;
118 }else if(locale
.getLanguage().equals(new Locale("en").getLanguage())){
119 initalSelectionIndex
= 1;
125 private enum Language
{
127 GERMAN("Deutsch"), ENGLISH("English"); //$NON-NLS-1$ //$NON-NLS-2$
128 private final String label
;
129 private Language(String label
){
136 public String
getLabel() {
142 public boolean performOk() {
144 if(isSelectionChanged
){
145 preferencesUtil
.writePropertyToConfigFile(combo
.getSelectionIndex());
146 boolean result
= MessageDialog
.openConfirm(getShell(), Messages
.LanguageEditorPreferencePage_PleaseRestart
, Messages
.LanguageEditorPreferencePage_EditorHasToRestart
);
149 PlatformUI
.getWorkbench().restart();
152 } catch (IOException e
) {
155 return super.performOk();