001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.preferences;
011:
012: import java.lang.reflect.InvocationTargetException;
013: import java.util.Dictionary;
014: import java.util.Locale;
015: import java.util.Set;
016: import java.util.StringTokenizer;
017: import java.util.TreeSet;
018:
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.core.runtime.IProgressMonitor;
021: import org.eclipse.core.runtime.Platform;
022: import org.eclipse.core.runtime.Preferences;
023: import org.eclipse.jdt.launching.JavaRuntime;
024: import org.eclipse.jface.dialogs.Dialog;
025: import org.eclipse.jface.operation.IRunnableWithProgress;
026: import org.eclipse.pde.internal.core.ICoreConstants;
027: import org.eclipse.pde.internal.core.ModelProviderEvent;
028: import org.eclipse.pde.internal.core.PDECore;
029: import org.eclipse.pde.internal.core.PDEState;
030: import org.eclipse.pde.internal.core.PluginModelManager;
031: import org.eclipse.pde.internal.core.TargetPlatformHelper;
032: import org.eclipse.pde.internal.core.itarget.IEnvironmentInfo;
033: import org.eclipse.pde.internal.core.itarget.ITarget;
034: import org.eclipse.pde.internal.core.itarget.ITargetJRE;
035: import org.eclipse.pde.internal.ui.IHelpContextIds;
036: import org.eclipse.pde.internal.ui.PDEPlugin;
037: import org.eclipse.pde.internal.ui.PDEUIMessages;
038: import org.eclipse.pde.internal.ui.launcher.VMHelper;
039: import org.eclipse.swt.SWT;
040: import org.eclipse.swt.layout.GridData;
041: import org.eclipse.swt.layout.GridLayout;
042: import org.eclipse.swt.widgets.Combo;
043: import org.eclipse.swt.widgets.Composite;
044: import org.eclipse.swt.widgets.Control;
045: import org.eclipse.swt.widgets.Group;
046: import org.eclipse.swt.widgets.Label;
047: import org.eclipse.ui.PlatformUI;
048:
049: public class TargetEnvironmentTab {
050: private Combo fOSCombo;
051: private Combo fWSCombo;
052: private Combo fNLCombo;
053: private Combo fArchCombo;
054:
055: private Preferences preferences;
056: private TreeSet fNLChoices;
057: private TreeSet fOSChoices;
058: private TreeSet fWSChoices;
059: private TreeSet fArchChoices;
060: private Combo fJRECombo;
061:
062: private static boolean LOCALES_INITIALIZED = false;
063: private String fDefaultJRE;
064: private TargetPlatformPreferencePage fPage;
065:
066: public TargetEnvironmentTab(TargetPlatformPreferencePage page) {
067: fPage = page;
068: preferences = PDECore.getDefault().getPluginPreferences();
069: }
070:
071: private void initializeChoices() {
072: fOSChoices = new TreeSet();
073: String[] os = Platform.knownOSValues();
074: for (int i = 0; i < os.length; i++)
075: fOSChoices.add(os[i]);
076: addExtraChoices(fOSChoices, preferences
077: .getString(ICoreConstants.OS_EXTRA));
078:
079: fWSChoices = new TreeSet();
080: String[] ws = Platform.knownWSValues();
081: for (int i = 0; i < ws.length; i++)
082: fWSChoices.add(ws[i]);
083: addExtraChoices(fWSChoices, preferences
084: .getString(ICoreConstants.WS_EXTRA));
085:
086: fArchChoices = new TreeSet();
087: String[] arch = Platform.knownOSArchValues();
088: for (int i = 0; i < arch.length; i++)
089: fArchChoices.add(arch[i]);
090: addExtraChoices(fArchChoices, preferences
091: .getString(ICoreConstants.ARCH_EXTRA));
092:
093: fNLChoices = new TreeSet();
094: if (LOCALES_INITIALIZED) {
095: initializeAllLocales();
096: } else {
097: fNLChoices.add(expandLocaleName(preferences
098: .getString(ICoreConstants.NL)));
099: }
100: }
101:
102: protected void updateChoices() {
103: if (LOCALES_INITIALIZED)
104: return;
105: final String current = fNLCombo.getText();
106: try {
107: PlatformUI.getWorkbench().getProgressService()
108: .busyCursorWhile(new IRunnableWithProgress() {
109: public void run(IProgressMonitor monitor) {
110: initializeAllLocales();
111: LOCALES_INITIALIZED = true;
112: }
113: });
114: } catch (InvocationTargetException e) {
115: PDEPlugin.log(e);
116: } catch (InterruptedException e) {
117: PDEPlugin.log(e);
118: }
119: if (!fNLCombo.isDisposed()) {
120: fNLCombo.setItems((String[]) fNLChoices
121: .toArray(new String[fNLChoices.size()]));
122: fNLCombo.setText(current);
123: }
124: }
125:
126: private void initializeAllLocales() {
127: String[] nl = getLocales();
128: for (int i = 0; i < nl.length; i++)
129: fNLChoices.add(nl[i]);
130: addExtraChoices(fNLChoices, preferences
131: .getString(ICoreConstants.NL_EXTRA));
132: }
133:
134: private void addExtraChoices(Set set, String preference) {
135: StringTokenizer tokenizer = new StringTokenizer(preference, ","); //$NON-NLS-1$
136: while (tokenizer.hasMoreTokens()) {
137: set.add(tokenizer.nextToken().trim());
138: }
139: }
140:
141: public Control createContents(Composite parent) {
142: Composite container = new Composite(parent, SWT.NONE);
143: GridLayout layout = new GridLayout();
144: layout.verticalSpacing = 15;
145: container.setLayout(layout);
146: container.setLayoutData(new GridData(GridData.FILL_BOTH));
147:
148: createTargetEnvironmentGroup(container);
149: createJREGroup(container);
150:
151: Dialog.applyDialogFont(container);
152: PlatformUI.getWorkbench().getHelpSystem().setHelp(container,
153: IHelpContextIds.TARGET_ENVIRONMENT_PREFERENCE_PAGE);
154: return container;
155: }
156:
157: private void createJREGroup(Composite container) {
158: Group group = new Group(container, SWT.NULL);
159: GridLayout layout = new GridLayout();
160: layout.numColumns = 2;
161: group.setLayout(layout);
162: group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
163: group.setText(PDEUIMessages.EnvironmentBlock_jreTitle);
164:
165: Label label = new Label(group, SWT.NONE);
166: label.setText(PDEUIMessages.EnvironmentBlock_jreGroup);
167:
168: fJRECombo = new Combo(group, SWT.SINGLE | SWT.READ_ONLY);
169: fJRECombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
170: fJRECombo.setItems(VMHelper.getVMInstallNames());
171: fDefaultJRE = VMHelper.getDefaultVMInstallName();
172: fJRECombo.setText(fDefaultJRE);
173:
174: label = new Label(group, SWT.WRAP);
175: label.setText(PDEUIMessages.EnvironmentBlock_jreNote);
176: GridData gd = new GridData();
177: gd.horizontalSpan = 2;
178: gd.horizontalIndent = 25;
179: gd.widthHint = 400;
180: label.setLayoutData(gd);
181: }
182:
183: private void createTargetEnvironmentGroup(Composite container) {
184: Group group = new Group(container, SWT.NULL);
185: GridLayout layout = new GridLayout();
186: layout.numColumns = 2;
187: group.setLayout(layout);
188: group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
189: group.setText(PDEUIMessages.EnvironmentBlock_targetEnv);
190:
191: initializeChoices();
192:
193: Label label = new Label(group, SWT.NULL);
194: label
195: .setText(PDEUIMessages.Preferences_TargetEnvironmentPage_os);
196:
197: fOSCombo = new Combo(group, SWT.SINGLE | SWT.BORDER);
198: fOSCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
199: fOSCombo.setItems((String[]) fOSChoices
200: .toArray(new String[fOSChoices.size()]));
201:
202: label = new Label(group, SWT.NULL);
203: label
204: .setText(PDEUIMessages.Preferences_TargetEnvironmentPage_ws);
205:
206: fWSCombo = new Combo(group, SWT.SINGLE | SWT.BORDER);
207: fWSCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
208: fWSCombo.setItems((String[]) fWSChoices
209: .toArray(new String[fWSChoices.size()]));
210:
211: label = new Label(group, SWT.NULL);
212: label
213: .setText(PDEUIMessages.Preferences_TargetEnvironmentPage_arch);
214:
215: fArchCombo = new Combo(group, SWT.SINGLE | SWT.BORDER);
216: fArchCombo
217: .setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
218: fArchCombo.setItems((String[]) fArchChoices
219: .toArray(new String[fArchChoices.size()]));
220:
221: label = new Label(group, SWT.NULL);
222: label
223: .setText(PDEUIMessages.Preferences_TargetEnvironmentPage_nl);
224:
225: fNLCombo = new Combo(group, SWT.SINGLE | SWT.BORDER);
226: fNLCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
227: fNLCombo.setItems((String[]) fNLChoices
228: .toArray(new String[fNLChoices.size()]));
229:
230: fOSCombo.setText(preferences.getString(ICoreConstants.OS));
231: fWSCombo.setText(preferences.getString(ICoreConstants.WS));
232: fNLCombo.setText(expandLocaleName(preferences
233: .getString(ICoreConstants.NL)));
234: fArchCombo.setText(preferences.getString(ICoreConstants.ARCH));
235: }
236:
237: protected void loadTargetProfile(ITarget target) {
238: loadTargetProfileEnvironment(target.getEnvironment());
239: loadTargetProfileJRE(target.getTargetJREInfo());
240: }
241:
242: private void loadTargetProfileEnvironment(IEnvironmentInfo info) {
243: if (info == null)
244: return;
245: String os = info.getDisplayOS();
246: String ws = info.getDisplayWS();
247: String arch = info.getDisplayArch();
248: String nl = info.getDisplayNL();
249: nl = expandLocaleName(nl);
250:
251: if (!os.equals("")) { //$NON-NLS-1$
252: if (fOSCombo.indexOf(os) == -1)
253: fOSCombo.add(os);
254: fOSCombo.setText(os);
255: }
256:
257: if (!ws.equals("")) { //$NON-NLS-1$
258: if (fWSCombo.indexOf(ws) == -1)
259: fWSCombo.add(ws);
260: fWSCombo.setText(ws);
261: }
262:
263: if (!arch.equals("")) { //$NON-NLS-1$
264: if (fArchCombo.indexOf(arch) == -1)
265: fArchCombo.add(arch);
266: fArchCombo.setText(arch);
267: }
268:
269: if (!nl.equals("")) { //$NON-NLS-1$
270: if (fNLCombo.indexOf(nl) == -1)
271: fNLCombo.add(nl);
272: fNLCombo.setText(nl);
273: }
274: }
275:
276: private void loadTargetProfileJRE(ITargetJRE info) {
277: if (info != null)
278: fJRECombo.setText(info.getCompatibleJRE());
279: }
280:
281: /**
282: * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
283: */
284: protected void performDefaults() {
285: fOSCombo.setText(preferences
286: .getDefaultString(ICoreConstants.OS));
287: fWSCombo.setText(preferences
288: .getDefaultString(ICoreConstants.WS));
289: fNLCombo.setText(expandLocaleName(preferences
290: .getDefaultString(ICoreConstants.NL)));
291: fArchCombo.setText(preferences
292: .getDefaultString(ICoreConstants.ARCH));
293: fJRECombo.setText(VMHelper.getDefaultVMInstallName());
294: }
295:
296: public boolean performOk() {
297: applyTargetEnvironmentGroup();
298: applyJREGroup();
299: return true;
300: }
301:
302: private void applyJREGroup() {
303: try {
304: if (!fDefaultJRE.equals(VMHelper.getDefaultVMInstallName()))
305: return;
306:
307: if (!VMHelper.getDefaultVMInstallName().equals(
308: fJRECombo.getText()))
309: JavaRuntime.setDefaultVMInstall(VMHelper
310: .getVMInstall(fJRECombo.getText()), null);
311: } catch (CoreException e) {
312: }
313: }
314:
315: private void applyTargetEnvironmentGroup() {
316: String oldOS = preferences.getString(ICoreConstants.OS);
317: String oldWS = preferences.getString(ICoreConstants.WS);
318: String oldARCH = preferences.getString(ICoreConstants.ARCH);
319: String oldNL = preferences.getString(ICoreConstants.NL);
320: boolean changed = false;
321:
322: String os = fOSCombo.getText().trim();
323: if (os.length() > 0) {
324: if (!fOSChoices.contains(os)) {
325: String value = preferences
326: .getString(ICoreConstants.OS_EXTRA);
327: value = (value.length() > 0) ? value + "," + os : os; //$NON-NLS-1$
328: preferences.setValue(ICoreConstants.OS_EXTRA, value);
329: }
330: preferences.setValue(ICoreConstants.OS, os);
331: changed |= !(os.equals(oldOS));
332: }
333:
334: String ws = fWSCombo.getText().trim();
335: if (ws.length() > 0) {
336: if (!fWSChoices.contains(ws)) {
337: String value = preferences
338: .getString(ICoreConstants.WS_EXTRA);
339: value = (value.length() > 0) ? value + "," + ws : ws; //$NON-NLS-1$
340: preferences.setValue(ICoreConstants.WS_EXTRA, value);
341: }
342: preferences.setValue(ICoreConstants.WS, ws);
343: changed |= !(ws.equals(oldWS));
344: }
345:
346: String arch = fArchCombo.getText().trim();
347: if (arch.length() > 0) {
348: if (!fArchChoices.contains(arch)) {
349: String value = preferences
350: .getString(ICoreConstants.ARCH_EXTRA);
351: value = (value.length() > 0) ? value + "," + arch : arch; //$NON-NLS-1$
352: preferences.setValue(ICoreConstants.ARCH_EXTRA, value);
353: }
354: preferences.setValue(ICoreConstants.ARCH, arch);
355: changed |= !(arch.equals(oldARCH));
356: }
357:
358: String locale = fNLCombo.getText().trim();
359: if (locale.length() > 0) {
360: if (!fNLChoices.contains(locale)) {
361: String value = preferences
362: .getString(ICoreConstants.NL_EXTRA);
363: value = (value.length() > 0) ? value + "," + locale : locale; //$NON-NLS-1$
364: preferences.setValue(ICoreConstants.NL_EXTRA, value);
365: }
366: int dash = locale.indexOf("-"); //$NON-NLS-1$
367: if (dash != -1)
368: locale = locale.substring(0, dash);
369: locale = locale.trim();
370: preferences.setValue(ICoreConstants.NL, locale);
371: changed |= !(locale.equals(oldNL));
372: }
373: PDECore.getDefault().savePluginPreferences();
374: if (changed) {
375: updateState();
376: }
377: }
378:
379: private void updateState() {
380: PDEState state = fPage.getCurrentState();
381: // update the current state with the platform properties of the current environment settings.
382: String[] knownExecutionEnvironments = TargetPlatformHelper
383: .getKnownExecutionEnvironments();
384: Dictionary[] properties = TargetPlatformHelper
385: .getPlatformProperties(knownExecutionEnvironments,
386: fPage.getCurrentState());
387: state.getState().setPlatformProperties(properties);
388: PluginModelManager manager = PDECore.getDefault()
389: .getModelManager();
390: // Resetting the state (manager.getState() != state) refreshes workspace projects automatically. So if we are not reseting
391: // the state, we need to fire an event to have the PluginModelManager re-resolve the current state with the new platform properties.
392: if (manager.getState() == state) {
393: manager.modelsChanged(new ModelProviderEvent(properties,
394: ICoreConstants.ENVIRONMENT_CHANGED, null, null,
395: null));
396: }
397: }
398:
399: private String expandLocaleName(String name) {
400: String language = ""; //$NON-NLS-1$
401: String country = ""; //$NON-NLS-1$
402: String variant = ""; //$NON-NLS-1$
403:
404: StringTokenizer tokenizer = new StringTokenizer(name, "_"); //$NON-NLS-1$
405: if (tokenizer.hasMoreTokens())
406: language = tokenizer.nextToken();
407: if (tokenizer.hasMoreTokens())
408: country = tokenizer.nextToken();
409: if (tokenizer.hasMoreTokens())
410: variant = tokenizer.nextToken();
411:
412: Locale locale = new Locale(language, country, variant);
413: return locale.toString() + " - " + locale.getDisplayName(); //$NON-NLS-1$
414: }
415:
416: private static String[] getLocales() {
417: Locale[] locales = Locale.getAvailableLocales();
418: String[] result = new String[locales.length];
419: for (int i = 0; i < locales.length; i++) {
420: Locale locale = locales[i];
421: StringBuffer buffer = new StringBuffer();
422: buffer.append(locale.toString());
423: buffer.append(" - "); //$NON-NLS-1$
424: buffer.append(locale.getDisplayName());
425: result[i] = buffer.toString();
426: }
427: return result;
428: }
429:
430: }
|