KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > wizards > preferences > WizardPreferencesPage


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.wizards.preferences;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.Hashtable JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.core.runtime.preferences.ConfigurationScope;
24 import org.eclipse.core.runtime.preferences.IPreferenceFilter;
25 import org.eclipse.core.runtime.preferences.InstanceScope;
26 import org.eclipse.jface.dialogs.Dialog;
27 import org.eclipse.jface.dialogs.IDialogConstants;
28 import org.eclipse.jface.dialogs.IDialogSettings;
29 import org.eclipse.jface.dialogs.MessageDialog;
30 import org.eclipse.jface.resource.ImageDescriptor;
31 import org.eclipse.jface.wizard.WizardPage;
32 import org.eclipse.osgi.util.NLS;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.events.SelectionAdapter;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.events.SelectionListener;
37 import org.eclipse.swt.graphics.Font;
38 import org.eclipse.swt.graphics.Image;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.widgets.Button;
42 import org.eclipse.swt.widgets.Combo;
43 import org.eclipse.swt.widgets.Composite;
44 import org.eclipse.swt.widgets.Event;
45 import org.eclipse.swt.widgets.FileDialog;
46 import org.eclipse.swt.widgets.Group;
47 import org.eclipse.swt.widgets.Label;
48 import org.eclipse.swt.widgets.Listener;
49 import org.eclipse.swt.widgets.Shell;
50 import org.eclipse.swt.widgets.Table;
51 import org.eclipse.swt.widgets.TableItem;
52 import org.eclipse.swt.widgets.Text;
53 import org.eclipse.swt.widgets.Widget;
54 import org.eclipse.ui.dialogs.IOverwriteQuery;
55 import org.eclipse.ui.internal.WorkbenchPlugin;
56 import org.eclipse.ui.internal.preferences.PreferenceTransferElement;
57 import org.eclipse.ui.internal.preferences.PreferenceTransferManager;
58
59 /**
60  * Base class for preference export/import pages.
61  *
62  * @since 3.1
63  */

64 public abstract class WizardPreferencesPage extends WizardPage implements
65         Listener, IOverwriteQuery {
66
67     // widgets
68
protected Combo destinationNameField;
69
70     // constants
71
private Button destinationBrowseButton;
72
73     private Button overwriteExistingFilesCheckbox;
74
75     protected Table transfersTable;
76     
77     protected Text text;
78
79     private Composite buttonComposite;
80
81     private Button allButton;
82
83     protected Button chooseImportsButton;
84
85     private Group group;
86
87     // dialog store id constants
88
private static final String JavaDoc STORE_DESTINATION_NAMES_ID = "WizardPreferencesExportPage1.STORE_DESTINATION_NAMES_ID";//$NON-NLS-1$
89

90     private static final String JavaDoc STORE_OVERWRITE_EXISTING_FILES_ID = "WizardPreferencesExportPage1.STORE_OVERWRITE_EXISTING_FILES_ID";//$NON-NLS-1$
91

92     private static final String JavaDoc TRANSFER_ALL_PREFERENCES_ID = "WizardPreferencesExportPage1.EXPORT_ALL_PREFERENCES_ID"; //$NON-NLS-1$
93

94     private Hashtable JavaDoc imageTable;
95
96     private PreferenceTransferElement[] transfers;
97
98     private String JavaDoc currentMessage;
99
100     private static final String JavaDoc STORE_DESTINATION_ID = null;
101
102     protected static final int COMBO_HISTORY_LENGTH = 5;
103     
104     /**
105      * @param pageName
106      */

107     protected WizardPreferencesPage(String JavaDoc pageName) {
108         super(pageName);
109     }
110
111     /**
112      * Creates a new button with the given id.
113      * <p>
114      * The <code>Dialog</code> implementation of this framework method creates
115      * a standard push button, registers for selection events including button
116      * presses and registers default buttons with its shell. The button id is
117      * stored as the buttons client data. Note that the parent's layout is
118      * assumed to be a GridLayout and the number of columns in this layout is
119      * incremented. Subclasses may override.
120      * </p>
121      *
122      * @param parent
123      * the parent composite
124      * @param id
125      * the id of the button (see <code>IDialogConstants.*_ID</code>
126      * constants for standard dialog button ids)
127      * @param label
128      * the label from the button
129      * @param defaultButton
130      * <code>true</code> if the button is to be the default button,
131      * and <code>false</code> otherwise
132      */

133     protected Button createButton(Composite parent, int id, String JavaDoc label,
134             boolean defaultButton) {
135         // increment the number of columns in the button bar
136
((GridLayout) parent.getLayout()).numColumns++;
137
138         Button button = new Button(parent, SWT.PUSH);
139         button.setFont(parent.getFont());
140
141         GridData buttonData = new GridData(GridData.FILL_HORIZONTAL);
142         button.setLayoutData(buttonData);
143
144         button.setData(new Integer JavaDoc(id));
145         button.setText(label);
146
147         if (defaultButton) {
148             Shell shell = parent.getShell();
149             if (shell != null) {
150                 shell.setDefaultButton(button);
151             }
152             button.setFocus();
153         }
154         return button;
155     }
156
157     /**
158      * Add the passed value to self's destination widget's history
159      *
160      * @param value
161      * java.lang.String
162      */

163     protected void addDestinationItem(String JavaDoc value) {
164         destinationNameField.add(value);
165     }
166
167     /**
168      * (non-Javadoc) Method declared on IDialogPage.
169      */

170     public void createControl(Composite parent) {
171         initializeDialogUnits(parent);
172         Composite composite = new Composite(parent, SWT.NULL);
173         composite.setLayout(new GridLayout());
174         composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL
175                 | GridData.HORIZONTAL_ALIGN_FILL));
176         
177
178         createTransferArea(composite);
179
180         restoreWidgetValues();
181         // updateWidgetEnablements();
182

