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
.preference
;
13 import java
.io
.IOException
;
14 import java
.util
.Locale
;
16 import org
.apache
.commons
.lang
.StringUtils
;
17 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
18 import org
.eclipse
.jface
.preference
.PreferencePage
;
19 import org
.eclipse
.swt
.SWT
;
20 import org
.eclipse
.swt
.custom
.CCombo
;
21 import org
.eclipse
.swt
.events
.SelectionEvent
;
22 import org
.eclipse
.swt
.events
.SelectionListener
;
23 import org
.eclipse
.swt
.layout
.GridData
;
24 import org
.eclipse
.swt
.layout
.GridLayout
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
26 import org
.eclipse
.swt
.widgets
.Control
;
27 import org
.eclipse
.swt
.widgets
.Label
;
28 import org
.eclipse
.ui
.IWorkbench
;
29 import org
.eclipse
.ui
.IWorkbenchPreferencePage
;
30 import org
.eclipse
.ui
.PlatformUI
;
32 import eu
.etaxonomy
.taxeditor
.Messages
;
33 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
37 * @created Dec 3, 2010
40 public class LanguageEditorPreferencePage
extends PreferencePage
implements IWorkbenchPreferencePage
{
43 PreferencesUtil preferencesUtil
= new PreferencesUtil();
44 private boolean isSelectionChanged
= false;
45 private int initalSelectionIndex
;
48 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
51 protected Control
createContents(Composite parent
) {
52 Composite container
= new Composite(parent
, SWT
.NULL
);
53 final GridLayout gridLayout
= new GridLayout();
54 container
.setLayout(gridLayout
);
55 createEditorDefaultLanguage(container
);
62 private void createEditorDefaultLanguage(Composite container
) {
63 // TODO Auto-generated method stub
64 final Label description
= new Label(container
, SWT
.NONE
);
65 description
.setText(Messages
.LanguageEditorPreferencePage_RestartRequired
);
67 final Label label
= new Label(container
, SWT
.NONE
);
68 label
.setText(Messages
.LanguageEditorPreferencePage_ChooseDefaultLanguage
);
70 GridData oneLine
= new GridData();
71 oneLine
.grabExcessHorizontalSpace
= true;
72 oneLine
.horizontalAlignment
= GridData
.FILL
;
74 combo
= new CCombo(container
, SWT
.NONE
);
75 combo
.setLayoutData(oneLine
);
77 combo
.add(Language
.GERMAN
.getLabel(), 0);
78 combo
.add(Language
.ENGLISH
.getLabel(), 1);
79 restoreSavedSelection();
81 combo
.addSelectionListener(new SelectionListener() {
84 public void widgetSelected(SelectionEvent e
) {
86 preferencesUtil
.writePropertyToConfigFile(combo
.getSelectionIndex());
87 if(initalSelectionIndex
!= combo
.getSelectionIndex()) {
88 isSelectionChanged
= true;
90 isSelectionChanged
= false;
92 } catch (IOException e1
) {
93 MessagingUtils
.messageDialog("Failed to write Config.ini", LanguageEditorPreferencePage
.class,
94 "Language switch failed, because could not write to Folder. No writing permissions!", null);
99 public void widgetDefaultSelected(SelectionEvent e
) {
100 // TODO Auto-generated method stub
107 * TODO: This method is not taking advantages of the enum field yet
109 private void restoreSavedSelection() {
110 String rememberedValue
= PreferencesUtil
.getPreferenceStore().getString(IPreferenceKeys
.DEFAULT_LANGUAGE_EDITOR
);
111 if(StringUtils
.isNotEmpty(rememberedValue
)&& StringUtils
.isNotBlank(rememberedValue
)){
112 if(rememberedValue
.equalsIgnoreCase("en")){ //$NON-NLS-1$
113 initalSelectionIndex
= 1;
115 }else if(rememberedValue
.equalsIgnoreCase("de")){ //$NON-NLS-1$
116 initalSelectionIndex
= 0;
120 Locale locale
= Locale
.getDefault();
121 if(locale
.getLanguage().equals(new Locale("de").getLanguage())){
122 initalSelectionIndex
= 0;
124 }else if(locale
.getLanguage().equals(new Locale("en").getLanguage())){
125 initalSelectionIndex
= 1;
131 private enum Language
{
133 GERMAN("Deutsch"), ENGLISH("English"); //$NON-NLS-1$ //$NON-NLS-2$
134 private final String label
;
135 private Language(String label
){
142 public String
getLabel() {
148 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
151 public void init(IWorkbench workbench
) {
152 setPreferenceStore(PreferencesUtil
.getPreferenceStore());
156 * @see org.eclipse.jface.preference.PreferencePage#performOk()
159 public boolean performOk() {
161 if(isSelectionChanged
){
162 preferencesUtil
.writePropertyToConfigFile(combo
.getSelectionIndex());
163 boolean result
= MessageDialog
.openConfirm(getShell(), Messages
.LanguageEditorPreferencePage_PleaseRestart
, Messages
.LanguageEditorPreferencePage_EditorHasToRestart
);
166 PlatformUI
.getWorkbench().restart();
169 } catch (IOException e
) {
172 return super.performOk();