ref #6429: adapt admin preferences to other preferences
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / LanguageEditorPreferencePage.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.preference;
11
12 import java.io.IOException;
13 import java.util.Locale;
14
15 import org.apache.commons.lang.StringUtils;
16 import org.eclipse.jface.dialogs.MessageDialog;
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.PlatformUI;
27
28 import eu.etaxonomy.taxeditor.l10n.Messages;
29 import eu.etaxonomy.taxeditor.model.MessagingUtils;
30 import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
31
32 /**
33 * @author n.hoffmann
34 * @created Dec 3, 2010
35 * @version 1.0
36 */
37 public class LanguageEditorPreferencePage extends CdmPreferencePage {
38
39 private CCombo combo;
40 PreferencesUtil preferencesUtil = new PreferencesUtil();
41 private boolean isSelectionChanged = false;
42 private int initalSelectionIndex;
43
44 @Override
45 protected Control createContents(Composite parent) {
46 Composite container = new Composite(parent, SWT.NULL);
47 final GridLayout gridLayout = new GridLayout();
48 container.setLayout(gridLayout);
49 createEditorDefaultLanguage(container);
50 return container;
51 }
52
53 /**
54 * @param container
55 */
56 private void createEditorDefaultLanguage(Composite container) {
57 // TODO Auto-generated method stub
58 final Label description = new Label(container, SWT.NONE);
59 description.setText(Messages.LanguageEditorPreferencePage_RestartRequired);
60
61 final Label label = new Label(container, SWT.NONE);
62 label.setText(Messages.LanguageEditorPreferencePage_ChooseDefaultLanguage);
63
64 GridData oneLine = new GridData();
65 oneLine.grabExcessHorizontalSpace = true;
66 oneLine.horizontalAlignment = GridData.FILL;
67
68 combo = new CCombo(container, SWT.NONE);
69 combo.setLayoutData(oneLine);
70
71 combo.add(Language.GERMAN.getLabel(), 0);
72 combo.add(Language.ENGLISH.getLabel(), 1);
73 restoreSavedSelection();
74
75 combo.addSelectionListener(new SelectionListener() {
76
77 @Override
78 public void widgetSelected(SelectionEvent e) {
79 try {
80 preferencesUtil.writePropertyToConfigFile(combo.getSelectionIndex());
81 if(initalSelectionIndex != combo.getSelectionIndex()) {
82 isSelectionChanged = true;
83 }else{
84 isSelectionChanged = false;
85 }
86 } catch (IOException e1) {
87 MessagingUtils.messageDialog("Failed to write Config.ini", LanguageEditorPreferencePage.class,
88 "Language switch failed, because could not write to Folder. No writing permissions!", null);
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")){ //$NON-NLS-1$
107 initalSelectionIndex = 1;
108 combo.select(1);
109 }else if(rememberedValue.equalsIgnoreCase("de")){ //$NON-NLS-1$
110 initalSelectionIndex = 0;
111 combo.select(0);
112 }
113 }else{
114 Locale locale = Locale.getDefault();
115 if(locale.getLanguage().equals(new Locale("de").getLanguage())){
116 initalSelectionIndex = 0;
117 combo.select(0);
118 }else if(locale.getLanguage().equals(new Locale("en").getLanguage())){
119 initalSelectionIndex = 1;
120 combo.select(1);
121 }
122 }
123 }
124
125 private enum Language{
126
127 GERMAN("Deutsch"), ENGLISH("English"); //$NON-NLS-1$ //$NON-NLS-2$
128 private final String label;
129 private Language(String label){
130 this.label = label;
131 }
132
133 /**
134 * @return the label
135 */
136 public String getLabel() {
137 return label;
138 }
139 }
140
141 @Override
142 public boolean performOk() {
143 try {
144 if(isSelectionChanged){
145 preferencesUtil.writePropertyToConfigFile(combo.getSelectionIndex());
146 boolean result = MessageDialog.openConfirm(getShell(), Messages.LanguageEditorPreferencePage_PleaseRestart, Messages.LanguageEditorPreferencePage_EditorHasToRestart);
147 if(result){
148 //Press Ok
149 PlatformUI.getWorkbench().restart();
150 }
151 }
152 } catch (IOException e) {
153 e.printStackTrace();
154 }
155 return super.performOk();
156 }
157
158 }