183         // can not finish initially, but don't want to start with an error
184
// message either
185
if (!(validDestination() && validateOptionsGroup() && validateSourceGroup())) {
186             setPageComplete(false);
187         }
188
189         setPreferenceTransfers();
190         setControl(composite);
191
192         giveFocusToDestination();
193         Dialog.applyDialogFont(composite);
194     }
195
196     /**
197      * @param composite
198      */

199     protected abstract void createTransferArea(Composite composite);
200
201     /**
202      * Validate the destination group.
203      * @return <code>true</code> if the group is valid. If
204      * not set the error message and return <code>false</code>.
205      */

206     protected boolean validateDestinationGroup() {
207         if (!validDestination()) {
208             currentMessage = getInvalidDestinationMessage();
209             return false;
210         }
211
212         return true;
213     }
214
215     /**
216      * Return the message that indicates an invalid destination.
217      * @return String
218      */

219     abstract protected String JavaDoc getInvalidDestinationMessage();
220
221     private String JavaDoc getNoOptionsMessage() {
222         return PreferencesMessages.WizardPreferencesPage_noOptionsSelected;
223     }
224     
225     protected boolean validDestination() {
226         File JavaDoc file = new File JavaDoc(getDestinationValue());
227         return !(file.getPath().length() <= 0 || file.isDirectory());
228     }
229
230     /**
231      *
232      */

233     protected void setPreferenceTransfers() {
234         PreferenceTransferElement[] transfers = getTransfers();
235         transfersTable.removeAll();
236         for (int i = 0; i < transfers.length; i++) {
237             PreferenceTransferElement element = transfers[i];
238             createItem(element, transfersTable);
239         }
240     }
241
242     /*
243      * return the PreferenceTransgerElements specified
244      */

245     protected PreferenceTransferElement[] getTransfers() {
246         if (transfers == null) {
247             transfers = PreferenceTransferManager.getPreferenceTransfers();
248         }
249         return transfers;
250     }
251
252     /**
253      * @param element
254      * @param table
255      */

256     private void createItem(PreferenceTransferElement element, Table table) {
257         TableItem item = new TableItem(table, SWT.CHECK);
258         item.setText(element.getName());
259         item.setData(element);
260         ImageDescriptor descriptor = element.getImageDescriptor();
261         Image image = null;
262         if (descriptor != null) {
263             Hashtable JavaDoc images = getImageTable();
264             image = (Image) images.get(descriptor);
265             if (image == null) {
266                 image = descriptor.createImage();
267                 images.put(descriptor, image);
268             }
269             item.setImage(image);
270         }
271
272     }
273
274     /**
275      * @return <code>Hashtable</code> the table of images
276      */

