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
|
* @version 1.0
|
34
|
*/
|
35
|
public class LanguageEditorPreferencePage extends CdmPreferencePage {
|
36
|
|
37
|
private CCombo combo;
|
38
|
PreferencesUtil preferencesUtil = new PreferencesUtil();
|
39
|
private boolean isSelectionChanged = false;
|
40
|
private int initalSelectionIndex;
|
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 description = new Label(container, SWT.NONE);
|
57
|
description.setText(Messages.LanguageEditorPreferencePage_RestartRequired);
|
58
|
|
59
|
final Label label = new Label(container, SWT.NONE);
|
60
|
label.setText(Messages.LanguageEditorPreferencePage_ChooseDefaultLanguage);
|
61
|
|
62
|
GridData oneLine = new GridData();
|
63
|
oneLine.grabExcessHorizontalSpace = true;
|
64
|
oneLine.horizontalAlignment = GridData.FILL;
|
65
|
|
66
|
combo = new CCombo(container, SWT.NONE);
|
67
|
combo.setLayoutData(oneLine);
|
68
|
|
69
|
combo.add(Language.GERMAN.getLabel(), 0);
|
70
|
combo.add(Language.ENGLISH.getLabel(), 1);
|
71
|
restoreSavedSelection();
|
72
|
|
73
|
combo.addSelectionListener(new SelectionListener() {
|
74
|
|
75
|
@Override
|
76
|
public void widgetSelected(SelectionEvent e) {
|
77
|
setApply(true);
|
78
|
// try {
|
79
|
// preferencesUtil.writePropertyToConfigFile(combo.getSelectionIndex());
|
80
|
if(initalSelectionIndex != combo.getSelectionIndex()) {
|
81
|
isSelectionChanged = true;
|
82
|
}else{
|
83
|
isSelectionChanged = false;
|
84
|
}
|
85
|
// } catch (IOException e1) {
|
86
|
// MessagingUtils.messageDialog("Failed to write Config.ini", LanguageEditorPreferencePage.class,
|
87
|
// "Language switch failed, because could not write to Folder. No writing permissions!", null);
|
88
|
// }
|
89
|
}
|
90
|
|
91
|
@Override
|
92
|
public void widgetDefaultSelected(SelectionEvent e) {
|
93
|
// TODO Auto-generated method stub
|
94
|
|
95
|
}
|
96
|
});
|
97
|
}
|
98
|
|
99
|
/**
|
100
|
* TODO: This method is not taking advantages of the enum field yet
|
101
|
*/
|
102
|
private void restoreSavedSelection() {
|
103
|
String rememberedValue = PreferencesUtil.getStringValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR);
|
104
|
if(StringUtils.isNotEmpty(rememberedValue)&& StringUtils.isNotBlank(rememberedValue)){
|
105
|
if(rememberedValue.equalsIgnoreCase("en")){ //$NON-NLS-1$
|
106
|
initalSelectionIndex = 1;
|
107
|
combo.select(1);
|
108
|
}else if(rememberedValue.equalsIgnoreCase("de")){ //$NON-NLS-1$
|
109
|
initalSelectionIndex = 0;
|
110
|
combo.select(0);
|
111
|
}
|
112
|
}else{
|
113
|
Locale locale = Locale.getDefault();
|
114
|
if(locale.getLanguage().equals(new Locale("de").getLanguage())){
|
115
|
initalSelectionIndex = 0;
|
116
|
combo.select(0);
|
117
|
}else if(locale.getLanguage().equals(new Locale("en").getLanguage())){
|
118
|
initalSelectionIndex = 1;
|
119
|
combo.select(1);
|
120
|
}
|
121
|
}
|
122
|
}
|
123
|
|
124
|
private enum Language{
|
125
|
|
126
|
GERMAN("Deutsch"), ENGLISH("English"); //$NON-NLS-1$ //$NON-NLS-2$
|
127
|
private final String label;
|
128
|
private Language(String label){
|
129
|
this.label = label;
|
130
|
}
|
131
|
|
132
|
/**
|
133
|
* @return the label
|
134
|
*/
|
135
|
public String getLabel() {
|
136
|
return label;
|
137
|
}
|
138
|
}
|
139
|
|
140
|
@Override
|
141
|
public boolean performOk() {
|
142
|
try {
|
143
|
if(isSelectionChanged){
|
144
|
preferencesUtil.writePropertyToConfigFile(combo.getSelectionIndex());
|
145
|
boolean result = MessageDialog.openConfirm(getShell(), Messages.LanguageEditorPreferencePage_PleaseRestart, Messages.LanguageEditorPreferencePage_EditorHasToRestart);
|
146
|
if(result){
|
147
|
//Press Ok
|
148
|
PlatformUI.getWorkbench().restart();
|
149
|
}
|
150
|
}
|
151
|
} catch (IOException e) {
|
152
|
e.printStackTrace();
|
153
|
}
|
154
|
return super.performOk();
|
155
|
}
|
156
|
|
157
|
}
|