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
;
14 import java
.io
.FileInputStream
;
15 import java
.io
.FileOutputStream
;
16 import java
.io
.IOException
;
17 import java
.util
.Properties
;
19 import org
.apache
.commons
.lang
.StringUtils
;
20 import org
.eclipse
.jface
.preference
.PreferencePage
;
21 import org
.eclipse
.swt
.SWT
;
22 import org
.eclipse
.swt
.custom
.CCombo
;
23 import org
.eclipse
.swt
.events
.SelectionEvent
;
24 import org
.eclipse
.swt
.events
.SelectionListener
;
25 import org
.eclipse
.swt
.layout
.GridData
;
26 import org
.eclipse
.swt
.layout
.GridLayout
;
27 import org
.eclipse
.swt
.layout
.RowLayout
;
28 import org
.eclipse
.swt
.widgets
.Composite
;
29 import org
.eclipse
.swt
.widgets
.Control
;
30 import org
.eclipse
.swt
.widgets
.Label
;
31 import org
.eclipse
.ui
.IWorkbench
;
32 import org
.eclipse
.ui
.IWorkbenchPreferencePage
;
36 * @created Dec 3, 2010
39 public class DefaultLanguageEditorPreferencePage
extends PreferencePage
implements IWorkbenchPreferencePage
{
43 private Composite
createComposite(Composite parent
){
44 Composite composite
= new Composite(parent
, SWT
.NULL
);
45 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true, false, 2, 1));
46 composite
.setLayout(new RowLayout(SWT
.HORIZONTAL
));
51 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
54 protected Control
createContents(Composite parent
) {
55 Composite container
= new Composite(parent
, SWT
.NULL
);
56 final GridLayout gridLayout
= new GridLayout();
57 container
.setLayout(gridLayout
);
58 createEditorDefaultLanguage(container
);
65 private void createEditorDefaultLanguage(Composite container
) {
66 // TODO Auto-generated method stub
67 final Label label
= new Label(container
, SWT
.NONE
);
68 label
.setText("Please choose your default language: ");
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 writeConfigAndRestart(combo
.getSelectionIndex());
87 } catch (IOException e1
) {
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")){
108 }else if(rememberedValue
.equalsIgnoreCase("de")){
114 private enum Language
{
116 GERMAN("Deutsch"), ENGLISH("English");
117 private final String label
;
118 private Language(String label
){
125 public String
getLabel() {
130 private void writeConfigAndRestart(int setLanguage
) throws IOException
{
131 File file
= org
.eclipse
.core
.runtime
.preferences
.ConfigurationScope
.INSTANCE
.getLocation().toFile();
132 Properties properties
= load(file
.getAbsolutePath()+"/config.ini");
135 properties
.setProperty("osgi.nl", "de");
136 PreferencesUtil
.getPreferenceStore().setValue(IPreferenceKeys
.DEFAULT_LANGUAGE_EDITOR
, "de");
139 properties
.setProperty("osgi.nl", "en");
140 PreferencesUtil
.getPreferenceStore().setValue(IPreferenceKeys
.DEFAULT_LANGUAGE_EDITOR
, "en");
145 save(file
+"/config.ini", properties
);
148 private Properties
load(String filename
) throws IOException
{
149 FileInputStream in
= new FileInputStream(filename
);
150 Properties prop
= new Properties();
156 private void save(String filename
, Properties properties
) throws IOException
{
157 FileOutputStream fos
= new FileOutputStream(filename
);
158 properties
.store(fos
, "");
163 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
166 public void init(IWorkbench workbench
) {
167 setPreferenceStore(PreferencesUtil
.getPreferenceStore());
171 * @see org.eclipse.jface.preference.PreferencePage#performOk()
174 public boolean performOk() {
176 writeConfigAndRestart(combo
.getSelectionIndex());
177 } catch (IOException e
) {
180 return super.performOk();