KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.TreeSet JavaDoc;
14
15 import org.eclipse.jdt.launching.JavaRuntime;
16 import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
17 import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
18 import org.eclipse.pde.core.IModelChangedEvent;
19 import org.eclipse.pde.internal.core.itarget.ITarget;
20 import org.eclipse.pde.internal.core.itarget.ITargetJRE;
21 import org.eclipse.pde.internal.core.itarget.ITargetModel;
22 import org.eclipse.pde.internal.ui.PDEUIMessages;
23 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
24 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
25 import org.eclipse.pde.internal.ui.editor.PDESection;
26 import org.eclipse.pde.internal.ui.launcher.VMHelper;
27 import org.eclipse.pde.internal.ui.parts.ComboPart;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.ModifyEvent;
30 import org.eclipse.swt.events.ModifyListener;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.ui.dialogs.PreferencesUtil;
37 import org.eclipse.ui.forms.widgets.FormToolkit;
38 import org.eclipse.ui.forms.widgets.Section;
39
40 public class JRESection extends PDESection {
41     
42     private Button fDefaultJREButton;
43     private Button fNamedJREButton;
44     private Button fExecEnvButton;
45     private ComboPart fNamedJREsCombo;
46     private ComboPart fExecEnvsCombo;
47     private TreeSet JavaDoc fExecEnvChoices;
48     private boolean fBlockChanges;
49     private Button fConfigureJREButton;
50     
51     private static String JavaDoc JRE_PREF_PAGE_ID = "org.eclipse.jdt.debug.ui.preferences.VMPreferencePage"; //$NON-NLS-1$
52
private static String JavaDoc EE_PREF_PAGE_ID = "org.eclipse.jdt.debug.ui.jreProfiles"; //$NON-NLS-1$
53

54     public JRESection(PDEFormPage page, Composite parent) {
55         super(page, parent, Section.DESCRIPTION);
56         createClient(getSection(), page.getEditor().getToolkit());
57     }
58
59     protected void createClient(Section section, FormToolkit toolkit) {
60         section.setText(PDEUIMessages.JRESection_title);
61         section.setDescription(PDEUIMessages.JRESection_description);
62         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
63         GridData data = new GridData(GridData.FILL_HORIZONTAL);
64         data.verticalAlignment = SWT.TOP;
65         data.horizontalSpan = 2;
66         section.setLayoutData(data);
67
68         Composite client = toolkit.createComposite(section);
69         client.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 3));
70         
71         initializeValues();
72         
73         fDefaultJREButton = toolkit.createButton(client, PDEUIMessages.JRESection_defaultJRE, SWT.RADIO);
74         GridData gd = new GridData();
75         gd.horizontalSpan = 3;
76         fDefaultJREButton.setLayoutData(gd);
77         fDefaultJREButton.addSelectionListener(new SelectionAdapter() {
78             public void widgetSelected(SelectionEvent e) {
79                 updateWidgets();
80                 if (!fBlockChanges)
81                     getRuntimeInfo().setDefaultJRE();
82             }
83         });
84         
85         fNamedJREButton = toolkit.createButton(client, PDEUIMessages.JRESection_JREName, SWT.RADIO);
86         fNamedJREButton.addSelectionListener(new SelectionAdapter() {
87             public void widgetSelected(SelectionEvent e) {
88                 updateWidgets();
89                 if (!fBlockChanges)
90                     getRuntimeInfo().setNamedJRE(fNamedJREsCombo.getSelection());
91             }
92         });
93         
94         fNamedJREsCombo = new ComboPart();
95         fNamedJREsCombo.createControl(client, toolkit, SWT.SINGLE | SWT.BORDER);
96         fNamedJREsCombo.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
97         String JavaDoc[] installs = VMHelper.getVMInstallNames();
98         fNamedJREsCombo.setItems(installs);
99         fNamedJREsCombo.addModifyListener(new ModifyListener() {
100             public void modifyText(ModifyEvent e) {
101                 if (!fBlockChanges)
102                     getRuntimeInfo().setNamedJRE(fNamedJREsCombo.getSelection());
103             }
104         });
105         
106         fConfigureJREButton = toolkit.createButton(client, PDEUIMessages.JRESection_jrePreference, SWT.PUSH);
107         fConfigureJREButton.addSelectionListener(new SelectionAdapter() {
108             public void widgetSelected(SelectionEvent e) {
109                 openPreferencePage(JRE_PREF_PAGE_ID);
110             }
111         });
112         
113         fExecEnvButton = toolkit.createButton(client, PDEUIMessages.JRESection_ExecutionEnv, SWT.RADIO);
114         fExecEnvButton.addSelectionListener(new SelectionAdapter() {
115             public void widgetSelected(SelectionEvent e) {
116                 updateWidgets();
117                 if (!fBlockChanges)
118                     getRuntimeInfo().setExecutionEnvJRE(fExecEnvsCombo.getSelection());
119             }
120         });
121         
122         fExecEnvsCombo = new ComboPart();
123         fExecEnvsCombo.createControl(client, toolkit, SWT.SINGLE | SWT.BORDER );
124         fExecEnvsCombo.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
125         fExecEnvsCombo.setItems((String JavaDoc[])fExecEnvChoices.toArray(new String JavaDoc[fExecEnvChoices.size()]));
126         fExecEnvsCombo.addModifyListener(new ModifyListener() {
127             public void modifyText(ModifyEvent e) {
128                 if (!fBlockChanges)
129                     getRuntimeInfo().setExecutionEnvJRE(fExecEnvsCombo.getSelection());
130             }
131         });
132         
133         Button configureEEButton = toolkit.createButton(client, PDEUIMessages.JRESection_eePreference, SWT.PUSH);
134         configureEEButton.addSelectionListener(new SelectionAdapter() {
135             public void widgetSelected(SelectionEvent e) {
136                 openPreferencePage(EE_PREF_PAGE_ID);
137             }
138         });
139         configureEEButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
140
141         section.setClient(client);
142         
143         // Register to be notified when the model changes
144
getModel().addModelChangedListener(this);
145     }
146     
147     /* (non-Javadoc)
148      * @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
149      */

