KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > dialogs > WizardExportPage


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.dialogs;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.StringTokenizer JavaDoc;
18 import java.util.Vector JavaDoc;
19
20 import org.eclipse.core.resources.IContainer;
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.resources.IWorkspace;
25 import org.eclipse.core.resources.IWorkspaceRoot;
26 import org.eclipse.core.resources.ResourcesPlugin;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.IAdaptable;
29 import org.eclipse.core.runtime.IPath;
30 import org.eclipse.core.runtime.IStatus;
31 import org.eclipse.jface.dialogs.IDialogSettings;
32 import org.eclipse.jface.dialogs.MessageDialog;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.jface.viewers.StructuredSelection;
35 import org.eclipse.osgi.util.NLS;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.layout.GridLayout;
39 import org.eclipse.swt.widgets.Button;
40 import org.eclipse.swt.widgets.Combo;
41 import org.eclipse.swt.widgets.Composite;
42 import org.eclipse.swt.widgets.Event;
43 import org.eclipse.swt.widgets.Label;
44 import org.eclipse.swt.widgets.Text;
45 import org.eclipse.swt.widgets.Widget;
46 import org.eclipse.ui.IFileEditorMapping;
47 import org.eclipse.ui.PlatformUI;
48 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
49 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
50
51 /**
52  * Abstract superclass for a typical export wizard's main page.
53  * <p>
54  * Clients may subclass this page to inherit its common destination resource
55  * selection facilities.
56  * </p>
57  * <p>
58  * Subclasses must implement
59  * <ul>
60  * <li><code>createDestinationGroup</code></li>
61  * </ul>
62  * </p>
63  * <p>
64  * Subclasses may override
65  * <ul>
66  * <li><code>allowNewContainerName</code></li>
67  * </ul>
68  * </p>
69  * <p>
70  * Subclasses may extend
71  * <ul>
72  * <li><code>handleEvent</code></li>
73  * <li><code>internalSaveWidgetValues</code></li>
74  * <li><code>updateWidgetEnablements</code></li>
75  * </ul>
76  * </p>
77  * @deprecated use WizardExportResourcePage
78  */

79 public abstract class WizardExportPage extends WizardDataTransferPage {
80     private IStructuredSelection currentResourceSelection;
81
82     private List JavaDoc selectedResources;
83
84     private List JavaDoc selectedTypes;
85
86     private boolean exportCurrentSelection = false;
87
88     private boolean exportAllResourcesPreSet = false;
89
90     // widgets
91
private Combo typesToExportField;
92
93     private Button typesToExportEditButton;
94
95     private Button exportAllTypesRadio;
96
97     private Button exportSpecifiedTypesRadio;
98
99     private Button resourceDetailsButton;
100
101     private Label resourceDetailsDescription;
102
103     private Text resourceNameField;
104
105     private Button resourceBrowseButton;
106
107     // initial value stores
108
private boolean initialExportAllTypesValue = true;
109
110     private String JavaDoc initialExportFieldValue;
111
112     private String JavaDoc initialTypesFieldValue;
113
114     // constants
115
private static final String JavaDoc CURRENT_SELECTION = "<current selection>";//$NON-NLS-1$
116

117     private static final String JavaDoc TYPE_DELIMITER = ",";//$NON-NLS-1$
118

119     // dialog store id constants
120
private static final String JavaDoc STORE_SELECTED_TYPES_ID = "WizardFileSystemExportPage1.STORE_SELECTED_TYPES_ID.";//$NON-NLS-1$
121

122     private static final String JavaDoc STORE_EXPORT_ALL_RESOURCES_ID = "WizardFileSystemExportPage1.STORE_EXPORT_ALL_RESOURCES_ID.";//$NON-NLS-1$
123

124     /**
125      * Creates an export wizard page. If the current resource selection
126      * is not empty then it will be used as the initial collection of resources
127      * selected for export.
128      *
129      * @param pageName the name of the page
130      * @param selection the current resource selection
131      */

132     protected WizardExportPage(String JavaDoc pageName, IStructuredSelection selection) {
133         super(pageName);
134         this.currentResourceSelection = selection;
135     }
136
137     /**
138      * The <code>WizardExportPage</code> implementation of this
139      * <code>WizardDataTransferPage</code> method returns <code>false</code>.
140      * Subclasses may override this method.
141      */

142     protected boolean allowNewContainerName() {
143         return false;
144     }
145
146     /** (non-Javadoc)
147      * Method declared on IDialogPage.
148      */

149     public void createControl(Composite parent) {
150         Composite composite = new Composite(parent, SWT.NULL);
151         composite.setLayout(new GridLayout());
152         composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL
153                 | GridData.HORIZONTAL_ALIGN_FILL));
154
155         createBoldLabel(composite, IDEWorkbenchMessages.WizardExportPage_whatLabel);
156         createSourceGroup(composite);
157
158         createSpacer(composite);
159
160         createBoldLabel(composite, IDEWorkbenchMessages.WizardExportPage_whereLabel);
161         createDestinationGroup(composite);
162
163         createSpacer(composite);
164
165         createBoldLabel(composite, IDEWorkbenchMessages.WizardExportPage_options);
166         createOptionsGroup(composite);
167
168         restoreResourceSpecificationWidgetValues(); // ie.- local
169
restoreWidgetValues(); // ie.- subclass hook
170
if (currentResourceSelection != null) {
171             setupBasedOnInitialSelections();
172         }
173
174         updateWidgetEnablements();
175         setPageComplete(determinePageCompletion());
176
177         setControl(composite);
178     }
179
180     /**
181      * Creates the export destination specification visual components.
182      * <p>
183      * Subclasses must implement this method.
184      * </p>
185      *
186      * @param parent the parent control
187      */

