KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > launcher > ProgramBlock


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.launcher;
12
13 import java.util.StringTokenizer JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.debug.core.ILaunchConfiguration;
17 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
18 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
19 import org.eclipse.pde.core.plugin.TargetPlatform;
20 import org.eclipse.pde.internal.ui.PDEUIMessages;
21 import org.eclipse.pde.ui.launcher.AbstractLauncherTab;
22 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Combo;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Group;
32
33 public class ProgramBlock {
34
35     protected Combo fApplicationCombo;
36     private Button fProductButton;
37     private Combo fProductCombo;
38     private Button fApplicationButton;
39     private AbstractLauncherTab fTab;
40     private Listener fListener = new Listener();
41
42     class Listener extends SelectionAdapter {
43         public void widgetSelected(SelectionEvent e) {
44             Object JavaDoc source = e.getSource();
45             if (source == fProductButton) {
46                 boolean enabled = fProductButton.getSelection();
47                 fProductCombo.setEnabled(enabled);
48                 fApplicationCombo.setEnabled(!enabled);
49             }
50             fTab.updateLaunchConfigurationDialog();
51         }
52     }
53     
54     public ProgramBlock(AbstractLauncherTab tab) {
55         fTab = tab;
56     }
57     
58     public void createControl(Composite parent) {
59         Group group = new Group(parent, SWT.NONE);
60         group.setText(PDEUIMessages.ProgramBlock_programToRun);
61         GridLayout layout = new GridLayout();
62         layout.numColumns = 2;
63         group.setLayout(layout);
64         group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
65                 
66         createProductSection(group);
67         createApplicationSection(group);
68     }
69     
70     protected void createProductSection(Composite parent) {
71         fProductButton = new Button(parent, SWT.RADIO);
72         fProductButton.setText(PDEUIMessages.ProgramBlock_runProduct);
73         fProductButton.addSelectionListener(fListener);
74         
75         fProductCombo = new Combo(parent, SWT.READ_ONLY|SWT.DROP_DOWN);
76         fProductCombo.setItems(TargetPlatform.getProducts());
77         fProductCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
78         fProductCombo.addSelectionListener(fListener);
79     }
80     
81     protected void createApplicationSection(Composite parent) {
82         fApplicationButton = new Button(parent, SWT.RADIO);
83         fApplicationButton.setText(PDEUIMessages.ProgramBlock_runApplication);
84             
85         fApplicationCombo = new Combo(parent, SWT.READ_ONLY|SWT.DROP_DOWN);
86         fApplicationCombo.setItems(getApplicationNames());
87         fApplicationCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
88         fApplicationCombo.addSelectionListener(fListener);
89     }
90         
91     public void initializeFrom(ILaunchConfiguration config) throws CoreException {
92         initializeProductSection(config);
93         initializeApplicationSection(config);
94         
95         boolean useProduct =
96                     config.getAttribute(IPDELauncherConstants.USE_PRODUCT, false)
97                     && fProductCombo.getItemCount() > 0;
98         fApplicationButton.setSelection(!useProduct);
99         fApplicationCombo.setEnabled(!useProduct);
100         fProductButton.setSelection(useProduct);
101         fProductButton.setEnabled(fProductCombo.getItemCount() > 0);
102         fProductCombo.setEnabled(useProduct);
103     }
104     
105     protected void initializeProductSection(ILaunchConfiguration config) throws CoreException {
106         if (fProductCombo.getItemCount() > 0) {
107             String JavaDoc productName = config.getAttribute(IPDELauncherConstants.PRODUCT, (String JavaDoc)null);
108             int index = productName == null ? -1 : fProductCombo.indexOf(productName);
109             if (index == -1)
110                 index = 0;
111             fProductCombo.setText(fProductCombo.getItem(index));
112         }
113     }
114
115     protected void initializeApplicationSection(ILaunchConfiguration config) throws CoreException {
116         
117         String JavaDoc attribute = getApplicationAttribute();
118         
119         // first see if the application name has been set on the launch config
120
String JavaDoc application = config.getAttribute(attribute, (String JavaDoc) null);
121         if (application == null
122             || fApplicationCombo.indexOf(application) == -1) {
123             application = null;
124             
125             // check if the user has entered the -application arg in the program arg field
126
StringTokenizer JavaDoc tokenizer =
127                 new StringTokenizer JavaDoc(config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, "")); //$NON-NLS-1$
128
while (tokenizer.hasMoreTokens()) {
129                 String JavaDoc token = tokenizer.nextToken();
130                 if (token.equals("-application") && tokenizer.hasMoreTokens()) { //$NON-NLS-1$
131
application = tokenizer.nextToken();
132                     break;
133                 }
134             }
135             
136             int index = -1;
137             if (application != null)
138                 index = fApplicationCombo.indexOf(application);
139             
140             // use default application as specified in the install.ini of the target platform
141
if (index == -1)
142                 index = fApplicationCombo.indexOf(TargetPlatform.getDefaultApplication());
143             
144             if (index != -1) {
145                 fApplicationCombo.setText(fApplicationCombo.getItem(index));
146             } else if (fApplicationCombo.getItemCount() > 0) {
147                 fApplicationCombo.setText(fApplicationCombo.getItem(0));
148             }
149         } else {
150             fApplicationCombo.setText(application);
151         }
152     }
153     
154     public void performApply(ILaunchConfigurationWorkingCopy config) {
155         saveApplicationSection(config);
156         saveProductSection(config);
157     }
158     
159     protected void saveProductSection(ILaunchConfigurationWorkingCopy config) {
160         config.setAttribute(IPDELauncherConstants.USE_PRODUCT, fProductButton.getSelection());
161         config.setAttribute(IPDELauncherConstants.PRODUCT, fProductCombo.getText());
162     }
163
164     protected void saveApplicationSection(ILaunchConfigurationWorkingCopy config) {
165         String JavaDoc text = fApplicationCombo.getText();
166         String JavaDoc attribute = getApplicationAttribute();
167         if (text.length() == 0 || text.equals(TargetPlatform.getDefaultApplication()))
168             config.setAttribute(attribute, (String JavaDoc) null);
169         else
170             config.setAttribute(attribute, text);
171     }
172     
173     public void setDefaults(ILaunchConfigurationWorkingCopy config) {
174         String JavaDoc product = TargetPlatform.getDefaultProduct();
175         if (product != null) {
176             config.setAttribute(IPDELauncherConstants.USE_PRODUCT, true);
177             config.setAttribute(IPDELauncherConstants.PRODUCT, product);
178         }
179     }
180     
181     protected String JavaDoc[] getApplicationNames() {
182         return TargetPlatform.getApplications();
183     }
184     
185     protected String JavaDoc getApplicationAttribute() {
186         return IPDELauncherConstants.APPLICATION;
187     }
188         
189 }
190
Popular Tags