KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > ToggleStepFiltersActionDelegate


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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;
12
13 import org.eclipse.debug.core.ILaunch;
14 import org.eclipse.debug.core.model.IDebugElement;
15 import org.eclipse.debug.core.model.IDebugTarget;
16 import org.eclipse.debug.core.model.IProcess;
17 import org.eclipse.debug.core.model.IStepFilters;
18 import org.eclipse.debug.internal.ui.DebugUIPlugin;
19 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
20 import org.eclipse.debug.ui.DebugUITools;
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.util.IPropertyChangeListener;
24 import org.eclipse.jface.util.PropertyChangeEvent;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.ui.IActionDelegate2;
28
29 /**
30  * Turns step filters on/off for a selected target.
31  */

32 public class ToggleStepFiltersActionDelegate extends AbstractDebugActionDelegate implements IActionDelegate2, IPropertyChangeListener {
33     
34     /* (non-Javadoc)
35      * @see org.eclipse.debug.internal.ui.actions.AbstractDebugActionDelegate#doAction(java.lang.Object)
36      */

37     protected void doAction(Object JavaDoc element) {
38         // do nothing - we override #run(IAction)
39
}
40
41     /* (non-Javadoc)
42      * @see org.eclipse.ui.IActionDelegate2#init(org.eclipse.jface.action.IAction)
43      */

44     public void init(IAction action) {
45         setAction(action);
46         action.setChecked(isUseStepFilters());
47         getPreferenceStore().addPropertyChangeListener(this);
48     }
49     
50     private boolean isUseStepFilters() {
51         return DebugUIPlugin.getDefault().getStepFilterManager().isUseStepFilters();
52     }
53     
54     private IPreferenceStore getPreferenceStore() {
55         return DebugUIPlugin.getDefault().getPreferenceStore();
56     }
57
58     /* (non-Javadoc)
59      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
60      */

61     public void propertyChange(PropertyChangeEvent event) {
62         if (event.getProperty().equals(IInternalDebugUIConstants.PREF_USE_STEP_FILTERS)) {
63             Object JavaDoc newValue= event.getNewValue();
64             if (newValue instanceof Boolean JavaDoc) {
65                 getAction().setChecked(((Boolean JavaDoc)(newValue)).booleanValue());
66             } else if (newValue instanceof String JavaDoc) {
67                 getAction().setChecked(Boolean.valueOf((String JavaDoc)newValue).booleanValue());
68             }
69         }
70     }
71     
72     /* (non-Javadoc)
73      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
74      */

75     public void dispose() {
76         super.dispose();
77         getPreferenceStore().removePropertyChangeListener(this);
78     }
79     
80     /* (non-Javadoc)
81      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
82      */

83     public void run(IAction action) {
84         DebugUITools.setUseStepFilters(action.isChecked());
85     }
86     
87     /* (non-Javadoc)
88      * @see org.eclipse.debug.internal.ui.actions.AbstractDebugActionDelegate#initialize(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
89      */

90     protected boolean initialize(IAction action, ISelection selection) {
91         boolean res = super.initialize(action, selection);
92         init(action);
93         return res;
94     }
95     
96     /* (non-Javadoc)
97      * @see org.eclipse.debug.internal.ui.actions.AbstractDebugActionDelegate#update(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
98      */

99     protected void update(IAction action, ISelection s) {
100         boolean enabled = true;
101         if (s != null && !s.isEmpty()) {
102             if (s instanceof IStructuredSelection) {
103                 IStructuredSelection ss = (IStructuredSelection) s;
104                 if (ss.size() == 1) {
105                     Object JavaDoc element = ss.getFirstElement();
106                     IDebugTarget[] debugTargets = getDebugTargets(element);
107                     for (int i = 0; i < debugTargets.length; i++) {
108                         IDebugTarget target = debugTargets[i];
109
110                         if (target instanceof IStepFilters) {
111                             IStepFilters filters = (IStepFilters) target;
112                             if (filters.supportsStepFilters()) {
113                                 enabled = true;
114                                 break; // found one that's enough.
115
}
116                         }
117                         // iff there is a valid selection, but no
118
// targets support step filters, disable
119
// the button.
120
enabled = false;
121                     }
122                 }
123             }
124         }
125         action.setEnabled(enabled);
126     }
127     
128     private IDebugTarget[] getDebugTargets(Object JavaDoc element) {
129         if (element instanceof IDebugElement) {
130             IDebugElement debugElement = (IDebugElement) element;
131             return new IDebugTarget[] {debugElement.getDebugTarget()};
132         } else if (element instanceof ILaunch) {
133             ILaunch launch = (ILaunch) element;
134             return launch.getDebugTargets();
135         } else if (element instanceof IProcess) {
136             IProcess process = (IProcess) element;
137             return process.getLaunch().getDebugTargets();
138         } else {
139             return new IDebugTarget[0];
140         }
141     }
142
143 }
144
Popular Tags