188     protected abstract void createDestinationGroup(Composite parent);
189
190     /**
191      * Creates the export source resource specification controls.
192      *
193      * @param parent the parent control
194      */

195     protected final void createSourceGroup(Composite parent) {
196         // top level group
197
Composite sourceGroup = new Composite(parent, SWT.NONE);
198         GridLayout layout = new GridLayout();
199         layout.numColumns = 3;
200         sourceGroup.setLayout(layout);
201         sourceGroup.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL
202                 | GridData.HORIZONTAL_ALIGN_FILL));
203
204         // resource label
205
new Label(sourceGroup, SWT.NONE).setText(IDEWorkbenchMessages.WizardExportPage_folder);
206
207         // resource name entry field
208
resourceNameField = new Text(sourceGroup, SWT.SINGLE | SWT.BORDER);
209         resourceNameField.addListener(SWT.KeyDown, this);
210         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
211                 | GridData.GRAB_HORIZONTAL);
212         data.widthHint = SIZING_TEXT_FIELD_WIDTH;
213         resourceNameField.setLayoutData(data);
214
215         // resource browse button
216
resourceBrowseButton = new Button(sourceGroup, SWT.PUSH);
217         resourceBrowseButton.setText(IDEWorkbenchMessages.WizardExportPage_browse);
218         resourceBrowseButton.addListener(SWT.Selection, this);
219         resourceBrowseButton.setLayoutData(new GridData(
220                 GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
221
222         // export all types radio
223
exportAllTypesRadio = new Button(sourceGroup, SWT.RADIO);
224         exportAllTypesRadio.setText(IDEWorkbenchMessages.WizardExportPage_allTypes);
225         exportAllTypesRadio.addListener(SWT.Selection, this);
226         data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
227                 | GridData.GRAB_HORIZONTAL);
228         data.horizontalSpan = 3;
229         exportAllTypesRadio.setLayoutData(data);
230
231         // export specific types radio
232
exportSpecifiedTypesRadio = new Button(sourceGroup, SWT.RADIO);
233         exportSpecifiedTypesRadio.setText(IDEWorkbenchMessages.WizardExportPage_specificTypes);
234         exportSpecifiedTypesRadio.addListener(SWT.Selection, this);
235
236         // types combo
237
typesToExportField = new Combo(sourceGroup, SWT.NONE);
238         data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
239                 | GridData.GRAB_HORIZONTAL);
240         data.widthHint = SIZING_TEXT_FIELD_WIDTH;
241         typesToExportField.setLayoutData(data);
242         typesToExportField.addListener(SWT.Modify, this);
243
244         // types edit button
245
typesToExportEditButton = new Button(sourceGroup, SWT.PUSH);
246         typesToExportEditButton.setText(IDEWorkbenchMessages.WizardExportPage_edit);
247         typesToExportEditButton.setLayoutData(new GridData(
248                 GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL
249                         | GridData.VERTICAL_ALIGN_END));
250         typesToExportEditButton.addListener(SWT.Selection, this);
251
252         // details button
253
resourceDetailsButton = new Button(sourceGroup, SWT.PUSH);
254         resourceDetailsButton.setText(IDEWorkbenchMessages.WizardExportPage_details);
255         resourceDetailsButton.addListener(SWT.Selection, this);
256
257         // details label
258
resourceDetailsDescription = new Label(sourceGroup, SWT.NONE);
259         data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
260                 | GridData.GRAB_HORIZONTAL);
261         data.horizontalSpan = 2;
262         resourceDetailsDescription.setLayoutData(data);
263
264         // initial setup
265
resetSelectedResources();
266         exportAllTypesRadio.setSelection(initialExportAllTypesValue);
267         exportSpecifiedTypesRadio.setSelection(!initialExportAllTypesValue);
268         typesToExportField.setEnabled(!initialExportAllTypesValue);
269         typesToExportEditButton.setEnabled(!initialExportAllTypesValue);
270
271         if (initialExportFieldValue != null) {
272             resourceNameField.setText(initialExportFieldValue);
273         }
274         if (initialTypesFieldValue != null) {
275             typesToExportField.setText(initialTypesFieldValue);
276         }
277     }
278
279     /**
280      * Display an error dialog with the specified message.
281      *
282      * @param message the error message
283      */

