KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > target > TargetDefinitionWizardPage


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.wizards.target;
12
13 import java.io.BufferedInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.net.URL JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.jface.dialogs.Dialog;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.pde.internal.core.PDECore;
23 import org.eclipse.pde.internal.core.TargetDefinitionManager;
24 import org.eclipse.pde.internal.core.itarget.ITargetModel;
25 import org.eclipse.pde.internal.core.target.TargetModel;
26 import org.eclipse.pde.internal.ui.IHelpContextIds;
27 import org.eclipse.pde.internal.ui.PDEUIMessages;
28 import org.eclipse.pde.internal.ui.editor.target.OpenTargetProfileAction;
29 import org.eclipse.pde.internal.ui.util.SWTUtil;
30 import org.eclipse.pde.internal.ui.wizards.PDEWizardNewFileCreationPage;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.events.SelectionAdapter;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Combo;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Group;
40 import org.eclipse.ui.PlatformUI;
41
42 public class TargetDefinitionWizardPage extends PDEWizardNewFileCreationPage {
43     
44     protected static final int USE_DEFAULT = 0;
45     protected static final int USE_CURRENT_TP = 1;
46     protected static final int USE_EXISTING_TARGET = 2;
47     
48     private Button fDefaultButton;
49     private Button fCurrentTPButton;
50     private Button fExistingTargetButton;
51     private Combo fTargets;
52     private String JavaDoc[] fTargetIds;
53     private Button fPreviewButton;
54     
55     private static String JavaDoc EXTENSION = "target"; //$NON-NLS-1$
56

57     public TargetDefinitionWizardPage(String JavaDoc pageName, IStructuredSelection selection) {
58         super(pageName, selection);
59         setTitle(PDEUIMessages.TargetProfileWizardPage_title);
60         setDescription(PDEUIMessages.TargetProfileWizardPage_description);
61         // Force the file extension to be 'target'
62
setFileExtension(EXTENSION);
63     }
64     
65     public void createControl(Composite parent) {
66         super.createControl(parent);
67
68         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IHelpContextIds.TARGET_DEFINITION_PAGE );
69     }
70     
71     protected void createAdvancedControls(Composite parent) {
72         Group group = new Group(parent, SWT.NONE);
73         group.setText(PDEUIMessages.TargetProfileWizardPage_groupTitle);
74         group.setLayout(new GridLayout(3, false));
75         group.setLayoutData(new GridData(GridData.FILL_BOTH));
76         
77         fDefaultButton = new Button(group, SWT.RADIO);
78         fDefaultButton.setText(PDEUIMessages.TargetProfileWizardPage_blankTarget);
79         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
80         gd.horizontalSpan = 3;
81         fDefaultButton.setLayoutData(gd);
82         fDefaultButton.setSelection(true);
83         
84         fCurrentTPButton = new Button(group, SWT.RADIO);
85         fCurrentTPButton.setText(PDEUIMessages.TargetProfileWizardPage_currentPlatform);
86         gd = new GridData(GridData.FILL_HORIZONTAL);
87         gd.horizontalSpan = 3;
88         fCurrentTPButton.setLayoutData(gd);
89         
90         fExistingTargetButton = new Button(group, SWT.RADIO);
91         fExistingTargetButton.setText(PDEUIMessages.TargetProfileWizardPage_existingTarget);
92         fExistingTargetButton.setLayoutData(new GridData());
93         fExistingTargetButton.addSelectionListener(new SelectionAdapter() {
94             public void widgetSelected(SelectionEvent e) {
95                 boolean enabled = fExistingTargetButton.getSelection();
96                 fTargets.setEnabled(enabled);
97                 fPreviewButton.setEnabled(enabled);
98             }
99         });
100         
101         fTargets = new Combo(group, SWT.SINGLE|SWT.READ_ONLY);
102         fTargets.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
103         fTargets.setEnabled(false);
104         initializeTargetCombo();
105         
106         fPreviewButton = new Button(group, SWT.PUSH);
107         fPreviewButton.setText(PDEUIMessages.TargetProfileWizardPage_viewProfile);
108         fPreviewButton.setLayoutData(new GridData());
109         SWTUtil.setButtonDimensionHint(fPreviewButton);
110         fPreviewButton.setEnabled(false);
111         fPreviewButton.addSelectionListener(new SelectionAdapter() {
112             public void widgetSelected(SelectionEvent e) {
113                 InputStream JavaDoc stream = null;
114                 try {
115                     URL JavaDoc url = getExternalTargetURL();
116                     if (url != null)
117                         stream = new BufferedInputStream JavaDoc(url.openStream());
118                     if (stream != null) {
119                         ITargetModel model = new TargetModel();
120                         model.load(stream, false);
121                         new OpenTargetProfileAction(getShell(), model, fTargets.getText()).run();
122                     }
123                 } catch (IOException JavaDoc e1) {
124                 } catch (CoreException e2) {
125                 } finally {
126                     try {
127                         if (stream != null)
128                             stream.close();
129                     } catch (IOException JavaDoc e3) {
130                     }
131                 }
132             }
133         });
134         
135         Dialog.applyDialogFont(group);
136     }
137     
138     private URL JavaDoc getExternalTargetURL() {
139         TargetDefinitionManager manager = PDECore.getDefault().getTargetProfileManager();
140         IConfigurationElement elem = manager.getTarget(fTargetIds[fTargets.getSelectionIndex()]);
141         if (elem != null) {
142             String JavaDoc path = elem.getAttribute("definition"); //$NON-NLS-1$
143
String JavaDoc symbolicName = elem.getDeclaringExtension().getNamespaceIdentifier();
144             return TargetDefinitionManager.getResourceURL(symbolicName, path);
145         }
146         return null;
147     }
148
149     
150     protected void initializeTargetCombo() {
151         IConfigurationElement[] elements = PDECore.getDefault().getTargetProfileManager().getSortedTargets();
152         fTargetIds = new String JavaDoc[elements.length];
153         for (int i = 0; i < elements.length; i++) {
154             String JavaDoc name =elements[i].getAttribute("name"); //$NON-NLS-1$
155
if (fTargets.indexOf(name) == -1)
156                 fTargets.add(name);
157             fTargetIds[i] = elements[i].getAttribute("id"); //$NON-NLS-1$
158
}
159         if (elements.length > 0)
160             fTargets.select(0);
161     }
162     
163     protected int getInitializationOption() {
164         if (fDefaultButton.getSelection())
165             return USE_DEFAULT;
166         else if (fCurrentTPButton.getSelection())
167             return USE_CURRENT_TP;
168         return USE_EXISTING_TARGET;
169     }
170     
171     protected String JavaDoc getTargetId() {
172         return fTargetIds[fTargets.getSelectionIndex()];
173     }
174     
175 }
176
Popular Tags