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;
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 isSelectionChanged
= true;
87 } catch (IOException e1
) {
88 MessagingUtils
.messageDialog("Failed to write Config.ini", LanguageEditorPreferencePage
.class,
89 "Language switch failed, because could not write to Folder. No writing permissions!", null);
94 public void widgetDefaultSelected(SelectionEvent e
) {
95 // TODO Auto-generated method stub
102 * TODO: This method is not taking advantages of the enum field yet
104 private void restoreSavedSelection() {
105 String rememberedValue
= PreferencesUtil
.getPreferenceStore().getString(IPreferenceKeys
.DEFAULT_LANGUAGE_EDITOR
);
106 if(StringUtils
.isNotEmpty(rememberedValue
)&& StringUtils
.isNotBlank(rememberedValue
)){
107 if(rememberedValue
.equalsIgnoreCase("en")){ //$NON-NLS-1$
109 }else if(rememberedValue
.equalsIgnoreCase("de")){ //$NON-NLS-1$
113 Locale locale
= Locale
.getDefault();
114 if(locale
.getLanguage().equals(new Locale("de").getLanguage())){
116 }else if(locale
.getLanguage().equals(new Locale("en").getLanguage())){
122 private enum Language
{
124 GERMAN("Deutsch"), ENGLISH("English"); //$NON-NLS-1$ //$NON-NLS-2$
125 private final String label
;
126 private Language(String label
){
133 public String
getLabel() {
139 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
142 public void init(IWorkbench workbench
) {
143 setPreferenceStore(PreferencesUtil
.getPreferenceStore());
147 * @see org.eclipse.jface.preference.PreferencePage#performOk()
150 public boolean performOk() {
152 if(isSelectionChanged
){
153 preferencesUtil
.writePropertyToConfigFile(combo
.getSelectionIndex());
154 boolean result
= MessageDialog
.openConfirm(getShell(), Messages
.LanguageEditorPreferencePage_PleaseRestart
, Messages
.LanguageEditorPreferencePage_EditorHasToRestart
);
157 PlatformUI
.getWorkbench().restart();
160 } catch (IOException e
) {
163 return super.performOk();