284     protected void displayErrorDialog(String JavaDoc message) {
285         MessageDialog.openError(getContainer().getShell(), IDEWorkbenchMessages.WizardExportPage_errorDialogTitle, message);
286     }
287
288     /**
289      * Displays a description message that indicates a selection of resources
290      * of the specified size.
291      *
292      * @param selectedResourceCount the resource selection size to display
293      */

294     protected void displayResourcesSelectedCount(int selectedResourceCount) {
295         if (selectedResourceCount == 1) {
296             resourceDetailsDescription.setText(IDEWorkbenchMessages.WizardExportPage_oneResourceSelected);
297         } else {
298             resourceDetailsDescription
299                     .setText(NLS.bind(IDEWorkbenchMessages.WizardExportPage_resourceCountMessage, new Integer JavaDoc(selectedResourceCount)));
300         }
301     }
302
303     /**
304      * Obsolete method. This was implemented to handle the case where ensureLocal()
305      * needed to be called but it doesn't use it any longer.
306      *
307      * @param resources the list of resources to ensure locality for
308      * @return <code>true</code> for successful completion
309      * @deprecated Only retained for backwards compatibility.
310      */

311     protected boolean ensureResourcesLocal(List JavaDoc resources) {
312         return true;
313     }
314
315     /**
316      * Returns a new subcollection containing only those resources which are not
317      * local.
318      *
319      * @param originalList the original list of resources (element type:
320      * <code>IResource</code>)
321      * @return the new list of non-local resources (element type:
322      * <code>IResource</code>)
323      */

324     protected List JavaDoc extractNonLocalResources(List JavaDoc originalList) {
325         Vector JavaDoc result = new Vector JavaDoc(originalList.size());
326         Iterator JavaDoc resourcesEnum = originalList.iterator();
327
328         while (resourcesEnum.hasNext()) {
329             IResource currentResource = (IResource) resourcesEnum.next();
330             if (!currentResource.isLocal(IResource.DEPTH_ZERO)) {
331                 result.addElement(currentResource);
332             }
333         }
334
335         return result;
336     }
337
338     /**
339      * Returns the current selection value of the "Export all types" radio,
340      * or its set initial value if it does not exist yet.
341      *
342      * @return the "Export All Types" radio's current value or anticipated initial
343      * value
344      */

