KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > target > EnvironmentSection


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.editor.target;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.Locale JavaDoc;
15 import java.util.StringTokenizer JavaDoc;
16 import java.util.TreeSet JavaDoc;
17
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.Platform;
20 import org.eclipse.jface.operation.IRunnableWithProgress;
21 import org.eclipse.pde.core.IModelChangedEvent;
22 import org.eclipse.pde.internal.core.itarget.IEnvironmentInfo;
23 import org.eclipse.pde.internal.core.itarget.ITarget;
24 import org.eclipse.pde.internal.core.itarget.ITargetModel;
25 import org.eclipse.pde.internal.ui.PDEPlugin;
26 import org.eclipse.pde.internal.ui.PDEUIMessages;
27 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
28 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
29 import org.eclipse.pde.internal.ui.editor.PDESection;
30 import org.eclipse.pde.internal.ui.parts.ComboPart;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.custom.CCombo;
33 import org.eclipse.swt.events.FocusAdapter;
34 import org.eclipse.swt.events.FocusEvent;
35 import org.eclipse.swt.events.ModifyEvent;
36 import org.eclipse.swt.events.ModifyListener;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.layout.GridLayout;
39 import org.eclipse.swt.widgets.Combo;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Control;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.ui.PlatformUI;
44 import org.eclipse.ui.forms.IFormColors;
45 import org.eclipse.ui.forms.widgets.FormToolkit;
46 import org.eclipse.ui.forms.widgets.Section;
47
48 public class EnvironmentSection extends PDESection {
49     
50     private ComboPart fOSCombo;
51     private ComboPart fWSCombo;
52     private ComboPart fNLCombo;
53     private ComboPart fArchCombo;
54     
55     private TreeSet JavaDoc fNLChoices;
56     private TreeSet JavaDoc fOSChoices;
57     private TreeSet JavaDoc fWSChoices;
58     private TreeSet JavaDoc fArchChoices;
59     private boolean LOCALES_INITIALIZED = false;
60
61     public EnvironmentSection(PDEFormPage page, Composite parent) {
62         super(page, parent, Section.DESCRIPTION);
63         createClient(getSection(), page.getEditor().getToolkit());
64     }
65
66     protected void createClient(Section section, FormToolkit toolkit) {
67         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
68         section.setText(PDEUIMessages.EnvironmentSection_title);
69         section.setDescription(PDEUIMessages.EnvironmentSection_description);
70         GridData data = new GridData(GridData.FILL_HORIZONTAL);
71         data.verticalAlignment = SWT.TOP;
72         data.horizontalSpan = 2;
73         section.setLayoutData(data);
74         
75         Composite client = toolkit.createComposite(section);
76         client.setLayout(FormLayoutFactory.createSectionClientGridLayout(true, 2));
77         client.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
78         
79         Composite left = toolkit.createComposite(client);
80         left.setLayout(new GridLayout(2, false));
81         GridLayout layout= FormLayoutFactory.createClearGridLayout(false, 2);
82         layout.horizontalSpacing = layout.verticalSpacing = 5;
83         left.setLayout(layout);
84         left.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
85         
86         IEnvironmentInfo orgEnv = getEnvironment();
87         initializeChoices(orgEnv);
88         
89         Label label = toolkit.createLabel(left, PDEUIMessages.EnvironmentSection_operationSystem);
90         label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
91         
92         fOSCombo = new ComboPart();
93         fOSCombo.createControl(left, toolkit, SWT.SINGLE | SWT.BORDER );
94         fOSCombo.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
95         fOSCombo.setItems((String JavaDoc[])fOSChoices.toArray(new String JavaDoc[fOSChoices.size()]));
96
97         label = toolkit.createLabel(left, PDEUIMessages.EnvironmentSection_windowingSystem);
98         label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
99         
100         fWSCombo = new ComboPart();
101         fWSCombo.createControl(left, toolkit, SWT.SINGLE | SWT.BORDER);
102         fWSCombo.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
103         fWSCombo.setItems((String JavaDoc[])fWSChoices.toArray(new String JavaDoc[fWSChoices.size()]));
104
105         Composite right = toolkit.createComposite(client);
106         layout= FormLayoutFactory.createClearGridLayout(false, 2);
107         layout.verticalSpacing = layout.horizontalSpacing = 5;
108         right.setLayout(layout);
109         right.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
110         
111         label = toolkit.createLabel(right, PDEUIMessages.EnvironmentSection_architecture);
112         label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
113         
114         fArchCombo = new ComboPart();
115         fArchCombo.createControl(right, toolkit, SWT.SINGLE | SWT.BORDER);
116         fArchCombo.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
117         fArchCombo.setItems((String JavaDoc[])fArchChoices.toArray(new String JavaDoc[fArchChoices.size()]));
118         
119         
120         label = toolkit.createLabel(right, PDEUIMessages.EnvironmentSection_locale);
121         label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
122         
123         fNLCombo = new ComboPart();
124         fNLCombo.createControl(right, toolkit, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL);
125         fNLCombo.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
126         fNLCombo.setItems((String JavaDoc[])fNLChoices.toArray(new String JavaDoc[fNLChoices.size()]));
127         
128         refresh();
129         
130         fOSCombo.addModifyListener(new ModifyListener() {
131             public void modifyText(ModifyEvent e) {
132                 getEnvironment().setOS(getText(fOSCombo));
133             }
134         });
135         fWSCombo.addModifyListener(new ModifyListener() {
136             public void modifyText(ModifyEvent e) {
137                 getEnvironment().setWS(getText(fWSCombo));
138             }
139         });
140         fArchCombo.addModifyListener(new ModifyListener() {
141             public void modifyText(ModifyEvent e) {
142                 getEnvironment().setArch(getText(fArchCombo));
143             }
144         });
145         fNLCombo.getControl().addFocusListener(new FocusAdapter() {
146             public void focusGained(FocusEvent event) {
147                 // if we haven't gotten all the values for the NL's, display a busy cursor to the user while we find them.
148
if (!LOCALES_INITIALIZED) {
149                     try {
150                         PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
151                             public void run(IProgressMonitor monitor) {
152                                 initializeAllLocales();
153                                 LOCALES_INITIALIZED = true;
154                             }
155                         });
156                     } catch (InvocationTargetException JavaDoc e) {
157                         PDEPlugin.log(e);
158                     } catch (InterruptedException JavaDoc e) {
159                         PDEPlugin.log(e);
160                     }
161                 }
162                 
163                 // first time through, we should have a max item count of 1.
164
// On the first time through, we need to set the new values, and also attach the listener
165
// If we attached the listener initially, when we call setItems(..), it would make the editor dirty (when the user didn't change anything)
166
if (fNLCombo.getItemCount() < 3) {
167                     String JavaDoc current = fNLCombo.getSelection();
168                     if (!fNLCombo.getControl().isDisposed()) {
169                         fNLCombo.setItems((String JavaDoc[])fNLChoices.toArray(new String JavaDoc[fNLChoices.size()]));
170                         fNLCombo.setText(current);
171                     }
172                     
173                     fNLCombo.addModifyListener(new ModifyListener() {
174                         public void modifyText(ModifyEvent e) {
175                             String JavaDoc value = getText(fNLCombo);
176                             int index = value.indexOf("-"); //$NON-NLS-1$
177
if (index > 0)
178                                 value = value.substring(0, index);
179                             getEnvironment().setNL(value.trim());
180                         }
181                     });
182                 }
183                     
184             }
185         });
186         
187         toolkit.paintBordersFor(client);
188         section.setClient(client);
189         
190         // Register to be notified when the model changes
191
getModel().addModelChangedListener(this);
192     }
193     
194     /* (non-Javadoc)
195      * @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
196      */