277     private Hashtable JavaDoc getImageTable() {
278         if (imageTable == null) {
279             imageTable = new Hashtable JavaDoc(10);
280         }
281         return imageTable;
282     }
283
284     /**
285      * @param composite
286      */

287     protected void createTransfersList(Composite composite) {
288
289         allButton = new Button(composite, SWT.RADIO);
290         allButton.setText(getAllButtonText());
291         
292         chooseImportsButton = new Button(composite, SWT.RADIO);
293         chooseImportsButton.setText(getChooseButtonText());
294         
295         group = new Group(composite, SWT.NONE);
296         group.setText(PreferencesMessages.WizardPreferencesExportPage1_preferences);
297         GridData data = new GridData(GridData.FILL_BOTH);
298         data.horizontalSpan = 2;
299         group.setLayoutData(data);
300
301         GridLayout layout = new GridLayout();
302         group.setLayout(layout);
303         
304         transfersTable = new Table(group, SWT.CHECK | SWT.BORDER);
305         transfersTable.setLayoutData(new GridData(GridData.FILL_BOTH));
306         
307         Label description = new Label(group, SWT.NONE);
308         description.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
309         description.setText(PreferencesMessages.WizardPreferences_description);
310         
311         text = new Text(group, SWT.V_SCROLL | SWT.READ_ONLY
312                 | SWT.BORDER | SWT.WRAP);
313         text.setLayoutData(new GridData(GridData.FILL_BOTH));
314         
315         SelectionListener selection = new SelectionListener() {
316
317             public void widgetSelected(SelectionEvent e) {
318                 // Selecting an item in the list forces
319
// the radio buttons to get selected
320
if (e.widget == transfersTable) {
321                     updateState(e);
322                     updateDescription();
323                 }
324                 updatePageCompletion();
325             }
326
327             private void updateState(SelectionEvent e) {
328                 if (((TableItem)e.item).getChecked()) {
329                     allButton.setSelection(false);
330                     chooseImportsButton.setSelection(true);
331                 }
332             }
333
334             public void widgetDefaultSelected(SelectionEvent e) {
335                 widgetSelected(e);
336             }
337
338             private void updateDescription() {
339                 if (transfersTable.getSelectionCount() > 0) {
340                     TableItem item = transfersTable.getSelection()[0];
341                     text.setText(((PreferenceTransferElement) item.getData())
342                             .getDescription());
343                 } else {
344                     text.setText(""); //$NON-NLS-1$
345
}
346             }
347         };
348
349         transfersTable.addSelectionListener(selection);
350         chooseImportsButton.addSelectionListener(selection);
351         allButton.addSelectionListener(selection);
352         
353         addSelectionButtons(group);
354
355     }
356
357     protected abstract String JavaDoc getChooseButtonText();
358
359     protected abstract String JavaDoc getAllButtonText();
360
361     /**
362      * Add the selection and deselection buttons to the composite.
363      *
364      * @param composite
365      * org.eclipse.swt.widgets.Composite
366      */

367     private void addSelectionButtons(Composite composite) {
368         Font parentFont = composite.getFont();
369         buttonComposite = new Composite(composite, SWT.NONE);
370         GridLayout layout = new GridLayout();
371         layout.numColumns = 2;
372         buttonComposite.setLayout(layout);
373         GridData data = new GridData(GridData.GRAB_HORIZONTAL);
374         data.grabExcessHorizontalSpace = true;
375         buttonComposite.setLayoutData(data);
376         buttonComposite.setFont(parentFont);
377         
378         Button selectButton = createButton(buttonComposite,
379                 IDialogConstants.SELECT_ALL_ID,
380                 PreferencesMessages.SelectionDialog_selectLabel, false);
381
382         SelectionListener listener = new SelectionAdapter() {
383             public void widgetSelected(SelectionEvent e) {
384                 setAllChecked(true);
385                 updatePageCompletion();
386             }
387         };
388         selectButton.addSelectionListener(listener);
389         selectButton.setFont(parentFont);
390         
391         Button deselectButton = createButton(buttonComposite,
392                 IDialogConstants.DESELECT_ALL_ID,
393                 PreferencesMessages.SelectionDialog_deselectLabel, false);
394
395         listener = new SelectionAdapter() {
396             public void widgetSelected(SelectionEvent e) {
397                 setAllChecked(false);
398                 updatePageCompletion();
399             }
400         };
401         deselectButton.addSelectionListener(listener);
402         deselectButton.setFont(parentFont);
403     }
404
405     /**
406      * @param bool
407      */

