KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > preferences > EnvironmentBlock


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.preferences;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.Locale JavaDoc;
15 import java.util.Set JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
17 import java.util.TreeSet JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.Platform;
22 import org.eclipse.core.runtime.Preferences;
23 import org.eclipse.jdt.launching.JavaRuntime;
24 import org.eclipse.jface.dialogs.Dialog;
25 import org.eclipse.jface.operation.IRunnableWithProgress;
26 import org.eclipse.pde.internal.core.ICoreConstants;
27 import org.eclipse.pde.internal.core.IEnvironmentVariables;
28 import org.eclipse.pde.internal.core.PDECore;
29 import org.eclipse.pde.internal.ui.IHelpContextIds;
30 import org.eclipse.pde.internal.ui.PDEPlugin;
31 import org.eclipse.pde.internal.ui.PDEUIMessages;
32 import org.eclipse.pde.internal.ui.launcher.LauncherUtils;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Combo;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.Group;
40 import org.eclipse.swt.widgets.Label;
41 import org.eclipse.ui.PlatformUI;
42
43 public class EnvironmentBlock implements IEnvironmentVariables {
44     private Combo fOSCombo;
45     private Combo fWSCombo;
46     private Combo fNLCombo;
47     private Combo fArchCombo;
48     
49     private Preferences preferences;
50     private TreeSet JavaDoc fNLChoices;
51     private TreeSet JavaDoc fOSChoices;
52     private TreeSet JavaDoc fWSChoices;
53     private TreeSet JavaDoc fArchChoices;
54     private Combo fJRECombo;
55     
56     private static boolean LOCALES_INITIALIZED = false;
57     private String JavaDoc fDefaultJRE;
58
59     public EnvironmentBlock() {
60         preferences = PDECore.getDefault().getPluginPreferences();
61     }
62     
63     private void initializeChoices() {
64         fOSChoices = new TreeSet JavaDoc();
65         String JavaDoc[] os = Platform.knownOSValues();
66         for (int i = 0; i < os.length; i++)
67             fOSChoices.add(os[i]);
68         addExtraChoices(fOSChoices, preferences.getString(OS_EXTRA));
69         
70         fWSChoices = new TreeSet JavaDoc();
71         String JavaDoc[] ws = Platform.knownWSValues();
72         for (int i = 0; i < ws.length; i++)
73             fWSChoices.add(ws[i]);
74         addExtraChoices(fWSChoices, preferences.getString(WS_EXTRA));
75         
76         fArchChoices = new TreeSet JavaDoc();
77         String JavaDoc[] arch = Platform.knownOSArchValues();
78         for (int i = 0; i < arch.length; i++)
79             fArchChoices.add(arch[i]);
80         addExtraChoices(fArchChoices, preferences.getString(ARCH_EXTRA));
81         
82         fNLChoices = new TreeSet JavaDoc();
83         if (LOCALES_INITIALIZED) {
84             initializeAllLocales();
85         } else {
86             fNLChoices.add(expandLocaleName(preferences.getString(NL)));
87         }
88     }
89     
90     protected void updateChoices() {
91         if (LOCALES_INITIALIZED)
92             return;
93         final String JavaDoc current = fNLCombo.getText();
94         try {
95             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
96                 public void run(IProgressMonitor monitor) {
97                     initializeAllLocales();
98                     LOCALES_INITIALIZED = true;
99                 }
100             });
101         } catch (InvocationTargetException JavaDoc e) {
102             PDEPlugin.log(e);
103         } catch (InterruptedException JavaDoc e) {
104             PDEPlugin.log(e);
105         }
106         fNLCombo.setItems((String JavaDoc[])fNLChoices.toArray(new String JavaDoc[fNLChoices.size()]));
107         fNLCombo.setText(current);
108     }
109     
110     private void initializeAllLocales() {
111         String JavaDoc[] nl = getLocales();
112         for (int i = 0; i < nl.length; i++)
113             fNLChoices.add(nl[i]);
114         addExtraChoices(fNLChoices, preferences.getString(NL_EXTRA));
115     }
116     
117     private void addExtraChoices(Set JavaDoc set, String JavaDoc preference) {
118         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(preference);
119         while (tokenizer.hasMoreTokens()) {
120             set.add(tokenizer.nextToken().trim());
121         }
122     }
123
124     public Control createContents(Composite parent) {
125         Composite container = new Composite(parent, SWT.NONE);
126         GridLayout layout = new GridLayout();
127         layout.verticalSpacing = 15;
128         container.setLayout(layout);
129         container.setLayoutData(new GridData(GridData.FILL_BOTH));
130         
131         createTargetEnvironmentGroup(container);
132         createJREGroup(container);
133         
134         Dialog.applyDialogFont(container);
135         PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.TARGET_ENVIRONMENT_PREFERENCE_PAGE);
136         return container;
137     }
138     
139     private void createJREGroup(Composite container) {
140         Group group = new Group(container, SWT.NULL);
141         GridLayout layout = new GridLayout();
142         layout.numColumns = 2;
143         group.setLayout(layout);
144         group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
145         group.setText(PDEUIMessages.EnvironmentBlock_jreTitle);
146         
147         Label label = new Label(group, SWT.NONE);
148         label.setText(PDEUIMessages.EnvironmentBlock_jreGroup); //$NON-NLS-1$
149

150         fJRECombo = new Combo(group, SWT.SINGLE|SWT.READ_ONLY);
151         fJRECombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
152         fJRECombo.setItems(LauncherUtils.getVMInstallNames());
153         fDefaultJRE = LauncherUtils.getDefaultVMInstallName();
154         fJRECombo.setText(fDefaultJRE);
155         
156         label = new Label(group, SWT.WRAP);
157         label.setText(PDEUIMessages.EnvironmentBlock_jreNote); //$NON-NLS-1$
158
GridData gd = new GridData();
159         gd.horizontalSpan = 2;
160         gd.horizontalIndent = 25;
161         gd.widthHint = 400;
162         label.setLayoutData(gd);
163     }
164
165     private void createTargetEnvironmentGroup(Composite container) {
166         Group group = new Group(container, SWT.NULL);
167         GridLayout layout = new GridLayout();
168         layout.numColumns = 2;
169         group.setLayout(layout);
170         group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
171         group.setText(PDEUIMessages.EnvironmentBlock_targetEnv); //$NON-NLS-1$
172

173         initializeChoices();
174         
175         Label label = new Label(group, SWT.NULL);
176         label.setText(PDEUIMessages.Preferences_TargetEnvironmentPage_os);
177         
178         fOSCombo = new Combo(group, SWT.SINGLE | SWT.BORDER);
179         fOSCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
180         fOSCombo.setItems((String JavaDoc[])fOSChoices.toArray(new String JavaDoc[fOSChoices.size()]));
181         
182         label = new Label(group, SWT.NULL);
183         label.setText(PDEUIMessages.Preferences_TargetEnvironmentPage_ws);
184         
185         fWSCombo = new Combo(group, SWT.SINGLE | SWT.BORDER);
186         fWSCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
187         fWSCombo.setItems((String JavaDoc[])fWSChoices.toArray(new String JavaDoc[fWSChoices.size()]));
188         
189         label = new Label(group, SWT.NULL);
190         label.setText(PDEUIMessages.Preferences_TargetEnvironmentPage_arch);
191         
192         fArchCombo = new Combo(group, SWT.SINGLE | SWT.BORDER);
193         fArchCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
194         fArchCombo.setItems((String JavaDoc[])fArchChoices.toArray(new String JavaDoc[fArchChoices.size()]));
195         
196         label = new Label(group, SWT.NULL);
197         label.setText(PDEUIMessages.Preferences_TargetEnvironmentPage_nl);
198         
199         fNLCombo = new Combo(group, SWT.SINGLE | SWT.BORDER);
200         fNLCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
201         fNLCombo.setItems((String JavaDoc[])fNLChoices.toArray(new String JavaDoc[fNLChoices.size()]));
202                 
203         fOSCombo.setText(preferences.getString(OS));
204         fWSCombo.setText(preferences.getString(WS));
205         fNLCombo.setText(expandLocaleName(preferences.getString(NL)));
206         fArchCombo.setText(preferences.getString(ARCH));
207     }
208     
209     /**
210      * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
211      */

