KickJava   Java API By Example, From Geeks To Geeks.

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


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 Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-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.util.*;
14
15 import org.eclipse.core.boot.BootLoader;
16 import org.eclipse.core.runtime.Preferences;
17 import org.eclipse.jface.dialogs.Dialog;
18 import org.eclipse.jface.preference.PreferencePage;
19 import org.eclipse.pde.internal.core.*;
20 import org.eclipse.pde.internal.ui.*;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.layout.*;
23 import org.eclipse.swt.widgets.*;
24 import org.eclipse.ui.*;
25 import org.eclipse.ui.help.WorkbenchHelp;
26
27 /**
28  */

29 public class TargetEnvironmentPreferencePage
30     extends PreferencePage
31     implements IWorkbenchPreferencePage, IEnvironmentVariables {
32     private static final String JavaDoc KEY_DESCRIPTION =
33         "Preferences.TargetEnvironmentPage.Description"; //$NON-NLS-1$
34
public static final String JavaDoc KEY_OS = "Preferences.TargetEnvironmentPage.os"; //$NON-NLS-1$
35
public static final String JavaDoc KEY_WS = "Preferences.TargetEnvironmentPage.ws"; //$NON-NLS-1$
36
public static final String JavaDoc KEY_NL = "Preferences.TargetEnvironmentPage.nl"; //$NON-NLS-1$
37
public static final String JavaDoc KEY_ARCH =
38         "Preferences.TargetEnvironmentPage.arch"; //$NON-NLS-1$
39

40     private Combo os;
41     private Combo ws;
42     private Combo nl;
43     private Combo arch;
44     
45     private Preferences preferences;
46
47     public TargetEnvironmentPreferencePage() {
48         setDescription(PDEPlugin.getResourceString(KEY_DESCRIPTION));
49         preferences = PDECore.getDefault().getPluginPreferences();
50     }
51
52     /**
53      * @see org.eclipse.jface.preference.PreferencePage#createContents(Composite)
54      */

55     protected Control createContents(Composite parent) {
56         Composite container = new Composite(parent, SWT.NULL);
57         GridLayout layout = new GridLayout();
58         layout.numColumns = 2;
59         container.setLayout(layout);
60         
61         Label label = new Label(container, SWT.NULL);
62         label.setText(PDEPlugin.getResourceString(KEY_OS));
63         
64         os = new Combo(container, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY);
65         os.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
66         os.setItems(BootLoader.knownOSValues());
67         
68         label = new Label(container, SWT.NULL);
69         label.setText(PDEPlugin.getResourceString(KEY_WS));
70         
71         ws = new Combo(container, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY);
72         ws.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
73         ws.setItems(BootLoader.knownWSValues());
74         
75         label = new Label(container, SWT.NULL);
76         label.setText(PDEPlugin.getResourceString(KEY_NL));
77         
78         nl = new Combo(container, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY);
79         nl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
80         nl.setItems(getLocales());
81                 
82         label = new Label(container, SWT.NULL);
83         label.setText(PDEPlugin.getResourceString(KEY_ARCH));
84         
85         arch = new Combo(container, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY);
86         arch.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
87         arch.setItems(BootLoader.knownOSArchValues());
88         
89         Dialog.applyDialogFont(container);
90         
91         os.setText(preferences.getString(OS));
92         ws.setText(preferences.getString(WS));
93         nl.setText(expandLocaleName(preferences.getString(NL)));
94         arch.setText(preferences.getString(ARCH));
95             
96         WorkbenchHelp.setHelp(container, IHelpContextIds.TARGET_ENVIRONMENT_PREFERENCE_PAGE);
97
98         return container;
99     }
100     
101     /**
102      * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
103      */

104     protected void performDefaults() {
105         os.setText(preferences.getDefaultString(OS));
106         ws.setText(preferences.getDefaultString(WS));
107         nl.setText(expandLocaleName(preferences.getDefaultString(NL)));
108         arch.setText(preferences.getDefaultString(ARCH));
109     }
110
111
112     public boolean performOk() {
113         preferences.setValue(OS, os.getText().trim());
114         preferences.setValue(WS, ws.getText().trim());
115         String JavaDoc locale = nl.getText().trim();
116         int dash = locale.indexOf("-"); //$NON-NLS-1$
117
if (dash != -1)
118             locale = locale.substring(0, dash);
119         locale = locale.trim();
120         preferences.setValue(NL, locale);
121         preferences.setValue(ARCH, arch.getText().trim());
122
123         PDEPlugin.getDefault().savePluginPreferences();
124         return super.performOk();
125     }
126     
127     /**
128      * Initializes this preference page using the passed desktop.
129      *
130      * @param desktop the current desktop
131      */

132     public void init(IWorkbench workbench) {
133     }
134     
135     private String JavaDoc expandLocaleName(String JavaDoc name) {
136         String JavaDoc language = ""; //$NON-NLS-1$
137
String JavaDoc country = ""; //$NON-NLS-1$
138
String JavaDoc variant = ""; //$NON-NLS-1$
139

140         StringTokenizer tokenizer = new StringTokenizer(name, "_"); //$NON-NLS-1$
141
if (tokenizer.hasMoreTokens())
142             language = tokenizer.nextToken();
143         if (tokenizer.hasMoreTokens())
144             country = tokenizer.nextToken();
145         if (tokenizer.hasMoreTokens())
146             variant = tokenizer.nextToken();
147             
148         Locale locale = new Locale(language, country, variant);
149         return locale.toString() + " - " + locale.getDisplayName(); //$NON-NLS-1$
150
}
151
152     private static String JavaDoc[] getLocales() {
153         Locale[] locales = Locale.getAvailableLocales();
154         String JavaDoc[] result = new String JavaDoc[locales.length];
155         for (int i = 0; i < locales.length; i++) {
156             Locale locale = locales[i];
157             result[i] = locale.toString() + " - " + locale.getDisplayName(); //$NON-NLS-1$
158
}
159         return result;
160     }
161     
162 }
163
Popular Tags