KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > breakpointGroups > GroupBreakpointsByDialog


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.actions.breakpointGroups;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
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 /**
54  * Dialog which presents available breakpoint groupings to
55  * the user and allows them to specify which they'd like
56  * to use and in what order they should be applied.
57  */

58 public class GroupBreakpointsByDialog extends TrayDialog {
59     
60     private BreakpointsView fView;
61     
62     // Table viewer that presents available containers
63
private TableViewer fAvailableViewer;
64     private AvailableOrganizersProvider fAvailableOrganizersProvider= new AvailableOrganizersProvider();
65     
66     // Tree viewer that presents selected containers
67
private TreeViewer fSelectedViewer;
68     private SelectedOrganizerProvider fSelectedOrganizersProvider= new SelectedOrganizerProvider();
69     
70     private List JavaDoc fResult= new ArrayList JavaDoc();
71
72     private Button fAddButton;
73     private Button fRemoveButton;
74     private Button fMoveUpButton;
75     private Button fMoveDownButton;
76     
77     /**
78      * Selection listener that listens to selection from all buttons in this
79      * dialog.
80      */

81     private SelectionAdapter fSelectionListener= new SelectionAdapter() {
82         public void widgetSelected(SelectionEvent e) {
83             Object JavaDoc 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     /* (non-Javadoc)
103      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
104      */

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     /* (non-Javadoc)
136      * @see org.eclipse.jface.dialogs.Dialog#createContents(org.eclipse.swt.widgets.Composite)
137      */

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     /**
145      * Divides the available breakpoint container factories into the
146      * appropriate viewers ("available" or "selected").
147      */

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     /**
162      * Creates and configured the viewer that shows the available (not currently selected)
163      * breakpoint container factories.
164      */

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 JavaDoc());
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     /**
201      * Creates and configures the viewer that shows the currently selected
202      * breakpoint container factories.
203      */

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 JavaDoc());
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     /**
262      * Returns the organizers chosen by the user. The order
263      * of the list is the order that the organizers should be displayed
264      * in the breakpoints view.
265      * @return the breakpoint organizers chosen by the user
266      */

267     public IBreakpointOrganizer[] getOrganizers() {
268         return (IBreakpointOrganizer[]) fResult.toArray(new IBreakpointOrganizer[fResult.size()]);
269     }
270     
271     /**
272      * When the user presses OK, convert the tree selection into a list.
273      */

274     protected void okPressed() {
275         Object JavaDoc[] factories= fSelectedOrganizersProvider.getElements(null);
276         while (factories.length > 0) {
277             Object JavaDoc factory= factories[0];
278             fResult.add(factory);
279             factories= fSelectedOrganizersProvider.getChildren(factory);
280         }
281         super.okPressed();
282     }
283     
284     /**
285      * Moves the selected item from the list of "available" factories
286      * to the tree of "selected" factories.
287      */

288     public void handleAddPressed() {
289         IStructuredSelection selection= (IStructuredSelection) fAvailableViewer.getSelection();
290         if (selection.size() < 1) {
291             return;
292         }
293         Iterator JavaDoc iter= selection.iterator();
294         while (iter.hasNext()) {
295             fSelectedOrganizersProvider.addSelected((IBreakpointOrganizer) iter.next());
296         }
297         updateViewers();
298     }
299     
300     /**
301      * Moves the selected item from the tree of "selected" factories
302      * to the list of "available" factories.
303      */

304     public void handleRemovePressed() {
305         IStructuredSelection selection= (IStructuredSelection) fSelectedViewer.getSelection();
306         if (selection.size() < 1) {
307             return;
308         }
309         Iterator JavaDoc iter= selection.iterator();
310         while (iter.hasNext()) {
311             fAvailableOrganizersProvider.addAvailable((IBreakpointOrganizer) iter.next());
312         }
313         updateViewers();
314     }
315     
316     /**
317      * Moves each selected item up in the tree of selected containers
318      */

319     public void handleMoveUpPressed() {
320         IStructuredSelection selection = (IStructuredSelection) fSelectedViewer.getSelection();
321         Iterator JavaDoc iter = selection.iterator();
322         while (iter.hasNext()) {
323             fSelectedOrganizersProvider.moveUp(iter.next());
324         }
325         updateViewers();
326     }
327     
328     /**
329      * Moves each selected item down in the tree of selected containers
330      */

331     public void handleMoveDownPressed() {
332         IStructuredSelection selection = (IStructuredSelection) fSelectedViewer.getSelection();
333         Object JavaDoc[] elements= selection.toArray();
334         for (int i= elements.length - 1; i >= 0; i--) {
335             fSelectedOrganizersProvider.moveDown(elements[i]);
336         }
337         updateViewers();
338     }
339     
340     /**
341      * Fully refreshes and updates all viewers and buttons.
342      */

343     public void updateViewers() {
344         fAvailableViewer.refresh();
345         fSelectedViewer.refresh();
346         fSelectedViewer.expandAll();
347         updateAddButton();
348         updateSelectedButtons();
349     }
350     
351     /**
352      * Updates all buttons associated with the tree of selected containers.
353      */

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 JavaDoc firstSelected= selection.getFirstElement();
377             Object JavaDoc 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 JavaDoc lastSelected= selection.toList().get(selection.size() - 1);
392             Object JavaDoc[] children= fSelectedOrganizersProvider.getChildren(lastSelected);
393             if (children.length < 1) {
394                 enabled= false;
395             }
396         }
397         fMoveDownButton.setEnabled(enabled);
398     }
399     
400     /**
401      * Content provider that provides the list of breakpoint organaisers
402      * that are available but not currently selected.
403      */