408     protected void setAllChecked(boolean bool) {
409         TableItem[] items = transfersTable.getItems();
410         for (int i = 0; i < items.length; i++) {
411             TableItem item = items[i];
412             item.setChecked(bool);
413         }
414     }
415
416     /**
417      * Create the export destination specification widgets
418      *
419      * @param parent
420      * org.eclipse.swt.widgets.Composite
421      */

422     protected void createDestinationGroup(Composite parent) {
423         // destination specification group
424
Composite destinationSelectionGroup = new Composite(parent, SWT.NONE);
425         GridLayout layout = new GridLayout();
426         layout.numColumns = 3;
427         destinationSelectionGroup.setLayout(layout);
428         destinationSelectionGroup.setLayoutData(new GridData(
429                 GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
430         
431         Label dest = new Label(destinationSelectionGroup, SWT.NONE);
432         dest.setText(getDestinationLabel());
433         
434         // destination name entry field
435
destinationNameField = new Combo(destinationSelectionGroup, SWT.SINGLE
436                 | SWT.BORDER);
437         destinationNameField.addListener(SWT.Modify, this);
438         destinationNameField.addListener(SWT.Selection, this);
439         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
440                 | GridData.GRAB_HORIZONTAL);
441         destinationNameField.setLayoutData(data);
442         
443         // destination browse button
444
destinationBrowseButton = new Button(destinationSelectionGroup,
445                 SWT.PUSH);
446         destinationBrowseButton
447                 .setText(PreferencesMessages.PreferencesExport_browse);
448         destinationBrowseButton.setLayoutData(new GridData(
449                 GridData.HORIZONTAL_ALIGN_FILL));
450         destinationBrowseButton.addListener(SWT.Selection, this);
451         
452         new Label(parent, SWT.NONE); // vertical spacer
453
}
454
455     /**
456      * Create the export options specification widgets.
457      *
458      * @param parent
459      * org.eclipse.swt.widgets.Composite
460      */

461     protected void createOptionsGroup(Composite parent) {
462         // options group
463

464         Composite optionsGroup = new Composite(parent, SWT.NONE);
465         GridLayout layout = new GridLayout();
466         layout.marginHeight = 0;
467         optionsGroup.setLayout(layout);
468         optionsGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL
469                 | GridData.GRAB_HORIZONTAL));
470     
471         // overwrite... checkbox
472
overwriteExistingFilesCheckbox = new Button(optionsGroup, SWT.CHECK
473                 | SWT.LEFT);
474         overwriteExistingFilesCheckbox
475                 .setText(PreferencesMessages.ExportFile_overwriteExisting);
476         
477     }
478
479     /**
480      * Attempts to ensure that the specified directory exists on the local file
481      * system. Answers a boolean indicating success.
482      *
483      * @return boolean
484      * @param directory
485      * java.io.File
486      */

487     protected boolean ensureDirectoryExists(File JavaDoc directory) {
488         if (!directory.exists()) {
489             if (!queryYesNoQuestion(PreferencesMessages.PreferencesExport_createTargetDirectory)) {
490                 return false;
491             }
492
493             if (!directory.mkdirs()) {
494                 MessageDialog
495                         .openError(
496                                 getContainer().getShell(),
497                                 PreferencesMessages.PreferencesExport_error,
498                                 PreferencesMessages.PreferencesExport_directoryCreationError);
499                 return false;
500             }
501         }
502         return true;
503     }
504
505     /**
506      * Displays a Yes/No question to the user with the specified message and
507      * returns the user's response.
508      *
509      * @param message
510      * the question to ask
511      * @return <code>true</code> for Yes, and <code>false</code> for No
512      */

