Added Default Language Selection for the TaxonomicEditor to fix #4358
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / DefaultLanguageEditorPreferencePage.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.preference;
12
13 import java.io.File;
14 import java.io.FileInputStream;
15 import java.io.FileOutputStream;
16 import java.io.IOException;
17 import java.util.Properties;
18
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;
33
34 /**
35 * @author n.hoffmann
36 * @created Dec 3, 2010
37 * @version 1.0
38 */
39 public class DefaultLanguageEditorPreferencePage extends PreferencePage implements IWorkbenchPreferencePage{
40
41 private CCombo combo;
42
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));
47 return composite;
48 }
49
50 /* (non-Javadoc)
51 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
52 */
53 @Override
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);
59 return container;
60 }
61
62 /**
63 * @param container
64 */
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: ");
69
70 GridData oneLine = new GridData();
71 oneLine.grabExcessHorizontalSpace = true;
72 oneLine.horizontalAlignment = GridData.FILL;
73
74 combo = new CCombo(container, SWT.NONE);
75 combo.setLayoutData(oneLine);
76
77 combo.add(Language.GERMAN.getLabel(), 0);
78 combo.add(Language.ENGLISH.getLabel(), 1);
79 restoreSavedSelection();
80
81 combo.addSelectionListener(new SelectionListener() {
82
83 @Override
84 public void widgetSelected(SelectionEvent e) {
85 try {
86 writeConfigAndRestart(combo.getSelectionIndex());
87 } catch (IOException e1) {
88 e1.printStackTrace();
89 }
90 }
91
92 @Override
93 public void widgetDefaultSelected(SelectionEvent e) {
94 // TODO Auto-generated method stub
95
96 }
97 });
98 }
99
100 /**
101 * TODO: This method is not taking advantages of the enum field yet
102 */
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")){
107 combo.select(1);
108 }else if(rememberedValue.equalsIgnoreCase("de")){
109 combo.select(0);
110 }
111 }
112 }
113
114 private enum Language{
115
116 GERMAN("Deutsch"), ENGLISH("English");
117 private final String label;
118 private Language(String label){
119 this.label = label;
120 }
121
122 /**
123 * @return the label
124 */
125 public String getLabel() {
126 return label;
127 }
128 }
129
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");
133 switch(setLanguage){
134 case 0:
135 properties.setProperty("osgi.nl", "de");
136 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "de");
137 break;
138 case 1:
139 properties.setProperty("osgi.nl", "en");
140 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "en");
141 break;
142 default:
143 break;
144 }
145 save(file+"/config.ini", properties);
146 }
147
148 private Properties load(String filename) throws IOException {
149 FileInputStream in = new FileInputStream(filename);
150 Properties prop = new Properties();
151 prop.load(in);
152 in.close();
153 return prop;
154 }
155
156 private void save(String filename, Properties properties) throws IOException{
157 FileOutputStream fos = new FileOutputStream(filename);
158 properties.store(fos, "");
159 fos.close();
160 }
161
162 /* (non-Javadoc)
163 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
164 */
165 @Override
166 public void init(IWorkbench workbench) {
167 setPreferenceStore(PreferencesUtil.getPreferenceStore());
168 }
169
170 /* (non-Javadoc)
171 * @see org.eclipse.jface.preference.PreferencePage#performOk()
172 */
173 @Override
174 public boolean performOk() {
175 try {
176 writeConfigAndRestart(combo.getSelectionIndex());
177 } catch (IOException e) {
178 e.printStackTrace();
179 }
180 return super.performOk();
181 }
182 }