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.ui.internal.dialogs;
011:
012: import org.eclipse.jface.dialogs.IDialogSettings;
013: import org.eclipse.jface.viewers.DoubleClickEvent;
014: import org.eclipse.jface.viewers.IDoubleClickListener;
015: import org.eclipse.jface.viewers.ISelectionChangedListener;
016: import org.eclipse.jface.viewers.IStructuredSelection;
017: import org.eclipse.jface.viewers.SelectionChangedEvent;
018: import org.eclipse.jface.viewers.StructuredSelection;
019: import org.eclipse.jface.viewers.TableViewer;
020: import org.eclipse.jface.wizard.IWizardNode;
021: import org.eclipse.swt.SWT;
022: import org.eclipse.swt.graphics.Font;
023: import org.eclipse.swt.layout.GridData;
024: import org.eclipse.swt.layout.GridLayout;
025: import org.eclipse.swt.widgets.Composite;
026: import org.eclipse.swt.widgets.Control;
027: import org.eclipse.swt.widgets.Label;
028: import org.eclipse.swt.widgets.Table;
029: import org.eclipse.ui.IWorkbench;
030: import org.eclipse.ui.internal.WorkbenchMessages;
031: import org.eclipse.ui.model.AdaptableList;
032: import org.eclipse.ui.model.WorkbenchLabelProvider;
033: import org.eclipse.ui.model.WorkbenchViewerComparator;
034:
035: /**
036: * Abstract implementation of a wizard selection page which simply displays a
037: * list of specified wizard elements and allows the user to select one to be
038: * launched. Subclasses just need to provide a method which creates an
039: * appropriate wizard node based upon a user selection.
040: */
041: public abstract class WorkbenchWizardListSelectionPage extends
042: WorkbenchWizardSelectionPage implements
043: ISelectionChangedListener, IDoubleClickListener {
044:
045: // id constants
046: private static final String DIALOG_SETTING_SECTION_NAME = "WizardListSelectionPage."; //$NON-NLS-1$
047:
048: private final static int SIZING_LISTS_HEIGHT = 200;
049:
050: private static final String STORE_SELECTED_WIZARD_ID = DIALOG_SETTING_SECTION_NAME
051: + "STORE_SELECTED_WIZARD_ID"; //$NON-NLS-1$
052:
053: private TableViewer viewer;
054:
055: private String message;
056:
057: /**
058: * Creates a <code>WorkbenchWizardListSelectionPage</code>.
059: *
060: * @param aWorkbench the current workbench
061: * @param currentSelection the workbench's current resource selection
062: * @param wizardElements the collection of <code>WorkbenchWizardElements</code>
063: * to display for selection
064: * @param message the message to display above the selection list
065: * @param triggerPointId the trigger point id
066: */
067: protected WorkbenchWizardListSelectionPage(IWorkbench aWorkbench,
068: IStructuredSelection currentSelection,
069: AdaptableList wizardElements, String message,
070: String triggerPointId) {
071: super (
072: "singleWizardSelectionPage", aWorkbench, currentSelection, wizardElements, triggerPointId); //$NON-NLS-1$
073: setDescription(WorkbenchMessages.WizardList_description);
074: this .message = message;
075: }
076:
077: /*
078: * (non-Javadoc)
079: *
080: * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
081: */
082: public void createControl(Composite parent) {
083:
084: Font font = parent.getFont();
085:
086: // create composite for page.
087: Composite outerContainer = new Composite(parent, SWT.NONE);
088: outerContainer.setLayout(new GridLayout());
089: outerContainer.setLayoutData(new GridData(
090: GridData.VERTICAL_ALIGN_FILL
091: | GridData.HORIZONTAL_ALIGN_FILL));
092: outerContainer.setFont(font);
093:
094: Label messageLabel = new Label(outerContainer, SWT.NONE);
095: messageLabel.setText(message);
096: messageLabel.setFont(font);
097:
098: createViewer(outerContainer);
099: layoutTopControl(viewer.getControl());
100:
101: restoreWidgetValues();
102:
103: setControl(outerContainer);
104: }
105:
106: /**
107: * Create a new viewer in the parent.
108: *
109: * @param parent the parent <code>Composite</code>.
110: */
111: private void createViewer(Composite parent) {
112: //Create a table for the list
113: Table table = new Table(parent, SWT.BORDER);
114: table.setFont(parent.getFont());
115:
116: // the list viewer
117: viewer = new TableViewer(table);
118: viewer.setContentProvider(new WizardContentProvider());
119: viewer.setLabelProvider(new WorkbenchLabelProvider());
120: viewer.setComparator(new WorkbenchViewerComparator());
121: viewer.addSelectionChangedListener(this );
122: viewer.addDoubleClickListener(this );
123: viewer.setInput(wizardElements);
124: }
125:
126: /**
127: * Returns an <code>IWizardNode</code> representing the specified
128: * workbench wizard which has been selected by the user. <b>Subclasses
129: * </b> must override this abstract implementation.
130: *
131: * @param element the wizard element that an <code>IWizardNode</code> is
132: * needed for
133: * @return org.eclipse.jface.wizards.IWizardNode
134: */
135: protected abstract IWizardNode createWizardNode(
136: WorkbenchWizardElement element);
137:
138: /**
139: * An item in a viewer has been double-clicked.
140: */
141: public void doubleClick(DoubleClickEvent event) {
142: selectionChanged(new SelectionChangedEvent(event.getViewer(),
143: event.getViewer().getSelection()));
144: getContainer().showPage(getNextPage());
145: }
146:
147: /**
148: * Layout the top control.
149: *
150: * @param control the control.
151: * @since 3.0
152: */
153: private void layoutTopControl(Control control) {
154: GridData data = new GridData(GridData.FILL_BOTH);
155:
156: int availableRows = DialogUtil.availableRows(control
157: .getParent());
158:
159: //Only give a height hint if the dialog is going to be too small
160: if (availableRows > 50) {
161: data.heightHint = SIZING_LISTS_HEIGHT;
162: } else {
163: data.heightHint = availableRows * 3;
164: }
165:
166: control.setLayoutData(data);
167:
168: }
169:
170: /**
171: * Uses the dialog store to restore widget values to the values that they
172: * held last time this wizard was used to completion.
173: */
174: private void restoreWidgetValues() {
175:
176: IDialogSettings settings = getDialogSettings();
177: if (settings == null) {
178: return;
179: }
180:
181: String wizardId = settings.get(STORE_SELECTED_WIZARD_ID);
182: WorkbenchWizardElement wizard = findWizard(wizardId);
183: if (wizard == null) {
184: return;
185: }
186:
187: StructuredSelection selection = new StructuredSelection(wizard);
188: viewer.setSelection(selection);
189: }
190:
191: /**
192: * Since Finish was pressed, write widget values to the dialog store so
193: * that they will persist into the next invocation of this wizard page
194: */
195: public void saveWidgetValues() {
196: IStructuredSelection sel = (IStructuredSelection) viewer
197: .getSelection();
198: if (sel.size() > 0) {
199: WorkbenchWizardElement selectedWizard = (WorkbenchWizardElement) sel
200: .getFirstElement();
201: getDialogSettings().put(STORE_SELECTED_WIZARD_ID,
202: selectedWizard.getId());
203: }
204: }
205:
206: /**
207: * Notes the newly-selected wizard element and updates the page
208: * accordingly.
209: *
210: * @param event the selection changed event
211: */
212: public void selectionChanged(SelectionChangedEvent event) {
213: setErrorMessage(null);
214: IStructuredSelection selection = (IStructuredSelection) event
215: .getSelection();
216: WorkbenchWizardElement currentWizardSelection = (WorkbenchWizardElement) selection
217: .getFirstElement();
218: if (currentWizardSelection == null) {
219: setMessage(null);
220: setSelectedNode(null);
221: return;
222: }
223:
224: setSelectedNode(createWizardNode(currentWizardSelection));
225: setMessage(currentWizardSelection.getDescription());
226: }
227: }
|