404     private class AvailableOrganizersProvider implements IStructuredContentProvider {
405         protected List JavaDoc availableOrganziers= new ArrayList JavaDoc();
406         
407         public void addAvailable(IBreakpointOrganizer organizer) {
408             availableOrganziers.add(organizer);
409             fSelectedOrganizersProvider.selectedOrganizers.remove(organizer);
410         }
411         
412         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
413             return availableOrganziers.toArray();
414         }
415         
416         public void dispose() {
417         }
418         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
419         }
420     }
421     
422     /**
423      * Content provider that returns the selected breakpoint organizers
424      * as a tree. This tree shows the list of organzizers as they will
425      * appear in the breakpoints view.
426      */

427     private class SelectedOrganizerProvider implements ITreeContentProvider {
428         protected List JavaDoc selectedOrganizers= new ArrayList JavaDoc();
429         
430         public void addSelected(IBreakpointOrganizer organizer) {
431             selectedOrganizers.add(organizer);
432             fAvailableOrganizersProvider.availableOrganziers.remove(organizer);
433         }
434         
435         public void moveUp(Object JavaDoc 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 JavaDoc 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 JavaDoc[] getChildren(Object JavaDoc parentElement) {
452             // A factory's "child" is the next factory in the list
453
int index = selectedOrganizers.indexOf(parentElement);
454             if (index < selectedOrganizers.size() - 1) {
455                 return new Object JavaDoc[] { selectedOrganizers.get(index + 1) };
456             }
457             return new Object JavaDoc[0];
458         }
459
460         public Object JavaDoc getParent(Object JavaDoc element) {
461             // A factory's "parent" is the factory before it
462
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 JavaDoc element) {
470             // A factory has "children" if there are more
471
// factories after it.
472
int index = selectedOrganizers.indexOf(element);
473             return index != -1 && index < selectedOrganizers.size() - 1;
474         }
475
476         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
477             if (selectedOrganizers.size() > 0) {
478                 return new Object JavaDoc[] { selectedOrganizers.get(0) };
479             }
480             return new Object JavaDoc[0];
481         }
482         public void dispose() {
483         }
484         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
485         }
486     }
487     
488     /**
489      * Label provider which provides text and images for breakpoint container factories
490      */

491     private class BreakpointOrganzierLabelProvider extends LabelProvider {
492         private HashMap JavaDoc fImageCache= new HashMap JavaDoc();
493         
494         public String JavaDoc getText(Object JavaDoc element) {
495             if (element instanceof IBreakpointOrganizer) {
496                 return ((IBreakpointOrganizer) element).getLabel();
497             }
498             return super.getText(element);
499         }
500         public Image getImage(Object JavaDoc 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 JavaDoc imageIter = fImageCache.values().iterator();
518             while (imageIter.hasNext()) {
519                 ((Image) imageIter.next()).dispose();
520             }
521             super.dispose();
522         }
523     }
524     
525     /*
526      * (non-Javadoc)
527      *
528      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
529      */

530     protected void configureShell(Shell shell) {
531         super.configureShell(shell);
532         shell.setText(BreakpointGroupMessages.GroupBreakpointsByDialog_7);
533     }
534 }
535
Popular Tags