513     protected boolean queryYesNoQuestion(String JavaDoc message) {
514         MessageDialog dialog = new MessageDialog(getContainer().getShell(),
515                 PreferencesMessages.Question, (Image) null, message,
516                 MessageDialog.NONE, new String JavaDoc[] { IDialogConstants.YES_LABEL,
517                         IDialogConstants.NO_LABEL }, 0);
518         // ensure yes is the default
519

520         return dialog.open() == 0;
521     }
522
523     /**
524      * If the target for export does not exist then attempt to create it. Answer
525      * a boolean indicating whether the target exists (ie.- if it either
526      * pre-existed or this method was able to create it)
527      *
528      * @return boolean
529      */

530     protected boolean ensureTargetIsValid(File JavaDoc file) {
531         if (file.exists()) {
532             if (!getOverwriteExisting()) {
533                 String JavaDoc msg = NLS
534                         .bind(
535                                 PreferencesMessages.WizardPreferencesExportPage1_overwrite,
536                                 file.getAbsolutePath());
537                 if (!queryYesNoQuestion(msg)) {
538                     return false;
539                 }
540             }
541             file.delete();
542         } else if (!file.isDirectory()) {
543             File JavaDoc parent = file.getParentFile();
544             if (parent != null) {
545                 file.getParentFile().mkdirs();
546             }
547         }
548         return true;
549     }
550
551     /*
552      * (non-Javadoc)
553      *
554      * @see org.eclipse.ui.dialogs.WizardDataTransferPage#saveWidgetValues()
555      */

556     protected void saveWidgetValues() {
557         // allow subclasses to save values
558
internalSaveWidgetValues();
559     }
560
561     /**
562      * The Finish button was pressed. Try to do the required work now and answer
563      * a boolean indicating success. If false is returned then the wizard will
564      * not close.
565      *
566      * @return boolean
567      */

568     public boolean finish() {
569         // about to invoke the operation so save our state
570
saveWidgetValues();
571
572         IPreferenceFilter[] transfers = null;
573
574         if (getTransferAll()) {
575             // export all
576
transfers = new IPreferenceFilter[1];
577
578             // For export all create a preference filter that can export
579
// all nodes of the Instance and Configuration scopes
580
transfers[0] = new IPreferenceFilter() {
581
582                 public String JavaDoc[] getScopes() {
583                     return new String JavaDoc[] { InstanceScope.SCOPE,
584                             ConfigurationScope.SCOPE };
585                 }
586
587                 public Map JavaDoc getMapping(String JavaDoc scope) {
588                     return null;
589                 }
590             };
591         } else {
592             transfers = getFilters();
593         }
594
595         boolean success = transfer(transfers);
596         // if it was a successful tranfer then store the name of the file to use
597
// it on the next export
598
if (success) {
599             saveWidgetValues();
600         }
601         return success;
602     }
603
604     /**
605      * @return the preference transfer filters
606      */

607     protected IPreferenceFilter[] getFilters() {
608         IPreferenceFilter[] filters = null;
609         PreferenceTransferElement[] transferElements;
610         transferElements = getPreferenceTransferElements();
611         if (transferElements != null) {
612             filters = new IPreferenceFilter[transferElements.length];
613             for (int j = 0; j < transferElements.length; j++) {
614                 PreferenceTransferElement element = transferElements[j];
615                 try {
616                     filters[j] = element.getFilter();
617                 } catch (CoreException e) {
618                     WorkbenchPlugin.log(e.getMessage(), e);
619                 }
620             }
621         } else {
622             filters = new IPreferenceFilter[0];
623         }
624
625         return filters;
626     }
627
628     /**
629      * @return the list of transfer elements
630      */

