9b76bf99d82d3204966a7a473e725cf1fdbe1afc
[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.IOException;
14
15 import org.apache.commons.lang.StringUtils;
16 import org.eclipse.jface.preference.PreferencePage;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.custom.CCombo;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.events.SelectionListener;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.ui.IWorkbench;
27 import org.eclipse.ui.IWorkbenchPreferencePage;
28
29 /**
30 * @author n.hoffmann
31 * @created Dec 3, 2010
32 * @version 1.0
33 */
34 public class DefaultLanguageEditorPreferencePage extends PreferencePage implements IWorkbenchPreferencePage{
35
36 private CCombo combo;
37 PreferencesUtil preferencesUtil = new PreferencesUtil();
38
39 /* (non-Javadoc)
40 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
41 */
42 @Override
43 protected Control createContents(Composite parent) {
44 Composite container = new Composite(parent, SWT.NULL);
45 final GridLayout gridLayout = new GridLayout();
46 container.setLayout(gridLayout);
47 createEditorDefaultLanguage(container);
48 return container;
49 }
50
51 /**
52 * @param container
53 */
54 private void createEditorDefaultLanguage(Composite container) {
55 // TODO Auto-generated method stub
56 final Label label = new Label(container, SWT.NONE);
57 label.setText("Please choose your default language: ");
58
59 GridData oneLine = new GridData();
60 oneLine.grabExcessHorizontalSpace = true;
61 oneLine.horizontalAlignment = GridData.FILL;
62
63 combo = new CCombo(container, SWT.NONE);
64 combo.setLayoutData(oneLine);
65
66 combo.add(Language.GERMAN.getLabel(), 0);
67 combo.add(Language.ENGLISH.getLabel(), 1);
68 restoreSavedSelection();
69
70 combo.addSelectionListener(new SelectionListener() {
71
72 @Override
73 public void widgetSelected(SelectionEvent e) {
74 try {
75 preferencesUtil.writePropertyToConfigFile(combo.getSelectionIndex());
76 } catch (IOException e1) {
77 e1.printStackTrace();
78 }
79 }
80
81 @Override
82 public void widgetDefaultSelected(SelectionEvent e) {
83 // TODO Auto-generated method stub
84
85 }
86 });
87 }
88
89 /**
90 * TODO: This method is not taking advantages of the enum field yet
91 */
92 private void restoreSavedSelection() {
93 String rememberedValue = PreferencesUtil.getPreferenceStore().getString(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR);
94 if(StringUtils.isNotEmpty(rememberedValue)&& StringUtils.isNotBlank(rememberedValue)){
95 if(rememberedValue.equalsIgnoreCase("en")){
96 combo.select(1);
97 }else if(rememberedValue.equalsIgnoreCase("de")){
98 combo.select(0);
99 }
100 }
101 }
102
103 private enum Language{
104
105 GERMAN("Deutsch"), ENGLISH("English");
106 private final String label;
107 private Language(String label){
108 this.label = label;
109 }
110
111 /**
112 * @return the label
113 */
114 public String getLabel() {
115 return label;
116 }
117 }
118
119 /* (non-Javadoc)
120 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
121 */
122 @Override
123 public void init(IWorkbench workbench) {
124 setPreferenceStore(PreferencesUtil.getPreferenceStore());
125 }
126
127 /* (non-Javadoc)
128 * @see org.eclipse.jface.preference.PreferencePage#performOk()
129 */
130 @Override
131 public boolean performOk() {
132 try {
133 preferencesUtil.writePropertyToConfigFile(combo.getSelectionIndex());
134 } catch (IOException e) {
135 e.printStackTrace();
136 }
137 return super.performOk();
138 }
139 }