KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > InstanceFiltersAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.jdt.internal.debug.ui.actions;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.debug.core.DebugPlugin;
20 import org.eclipse.debug.core.model.IBreakpoint;
21 import org.eclipse.debug.core.model.IValue;
22 import org.eclipse.debug.ui.DebugUITools;
23 import org.eclipse.debug.ui.IDebugModelPresentation;
24 import org.eclipse.jdt.debug.core.IJavaBreakpoint;
25 import org.eclipse.jdt.debug.core.IJavaClassType;
26 import org.eclipse.jdt.debug.core.IJavaExceptionBreakpoint;
27 import org.eclipse.jdt.debug.core.IJavaFieldVariable;
28 import org.eclipse.jdt.debug.core.IJavaObject;
29 import org.eclipse.jdt.debug.core.IJavaType;
30 import org.eclipse.jdt.debug.core.IJavaVariable;
31 import org.eclipse.jdt.debug.core.IJavaWatchpoint;
32 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
33 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds;
34 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
35 import org.eclipse.jface.action.IAction;
36 import org.eclipse.jface.dialogs.IDialogSettings;
37 import org.eclipse.jface.dialogs.MessageDialog;
38 import org.eclipse.jface.viewers.ILabelProvider;
39 import org.eclipse.jface.viewers.IStructuredContentProvider;
40 import org.eclipse.jface.viewers.IStructuredSelection;
41 import org.eclipse.jface.viewers.Viewer;
42 import org.eclipse.jface.window.Window;
43 import org.eclipse.swt.SWT;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Control;
46 import org.eclipse.swt.widgets.Shell;
47 import org.eclipse.ui.PlatformUI;
48 import org.eclipse.ui.dialogs.ListSelectionDialog;
49
50 import com.ibm.icu.text.MessageFormat;
51
52 /**
53  * Action to associate an object with one or more breakpoints.
54  */