345     public boolean getExportAllTypesValue() {
346         if (exportAllTypesRadio == null) {
347             return initialExportAllTypesValue;
348         }
349
350         return exportAllTypesRadio.getSelection();
351     }
352
353     /**
354      * Returns the current contents of the resource name entry field,
355      * or its set initial value if it does not exist yet (which could
356      * be <code>null</code>).
357      *
358      * @return the resource name field's current value or anticipated initial value,
359      * or <code>null</code>
360      */

361     public String JavaDoc getResourceFieldValue() {
362         if (resourceNameField == null) {
363             return initialExportFieldValue;
364         }
365
366         return resourceNameField.getText();
367     }
368
369     /**
370      * Return the path for the resource field.
371      * @return org.eclipse.core.runtime.IPath
372      */

373     protected IPath getResourcePath() {
374         return getPathFromText(this.resourceNameField);
375     }
376
377     /**
378      * Returns this page's collection of currently-specified resources to be
379      * exported. This is the primary resource selection facility accessor for
380      * subclasses.
381      *
382      * @return the collection of resources currently selected for export (element
383      * type: <code>IResource</code>)
384      */

385     protected List JavaDoc getSelectedResources() {
386         if (selectedResources == null) {
387             IResource sourceResource = getSourceResource();
388
389             if (sourceResource != null) {
390                 selectAppropriateResources(sourceResource);
391             }
392         }
393
394         return selectedResources;
395     }
396
397     /**
398      * Returns the resource object specified in the resource name entry field,
399      * or <code>null</code> if such a resource does not exist in the workbench.
400      *
401      * @return the resource specified in the resource name entry field, or
402      * <code>null</code>
403      */

404     protected IResource getSourceResource() {
405         IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
406         //make the path absolute to allow for optional leading slash
407
IPath testPath = getResourcePath();
408
409         IStatus result = workspace.validatePath(testPath.toString(),
410                 IResource.ROOT | IResource.PROJECT | IResource.FOLDER
411                         | IResource.FILE);
412
413         if (result.isOK() && workspace.getRoot().exists(testPath)) {
414             return workspace.getRoot().findMember(testPath);
415         }
416
417         return null;
418     }
419
420     /**
421      * Returns the current contents of the types entry field, or its set
422      * initial value if it does not exist yet (which could be <code>null</code>).
423      *
424      * @return the types entry field's current value or anticipated initial value,
425      * or <code>null</code>
426      */

427     public String JavaDoc getTypesFieldValue() {
428         if (typesToExportField == null) {
429             return initialTypesFieldValue;
430         }
431
432         return typesToExportField.getText();
433     }
434
435     /**
436      * Returns the resource extensions currently specified to be exported.
437      *
438      * @return the resource extensions currently specified to be exported (element
439      * type: <code>String</code>)
440      */

441     protected List JavaDoc getTypesToExport() {
442         List JavaDoc result = new ArrayList JavaDoc();
443         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(typesToExportField
444                 .getText(), TYPE_DELIMITER);
445
446         while (tokenizer.hasMoreTokens()) {
447             String JavaDoc currentExtension = tokenizer.nextToken().trim();
448             if (!currentExtension.equals("")) { //$NON-NLS-1$
449
result.add(currentExtension);
450             }
451         }
452
453         return result;
454     }
455
456     /**
457      * The <code>WizardExportPage</code> implementation of this
458      * <code>Listener</code> method handles all events and enablements for controls
459      * on this page. Subclasses may extend.
460      */

461     public void handleEvent(Event event) {
462         Widget source = event.widget;
463
464         if (source == exportAllTypesRadio || source == typesToExportField
465                 || source == resourceNameField) {
466             resetSelectedResources();
467         } else if (source == exportSpecifiedTypesRadio) {
468             resetSelectedResources();
469             typesToExportField.setFocus();
470         } else if (source == resourceDetailsButton) {
471             handleResourceDetailsButtonPressed();
472         } else if (source == resourceBrowseButton) {
473             handleResourceBrowseButtonPressed();
474         } else if (source == typesToExportEditButton) {
475             handleTypesEditButtonPressed();
476         }
477
478         setPageComplete(determinePageCompletion());
479         updateWidgetEnablements();
480     }
481
482     /**
483      * Opens a container selection dialog and displays the user's subsequent
484      * container selection in this page's resource name field.
485      */

