KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > WorkbenchWizardListSelectionPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.ui.internal.dialogs;
12
13 import org.eclipse.jface.dialogs.IDialogSettings;
14 import org.eclipse.jface.viewers.DoubleClickEvent;
15 import org.eclipse.jface.viewers.IDoubleClickListener;
16 import org.eclipse.jface.viewers.ISelectionChangedListener;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.jface.viewers.SelectionChangedEvent;
19 import org.eclipse.jface.viewers.StructuredSelection;
20 import org.eclipse.jface.viewers.TableViewer;
21 import org.eclipse.jface.wizard.IWizardNode;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.graphics.Font;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.swt.widgets.Table;
30 import org.eclipse.ui.IWorkbench;
31 import org.eclipse.ui.internal.WorkbenchMessages;
32 import org.eclipse.ui.model.AdaptableList;
33 import org.eclipse.ui.model.WorkbenchLabelProvider;
34 import org.eclipse.ui.model.WorkbenchViewerComparator;
35
36 /**
37  * Abstract implementation of a wizard selection page which simply displays a
38  * list of specified wizard elements and allows the user to select one to be
39  * launched. Subclasses just need to provide a method which creates an
40  * appropriate wizard node based upon a user selection.
41  */

42 public abstract class WorkbenchWizardListSelectionPage extends
43         WorkbenchWizardSelectionPage implements ISelectionChangedListener,
44         IDoubleClickListener {
45
46     // id constants
47
private static final String JavaDoc DIALOG_SETTING_SECTION_NAME = "WizardListSelectionPage."; //$NON-NLS-1$
48

49     private final static int SIZING_LISTS_HEIGHT = 200;
50
51     private static final String JavaDoc STORE_SELECTED_WIZARD_ID = DIALOG_SETTING_SECTION_NAME
52             + "STORE_SELECTED_WIZARD_ID"; //$NON-NLS-1$
53

54     private TableViewer viewer;
55
56     private String JavaDoc message;
57
58     /**
59      * Creates a <code>WorkbenchWizardListSelectionPage</code>.
60      *
61      * @param aWorkbench the current workbench
62      * @param currentSelection the workbench's current resource selection
63      * @param wizardElements the collection of <code>WorkbenchWizardElements</code>
64      * to display for selection
65      * @param message the message to display above the selection list
66      * @param triggerPointId the trigger point id
67      */

68     protected WorkbenchWizardListSelectionPage(IWorkbench aWorkbench,
69             IStructuredSelection currentSelection,
70             AdaptableList wizardElements, String JavaDoc message, String JavaDoc triggerPointId) {
71         super(
72                 "singleWizardSelectionPage", aWorkbench, currentSelection, wizardElements, triggerPointId); //$NON-NLS-1$
73
setDescription(WorkbenchMessages.WizardList_description);
74         this.message = message;
75     }
76
77     /*
78      * (non-Javadoc)
79      *
80      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
81      */

82     public void createControl(Composite parent) {
83
84         Font font = parent.getFont();
85
86         // create composite for page.
87
Composite outerContainer = new Composite(parent, SWT.NONE);
88         outerContainer.setLayout(new GridLayout());
89         outerContainer.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL
90                 | GridData.HORIZONTAL_ALIGN_FILL));
91         outerContainer.setFont(font);
92
93         Label messageLabel = new Label(outerContainer, SWT.NONE);
94         messageLabel.setText(message);
95         messageLabel.setFont(font);
96
97         createViewer(outerContainer);
98         layoutTopControl(viewer.getControl());
99
100         restoreWidgetValues();
101
102         setControl(outerContainer);
103     }
104
105     /**
106      * Create a new viewer in the parent.
107      *
108      * @param parent the parent <code>Composite</code>.
109      */

110     private void createViewer(Composite parent) {
111         //Create a table for the list
112
Table table = new Table(parent, SWT.BORDER);
113         table.setFont(parent.getFont());
114
115         // the list viewer
116
viewer = new TableViewer(table);
117         viewer.setContentProvider(new WizardContentProvider());
118         viewer.setLabelProvider(new WorkbenchLabelProvider());
119         viewer.setComparator(new WorkbenchViewerComparator());
120         viewer.addSelectionChangedListener(this);
121         viewer.addDoubleClickListener(this);
122         viewer.setInput(wizardElements);
123     }
124
125     /**
126      * Returns an <code>IWizardNode</code> representing the specified
127      * workbench wizard which has been selected by the user. <b>Subclasses
128      * </b> must override this abstract implementation.
129      *
130      * @param element the wizard element that an <code>IWizardNode</code> is
131      * needed for
132      * @return org.eclipse.jface.wizards.IWizardNode
133      */

134     protected abstract IWizardNode createWizardNode(
135             WorkbenchWizardElement element);
136
137     /**
138      * An item in a viewer has been double-clicked.
139      */

140     public void doubleClick(DoubleClickEvent event) {
141         selectionChanged(new SelectionChangedEvent(event.getViewer(), event
142                 .getViewer().getSelection()));
143         getContainer().showPage(getNextPage());
144     }
145
146     /**
147      * Layout the top control.
148      *
149      * @param control the control.
150      * @since 3.0
151      */

152     private void layoutTopControl(Control control) {
153         GridData data = new GridData(GridData.FILL_BOTH);
154
155         int availableRows = DialogUtil.availableRows(control.getParent());
156
157         //Only give a height hint if the dialog is going to be too small
158
if (availableRows > 50) {
159             data.heightHint = SIZING_LISTS_HEIGHT;
160         } else {
161             data.heightHint = availableRows * 3;
162         }
163
164         control.setLayoutData(data);
165
166     }
167
168     /**
169      * Uses the dialog store to restore widget values to the values that they
170      * held last time this wizard was used to completion.
171      */

172     private void restoreWidgetValues() {
173
174         IDialogSettings settings = getDialogSettings();
175         if (settings == null) {
176             return;
177         }
178
179         String JavaDoc wizardId = settings.get(STORE_SELECTED_WIZARD_ID);
180         WorkbenchWizardElement wizard = findWizard(wizardId);
181         if (wizard == null) {
182             return;
183         }
184
185         StructuredSelection selection = new StructuredSelection(wizard);
186         viewer.setSelection(selection);
187     }
188
189     /**
190      * Since Finish was pressed, write widget values to the dialog store so
191      * that they will persist into the next invocation of this wizard page
192      */

193     public void saveWidgetValues() {
194         IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
195         if (sel.size() > 0) {
196             WorkbenchWizardElement selectedWizard = (WorkbenchWizardElement) sel
197                     .getFirstElement();
198             getDialogSettings().put(STORE_SELECTED_WIZARD_ID,
199                     selectedWizard.getId());
200         }
201     }
202
203     /**
204      * Notes the newly-selected wizard element and updates the page
205      * accordingly.
206      *
207      * @param event the selection changed event
208      */

209     public void selectionChanged(SelectionChangedEvent event) {
210         setErrorMessage(null);
211         IStructuredSelection selection = (IStructuredSelection) event
212                 .getSelection();
213         WorkbenchWizardElement currentWizardSelection = (WorkbenchWizardElement) selection
214                 .getFirstElement();
215         if (currentWizardSelection == null) {
216             setMessage(null);
217             setSelectedNode(null);
218             return;
219         }
220
221         setSelectedNode(createWizardNode(currentWizardSelection));
222         setMessage(currentWizardSelection.getDescription());
223     }
224 }
225
Popular Tags