KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.Dictionary JavaDoc;
15 import java.util.Locale JavaDoc;
16 import java.util.Set JavaDoc;
17 import java.util.StringTokenizer JavaDoc;
18 import java.util.TreeSet JavaDoc;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.Platform;
23 import org.eclipse.core.runtime.Preferences;
24 import org.eclipse.jdt.launching.JavaRuntime;
25 import org.eclipse.jface.dialogs.Dialog;
26 import org.eclipse.jface.operation.IRunnableWithProgress;
27 import org.eclipse.pde.internal.core.ICoreConstants;
28 import org.eclipse.pde.internal.core.ModelProviderEvent;
29 import org.eclipse.pde.internal.core.PDECore;
30 import org.eclipse.pde.internal.core.PDEState;
31 import org.eclipse.pde.internal.core.PluginModelManager;
32 import org.eclipse.pde.internal.core.TargetPlatformHelper;
33 import org.eclipse.pde.internal.core.itarget.IEnvironmentInfo;
34 import org.eclipse.pde.internal.core.itarget.ITarget;
35 import org.eclipse.pde.internal.core.itarget.ITargetJRE;
36 import org.eclipse.pde.internal.ui.IHelpContextIds;
37 import org.eclipse.pde.internal.ui.PDEPlugin;
38 import org.eclipse.pde.internal.ui.PDEUIMessages;
39 import org.eclipse.pde.internal.ui.launcher.VMHelper;
40 import org.eclipse.swt.SWT;
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.layout.GridLayout;
43 import org.eclipse.swt.widgets.Combo;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Control;
46 import org.eclipse.swt.widgets.Group;
47 import org.eclipse.swt.widgets.Label;
48 import org.eclipse.ui.PlatformUI;
49
50 public class TargetEnvironmentTab {
51     private Combo fOSCombo;
52     private Combo fWSCombo;
53     private Combo fNLCombo;
54     private Combo fArchCombo;
55     
56     private Preferences preferences;
57     private TreeSet JavaDoc fNLChoices;
58     private TreeSet JavaDoc fOSChoices;
59     private TreeSet JavaDoc fWSChoices;
60     private TreeSet JavaDoc fArchChoices;
61     private Combo fJRECombo;
62     
63     private static boolean LOCALES_INITIALIZED = false;
64     private String JavaDoc fDefaultJRE;
65     private TargetPlatformPreferencePage fPage;
66
67     public TargetEnvironmentTab(TargetPlatformPreferencePage page) {
68         fPage = page;
69         preferences = PDECore.getDefault().getPluginPreferences();
70     }
71     
72     private void initializeChoices() {
73         fOSChoices = new TreeSet JavaDoc();
74         String JavaDoc[] os = Platform.knownOSValues();
75         for (int i = 0; i < os.length; i++)
76             fOSChoices.add(os[i]);
77         addExtraChoices(fOSChoices, preferences.getString(ICoreConstants.OS_EXTRA));
78         
79         fWSChoices = new TreeSet JavaDoc();
80         String JavaDoc[] ws = Platform.knownWSValues();
81         for (int i = 0; i < ws.length; i++)
82             fWSChoices.add(ws[i]);
83         addExtraChoices(fWSChoices, preferences.getString(ICoreConstants.WS_EXTRA));
84         
85         fArchChoices = new TreeSet JavaDoc();
86         String JavaDoc[] arch = Platform.knownOSArchValues();
87         for (int i = 0; i < arch.length; i++)
88             fArchChoices.add(arch[i]);
89         addExtraChoices(fArchChoices, preferences.getString(ICoreConstants.ARCH_EXTRA));
90         
91         fNLChoices = new TreeSet JavaDoc();
92         if (LOCALES_INITIALIZED) {
93             initializeAllLocales();
94         } else {
95             fNLChoices.add(expandLocaleName(preferences.getString(ICoreConstants.NL)));
96         }
97     }
98     
99     protected void updateChoices() {
100         if (LOCALES_INITIALIZED)
101             return;
102         final String JavaDoc current = fNLCombo.getText();
103         try {
104             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
105                 public void run(IProgressMonitor monitor) {
106                     initializeAllLocales();
107                     LOCALES_INITIALIZED = true;
108                 }
109             });
110         } catch (InvocationTargetException JavaDoc e) {
111             PDEPlugin.log(e);
112         } catch (InterruptedException JavaDoc e) {
113             PDEPlugin.log(e);
114         }
115         if (!fNLCombo.isDisposed()) {
116             fNLCombo.setItems((String JavaDoc[])fNLChoices.toArray(new String JavaDoc[fNLChoices.size()]));
117             fNLCombo.setText(current);
118         }
119     }
120     
121     private void initializeAllLocales() {
122         String JavaDoc[] nl = getLocales();
123         for (int i = 0; i < nl.length; i++)
124             fNLChoices.add(nl[i]);
125         addExtraChoices(fNLChoices, preferences.getString(ICoreConstants.NL_EXTRA));
126     }
127     
128     private void addExtraChoices(Set JavaDoc set, String JavaDoc preference) {
129         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(preference,","); //$NON-NLS-1$
130
while (tokenizer.hasMoreTokens()) {
131             set.add(tokenizer.nextToken().trim());
132         }
133     }
134
135     public Control createContents(Composite parent) {
136         Composite container = new Composite(parent, SWT.NONE);
137         GridLayout layout = new GridLayout();
138         layout.verticalSpacing = 15;
139         container.setLayout(layout);
140         container.setLayoutData(new GridData(GridData.FILL_BOTH));
141         
142         createTargetEnvironmentGroup(container);
143         createJREGroup(container);
144         
145         Dialog.applyDialogFont(container);
146         PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.TARGET_ENVIRONMENT_PREFERENCE_PAGE);
147         return container;
148     }
149     
150     private void createJREGroup(Composite container) {
151         Group group = new Group(container, SWT.NULL);
152         GridLayout layout = new GridLayout();
153         layout.numColumns = 2;
154         group.setLayout(layout);
155         group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
156         group.setText(PDEUIMessages.EnvironmentBlock_jreTitle);
157         
158         Label label = new Label(group, SWT.NONE);
159         label.setText(PDEUIMessages.EnvironmentBlock_jreGroup);
160         
161         fJRECombo = new Combo(group, SWT.SINGLE|SWT.READ_ONLY);
162         fJRECombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
163         fJRECombo.setItems(VMHelper.getVMInstallNames());
164         fDefaultJRE = VMHelper.getDefaultVMInstallName();
165         fJRECombo.setText(fDefaultJRE);
166         
167         label = new Label(group, SWT.WRAP);
168         label.setText(PDEUIMessages.EnvironmentBlock_jreNote);
169         GridData gd = new GridData();
170         gd.horizontalSpan = 2;
171         gd.horizontalIndent = 25;
172         gd.widthHint = 400;
173         label.setLayoutData(gd);
174     }
175
176     private void createTargetEnvironmentGroup(Composite container) {
177         Group group = new Group(container, SWT.NULL);
178         GridLayout layout = new GridLayout();
179         layout.numColumns = 2;
180         group.setLayout(layout);
181         group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
182         group.setText(PDEUIMessages.EnvironmentBlock_targetEnv);
183         
184         initializeChoices();
185         
186         Label label = new Label(group, SWT.NULL);
187         label.setText(PDEUIMessages.Preferences_TargetEnvironmentPage_os);
188         
189         fOSCombo = new Combo(group, SWT.SINGLE | SWT.BORDER);
190         fOSCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
191         fOSCombo.setItems((String JavaDoc[])fOSChoices.toArray(new String JavaDoc[fOSChoices.size()]));
192         
193         label = new Label(group, SWT.NULL);
194         label.setText(PDEUIMessages.Preferences_TargetEnvironmentPage_ws);
195         
196         fWSCombo = new Combo(group, SWT.SINGLE | SWT.BORDER);
197         fWSCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
198         fWSCombo.setItems((String JavaDoc[])fWSChoices.toArray(new String JavaDoc[fWSChoices.size()]));
199         
200         label = new Label(group, SWT.NULL);
201         label.setText(PDEUIMessages.Preferences_TargetEnvironmentPage_arch);
202         
203         fArchCombo = new Combo(group, SWT.SINGLE | SWT.BORDER);
204         fArchCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
205         fArchCombo.setItems((String JavaDoc[])fArchChoices.toArray(new String JavaDoc[fArchChoices.size()]));
206         
207         label = new Label(group, SWT.NULL);
208         label.setText(PDEUIMessages.Preferences_TargetEnvironmentPage_nl);
209         
210         fNLCombo = new Combo(group, SWT.SINGLE | SWT.BORDER);
211         fNLCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
212         fNLCombo.setItems((String JavaDoc[])fNLChoices.toArray(new String JavaDoc[fNLChoices.size()]));
213                 
214         fOSCombo.setText(preferences.getString(ICoreConstants.OS));
215         fWSCombo.setText(preferences.getString(ICoreConstants.WS));
216         fNLCombo.setText(expandLocaleName(preferences.getString(ICoreConstants.NL)));
217         fArchCombo.setText(preferences.getString(ICoreConstants.ARCH));
218     }
219     
220     protected void loadTargetProfile(ITarget target) {
221         loadTargetProfileEnvironment(target.getEnvironment());
222         loadTargetProfileJRE(target.getTargetJREInfo());
223     }
224     
225     private void loadTargetProfileEnvironment(IEnvironmentInfo info) {
226         if (info == null)
227             return;
228         String JavaDoc os = info.getDisplayOS();
229         String JavaDoc ws = info.getDisplayWS();
230         String JavaDoc arch = info.getDisplayArch();
231         String JavaDoc nl = info.getDisplayNL();
232         nl = expandLocaleName(nl);
233         
234         if (!os.equals("")) { //$NON-NLS-1$
235
if (fOSCombo.indexOf(os) == -1)
236                 fOSCombo.add(os);
237             fOSCombo.setText(os);
238         }
239         
240         if (!ws.equals("")) { //$NON-NLS-1$
241
if (fWSCombo.indexOf(ws) == -1)
242                 fWSCombo.add(ws);
243             fWSCombo.setText(ws);
244         }
245         
246         if (!arch.equals("")) { //$NON-NLS-1$
247
if (fArchCombo.indexOf(arch) == -1)
248                 fArchCombo.add(arch);
249             fArchCombo.setText(arch);
250         }
251         
252         if (!nl.equals("")) { //$NON-NLS-1$
253
if (fNLCombo.indexOf(nl) == -1)
254                 fNLCombo.add(nl);
255             fNLCombo.setText(nl);
256         }
257     }
258     
259     private void loadTargetProfileJRE(ITargetJRE info) {
260         if (info != null)
261             fJRECombo.setText(info.getCompatibleJRE());
262     }
263     
264     /**
265      * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
266      */

