KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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 java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.viewers.DoubleClickEvent;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.eclipse.jface.viewers.TreeViewer;
24 import org.eclipse.jface.wizard.IWizardNode;
25 import org.eclipse.jface.wizard.IWizardPage;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.graphics.Font;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Label;
33 import org.eclipse.ui.IWorkbench;
34 import org.eclipse.ui.IWorkbenchWizard;
35 import org.eclipse.ui.activities.ITriggerPoint;
36 import org.eclipse.ui.activities.WorkbenchActivityHelper;
37 import org.eclipse.ui.dialogs.FilteredTree;
38 import org.eclipse.ui.internal.WorkbenchMessages;
39 import org.eclipse.ui.model.AdaptableList;
40 import org.eclipse.ui.model.WorkbenchLabelProvider;
41 import org.eclipse.ui.wizards.IWizardCategory;
42 import org.eclipse.ui.wizards.IWizardDescriptor;
43
44 /**
45  * Abstract wizard page class from which an import or export wizard can be chosen.
46  *
47  * @since 3.2
48  *
49  */

50 public abstract class ImportExportPage extends WorkbenchWizardSelectionPage{
51     protected static final String JavaDoc DIALOG_SETTING_SECTION_NAME = "ImportExportPage."; //$NON-NLS-1$
52

53     // tree viewer of wizard selections
54
private TreeViewer treeViewer;
55     
56     /*
57      * Class to create a control that shows a categorized tree of wizard types.
58      */

59     protected class CategorizedWizardSelectionTree {
60         private final static int SIZING_LISTS_HEIGHT = 200;
61         
62         private IWizardCategory wizardCategories;
63         private String JavaDoc message;
64         private TreeViewer viewer;
65
66         /**
67          * Constructor for CategorizedWizardSelectionTree
68          *
69          * @param categories root wizard category for the wizard type
70          * @param msg message describing what the user should choose from the tree.
71          */

72         protected CategorizedWizardSelectionTree(IWizardCategory categories, String JavaDoc msg){
73             this.wizardCategories = categories;
74             this.message = msg;
75         }
76         
77         /**
78          * Create the tree viewer and a message describing what the user should choose
79          * from the tree.
80          *
81          * @param parent Composite on which the tree viewer is to be created
82          * @return Comoposite with all widgets
83          */

84         protected Composite createControl(Composite parent){
85             Font font = parent.getFont();
86
87             // create composite for page.
88
Composite outerContainer = new Composite(parent, SWT.NONE);
89             outerContainer.setLayout(new GridLayout());
90             outerContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
91             outerContainer.setFont(font);
92
93             Label messageLabel = new Label(outerContainer, SWT.NONE);
94             if (message != null) {
95                 messageLabel.setText(message);
96             }
97             messageLabel.setFont(font);
98
99             createFilteredTree(outerContainer);
100             layoutTopControl(viewer.getControl());
101
102             return outerContainer;
103         }
104         
105         /**
106          * Create the categorized tree viewer.
107          *
108          * @param parent
109          */

110         private void createFilteredTree(Composite parent){
111             // Create a FilteredTree for the categories and wizards
112
FilteredTree filteredTree = new FilteredTree(parent, SWT.SINGLE | SWT.H_SCROLL
113                     | SWT.V_SCROLL | SWT.BORDER, new WizardPatternFilter());
114             viewer = filteredTree.getViewer();
115             filteredTree.setFont(parent.getFont());
116
117             viewer.setContentProvider(new WizardContentProvider());
118             viewer.setLabelProvider(new WorkbenchLabelProvider());
119             viewer.setComparator(DataTransferWizardCollectionComparator.INSTANCE);
120             
121             ArrayList JavaDoc inputArray = new ArrayList JavaDoc();
122             boolean expandTop = false;
123
124             if (wizardCategories != null) {
125                 if (wizardCategories.getParent() == null) {
126                     IWizardCategory [] children = wizardCategories.getCategories();
127                     for (int i = 0; i < children.length; i++) {
128                         inputArray.add(children[i]);
129                     }
130                 } else {
131                     expandTop = true;
132                     inputArray.add(wizardCategories);
133                 }
134             }
135
136             // ensure the category is expanded. If there is a remembered expansion it will be set later.
137
if (expandTop) {
138                 viewer.setAutoExpandLevel(2);
139             }
140
141             AdaptableList input = new AdaptableList(inputArray);
142             
143             // filter wizard list according to capabilities that are enabled
144
viewer.addFilter(new WizardActivityFilter());
145             
146             viewer.setInput(input);
147         }
148
149         /**
150          *
151          * @return the categorized tree viewer
152          */

153         protected TreeViewer getViewer(){
154             return viewer;
155         }
156
157         /**
158          * Layout for the given control.
159          *
160          * @param control
161          */

162         private void layoutTopControl(Control control) {
163             GridData data = new GridData(GridData.FILL_BOTH);
164
165             int availableRows = DialogUtil.availableRows(control.getParent());
166
167             //Only give a height hint if the dialog is going to be too small
168
if (availableRows > 50) {
169                 data.heightHint = SIZING_LISTS_HEIGHT;
170             } else {
171                 data.heightHint = availableRows * 3;
172             }
173
174             control.setLayoutData(data);
175         }
176     }
177     
178     /**
179      * Constructor for import/export wizard page.
180      *
181      * @param aWorkbench current workbench
182      * @param currentSelection current selection
183      */

184     protected ImportExportPage(IWorkbench aWorkbench, IStructuredSelection currentSelection){
185         super("importExportPage", aWorkbench, currentSelection, null, null); //$NON-NLS-1$
186
setTitle(WorkbenchMessages.Select);
187     }
188     
189     /*
190      * (non-Javadoc)
191      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
192      */

193     public void createControl(Composite parent) {
194         Font font = parent.getFont();
195     
196         // create composite for page.
197
Composite outerContainer = new Composite(parent, SWT.NONE);
198         outerContainer.setLayout(new GridLayout());
199         outerContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
200         outerContainer.setFont(font);
201
202         Composite comp = createTreeViewer(outerContainer);
203         
204         Dialog.applyDialogFont(comp);
205         
206         restoreWidgetValues();
207     
208         setControl(outerContainer);
209         
210        initialize();
211     }
212
213     /**
214      * Create the tree viewer from which a wizard is selected.
215      */

216     protected abstract Composite createTreeViewer(Composite parent);
217     
218     /**
219      * Method to call when an item in one of the lists is double-clicked.
220      * Shows the first page of the selected wizard or expands a collapsed
221      * tree.
222      * @param event
223      */

224     protected void treeDoubleClicked(DoubleClickEvent event){
225         ISelection selection = event.getViewer().getSelection();
226         IStructuredSelection ss = (IStructuredSelection) selection;
227         listSelectionChanged(ss);
228         
229         Object JavaDoc element = ss.getFirstElement();
230         TreeViewer v = (TreeViewer)event.getViewer();
231         if (v.isExpandable(element)) {
232             v.setExpandedState(element, !v.getExpandedState(element));
233         } else if (element instanceof WorkbenchWizardElement) {
234             if (canFlipToNextPage()) {
235                 getContainer().showPage(getNextPage());
236             }
237         }
238         getContainer().showPage(getNextPage());
239     }
240     
241     /*
242      * Update the wizard's message based on the given (selected) wizard element.
243      */

244     private void updateSelectedNode(WorkbenchWizardElement wizardElement){
245         setErrorMessage(null);
246         if (wizardElement == null) {
247             updateMessage();
248             setSelectedNode(null);
249             return;
250         }
251
252         setSelectedNode(createWizardNode(wizardElement));
253         setMessage(wizardElement.getDescription());
254     }
255     
256     /*
257      * Update the wizard's message based on the currently selected tab
258      * and the selected wizard on that tab.
259      */

260     protected void updateMessage(){
261         TreeViewer viewer = getTreeViewer();
262         if (viewer != null){
263             ISelection selection = viewer.getSelection();
264             IStructuredSelection ss = (IStructuredSelection) selection;
265             Object JavaDoc sel = ss.getFirstElement();
266             if (sel instanceof WorkbenchWizardElement){
267                 updateSelectedNode((WorkbenchWizardElement)sel);
268             }
269             else{
270                 setSelectedNode(null);
271             }
272         } else {
273             setMessage(null);
274         }
275     }
276     
277     /*
278      * Method to call whenever the selection in one of the lists has changed.
279      * Updates the wizard's message to relect the description of the currently
280      * selected wizard.
281      */

282     protected void listSelectionChanged(ISelection selection){
283         setErrorMessage(null);
284         IStructuredSelection ss = (IStructuredSelection) selection;
285         Object JavaDoc sel = ss.getFirstElement();
286         if (sel instanceof WorkbenchWizardElement){
287             WorkbenchWizardElement currentWizardSelection = (WorkbenchWizardElement) sel;
288             updateSelectedNode(currentWizardSelection);
289         } else {
290             updateSelectedNode(null);
291         }
292     }
293
294     /*
295      * Create a wizard node given a wizard's descriptor.
296      */

297     private IWizardNode createWizardNode(IWizardDescriptor element) {
298         return new WorkbenchWizardNode(this, element) {
299             public IWorkbenchWizard createWizard() throws CoreException {
300                 return wizardElement.createWizard();
301             }
302         };
303     }
304     
305     /**
306      * Uses the dialog store to restore widget values to the values that they
307      * held last time this wizard was used to completion.
308      */

309     protected void restoreWidgetValues() {
310         updateMessage();
311     }
312
313     /**
314      * Expands the wizard categories in this page's category viewer that were
315      * expanded last time this page was used. If a category that was previously
316      * expanded no longer exists then it is ignored.
317      */

318     protected void expandPreviouslyExpandedCategories(String JavaDoc setting, IWizardCategory wizardCategories, TreeViewer viewer) {
319         String JavaDoc[] expandedCategoryPaths = getDialogSettings()
320                 .getArray(setting);
321         if (expandedCategoryPaths == null || expandedCategoryPaths.length == 0) {
322             return;
323         }
324
325         List JavaDoc categoriesToExpand = new ArrayList JavaDoc(expandedCategoryPaths.length);
326
327         if (wizardCategories != null) {
328             for (int i = 0; i < expandedCategoryPaths.length; i++) {
329                 IWizardCategory category = wizardCategories
330                         .findCategory(new Path(expandedCategoryPaths[i]));
331                 if (category != null) {
332                     categoriesToExpand.add(category);
333                 }
334             }
335         }
336
337         if (!categoriesToExpand.isEmpty()) {
338             viewer.setExpandedElements(categoriesToExpand.toArray());
339         }
340
341     }
342
343     /**
344      * Selects the wizard category and wizard in this page that were selected
345      * last time this page was used. If a category or wizard that was
346      * previously selected no longer exists then it is ignored.
347      */

348     protected void selectPreviouslySelected(String JavaDoc setting, IWizardCategory wizardCategories, final TreeViewer viewer) {
349         String JavaDoc selectedId = getDialogSettings().get(setting);
350         if (selectedId == null) {
351             return;
352         }
353
354         if (wizardCategories == null) {
355             return;
356         }
357
358         Object JavaDoc selected = wizardCategories.findCategory(new Path(
359                 selectedId));
360
361         if (selected == null) {
362             selected = wizardCategories.findWizard(selectedId);
363
364             if (selected == null) {
365                 // if we cant find either a category or a wizard, abort.
366
return;
367             }
368         }
369
370         viewer.setSelection(new StructuredSelection(selected), true);
371     }
372  
373     /**
374      * Stores the collection of currently-expanded categories in this page's
375      * dialog store, in order to recreate this page's state in the next
376      * instance of this page.
377      */

378     protected void storeExpandedCategories(String JavaDoc setting, TreeViewer viewer) {
379         Object JavaDoc[] expandedElements = viewer.getExpandedElements();
380         List JavaDoc expandedElementPaths = new ArrayList JavaDoc(expandedElements.length);
381         for (int i = 0; i < expandedElements.length; ++i) {
382             if (expandedElements[i] instanceof IWizardCategory) {
383                 expandedElementPaths
384                         .add(((IWizardCategory) expandedElements[i])
385                                 .getPath().toString());
386             }
387         }
388         getDialogSettings().put(setting,
389                 (String JavaDoc[]) expandedElementPaths
390                         .toArray(new String JavaDoc[expandedElementPaths.size()]));
391     }
392
393     /**
394      * Stores the currently-selected element in this page's dialog store, in
395      * order to recreate this page's state in the next instance of this page.
396      */

397     protected void storeSelectedCategoryAndWizard(String JavaDoc setting, TreeViewer viewer) {
398         Object JavaDoc selected = ((IStructuredSelection) viewer
399                 .getSelection()).getFirstElement();
400
401         if (selected != null) {
402             if (selected instanceof IWizardCategory) {
403                 getDialogSettings().put(setting,
404                         ((IWizardCategory) selected).getPath()
405                                 .toString());
406             } else {
407                 // else its a wizard
408
getDialogSettings().put(setting,
409                         ((IWizardDescriptor) selected).getId());
410             }
411         }
412     }
413     
414     /**
415      * When Finish is pressed, write widget values to the dialog store so
416      * that they will persist into the next invocation of the wizard page.
417      *
418      */

419     public void saveWidgetValues(){
420         // do nothing by default - subclasses should override
421
}
422     
423     /*
424      * (non-Javadoc)
425      * @see org.eclipse.jface.wizard.IWizardPage#getNextPage()
426      */

427     public IWizardPage getNextPage() {
428         ITriggerPoint triggerPoint = getTriggerPoint();
429         
430         if (triggerPoint == null || WorkbenchActivityHelper.allowUseOf(triggerPoint, getSelectedNode())) {
431             return super.getNextPage();
432         }
433         return null;
434     }
435
436     /**
437      * Get the trigger point for the wizard type, if one exists.
438      *
439      * @return the wizard's trigger point
440      */

441     protected ITriggerPoint getTriggerPoint(){
442         return null; // default implementation
443
}
444     
445     /**
446      * Set the tree viewer that is used for this wizard selection page.
447      *
448      * @param viewer
449      */

450     protected void setTreeViewer(TreeViewer viewer){
451         treeViewer = viewer;
452     }
453     
454     /**
455      * Get the tree viewer that is used for this wizard selection page.
456      *
457      * @return tree viewer used for this wizard's selection page
458      */

459     protected TreeViewer getTreeViewer(){
460         return treeViewer;
461     }
462     
463     /**
464      * Perform any initialization of the wizard page that needs to be done
465      * after widgets are created and main control is set.
466      */

467     protected void initialize(){
468         // do nothing by default
469
}
470 }
471
Popular Tags