486     protected void handleResourceBrowseButtonPressed() {
487         IResource currentFolder = getSourceResource();
488         if (currentFolder != null && currentFolder.getType() == IResource.FILE) {
489             currentFolder = currentFolder.getParent();
490         }
491
492         IPath containerPath = queryForContainer((IContainer) currentFolder,
493                 IDEWorkbenchMessages.WizardExportPage_selectResourcesToExport);
494         if (containerPath != null) { // null means user cancelled
495
String JavaDoc relativePath = containerPath.makeRelative().toString();
496             if (!relativePath.toString().equals(resourceNameField.getText())) {
497                 resetSelectedResources();
498                 resourceNameField.setText(relativePath);
499             }
500         }
501     }
502
503     /**
504      * Opens a resource selection dialog and records the user's subsequent
505      * resource selections.
506      */

507     protected void handleResourceDetailsButtonPressed() {
508         IAdaptable source = getSourceResource();
509
510         if (source == null) {
511             source = ResourcesPlugin.getWorkspace().getRoot();
512         }
513
514         selectAppropriateResources(source);
515
516         if (source instanceof IFile) {
517             source = ((IFile) source).getParent();
518             setResourceToDisplay((IResource) source);
519         }
520
521         Object JavaDoc[] newlySelectedResources = queryIndividualResourcesToExport(source);
522
523         if (newlySelectedResources != null) {
524             selectedResources = Arrays.asList(newlySelectedResources);
525             displayResourcesSelectedCount(selectedResources.size());
526         }
527     }
528
529     /**
530      * Queries the user for the types of resources to be exported and
531      * displays these types in this page's "Types to export" field.
532      */

533     protected void handleTypesEditButtonPressed() {
534         Object JavaDoc[] newSelectedTypes = queryResourceTypesToExport();
535
536         if (newSelectedTypes != null) { // ie.- did not press Cancel
537
List JavaDoc result = new ArrayList JavaDoc(newSelectedTypes.length);
538             for (int i = 0; i < newSelectedTypes.length; i++) {
539                 result.add(((IFileEditorMapping) newSelectedTypes[i])
540                         .getExtension());
541             }
542             setTypesToExport(result);
543         }
544     }
545
546     /**
547      * Returns whether the extension of the given resource name is an extension that
548      * has been specified for export by the user.
549      *
550      * @param resourceName the resource name
551      * @return <code>true</code> if the resource name is suitable for export based
552      * upon its extension
553      */

554     protected boolean hasExportableExtension(String JavaDoc resourceName) {
555         if (selectedTypes == null) {
556             return true;
557         }
558
559         int separatorIndex = resourceName.lastIndexOf(".");//$NON-NLS-1$
560
if (separatorIndex == -1) {
561             return false;
562         }
563
564         String JavaDoc extension = resourceName.substring(separatorIndex + 1);
565
566         Iterator JavaDoc it = selectedTypes.iterator();
567         while (it.hasNext()) {
568             if (extension.equalsIgnoreCase((String JavaDoc) it.next())) {
569                 return true;
570             }
571         }
572
573         return false;
574     }
575
576     /**
577      * Persists additional setting that are to be restored in the next instance of
578      * this page.
579      * <p>
580      * The <code>WizardImportPage</code> implementation of this method does
581      * nothing. Subclasses may extend to persist additional settings.
582      * </p>
583      */

584     protected void internalSaveWidgetValues() {
585     }
586
587     /**
588      * Queries the user for the individual resources that are to be exported
589      * and returns these resources as a collection.
590      *
591      * @param rootResource the resource to use as the root of the selection query
592      * @return the resources selected for export (element type:
593      * <code>IResource</code>), or <code>null</code> if the user canceled the
594      * selection
595      */

