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.launcher;
011:
012: import java.util.Arrays;
013: import java.util.Comparator;
014:
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.debug.core.ILaunchConfiguration;
017: import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
018: import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
019: import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
020: import org.eclipse.osgi.util.NLS;
021: import org.eclipse.pde.internal.ui.PDEUIMessages;
022: import org.eclipse.pde.internal.ui.preferences.PDEPreferencesUtil;
023: import org.eclipse.pde.internal.ui.util.SWTUtil;
024: import org.eclipse.pde.ui.launcher.AbstractLauncherTab;
025: import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
026: import org.eclipse.swt.SWT;
027: import org.eclipse.swt.events.ModifyEvent;
028: import org.eclipse.swt.events.ModifyListener;
029: import org.eclipse.swt.events.SelectionAdapter;
030: import org.eclipse.swt.events.SelectionEvent;
031: import org.eclipse.swt.layout.GridData;
032: import org.eclipse.swt.layout.GridLayout;
033: import org.eclipse.swt.widgets.Button;
034: import org.eclipse.swt.widgets.Combo;
035: import org.eclipse.swt.widgets.Composite;
036: import org.eclipse.swt.widgets.Group;
037: import org.eclipse.swt.widgets.Label;
038: import org.eclipse.swt.widgets.Text;
039:
040: public class JREBlock {
041:
042: private AbstractLauncherTab fTab;
043: private Listener fListener = new Listener();
044: private Button fJavawButton;
045: private Button fJavaButton;
046: private Button fJreButton;
047: private Button fEeButton;
048: private Button fJrePrefButton;
049: private Button fEePrefButton;
050: private Combo fJreCombo;
051: private Combo fEeCombo;
052: private Text fBootstrap;
053:
054: class Listener extends SelectionAdapter implements ModifyListener {
055: public void widgetSelected(SelectionEvent e) {
056: Object source = e.getSource();
057: // When a radio button selection changes, we get two events. One for the deselection of the old button and another for the selection
058: // of the new button. We only need to update the configuration once when the selection changes. Hence, we can ignore the deselection
059: // event of the old button.
060: if (source instanceof Button
061: && !((Button) source).getSelection())
062: return;
063: fTab.updateLaunchConfigurationDialog();
064: if (source == fJreButton || source == fEeButton)
065: updateJREEnablement();
066: }
067:
068: public void modifyText(ModifyEvent e) {
069: fTab.updateLaunchConfigurationDialog();
070: }
071: }
072:
073: public JREBlock(AbstractLauncherTab tab) {
074: fTab = tab;
075: }
076:
077: public void createControl(Composite parent) {
078: Group group = new Group(parent, SWT.NONE);
079: group.setText(PDEUIMessages.MainTab_jreSection);
080: GridLayout layout = new GridLayout();
081: layout.numColumns = 3;
082: group.setLayout(layout);
083: group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
084:
085: createJavaExecutableSection(group);
086: createJRESection(group);
087: createBootstrapEntriesSection(group);
088: }
089:
090: protected void createJRESection(Composite parent) {
091: fJreButton = new Button(parent, SWT.RADIO);
092: fJreButton.setText(PDEUIMessages.BasicLauncherTab_jre);
093: fJreButton.addSelectionListener(fListener);
094:
095: fJreCombo = new Combo(parent, SWT.DROP_DOWN | SWT.READ_ONLY);
096: fJreCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
097: false));
098: fJreCombo.addSelectionListener(fListener);
099:
100: fJrePrefButton = new Button(parent, SWT.PUSH);
101: fJrePrefButton
102: .setText(PDEUIMessages.BasicLauncherTab_installedJREs);
103: fJrePrefButton.addSelectionListener(new SelectionAdapter() {
104: public void widgetSelected(SelectionEvent e) {
105: String currentVM = fJreCombo.getText();
106: String currentEE = parseEESelection(fEeCombo.getText());
107: boolean useDefault = VMHelper.getDefaultVMInstallName()
108: .equals(currentVM);
109: String[] pageIDs = new String[] { "org.eclipse.jdt.debug.ui.preferences.VMPreferencePage" }; //$NON-NLS-1$
110: if (PDEPreferencesUtil.showPreferencePage(pageIDs, fTab
111: .getControl().getShell())) {
112: setJRECombo();
113: if (useDefault
114: || fJreCombo.indexOf(currentVM) == -1)
115: fJreCombo.setText(VMHelper
116: .getDefaultVMInstallName());
117: else
118: fJreCombo.setText(currentVM);
119: setEECombo();
120: setEEComboSelection(currentEE);
121: }
122: }
123: });
124: fJrePrefButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
125: false, false));
126: SWTUtil.setButtonDimensionHint(fJrePrefButton);
127:
128: fEeButton = new Button(parent, SWT.RADIO);
129: fEeButton.setText(PDEUIMessages.BasicLauncherTab_ee);
130: fEeButton.addSelectionListener(fListener);
131:
132: fEeCombo = new Combo(parent, SWT.DROP_DOWN | SWT.READ_ONLY);
133: fEeCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
134: false));
135: fEeCombo.addSelectionListener(fListener);
136:
137: fEePrefButton = new Button(parent, SWT.PUSH);
138: fEePrefButton
139: .setText(PDEUIMessages.BasicLauncherTab_environments);
140: fEePrefButton.addSelectionListener(new SelectionAdapter() {
141: public void widgetSelected(SelectionEvent e) {
142: String currentEE = parseEESelection(fEeCombo.getText());
143: String[] pageIDs = new String[] { "org.eclipse.jdt.debug.ui.jreProfiles" }; //$NON-NLS-1$
144: if (PDEPreferencesUtil.showPreferencePage(pageIDs, fTab
145: .getControl().getShell())) {
146: setEECombo();
147: setEEComboSelection(currentEE);
148: }
149: }
150: });
151: fEePrefButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
152: false, false));
153: SWTUtil.setButtonDimensionHint(fEePrefButton);
154: }
155:
156: protected void createJavaExecutableSection(Composite parent) {
157: Label label = new Label(parent, SWT.NONE);
158: label.setText(PDEUIMessages.BasicLauncherTab_javaExec);
159:
160: Composite composite = new Composite(parent, SWT.NONE);
161: GridLayout layout = new GridLayout();
162: layout.numColumns = 2;
163: layout.marginHeight = layout.marginWidth = 0;
164: layout.horizontalSpacing = 20;
165: composite.setLayout(layout);
166: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
167: gd.horizontalSpan = 2;
168: composite.setLayoutData(gd);
169:
170: fJavawButton = new Button(composite, SWT.RADIO);
171: fJavawButton
172: .setText(PDEUIMessages.BasicLauncherTab_javaExecDefault); //
173: fJavawButton.addSelectionListener(fListener);
174:
175: fJavaButton = new Button(composite, SWT.RADIO);
176: fJavaButton.setText("&java"); //$NON-NLS-1$
177: fJavaButton.addSelectionListener(fListener);
178: }
179:
180: private void createBootstrapEntriesSection(Composite parent) {
181: Label label = new Label(parent, SWT.NONE);
182: label.setText(PDEUIMessages.BasicLauncherTab_bootstrap);
183:
184: fBootstrap = new Text(parent, SWT.BORDER);
185: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
186: gd.widthHint = 300;
187: gd.horizontalSpan = 2;
188: fBootstrap.setLayoutData(gd);
189: fBootstrap.addModifyListener(fListener);
190: }
191:
192: public void initializeFrom(ILaunchConfiguration config)
193: throws CoreException {
194: initializeJRESection(config);
195: initializeBootstrapEntriesSection(config);
196: }
197:
198: private void initializeJRESection(ILaunchConfiguration config)
199: throws CoreException {
200: String javaCommand = config.getAttribute(
201: IJavaLaunchConfigurationConstants.ATTR_JAVA_COMMAND,
202: "javaw"); //$NON-NLS-1$
203: fJavawButton.setSelection(javaCommand.equals("javaw")); //$NON-NLS-1$
204: fJavaButton.setSelection(!fJavawButton.getSelection());
205:
206: boolean useVMInstall = config.getAttribute(
207: IPDELauncherConstants.USE_VMINSTALL, true); //$NON-NLS-1$
208: fJreButton.setSelection(useVMInstall); //$NON-NLS-1$
209: fEeButton.setSelection(!useVMInstall);
210:
211: setJRECombo();
212: String vmInstallName = config.getAttribute(
213: IPDELauncherConstants.VMINSTALL, VMHelper
214: .getDefaultVMInstallName());
215: fJreCombo.setText(vmInstallName);
216: if (fJreCombo.getSelectionIndex() == -1)
217: fJreCombo.setText(VMHelper.getDefaultVMInstallName());
218:
219: setEECombo();
220: String eeId = config.getAttribute(
221: IPDELauncherConstants.EXECUTION_ENVIRONMENT,
222: (String) null);
223: setEEComboSelection(eeId);
224:
225: updateJREEnablement();
226: }
227:
228: private void setEEComboSelection(String eeId) {
229: if (eeId != null) {
230: String[] items = fEeCombo.getItems();
231: for (int i = 0; i < items.length; i++) {
232: if (parseEESelection(items[i]).equals(eeId)) {
233: fEeCombo.select(i);
234: return;
235: }
236: }
237: }
238: if (fEeCombo.getItemCount() > 0
239: && fEeCombo.getSelectionIndex() == -1)
240: fEeCombo.select(0);
241: }
242:
243: private void updateJREEnablement() {
244: fJreCombo.setEnabled(fJreButton.getSelection());
245: fJrePrefButton.setEnabled(fJreButton.getSelection());
246: fEeCombo.setEnabled(fEeButton.getSelection());
247: fEePrefButton.setEnabled(fEeButton.getSelection());
248: }
249:
250: private void initializeBootstrapEntriesSection(
251: ILaunchConfiguration config) throws CoreException {
252: fBootstrap.setText(config.getAttribute(
253: IPDELauncherConstants.BOOTSTRAP_ENTRIES, "")); //$NON-NLS-1$
254: }
255:
256: public void performApply(ILaunchConfigurationWorkingCopy config) {
257: saveJRESection(config);
258: saveBootstrapEntriesSection(config);
259: }
260:
261: protected void saveJRESection(ILaunchConfigurationWorkingCopy config) {
262: try {
263: String javaCommand = fJavawButton.getSelection() ? null
264: : "java"; //$NON-NLS-1$
265: config
266: .setAttribute(
267: IJavaLaunchConfigurationConstants.ATTR_JAVA_COMMAND,
268: javaCommand);
269:
270: config.setAttribute(IPDELauncherConstants.USE_VMINSTALL,
271: fJreButton.getSelection());
272: if (fJreButton.getSelection()) {
273: if (fJreCombo.getSelectionIndex() == -1)
274: return;
275:
276: String jre = fJreCombo.getText();
277: if (config.getAttribute(
278: IPDELauncherConstants.VMINSTALL, (String) null) != null) {
279: config.setAttribute(
280: IPDELauncherConstants.VMINSTALL, jre);
281: } else {
282: config.setAttribute(
283: IPDELauncherConstants.VMINSTALL,
284: jre.equals(VMHelper
285: .getDefaultVMInstallName()) ? null
286: : jre);
287: }
288: } else {
289: if (fEeCombo.getSelectionIndex() == -1)
290: return;
291:
292: config.setAttribute(
293: IPDELauncherConstants.EXECUTION_ENVIRONMENT,
294: parseEESelection(fEeCombo.getText()));
295: }
296: } catch (CoreException e) {
297: }
298: }
299:
300: protected void saveBootstrapEntriesSection(
301: ILaunchConfigurationWorkingCopy config) {
302: config.setAttribute(IPDELauncherConstants.BOOTSTRAP_ENTRIES,
303: fBootstrap.getText().trim());
304: }
305:
306: public void setDefaults(ILaunchConfigurationWorkingCopy config) {
307: config
308: .setAttribute(IPDELauncherConstants.BOOTSTRAP_ENTRIES,
309: ""); //$NON-NLS-1$
310: }
311:
312: private void setJRECombo() {
313: String[] jres = VMHelper.getVMInstallNames();
314: Arrays.sort(jres, getComparator());
315: fJreCombo.setItems(jres);
316: }
317:
318: private void setEECombo() {
319: IExecutionEnvironment[] eeObjects = VMHelper
320: .getExecutionEnvironments();
321: String[] ees = new String[eeObjects.length];
322: for (int i = 0; i < eeObjects.length; i++) {
323: String vm;
324: try {
325: vm = VMHelper.getVMInstallName(eeObjects[i]);
326: } catch (CoreException e) {
327: vm = PDEUIMessages.BasicLauncherTab_unbound;
328: }
329: ees[i] = NLS
330: .bind(
331: "{0} ({1})", new String[] { eeObjects[i].getId(), vm }); //$NON-NLS-1$
332: }
333: Arrays.sort(ees, getComparator());
334: fEeCombo.setItems(ees);
335: }
336:
337: private Comparator getComparator() {
338: return new Comparator() {
339: public int compare(Object arg0, Object arg1) {
340: return arg0.toString().compareTo(arg1.toString());
341: }
342: };
343: }
344:
345: public String validate() {
346: if (fEeButton.getSelection()
347: && fEeCombo.getText().indexOf(
348: PDEUIMessages.BasicLauncherTab_unbound) != -1)
349: return NLS.bind(
350: PDEUIMessages.BasicLauncherTab_noJreForEeMessage,
351: parseEESelection(fEeCombo.getText()));
352: return null;
353: }
354:
355: private String parseEESelection(String selection) {
356: int index = selection.indexOf(" ("); //$NON-NLS-1$
357: if (index == -1)
358: return selection;
359: return selection.substring(0, index);
360: }
361: }
|