1 11 package org.eclipse.ui.internal.activities.ws; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Collection ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Properties ; 20 import java.util.Set ; 21 22 import org.eclipse.jface.dialogs.Dialog; 23 import org.eclipse.jface.dialogs.IDialogConstants; 24 import org.eclipse.jface.resource.JFaceResources; 25 import org.eclipse.jface.viewers.AbstractTreeViewer; 26 import org.eclipse.jface.viewers.CheckStateChangedEvent; 27 import org.eclipse.jface.viewers.CheckboxTreeViewer; 28 import org.eclipse.jface.viewers.ICheckStateListener; 29 import org.eclipse.jface.viewers.ISelectionChangedListener; 30 import org.eclipse.jface.viewers.IStructuredSelection; 31 import org.eclipse.jface.viewers.SelectionChangedEvent; 32 import org.eclipse.jface.viewers.StructuredSelection; 33 import org.eclipse.jface.viewers.ViewerComparator; 34 import org.eclipse.swt.SWT; 35 import org.eclipse.swt.events.SelectionAdapter; 36 import org.eclipse.swt.events.SelectionEvent; 37 import org.eclipse.swt.graphics.FontMetrics; 38 import org.eclipse.swt.graphics.GC; 39 import org.eclipse.swt.graphics.Point; 40 import org.eclipse.swt.layout.GridData; 41 import org.eclipse.swt.layout.GridLayout; 42 import org.eclipse.swt.widgets.Button; 43 import org.eclipse.swt.widgets.Composite; 44 import org.eclipse.swt.widgets.Control; 45 import org.eclipse.swt.widgets.Label; 46 import org.eclipse.swt.widgets.Text; 47 import org.eclipse.ui.activities.ActivitiesPreferencePage; 48 import org.eclipse.ui.activities.IActivity; 49 import org.eclipse.ui.activities.ICategory; 50 import org.eclipse.ui.activities.ICategoryActivityBinding; 51 import org.eclipse.ui.activities.IMutableActivityManager; 52 import org.eclipse.ui.activities.NotDefinedException; 53 54 60 public class ActivityEnabler { 61 62 private static final int ALL = 2; 63 64 private static final int NONE = 0; 65 66 private static final int SOME = 1; 67 68 private ISelectionChangedListener selectionListener = new ISelectionChangedListener() { 69 70 75 public void selectionChanged(SelectionChangedEvent event) { 76 Object element = ((IStructuredSelection) event.getSelection()) 77 .getFirstElement(); 78 try { 79 if (element instanceof ICategory) { 80 descriptionText.setText(((ICategory) element) 81 .getDescription()); 82 } else if (element instanceof IActivity) { 83 descriptionText.setText(((IActivity) element) 84 .getDescription()); 85 } 86 } catch (NotDefinedException e) { 87 descriptionText.setText(""); } 89 } 90 }; 91 92 95 private ICheckStateListener checkListener = new ICheckStateListener() { 96 97 102 public void checkStateChanged(CheckStateChangedEvent event) { 103 Set checked = new HashSet (Arrays.asList(dualViewer 104 .getCheckedElements())); 105 Object element = event.getElement(); 106 if (element instanceof ICategory) { 107 dualViewer.setSubtreeChecked(element, event.getChecked()); 110 dualViewer.setGrayed(element, false); 113 Object categoryActivities[] = provider.getChildren(element); 114 for (int index = 0; index < categoryActivities.length; index++) { 117 handleDuplicateActivities(event.getChecked(), 118 categoryActivities[index]); 119 } 120 121 } else { 122 handleActivityCheck(checked, element); 124 handleDuplicateActivities(event.getChecked(), element); 125 } 126 } 127 128 136 private void handleDuplicateActivities(boolean checkedState, 137 Object element) { 138 Object [] duplicateActivities = provider 140 .getDuplicateCategoryActivities((CategorizedActivity) element); 141 CategorizedActivity activity = null; 142 for (int index = 0; index < duplicateActivities.length; index++) { 143 activity = (CategorizedActivity) duplicateActivities[index]; 144 dualViewer.setChecked(activity, checkedState); 147 handleActivityCheck(new HashSet (Arrays.asList(dualViewer 150 .getCheckedElements())), activity); 151 } 152 } 153 154 163 private void handleActivityCheck(Set checked, Object element) { 164 CategorizedActivity proxy = (CategorizedActivity) element; 167 Object [] children = provider.getChildren(proxy.getCategory()); 168 int state = NONE; 169 int count = 0; 170 for (int i = 0; i < children.length; i++) { 171 if (checked.contains(children[i])) { 172 count++; 173 } 174 } 175 176 if (count == children.length) { 177 state = ALL; 178 } else if (count != 0) { 179 state = SOME; 180 } 181 182 if (state == NONE) { 183 checked.remove(proxy.getCategory()); 184 } else { 185 checked.add(proxy.getCategory()); 186 } 187 188 dualViewer.setGrayed(proxy.getCategory(), state == SOME); 189 dualViewer.setCheckedElements(checked.toArray()); 190 handleRequiredActivities(checked, element); 193 } 194 195 204 private void handleRequiredActivities(Set checked, Object element) { 205 Object [] requiredActivities = null; 206 if (checked.contains(element)) { 209 requiredActivities = provider 210 .getChildRequiredActivities(((CategorizedActivity) element) 211 .getId()); 212 for (int index = 0; index < requiredActivities.length; index++) { 213 if (!checked.contains(requiredActivities[index])) { 215 dualViewer.setChecked(requiredActivities[index], true); 216 handleActivityCheck(new HashSet (Arrays 217 .asList(dualViewer.getCheckedElements())), 218 requiredActivities[index]); 219 } 220 } 221 } 222 else { 225 requiredActivities = provider 226 .getParentRequiredActivities(((CategorizedActivity) element) 227 .getId()); 228 for (int index = 0; index < requiredActivities.length; index++) { 229 if (checked.contains(requiredActivities[index])) { 231 dualViewer.setChecked(requiredActivities[index], false); 232 handleActivityCheck(new HashSet (Arrays 233 .asList(dualViewer.getCheckedElements())), 234 requiredActivities[index]); 235 } 236 } 237 } 238 } 239 240 }; 241 242 protected CheckboxTreeViewer dualViewer; 243 244 247 private Set managedActivities = new HashSet (7); 248 249 252 protected ActivityCategoryContentProvider provider = new ActivityCategoryContentProvider(); 253 254 257 protected Text descriptionText; 258 259 private Properties strings; 260 261 private IMutableActivityManager activitySupport; 262 263 273 public ActivityEnabler(IMutableActivityManager activitySupport, Properties strings) { 274 this.activitySupport = activitySupport; 275 this.strings = strings; 276 } 277 278 285 public Control createControl(Composite parent) { 286 GC gc = new GC(parent); 287 gc.setFont(JFaceResources.getDialogFont()); 288 FontMetrics fontMetrics = gc.getFontMetrics(); 289 gc.dispose(); 290 291 Composite composite = new Composite(parent, SWT.NONE); 292 composite.setLayout(createGridLayoutWithoutMargins(1, fontMetrics)); 293 294 new Label(composite, SWT.NONE).setText(strings.getProperty(ActivitiesPreferencePage.ACTIVITY_NAME, ActivityMessages.ActivityEnabler_activities) + ':'); 295 296 dualViewer = new CheckboxTreeViewer(composite); 297 dualViewer.setComparator(new ViewerComparator()); 298 dualViewer.setLabelProvider(new ActivityCategoryLabelProvider()); 299 dualViewer.setContentProvider(provider); 300 dualViewer.setInput(activitySupport); 301 GridData data = new GridData(SWT.FILL, SWT.FILL, true, true); 302 dualViewer.getControl().setLayoutData(data); 303 304 Composite buttonComposite = new Composite(composite, SWT.NONE); 305 buttonComposite.setLayout(createGridLayoutWithoutMargins(2, fontMetrics)); 306 307 Button selectAllButton = new Button(buttonComposite, SWT.PUSH); 308 selectAllButton.setText(ActivityMessages.ActivityEnabler_selectAll); 309 selectAllButton.addSelectionListener(new SelectionAdapter() { 310 315 public void widgetSelected(SelectionEvent e) { 316 toggleTreeEnablement(true); 317 } 318 }); 319 setButtonLayoutData(selectAllButton, fontMetrics); 320 321 Button deselectAllButton = new Button(buttonComposite, SWT.PUSH); 322 deselectAllButton.setText(ActivityMessages.ActivityEnabler_deselectAll); 323 deselectAllButton.addSelectionListener(new SelectionAdapter() { 324 329 public void widgetSelected(SelectionEvent e) { 330 toggleTreeEnablement(false); 331 } 332 }); 333 setButtonLayoutData(deselectAllButton, fontMetrics); 334 335 new Label(composite, SWT.NONE).setText(ActivityMessages.ActivityEnabler_description); 336 337 descriptionText = new Text(composite, SWT.READ_ONLY | SWT.WRAP | SWT.BORDER 338 | SWT.V_SCROLL); 339 data = new GridData(SWT.FILL, SWT.FILL, true, false); 340 data.heightHint = Dialog.convertHeightInCharsToPixels(fontMetrics, 5); 341 descriptionText.setLayoutData(data); 342 setInitialStates(); 343 344 dualViewer.addCheckStateListener(checkListener); 345 dualViewer.addSelectionChangedListener(selectionListener); 346 347 dualViewer.setSelection(new StructuredSelection()); 348 349 Dialog.applyDialogFont(composite); 350 351 return composite; 352 } 353 354 private GridLayout createGridLayoutWithoutMargins(int nColumns, FontMetrics fontMetrics) { 355 GridLayout layout = new GridLayout(nColumns, false); 356 layout.marginHeight = 0; 357 layout.marginWidth = 0; 358 layout.horizontalSpacing = Dialog.convertHorizontalDLUsToPixels(fontMetrics, IDialogConstants.HORIZONTAL_SPACING); 359 layout.verticalSpacing = Dialog.convertVerticalDLUsToPixels(fontMetrics, IDialogConstants.VERTICAL_SPACING); 360 return layout; 361 } 362 363 private GridData setButtonLayoutData(Button button, FontMetrics fontMetrics) { 364 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 365 int widthHint = Dialog.convertHorizontalDLUsToPixels(fontMetrics, IDialogConstants.BUTTON_WIDTH); 366 Point minSize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true); 367 data.widthHint = Math.max(widthHint, minSize.x); 368 button.setLayoutData(data); 369 return data; 370 } 371 372 378 private Collection getCategoryActivityIds(String categoryId) { 379 ICategory category = activitySupport.getCategory( 380 categoryId); 381 Set activityBindings = category.getCategoryActivityBindings(); 382 List categoryActivities = new ArrayList (activityBindings.size()); 383 for (Iterator i = activityBindings.iterator(); i.hasNext();) { 384 ICategoryActivityBinding binding = (ICategoryActivityBinding) i 385 .next(); 386 String activityId = binding.getActivityId(); 387 categoryActivities.add(activityId); 388 } 389 return categoryActivities; 390 } 391 392 396 private void setInitialStates() { 397 Set enabledActivities = activitySupport 398 .getEnabledActivityIds(); 399 setEnabledStates(enabledActivities); 400 } 401 402 private void setEnabledStates(Set enabledActivities) { 403 Set categories = activitySupport 404 .getDefinedCategoryIds(); 405 List checked = new ArrayList (10), grayed = new ArrayList (10); 406 for (Iterator i = categories.iterator(); i.hasNext();) { 407 String categoryId = (String ) i.next(); 408 ICategory category = activitySupport 409 .getCategory(categoryId); 410 411 int state = NONE; 412 Collection activities = getCategoryActivityIds(categoryId); 413 int foundCount = 0; 414 for (Iterator j = activities.iterator(); j.hasNext();) { 415 String activityId = (String ) j.next(); 416 managedActivities.add(activityId); 417 if (enabledActivities.contains(activityId)) { 418 IActivity activity = activitySupport 419 .getActivity(activityId); 420 checked.add(new CategorizedActivity(category, activity)); 421 foundCount++; 423 } 424 } 425 426 if (foundCount == activities.size()) { 427 state = ALL; 428 } else if (foundCount > 0) { 429 state = SOME; 430 } 431 432 if (state == NONE) { 433 continue; 434 } 435 checked.add(category); 436 437 if (state == SOME) { 438 grayed.add(category); 439 } 440 } 441 442 dualViewer.setCheckedElements(checked.toArray()); 443 dualViewer.setGrayedElements(grayed.toArray()); 444 } 445 446 450 public void updateActivityStates() { 451 Set enabledActivities = new HashSet (activitySupport 452 .getEnabledActivityIds()); 453 454 enabledActivities.removeAll(managedActivities); 456 457 Object [] checked = dualViewer.getCheckedElements(); 458 for (int i = 0; i < checked.length; i++) { 459 Object element = checked[i]; 460 if (element instanceof ICategory || dualViewer.getGrayed(element)) { 461 continue; 462 } 463 enabledActivities.add(((IActivity) element).getId()); 464 } 465 466 activitySupport.setEnabledActivityIds(enabledActivities); 467 } 468 469 472 public void restoreDefaults() { 473 Set defaultEnabled = new HashSet (); 474 Set activityIds = activitySupport.getDefinedActivityIds(); 475 for (Iterator i = activityIds.iterator(); i.hasNext();) { 476 String activityId = (String ) i.next(); 477 IActivity activity = activitySupport.getActivity(activityId); 478 try { 479 if (activity.isDefaultEnabled()) { 480 defaultEnabled.add(activityId); 481 } 482 } catch (NotDefinedException e) { 483 } 485 } 486 487 setEnabledStates(defaultEnabled); 488 } 489 490 496 protected void toggleTreeEnablement(boolean enabled) { 497 Object [] elements = provider.getElements(activitySupport); 498 499 dualViewer.setGrayedElements(new Object [0]); 501 502 for (int i = 0; i < elements.length; i++) { 504 dualViewer 505 .expandToLevel(elements[i], AbstractTreeViewer.ALL_LEVELS); 506 dualViewer.setSubtreeChecked(elements[i], enabled); 507 } 508 } 509 } 510 | Popular Tags |