596     protected Object JavaDoc[] queryIndividualResourcesToExport(IAdaptable rootResource) {
597         ResourceSelectionDialog dialog = new ResourceSelectionDialog(
598                 getContainer().getShell(), rootResource, IDEWorkbenchMessages.WizardExportPage_selectResourcesTitle);
599         dialog.setInitialSelections(selectedResources
600                 .toArray(new Object JavaDoc[selectedResources.size()]));
601         dialog.open();
602         return dialog.getResult();
603     }
604
605     /**
606      * Queries the user for the resource types that are to be exported and returns
607      * these types as a collection.
608      *
609      * @return the resource types selected for export (element type:
610      * <code>String</code>), or <code>null</code> if the user canceled the
611      * selection
612      */

613     protected Object JavaDoc[] queryResourceTypesToExport() {
614         IFileEditorMapping editorMappings[] = PlatformUI.getWorkbench()
615                 .getEditorRegistry().getFileEditorMappings();
616
617         int mappingsSize = editorMappings.length;
618         List JavaDoc selectedTypes = getTypesToExport();
619         List JavaDoc initialSelections = new ArrayList JavaDoc(selectedTypes.size());
620
621         for (int i = 0; i < mappingsSize; i++) {
622             IFileEditorMapping currentMapping = editorMappings[i];
623             if (selectedTypes.contains(currentMapping.getExtension())) {
624                 initialSelections.add(currentMapping);
625             }
626         }
627
628         ListSelectionDialog dialog = new ListSelectionDialog(getContainer()
629                 .getShell(), editorMappings,
630                 FileEditorMappingContentProvider.INSTANCE,
631                 FileEditorMappingLabelProvider.INSTANCE, IDEWorkbenchMessages.WizardExportPage_selectionDialogMessage);
632
633         dialog.setTitle(IDEWorkbenchMessages.WizardExportPage_resourceTypeDialog);
634         dialog.open();
635
636         return dialog.getResult();
637     }
638
639     /**
640      * Resets this page's selected resources collection and updates its controls
641      * accordingly.
642      */

643     protected void resetSelectedResources() {
644         resourceDetailsDescription.setText(IDEWorkbenchMessages.WizardExportPage_detailsMessage);
645         selectedResources = null;
646
647         if (exportCurrentSelection) {
648             exportCurrentSelection = false;
649
650             if (resourceNameField.getText().length() > CURRENT_SELECTION
651                     .length()) {
652                 resourceNameField.setText(resourceNameField.getText()
653                         .substring(CURRENT_SELECTION.length()));
654             } else {
655                 resourceNameField.setText("");//$NON-NLS-1$
656
}
657         }
658     }
659
660     /**
661      * Restores resource specification control settings that were persisted
662      * in the previous instance of this page. Subclasses wishing to restore
663      * persisted values for their controls may extend.
664      */

665     protected void restoreResourceSpecificationWidgetValues() {
666         IDialogSettings settings = getDialogSettings();
667         if (settings != null) {
668             String JavaDoc pageName = getName();
669             boolean exportAllResources = settings
670                     .getBoolean(STORE_EXPORT_ALL_RESOURCES_ID + pageName);
671
672             // restore all/typed radio values iff not already explicitly set
673
if (!exportAllResourcesPreSet) {
674                 exportAllTypesRadio.setSelection(exportAllResources);
675                 exportSpecifiedTypesRadio.setSelection(!exportAllResources);
676             }
677
678             // restore selected types iff not explicitly already set
679
if (initialTypesFieldValue == null) {
680                 String JavaDoc[] selectedTypes = settings
681                         .getArray(STORE_SELECTED_TYPES_ID + pageName);
682                 if (selectedTypes != null) {
683                     if (selectedTypes.length > 0) {
684                         typesToExportField.setText(selectedTypes[0]);
685                     }
686                     for (int i = 0; i < selectedTypes.length; i++) {
687                         typesToExportField.add(selectedTypes[i]);
688                     }
689                 }
690             }
691         }
692     }
693
694     /**
695      * Persists resource specification control setting that are to be restored
696      * in the next instance of this page. Subclasses wishing to persist additional
697      * setting for their controls should extend hook method
698      * <code>internalSaveWidgetValues</code>.
699      */