212     protected void performDefaults() {
213         fOSCombo.setText(preferences.getDefaultString(OS));
214         fWSCombo.setText(preferences.getDefaultString(WS));
215         fNLCombo.setText(expandLocaleName(preferences.getDefaultString(NL)));
216         fArchCombo.setText(preferences.getDefaultString(ARCH));
217         fJRECombo.setText(LauncherUtils.getDefaultVMInstallName());
218     }
219
220     public boolean performOk() {
221         applySelfHostingMode();
222         applyTargetEnvironmentGroup();
223         applyJREGroup();
224         return true;
225     }
226     
227     private void applyJREGroup() {
228         try {
229             if (!fDefaultJRE.equals(LauncherUtils.getDefaultVMInstallName()))
230                 return;
231             
232             if (!LauncherUtils.getDefaultVMInstallName().equals(fJRECombo.getText()))
233                 JavaRuntime.setDefaultVMInstall(LauncherUtils.getVMInstall(fJRECombo.getText()), null);
234         } catch (CoreException e) {
235         }
236     }
237     
238     private void applySelfHostingMode() {
239         preferences.setValue(ICoreConstants.STRICT_MODE, false);
240     }
241     
242     private void applyTargetEnvironmentGroup() {
243         String JavaDoc os = fOSCombo.getText().trim();
244         if (os.length() > 0) {
245             if (!fOSChoices.contains(os)) {
246                 String JavaDoc value = preferences.getString(OS_EXTRA);
247                 value = (value.length() > 0) ? value + "," + os : os; //$NON-NLS-1$
248
preferences.setValue(OS_EXTRA, value);
249             }
250             preferences.setValue(OS, os);
251         }
252         
253         String JavaDoc ws = fWSCombo.getText().trim();
254         if (ws.length() > 0) {
255             if (!fWSChoices.contains(ws)) {
256                 String JavaDoc value = preferences.getString(WS_EXTRA);
257                 value = (value.length() > 0) ? value + "," + ws : ws; //$NON-NLS-1$
258
preferences.setValue(WS_EXTRA, value);
259             }
260             preferences.setValue(WS, ws);
261         }
262         
263         String JavaDoc arch = fArchCombo.getText().trim();
264         if (arch.length() > 0) {
265             if (!fArchChoices.contains(arch)) {
266                 String JavaDoc value = preferences.getString(ARCH_EXTRA);
267                 value = (value.length() > 0) ? value + "," + arch : arch; //$NON-NLS-1$
268
preferences.setValue(ARCH_EXTRA, value);
269             }
270             preferences.setValue(ARCH, arch);
271         }
272         
273         String JavaDoc locale = fNLCombo.getText().trim();
274         if (locale.length() > 0) {
275             if (!fNLChoices.contains(locale)) {
276                 String JavaDoc value = preferences.getString(NL_EXTRA);
277                 value = (value.length() > 0) ? value + "," + locale : locale; //$NON-NLS-1$
278
preferences.setValue(NL_EXTRA, value);
279             }
280             int dash = locale.indexOf("-"); //$NON-NLS-1$
281
if (dash != -1)
282                 locale = locale.substring(0, dash);
283             locale = locale.trim();
284             preferences.setValue(NL, locale);
285         }
286         PDECore.getDefault().savePluginPreferences();
287     }
288         
289     
290     private String JavaDoc expandLocaleName(String JavaDoc name) {
291         String JavaDoc language = ""; //$NON-NLS-1$
292
String JavaDoc country = ""; //$NON-NLS-1$
293
String JavaDoc variant = ""; //$NON-NLS-1$
294

295         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(name, "_"); //$NON-NLS-1$
296
if (tokenizer.hasMoreTokens())
297             language = tokenizer.nextToken();
298         if (tokenizer.hasMoreTokens())
299             country = tokenizer.nextToken();
300         if (tokenizer.hasMoreTokens())
301             variant = tokenizer.nextToken();
302             
303         Locale JavaDoc locale = new Locale JavaDoc(language, country, variant);
304         return locale.toString() + " - " + locale.getDisplayName(); //$NON-NLS-1$
305
}
306
307     private static String JavaDoc[] getLocales() {
308         Locale JavaDoc[] locales = Locale.getAvailableLocales();
309         String JavaDoc[] result = new String JavaDoc[locales.length];
310         for (int i = 0; i < locales.length; i++) {
311             Locale JavaDoc locale = locales[i];
312             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
313             buffer.append(locale.toString());
314             buffer.append(" - "); //$NON-NLS-1$
315
buffer.append(locale.getDisplayName());
316             result[i] = buffer.toString();
317         }
318         return result;
319     }
320 }
321
Popular Tags