631     protected PreferenceTransferElement[] getPreferenceTransferElements() {
632         PreferenceTransferElement[] transferElements;
633         // export selected transfer types
634
TableItem[] items = transfersTable.getItems();
635         List JavaDoc transferList = new ArrayList JavaDoc();
636         for (int i = 0; i < items.length; i++) {
637             TableItem item = items[i];
638             if (item.getChecked()) {
639                 transferList.add(item.getData());
640             }
641         }
642         transferElements = new PreferenceTransferElement[transferList.size()];
643         int i = 0;
644         for (Iterator JavaDoc iter = transferList.iterator(); iter.hasNext();) {
645             transferElements[i] = (PreferenceTransferElement) iter.next();
646             i++;
647         }
648         return transferElements;
649     }
650
651     /**
652      * @param transfers
653      * @return boolean
654      */

655     protected abstract boolean transfer(IPreferenceFilter[] transfers);
656
657     /**
658      * Check whether the internal state of the page is complete and update the
659      * dialog
660      */

661     public void setPageComplete() {
662         boolean complete = true;
663
664         if (!determinePageCompletion()) {
665             complete = false;
666         }
667
668         super.setPageComplete(complete);
669     }
670
671     /**
672      * Returns whether this page is complete. This determination is made based
673      * upon the current contents of this page's controls. Subclasses wishing to
674      * include their controls in this determination should override the hook
675      * methods <code>validateSourceGroup</code> and/or
676      * <code>validateOptionsGroup</code>.
677      *
678      * @return <code>true</code> if this page is complete, and
679      * <code>false</code> if incomplete
680      * @see #validateSourceGroup
681      * @see #validateOptionsGroup
682      */

683     protected boolean determinePageCompletion() {
684         
685         // validate groups in order of priority so error message is the most important one
686
boolean complete = validateSourceGroup() && validateDestinationGroup()
687                 && validateOptionsGroup();
688
689         // Avoid draw flicker by not clearing the error
690
// message unless all is valid.
691
if (complete) {
692             setErrorMessage(null);
693         } else {
694             setErrorMessage(currentMessage);
695         }
696
697         return complete;
698     }
699
700     /**
701      * Returns whether this page's options group's controls currently all
702      * contain valid values.
703      * <p>
704      * The <code>WizardPreferencesPage</code> implementation of this method
705      * returns <code>true</code> if the button to transfer all preferences is
706      * selected OR at least one of the individual items are checked. Subclasses
707      * may reimplement this method.
708      * </p>
709      *
710      * @return <code>true</code> indicating validity of all controls in the
711      * options group
712      */

713     protected boolean validateOptionsGroup() {
714         if (chooseImportsButton.getSelection()) {
715             TableItem[] items = transfersTable.getItems();
716             for (int i = 0; i < items.length; i++) {
717                 TableItem item = items[i];
718                 if (item.getChecked()) {
719                     return true;
720                 }
721             }
722             currentMessage = getNoOptionsMessage();
723             return false;
724         }
725         return true;
726     }
727
728     /**
729      * Returns whether this page's source specification controls currently all
730      * contain valid values.
731      * <p>
732      * The <code>WizardDataTransferPage</code> implementation of this method
733      * returns <code>true</code>. Subclasses may reimplement this hook
734      * method.
735      * </p>
736      *
737      * @return <code>true</code> indicating validity of all controls in the
738      * source specification group
739      */

740     protected boolean validateSourceGroup() {
741         return true;
742     }
743
744     /**
745      * Answer the string to display in self as the destination type
746      *
747      * @return java.lang.String
748      */

749     protected abstract String JavaDoc getDestinationLabel();
750
751     /**
752      * Answer the contents of self's destination specification widget
753      *
754      * @return java.lang.String
755      */

756     protected String JavaDoc getDestinationValue() {
757         return destinationNameField.getText().trim();
758     }
759
760     /**
761      * Set the current input focus to self's destination entry field
762      */

763     protected void giveFocusToDestination() {
764         destinationNameField.setFocus();
765     }
766
767     /**
768      * Open an appropriate destination browser so that the user can specify a
769      * source to import from
770      */