700     protected void saveWidgetValues() {
701         IDialogSettings settings = getDialogSettings();
702         if (settings != null) {
703             String JavaDoc pageName = getName();
704
705             // update specific types to export history
706
String JavaDoc[] selectedTypesNames = settings
707                     .getArray(STORE_SELECTED_TYPES_ID + pageName);
708             if (selectedTypesNames == null) {
709                 selectedTypesNames = new String JavaDoc[0];
710             }
711
712             if (exportSpecifiedTypesRadio.getSelection()) {
713                 selectedTypesNames = addToHistory(selectedTypesNames,
714                         typesToExportField.getText());
715             }
716
717             settings
718                     .put(STORE_SELECTED_TYPES_ID + pageName, selectedTypesNames);
719
720             // radio buttons
721
settings.put(STORE_EXPORT_ALL_RESOURCES_ID + pageName,
722                     exportAllTypesRadio.getSelection());
723         }
724
725         // allow subclasses to save values
726
internalSaveWidgetValues();
727
728     }
729
730     /**
731      * Records a container's recursive file descendents which have an extension
732      * that has been specified for export by the user.
733      *
734      * @param resource the parent container
735      */

736     protected void selectAppropriateFolderContents(IContainer resource) {
737         try {
738             IResource[] members = resource.members();
739
740             for (int i = 0; i < members.length; i++) {
741                 if (members[i].getType() == IResource.FILE) {
742                     IFile currentFile = (IFile) members[i];
743                     if (hasExportableExtension(currentFile.getFullPath()
744                             .toString())) {
745                         selectedResources.add(currentFile);
746                     }
747                 }
748                 if (members[i].getType() == IResource.FOLDER) {
749                     selectAppropriateFolderContents((IContainer) members[i]);
750                 }
751             }
752         } catch (CoreException e) {
753             //don't show children if there are errors -- should at least log this
754
}
755     }
756
757     /**
758      * Records a resource's recursive descendents which are appropriate
759      * for export based upon this page's current controls contents.
760      *
761      * @param resource the parent resource
762      */

763     protected void selectAppropriateResources(Object JavaDoc resource) {
764         if (selectedResources == null) {
765
766             if (exportSpecifiedTypesRadio.getSelection()) {
767                 selectedTypes = getTypesToExport();
768             } else {
769                 selectedTypes = null; // sentinel for select all extensions
770
}
771
772             selectedResources = new ArrayList JavaDoc();
773             if (resource instanceof IWorkspaceRoot) {
774                 IProject[] projects = ((IWorkspaceRoot) resource).getProjects();
775                 for (int i = 0; i < projects.length; i++) {
776                     selectAppropriateFolderContents(projects[i]);
777                 }
778             } else if (resource instanceof IFile) {
779                 IFile file = (IFile) resource;
780                 if (hasExportableExtension(file.getFullPath().toString())) {
781                     selectedResources.add(file);
782                 }
783             } else {
784                 selectAppropriateFolderContents((IContainer) resource);
785             }
786         }
787     }
788
789     /**
790      * Sets the selection value of this page's "Export all types" radio, or stores
791      * it for future use if this visual component does not exist yet.
792      *
793      * @param value new selection value
794      */

795     public void setExportAllTypesValue(boolean value) {
796         if (exportAllTypesRadio == null) {
797             initialExportAllTypesValue = value;
798             exportAllResourcesPreSet = true;
799         } else {
800             exportAllTypesRadio.setSelection(value);
801             exportSpecifiedTypesRadio.setSelection(!value);
802         }
803     }
804
805     /**
806      * Sets the value of this page's source resource field, or stores
807      * it for future use if this visual component does not exist yet.
808      *
809      * @param value new value
810      */

811     public void setResourceFieldValue(String JavaDoc value) {
812         if (resourceNameField == null) {
813             initialExportFieldValue = value;
814         } else {
815             resourceNameField.setText(value);
816         }
817     }
818
819     /**
820      * Set the resource whos name we will display.
821      * @param resource
822      */

823     protected void setResourceToDisplay(IResource resource) {
824         setResourceFieldValue(resource.getFullPath().makeRelative().toString());
825     }
826
827     /**
828      * Sets the value of this page's "Types to export" field, or stores
829      * it for future use if this visual component does not exist yet.
830      *
831      * @param value new value
832      */

