Project

General

Profile

Download (4.29 KB) Statistics
| Branch: | Tag: | Revision:
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 description = new Label(container, SWT.NONE);
57
        description.setText("After changing the default language, a restart is required,\nin order for the new settings to take effect.");
58

    
59
        final Label label = new Label(container, SWT.NONE);
60
        label.setText("Please choose your default language: ");
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
                try {
78
                    preferencesUtil.writePropertyToConfigFile(combo.getSelectionIndex());
79
                } catch (IOException e1) {
80
                    e1.printStackTrace();
81
                }
82
            }
83

    
84
            @Override
85
            public void widgetDefaultSelected(SelectionEvent e) {
86
                // TODO Auto-generated method stub
87

    
88
            }
89
        });
90
    }
91

    
92
    /**
93
     * TODO: This method is not taking advantages of the enum field yet
94
     */
95
    private void restoreSavedSelection() {
96
        String rememberedValue = PreferencesUtil.getPreferenceStore().getString(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR);
97
        if(StringUtils.isNotEmpty(rememberedValue)&& StringUtils.isNotBlank(rememberedValue)){
98
            if(rememberedValue.equalsIgnoreCase("en")){
99
                combo.select(1);
100
            }else if(rememberedValue.equalsIgnoreCase("de")){
101
                 combo.select(0);
102
            }
103
        }
104
    }
105

    
106
    private enum Language{
107

    
108
        GERMAN("Deutsch"), ENGLISH("English");
109
        private final String label;
110
        private Language(String label){
111
            this.label = label;
112
        }
113

    
114
        /**
115
         * @return the label
116
         */
117
        public String getLabel() {
118
            return label;
119
        }
120
    }
121

    
122
 	/* (non-Javadoc)
123
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
124
	 */
125
	@Override
126
	public void init(IWorkbench workbench) {
127
		setPreferenceStore(PreferencesUtil.getPreferenceStore());
128
	}
129

    
130
	/* (non-Javadoc)
131
	 * @see org.eclipse.jface.preference.PreferencePage#performOk()
132
	 */
133
	@Override
134
	public boolean performOk() {
135
        try {
136
            preferencesUtil.writePropertyToConfigFile(combo.getSelectionIndex());
137
        } catch (IOException e) {
138
            e.printStackTrace();
139
        }
140
		return super.performOk();
141
	}
142
}
(4-4/21)