1 11 package org.eclipse.debug.internal.ui.actions.breakpointGroups; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.debug.internal.ui.IDebugHelpContextIds; 19 import org.eclipse.debug.internal.ui.SWTFactory; 20 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointOrganizerManager; 21 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointsView; 22 import org.eclipse.debug.internal.ui.views.breakpoints.IBreakpointOrganizer; 23 import org.eclipse.jface.dialogs.Dialog; 24 import org.eclipse.jface.dialogs.TrayDialog; 25 import org.eclipse.jface.resource.ImageDescriptor; 26 import org.eclipse.jface.viewers.DoubleClickEvent; 27 import org.eclipse.jface.viewers.IDoubleClickListener; 28 import org.eclipse.jface.viewers.ILabelProvider; 29 import org.eclipse.jface.viewers.ISelectionChangedListener; 30 import org.eclipse.jface.viewers.IStructuredContentProvider; 31 import org.eclipse.jface.viewers.IStructuredSelection; 32 import org.eclipse.jface.viewers.ITreeContentProvider; 33 import org.eclipse.jface.viewers.LabelProvider; 34 import org.eclipse.jface.viewers.SelectionChangedEvent; 35 import org.eclipse.jface.viewers.TableViewer; 36 import org.eclipse.jface.viewers.TreeViewer; 37 import org.eclipse.jface.viewers.Viewer; 38 import org.eclipse.swt.SWT; 39 import org.eclipse.swt.events.SelectionAdapter; 40 import org.eclipse.swt.events.SelectionEvent; 41 import org.eclipse.swt.graphics.Image; 42 import org.eclipse.swt.layout.GridData; 43 import org.eclipse.swt.layout.GridLayout; 44 import org.eclipse.swt.widgets.Button; 45 import org.eclipse.swt.widgets.Composite; 46 import org.eclipse.swt.widgets.Control; 47 import org.eclipse.swt.widgets.Label; 48 import org.eclipse.swt.widgets.Shell; 49 import org.eclipse.swt.widgets.Table; 50 import org.eclipse.swt.widgets.Tree; 51 import org.eclipse.ui.PlatformUI; 52 53 58 public class GroupBreakpointsByDialog extends TrayDialog { 59 60 private BreakpointsView fView; 61 62 private TableViewer fAvailableViewer; 64 private AvailableOrganizersProvider fAvailableOrganizersProvider= new AvailableOrganizersProvider(); 65 66 private TreeViewer fSelectedViewer; 68 private SelectedOrganizerProvider fSelectedOrganizersProvider= new SelectedOrganizerProvider(); 69 70 private List fResult= new ArrayList (); 71 72 private Button fAddButton; 73 private Button fRemoveButton; 74 private Button fMoveUpButton; 75 private Button fMoveDownButton; 76 77 81 private SelectionAdapter fSelectionListener= new SelectionAdapter() { 82 public void widgetSelected(SelectionEvent e) { 83 Object source= e.getSource(); 84 if (source == fAddButton) { 85 handleAddPressed(); 86 } else if (source == fRemoveButton) { 87 handleRemovePressed(); 88 } else if (source == fMoveUpButton) { 89 handleMoveUpPressed(); 90 } else if (source == fMoveDownButton) { 91 handleMoveDownPressed(); 92 } 93 } 94 }; 95 96 protected GroupBreakpointsByDialog(BreakpointsView view) { 97 super(view.getSite().getShell()); 98 fView= view; 99 setShellStyle(getShellStyle() | SWT.RESIZE); 100 } 101 102 105 protected Control createDialogArea(Composite parent) { 106 ILabelProvider labelProvider= new BreakpointOrganzierLabelProvider(); 107 108 Composite parentComposite= (Composite) super.createDialogArea(parent); 109 parentComposite.setFont(parent.getFont()); 110 Composite composite= new Composite(parentComposite, SWT.NONE); 111 GridLayout layout = new GridLayout(); 112 layout.numColumns = 3; 113 composite.setLayout(layout); 114 GridData data= new GridData(GridData.FILL_BOTH); 115 data.heightHint= 400; 116 composite.setLayoutData(data); 117 composite.setFont(parent.getFont()); 118 119 Label label= new Label(composite, SWT.WRAP); 120 label.setText(BreakpointGroupMessages.GroupBreakpointsByDialog_0); 121 GridData gridData = new GridData(GridData.FILL_HORIZONTAL); 122 gridData.horizontalSpan = 3; 123 label.setLayoutData(gridData); 124 125 createAvailableViewer(composite, labelProvider); 126 createButtons(composite); 127 createSelectedViewer(composite, labelProvider); 128 129 initializeContent(); 130 updateViewers(); 131 Dialog.applyDialogFont(parentComposite); 132 return parentComposite; 133 } 134 135 138 protected Control createContents(Composite parent) { 139 Control contents = super.createContents(parent); 140 PlatformUI.getWorkbench().getHelpSystem().setHelp(getDialogArea(), IDebugHelpContextIds.GROUP_BREAKPOINTS_DIALOG); 141 return contents; 142 } 143 144 148 private void initializeContent() { 149 IBreakpointOrganizer[] organizers= BreakpointOrganizerManager.getDefault().getOrganizers(); 150 for (int i = 0; i < organizers.length; i++) { 151 fAvailableOrganizersProvider.addAvailable(organizers[i]); 152 } 153 organizers = fView.getBreakpointOrganizers(); 154 if (organizers != null) { 155 for (int i = 0; i < organizers.length; i++) { 156 fSelectedOrganizersProvider.addSelected(organizers[i]); 157 } 158 } 159 } 160 161 165 private void createAvailableViewer(Composite parent, ILabelProvider labelProvider) { 166 Composite availableComposite= new Composite(parent, SWT.NONE); 167 availableComposite.setFont(parent.getFont()); 168 GridLayout layout = new GridLayout(); 169 layout.marginHeight=0; 170 layout.marginWidth=0; 171 availableComposite.setLayout(layout); 172 GridData gridData= new GridData(GridData.FILL_BOTH); 173 gridData.widthHint= 225; 174 availableComposite.setLayoutData(gridData); 175 176 Label label= new Label(availableComposite, SWT.WRAP); 177 label.setText(BreakpointGroupMessages.GroupBreakpointsByDialog_1); 178 gridData = new GridData(GridData.FILL_HORIZONTAL); 179 label.setLayoutData(gridData); 180 181 fAvailableViewer= new TableViewer(availableComposite); 182 fAvailableViewer.setContentProvider(fAvailableOrganizersProvider); 183 fAvailableViewer.setLabelProvider(labelProvider); 184 fAvailableViewer.setInput(new Object ()); 185 Table table = fAvailableViewer.getTable(); 186 table.setLayoutData(new GridData(GridData.FILL_BOTH)); 187 table.setFont(parent.getFont()); 188 fAvailableViewer.addDoubleClickListener(new IDoubleClickListener() { 189 public void doubleClick(DoubleClickEvent event) { 190 handleAddPressed(); 191 } 192 }); 193 fAvailableViewer.addSelectionChangedListener(new ISelectionChangedListener() { 194 public void selectionChanged(SelectionChangedEvent event) { 195 updateAddButton(); 196 } 197 }); 198 } 199 200 204 private void createSelectedViewer(Composite parent, ILabelProvider labelProvider) { 205 Composite selectedComposite= new Composite(parent, SWT.NONE); 206 GridLayout layout = new GridLayout(); 207 layout.marginHeight=0; 208 layout.marginWidth=0; 209 layout.numColumns= 2; 210 selectedComposite.setLayout(layout); 211 GridData gridData= new GridData(GridData.FILL_BOTH); 212 gridData.widthHint= 225; 213 selectedComposite.setLayoutData(gridData); 214 selectedComposite.setFont(parent.getFont()); 215 216 Label label= new Label(selectedComposite, SWT.WRAP); 217 label.setText(BreakpointGroupMessages.GroupBreakpointsByDialog_3); 218 gridData = new GridData(); 219 gridData.horizontalSpan = 2; 220 label.setLayoutData(gridData); 221 222 fSelectedViewer= new TreeViewer(selectedComposite); 223 fSelectedViewer.setContentProvider(fSelectedOrganizersProvider); 224 fSelectedViewer.setLabelProvider(labelProvider); 225 fSelectedViewer.setInput(new Object ()); 226 Tree tree = fSelectedViewer.getTree(); 227 tree.setLayoutData(new GridData(GridData.FILL_BOTH)); 228 tree.setFont(parent.getFont()); 229 fSelectedViewer.addDoubleClickListener(new IDoubleClickListener() { 230 public void doubleClick(DoubleClickEvent event) { 231 handleRemovePressed(); 232 } 233 }); 234 fSelectedViewer.addSelectionChangedListener(new ISelectionChangedListener() { 235 public void selectionChanged(SelectionChangedEvent event) { 236 updateSelectedButtons(); 237 } 238 }); 239 } 240 241 public void createButtons(Composite parent) { 242 Composite buttonComposite= new Composite(parent, SWT.NONE); 243 buttonComposite.setLayout(new GridLayout()); 244 buttonComposite.setLayoutData(new GridData()); 245 buttonComposite.setFont(parent.getFont()); 246 247 fAddButton= SWTFactory.createPushButton(buttonComposite, BreakpointGroupMessages.GroupBreakpointsByDialog_2, null); 248 fAddButton.addSelectionListener(fSelectionListener); 249 250 fRemoveButton= SWTFactory.createPushButton(buttonComposite, BreakpointGroupMessages.GroupBreakpointsByDialog_4, null); 251 fRemoveButton.addSelectionListener(fSelectionListener); 252 253 fMoveUpButton= SWTFactory.createPushButton(buttonComposite, BreakpointGroupMessages.GroupBreakpointsByDialog_5, null); 254 fMoveUpButton.addSelectionListener(fSelectionListener); 255 256 fMoveDownButton= SWTFactory.createPushButton(buttonComposite, BreakpointGroupMessages.GroupBreakpointsByDialog_6, null); 257 fMoveDownButton.addSelectionListener(fSelectionListener); 258 259 } 260 261 267 public IBreakpointOrganizer[] getOrganizers() { 268 return (IBreakpointOrganizer[]) fResult.toArray(new IBreakpointOrganizer[fResult.size()]); 269 } 270 271 274 protected void okPressed() { 275 Object [] factories= fSelectedOrganizersProvider.getElements(null); 276 while (factories.length > 0) { 277 Object factory= factories[0]; 278 fResult.add(factory); 279 factories= fSelectedOrganizersProvider.getChildren(factory); 280 } 281 super.okPressed(); 282 } 283 284 288 public void handleAddPressed() { 289 IStructuredSelection selection= (IStructuredSelection) fAvailableViewer.getSelection(); 290 if (selection.size() < 1) { 291 return; 292 } 293 Iterator iter= selection.iterator(); 294 while (iter.hasNext()) { 295 fSelectedOrganizersProvider.addSelected((IBreakpointOrganizer) iter.next()); 296 } 297 updateViewers(); 298 } 299 300 304 public void handleRemovePressed() { 305 IStructuredSelection selection= (IStructuredSelection) fSelectedViewer.getSelection(); 306 if (selection.size() < 1) { 307 return; 308 } 309 Iterator iter= selection.iterator(); 310 while (iter.hasNext()) { 311 fAvailableOrganizersProvider.addAvailable((IBreakpointOrganizer) iter.next()); 312 } 313 updateViewers(); 314 } 315 316 319 public void handleMoveUpPressed() { 320 IStructuredSelection selection = (IStructuredSelection) fSelectedViewer.getSelection(); 321 Iterator iter = selection.iterator(); 322 while (iter.hasNext()) { 323 fSelectedOrganizersProvider.moveUp(iter.next()); 324 } 325 updateViewers(); 326 } 327 328 331 public void handleMoveDownPressed() { 332 IStructuredSelection selection = (IStructuredSelection) fSelectedViewer.getSelection(); 333 Object [] elements= selection.toArray(); 334 for (int i= elements.length - 1; i >= 0; i--) { 335 fSelectedOrganizersProvider.moveDown(elements[i]); 336 } 337 updateViewers(); 338 } 339 340 343 public void updateViewers() { 344 fAvailableViewer.refresh(); 345 fSelectedViewer.refresh(); 346 fSelectedViewer.expandAll(); 347 updateAddButton(); 348 updateSelectedButtons(); 349 } 350 351 354 public void updateSelectedButtons() { 355 updateRemoveButton(); 356 updateMoveUpButton(); 357 updateMoveDownButton(); 358 } 359 360 public void updateAddButton() { 361 IStructuredSelection selection = (IStructuredSelection) fAvailableViewer.getSelection(); 362 fAddButton.setEnabled(selection.size() > 0); 363 } 364 365 public void updateRemoveButton() { 366 IStructuredSelection selection = (IStructuredSelection) fSelectedViewer.getSelection(); 367 fRemoveButton.setEnabled(selection.size() > 0); 368 } 369 370 public void updateMoveUpButton() { 371 boolean enabled= true; 372 IStructuredSelection selection = (IStructuredSelection) fSelectedViewer.getSelection(); 373 if (selection.size() == 0) { 374 enabled= false; 375 } else { 376 Object firstSelected= selection.getFirstElement(); 377 Object parent= fSelectedOrganizersProvider.getParent(firstSelected); 378 if (!(parent instanceof IBreakpointOrganizer)) { 379 enabled= false; 380 } 381 } 382 fMoveUpButton.setEnabled(enabled); 383 } 384 385 public void updateMoveDownButton() { 386 boolean enabled= true; 387 IStructuredSelection selection = (IStructuredSelection) fSelectedViewer.getSelection(); 388 if (selection.size() == 0) { 389 enabled= false; 390 } else { 391 Object lastSelected= selection.toList().get(selection.size() - 1); 392 Object [] children= fSelectedOrganizersProvider.getChildren(lastSelected); 393 if (children.length < 1) { 394 enabled= false; 395 } 396 } 397 fMoveDownButton.setEnabled(enabled); 398 } 399 400 404 private class AvailableOrganizersProvider implements IStructuredContentProvider { 405 protected List availableOrganziers= new ArrayList (); 406 407 public void addAvailable(IBreakpointOrganizer organizer) { 408 availableOrganziers.add(organizer); 409 fSelectedOrganizersProvider.selectedOrganizers.remove(organizer); 410 } 411 412 public Object [] getElements(Object inputElement) { 413 return availableOrganziers.toArray(); 414 } 415 416 public void dispose() { 417 } 418 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 419 } 420 } 421 422 427 private class SelectedOrganizerProvider implements ITreeContentProvider { 428 protected List selectedOrganizers= new ArrayList (); 429 430 public void addSelected(IBreakpointOrganizer organizer) { 431 selectedOrganizers.add(organizer); 432 fAvailableOrganizersProvider.availableOrganziers.remove(organizer); 433 } 434 435 public void moveUp(Object object) { 436 int index = selectedOrganizers.indexOf(object); 437 if (index > 0) { 438 selectedOrganizers.remove(object); 439 selectedOrganizers.add(index - 1, object); 440 } 441 } 442 443 public void moveDown(Object object) { 444 int index = selectedOrganizers.indexOf(object); 445 if (index < selectedOrganizers.size() - 1) { 446 selectedOrganizers.remove(object); 447 selectedOrganizers.add(index + 1, object); 448 } 449 } 450 451 public Object [] getChildren(Object parentElement) { 452 int index = selectedOrganizers.indexOf(parentElement); 454 if (index < selectedOrganizers.size() - 1) { 455 return new Object [] { selectedOrganizers.get(index + 1) }; 456 } 457 return new Object [0]; 458 } 459 460 public Object getParent(Object element) { 461 int index = selectedOrganizers.indexOf(element); 463 if (index <= 0 || selectedOrganizers.size() <= 1) { 464 return null; 465 } 466 return selectedOrganizers.get(index - 1); 467 } 468 469 public boolean hasChildren(Object element) { 470 int index = selectedOrganizers.indexOf(element); 473 return index != -1 && index < selectedOrganizers.size() - 1; 474 } 475 476 public Object [] getElements(Object inputElement) { 477 if (selectedOrganizers.size() > 0) { 478 return new Object [] { selectedOrganizers.get(0) }; 479 } 480 return new Object [0]; 481 } 482 public void dispose() { 483 } 484 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 485 } 486 } 487 488 491 private class BreakpointOrganzierLabelProvider extends LabelProvider { 492 private HashMap fImageCache= new HashMap (); 493 494 public String getText(Object element) { 495 if (element instanceof IBreakpointOrganizer) { 496 return ((IBreakpointOrganizer) element).getLabel(); 497 } 498 return super.getText(element); 499 } 500 public Image getImage(Object element) { 501 if (element instanceof IBreakpointOrganizer) { 502 ImageDescriptor imageDescriptor = ((IBreakpointOrganizer) element).getImageDescriptor(); 503 if (imageDescriptor != null) { 504 Image image = (Image) fImageCache.get(imageDescriptor); 505 if (image == null) { 506 image= imageDescriptor.createImage(); 507 if (image != null) { 508 fImageCache.put(imageDescriptor, image); 509 } 510 } 511 return image; 512 } 513 } 514 return super.getImage(element); 515 } 516 public void dispose() { 517 Iterator imageIter = fImageCache.values().iterator(); 518 while (imageIter.hasNext()) { 519 ((Image) imageIter.next()).dispose(); 520 } 521 super.dispose(); 522 } 523 } 524 525 530 protected void configureShell(Shell shell) { 531 super.configureShell(shell); 532 shell.setText(BreakpointGroupMessages.GroupBreakpointsByDialog_7); 533 } 534 } 535 | Popular Tags |