833     public void setTypesFieldValue(String JavaDoc value) {
834         if (typesToExportField == null) {
835             initialTypesFieldValue = value;
836         } else {
837             typesToExportField.setText(value);
838         }
839     }
840
841     /**
842      * Sets the value of this page's "Types to export" field based upon the
843      * collection of extensions.
844      *
845      * @param typeStrings the collection of extensions to populate the "Types to
846      * export" field with (element type: <code>String</code>)
847      */

848     protected void setTypesToExport(List JavaDoc typeStrings) {
849         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
850         Iterator JavaDoc typesEnum = typeStrings.iterator();
851
852         while (typesEnum.hasNext()) {
853             result.append(typesEnum.next());
854             result.append(TYPE_DELIMITER);
855             result.append(" ");//$NON-NLS-1$
856
}
857
858         typesToExportField.setText(result.toString());
859     }
860
861     /**
862      * Populates the resource name field based upon the currently selected resources.
863      */

864     protected void setupBasedOnInitialSelections() {
865         if (initialExportFieldValue != null) {
866             // a source resource has been programatically specified, which overrides
867
// the current workbench resource selection
868
IResource specifiedSourceResource = getSourceResource();
869             if (specifiedSourceResource == null) {
870                 currentResourceSelection = new StructuredSelection();
871             } else {
872                 currentResourceSelection = new StructuredSelection(
873                         specifiedSourceResource);
874             }
875         }
876
877         if (currentResourceSelection.isEmpty()) {
878             return; // no setup needed
879
}
880
881         List JavaDoc selections = new ArrayList JavaDoc();
882         Iterator JavaDoc it = currentResourceSelection.iterator();
883         while (it.hasNext()) {
884             IResource currentResource = (IResource) it.next();
885             // do not add inaccessible elements
886
if (currentResource.isAccessible()) {
887                 selections.add(currentResource);
888             }
889         }
890
891         if (selections.isEmpty()) {
892             return; // setup not needed anymore
893
}
894
895         int selectedResourceCount = selections.size();
896         if (selectedResourceCount == 1) {
897             IResource resource = (IResource) selections.get(0);
898             setResourceToDisplay(resource);
899         } else {
900             selectedResources = selections;
901             exportAllTypesRadio.setSelection(true);
902             exportSpecifiedTypesRadio.setSelection(false);
903             resourceNameField.setText(CURRENT_SELECTION);
904             exportCurrentSelection = true;
905             displayResourcesSelectedCount(selectedResourceCount);
906         }
907     }
908
909     /**
910      * Updates the enablements of this page's controls. Subclasses may extend.
911      */

912     protected void updateWidgetEnablements() {
913         if (exportCurrentSelection) {
914             resourceDetailsButton.setEnabled(true);
915         } else {
916             IResource resource = getSourceResource();
917             resourceDetailsButton.setEnabled(resource != null
918                     && resource.isAccessible());
919         }
920
921         exportSpecifiedTypesRadio.setEnabled(!exportCurrentSelection);
922         typesToExportField.setEnabled(exportSpecifiedTypesRadio.getSelection());
923         typesToExportEditButton.setEnabled(exportSpecifiedTypesRadio
924                 .getSelection());
925     }
926
927     /* (non-Javadoc)
928      * Method declared on WizardDataTransferPage.
929      */

930     protected final boolean validateSourceGroup() {
931         if (exportCurrentSelection) {
932             return true;
933         }
934
935         String JavaDoc sourceString = resourceNameField.getText();
936         if (sourceString.equals("")) {//$NON-NLS-1$
937
setErrorMessage(null);
938             return false;
939         }
940
941         IResource resource = getSourceResource();
942
943         if (resource == null) {
944             setErrorMessage(IDEWorkbenchMessages.WizardExportPage_mustExistMessage);
945             return false;
946         }
947
948         if (!resource.isAccessible()) {
949             setErrorMessage(IDEWorkbenchMessages.WizardExportPage_mustBeAccessibleMessage);
950             return false;
951         }
952
953         return true;
954     }
955 }
956
Popular Tags