1 11 package org.eclipse.ui.activities; 12 13 import java.util.Collections ; 14 import java.util.HashSet ; 15 import java.util.Hashtable ; 16 import java.util.Iterator ; 17 import java.util.Properties ; 18 import java.util.Set ; 19 20 import org.eclipse.core.runtime.IConfigurationElement; 21 import org.eclipse.core.runtime.IExecutableExtension; 22 import org.eclipse.jface.dialogs.Dialog; 23 import org.eclipse.jface.dialogs.IDialogConstants; 24 import org.eclipse.jface.dialogs.IDialogSettings; 25 import org.eclipse.jface.dialogs.TrayDialog; 26 import org.eclipse.jface.preference.PreferencePage; 27 import org.eclipse.jface.resource.DeviceResourceException; 28 import org.eclipse.jface.resource.ImageDescriptor; 29 import org.eclipse.jface.resource.JFaceResources; 30 import org.eclipse.jface.resource.LocalResourceManager; 31 import org.eclipse.jface.viewers.CheckboxTableViewer; 32 import org.eclipse.jface.viewers.ISelectionChangedListener; 33 import org.eclipse.jface.viewers.IStructuredContentProvider; 34 import org.eclipse.jface.viewers.IStructuredSelection; 35 import org.eclipse.jface.viewers.ITableLabelProvider; 36 import org.eclipse.jface.viewers.LabelProvider; 37 import org.eclipse.jface.viewers.LabelProviderChangedEvent; 38 import org.eclipse.jface.viewers.SelectionChangedEvent; 39 import org.eclipse.jface.viewers.TableViewer; 40 import org.eclipse.jface.viewers.Viewer; 41 import org.eclipse.jface.viewers.ViewerComparator; 42 import org.eclipse.jface.viewers.ViewerFilter; 43 import org.eclipse.osgi.util.NLS; 44 import org.eclipse.swt.SWT; 45 import org.eclipse.swt.events.SelectionAdapter; 46 import org.eclipse.swt.events.SelectionEvent; 47 import org.eclipse.swt.graphics.Image; 48 import org.eclipse.swt.graphics.ImageData; 49 import org.eclipse.swt.graphics.Point; 50 import org.eclipse.swt.layout.GridData; 51 import org.eclipse.swt.layout.GridLayout; 52 import org.eclipse.swt.widgets.Button; 53 import org.eclipse.swt.widgets.Composite; 54 import org.eclipse.swt.widgets.Control; 55 import org.eclipse.swt.widgets.Label; 56 import org.eclipse.swt.widgets.Shell; 57 import org.eclipse.swt.widgets.Table; 58 import org.eclipse.swt.widgets.TableItem; 59 import org.eclipse.swt.widgets.Text; 60 import org.eclipse.ui.IWorkbench; 61 import org.eclipse.ui.IWorkbenchPreferencePage; 62 import org.eclipse.ui.PlatformUI; 63 import org.eclipse.ui.internal.IPreferenceConstants; 64 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 65 import org.eclipse.ui.internal.OverlayIcon; 66 import org.eclipse.ui.internal.WorkbenchPlugin; 67 import org.eclipse.ui.internal.activities.ws.ActivityEnabler; 68 import org.eclipse.ui.internal.activities.ws.ActivityMessages; 69 import org.eclipse.ui.plugin.AbstractUIPlugin; 70 71 87 public final class ActivityCategoryPreferencePage extends PreferencePage implements 88 IWorkbenchPreferencePage, IExecutableExtension { 89 90 93 public static final String ACTIVITY_NAME = "activityName"; 95 99 public static final String ALLOW_ADVANCED = "allowAdvanced"; 101 104 public static final String CAPTION_MESSAGE = "captionMessage"; 106 109 public static final String CATEGORY_NAME = "categoryName"; 111 114 public static final String ACTIVITY_PROMPT_BUTTON = "activityPromptButton"; 116 119 public static final String ACTIVITY_PROMPT_BUTTON_TOOLTIP = "activityPromptButtonTooltip"; 121 private class AdvancedDialog extends TrayDialog { 122 123 private static final String DIALOG_SETTINGS_SECTION = "ActivityCategoryPreferencePageAdvancedDialogSettings"; 125 126 ActivityEnabler enabler; 127 130 protected AdvancedDialog(Shell parentShell) { 131 super(parentShell); 132 setShellStyle(getShellStyle() | SWT.RESIZE); 133 } 134 135 138 protected void configureShell(Shell newShell) { 139 super.configureShell(newShell); 140 String activityName = strings.getProperty(ACTIVITY_NAME, ActivityMessages.ActivityEnabler_activities); 141 activityName = activityName.replaceAll("&", ""); newShell.setText(NLS.bind( 143 ActivityMessages.ActivitiesPreferencePage_advancedDialogTitle, 144 activityName 145 )); 146 } 147 148 151 protected Control createDialogArea(Composite parent) { 152 Composite composite = (Composite) super.createDialogArea(parent); 153 enabler = new ActivityEnabler(workingCopy, strings); 154 Control enablerControl = enabler.createControl(composite); 155 enablerControl.setLayoutData(new GridData(GridData.FILL_BOTH)); 156 return composite; 157 } 158 159 162 protected void okPressed() { 163 enabler.updateActivityStates(); 164 super.okPressed(); 165 } 166 167 172 protected IDialogSettings getDialogBoundsSettings() { 173 IDialogSettings settings = WorkbenchPlugin.getDefault().getDialogSettings(); 174 IDialogSettings section = settings.getSection(DIALOG_SETTINGS_SECTION); 175 if (section == null) { 176 section = settings.addNewSection(DIALOG_SETTINGS_SECTION); 177 } 178 return section; 179 } 180 } 181 private class CategoryLabelProvider extends LabelProvider implements 182 ITableLabelProvider, IActivityManagerListener { 183 184 private LocalResourceManager manager = new LocalResourceManager( 185 JFaceResources.getResources()); 186 187 private ImageDescriptor lockDescriptor; 188 189 private boolean decorate; 190 191 194 public CategoryLabelProvider(boolean decorate) { 195 this.decorate = decorate; 196 lockDescriptor = AbstractUIPlugin.imageDescriptorFromPlugin( 197 PlatformUI.PLUGIN_ID, "icons/full/ovr16/lock_ovr.gif"); } 199 200 206 public Image getColumnImage(Object element, int columnIndex) { 207 ICategory category = (ICategory) element; 208 ImageDescriptor descriptor = PlatformUI.getWorkbench() 209 .getActivitySupport().getImageDescriptor(category); 210 if (descriptor != null) { 211 try { 212 if (decorate) { 213 if (isLocked(category)) { 214 ImageData originalImageData = descriptor 215 .getImageData(); 216 OverlayIcon overlay = new OverlayIcon( 217 descriptor, lockDescriptor, new Point( 218 originalImageData.width, 219 originalImageData.height)); 220 return manager.createImage(overlay); 221 } 222 } 223 224 return manager.createImage(descriptor); 225 } catch (DeviceResourceException e) { 226 WorkbenchPlugin.log(e); 227 } 228 } 229 return null; 230 } 231 232 235 public String getText(Object element) { 236 String name = null; 237 ICategory category = (ICategory) element; 238 try { 239 name = category.getName(); 240 } catch (NotDefinedException e) { 241 name = category.getId(); 242 } 243 if (decorate && isLocked(category)) { 244 name = NLS.bind(ActivityMessages.ActivitiesPreferencePage_lockedMessage, name); 245 } 246 return name; 247 } 248 249 252 public String getColumnText(Object element, int columnIndex) { 253 return getText(element); 254 } 255 256 261 public void dispose() { 262 super.dispose(); 263 manager.dispose(); 264 } 265 266 271 public void activityManagerChanged( 272 ActivityManagerEvent activityManagerEvent) { 273 if (activityManagerEvent.haveEnabledActivityIdsChanged()) { 274 updateCategoryCheckState(); 275 fireLabelProviderChanged(new LabelProviderChangedEvent(this)); 276 } 277 } 278 } 279 280 private class CategoryContentProvider implements IStructuredContentProvider { 281 282 287 public Object [] getElements(Object inputElement) { 288 return WorkbenchActivityHelper.resolveCategories(workingCopy, 290 (Set ) inputElement); 291 } 292 293 298 public void dispose() { 299 300 } 301 302 308 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 309 310 } 311 } 312 313 private class EmptyCategoryFilter extends ViewerFilter { 314 315 321 public boolean select(Viewer viewer, Object parentElement, 322 Object element) { 323 ICategory category = (ICategory) element; 324 if (WorkbenchActivityHelper.getActivityIdsForCategory(category) 325 .isEmpty()) { 326 return false; 327 } 328 return true; 329 } 330 } 331 332 protected IWorkbench workbench; 333 334 private CheckboxTableViewer categoryViewer; 335 336 private TableViewer dependantViewer; 337 338 private Text descriptionText; 339 340 private IMutableActivityManager workingCopy; 341 342 private Button activityPromptButton; 343 344 private boolean allowAdvanced = false; 345 346 private Button advancedButton; 347 348 private Properties strings = new Properties (); 349 350 355 protected Control createContents(Composite parent) { 356 initializeDialogUnits(parent); 357 358 Composite composite = new Composite(parent, SWT.NONE); 359 GridLayout layout = new GridLayout(2, false); 360 layout.marginHeight = layout.marginWidth = 0; 361 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 362 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 363 composite.setLayout(layout); 364 Label label = new Label(composite, SWT.WRAP); 365 label 366 .setText(strings.getProperty(CAPTION_MESSAGE, ActivityMessages.ActivitiesPreferencePage_captionMessage)); 367 GridData data = new GridData(GridData.FILL_HORIZONTAL); 368 data.widthHint = 400; 369 data.horizontalSpan = 2; 370 label.setLayoutData(data); 371 label = new Label(composite, SWT.NONE); data = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 373 data.horizontalSpan = 2; 374 label.setLayoutData(data); 375 createPromptButton(composite); 376 createCategoryArea(composite); 377 createDetailsArea(composite); 378 createButtons(composite); 379 380 workbench.getHelpSystem().setHelp(parent, 381 IWorkbenchHelpContextIds.CAPABILITY_PREFERENCE_PAGE); 382 383 Dialog.applyDialogFont(composite); 384 385 return composite; 386 } 387 388 391 private void createPromptButton(Composite composite) { 392 activityPromptButton = new Button(composite, SWT.CHECK); 393 activityPromptButton.setText(strings.getProperty(ACTIVITY_PROMPT_BUTTON, ActivityMessages.activityPromptButton)); 394 activityPromptButton.setToolTipText(strings.getProperty(ACTIVITY_PROMPT_BUTTON_TOOLTIP, ActivityMessages.activityPromptToolTip)); 395 GridData data = new GridData(); 396 data.horizontalSpan = 2; 397 activityPromptButton.setLayoutData(data); 398 activityPromptButton.setSelection(getPreferenceStore() 399 .getBoolean( 400 IPreferenceConstants.SHOULD_PROMPT_FOR_ENABLEMENT)); 401 } 402 403 private void createButtons(final Composite parent) { 404 Composite composite = new Composite(parent, SWT.NONE); 405 GridLayout layout = new GridLayout(4, false); 406 layout.marginHeight = layout.marginWidth = 0; 407 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 408 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 409 composite.setLayout(layout); 410 GridData data = new GridData(GridData.FILL_HORIZONTAL); 411 data.horizontalSpan = 2; 412 composite.setLayoutData(data); 413 414 Button enableAll = new Button(composite, SWT.PUSH); 415 enableAll.addSelectionListener(new SelectionAdapter() { 416 417 422 public void widgetSelected(SelectionEvent e) { 423 workingCopy.setEnabledActivityIds(workingCopy 424 .getDefinedActivityIds()); 425 } 426 }); 427 enableAll.setText(ActivityMessages.ActivityEnabler_selectAll); 428 setButtonLayoutData(enableAll); 429 430 Button disableAll = new Button(composite, SWT.PUSH); 431 disableAll.addSelectionListener(new SelectionAdapter() { 432 437 public void widgetSelected(SelectionEvent e) { 438 workingCopy.setEnabledActivityIds(Collections.EMPTY_SET); 439 } 440 }); 441 disableAll.setText(ActivityMessages.ActivityEnabler_deselectAll); 442 setButtonLayoutData(disableAll); 443 444 if (allowAdvanced) { 445 Label spacer = new Label(composite, SWT.NONE); 446 data = new GridData(GridData.GRAB_HORIZONTAL); 447 spacer.setLayoutData(data); 448 advancedButton = new Button(composite, SWT.PUSH); 449 advancedButton.addSelectionListener(new SelectionAdapter() { 450 451 456 public void widgetSelected(SelectionEvent e) { 457 AdvancedDialog dialog = new AdvancedDialog(parent.getShell()); 458 dialog.open(); } 460 }); 461 advancedButton.setText(ActivityMessages.ActivitiesPreferencePage_advancedButton); 462 setButtonLayoutData(advancedButton); 463 } 464 } 465 466 469 private void createDetailsArea(Composite parent) { 470 Composite composite = new Composite(parent, SWT.NONE); 471 GridLayout layout = new GridLayout(); 472 layout.marginHeight = layout.marginWidth = 0; 473 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 474 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 475 composite.setLayout(layout); 476 composite.setLayoutData(new GridData(GridData.FILL_BOTH)); 477 478 new Label(composite, SWT.NONE).setText(ActivityMessages.ActivityEnabler_description); 479 descriptionText = new Text(composite, SWT.WRAP | SWT.READ_ONLY | SWT.BORDER); 480 GridData data = new GridData(GridData.FILL_BOTH); 481 data.heightHint = 100; 482 data.widthHint = 200; 483 descriptionText.setLayoutData(data); 484 485 new Label(composite, SWT.NONE).setText(ActivityMessages.ActivitiesPreferencePage_requirements); 486 dependantViewer = new TableViewer(composite, SWT.BORDER); 487 dependantViewer.getControl().setLayoutData( 488 new GridData(GridData.FILL_BOTH)); 489 dependantViewer.setContentProvider(new CategoryContentProvider()); 490 dependantViewer.addFilter(new EmptyCategoryFilter()); 491 dependantViewer.setLabelProvider(new CategoryLabelProvider(false)); 492 dependantViewer.setInput(Collections.EMPTY_SET); 493 } 494 495 498 private void createCategoryArea(Composite parent) { 499 Composite composite = new Composite(parent, SWT.NONE); 500 GridLayout layout = new GridLayout(); 501 layout.marginHeight = layout.marginWidth = 0; 502 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 503 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 504 composite.setLayout(layout); 505 GridData data = new GridData(GridData.FILL_BOTH); 506 data.widthHint = 200; 507 composite.setLayoutData(data); 508 Label label = new Label(composite, SWT.NONE); 509 label.setText(strings.getProperty(CATEGORY_NAME, ActivityMessages.ActivityEnabler_categories) + ':'); 510 Table table = new Table(composite, SWT.CHECK | SWT.BORDER | SWT.SINGLE); 511 table.addSelectionListener(new SelectionAdapter() { 512 513 518 public void widgetSelected(SelectionEvent e) { 519 if (e.detail == SWT.CHECK) { 520 TableItem tableItem = (TableItem) e.item; 521 522 ICategory category = (ICategory) tableItem.getData(); 523 if (isLocked(category)) { 524 tableItem.setChecked(true); 525 e.doit = false; return; 527 } 528 Set activitySet = WorkbenchActivityHelper 529 .getActivityIdsForCategory(category); 530 if (tableItem.getChecked()) { 531 activitySet.addAll(workingCopy.getEnabledActivityIds()); 532 } else { 533 HashSet newSet = new HashSet (workingCopy 534 .getEnabledActivityIds()); 535 newSet.removeAll(activitySet); 536 activitySet = newSet; 537 } 538 539 workingCopy.setEnabledActivityIds(activitySet); 540 updateCategoryCheckState(); } 545 } 546 }); 547 categoryViewer = new CheckboxTableViewer(table); 548 categoryViewer.getControl().setLayoutData( 549 new GridData(GridData.FILL_BOTH)); 550 categoryViewer.setContentProvider(new CategoryContentProvider()); 551 CategoryLabelProvider categoryLabelProvider = new CategoryLabelProvider( 552 true); 553 workingCopy.addActivityManagerListener(categoryLabelProvider); 554 categoryViewer.setLabelProvider(categoryLabelProvider); 555 categoryViewer.setComparator(new ViewerComparator()); 556 categoryViewer.addFilter(new EmptyCategoryFilter()); 557 558 categoryViewer 559 .addSelectionChangedListener(new ISelectionChangedListener() { 560 561 566 public void selectionChanged(SelectionChangedEvent event) { 567 ICategory element = (ICategory) ((IStructuredSelection) event 568 .getSelection()).getFirstElement(); 569 setDetails(element); 570 } 571 }); 572 categoryViewer.setInput(workingCopy.getDefinedCategoryIds()); 573 574 updateCategoryCheckState(); 575 } 576 577 582 private void updateCategoryCheckState() { 583 ICategory[] enabledCategories = getEnabledCategories(); 584 ICategory[] partiallyEnabledCategories = getPartialCategories(); 585 Object [] allChecked = new Object [enabledCategories.length 586 + partiallyEnabledCategories.length]; 587 System.arraycopy(enabledCategories, 0, allChecked, 0, enabledCategories.length); 588 System.arraycopy(partiallyEnabledCategories, 0, allChecked, enabledCategories.length, partiallyEnabledCategories.length); 589 categoryViewer.setCheckedElements(allChecked); 590 categoryViewer.setGrayedElements(partiallyEnabledCategories); 591 } 592 593 private ICategory[] getPartialCategories() { 594 return WorkbenchActivityHelper.resolveCategories(workingCopy, 595 WorkbenchActivityHelper 596 .getPartiallyEnabledCategories(workingCopy)); 597 } 598 599 private ICategory[] getEnabledCategories() { 600 return WorkbenchActivityHelper.resolveCategories(workingCopy, 601 WorkbenchActivityHelper.getEnabledCategories(workingCopy)); 602 } 603 604 protected void setDetails(ICategory category) { 605 if (category == null) { 606 clearDetails(); 607 return; 608 } 609 Set categories = null; 610 if (WorkbenchActivityHelper.isEnabled(workingCopy, category.getId())) { 611 categories = WorkbenchActivityHelper.getDisabledCategories( 612 workingCopy, category.getId()); 613 614 } else { 615 categories = WorkbenchActivityHelper.getEnabledCategories( 616 workingCopy, category.getId()); 617 } 618 619 categories = WorkbenchActivityHelper.getContainedCategories( 620 workingCopy, category.getId()); 621 dependantViewer.setInput(categories); 622 try { 623 descriptionText.setText(category.getDescription()); 624 } catch (NotDefinedException e) { 625 descriptionText.setText(""); } 627 } 628 629 632 protected void clearDetails() { 633 dependantViewer.setInput(Collections.EMPTY_SET); 634 descriptionText.setText(""); } 636 637 642 public void init(IWorkbench workbench) { 643 this.workbench = workbench; 644 workingCopy = workbench.getActivitySupport().createWorkingCopy(); 645 setPreferenceStore(WorkbenchPlugin.getDefault().getPreferenceStore()); 646 } 647 648 655 protected boolean isLocked(ICategory category) { 656 return !WorkbenchActivityHelper.getDisabledCategories(workingCopy, 657 category.getId()).isEmpty(); 658 } 659 660 665 public boolean performOk() { 666 workbench.getActivitySupport().setEnabledActivityIds( 667 workingCopy.getEnabledActivityIds()); 668 getPreferenceStore().setValue( 669 IPreferenceConstants.SHOULD_PROMPT_FOR_ENABLEMENT, 670 activityPromptButton.getSelection()); 671 return true; 672 } 673 674 677 protected void performDefaults() { 678 super.performDefaults(); 679 activityPromptButton.setSelection(getPreferenceStore() 680 .getDefaultBoolean( 681 IPreferenceConstants.SHOULD_PROMPT_FOR_ENABLEMENT)); 682 683 Set defaultEnabled = new HashSet (); 684 Set activityIds = workingCopy.getDefinedActivityIds(); 685 for (Iterator i = activityIds.iterator(); i.hasNext();) { 686 String activityId = (String ) i.next(); 687 IActivity activity = workingCopy.getActivity(activityId); 688 try { 689 if (activity.isDefaultEnabled()) { 690 defaultEnabled.add(activityId); 691 } 692 } catch (NotDefinedException e) { 693 } 695 } 696 697 workingCopy.setEnabledActivityIds(defaultEnabled); 698 } 699 700 706 public void setInitializationData(IConfigurationElement config, 707 String propertyName, Object data) { 708 if (data instanceof Hashtable ) { 709 Hashtable table = (Hashtable )data; 710 allowAdvanced = Boolean.valueOf((String ) table.remove(ALLOW_ADVANCED)).booleanValue(); 711 strings.putAll(table); 712 } 713 } 714 } 715 | Popular Tags |