KickJava   Java API By Example, From Geeks To Geeks.

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


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.Arrays JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.jface.dialogs.IDialogConstants;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.resource.JFaceResources;
22 import org.eclipse.jface.wizard.WizardPage;
23 import org.eclipse.osgi.util.NLS;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Group;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Listener;
32 import org.eclipse.swt.widgets.Text;
33 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
34
35 /**
36  * The common superclass for wizard import and export pages.
37  * <p>
38  * This class is not intended to be subclassed outside of the workbench.
39  * </p>
40  */

41 public abstract class WizardDataTransferPage extends WizardPage implements
42         Listener, IOverwriteQuery {
43
44     // constants
45
protected static final int SIZING_TEXT_FIELD_WIDTH = 250;
46
47     protected static final int COMBO_HISTORY_LENGTH = 5;
48
49     /**
50      * Creates a new wizard page.
51      *
52      * @param pageName the name of the page
53      */

54     protected WizardDataTransferPage(String JavaDoc pageName) {
55         super(pageName);
56     }
57
58     /**
59      * Adds an entry to a history, while taking care of duplicate history items
60      * and excessively long histories. The assumption is made that all histories
61      * should be of length <code>WizardDataTransferPage.COMBO_HISTORY_LENGTH</code>.
62      *
63      * @param history the current history
64      * @param newEntry the entry to add to the history
65      */

66     protected String JavaDoc[] addToHistory(String JavaDoc[] history, String JavaDoc newEntry) {
67         java.util.ArrayList JavaDoc l = new java.util.ArrayList JavaDoc(Arrays.asList(history));
68         addToHistory(l, newEntry);
69         String JavaDoc[] r = new String JavaDoc[l.size()];
70         l.toArray(r);
71         return r;
72     }
73
74     /**
75      * Adds an entry to a history, while taking care of duplicate history items
76      * and excessively long histories. The assumption is made that all histories
77      * should be of length <code>WizardDataTransferPage.COMBO_HISTORY_LENGTH</code>.
78      *
79      * @param history the current history
80      * @param newEntry the entry to add to the history
81      */

82     protected void addToHistory(List JavaDoc history, String JavaDoc newEntry) {
83         history.remove(newEntry);
84         history.add(0, newEntry);
85
86         // since only one new item was added, we can be over the limit
87
// by at most one item
88
if (history.size() > COMBO_HISTORY_LENGTH) {
89             history.remove(COMBO_HISTORY_LENGTH);
90         }
91     }
92
93     /**
94      * Return whether the user is allowed to enter a new container name or just
95      * choose from existing ones.
96      * <p>
97      * Subclasses must implement this method.
98      * </p>
99      *
100      * @return <code>true</code> if new ones are okay, and <code>false</code>
101      * if only existing ones are allowed
102      */

103     protected abstract boolean allowNewContainerName();
104
105     /**
106      * Creates a new label with a bold font.
107      *
108      * @param parent the parent control
109      * @param text the label text
110      * @return the new label control
111      */

112     protected Label createBoldLabel(Composite parent, String JavaDoc text) {
113         Label label = new Label(parent, SWT.NONE);
114         label.setFont(JFaceResources.getBannerFont());
115         label.setText(text);
116         GridData data = new GridData();
117         data.verticalAlignment = GridData.FILL;
118         data.horizontalAlignment = GridData.FILL;
119         label.setLayoutData(data);
120         return label;
121     }
122
123     /**
124      * Creates the import/export options group controls.
125      * <p>
126      * The <code>WizardDataTransferPage</code> implementation of this method does
127      * nothing. Subclasses wishing to define such components should reimplement
128      * this hook method.
129      * </p>
130      *
131      * @param optionsGroup the parent control
132      */

133     protected void createOptionsGroupButtons(Group optionsGroup) {
134     }
135
136     /**
137      * Creates a new label with a bold font.
138      *
139      * @param parent the parent control
140      * @param text the label text
141      * @return the new label control
142      */

143     protected Label createPlainLabel(Composite parent, String JavaDoc text) {
144         Label label = new Label(parent, SWT.NONE);
145         label.setText(text);
146         label.setFont(parent.getFont());
147         GridData data = new GridData();
148         data.verticalAlignment = GridData.FILL;
149         data.horizontalAlignment = GridData.FILL;
150         label.setLayoutData(data);
151         return label;
152     }
153
154     /**
155      * Creates a horizontal spacer line that fills the width of its container.
156      *
157      * @param parent the parent control
158      */

159     protected void createSpacer(Composite parent) {
160         Label spacer = new Label(parent, SWT.NONE);
161         GridData data = new GridData();
162         data.horizontalAlignment = GridData.FILL;
163         data.verticalAlignment = GridData.BEGINNING;
164         spacer.setLayoutData(data);
165     }
166
167     /**
168      * Returns whether this page is complete. This determination is made based upon
169      * the current contents of this page's controls. Subclasses wishing to include
170      * their controls in this determination should override the hook methods
171      * <code>validateSourceGroup</code> and/or <code>validateOptionsGroup</code>.
172      *
173      * @return <code>true</code> if this page is complete, and <code>false</code> if
174      * incomplete
175      * @see #validateSourceGroup
176      * @see #validateOptionsGroup
177      */

178     protected boolean determinePageCompletion() {
179         boolean complete = validateSourceGroup() && validateDestinationGroup()
180                 && validateOptionsGroup();
181
182         // Avoid draw flicker by not clearing the error
183
// message unless all is valid.
184
if (complete) {
185             setErrorMessage(null);
186         }
187
188         return complete;
189     }
190
191     /**
192      * Get a path from the supplied text widget.
193      * @return org.eclipse.core.runtime.IPath
194      */

195     protected IPath getPathFromText(Text textField) {
196         String JavaDoc text = textField.getText();
197         //Do not make an empty path absolute so as not to confuse with the root
198
if (text.length() == 0) {
199             return new Path(text);
200         }
201        
202         return (new Path(text)).makeAbsolute();
203     }
204
205     /**
206      * Queries the user to supply a container resource.
207      *
208      * @return the path to an existing or new container, or <code>null</code> if the
209      * user cancelled the dialog
210      */

211     protected IPath queryForContainer(IContainer initialSelection, String JavaDoc msg) {
212         return queryForContainer(initialSelection, msg, null);
213     }
214
215     /**
216      * Queries the user to supply a container resource.
217      *
218      * @return the path to an existing or new container, or <code>null</code> if the
219      * user cancelled the dialog
220      */

221     protected IPath queryForContainer(IContainer initialSelection, String JavaDoc msg,
222             String JavaDoc title) {
223         ContainerSelectionDialog dialog = new ContainerSelectionDialog(
224                 getControl().getShell(), initialSelection,
225                 allowNewContainerName(), msg);
226         if (title != null) {
227             dialog.setTitle(title);
228         }
229         dialog.showClosedProjects(false);
230         dialog.open();
231         Object JavaDoc[] result = dialog.getResult();
232         if (result != null && result.length == 1) {
233             return (IPath) result[0];
234         }
235         return null;
236     }
237
238     /**
239      * The <code>WizardDataTransfer</code> implementation of this
240      * <code>IOverwriteQuery</code> method asks the user whether the existing
241      * resource at the given path should be overwritten.
242      *
243      * @param pathString
244      * @return the user's reply: one of <code>"YES"</code>, <code>"NO"</code>, <code>"ALL"</code>,
245      * or <code>"CANCEL"</code>
246      */

247     public String JavaDoc queryOverwrite(String JavaDoc pathString) {
248
249         Path path = new Path(pathString);
250
251         String JavaDoc messageString;
252         //Break the message up if there is a file name and a directory
253
//and there are at least 2 segments.
254
if (path.getFileExtension() == null || path.segmentCount() < 2) {
255             messageString = NLS.bind(IDEWorkbenchMessages.WizardDataTransfer_existsQuestion, pathString);
256         } else {
257             messageString = NLS.bind(IDEWorkbenchMessages.WizardDataTransfer_overwriteNameAndPathQuestion, path.lastSegment(),
258             path.removeLastSegments(1).toOSString());
259         }
260
261         final MessageDialog dialog = new MessageDialog(getContainer()
262                 .getShell(), IDEWorkbenchMessages.Question,
263                 null, messageString, MessageDialog.QUESTION, new String JavaDoc[] {
264                         IDialogConstants.YES_LABEL,
265                         IDialogConstants.YES_TO_ALL_LABEL,
266                         IDialogConstants.NO_LABEL,
267                         IDialogConstants.NO_TO_ALL_LABEL,
268                         IDialogConstants.CANCEL_LABEL }, 0);
269         String JavaDoc[] response = new String JavaDoc[] { YES, ALL, NO, NO_ALL, CANCEL };
270         //run in syncExec because callback is from an operation,
271
//which is probably not running in the UI thread.
272
getControl().getDisplay().syncExec(new Runnable JavaDoc() {
273             public void run() {
274                 dialog.open();
275             }
276         });
277         return dialog.getReturnCode() < 0 ? CANCEL : response[dialog
278                 .getReturnCode()];
279     }
280
281     /**
282      * Displays a Yes/No question to the user with the specified message and returns
283      * the user's response.
284      *
285      * @param message the question to ask
286      * @return <code>true</code> for Yes, and <code>false</code> for No
287      */

288     protected boolean queryYesNoQuestion(String JavaDoc message) {
289         MessageDialog dialog = new MessageDialog(getContainer().getShell(),
290                 IDEWorkbenchMessages.Question,
291                 (Image) null, message, MessageDialog.NONE,
292                 new String JavaDoc[] { IDialogConstants.YES_LABEL,
293                         IDialogConstants.NO_LABEL }, 0);
294         // ensure yes is the default
295

296         return dialog.open() == 0;
297     }
298
299     /**
300      * Restores control settings that were saved in the previous instance of this
301      * page.
302      * <p>
303      * The <code>WizardDataTransferPage</code> implementation of this method does
304      * nothing. Subclasses may override this hook method.
305      * </p>
306      */

307     protected void restoreWidgetValues() {
308     }
309
310     /**
311      * Saves control settings that are to be restored in the next instance of
312      * this page.
313      * <p>
314      * The <code>WizardDataTransferPage</code> implementation of this method does
315      * nothing. Subclasses may override this hook method.
316      * </p>
317      */

318     protected void saveWidgetValues() {
319     }
320
321     /**
322      * Determine if the page is complete and update the page appropriately.
323      */

324     protected void updatePageCompletion() {
325         boolean pageComplete = determinePageCompletion();
326         setPageComplete(pageComplete);
327         if (pageComplete) {
328             setErrorMessage(null);
329         }
330     }
331
332     /**
333      * Updates the enable state of this page's controls.
334      * <p>
335      * The <code>WizardDataTransferPage</code> implementation of this method does
336      * nothing. Subclasses may extend this hook method.
337      * </p>
338      */

339     protected void updateWidgetEnablements() {
340     }
341
342     /**
343      * Returns whether this page's destination specification controls currently all
344      * contain valid values.
345      * <p>
346      * The <code>WizardDataTransferPage</code> implementation of this method returns
347      * <code>true</code>. Subclasses may reimplement this hook method.
348      * </p>
349      *
350      * @return <code>true</code> indicating validity of all controls in the
351      * destination specification group
352      */

353     protected boolean validateDestinationGroup() {
354         return true;
355     }
356
357     /**
358      * Returns whether this page's options group's controls currently all contain
359      * valid values.
360      * <p>
361      * The <code>WizardDataTransferPage</code> implementation of this method returns
362      * <code>true</code>. Subclasses may reimplement this hook method.
363      * </p>
364      *
365      * @return <code>true</code> indicating validity of all controls in the options
366      * group
367      */

368     protected boolean validateOptionsGroup() {
369         return true;
370     }
371
372     /**
373      * Returns whether this page's source specification controls currently all
374      * contain valid values.
375      * <p>
376      * The <code>WizardDataTransferPage</code> implementation of this method returns
377      * <code>true</code>. Subclasses may reimplement this hook method.
378      * </p>
379      *
380      * @return <code>true</code> indicating validity of all controls in the
381      * source specification group
382      */

383     protected boolean validateSourceGroup() {
384         return true;
385     }
386
387     /**
388      * Create the options specification widgets.
389      *
390      * @param parent org.eclipse.swt.widgets.Composite
391      */

392     protected void createOptionsGroup(Composite parent) {
393         // options group
394
Group optionsGroup = new Group(parent, SWT.NONE);
395         GridLayout layout = new GridLayout();
396         optionsGroup.setLayout(layout);
397         optionsGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL
398                 | GridData.GRAB_HORIZONTAL));
399         optionsGroup.setText(IDEWorkbenchMessages.WizardExportPage_options);
400         optionsGroup.setFont(parent.getFont());
401
402         createOptionsGroupButtons(optionsGroup);
403
404     }
405
406     /**
407      * Display an error dialog with the specified message.
408      *
409      * @param message the error message
410      */

411     protected void displayErrorDialog(String JavaDoc message) {
412         MessageDialog.openError(getContainer().getShell(),
413                 getErrorDialogTitle(), message);
414     }
415
416     /**
417      * Display an error dislog with the information from the
418      * supplied exception.
419      * @param exception Throwable
420      */

421     protected void displayErrorDialog(Throwable JavaDoc exception) {
422         String JavaDoc message = exception.getMessage();
423         //Some system exceptions have no message
424
if (message == null) {
425             message = NLS.bind(IDEWorkbenchMessages.WizardDataTransfer_exceptionMessage, exception);
426         }
427         displayErrorDialog(message);
428     }
429
430     /**
431      * Get the title for an error dialog. Subclasses should
432      * override.
433      */

434     protected String JavaDoc getErrorDialogTitle() {
435         return IDEWorkbenchMessages.WizardExportPage_internalErrorTitle;
436     }
437
438 }
439
Popular Tags