197     public void modelChanged(IModelChangedEvent e) {
198         // No need to call super, handling world changed event here
199
if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
200             handleModelEventWorldChanged(e);
201         }
202     }
203     
204     /**
205      * @param event
206      */

207     private void handleModelEventWorldChanged(IModelChangedEvent event) {
208         // Perform the refresh
209
refresh();
210     }
211
212     /* (non-Javadoc)
213      * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
214      */

215     public void dispose() {
216         ITargetModel model = getModel();
217         if (model != null) {
218             model.removeModelChangedListener(this);
219         }
220         super.dispose();
221     }
222     
223     private void initializeChoices(IEnvironmentInfo orgEnv) {
224         fOSChoices = new TreeSet JavaDoc();
225         String JavaDoc[] os = Platform.knownOSValues();
226         for (int i = 0; i < os.length; i++)
227             fOSChoices.add(os[i]);
228         fOSChoices.add(""); //$NON-NLS-1$
229
String JavaDoc fileValue = orgEnv.getOS();
230         if (fileValue != null)
231             fOSChoices.add(fileValue);
232         
233         fWSChoices = new TreeSet JavaDoc();
234         String JavaDoc[] ws = Platform.knownWSValues();
235         for (int i = 0; i < ws.length; i++)
236             fWSChoices.add(ws[i]);
237         fWSChoices.add(""); //$NON-NLS-1$
238
fileValue = orgEnv.getWS();
239         if (fileValue != null)
240             fWSChoices.add(fileValue);
241         
242         fArchChoices = new TreeSet JavaDoc();
243         String JavaDoc[] arch = Platform.knownOSArchValues();
244         for (int i = 0; i < arch.length; i++)
245             fArchChoices.add(arch[i]);
246         fArchChoices.add(""); //$NON-NLS-1$
247
fileValue = orgEnv.getArch();
248         if (fileValue != null)
249             fArchChoices.add(fileValue);
250         
251         fNLChoices = new TreeSet JavaDoc();
252         fNLChoices.add(""); //$NON-NLS-1$
253
}
254     
255     private void initializeAllLocales() {
256         String JavaDoc[] nl = getLocales();
257         for (int i = 0; i < nl.length; i++)
258             fNLChoices.add(nl[i]);
259         String JavaDoc fileValue = getEnvironment().getNL();
260         if (fileValue != null)
261             fNLChoices.add(expandLocaleName(fileValue));
262         LOCALES_INITIALIZED = true;
263     }
264     
265     private static String JavaDoc[] getLocales() {
266         Locale JavaDoc[] locales = Locale.getAvailableLocales();
267         String JavaDoc[] result = new String JavaDoc[locales.length];
268         for (int i = 0; i < locales.length; i++) {
269             Locale JavaDoc locale = locales[i];
270             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
271             buffer.append(locale.toString());
272             buffer.append(" - "); //$NON-NLS-1$
273
buffer.append(locale.getDisplayName());
274             result[i] = buffer.toString();
275         }
276         return result;
277     }
278     
279     private String JavaDoc expandLocaleName(String JavaDoc name) {
280         String JavaDoc language = ""; //$NON-NLS-1$
281
String JavaDoc country = ""; //$NON-NLS-1$
282
String JavaDoc variant = ""; //$NON-NLS-1$
283

284         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(name, "_"); //$NON-NLS-1$
285
if (tokenizer.hasMoreTokens())
286             language = tokenizer.nextToken();
287         if (tokenizer.hasMoreTokens())
288             country = tokenizer.nextToken();
289         if (tokenizer.hasMoreTokens())
290             variant = tokenizer.nextToken();
291             
292         Locale JavaDoc locale = new Locale JavaDoc(language, country, variant);
293         return locale.toString() + " - " + locale.getDisplayName(); //$NON-NLS-1$
294
}
295     
296     private String JavaDoc getText(ComboPart combo) {
297         Control control = combo.getControl();
298         if (control instanceof Combo)
299             return ((Combo) control).getText();
300         return ((CCombo) control).getText();
301     }
302     
303     private IEnvironmentInfo getEnvironment() {
304         IEnvironmentInfo info = getTarget().getEnvironment();
305         if (info == null) {
306             info = getModel().getFactory().createEnvironment();
307             getTarget().setEnvironment(info);
308         }
309         return info;
310     }
311     
312     private ITarget getTarget() {
313         return getModel().getTarget();
314     }
315     
316     private ITargetModel getModel() {
317         return (ITargetModel)getPage().getPDEEditor().getAggregateModel();
318     }
319     
320     public void refresh() {
321         IEnvironmentInfo orgEnv = getEnvironment();
322         String JavaDoc presetValue = (orgEnv.getOS() == null) ? "" : orgEnv.getOS(); //$NON-NLS-1$
323
fOSCombo.setText(presetValue);
324         presetValue = (orgEnv.getWS() == null) ? "" : orgEnv.getWS(); //$NON-NLS-1$
325
fWSCombo.setText(presetValue);
326         presetValue = (orgEnv.getArch() == null) ? "" : orgEnv.getArch(); //$NON-NLS-1$
327
fArchCombo.setText(presetValue);
328         presetValue = (orgEnv.getNL() == null) ? "" : expandLocaleName(orgEnv.getNL()); //$NON-NLS-1$
329
fNLCombo.setText(presetValue);
330         
331         super.refresh();
332     }
333     
334     protected void updateChoices() {
335         if (LOCALES_INITIALIZED)
336             return;
337         // prevent NPE Mike found, which we can't reproduce. Somehow we call initializeAllLocales before the ITargetModel exists.
338
if (getModel() == null)
339             return;
340         // kick off thread in backgroud to find the NL values
341
new Thread JavaDoc(new Runnable JavaDoc() {
342             public void run() {
343                 initializeAllLocales();
344             }
345         }).start();
346     }
347
348 }
349
Popular Tags