KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > product > ProductFileWizadPage


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.wizards.product;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.debug.core.DebugPlugin;
22 import org.eclipse.debug.core.ILaunchConfiguration;
23 import org.eclipse.debug.core.ILaunchConfigurationType;
24 import org.eclipse.debug.core.ILaunchManager;
25 import org.eclipse.debug.ui.DebugUITools;
26 import org.eclipse.jface.dialogs.Dialog;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.pde.core.plugin.IPluginExtension;
29 import org.eclipse.pde.core.plugin.IPluginModelBase;
30 import org.eclipse.pde.internal.core.PDECore;
31 import org.eclipse.pde.internal.core.TargetPlatform;
32 import org.eclipse.pde.internal.ui.IHelpContextIds;
33 import org.eclipse.pde.internal.ui.PDEPlugin;
34 import org.eclipse.pde.internal.ui.PDEUIMessages;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.events.SelectionAdapter;
37 import org.eclipse.swt.events.SelectionEvent;
38 import org.eclipse.swt.layout.GridData;
39 import org.eclipse.swt.layout.GridLayout;
40 import org.eclipse.swt.widgets.Button;
41 import org.eclipse.swt.widgets.Combo;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Group;
44 import org.eclipse.ui.PlatformUI;
45 import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
46
47 public class ProductFileWizadPage extends WizardNewFileCreationPage {
48     
49     public final static int USE_DEFAULT = 0;
50     public final static int USE_PRODUCT = 1;
51     public final static int USE_LAUNCH_CONFIG = 2;
52     
53     private Button fBasicButton;
54     private Button fProductButton;
55     private Combo fProductCombo;
56     private Button fLaunchConfigButton;
57     private Combo fLaunchConfigCombo;
58     private Group fGroup;
59     
60     private IPluginModelBase fModel;
61     
62     public ProductFileWizadPage(String JavaDoc pageName, IStructuredSelection selection) {
63         super(pageName, selection);
64         setDescription(PDEUIMessages.ProductFileWizadPage_title);
65         setTitle(PDEUIMessages.NewProductFileWizard_title);
66         initializeModel(selection);
67     }
68     
69     private void initializeModel(IStructuredSelection selection) {
70         Object JavaDoc selected = selection.getFirstElement();
71         if (selected instanceof IAdaptable) {
72             IResource resource = (IResource)((IAdaptable)selected).getAdapter(IResource.class);
73             if (resource != null) {
74                 IProject project = resource.getProject();
75                 fModel = PDECore.getDefault().getModelManager().findModel(project);
76             }
77         }
78     }
79
80     /* (non-Javadoc)
81      * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#createAdvancedControls(org.eclipse.swt.widgets.Composite)
82      */

83     protected void createAdvancedControls(Composite parent) {
84         fGroup = new Group(parent, SWT.NONE);
85         fGroup.setText(PDEUIMessages.ProductFileWizadPage_groupTitle);
86         fGroup.setLayout(new GridLayout(2, false));
87         fGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
88         
89         fBasicButton = new Button(fGroup, SWT.RADIO);
90         GridData gd = new GridData();
91         gd.horizontalSpan = 2;
92         fBasicButton.setLayoutData(gd);
93         fBasicButton.setText(PDEUIMessages.ProductFileWizadPage_basic);
94         
95         fProductButton = new Button(fGroup, SWT.RADIO);
96         fProductButton.setText(PDEUIMessages.ProductFileWizadPage_existingProduct);
97         fProductButton.addSelectionListener(new SelectionAdapter() {
98             public void widgetSelected(SelectionEvent e) {
99                 fProductCombo.setEnabled(fProductButton.getSelection());
100             }
101         });
102         
103         fProductCombo = new Combo(fGroup, SWT.SINGLE|SWT.READ_ONLY);
104         fProductCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
105         fProductCombo.setItems(TargetPlatform.getProductNames());
106         
107         fLaunchConfigButton = new Button(fGroup, SWT.RADIO);
108         fLaunchConfigButton.setText(PDEUIMessages.ProductFileWizadPage_existingLaunchConfig);
109         fLaunchConfigButton.addSelectionListener(new SelectionAdapter() {
110             public void widgetSelected(SelectionEvent e) {
111                 fLaunchConfigCombo.setEnabled(fLaunchConfigButton.getSelection());
112             }
113         });
114         
115         fLaunchConfigCombo = new Combo(fGroup, SWT.SINGLE|SWT.READ_ONLY);
116         fLaunchConfigCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
117         fLaunchConfigCombo.setItems(getLaunchConfigurations());
118         
119         initializeState();
120     }
121     
122     private void initializeState() {
123         fLaunchConfigCombo.setEnabled(false);
124         if (fLaunchConfigCombo.getItemCount() > 0)
125             fLaunchConfigCombo.setText(fLaunchConfigCombo.getItem(0));
126         
127         if (fModel != null && fModel.getPluginBase().getId() != null) {
128             IPluginExtension[] extensions = fModel.getPluginBase().getExtensions();
129             for (int i = 0; i < extensions.length; i++) {
130                 String JavaDoc point = extensions[i].getPoint();
131                 if ("org.eclipse.core.runtime.products".equals(point)) { //$NON-NLS-1$
132
String JavaDoc id = extensions[i].getId();
133                     if (id != null) {
134                         String JavaDoc full = fModel.getPluginBase().getId() + "." + id; //$NON-NLS-1$
135
if (fProductCombo.indexOf(full) != -1) {
136                             fProductCombo.setText(full);
137                             fProductButton.setSelection(true);
138                             return;
139                         }
140                     }
141                 }
142             }
143         }
144         
145         fBasicButton.setSelection(true);
146
147         fProductCombo.setEnabled(false);
148         if (fProductCombo.getItemCount() > 0)
149             fProductCombo.setText(fProductCombo.getItem(0));
150
151     }
152     
153     /* (non-Javadoc)
154      * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#validatePage()
155      */

156     protected boolean validatePage() {
157         if (!getFileName().trim().endsWith(".product")) { //$NON-NLS-1$
158
setErrorMessage(PDEUIMessages.ProductFileWizadPage_error);
159             return false;
160         }
161         if (getFileName().trim().length() <= 8) {
162             return false;
163         }
164         return super.validatePage();
165     }
166     
167     /* (non-Javadoc)
168      * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#validateLinkedResource()
169      */

170     protected IStatus validateLinkedResource() {
171         return new Status(IStatus.OK, PDEPlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$
172
}
173     
174     /* (non-Javadoc)
175      * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#createLinkTarget()
176      */

177     protected void createLinkTarget() {
178     }
179     
180     private String JavaDoc[] getLaunchConfigurations() {
181         ArrayList JavaDoc list = new ArrayList JavaDoc();
182         try {
183             ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
184             ILaunchConfigurationType type = manager.getLaunchConfigurationType("org.eclipse.pde.ui.RuntimeWorkbench"); //$NON-NLS-1$
185
ILaunchConfiguration[] configs = manager.getLaunchConfigurations(type);
186             for (int i = 0; i < configs.length; i++) {
187                 if (!DebugUITools.isPrivate(configs[i]))
188                     list.add(configs[i].getName());
189             }
190         } catch (CoreException e) {
191         }
192         return (String JavaDoc[])list.toArray(new String JavaDoc[list.size()]);
193     }
194     
195     public ILaunchConfiguration getSelectedLaunchConfiguration() {
196         if (!fLaunchConfigButton.getSelection())
197             return null;
198         
199         String JavaDoc configName = fLaunchConfigCombo.getText();
200         try {
201             ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
202             ILaunchConfigurationType type = manager.getLaunchConfigurationType("org.eclipse.pde.ui.RuntimeWorkbench"); //$NON-NLS-1$
203
ILaunchConfiguration[] configs = manager.getLaunchConfigurations(type);
204             for (int i = 0; i < configs.length; i++) {
205                 if (configs[i].getName().equals(configName) && !DebugUITools.isPrivate(configs[i]))
206                     return configs[i];
207             }
208         } catch (CoreException e) {
209         }
210         return null;
211     }
212     
213     public String JavaDoc getSelectedProduct() {
214         return fProductButton.getSelection() ? fProductCombo.getText() : null;
215     }
216     
217     public int getInitializationOption() {
218         if (fBasicButton.getSelection())
219             return USE_DEFAULT;
220         if (fProductButton.getSelection())
221             return USE_PRODUCT;
222         return USE_LAUNCH_CONFIG;
223     }
224     
225     public void createControl(Composite parent) {
226         super.createControl(parent);
227         Dialog.applyDialogFont(fGroup);
228         setFileName(".product"); //$NON-NLS-1$
229

230         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IHelpContextIds.PRODUCT_FILE_PAGE );
231     }
232     
233 }
234
Popular Tags