001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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;
011:
012: import java.util.Iterator;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IConfigurationElement;
016: import org.eclipse.core.runtime.IExecutableExtension;
017: import org.eclipse.jface.action.Action;
018: import org.eclipse.jface.dialogs.Dialog;
019: import org.eclipse.jface.viewers.DoubleClickEvent;
020: import org.eclipse.jface.viewers.IDoubleClickListener;
021: import org.eclipse.jface.viewers.ISelectionChangedListener;
022: import org.eclipse.jface.viewers.IStructuredSelection;
023: import org.eclipse.jface.viewers.SelectionChangedEvent;
024: import org.eclipse.jface.viewers.StructuredSelection;
025: import org.eclipse.jface.viewers.TableViewer;
026: import org.eclipse.jface.wizard.IWizard;
027: import org.eclipse.jface.wizard.IWizardNode;
028: import org.eclipse.jface.wizard.IWizardPage;
029: import org.eclipse.pde.internal.ui.elements.ElementList;
030: import org.eclipse.pde.internal.ui.elements.ListContentProvider;
031: import org.eclipse.pde.ui.IPluginContentWizard;
032: import org.eclipse.swt.SWT;
033: import org.eclipse.swt.custom.SashForm;
034: import org.eclipse.swt.layout.GridData;
035: import org.eclipse.swt.layout.GridLayout;
036: import org.eclipse.swt.widgets.Composite;
037: import org.eclipse.swt.widgets.Label;
038: import org.eclipse.swt.widgets.Table;
039: import org.eclipse.swt.widgets.TableItem;
040:
041: public abstract class WizardListSelectionPage extends
042: BaseWizardSelectionPage implements ISelectionChangedListener,
043: IExecutableExtension {
044: protected TableViewer wizardSelectionViewer;
045: protected ElementList wizardElements;
046: private WizardSelectedAction doubleClickAction = new WizardSelectedAction();
047:
048: private class WizardSelectedAction extends Action {
049: public WizardSelectedAction() {
050: super ("wizardSelection"); //$NON-NLS-1$
051: }
052:
053: public void run() {
054: selectionChanged(new SelectionChangedEvent(
055: wizardSelectionViewer, wizardSelectionViewer
056: .getSelection()));
057: advanceToNextPage();
058: }
059: }
060:
061: public WizardListSelectionPage(ElementList wizardElements,
062: String message) {
063: super ("ListSelection", message); //$NON-NLS-1$
064: this .wizardElements = wizardElements;
065: }
066:
067: public void advanceToNextPage() {
068: getContainer().showPage(getNextPage());
069: }
070:
071: public ElementList getWizardElements() {
072: return wizardElements;
073: }
074:
075: public void createControl(Composite parent) {
076: Composite container = new Composite(parent, SWT.NONE);
077: GridLayout layout = new GridLayout();
078: layout.verticalSpacing = 10;
079: container.setLayout(layout);
080: container.setLayoutData(new GridData(GridData.FILL_BOTH));
081:
082: createAbove(container, 1);
083: Label label = new Label(container, SWT.NONE);
084: label.setText(getLabel());
085: GridData gd = new GridData();
086: label.setLayoutData(gd);
087:
088: SashForm sashForm = new SashForm(container, SWT.HORIZONTAL);
089: gd = new GridData(GridData.FILL_BOTH);
090: // limit the width of the sash form to avoid the wizard
091: // opening very wide. This is just preferred size -
092: // it can be made bigger by the wizard
093: // See bug #83356
094: gd.widthHint = 300;
095: sashForm.setLayoutData(gd);
096:
097: wizardSelectionViewer = new TableViewer(sashForm, SWT.BORDER);
098: wizardSelectionViewer
099: .setContentProvider(new ListContentProvider());
100: wizardSelectionViewer
101: .setLabelProvider(ListUtil.TABLE_LABEL_PROVIDER);
102: wizardSelectionViewer.setComparator(ListUtil.NAME_COMPARATOR);
103: wizardSelectionViewer
104: .addDoubleClickListener(new IDoubleClickListener() {
105: public void doubleClick(DoubleClickEvent event) {
106: doubleClickAction.run();
107: }
108: });
109: createDescriptionIn(sashForm);
110: createBelow(container, 1);
111: initializeViewer();
112: wizardSelectionViewer.setInput(wizardElements);
113: wizardSelectionViewer.addSelectionChangedListener(this );
114: Dialog.applyDialogFont(container);
115: setControl(container);
116: }
117:
118: protected void createAbove(Composite container, int span) {
119: }
120:
121: protected void createBelow(Composite container, int span) {
122: }
123:
124: protected void initializeViewer() {
125: }
126:
127: public void selectionChanged(SelectionChangedEvent event) {
128: setErrorMessage(null);
129: IStructuredSelection selection = (IStructuredSelection) event
130: .getSelection();
131: WizardElement currentWizardSelection = null;
132: Iterator iter = selection.iterator();
133: if (iter.hasNext())
134: currentWizardSelection = (WizardElement) iter.next();
135: if (currentWizardSelection == null) {
136: setDescriptionText(""); //$NON-NLS-1$
137: setSelectedNode(null);
138: return;
139: }
140: final WizardElement finalSelection = currentWizardSelection;
141: setSelectedNode(createWizardNode(finalSelection));
142: setDescriptionText(finalSelection.getDescription());
143: getContainer().updateButtons();
144: }
145:
146: public IWizardPage getNextPage(boolean shouldCreate) {
147: if (!shouldCreate)
148: return super .getNextPage();
149: IWizardNode selectedNode = getSelectedNode();
150: selectedNode.dispose();
151: IWizard wizard = selectedNode.getWizard();
152: if (wizard == null) {
153: super .setSelectedNode(null);
154: return null;
155: }
156: if (shouldCreate)
157: // Allow the wizard to create its pages
158: wizard.addPages();
159: return wizard.getStartingPage();
160: }
161:
162: protected void focusAndSelectFirst() {
163: Table table = wizardSelectionViewer.getTable();
164: table.setFocus();
165: TableItem[] items = table.getItems();
166: if (items.length > 0) {
167: TableItem first = items[0];
168: Object obj = first.getData();
169: wizardSelectionViewer.setSelection(new StructuredSelection(
170: obj));
171: }
172: }
173:
174: /* (non-Javadoc)
175: * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement, java.lang.String, java.lang.Object)
176: */
177: public void setInitializationData(IConfigurationElement config,
178: String propertyName, Object data) throws CoreException {
179: }
180:
181: public IPluginContentWizard getSelectedWizard() {
182: IWizardNode node = getSelectedNode();
183: if (node != null)
184: return (IPluginContentWizard) node.getWizard();
185: return null;
186: }
187:
188: /* (non-Javadoc)
189: * @see org.eclipse.jface.wizard.WizardSelectionPage#canFlipToNextPage()
190: */
191: public boolean canFlipToNextPage() {
192: IStructuredSelection ssel = (IStructuredSelection) wizardSelectionViewer
193: .getSelection();
194: return ssel != null && !ssel.isEmpty();
195: }
196: }
|