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.wizards.target;
011:
012: import java.io.BufferedInputStream;
013: import java.io.IOException;
014: import java.io.InputStream;
015: import java.net.URL;
016:
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IConfigurationElement;
019: import org.eclipse.jface.dialogs.Dialog;
020: import org.eclipse.jface.viewers.IStructuredSelection;
021: import org.eclipse.pde.internal.core.PDECore;
022: import org.eclipse.pde.internal.core.TargetDefinitionManager;
023: import org.eclipse.pde.internal.core.itarget.ITargetModel;
024: import org.eclipse.pde.internal.core.target.TargetModel;
025: import org.eclipse.pde.internal.ui.IHelpContextIds;
026: import org.eclipse.pde.internal.ui.PDEUIMessages;
027: import org.eclipse.pde.internal.ui.editor.target.OpenTargetProfileAction;
028: import org.eclipse.pde.internal.ui.util.SWTUtil;
029: import org.eclipse.pde.internal.ui.wizards.PDEWizardNewFileCreationPage;
030: import org.eclipse.swt.SWT;
031: import org.eclipse.swt.events.SelectionAdapter;
032: import org.eclipse.swt.events.SelectionEvent;
033: import org.eclipse.swt.layout.GridData;
034: import org.eclipse.swt.layout.GridLayout;
035: import org.eclipse.swt.widgets.Button;
036: import org.eclipse.swt.widgets.Combo;
037: import org.eclipse.swt.widgets.Composite;
038: import org.eclipse.swt.widgets.Group;
039: import org.eclipse.ui.PlatformUI;
040:
041: public class TargetDefinitionWizardPage extends
042: PDEWizardNewFileCreationPage {
043:
044: protected static final int USE_DEFAULT = 0;
045: protected static final int USE_CURRENT_TP = 1;
046: protected static final int USE_EXISTING_TARGET = 2;
047:
048: private Button fDefaultButton;
049: private Button fCurrentTPButton;
050: private Button fExistingTargetButton;
051: private Combo fTargets;
052: private String[] fTargetIds;
053: private Button fPreviewButton;
054:
055: private static String EXTENSION = "target"; //$NON-NLS-1$
056:
057: public TargetDefinitionWizardPage(String pageName,
058: IStructuredSelection selection) {
059: super (pageName, selection);
060: setTitle(PDEUIMessages.TargetProfileWizardPage_title);
061: setDescription(PDEUIMessages.TargetProfileWizardPage_description);
062: // Force the file extension to be 'target'
063: setFileExtension(EXTENSION);
064: }
065:
066: public void createControl(Composite parent) {
067: super .createControl(parent);
068:
069: PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
070: IHelpContextIds.TARGET_DEFINITION_PAGE);
071: }
072:
073: protected void createAdvancedControls(Composite parent) {
074: Group group = new Group(parent, SWT.NONE);
075: group.setText(PDEUIMessages.TargetProfileWizardPage_groupTitle);
076: group.setLayout(new GridLayout(3, false));
077: group.setLayoutData(new GridData(GridData.FILL_BOTH));
078:
079: fDefaultButton = new Button(group, SWT.RADIO);
080: fDefaultButton
081: .setText(PDEUIMessages.TargetProfileWizardPage_blankTarget);
082: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
083: gd.horizontalSpan = 3;
084: fDefaultButton.setLayoutData(gd);
085: fDefaultButton.setSelection(true);
086:
087: fCurrentTPButton = new Button(group, SWT.RADIO);
088: fCurrentTPButton
089: .setText(PDEUIMessages.TargetProfileWizardPage_currentPlatform);
090: gd = new GridData(GridData.FILL_HORIZONTAL);
091: gd.horizontalSpan = 3;
092: fCurrentTPButton.setLayoutData(gd);
093:
094: fExistingTargetButton = new Button(group, SWT.RADIO);
095: fExistingTargetButton
096: .setText(PDEUIMessages.TargetProfileWizardPage_existingTarget);
097: fExistingTargetButton.setLayoutData(new GridData());
098: fExistingTargetButton
099: .addSelectionListener(new SelectionAdapter() {
100: public void widgetSelected(SelectionEvent e) {
101: boolean enabled = fExistingTargetButton
102: .getSelection();
103: fTargets.setEnabled(enabled);
104: fPreviewButton.setEnabled(enabled);
105: }
106: });
107:
108: fTargets = new Combo(group, SWT.SINGLE | SWT.READ_ONLY);
109: fTargets.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
110: fTargets.setEnabled(false);
111: initializeTargetCombo();
112:
113: fPreviewButton = new Button(group, SWT.PUSH);
114: fPreviewButton
115: .setText(PDEUIMessages.TargetProfileWizardPage_viewProfile);
116: fPreviewButton.setLayoutData(new GridData());
117: SWTUtil.setButtonDimensionHint(fPreviewButton);
118: fPreviewButton.setEnabled(false);
119: fPreviewButton.addSelectionListener(new SelectionAdapter() {
120: public void widgetSelected(SelectionEvent e) {
121: InputStream stream = null;
122: try {
123: URL url = getExternalTargetURL();
124: if (url != null)
125: stream = new BufferedInputStream(url
126: .openStream());
127: if (stream != null) {
128: ITargetModel model = new TargetModel();
129: model.load(stream, false);
130: new OpenTargetProfileAction(getShell(), model,
131: fTargets.getText()).run();
132: }
133: } catch (IOException e1) {
134: } catch (CoreException e2) {
135: } finally {
136: try {
137: if (stream != null)
138: stream.close();
139: } catch (IOException e3) {
140: }
141: }
142: }
143: });
144:
145: Dialog.applyDialogFont(group);
146: }
147:
148: private URL getExternalTargetURL() {
149: TargetDefinitionManager manager = PDECore.getDefault()
150: .getTargetProfileManager();
151: IConfigurationElement elem = manager
152: .getTarget(fTargetIds[fTargets.getSelectionIndex()]);
153: if (elem != null) {
154: String path = elem.getAttribute("definition"); //$NON-NLS-1$
155: String symbolicName = elem.getDeclaringExtension()
156: .getNamespaceIdentifier();
157: return TargetDefinitionManager.getResourceURL(symbolicName,
158: path);
159: }
160: return null;
161: }
162:
163: protected void initializeTargetCombo() {
164: IConfigurationElement[] elements = PDECore.getDefault()
165: .getTargetProfileManager().getSortedTargets();
166: fTargetIds = new String[elements.length];
167: for (int i = 0; i < elements.length; i++) {
168: String name = elements[i].getAttribute("name"); //$NON-NLS-1$
169: if (fTargets.indexOf(name) == -1)
170: fTargets.add(name);
171: fTargetIds[i] = elements[i].getAttribute("id"); //$NON-NLS-1$
172: }
173: if (elements.length > 0)
174: fTargets.select(0);
175: }
176:
177: protected int getInitializationOption() {
178: if (fDefaultButton.getSelection())
179: return USE_DEFAULT;
180: else if (fCurrentTPButton.getSelection())
181: return USE_CURRENT_TP;
182: return USE_EXISTING_TARGET;
183: }
184:
185: protected String getTargetId() {
186: return fTargetIds[fTargets.getSelectionIndex()];
187: }
188:
189: }
|