771     protected void handleDestinationBrowseButtonPressed() {
772         FileDialog dialog = new FileDialog(getContainer().getShell(),
773                 getFileDialogStyle());
774         dialog.setText(getFileDialogTitle());
775         dialog.setFilterPath(getDestinationValue());
776         dialog.setFilterExtensions(new String JavaDoc[] { "*.epf" ,"*.*"}); //$NON-NLS-1$ //$NON-NLS-2$
777
String JavaDoc selectedFileName = dialog.open();
778
779         if (selectedFileName != null) {
780             setDestinationValue(selectedFileName);
781         }
782     }
783
784     protected abstract String JavaDoc getFileDialogTitle();
785
786     protected abstract int getFileDialogStyle();
787
788     /**
789      * Handle all events and enablements for widgets in this page
790      *
791      * @param e
792      * Event
793      */

794     public void handleEvent(Event e) {
795         Widget source = e.widget;
796
797         if (source == destinationBrowseButton) {
798             handleDestinationBrowseButtonPressed();
799         }
800
801         updatePageCompletion();
802     }
803
804     /**
805      * Determine if the page is complete and update the page appropriately.
806      */

807     protected void updatePageCompletion() {
808         boolean pageComplete = determinePageCompletion();
809         setPageComplete(pageComplete);
810         if (pageComplete) {
811             setMessage(null);
812         }
813     }
814
815     /**
816      * Hook method for saving widget values for restoration by the next instance
817      * of this class.
818      */

819     protected void internalSaveWidgetValues() {
820         // update directory names history
821
IDialogSettings settings = getDialogSettings();
822         if (settings != null) {
823             String JavaDoc[] directoryNames = settings
824                     .getArray(STORE_DESTINATION_NAMES_ID);
825             if (directoryNames == null) {
826                 directoryNames = new String JavaDoc[0];
827             }
828
829             directoryNames = addToHistory(directoryNames, getDestinationValue());
830             settings.put(STORE_DESTINATION_NAMES_ID, directoryNames);
831             String JavaDoc current = getDestinationValue();
832             if (current != null && !current.equals("")) { //$NON-NLS-1$
833
settings.put(STORE_DESTINATION_ID, current);
834             }
835             // options
836
if (overwriteExistingFilesCheckbox != null) {
837                 settings.put(STORE_OVERWRITE_EXISTING_FILES_ID,
838                         overwriteExistingFilesCheckbox.getSelection());
839             }
840             settings.put(TRANSFER_ALL_PREFERENCES_ID, allButton.getSelection());
841
842         }
843     }
844
845       /**
846      * Adds an entry to a history, while taking care of duplicate history items
847      * and excessively long histories. The assumption is made that all histories
848      * should be of length <code>WizardDataTransferPage.COMBO_HISTORY_LENGTH</code>.
849      *
850      * @param history the current history
851      * @param newEntry the entry to add to the history
852      */

853     protected String JavaDoc[] addToHistory(String JavaDoc[] history, String JavaDoc newEntry) {
854         java.util.ArrayList JavaDoc l = new java.util.ArrayList JavaDoc(Arrays.asList(history));
855         addToHistory(l, newEntry);
856         String JavaDoc[] r = new String JavaDoc[l.size()];
857         l.toArray(r);
858         return r;
859     }
860
861     /**
862      * Adds an entry to a history, while taking care of duplicate history items
863      * and excessively long histories. The assumption is made that all histories
864      * should be of length <code>WizardDataTransferPage.COMBO_HISTORY_LENGTH</code>.
865      *
866      * @param history the current history
867      * @param newEntry the entry to add to the history
868      */

869     protected void addToHistory(List JavaDoc history, String JavaDoc newEntry) {
870         history.remove(newEntry);
871         history.add(0, newEntry);
872
873         // since only one new item was added, we can be over the limit
874
// by at most one item
875
if (history.size() > COMBO_HISTORY_LENGTH) {
876             history.remove(COMBO_HISTORY_LENGTH);
877         }
878     }
879
880     /**
881      * Hook method for restoring widget values to the values that they held last
882      * time this wizard was used to completion.
883      */