55 public class InstanceFiltersAction extends ObjectActionDelegate {
56     
57     class InstanceFilterDialog extends ListSelectionDialog {
58         
59         public InstanceFilterDialog(
60             Shell parentShell,
61             Object JavaDoc input,
62             IStructuredContentProvider contentProvider,
63             ILabelProvider labelProvider,
64             String JavaDoc message) {
65             super(parentShell, input, contentProvider, labelProvider, message);
66             setShellStyle(getShellStyle() | SWT.RESIZE);
67         }
68         
69         
70
71         /* (non-Javadoc)
72          * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
73          */

74         protected Control createDialogArea(Composite parent) {
75             Control control = super.createDialogArea(parent);
76             PlatformUI.getWorkbench().getHelpSystem().setHelp(
77                 parent,
78                 IJavaDebugHelpContextIds.INSTANCE_BREAKPOINT_SELECTION_DIALOG);
79             return control;
80             
81         }
82
83         protected String JavaDoc getDialogSettingsSectionName() {
84             return IJavaDebugUIConstants.PLUGIN_ID + ".INSTANCE_FILTERS_ACTION_DIALOG"; //$NON-NLS-1$
85
}
86         
87          /* (non-Javadoc)
88          * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsSettings()
89          */

90         protected IDialogSettings getDialogBoundsSettings() {
91             IDialogSettings settings = JDIDebugUIPlugin.getDefault().getDialogSettings();
92             IDialogSettings section = settings.getSection(getDialogSettingsSectionName());
93             if (section == null) {
94                 section = settings.addNewSection(getDialogSettingsSectionName());
95             }
96             return section;
97         }
98 }
99
100     /**
101      * @see org.eclipse.ui.IActionDelegate#run(IAction)
102      */

103     public void run(IAction action) {
104         IStructuredSelection selection = getCurrentSelection();
105         if (selection == null || selection.size() > 1) {
106             return;
107         }
108         
109         Object JavaDoc o = selection.getFirstElement();
110         if (o instanceof IJavaVariable) {
111             final IJavaVariable var = (IJavaVariable)o;
112             try {
113                 IValue value = var.getValue();
114                 if (value instanceof IJavaObject) {
115                     final IJavaObject object = (IJavaObject)value;
116                     final List JavaDoc breakpoints = getApplicableBreakpoints(var, object);
117                     IStructuredContentProvider content = new IStructuredContentProvider() {
118                         public void dispose() {}
119                         
120                         public Object JavaDoc[] getElements(Object JavaDoc input) {
121                             return breakpoints.toArray();
122                         }
123                         
124                         public void inputChanged(Viewer viewer, Object JavaDoc a, Object JavaDoc b) {}
125                     };
126                     final IDebugModelPresentation modelPresentation= DebugUITools.newDebugModelPresentation();
127                     ListSelectionDialog dialog = new InstanceFilterDialog(JDIDebugUIPlugin.getActiveWorkbenchShell(), breakpoints, content, modelPresentation, MessageFormat.format(ActionMessages.InstanceFiltersAction_1, new String JavaDoc[] {var.getName()})){
128                         public void okPressed() {
129                             // check if breakpoints have already been restricted to other objects.
130
Object JavaDoc[] checkBreakpoint= getViewer().getCheckedElements();
131                             for (int k= 0; k < checkBreakpoint.length; k++) {
132                                 IJavaBreakpoint breakpoint= (IJavaBreakpoint) checkBreakpoint[k];
133                                 try {
134                                     IJavaObject[] instanceFilters= breakpoint.getInstanceFilters();
135                                     boolean sameTarget = false;
136                                     for (int i = 0; i < instanceFilters.length; i++) {
137                                         IJavaObject instanceFilter = instanceFilters[i];
138                                         if (instanceFilter.getDebugTarget().equals(object.getDebugTarget())) {
139                                             sameTarget = true;
140                                             break;
141                                         }
142                                     }
143                                     if (sameTarget) {
144                                         MessageDialog messageDialog= new MessageDialog(JDIDebugUIPlugin.getActiveWorkbenchShell(), ActionMessages.InstanceFiltersAction_2,
145                                             null, MessageFormat.format(ActionMessages.InstanceFiltersAction_3, new String JavaDoc[] { modelPresentation.getText(breakpoint), var.getName()}),
146                                             MessageDialog.QUESTION, new String JavaDoc[] { ActionMessages.InstanceFiltersAction_Yes_2, ActionMessages.InstanceFiltersAction_Cancel_3}, //
147
0);
148                                         if (messageDialog.open() == Window.OK) {
149                                             for (int i= 0; i < instanceFilters.length; i++) {
150                                                 breakpoint.removeInstanceFilter(instanceFilters[i]);
151                                             }
152                                         } else {
153                                             // if 'cancel', do not close the instance filter dialog
154
return;
155                                         }
156                                     }
157                                 } catch (CoreException e) {
158                                     JDIDebugUIPlugin.log(e);
159                                 }
160                             }
161                             super.okPressed();
162                         }
163                     };
164                     dialog.setTitle(ActionMessages.InstanceFiltersAction_2);
165                     
166                     // determine initial selection
167
List JavaDoc existing = new ArrayList JavaDoc();
168                     Iterator JavaDoc iter = breakpoints.iterator();
169                     while (iter.hasNext()) {
170                         IJavaBreakpoint bp = (IJavaBreakpoint)iter.next();
171                         IJavaObject[] filters = bp.getInstanceFilters();
172                         for (int i = 0; i < filters.length; i++) {
173                             if (filters[i].equals(object)) {
174                                 existing.add(bp);
175                                 break;
176                             }
177                         }
178                     }
179                     dialog.setInitialSelections(existing.toArray());
180                     
181                     if (dialog.open() == Window.OK) {
182                         Object JavaDoc[] selectedBreakpoints = dialog.getResult();
183                         if (selectedBreakpoints != null) {
184                             // add
185
for (int i = 0; i < selectedBreakpoints.length; i++) {
186                                 IJavaBreakpoint bp = (IJavaBreakpoint)selectedBreakpoints[i];
187                                 bp.addInstanceFilter(object);
188                                 existing.remove(bp);
189                             }
190                             // remove
191
iter = existing.iterator();
192                             while (iter.hasNext()) {
193                                 IJavaBreakpoint bp = (IJavaBreakpoint)iter.next();
194                                 bp.removeInstanceFilter(object);
195                             }
196                         }
197                     }
198                 } else {
199                     // only allowed for objects
200
}
201             } catch (CoreException e) {
202                 JDIDebugUIPlugin.log(e);
203             }
204         }
205     }
206     
207     protected List JavaDoc getApplicableBreakpoints(IJavaVariable variable, IJavaObject object) {
208         List JavaDoc breakpoints = new ArrayList JavaDoc();
209         
210         try {
211             // collect names in type hierarchy
212
List JavaDoc superTypeNames = new ArrayList JavaDoc();
213             IJavaType type = object.getJavaType();
214             while (type instanceof IJavaClassType) {
215                 superTypeNames.add(type.getName());
216                 type = ((IJavaClassType)type).getSuperclass();
217             }
218             
219             IBreakpoint[] allBreakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints();
220             for (int i = 0; i < allBreakpoints.length; i++) {
221                 if (allBreakpoints[i] instanceof IJavaBreakpoint) {
222                     IJavaBreakpoint jbp = (IJavaBreakpoint)allBreakpoints[i];
223                     IJavaBreakpoint valid = null;
224                     if (jbp instanceof IJavaWatchpoint && variable instanceof IJavaFieldVariable) {
225                         IJavaWatchpoint wp = (IJavaWatchpoint)jbp;
226                         IJavaFieldVariable fv = (IJavaFieldVariable)variable;
227                         if (variable.getName().equals(wp.getFieldName()) && fv.getDeclaringType().getName().equals(wp.getTypeName())) {
228                             valid = wp;
229                         }
230                     } else if (superTypeNames.contains(jbp.getTypeName()) || jbp instanceof IJavaExceptionBreakpoint) {
231                         valid = jbp;
232                     }
233                     if (valid != null && valid.supportsInstanceFilters()) {
234                         breakpoints.add(valid);
235                     }
236                 }
237             }
238         } catch (CoreException e) {
239             JDIDebugUIPlugin.log(e);
240         }
241          
242         return breakpoints;
243     }
244
245 }
246
Popular Tags