1 11 package org.eclipse.ui.internal.wizards.preferences; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.Hashtable ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 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 64 public abstract class WizardPreferencesPage extends WizardPage implements 65 Listener, IOverwriteQuery { 66 67 protected Combo destinationNameField; 69 70 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 private static final String STORE_DESTINATION_NAMES_ID = "WizardPreferencesExportPage1.STORE_DESTINATION_NAMES_ID"; 90 private static final String STORE_OVERWRITE_EXISTING_FILES_ID = "WizardPreferencesExportPage1.STORE_OVERWRITE_EXISTING_FILES_ID"; 92 private static final String TRANSFER_ALL_PREFERENCES_ID = "WizardPreferencesExportPage1.EXPORT_ALL_PREFERENCES_ID"; 94 private Hashtable imageTable; 95 96 private PreferenceTransferElement[] transfers; 97 98 private String currentMessage; 99 100 private static final String STORE_DESTINATION_ID = null; 101 102 protected static final int COMBO_HISTORY_LENGTH = 5; 103 104 107 protected WizardPreferencesPage(String pageName) { 108 super(pageName); 109 } 110 111 133 protected Button createButton(Composite parent, int id, String label, 134 boolean defaultButton) { 135 ((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 (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 163 protected void addDestinationItem(String value) { 164 destinationNameField.add(value); 165 } 166 167 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 183 if (!(validDestination() && validateOptionsGroup() && validateSourceGroup())) { 186 setPageComplete(false); 187 } 188 189 setPreferenceTransfers(); 190 setControl(composite); 191 192 giveFocusToDestination(); 193 Dialog.applyDialogFont(composite); 194 } 195 196 199 protected abstract void createTransferArea(Composite composite); 200 201 206 protected boolean validateDestinationGroup() { 207 if (!validDestination()) { 208 currentMessage = getInvalidDestinationMessage(); 209 return false; 210 } 211 212 return true; 213 } 214 215 219 abstract protected String getInvalidDestinationMessage(); 220 221 private String getNoOptionsMessage() { 222 return PreferencesMessages.WizardPreferencesPage_noOptionsSelected; 223 } 224 225 protected boolean validDestination() { 226 File file = new File (getDestinationValue()); 227 return !(file.getPath().length() <= 0 || file.isDirectory()); 228 } 229 230 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 245 protected PreferenceTransferElement[] getTransfers() { 246 if (transfers == null) { 247 transfers = PreferenceTransferManager.getPreferenceTransfers(); 248 } 249 return transfers; 250 } 251 252 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 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 277 private Hashtable getImageTable() { 278 if (imageTable == null) { 279 imageTable = new Hashtable (10); 280 } 281 return imageTable; 282 } 283 284 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 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(""); } 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 getChooseButtonText(); 358 359 protected abstract String getAllButtonText(); 360 361 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 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 422 protected void createDestinationGroup(Composite parent) { 423 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 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 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); } 454 455 461 protected void createOptionsGroup(Composite parent) { 462 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 overwriteExistingFilesCheckbox = new Button(optionsGroup, SWT.CHECK 473 | SWT.LEFT); 474 overwriteExistingFilesCheckbox 475 .setText(PreferencesMessages.ExportFile_overwriteExisting); 476 477 } 478 479 487 protected boolean ensureDirectoryExists(File 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 513 protected boolean queryYesNoQuestion(String message) { 514 MessageDialog dialog = new MessageDialog(getContainer().getShell(), 515 PreferencesMessages.Question, (Image) null, message, 516 MessageDialog.NONE, new String [] { IDialogConstants.YES_LABEL, 517 IDialogConstants.NO_LABEL }, 0); 518 520 return dialog.open() == 0; 521 } 522 523 530 protected boolean ensureTargetIsValid(File file) { 531 if (file.exists()) { 532 if (!getOverwriteExisting()) { 533 String 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 parent = file.getParentFile(); 544 if (parent != null) { 545 file.getParentFile().mkdirs(); 546 } 547 } 548 return true; 549 } 550 551 556 protected void saveWidgetValues() { 557 internalSaveWidgetValues(); 559 } 560 561 568 public boolean finish() { 569 saveWidgetValues(); 571 572 IPreferenceFilter[] transfers = null; 573 574 if (getTransferAll()) { 575 transfers = new IPreferenceFilter[1]; 577 578 transfers[0] = new IPreferenceFilter() { 581 582 public String [] getScopes() { 583 return new String [] { InstanceScope.SCOPE, 584 ConfigurationScope.SCOPE }; 585 } 586 587 public Map getMapping(String scope) { 588 return null; 589 } 590 }; 591 } else { 592 transfers = getFilters(); 593 } 594 595 boolean success = transfer(transfers); 596 if (success) { 599 saveWidgetValues(); 600 } 601 return success; 602 } 603 604 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 631 protected PreferenceTransferElement[] getPreferenceTransferElements() { 632 PreferenceTransferElement[] transferElements; 633 TableItem[] items = transfersTable.getItems(); 635 List transferList = new ArrayList (); 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 iter = transferList.iterator(); iter.hasNext();) { 645 transferElements[i] = (PreferenceTransferElement) iter.next(); 646 i++; 647 } 648 return transferElements; 649 } 650 651 655 protected abstract boolean transfer(IPreferenceFilter[] transfers); 656 657 661 public void setPageComplete() { 662 boolean complete = true; 663 664 if (!determinePageCompletion()) { 665 complete = false; 666 } 667 668 super.setPageComplete(complete); 669 } 670 671 683 protected boolean determinePageCompletion() { 684 685 boolean complete = validateSourceGroup() && validateDestinationGroup() 687 && validateOptionsGroup(); 688 689 if (complete) { 692 setErrorMessage(null); 693 } else { 694 setErrorMessage(currentMessage); 695 } 696 697 return complete; 698 } 699 700 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 740 protected boolean validateSourceGroup() { 741 return true; 742 } 743 744 749 protected abstract String getDestinationLabel(); 750 751 756 protected String getDestinationValue() { 757 return destinationNameField.getText().trim(); 758 } 759 760 763 protected void giveFocusToDestination() { 764 destinationNameField.setFocus(); 765 } 766 767 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 [] { "*.epf" ,"*.*"}); String selectedFileName = dialog.open(); 778 779 if (selectedFileName != null) { 780 setDestinationValue(selectedFileName); 781 } 782 } 783 784 protected abstract String getFileDialogTitle(); 785 786 protected abstract int getFileDialogStyle(); 787 788 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 807 protected void updatePageCompletion() { 808 boolean pageComplete = determinePageCompletion(); 809 setPageComplete(pageComplete); 810 if (pageComplete) { 811 setMessage(null); 812 } 813 } 814 815 819 protected void internalSaveWidgetValues() { 820 IDialogSettings settings = getDialogSettings(); 822 if (settings != null) { 823 String [] directoryNames = settings 824 .getArray(STORE_DESTINATION_NAMES_ID); 825 if (directoryNames == null) { 826 directoryNames = new String [0]; 827 } 828 829 directoryNames = addToHistory(directoryNames, getDestinationValue()); 830 settings.put(STORE_DESTINATION_NAMES_ID, directoryNames); 831 String current = getDestinationValue(); 832 if (current != null && !current.equals("")) { settings.put(STORE_DESTINATION_ID, current); 834 } 835 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 853 protected String [] addToHistory(String [] history, String newEntry) { 854 java.util.ArrayList l = new java.util.ArrayList (Arrays.asList(history)); 855 addToHistory(l, newEntry); 856 String [] r = new String [l.size()]; 857 l.toArray(r); 858 return r; 859 } 860 861 869 protected void addToHistory(List history, String newEntry) { 870 history.remove(newEntry); 871 history.add(0, newEntry); 872 873 if (history.size() > COMBO_HISTORY_LENGTH) { 876 history.remove(COMBO_HISTORY_LENGTH); 877 } 878 } 879 880 884 protected void restoreWidgetValues() { 885 IDialogSettings settings = getDialogSettings(); 886 boolean all = true; 887 if (settings != null) { 888 String [] directoryNames = settings 889 .getArray(STORE_DESTINATION_NAMES_ID); 890 if (directoryNames != null) { 891 setDestinationValue(directoryNames[0]); 893 for (int i = 0; i < directoryNames.length; i++) { 894 addDestinationItem(directoryNames[i]); 895 } 896 897 String current = settings.get(STORE_DESTINATION_ID); 898 if (current != null) { 899 setDestinationValue(current); 900 } 901 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 932 protected void setDestinationValue(String value) { 933 destinationNameField.setText(value); 934 } 935 936 941 public void dispose() { 942 super.dispose(); 943 if (imageTable == null) { 944 return; 945 } 946 947 for (Iterator i = imageTable.values().iterator(); i.hasNext();) { 948 ((Image) i.next()).dispose(); 949 } 950 imageTable = null; 951 transfers = null; 952 } 953 954 959 protected boolean allowNewContainerName() { 960 return true; 961 } 962 963 972 public String queryOverwrite(String pathString) { 973 974 Path path = new Path(pathString); 975 976 String messageString; 977 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 [] { 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 [] response = new String [] { YES, ALL, NO, NO_ALL, CANCEL }; 1000 getControl().getDisplay().syncExec(new Runnable () { 1003 public void run() { 1004 dialog.open(); 1005 } 1006 }); 1007 return dialog.getReturnCode() < 0 ? CANCEL : response[dialog 1008 .getReturnCode()]; 1009 } 1010} 1011 | Popular Tags |