1f836ce04dacf84534796387b4cc0eba7e4bf6a0
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / DefaultLanguageDialog.java
1 // $Id$
2 /**
3 * Copyright (C) 2014 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 package eu.etaxonomy.taxeditor.ui.dialog;
11
12 import java.io.File;
13 import java.io.FileInputStream;
14 import java.io.FileOutputStream;
15 import java.io.IOException;
16 import java.util.Properties;
17
18 import org.eclipse.jface.dialogs.IMessageProvider;
19 import org.eclipse.jface.dialogs.TitleAreaDialog;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.custom.CCombo;
22 import org.eclipse.swt.custom.CLabel;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.events.SelectionListener;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Shell;
30
31 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
32 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
33
34
35 /**
36 * @author alex
37 * @date 19.08.2014
38 *
39 */
40 public class DefaultLanguageDialog extends TitleAreaDialog{
41
42
43 private CCombo combo;
44 /**
45 * @param parentShell
46 */
47 public DefaultLanguageDialog(Shell parentShell) {
48 super(parentShell);
49 }
50
51 @Override
52 protected void configureShell(Shell shell) {
53 super.configureShell(shell);
54 }
55
56 /* (non-Javadoc)
57 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
58 */
59 @Override
60 protected Control createDialogArea(Composite parent) {
61 // TODO Auto-generated method stub
62 setTitle("Select your default Language");
63 setMessage("This is will set your default langauge once.\n You will be able to change this in the prefrence menue at any time.", IMessageProvider.INFORMATION);
64 Composite composite = (Composite)super.createDialogArea(parent);
65 Composite container = new Composite(parent, SWT.NONE);
66 container.setLayoutData(new GridData(SWT.TOP));
67
68 GridLayout layout = new GridLayout(1, false);
69 container.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, true));
70 container.setLayout(layout);
71
72 final CLabel titleLabel = new CLabel(container, SWT.NONE);
73 titleLabel.setText("After this the editor will restart, " +
74 "with your selection loaded. ");
75
76
77 createComboElement(container);
78
79 return composite;
80 }
81
82
83 /**
84 *
85 */
86 private void createComboElement(Composite parent) {
87 Composite container1 = new Composite(parent, SWT.NONE);
88 container1.setLayoutData(new GridData(GridData.FILL_BOTH));
89
90 GridLayout layout1 = new GridLayout(2, false);
91 container1.setLayoutData(new GridData(SWT.RIGHT, SWT.RIGHT, true, true));
92 container1.setLayout(layout1);
93
94
95 final CLabel comboLabel = new CLabel(container1, SWT.NONE);
96 comboLabel.setText("Please choose your default language: ");
97
98 GridData oneLine = new GridData();
99 oneLine.grabExcessHorizontalSpace = true;
100 oneLine.horizontalAlignment = GridData.FILL;
101
102 combo = new CCombo(container1, SWT.NONE);
103 combo.setLayoutData(oneLine);
104
105 combo.add(Language.GERMAN.getLabel(), 0);
106 combo.add(Language.ENGLISH.getLabel(), 1);
107 combo.select(1);
108 combo.addSelectionListener(new SelectionListener() {
109
110 @Override
111 public void widgetSelected(SelectionEvent e) {
112 try {
113 writeConfigAndRestart(combo.getSelectionIndex());
114 } catch (IOException e1) {
115 e1.printStackTrace();
116 }
117 }
118
119 @Override
120 public void widgetDefaultSelected(SelectionEvent e) {
121 // TODO Auto-generated method stub
122
123 }
124 });
125 }
126
127 @Override
128 protected void okPressed() {
129 try {
130 writeConfigAndRestart(combo.getSelectionIndex());
131 } catch (IOException e) {
132 e.printStackTrace();
133 }
134 super.okPressed();
135 }
136
137
138 private void writeConfigAndRestart(int setLanguage) throws IOException {
139 File file = org.eclipse.core.runtime.preferences.ConfigurationScope.INSTANCE.getLocation().toFile();
140 Properties properties = load(file.getAbsolutePath()+"/config.ini");
141 switch(setLanguage){
142 case 0:
143 properties.setProperty("osgi.nl", "de");
144 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "de");
145 break;
146 case 1:
147 properties.setProperty("osgi.nl", "en");
148 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "en");
149 break;
150 default:
151 break;
152 }
153 save(file+"/config.ini", properties);
154 }
155
156 private Properties load(String filename) throws IOException {
157 FileInputStream in = new FileInputStream(filename);
158 Properties prop = new Properties();
159 prop.load(in);
160 in.close();
161 return prop;
162 }
163
164 private void save(String filename, Properties properties) throws IOException{
165 FileOutputStream fos = new FileOutputStream(filename);
166 properties.store(fos, "");
167 fos.close();
168 }
169
170 private enum Language{
171
172 GERMAN("Deutsch"), ENGLISH("English");
173 private String label;
174 private Language(String label){
175 this.label = label;
176 }
177
178 /**
179 * @return the label
180 */
181 public String getLabel() {
182 return label;
183 }
184 }
185 private static final GridLayout GRID_LAYOUT (int columns, boolean equalwidth){
186 GridLayout layout = new GridLayout();
187 layout.marginTop = 0;
188 layout.marginRight = 0;
189 layout.marginBottom = 0;
190 layout.marginLeft = 0;
191 layout.numColumns = columns;
192 layout.makeColumnsEqualWidth = equalwidth;
193 return layout;
194 }
195 }