1 11 12 package org.eclipse.ui.internal.dialogs; 13 14 import com.ibm.icu.text.Collator; 15 import java.util.ArrayList ; 16 import java.util.Collections ; 17 import java.util.Comparator ; 18 19 import org.eclipse.jface.dialogs.IDialogConstants; 20 import org.eclipse.jface.dialogs.MessageDialog; 21 import org.eclipse.jface.preference.IPreferenceStore; 22 import org.eclipse.jface.preference.PreferencePage; 23 import org.eclipse.jface.resource.ImageDescriptor; 24 import org.eclipse.osgi.util.NLS; 25 import org.eclipse.swt.SWT; 26 import org.eclipse.swt.events.SelectionAdapter; 27 import org.eclipse.swt.events.SelectionEvent; 28 import org.eclipse.swt.graphics.Font; 29 import org.eclipse.swt.layout.GridData; 30 import org.eclipse.swt.layout.GridLayout; 31 import org.eclipse.swt.widgets.Button; 32 import org.eclipse.swt.widgets.Composite; 33 import org.eclipse.swt.widgets.Control; 34 import org.eclipse.swt.widgets.Group; 35 import org.eclipse.swt.widgets.Label; 36 import org.eclipse.swt.widgets.Shell; 37 import org.eclipse.swt.widgets.Table; 38 import org.eclipse.swt.widgets.TableItem; 39 import org.eclipse.swt.widgets.Widget; 40 import org.eclipse.ui.IPerspectiveDescriptor; 41 import org.eclipse.ui.IWorkbench; 42 import org.eclipse.ui.IWorkbenchPage; 43 import org.eclipse.ui.IWorkbenchPreferenceConstants; 44 import org.eclipse.ui.IWorkbenchPreferencePage; 45 import org.eclipse.ui.IWorkbenchWindow; 46 import org.eclipse.ui.PlatformUI; 47 import org.eclipse.ui.internal.IPreferenceConstants; 48 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 49 import org.eclipse.ui.internal.WorkbenchMessages; 50 import org.eclipse.ui.internal.WorkbenchPage; 51 import org.eclipse.ui.internal.WorkbenchPlugin; 52 import org.eclipse.ui.internal.registry.PerspectiveDescriptor; 53 import org.eclipse.ui.internal.registry.PerspectiveRegistry; 54 import org.eclipse.ui.internal.util.Descriptors; 55 import org.eclipse.ui.internal.util.PrefUtil; 56 import org.eclipse.ui.internal.util.Util; 57 58 61 public class PerspectivesPreferencePage extends PreferencePage implements 62 IWorkbenchPreferencePage { 63 private IWorkbench workbench; 64 65 private PerspectiveRegistry perspectiveRegistry; 66 67 private ArrayList perspectives; 68 69 private String defaultPerspectiveId; 70 71 private ArrayList perspToDelete = new ArrayList (); 72 73 private ArrayList perspToRevert = new ArrayList (); 74 75 private Table perspectivesTable; 76 77 private Button revertButton; 78 79 private Button deleteButton; 80 81 private Button setDefaultButton; 82 83 private Button openSameWindowButton; 85 86 private Button openNewWindowButton; 87 88 private int openPerspMode; 89 90 private int openViewMode; 92 93 private Button openEmbedButton; 94 95 private Button openFastButton; 96 97 private final String OVM_TITLE = WorkbenchMessages.OpenViewMode_title; 99 100 private final String OVM_EMBED = WorkbenchMessages.OpenViewMode_embed; 101 102 private final String OVM_FAST = WorkbenchMessages.OpenViewMode_fast; 103 104 private final String OPM_TITLE = WorkbenchMessages.OpenPerspectiveMode_optionsTitle; 105 106 private final String OPM_SAME_WINDOW = WorkbenchMessages.OpenPerspectiveMode_sameWindow; 107 108 private final String OPM_NEW_WINDOW = WorkbenchMessages.OpenPerspectiveMode_newWindow; 109 110 113 private Comparator comparator = new Comparator () { 114 private Collator collator = Collator.getInstance(); 115 116 public int compare(Object ob1, Object ob2) { 117 IPerspectiveDescriptor d1 = (IPerspectiveDescriptor) ob1; 118 IPerspectiveDescriptor d2 = (IPerspectiveDescriptor) ob2; 119 return collator.compare(d1.getLabel(), d2.getLabel()); 120 } 121 }; 122 123 126 protected Control createContents(Composite parent) { 127 PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, 130 IWorkbenchHelpContextIds.PERSPECTIVES_PREFERENCE_PAGE); 131 132 Composite composite = createComposite(parent); 133 134 createOpenPerspButtonGroup(composite); 135 createOpenViewButtonGroup(composite); 136 createCustomizePerspective(composite); 137 138 return composite; 139 } 140 141 149 protected Composite createComposite(Composite parent) { 150 Composite composite = new Composite(parent, SWT.NONE); 151 GridData data = new GridData(GridData.FILL_BOTH); 152 composite.setLayoutData(data); 153 composite.setFont(parent.getFont()); 154 GridLayout layout = new GridLayout(); 155 layout.marginWidth = 0; 156 layout.marginHeight = 0; 157 layout.verticalSpacing = 10; 158 composite.setLayout(layout); 159 return composite; 160 } 161 162 169 protected void createOpenPerspButtonGroup(Composite composite) { 170 171 Font font = composite.getFont(); 172 173 Group buttonComposite = new Group(composite, SWT.LEFT); 174 buttonComposite.setText(OPM_TITLE); 175 buttonComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 176 buttonComposite.setFont(composite.getFont()); 177 GridLayout layout = new GridLayout(); 178 layout.numColumns = 2; 179 buttonComposite.setLayout(layout); 180 181 openSameWindowButton = new Button(buttonComposite, SWT.RADIO); 182 openSameWindowButton.setText(OPM_SAME_WINDOW); 183 openSameWindowButton 184 .setSelection(IPreferenceConstants.OPM_ACTIVE_PAGE == openPerspMode); 185 openSameWindowButton.setFont(font); 186 openSameWindowButton.addSelectionListener(new SelectionAdapter() { 187 public void widgetSelected(SelectionEvent e) { 188 openPerspMode = IPreferenceConstants.OPM_ACTIVE_PAGE; 189 } 190 }); 191 192 openNewWindowButton = new Button(buttonComposite, SWT.RADIO); 193 openNewWindowButton.setText(OPM_NEW_WINDOW); 194 openNewWindowButton 195 .setSelection(IPreferenceConstants.OPM_NEW_WINDOW == openPerspMode); 196 openNewWindowButton.setFont(font); 197 openNewWindowButton.addSelectionListener(new SelectionAdapter() { 198 public void widgetSelected(SelectionEvent e) { 199 openPerspMode = IPreferenceConstants.OPM_NEW_WINDOW; 200 } 201 }); 202 203 } 204 205 211 protected void createOpenViewButtonGroup(Composite composite) { 212 213 Font font = composite.getFont(); 214 215 Group buttonComposite = new Group(composite, SWT.LEFT); 216 buttonComposite.setText(OVM_TITLE); 217 buttonComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 218 buttonComposite.setFont(composite.getFont()); 219 GridLayout layout = new GridLayout(); 220 layout.numColumns = 2; 221 buttonComposite.setLayout(layout); 222 223 openEmbedButton = new Button(buttonComposite, SWT.RADIO); 224 openEmbedButton.setText(OVM_EMBED); 225 openEmbedButton 226 .setSelection(openViewMode == IPreferenceConstants.OVM_EMBED); 227 openEmbedButton.addSelectionListener(new SelectionAdapter() { 228 public void widgetSelected(SelectionEvent e) { 229 openViewMode = IPreferenceConstants.OVM_EMBED; 230 } 231 }); 232 openEmbedButton.setFont(font); 233 234 if (openViewMode == IPreferenceConstants.OVM_FLOAT) { 236 openViewMode = IPreferenceConstants.OVM_FAST; 237 } 238 239 openFastButton = new Button(buttonComposite, SWT.RADIO); 240 openFastButton.setText(OVM_FAST); 241 openFastButton 242 .setSelection(openViewMode == IPreferenceConstants.OVM_FAST); 243 openFastButton.addSelectionListener(new SelectionAdapter() { 244 public void widgetSelected(SelectionEvent e) { 245 openViewMode = IPreferenceConstants.OVM_FAST; 246 } 247 }); 248 openFastButton.setFont(font); 249 250 } 251 252 260 protected Composite createCustomizePerspective(Composite parent) { 261 262 Font font = parent.getFont(); 263 264 Composite perspectivesComponent = new Composite(parent, SWT.NONE); 266 perspectivesComponent.setLayoutData(new GridData(GridData.FILL_BOTH)); 267 perspectivesComponent.setFont(parent.getFont()); 268 269 GridLayout layout = new GridLayout(); 270 layout.numColumns = 2; 271 layout.marginWidth = 0; 272 layout.marginHeight = 0; 273 perspectivesComponent.setLayout(layout); 274 275 Label label = new Label(perspectivesComponent, SWT.LEFT); 277 label.setText(WorkbenchMessages.PerspectivesPreference_available); 278 GridData data = new GridData(); 279 data.horizontalSpan = 2; 280 label.setLayoutData(data); 281 label.setFont(font); 282 283 perspectivesTable = new Table(perspectivesComponent, SWT.H_SCROLL | SWT.V_SCROLL 285 | SWT.BORDER); 286 perspectivesTable.addSelectionListener(new SelectionAdapter() { 287 public void widgetSelected(SelectionEvent e) { 288 updateButtons(); 289 } 290 }); 291 perspectivesTable.setFont(font); 292 293 data = new GridData(GridData.FILL_BOTH); 294 data.grabExcessHorizontalSpace = true; 295 data.grabExcessVerticalSpace = true; 296 perspectivesTable.setLayoutData(data); 297 298 IPerspectiveDescriptor[] persps = perspectiveRegistry.getPerspectives(); 300 perspectives = new ArrayList (persps.length); 301 for (int i = 0; i < persps.length; i++) { 302 perspectives.add(i, persps[i]); 303 } 304 Collections.sort(perspectives, comparator); 305 defaultPerspectiveId = perspectiveRegistry.getDefaultPerspective(); 306 updatePerspectivesTable(); 307 308 Composite buttonBar = (Composite) createVerticalButtonBar(perspectivesComponent); 310 data = new GridData(GridData.FILL_VERTICAL); 311 buttonBar.setLayoutData(data); 312 313 String NOTE_LABEL = WorkbenchMessages.Preference_note; 315 String REVERT_NOTE = WorkbenchMessages.RevertPerspective_note; 316 Composite noteComposite = createNoteComposite(font, parent, 317 NOTE_LABEL, REVERT_NOTE); 318 GridData noteData = new GridData(); 319 noteData.horizontalSpan = 2; 320 noteComposite.setLayoutData(noteData); 321 return perspectivesComponent; 322 } 323 324 342 protected Button createVerticalButton(Composite parent, String label, 343 boolean defaultButton) { 344 Button button = new Button(parent, SWT.PUSH); 345 346 button.setText(label); 347 348 GridData data = setButtonLayoutData(button); 349 data.horizontalAlignment = GridData.FILL; 350 351 button.addSelectionListener(new SelectionAdapter() { 352 public void widgetSelected(SelectionEvent event) { 353 verticalButtonPressed(event.widget); 354 } 355 }); 356 button.setToolTipText(label); 357 if (defaultButton) { 358 Shell shell = parent.getShell(); 359 if (shell != null) { 360 shell.setDefaultButton(button); 361 } 362 } 363 button.setFont(parent.getFont()); 364 return button; 365 } 366 367 374 protected Control createVerticalButtonBar(Composite parent) { 375 Composite composite = new Composite(parent, SWT.NULL); 377 378 GridLayout layout = new GridLayout(); 381 layout.numColumns = 1; 382 layout.marginWidth = 5; 383 layout.marginHeight = 0; 384 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 385 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 386 composite.setLayout(layout); 387 composite.setFont(parent.getFont()); 388 389 setDefaultButton = createVerticalButton(composite, WorkbenchMessages.PerspectivesPreference_MakeDefault, false); 391 setDefaultButton.setToolTipText(WorkbenchMessages.PerspectivesPreference_MakeDefaultTip); 392 393 revertButton = createVerticalButton(composite, WorkbenchMessages.PerspectivesPreference_Reset, false); 394 revertButton.setToolTipText(WorkbenchMessages.PerspectivesPreference_ResetTip); 395 396 deleteButton = createVerticalButton(composite, WorkbenchMessages.PerspectivesPreference_Delete, false); 397 deleteButton.setToolTipText(WorkbenchMessages.PerspectivesPreference_DeleteTip); 398 updateButtons(); 399 400 return composite; 401 } 402 403 406 public void init(IWorkbench aWorkbench) { 407 this.workbench = aWorkbench; 408 this.perspectiveRegistry = (PerspectiveRegistry) workbench 409 .getPerspectiveRegistry(); 410 IPreferenceStore store = WorkbenchPlugin.getDefault() 411 .getPreferenceStore(); 412 setPreferenceStore(store); 413 414 openViewMode = store.getInt(IPreferenceConstants.OPEN_VIEW_MODE); 415 openPerspMode = store.getInt(IPreferenceConstants.OPEN_PERSP_MODE); 416 } 417 418 421 protected void performDefaults() { 422 IPreferenceStore store = WorkbenchPlugin.getDefault() 424 .getPreferenceStore(); 425 426 openViewMode = store.getDefaultInt(IPreferenceConstants.OPEN_VIEW_MODE); 427 if (openViewMode == IPreferenceConstants.OVM_FLOAT) { 429 openViewMode = IPreferenceConstants.OVM_FAST; 430 } 431 openEmbedButton 432 .setSelection(openViewMode == IPreferenceConstants.OVM_EMBED); 433 openFastButton 434 .setSelection(openViewMode == IPreferenceConstants.OVM_FAST); 435 436 openPerspMode = store 437 .getDefaultInt(IPreferenceConstants.OPEN_PERSP_MODE); 438 openSameWindowButton 439 .setSelection(IPreferenceConstants.OPM_ACTIVE_PAGE == openPerspMode); 440 openNewWindowButton 441 .setSelection(IPreferenceConstants.OPM_NEW_WINDOW == openPerspMode); 442 443 String currentDefault = perspectiveRegistry.getDefaultPerspective(); 444 445 int index = indexOf(currentDefault); 446 if (index >= 0){ 447 defaultPerspectiveId = currentDefault; 448 updatePerspectivesTable(); 449 perspectivesTable.setSelection(index); 450 } 451 452 String newDefault = PrefUtil.getAPIPreferenceStore().getDefaultString( 453 IWorkbenchPreferenceConstants.DEFAULT_PERSPECTIVE_ID); 454 455 IPerspectiveDescriptor desc = null; 456 if (newDefault != null) { 457 desc = workbench.getPerspectiveRegistry().findPerspectiveWithId(newDefault); 458 } 459 if (desc == null) { 460 newDefault = workbench.getPerspectiveRegistry().getDefaultPerspective(); 461 } 462 463 defaultPerspectiveId = newDefault; 464 updatePerspectivesTable(); 465 466 } 467 468 473 private int indexOf(String perspectiveId) { 474 if (perspectiveId == null) { 475 return -1; 476 } 477 PerspectiveDescriptor[] descriptors = 478 new PerspectiveDescriptor[perspectives.size()]; 479 perspectives.toArray(descriptors); 480 for (int i = 0; i < descriptors.length; i++) { 481 PerspectiveDescriptor descriptor = descriptors[i]; 482 if(descriptor.getId().equals(perspectiveId)) { 483 return i; 484 } 485 } 486 return -1; 487 } 488 489 496 private boolean findOpenInstance(IPerspectiveDescriptor desc) { 497 IWorkbenchWindow windows[] = workbench.getWorkbenchWindows(); 498 499 for (int i = 0; i < windows.length; i++) { 501 IWorkbenchPage pages[] = windows[i].getPages(); 502 for (int j = 0; j < pages.length; j++) { 503 WorkbenchPage page = (WorkbenchPage) pages[j]; 504 if (page.findPerspective(desc) != null) { 505 MessageDialog 506 .openInformation( 507 getShell(), 508 WorkbenchMessages.PerspectivesPreference_cannotdelete_title, 509 NLS.bind(WorkbenchMessages.PerspectivesPreference_cannotdelete_message,desc.getLabel())); 510 return true; 511 } 512 } 513 } 514 515 return false; 516 } 517 518 521 public boolean performOk() { 522 if (!Util.equals(defaultPerspectiveId, perspectiveRegistry.getDefaultPerspective())) { 524 perspectiveRegistry.setDefaultPerspective(defaultPerspectiveId); 525 } 526 527 if(perspectives.size()<perspectiveRegistry.getPerspectives().length) { 529 perspectiveRegistry.deletePerspectives(perspToDelete); 530 } 531 532 perspectiveRegistry.revertPerspectives(perspToRevert); 534 535 IPreferenceStore store = getPreferenceStore(); 536 537 store.setValue(IPreferenceConstants.OPEN_VIEW_MODE, openViewMode); 539 540 store.setValue(IPreferenceConstants.OPEN_PERSP_MODE, openPerspMode); 542 543 PrefUtil.savePrefs(); 547 548 return true; 549 } 550 551 554 protected void updateButtons() { 555 int index = perspectivesTable.getSelectionIndex(); 557 558 PerspectiveDescriptor desc = null; 560 if (index > -1) { 561 desc = (PerspectiveDescriptor) perspectives.get(index); 562 } 563 564 if (desc != null) { 566 revertButton.setEnabled(desc.isPredefined() 567 && desc.hasCustomDefinition() 568 && !perspToRevert.contains(desc)); 569 deleteButton.setEnabled(!desc.isPredefined()); 570 setDefaultButton.setEnabled(true); 571 } else { 572 revertButton.setEnabled(false); 573 deleteButton.setEnabled(false); 574 setDefaultButton.setEnabled(false); 575 } 576 } 577 578 581 protected void updatePerspectivesTable() { 582 perspectivesTable.removeAll(); 584 for (int i = 0; i < perspectives.size(); i++) { 585 PerspectiveDescriptor persp = (PerspectiveDescriptor) perspectives.get(i); 586 newPerspectivesTableItem(persp, i, false); 587 } 588 } 589 590 593 protected TableItem newPerspectivesTableItem(IPerspectiveDescriptor persp, 594 int index, boolean selected) { 595 596 ImageDescriptor image = persp.getImageDescriptor(); 597 598 TableItem item = new TableItem(perspectivesTable, SWT.NULL, index); 599 if (image != null) { 600 Descriptors.setImage(item, image); 601 } 602 String label=persp.getLabel(); 603 if (persp.getId().equals(defaultPerspectiveId)){ 604 label = NLS.bind(WorkbenchMessages.PerspectivesPreference_defaultLabel, label ); 605 606 } 607 item.setText(label); 608 item.setData(persp); 609 if (selected) { 610 perspectivesTable.setSelection(index); 611 } 612 613 return item; 614 } 615 616 622 protected void verticalButtonPressed(Widget button) { 623 int index = perspectivesTable.getSelectionIndex(); 625 626 PerspectiveDescriptor desc = null; 628 if (index > -1) { 629 desc = (PerspectiveDescriptor) perspectives.get(index); 630 } else { 631 return; 632 } 633 634 if (button == revertButton) { 636 if (desc.isPredefined() && !perspToRevert.contains(desc)) { 637 perspToRevert.add(desc); 638 } 639 } else if (button == deleteButton) { 640 if (!desc.isPredefined() && !perspToDelete.contains(desc)) { 641 if(!findOpenInstance(desc)){ 642 perspToDelete.add(desc); 643 perspToRevert.remove(desc); 644 perspectives.remove(desc); 645 updatePerspectivesTable(); 646 } 647 648 } 649 } else if (button == setDefaultButton) { 650 defaultPerspectiveId = desc.getId(); 651 updatePerspectivesTable(); 652 perspectivesTable.setSelection(index); 653 } 654 655 updateButtons(); 656 } 657 658 public void dispose() { 659 super.dispose(); 660 } 661 } 662 | Popular Tags |