267     protected void performDefaults() {
268         fOSCombo.setText(preferences.getDefaultString(ICoreConstants.OS));
269         fWSCombo.setText(preferences.getDefaultString(ICoreConstants.WS));
270         fNLCombo.setText(expandLocaleName(preferences.getDefaultString(ICoreConstants.NL)));
271         fArchCombo.setText(preferences.getDefaultString(ICoreConstants.ARCH));
272         fJRECombo.setText(VMHelper.getDefaultVMInstallName());
273     }
274
275     public boolean performOk() {
276         applyTargetEnvironmentGroup();
277         applyJREGroup();
278         return true;
279     }
280     
281     private void applyJREGroup() {
282         try {
283             if (!fDefaultJRE.equals(VMHelper.getDefaultVMInstallName()))
284                 return;
285             
286             if (!VMHelper.getDefaultVMInstallName().equals(fJRECombo.getText()))
287                 JavaRuntime.setDefaultVMInstall(VMHelper.getVMInstall(fJRECombo.getText()), null);
288         } catch (CoreException e) {
289         }
290     }
291     
292     private void applyTargetEnvironmentGroup() {
293         String JavaDoc oldOS = preferences.getString(ICoreConstants.OS);
294         String JavaDoc oldWS = preferences.getString(ICoreConstants.WS);
295         String JavaDoc oldARCH = preferences.getString(ICoreConstants.ARCH);
296         String JavaDoc oldNL = preferences.getString(ICoreConstants.NL);
297         boolean changed = false;
298         
299         String JavaDoc os = fOSCombo.getText().trim();
300         if (os.length() > 0) {
301             if (!fOSChoices.contains(os)) {
302                 String JavaDoc value = preferences.getString(ICoreConstants.OS_EXTRA);
303                 value = (value.length() > 0) ? value + "," + os : os; //$NON-NLS-1$
304
preferences.setValue(ICoreConstants.OS_EXTRA, value);
305             }
306             preferences.setValue(ICoreConstants.OS, os);
307             changed |= !(os.equals(oldOS));
308         }
309         
310         String JavaDoc ws = fWSCombo.getText().trim();
311         if (ws.length() > 0) {
312             if (!fWSChoices.contains(ws)) {
313                 String JavaDoc value = preferences.getString(ICoreConstants.WS_EXTRA);
314                 value = (value.length() > 0) ? value + "," + ws : ws; //$NON-NLS-1$
315
preferences.setValue(ICoreConstants.WS_EXTRA, value);
316             }
317             preferences.setValue(ICoreConstants.WS, ws);
318             changed |= !(ws.equals(oldWS));
319         }
320         
321         String JavaDoc arch = fArchCombo.getText().trim();
322         if (arch.length() > 0) {
323             if (!fArchChoices.contains(arch)) {
324                 String JavaDoc value = preferences.getString(ICoreConstants.ARCH_EXTRA);
325                 value = (value.length() > 0) ? value + "," + arch : arch; //$NON-NLS-1$
326
preferences.setValue(ICoreConstants.ARCH_EXTRA, value);
327             }
328             preferences.setValue(ICoreConstants.ARCH, arch);
329             changed |= !(arch.equals(oldARCH));
330         }
331         
332         String JavaDoc locale = fNLCombo.getText().trim();
333         if (locale.length() > 0) {
334             if (!fNLChoices.contains(locale)) {
335                 String JavaDoc value = preferences.getString(ICoreConstants.NL_EXTRA);
336                 value = (value.length() > 0) ? value + "," + locale : locale; //$NON-NLS-1$
337
preferences.setValue(ICoreConstants.NL_EXTRA, value);
338             }
339             int dash = locale.indexOf("-"); //$NON-NLS-1$
340
if (dash != -1)
341                 locale = locale.substring(0, dash);
342             locale = locale.trim();
343             preferences.setValue(ICoreConstants.NL, locale);
344             changed |= !(locale.equals(oldNL));
345         }
346         PDECore.getDefault().savePluginPreferences();
347         if (changed) {
348             updateState();
349         }
350     }
351     
352     private void updateState() {
353         PDEState state = fPage.getCurrentState();
354         // update the current state with the platform properties of the current environment settings.
355
String JavaDoc[] knownExecutionEnvironments = TargetPlatformHelper.getKnownExecutionEnvironments();
356         Dictionary JavaDoc[] properties = TargetPlatformHelper.getPlatformProperties(knownExecutionEnvironments, fPage.getCurrentState());
357         state.getState().setPlatformProperties(properties);
358         PluginModelManager manager = PDECore.getDefault().getModelManager();
359         // Resetting the state (manager.getState() != state) refreshes workspace projects automatically. So if we are not reseting
360
// the state, we need to fire an event to have the PluginModelManager re-resolve the current state with the new platform properties.
361
if (manager.getState() == state) {
362             manager.modelsChanged(new ModelProviderEvent(
363                     properties,
364                     ICoreConstants.ENVIRONMENT_CHANGED,
365                     null,
366                     null,
367                     null));
368         }
369     }
370     
371     private String JavaDoc expandLocaleName(String JavaDoc name) {
372         String JavaDoc language = ""; //$NON-NLS-1$
373
String JavaDoc country = ""; //$NON-NLS-1$
374
String JavaDoc variant = ""; //$NON-NLS-1$
375

376         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(name, "_"); //$NON-NLS-1$
377
if (tokenizer.hasMoreTokens())
378             language = tokenizer.nextToken();
379         if (tokenizer.hasMoreTokens())
380             country = tokenizer.nextToken();
381         if (tokenizer.hasMoreTokens())
382             variant = tokenizer.nextToken();
383             
384         Locale JavaDoc locale = new Locale JavaDoc(language, country, variant);
385         return locale.toString() + " - " + locale.getDisplayName(); //$NON-NLS-1$
386
}
387
388     private static String JavaDoc[] getLocales() {
389         Locale JavaDoc[] locales = Locale.getAvailableLocales();
390         String JavaDoc[] result = new String JavaDoc[locales.length];
391         for (int i = 0; i < locales.length; i++) {
392             Locale JavaDoc locale = locales[i];
393             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
394             buffer.append(locale.toString());
395             buffer.append(" - "); //$NON-NLS-1$
396
buffer.append(locale.getDisplayName());
397             result[i] = buffer.toString();
398         }
399         return result;
400     }
401     
402 }
403
Popular Tags