884     protected void restoreWidgetValues() {
885         IDialogSettings settings = getDialogSettings();
886         boolean all = true;
887         if (settings != null) {
888             String JavaDoc[] directoryNames = settings
889                     .getArray(STORE_DESTINATION_NAMES_ID);
890             if (directoryNames != null) {
891                 // destination
892
setDestinationValue(directoryNames[0]);
893                 for (int i = 0; i < directoryNames.length; i++) {
894                     addDestinationItem(directoryNames[i]);
895                 }
896
897                 String JavaDoc current = settings.get(STORE_DESTINATION_ID);
898                 if (current != null) {
899                     setDestinationValue(current);
900                 }
901                 // options
902
if (overwriteExistingFilesCheckbox != null) {
903                     overwriteExistingFilesCheckbox.setSelection(settings
904                             .getBoolean(STORE_OVERWRITE_EXISTING_FILES_ID));
905                 }
906                 all = settings.getBoolean(TRANSFER_ALL_PREFERENCES_ID);
907             }
908         }
909         if (all) {
910             allButton.setSelection(true);
911         } else {
912             chooseImportsButton.setSelection(true);
913         }
914
915     }
916
917     private boolean getOverwriteExisting() {
918         return overwriteExistingFilesCheckbox.getSelection();
919     }
920
921     private boolean getTransferAll() {
922         return allButton.getSelection();
923     }
924
925     /**
926      * Set the contents of self's destination specification widget to the passed
927      * value
928      *
929      * @param value
930      * java.lang.String
931      */

932     protected void setDestinationValue(String JavaDoc value) {
933         destinationNameField.setText(value);
934     }
935
936     /*
937      * (non-Javadoc)
938      *
939      * @see org.eclipse.jface.dialogs.DialogPage#dispose()
940      */

941     public void dispose() {
942         super.dispose();
943         if (imageTable == null) {
944             return;
945         }
946
947         for (Iterator JavaDoc i = imageTable.values().iterator(); i.hasNext();) {
948             ((Image) i.next()).dispose();
949         }
950         imageTable = null;
951         transfers = null;
952     }
953
954     /*
955      * (non-Javadoc)
956      *
957      * @see org.eclipse.ui.dialogs.WizardDataTransferPage#allowNewContainerName()
958      */

959     protected boolean allowNewContainerName() {
960         return true;
961     }
962
963     /**
964      * The <code>WizardDataTransfer</code> implementation of this
965      * <code>IOverwriteQuery</code> method asks the user whether the existing
966      * resource at the given path should be overwritten.
967      *
968      * @param pathString
969      * @return the user's reply: one of <code>"YES"</code>, <code>"NO"</code>,
970      * <code>"ALL"</code>, or <code>"CANCEL"</code>
971      */

972     public String JavaDoc queryOverwrite(String JavaDoc pathString) {
973
974         Path path = new Path(pathString);
975
976         String JavaDoc messageString;
977         // Break the message up if there is a file name and a directory
978
// and there are at least 2 segments.
979
if (path.getFileExtension() == null || path.segmentCount() < 2) {
980             messageString = NLS.bind(
981                     PreferencesMessages.WizardDataTransfer_existsQuestion,
982                     pathString);
983         } else {
984             messageString = NLS
985                     .bind(
986                             PreferencesMessages.WizardDataTransfer_overwriteNameAndPathQuestion,
987                             path.lastSegment(), path.removeLastSegments(1)
988                                     .toOSString());
989         }
990
991         final MessageDialog dialog = new MessageDialog(getContainer()
992                 .getShell(), PreferencesMessages.Question, null, messageString,
993                 MessageDialog.QUESTION, new String JavaDoc[] {
994                         IDialogConstants.YES_LABEL,
995                         IDialogConstants.YES_TO_ALL_LABEL,
996                         IDialogConstants.NO_LABEL,
997                         IDialogConstants.NO_TO_ALL_LABEL,
998                         IDialogConstants.CANCEL_LABEL }, 0);
999         String JavaDoc[] response = new String JavaDoc[] { YES, ALL, NO, NO_ALL, CANCEL };
1000        // run in syncExec because callback is from an operation,
1001
// which is probably not running in the UI thread.
1002
getControl().getDisplay().syncExec(new Runnable JavaDoc() {
1003            public void run() {
1004                dialog.open();
1005            }
1006        });
1007        return dialog.getReturnCode() < 0 ? CANCEL : response[dialog
1008                .getReturnCode()];
1009    }
1010}
1011
Popular Tags