ref #9340, #9734, #9668: further improvements for configurable sec handling during...
[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 package eu.etaxonomy.taxeditor.preference;
10
11 import java.io.IOException;
12 import java.util.Locale;
13
14 import org.apache.commons.lang3.StringUtils;
15 import org.eclipse.jface.dialogs.MessageDialog;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.custom.CCombo;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.events.SelectionListener;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Label;
25 import org.eclipse.ui.PlatformUI;
26
27 import eu.etaxonomy.taxeditor.l10n.Messages;
28 import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
29
30 /**
31 * @author n.hoffmann
32 * @created Dec 3, 2010
33 */
34 public class LanguageEditorPreferencePage extends CdmPreferencePage {
35
36 private CCombo combo;
37 PreferencesUtil preferencesUtil = new PreferencesUtil();
38 private boolean isSelectionChanged = false;
39 private int initalSelectionIndex;
40
41 @Override
42 protected Control createContents(Composite parent) {
43 Composite container = new Composite(parent, SWT.NULL);
44 final GridLayout gridLayout = new GridLayout();
45 container.setLayout(gridLayout);
46 createEditorDefaultLanguage(container);
47 return container;
48 }
49
50 /**
51 * @param container
52 */
53 private void createEditorDefaultLanguage(Composite container) {
54 // TODO Auto-generated method stub
55 final Label description = new Label(container, SWT.NONE);
56 description.setText(Messages.LanguageEditorPreferencePage_RestartRequired);
57
58 final Label label = new Label(container, SWT.NONE);
59 label.setText(Messages.LanguageEditorPreferencePage_ChooseDefaultLanguage);
60
61 GridData oneLine = new GridData();
62 oneLine.grabExcessHorizontalSpace = true;
63 oneLine.horizontalAlignment = GridData.FILL;
64
65 combo = new CCombo(container, SWT.NONE);
66 combo.setLayoutData(oneLine);
67
68 combo.add(Language.GERMAN.getLabel(), 0);
69 combo.add(Language.ENGLISH.getLabel(), 1);
70 restoreSavedSelection();
71
72 combo.addSelectionListener(new SelectionListener() {
73
74 @Override
75 public void widgetSelected(SelectionEvent e) {
76 setApply(true);
77 // try {
78 // preferencesUtil.writePropertyToConfigFile(combo.getSelectionIndex());
79 if(initalSelectionIndex != combo.getSelectionIndex()) {
80 isSelectionChanged = true;
81 }else{
82 isSelectionChanged = false;
83 }
84 // } catch (IOException e1) {
85 // MessagingUtils.messageDialog("Failed to write Config.ini", LanguageEditorPreferencePage.class,
86 // "Language switch failed, because could not write to Folder. No writing permissions!", null);
87 // }
88 }
89
90 @Override
91 public void widgetDefaultSelected(SelectionEvent e) {
92 // TODO Auto-generated method stub
93
94 }
95 });
96 }
97
98 /**
99 * TODO: This method is not taking advantages of the enum field yet
100 */
101 private void restoreSavedSelection() {
102 String rememberedValue = PreferencesUtil.getStringValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR);
103 if(StringUtils.isNotEmpty(rememberedValue)&& StringUtils.isNotBlank(rememberedValue)){
104 if(rememberedValue.equalsIgnoreCase("en")){ //$NON-NLS-1$
105 initalSelectionIndex = 1;
106 combo.select(1);
107 }else if(rememberedValue.equalsIgnoreCase("de")){ //$NON-NLS-1$
108 initalSelectionIndex = 0;
109 combo.select(0);
110 }
111 }else{
112 Locale locale = Locale.getDefault();
113 if(locale.getLanguage().equals(new Locale("de").getLanguage())){
114 initalSelectionIndex = 0;
115 combo.select(0);
116 }else if(locale.getLanguage().equals(new Locale("en").getLanguage())){
117 initalSelectionIndex = 1;
118 combo.select(1);
119 }
120 }
121 }
122
123 private enum Language{
124
125 GERMAN("Deutsch"), ENGLISH("English"); //$NON-NLS-1$ //$NON-NLS-2$
126 private final String label;
127 private Language(String label){
128 this.label = label;
129 }
130
131 /**
132 * @return the label
133 */
134 public String getLabel() {
135 return label;
136 }
137 }
138
139 @Override
140 public boolean performOk() {
141 try {
142 if(isSelectionChanged){
143 preferencesUtil.writePropertyToConfigFile(combo.getSelectionIndex());
144 boolean result = MessageDialog.openConfirm(getShell(), Messages.LanguageEditorPreferencePage_PleaseRestart, Messages.LanguageEditorPreferencePage_EditorHasToRestart);
145 if(result){
146 //Press Ok
147 PlatformUI.getWorkbench().restart();
148 }
149 }
150 } catch (IOException e) {
151 e.printStackTrace();
152 }
153 return super.performOk();
154 }
155 }