001: /*******************************************************************************
002: * Copyright (c) 2005, 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.editor.target;
011:
012: import java.util.TreeSet;
013:
014: import org.eclipse.jdt.launching.JavaRuntime;
015: import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
016: import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
017: import org.eclipse.pde.core.IModelChangedEvent;
018: import org.eclipse.pde.internal.core.itarget.ITarget;
019: import org.eclipse.pde.internal.core.itarget.ITargetJRE;
020: import org.eclipse.pde.internal.core.itarget.ITargetModel;
021: import org.eclipse.pde.internal.ui.PDEUIMessages;
022: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
023: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
024: import org.eclipse.pde.internal.ui.editor.PDESection;
025: import org.eclipse.pde.internal.ui.launcher.VMHelper;
026: import org.eclipse.pde.internal.ui.parts.ComboPart;
027: import org.eclipse.swt.SWT;
028: import org.eclipse.swt.events.ModifyEvent;
029: import org.eclipse.swt.events.ModifyListener;
030: import org.eclipse.swt.events.SelectionAdapter;
031: import org.eclipse.swt.events.SelectionEvent;
032: import org.eclipse.swt.layout.GridData;
033: import org.eclipse.swt.widgets.Button;
034: import org.eclipse.swt.widgets.Composite;
035: import org.eclipse.ui.dialogs.PreferencesUtil;
036: import org.eclipse.ui.forms.widgets.FormToolkit;
037: import org.eclipse.ui.forms.widgets.Section;
038:
039: public class JRESection extends PDESection {
040:
041: private Button fDefaultJREButton;
042: private Button fNamedJREButton;
043: private Button fExecEnvButton;
044: private ComboPart fNamedJREsCombo;
045: private ComboPart fExecEnvsCombo;
046: private TreeSet fExecEnvChoices;
047: private boolean fBlockChanges;
048: private Button fConfigureJREButton;
049:
050: private static String JRE_PREF_PAGE_ID = "org.eclipse.jdt.debug.ui.preferences.VMPreferencePage"; //$NON-NLS-1$
051: private static String EE_PREF_PAGE_ID = "org.eclipse.jdt.debug.ui.jreProfiles"; //$NON-NLS-1$
052:
053: public JRESection(PDEFormPage page, Composite parent) {
054: super (page, parent, Section.DESCRIPTION);
055: createClient(getSection(), page.getEditor().getToolkit());
056: }
057:
058: protected void createClient(Section section, FormToolkit toolkit) {
059: section.setText(PDEUIMessages.JRESection_title);
060: section.setDescription(PDEUIMessages.JRESection_description);
061: section.setLayout(FormLayoutFactory.createClearGridLayout(
062: false, 1));
063: GridData data = new GridData(GridData.FILL_HORIZONTAL);
064: data.verticalAlignment = SWT.TOP;
065: data.horizontalSpan = 2;
066: section.setLayoutData(data);
067:
068: Composite client = toolkit.createComposite(section);
069: client.setLayout(FormLayoutFactory
070: .createSectionClientGridLayout(false, 3));
071:
072: initializeValues();
073:
074: fDefaultJREButton = toolkit.createButton(client,
075: PDEUIMessages.JRESection_defaultJRE, SWT.RADIO);
076: GridData gd = new GridData();
077: gd.horizontalSpan = 3;
078: fDefaultJREButton.setLayoutData(gd);
079: fDefaultJREButton.addSelectionListener(new SelectionAdapter() {
080: public void widgetSelected(SelectionEvent e) {
081: updateWidgets();
082: if (!fBlockChanges)
083: getRuntimeInfo().setDefaultJRE();
084: }
085: });
086:
087: fNamedJREButton = toolkit.createButton(client,
088: PDEUIMessages.JRESection_JREName, SWT.RADIO);
089: fNamedJREButton.addSelectionListener(new SelectionAdapter() {
090: public void widgetSelected(SelectionEvent e) {
091: updateWidgets();
092: if (!fBlockChanges)
093: getRuntimeInfo().setNamedJRE(
094: fNamedJREsCombo.getSelection());
095: }
096: });
097:
098: fNamedJREsCombo = new ComboPart();
099: fNamedJREsCombo.createControl(client, toolkit, SWT.SINGLE
100: | SWT.BORDER);
101: fNamedJREsCombo.getControl().setLayoutData(
102: new GridData(GridData.FILL_HORIZONTAL));
103: String[] installs = VMHelper.getVMInstallNames();
104: fNamedJREsCombo.setItems(installs);
105: fNamedJREsCombo.addModifyListener(new ModifyListener() {
106: public void modifyText(ModifyEvent e) {
107: if (!fBlockChanges)
108: getRuntimeInfo().setNamedJRE(
109: fNamedJREsCombo.getSelection());
110: }
111: });
112:
113: fConfigureJREButton = toolkit.createButton(client,
114: PDEUIMessages.JRESection_jrePreference, SWT.PUSH);
115: fConfigureJREButton
116: .addSelectionListener(new SelectionAdapter() {
117: public void widgetSelected(SelectionEvent e) {
118: openPreferencePage(JRE_PREF_PAGE_ID);
119: }
120: });
121:
122: fExecEnvButton = toolkit.createButton(client,
123: PDEUIMessages.JRESection_ExecutionEnv, SWT.RADIO);
124: fExecEnvButton.addSelectionListener(new SelectionAdapter() {
125: public void widgetSelected(SelectionEvent e) {
126: updateWidgets();
127: if (!fBlockChanges)
128: getRuntimeInfo().setExecutionEnvJRE(
129: fExecEnvsCombo.getSelection());
130: }
131: });
132:
133: fExecEnvsCombo = new ComboPart();
134: fExecEnvsCombo.createControl(client, toolkit, SWT.SINGLE
135: | SWT.BORDER);
136: fExecEnvsCombo.getControl().setLayoutData(
137: new GridData(GridData.FILL_HORIZONTAL));
138: fExecEnvsCombo.setItems((String[]) fExecEnvChoices
139: .toArray(new String[fExecEnvChoices.size()]));
140: fExecEnvsCombo.addModifyListener(new ModifyListener() {
141: public void modifyText(ModifyEvent e) {
142: if (!fBlockChanges)
143: getRuntimeInfo().setExecutionEnvJRE(
144: fExecEnvsCombo.getSelection());
145: }
146: });
147:
148: Button configureEEButton = toolkit.createButton(client,
149: PDEUIMessages.JRESection_eePreference, SWT.PUSH);
150: configureEEButton.addSelectionListener(new SelectionAdapter() {
151: public void widgetSelected(SelectionEvent e) {
152: openPreferencePage(EE_PREF_PAGE_ID);
153: }
154: });
155: configureEEButton.setLayoutData(new GridData(
156: GridData.HORIZONTAL_ALIGN_FILL));
157:
158: section.setClient(client);
159:
160: // Register to be notified when the model changes
161: getModel().addModelChangedListener(this );
162: }
163:
164: /* (non-Javadoc)
165: * @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
166: */
167: public void modelChanged(IModelChangedEvent e) {
168: // No need to call super, handling world changed event here
169: if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
170: handleModelEventWorldChanged(e);
171: }
172: }
173:
174: /**
175: * @param event
176: */
177: private void handleModelEventWorldChanged(IModelChangedEvent event) {
178: // Perform the refresh
179: refresh();
180: // Note: A deferred selection event is fired from radio buttons when
181: // their value is toggled, the user switches to another page, and the
182: // user switches back to the same page containing the radio buttons
183: // This appears to be a result of a SWT bug.
184: // If the radio button is the last widget to have focus when leaving
185: // the page, an event will be fired when entering the page again.
186: // An event is not fired if the radio button does not have focus.
187: // The solution is to redirect focus to a stable widget.
188: getPage().setLastFocusControl(fConfigureJREButton);
189: }
190:
191: /* (non-Javadoc)
192: * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
193: */
194: public void dispose() {
195: ITargetModel model = getModel();
196: if (model != null) {
197: model.removeModelChangedListener(this );
198: }
199: super .dispose();
200: }
201:
202: protected void initializeValues() {
203: fExecEnvChoices = new TreeSet();
204: IExecutionEnvironmentsManager manager = JavaRuntime
205: .getExecutionEnvironmentsManager();
206: IExecutionEnvironment[] envs = manager
207: .getExecutionEnvironments();
208: for (int i = 0; i < envs.length; i++)
209: fExecEnvChoices.add(envs[i].getId());
210: }
211:
212: protected void updateWidgets() {
213: fNamedJREsCombo.setEnabled(fNamedJREButton.getSelection());
214: fExecEnvsCombo.setEnabled(fExecEnvButton.getSelection());
215: }
216:
217: private ITargetJRE getRuntimeInfo() {
218: ITargetJRE info = getTarget().getTargetJREInfo();
219: if (info == null) {
220: info = getModel().getFactory().createJREInfo();
221: getTarget().setTargetJREInfo(info);
222: }
223: return info;
224: }
225:
226: private ITarget getTarget() {
227: return getModel().getTarget();
228: }
229:
230: private ITargetModel getModel() {
231: return (ITargetModel) getPage().getPDEEditor()
232: .getAggregateModel();
233: }
234:
235: public void refresh() {
236: fBlockChanges = true;
237: ITargetJRE info = getRuntimeInfo();
238:
239: int jreType = info.getJREType();
240: fDefaultJREButton
241: .setSelection(jreType == ITargetJRE.TYPE_DEFAULT);
242: fNamedJREButton.setSelection(jreType == ITargetJRE.TYPE_NAMED);
243: fExecEnvButton
244: .setSelection(jreType == ITargetJRE.TYPE_EXECUTION_ENV);
245:
246: String jreName = info.getJREName();
247: if (jreType == ITargetJRE.TYPE_NAMED) {
248: if (fNamedJREsCombo.indexOf(jreName) < 0)
249: fNamedJREsCombo.add(jreName);
250: fNamedJREsCombo.setText(jreName);
251: } else if (jreType == ITargetJRE.TYPE_EXECUTION_ENV) {
252: if (fExecEnvsCombo.indexOf(jreName) < 0)
253: fExecEnvsCombo.add(jreName);
254: fExecEnvsCombo.setText(jreName);
255: }
256:
257: if (fExecEnvsCombo.getSelectionIndex() == -1)
258: fExecEnvsCombo.setText(fExecEnvChoices.first().toString());
259:
260: if (fNamedJREsCombo.getSelectionIndex() == -1)
261: fNamedJREsCombo.setText(VMHelper.getDefaultVMInstallName());
262:
263: updateWidgets();
264: super .refresh();
265: fBlockChanges = false;
266: }
267:
268: private void openPreferencePage(String pageID) {
269: fBlockChanges = true;
270: PreferencesUtil.createPreferenceDialogOn(
271: getPage().getEditor().getEditorSite().getShell(),
272: pageID, new String[] { pageID }, null).open();
273: // reset JRE select because either JDT preference page allows user to add/remove JREs
274: fNamedJREsCombo.setItems(VMHelper.getVMInstallNames());
275: fBlockChanges = false;
276: }
277:
278: }
|