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.StringTokenizer;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.debug.core.ILaunchConfiguration;
016: import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
017: import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
018: import org.eclipse.pde.core.plugin.TargetPlatform;
019: import org.eclipse.pde.internal.ui.PDEUIMessages;
020: import org.eclipse.pde.ui.launcher.AbstractLauncherTab;
021: import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
022: import org.eclipse.swt.SWT;
023: import org.eclipse.swt.events.SelectionAdapter;
024: import org.eclipse.swt.events.SelectionEvent;
025: import org.eclipse.swt.layout.GridData;
026: import org.eclipse.swt.layout.GridLayout;
027: import org.eclipse.swt.widgets.Button;
028: import org.eclipse.swt.widgets.Combo;
029: import org.eclipse.swt.widgets.Composite;
030: import org.eclipse.swt.widgets.Group;
031:
032: public class ProgramBlock {
033:
034: protected Combo fApplicationCombo;
035: private Button fProductButton;
036: private Combo fProductCombo;
037: private Button fApplicationButton;
038: private AbstractLauncherTab fTab;
039: private Listener fListener = new Listener();
040:
041: class Listener extends SelectionAdapter {
042: public void widgetSelected(SelectionEvent e) {
043: Object source = e.getSource();
044: if (source == fProductButton) {
045: boolean enabled = fProductButton.getSelection();
046: fProductCombo.setEnabled(enabled);
047: fApplicationCombo.setEnabled(!enabled);
048: }
049: fTab.updateLaunchConfigurationDialog();
050: }
051: }
052:
053: public ProgramBlock(AbstractLauncherTab tab) {
054: fTab = tab;
055: }
056:
057: public void createControl(Composite parent) {
058: Group group = new Group(parent, SWT.NONE);
059: group.setText(PDEUIMessages.ProgramBlock_programToRun);
060: GridLayout layout = new GridLayout();
061: layout.numColumns = 2;
062: group.setLayout(layout);
063: group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
064:
065: createProductSection(group);
066: createApplicationSection(group);
067: }
068:
069: protected void createProductSection(Composite parent) {
070: fProductButton = new Button(parent, SWT.RADIO);
071: fProductButton.setText(PDEUIMessages.ProgramBlock_runProduct);
072: fProductButton.addSelectionListener(fListener);
073:
074: fProductCombo = new Combo(parent, SWT.READ_ONLY | SWT.DROP_DOWN);
075: fProductCombo.setItems(TargetPlatform.getProducts());
076: fProductCombo.setLayoutData(new GridData(
077: GridData.FILL_HORIZONTAL));
078: fProductCombo.addSelectionListener(fListener);
079: }
080:
081: protected void createApplicationSection(Composite parent) {
082: fApplicationButton = new Button(parent, SWT.RADIO);
083: fApplicationButton
084: .setText(PDEUIMessages.ProgramBlock_runApplication);
085:
086: fApplicationCombo = new Combo(parent, SWT.READ_ONLY
087: | SWT.DROP_DOWN);
088: fApplicationCombo.setItems(getApplicationNames());
089: fApplicationCombo.setLayoutData(new GridData(
090: GridData.FILL_HORIZONTAL));
091: fApplicationCombo.addSelectionListener(fListener);
092: }
093:
094: public void initializeFrom(ILaunchConfiguration config)
095: throws CoreException {
096: initializeProductSection(config);
097: initializeApplicationSection(config);
098:
099: boolean useProduct = config.getAttribute(
100: IPDELauncherConstants.USE_PRODUCT, false)
101: && fProductCombo.getItemCount() > 0;
102: fApplicationButton.setSelection(!useProduct);
103: fApplicationCombo.setEnabled(!useProduct);
104: fProductButton.setSelection(useProduct);
105: fProductButton.setEnabled(fProductCombo.getItemCount() > 0);
106: fProductCombo.setEnabled(useProduct);
107: }
108:
109: protected void initializeProductSection(ILaunchConfiguration config)
110: throws CoreException {
111: if (fProductCombo.getItemCount() > 0) {
112: String productName = config.getAttribute(
113: IPDELauncherConstants.PRODUCT, (String) null);
114: int index = productName == null ? -1 : fProductCombo
115: .indexOf(productName);
116: if (index == -1)
117: index = 0;
118: fProductCombo.setText(fProductCombo.getItem(index));
119: }
120: }
121:
122: protected void initializeApplicationSection(
123: ILaunchConfiguration config) throws CoreException {
124:
125: String attribute = getApplicationAttribute();
126:
127: // first see if the application name has been set on the launch config
128: String application = config.getAttribute(attribute,
129: (String) null);
130: if (application == null
131: || fApplicationCombo.indexOf(application) == -1) {
132: application = null;
133:
134: // check if the user has entered the -application arg in the program arg field
135: StringTokenizer tokenizer = new StringTokenizer(
136: config
137: .getAttribute(
138: IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
139: "")); //$NON-NLS-1$
140: while (tokenizer.hasMoreTokens()) {
141: String token = tokenizer.nextToken();
142: if (token.equals("-application") && tokenizer.hasMoreTokens()) { //$NON-NLS-1$
143: application = tokenizer.nextToken();
144: break;
145: }
146: }
147:
148: int index = -1;
149: if (application != null)
150: index = fApplicationCombo.indexOf(application);
151:
152: // use default application as specified in the install.ini of the target platform
153: if (index == -1)
154: index = fApplicationCombo.indexOf(TargetPlatform
155: .getDefaultApplication());
156:
157: if (index != -1) {
158: fApplicationCombo.setText(fApplicationCombo
159: .getItem(index));
160: } else if (fApplicationCombo.getItemCount() > 0) {
161: fApplicationCombo.setText(fApplicationCombo.getItem(0));
162: }
163: } else {
164: fApplicationCombo.setText(application);
165: }
166: }
167:
168: public void performApply(ILaunchConfigurationWorkingCopy config) {
169: saveApplicationSection(config);
170: saveProductSection(config);
171: }
172:
173: protected void saveProductSection(
174: ILaunchConfigurationWorkingCopy config) {
175: config.setAttribute(IPDELauncherConstants.USE_PRODUCT,
176: fProductButton.getSelection());
177: config.setAttribute(IPDELauncherConstants.PRODUCT,
178: fProductCombo.getText());
179: }
180:
181: protected void saveApplicationSection(
182: ILaunchConfigurationWorkingCopy config) {
183: String text = fApplicationCombo.getText();
184: String attribute = getApplicationAttribute();
185: if (text.length() == 0
186: || text.equals(TargetPlatform.getDefaultApplication()))
187: config.setAttribute(attribute, (String) null);
188: else
189: config.setAttribute(attribute, text);
190: }
191:
192: public void setDefaults(ILaunchConfigurationWorkingCopy config) {
193: String product = TargetPlatform.getDefaultProduct();
194: if (product != null) {
195: config
196: .setAttribute(IPDELauncherConstants.USE_PRODUCT,
197: true);
198: config.setAttribute(IPDELauncherConstants.PRODUCT, product);
199: }
200: }
201:
202: protected String[] getApplicationNames() {
203: return TargetPlatform.getApplications();
204: }
205:
206: protected String getApplicationAttribute() {
207: return IPDELauncherConstants.APPLICATION;
208: }
209:
210: }
|