KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.resources.IContainer;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.resources.IWorkspace;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Event;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.Text;
28 import org.eclipse.swt.widgets.Widget;
29 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
30 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
31
32 /**
33  * The abstract superclass for a typical import wizard's main page.
34  * <p>
35  * Clients may subclass this page to inherit its common destination resource
36  * selection facilities.
37  * </p>
38  * <p>
39  * Subclasses must implement
40  * <ul>
41  * <li><code>createSourceGroup</code></li>
42  * </ul>
43  * </p>
44  * <p>
45  * Subclasses may override
46  * <ul>
47  * <li><code>allowNewContainerName</code></li>
48  * </ul>
49  * </p>
50  * <p>
51  * Subclasses may extend
52  * <ul>
53  * <li><code>handleEvent</code></li>
54  * </ul>
55  * </p>
56  * @deprecated use WizardResourceImportPage
57  */

58 public abstract class WizardImportPage extends WizardDataTransferPage {
59     private IResource currentResourceSelection;
60
61     // initial value stores
62
private String JavaDoc initialContainerFieldValue;
63
64     // widgets
65
private Text containerNameField;
66
67     private Button containerBrowseButton;
68
69     /**
70      * Creates an import wizard page. If the initial resource selection
71      * contains exactly one container resource then it will be used as the default
72      * import destination.
73      *
74      * @param name the name of the page
75      * @param selection the current resource selection
76      */

77     protected WizardImportPage(String JavaDoc name, IStructuredSelection selection) {
78         super(name);
79
80         if (selection.size() == 1) {
81             currentResourceSelection = (IResource) selection.getFirstElement();
82         } else {
83             currentResourceSelection = null;
84         }
85
86         if (currentResourceSelection != null) {
87             if (currentResourceSelection.getType() == IResource.FILE) {
88                 currentResourceSelection = currentResourceSelection.getParent();
89             }
90
91             if (!currentResourceSelection.isAccessible()) {
92                 currentResourceSelection = null;
93             }
94         }
95
96     }
97
98     /**
99      * The <code>WizardImportPage</code> implementation of this
100      * <code>WizardDataTransferPage</code> method returns <code>true</code>.
101      * Subclasses may override this method.
102      */

103     protected boolean allowNewContainerName() {
104         return true;
105     }
106
107     /** (non-Javadoc)
108      * Method declared on IDialogPage.
109      */

110     public void createControl(Composite parent) {
111         Composite composite = new Composite(parent, SWT.NULL);
112         composite.setLayout(new GridLayout());
113         composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL
114                 | GridData.HORIZONTAL_ALIGN_FILL));
115         composite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
116
117         createSourceGroup(composite);
118
119         createSpacer(composite);
120
121         createBoldLabel(composite, IDEWorkbenchMessages.WizardImportPage_destinationLabel);
122         createDestinationGroup(composite);
123
124         createSpacer(composite);
125
126         createBoldLabel(composite, IDEWorkbenchMessages.WizardImportPage_options);
127         createOptionsGroup(composite);
128
129         restoreWidgetValues();
130         updateWidgetEnablements();
131         setPageComplete(determinePageCompletion());
132
133         setControl(composite);
134     }
135
136     /**
137      * Creates the import destination specification controls.
138      *
139      * @param parent the parent control
140      */