150     public void modelChanged(IModelChangedEvent e) {
151         // No need to call super, handling world changed event here
152
if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
153             handleModelEventWorldChanged(e);
154         }
155     }
156     
157     /**
158      * @param event
159      */

160     private void handleModelEventWorldChanged(IModelChangedEvent event) {
161         // Perform the refresh
162
refresh();
163         // Note: A deferred selection event is fired from radio buttons when
164
// their value is toggled, the user switches to another page, and the
165
// user switches back to the same page containing the radio buttons
166
// This appears to be a result of a SWT bug.
167
// If the radio button is the last widget to have focus when leaving
168
// the page, an event will be fired when entering the page again.
169
// An event is not fired if the radio button does not have focus.
170
// The solution is to redirect focus to a stable widget.
171
getPage().setLastFocusControl(fConfigureJREButton);
172     }
173
174     /* (non-Javadoc)
175      * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
176      */

177     public void dispose() {
178         ITargetModel model = getModel();
179         if (model != null) {
180             model.removeModelChangedListener(this);
181         }
182         super.dispose();
183     }
184     
185     protected void initializeValues() {
186         fExecEnvChoices = new TreeSet JavaDoc();
187         IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager();
188         IExecutionEnvironment[] envs = manager.getExecutionEnvironments();
189         for (int i = 0; i < envs.length; i++)
190             fExecEnvChoices.add(envs[i].getId());
191     }
192     
193     protected void updateWidgets() {
194         fNamedJREsCombo.setEnabled(fNamedJREButton.getSelection());
195         fExecEnvsCombo.setEnabled(fExecEnvButton.getSelection());
196     }
197     
198     private ITargetJRE getRuntimeInfo() {
199         ITargetJRE info = getTarget().getTargetJREInfo();
200         if (info == null) {
201             info = getModel().getFactory().createJREInfo();
202             getTarget().setTargetJREInfo(info);
203         }
204         return info;
205     }
206     
207     private ITarget getTarget() {
208         return getModel().getTarget();
209     }
210     
211     private ITargetModel getModel() {
212         return (ITargetModel)getPage().getPDEEditor().getAggregateModel();
213     }
214     
215     public void refresh() {
216         fBlockChanges = true;
217         ITargetJRE info = getRuntimeInfo();
218
219         int jreType = info.getJREType();
220         fDefaultJREButton.setSelection(jreType == ITargetJRE.TYPE_DEFAULT);
221         fNamedJREButton.setSelection(jreType == ITargetJRE.TYPE_NAMED);
222         fExecEnvButton.setSelection(jreType == ITargetJRE.TYPE_EXECUTION_ENV);
223         
224         String JavaDoc jreName = info.getJREName();
225         if (jreType == ITargetJRE.TYPE_NAMED) {
226             if (fNamedJREsCombo.indexOf(jreName) < 0)
227                 fNamedJREsCombo.add(jreName);
228             fNamedJREsCombo.setText(jreName);
229         } else if (jreType == ITargetJRE.TYPE_EXECUTION_ENV) {
230             if (fExecEnvsCombo.indexOf(jreName) < 0)
231                 fExecEnvsCombo.add(jreName);
232             fExecEnvsCombo.setText(jreName);
233         }
234         
235         if (fExecEnvsCombo.getSelectionIndex() == -1)
236             fExecEnvsCombo.setText(fExecEnvChoices.first().toString());
237         
238         if (fNamedJREsCombo.getSelectionIndex() == -1)
239             fNamedJREsCombo.setText(VMHelper.getDefaultVMInstallName());
240         
241         updateWidgets();
242         super.refresh();
243         fBlockChanges = false;
244     }
245     
246     private void openPreferencePage(String JavaDoc pageID) {
247         fBlockChanges = true;
248         PreferencesUtil.createPreferenceDialogOn(getPage().getEditor().getEditorSite().getShell(), pageID, new String JavaDoc[] { pageID }, null).open();
249         // reset JRE select because either JDT preference page allows user to add/remove JREs
250
fNamedJREsCombo.setItems(VMHelper.getVMInstallNames());
251         fBlockChanges = false;
252     }
253     
254 }
255
Popular Tags