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
|
import java.util.Locale;
|
15
|
|
16
|
import org.apache.commons.lang.StringUtils;
|
17
|
import org.eclipse.jface.dialogs.MessageDialog;
|
18
|
import org.eclipse.jface.preference.PreferencePage;
|
19
|
import org.eclipse.swt.SWT;
|
20
|
import org.eclipse.swt.custom.CCombo;
|
21
|
import org.eclipse.swt.events.SelectionEvent;
|
22
|
import org.eclipse.swt.events.SelectionListener;
|
23
|
import org.eclipse.swt.layout.GridData;
|
24
|
import org.eclipse.swt.layout.GridLayout;
|
25
|
import org.eclipse.swt.widgets.Composite;
|
26
|
import org.eclipse.swt.widgets.Control;
|
27
|
import org.eclipse.swt.widgets.Label;
|
28
|
import org.eclipse.ui.IWorkbench;
|
29
|
import org.eclipse.ui.IWorkbenchPreferencePage;
|
30
|
import org.eclipse.ui.PlatformUI;
|
31
|
|
32
|
import eu.etaxonomy.taxeditor.Messages;
|
33
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
34
|
|
35
|
/**
|
36
|
* @author n.hoffmann
|
37
|
* @created Dec 3, 2010
|
38
|
* @version 1.0
|
39
|
*/
|
40
|
public class LanguageEditorPreferencePage extends PreferencePage implements IWorkbenchPreferencePage{
|
41
|
|
42
|
private CCombo combo;
|
43
|
PreferencesUtil preferencesUtil = new PreferencesUtil();
|
44
|
private boolean isSelectionChanged = false;
|
45
|
private int initalSelectionIndex;
|
46
|
|
47
|
/* (non-Javadoc)
|
48
|
* @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
|
49
|
*/
|
50
|
@Override
|
51
|
protected Control createContents(Composite parent) {
|
52
|
Composite container = new Composite(parent, SWT.NULL);
|
53
|
final GridLayout gridLayout = new GridLayout();
|
54
|
container.setLayout(gridLayout);
|
55
|
createEditorDefaultLanguage(container);
|
56
|
return container;
|
57
|
}
|
58
|
|
59
|
/**
|
60
|
* @param container
|
61
|
*/
|
62
|
private void createEditorDefaultLanguage(Composite container) {
|
63
|
// TODO Auto-generated method stub
|
64
|
final Label description = new Label(container, SWT.NONE);
|
65
|
description.setText(Messages.LanguageEditorPreferencePage_RestartRequired);
|
66
|
|
67
|
final Label label = new Label(container, SWT.NONE);
|
68
|
label.setText(Messages.LanguageEditorPreferencePage_ChooseDefaultLanguage);
|
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
|
preferencesUtil.writePropertyToConfigFile(combo.getSelectionIndex());
|
87
|
if(initalSelectionIndex != combo.getSelectionIndex()) {
|
88
|
isSelectionChanged = true;
|
89
|
}else{
|
90
|
isSelectionChanged = false;
|
91
|
}
|
92
|
} catch (IOException e1) {
|
93
|
MessagingUtils.messageDialog("Failed to write Config.ini", LanguageEditorPreferencePage.class,
|
94
|
"Language switch failed, because could not write to Folder. No writing permissions!", null);
|
95
|
}
|
96
|
}
|
97
|
|
98
|
@Override
|
99
|
public void widgetDefaultSelected(SelectionEvent e) {
|
100
|
// TODO Auto-generated method stub
|
101
|
|
102
|
}
|
103
|
});
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* TODO: This method is not taking advantages of the enum field yet
|
108
|
*/
|
109
|
private void restoreSavedSelection() {
|
110
|
String rememberedValue = PreferencesUtil.getPreferenceStore().getString(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR);
|
111
|
if(StringUtils.isNotEmpty(rememberedValue)&& StringUtils.isNotBlank(rememberedValue)){
|
112
|
if(rememberedValue.equalsIgnoreCase("en")){ //$NON-NLS-1$
|
113
|
initalSelectionIndex = 1;
|
114
|
combo.select(1);
|
115
|
}else if(rememberedValue.equalsIgnoreCase("de")){ //$NON-NLS-1$
|
116
|
initalSelectionIndex = 0;
|
117
|
combo.select(0);
|
118
|
}
|
119
|
}else{
|
120
|
Locale locale = Locale.getDefault();
|
121
|
if(locale.getLanguage().equals(new Locale("de").getLanguage())){
|
122
|
initalSelectionIndex = 0;
|
123
|
combo.select(0);
|
124
|
}else if(locale.getLanguage().equals(new Locale("en").getLanguage())){
|
125
|
initalSelectionIndex = 1;
|
126
|
combo.select(1);
|
127
|
}
|
128
|
}
|
129
|
}
|
130
|
|
131
|
private enum Language{
|
132
|
|
133
|
GERMAN("Deutsch"), ENGLISH("English"); //$NON-NLS-1$ //$NON-NLS-2$
|
134
|
private final String label;
|
135
|
private Language(String label){
|
136
|
this.label = label;
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* @return the label
|
141
|
*/
|
142
|
public String getLabel() {
|
143
|
return label;
|
144
|
}
|
145
|
}
|
146
|
|
147
|
/* (non-Javadoc)
|
148
|
* @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
|
149
|
*/
|
150
|
@Override
|
151
|
public void init(IWorkbench workbench) {
|
152
|
setPreferenceStore(PreferencesUtil.getPreferenceStore());
|
153
|
}
|
154
|
|
155
|
/* (non-Javadoc)
|
156
|
* @see org.eclipse.jface.preference.PreferencePage#performOk()
|
157
|
*/
|
158
|
@Override
|
159
|
public boolean performOk() {
|
160
|
try {
|
161
|
if(isSelectionChanged){
|
162
|
preferencesUtil.writePropertyToConfigFile(combo.getSelectionIndex());
|
163
|
boolean result = MessageDialog.openConfirm(getShell(), Messages.LanguageEditorPreferencePage_PleaseRestart, Messages.LanguageEditorPreferencePage_EditorHasToRestart);
|
164
|
if(result){
|
165
|
//Press Ok
|
166
|
PlatformUI.getWorkbench().restart();
|
167
|
}
|
168
|
}
|
169
|
} catch (IOException e) {
|
170
|
e.printStackTrace();
|
171
|
}
|
172
|
return super.performOk();
|
173
|
}
|
174
|
|
175
|
}
|