Project

General

Profile

Download (5.27 KB) Statistics
| Branch: | Tag: | Revision:
1 4afd9d97 Alexander Oppermann
/**
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 7a8e7c22 Alexander Oppermann
import java.util.Locale;
14 4afd9d97 Alexander Oppermann
15
import org.apache.commons.lang.StringUtils;
16 a1ce478d Alexander Oppermann
import org.eclipse.jface.dialogs.MessageDialog;
17 4afd9d97 Alexander Oppermann
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 a1ce478d Alexander Oppermann
import org.eclipse.ui.PlatformUI;
27 4afd9d97 Alexander Oppermann
28 a86dbf9a Patrick Plitzner
import eu.etaxonomy.taxeditor.l10n.Messages;
29 672874ab Patrick Plitzner
import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
30 57318058 Patric Plitzner
31 4afd9d97 Alexander Oppermann
/**
32
 * @author n.hoffmann
33
 * @created Dec 3, 2010
34
 * @version 1.0
35
 */
36 672874ab Patrick Plitzner
public class LanguageEditorPreferencePage extends CdmPreferencePage {
37 4afd9d97 Alexander Oppermann
38
    private CCombo combo;
39 86cc5c9a Alexander Oppermann
    PreferencesUtil preferencesUtil = new PreferencesUtil();
40 7a8e7c22 Alexander Oppermann
    private boolean isSelectionChanged = false;
41 054d53e4 Alexander Oppermann
    private int initalSelectionIndex;
42 4afd9d97 Alexander Oppermann
43
	@Override
44
	protected Control createContents(Composite parent) {
45
		Composite container = new Composite(parent, SWT.NULL);
46
		final GridLayout gridLayout = new GridLayout();
47
		container.setLayout(gridLayout);
48
		createEditorDefaultLanguage(container);
49
		return container;
50
	}
51
52
	/**
53
     * @param container
54
     */
55
    private void createEditorDefaultLanguage(Composite container) {
56
        // TODO Auto-generated method stub
57 a61d8cd3 Alexander Oppermann
        final Label description = new Label(container, SWT.NONE);
58 57318058 Patric Plitzner
        description.setText(Messages.LanguageEditorPreferencePage_RestartRequired);
59 a61d8cd3 Alexander Oppermann
60 4afd9d97 Alexander Oppermann
        final Label label = new Label(container, SWT.NONE);
61 57318058 Patric Plitzner
        label.setText(Messages.LanguageEditorPreferencePage_ChooseDefaultLanguage);
62 4afd9d97 Alexander Oppermann
63
        GridData oneLine = new GridData();
64
        oneLine.grabExcessHorizontalSpace = true;
65
        oneLine.horizontalAlignment = GridData.FILL;
66
67
        combo = new CCombo(container, SWT.NONE);
68
        combo.setLayoutData(oneLine);
69
70
        combo.add(Language.GERMAN.getLabel(), 0);
71
        combo.add(Language.ENGLISH.getLabel(), 1);
72
        restoreSavedSelection();
73
74
        combo.addSelectionListener(new SelectionListener() {
75
76
            @Override
77
            public void widgetSelected(SelectionEvent e) {
78 fbd932cc Katja Luther
                setApply(true);
79 ded7a114 Katja Luther
//                try {
80
//                    preferencesUtil.writePropertyToConfigFile(combo.getSelectionIndex());
81 054d53e4 Alexander Oppermann
                    if(initalSelectionIndex != combo.getSelectionIndex()) {
82
                        isSelectionChanged = true;
83
                    }else{
84
                        isSelectionChanged = false;
85
                    }
86 ded7a114 Katja Luther
//                } 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 4afd9d97 Alexander Oppermann
            }
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 69e82edd Katja Luther
        String rememberedValue = PreferencesUtil.getStringValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR);
105 4afd9d97 Alexander Oppermann
        if(StringUtils.isNotEmpty(rememberedValue)&& StringUtils.isNotBlank(rememberedValue)){
106 57318058 Patric Plitzner
            if(rememberedValue.equalsIgnoreCase("en")){ //$NON-NLS-1$
107 054d53e4 Alexander Oppermann
                initalSelectionIndex = 1;
108 4afd9d97 Alexander Oppermann
                combo.select(1);
109 57318058 Patric Plitzner
            }else if(rememberedValue.equalsIgnoreCase("de")){ //$NON-NLS-1$
110 054d53e4 Alexander Oppermann
                initalSelectionIndex = 0;
111
                combo.select(0);
112 4afd9d97 Alexander Oppermann
            }
113 7a8e7c22 Alexander Oppermann
        }else{
114
            Locale locale = Locale.getDefault();
115
            if(locale.getLanguage().equals(new Locale("de").getLanguage())){
116 054d53e4 Alexander Oppermann
                initalSelectionIndex = 0;
117 7a8e7c22 Alexander Oppermann
                combo.select(0);
118
            }else if(locale.getLanguage().equals(new Locale("en").getLanguage())){
119 054d53e4 Alexander Oppermann
                initalSelectionIndex = 1;
120 7a8e7c22 Alexander Oppermann
                combo.select(1);
121
            }
122 4afd9d97 Alexander Oppermann
        }
123
    }
124
125
    private enum Language{
126
127 57318058 Patric Plitzner
        GERMAN("Deutsch"), ENGLISH("English"); //$NON-NLS-1$ //$NON-NLS-2$
128 4afd9d97 Alexander Oppermann
        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 7a8e7c22 Alexander Oppermann
            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 a1ce478d Alexander Oppermann
            }
152 4afd9d97 Alexander Oppermann
        } catch (IOException e) {
153
            e.printStackTrace();
154
        }
155
		return super.performOk();
156
	}
157 a1ce478d Alexander Oppermann
158 4afd9d97 Alexander Oppermann
}