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
.jface
.preference
.PreferencePage
;
18 import org
.eclipse
.swt
.SWT
;
19 import org
.eclipse
.swt
.custom
.CCombo
;
20 import org
.eclipse
.swt
.events
.SelectionEvent
;
21 import org
.eclipse
.swt
.events
.SelectionListener
;
22 import org
.eclipse
.swt
.layout
.GridData
;
23 import org
.eclipse
.swt
.layout
.GridLayout
;
24 import org
.eclipse
.swt
.widgets
.Composite
;
25 import org
.eclipse
.swt
.widgets
.Control
;
26 import org
.eclipse
.swt
.widgets
.Label
;
27 import org
.eclipse
.ui
.IWorkbench
;
28 import org
.eclipse
.ui
.IWorkbenchPreferencePage
;
29 import org
.eclipse
.ui
.PlatformUI
;
31 import eu
.etaxonomy
.taxeditor
.Messages
;
32 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
36 * @created Dec 3, 2010
39 public class LanguageEditorPreferencePage
extends PreferencePage
implements IWorkbenchPreferencePage
{
42 PreferencesUtil preferencesUtil
= new PreferencesUtil();
43 private boolean isSelectionChanged
= false;
44 private int initalSelectionIndex
;
47 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
50 protected Control
createContents(Composite parent
) {
51 Composite container
= new Composite(parent
, SWT
.NULL
);
52 final GridLayout gridLayout
= new GridLayout();
53 container
.setLayout(gridLayout
);
54 createEditorDefaultLanguage(container
);
61 private void createEditorDefaultLanguage(Composite container
) {
62 // TODO Auto-generated method stub
63 final Label description
= new Label(container
, SWT
.NONE
);
64 description
.setText(Messages
.LanguageEditorPreferencePage_RestartRequired
);
66 final Label label
= new Label(container
, SWT
.NONE
);
67 label
.setText(Messages
.LanguageEditorPreferencePage_ChooseDefaultLanguage
);
69 GridData oneLine
= new GridData();
70 oneLine
.grabExcessHorizontalSpace
= true;
71 oneLine
.horizontalAlignment
= GridData
.FILL
;
73 combo
= new CCombo(container
, SWT
.NONE
);
74 combo
.setLayoutData(oneLine
);
76 combo
.add(Language
.GERMAN
.getLabel(), 0);
77 combo
.add(Language
.ENGLISH
.getLabel(), 1);
78 restoreSavedSelection();
80 combo
.addSelectionListener(new SelectionListener() {
83 public void widgetSelected(SelectionEvent e
) {
85 preferencesUtil
.writePropertyToConfigFile(combo
.getSelectionIndex());
86 if(initalSelectionIndex
!= combo
.getSelectionIndex()) {
87 isSelectionChanged
= true;
89 isSelectionChanged
= false;
91 } catch (IOException e1
) {
92 MessagingUtils
.messageDialog("Failed to write Config.ini", LanguageEditorPreferencePage
.class,
93 "Language switch failed, because could not write to Folder. No writing permissions!", null);
98 public void widgetDefaultSelected(SelectionEvent e
) {
99 // TODO Auto-generated method stub
106 * TODO: This method is not taking advantages of the enum field yet
108 private void restoreSavedSelection() {
109 String rememberedValue
= PreferencesUtil
.getPreferenceStore().getString(IPreferenceKeys
.DEFAULT_LANGUAGE_EDITOR
);
110 if(StringUtils
.isNotEmpty(rememberedValue
)&& StringUtils
.isNotBlank(rememberedValue
)){
111 if(rememberedValue
.equalsIgnoreCase("en")){ //$NON-NLS-1$
112 initalSelectionIndex
= 1;
114 }else if(rememberedValue
.equalsIgnoreCase("de")){ //$NON-NLS-1$
115 initalSelectionIndex
= 0;
119 Locale locale
= Locale
.getDefault();
120 if(locale
.getLanguage().equals(new Locale("de").getLanguage())){
121 initalSelectionIndex
= 0;
123 }else if(locale
.getLanguage().equals(new Locale("en").getLanguage())){
124 initalSelectionIndex
= 1;
130 private enum Language
{
132 GERMAN("Deutsch"), ENGLISH("English"); //$NON-NLS-1$ //$NON-NLS-2$
133 private final String label
;
134 private Language(String label
){
141 public String
getLabel() {
147 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
150 public void init(IWorkbench workbench
) {
151 setPreferenceStore(PreferencesUtil
.getPreferenceStore());
155 * @see org.eclipse.jface.preference.PreferencePage#performOk()
158 public boolean performOk() {
160 if(isSelectionChanged
){
161 preferencesUtil
.writePropertyToConfigFile(combo
.getSelectionIndex());
162 boolean result
= MessageDialog
.openConfirm(getShell(), Messages
.LanguageEditorPreferencePage_PleaseRestart
, Messages
.LanguageEditorPreferencePage_EditorHasToRestart
);
165 PlatformUI
.getWorkbench().restart();
168 } catch (IOException e
) {
171 return super.performOk();