141     protected final void createDestinationGroup(Composite parent) {
142         // container specification group
143
Composite containerGroup = new Composite(parent, SWT.NONE);
144         GridLayout layout = new GridLayout();
145         layout.numColumns = 3;
146         containerGroup.setLayout(layout);
147         containerGroup.setLayoutData(new GridData(
148                 GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
149
150         // container label
151
Label resourcesLabel = new Label(containerGroup, SWT.NONE);
152         resourcesLabel.setText(IDEWorkbenchMessages.WizardImportPage_folder);
153
154         // container name entry field
155
containerNameField = new Text(containerGroup, SWT.SINGLE | SWT.BORDER);
156         containerNameField.addListener(SWT.Modify, this);
157         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
158                 | GridData.GRAB_HORIZONTAL);
159         data.widthHint = SIZING_TEXT_FIELD_WIDTH;
160         containerNameField.setLayoutData(data);
161
162         // container browse button
163
containerBrowseButton = new Button(containerGroup, SWT.PUSH);
164         containerBrowseButton.setText(IDEWorkbenchMessages.WizardImportPage_browseLabel);
165         containerBrowseButton.setLayoutData(new GridData(
166                 GridData.HORIZONTAL_ALIGN_FILL));
167         containerBrowseButton.addListener(SWT.Selection, this);
168
169         initialPopulateContainerField();
170     }
171
172     /**
173      * Creates the import source specification controls.
174      * <p>
175      * Subclasses must implement this method.
176      * </p>
177      *
178      * @param parent the parent control
179      */

180     protected abstract void createSourceGroup(Composite parent);
181
182     /**
183      * Display an error dialog with the specified message.
184      *
185      * @param message the error message
186      */

187     protected void displayErrorDialog(String JavaDoc message) {
188         MessageDialog.openError(getContainer().getShell(), IDEWorkbenchMessages.WizardImportPage_errorDialogTitle, message);
189     }
190
191     /**
192      * Returns the path of the container resource specified in the container
193      * name entry field, or <code>null</code> if no name has been typed in.
194      * <p>
195      * The container specified by the full path might not exist and would need to
196      * be created.
197      * </p>
198      *
199      * @return the full path of the container resource specified in
200      * the container name entry field, or <code>null</code>
201      */

202     protected IPath getContainerFullPath() {
203         IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
204
205         //make the path absolute to allow for optional leading slash
206
IPath testPath = getResourcePath();
207
208         IStatus result = workspace.validatePath(testPath.toString(),
209                 IResource.PROJECT | IResource.FOLDER);
210         if (result.isOK()) {
211             return testPath;
212         }
213
214         return null;
215     }
216
217     /**
218      * Return the path for the resource field.
219      * @return org.eclipse.core.runtime.IPath
220      */

221     protected IPath getResourcePath() {
222         return getPathFromText(this.containerNameField);
223     }
224
225     /**
226      * Returns the container resource specified in the container name entry field,
227      * or <code>null</code> if such a container does not exist in the workbench.
228      *
229      * @return the container resource specified in the container name entry field,
230      * or <code>null</code>
231      */

232     protected IContainer getSpecifiedContainer() {
233         IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
234         IPath path = getContainerFullPath();
235         if (workspace.getRoot().exists(path)) {
236             return (IContainer) workspace.getRoot().findMember(path);
237         }
238
239         return null;
240     }
241
242     /**
243      * Opens a container selection dialog and displays the user's subsequent
244      * container resource selection in this page's container name field.
245      */

246     protected void handleContainerBrowseButtonPressed() {
247         // see if the user wishes to modify this container selection
248
IPath containerPath = queryForContainer(getSpecifiedContainer(),
249                 IDEWorkbenchMessages.WizardImportPage_selectFolderLabel);
250
251         // if a container was selected then put its name in the container name field
252
if (containerPath != null) {
253             containerNameField.setText(containerPath.makeRelative().toString());
254         }
255     }
256
257     /**
258      * The <code>WizardImportPage</code> implementation of this
259      * <code>Listener</code> method handles all events and enablements for controls
260      * on this page. Subclasses may extend.
261      */

262     public void handleEvent(Event event) {
263         Widget source = event.widget;
264
265         if (source == containerBrowseButton) {
266             handleContainerBrowseButtonPressed();
267         }
268
269         setPageComplete(determinePageCompletion());
270         updateWidgetEnablements();
271     }
272
273     /**
274      * Sets the initial contents of the container name field.
275      */

276     protected final void initialPopulateContainerField() {
277         if (initialContainerFieldValue != null) {
278             containerNameField.setText(initialContainerFieldValue);
279         } else if (currentResourceSelection != null) {
280             containerNameField.setText(currentResourceSelection.getFullPath()
281                     .toString());
282         }
283     }
284
285     /**
286      * Sets the value of this page's container resource field, or stores
287      * it for future use if this page's controls do not exist yet.
288      *
289      * @param value new value
290      */

291     public void setContainerFieldValue(String JavaDoc value) {
292         if (containerNameField == null) {
293             initialContainerFieldValue = value;
294         } else {
295             containerNameField.setText(value);
296         }
297     }
298
299     /* (non-Javadoc)
300      * Method declared on WizardDataTransferPage.
301      */

302     protected final boolean validateDestinationGroup() {
303         if (getContainerFullPath() == null) {
304             return false;
305         }
306
307         // If the container exist, validate it
308
IContainer container = getSpecifiedContainer();
309         if (container != null) {
310             if (!container.isAccessible()) {
311                 setErrorMessage(IDEWorkbenchMessages.WizardImportPage_folderMustExist);
312                 return false;
313             }
314         }
315
316         return true;
317
318     }
319 }
320
Popular Tags