KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > breakpoints > ToggleBreakpointObjectActionDelegate


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.breakpoints;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.core.runtime.IAdapterManager;
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.debug.core.DebugPlugin;
18 import org.eclipse.debug.ui.actions.IToggleBreakpointsTarget;
19 import org.eclipse.jface.action.IAction;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.swt.widgets.Event;
23 import org.eclipse.ui.IActionDelegate2;
24 import org.eclipse.ui.IObjectActionDelegate;
25 import org.eclipse.ui.IWorkbenchPart;
26
27 /**
28  * A toggle breakpoint action that can be contributed to an object. The action
29  * will perform a toggle breakpoint operation for a selected object.
30  *
31  * <p>Clients may subclass this class.</p>
32  *
33  * @since 3.0
34  */

35 public abstract class ToggleBreakpointObjectActionDelegate implements IObjectActionDelegate, IActionDelegate2 {
36     
37     private IWorkbenchPart fPart;
38     private IStructuredSelection fSelection;
39     
40     /* (non-Javadoc)
41      * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
42      */

43     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
44         fPart = targetPart;
45     }
46     /* (non-Javadoc)
47      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
48      */

49     public void run(IAction action) {
50         IAdaptable adaptable = (IAdaptable) fSelection.getFirstElement();
51         IToggleBreakpointsTarget target = (IToggleBreakpointsTarget) adaptable.getAdapter(IToggleBreakpointsTarget.class);
52         if (target == null) {
53             IAdapterManager adapterManager = Platform.getAdapterManager();
54             target = (IToggleBreakpointsTarget) adapterManager.loadAdapter(adaptable, IToggleBreakpointsTarget.class.getName());
55         }
56         if (target != null) {
57             try {
58                 performAction(target, fPart, fSelection);
59             } catch (CoreException e) {
60                 DebugPlugin.log(e);
61             }
62         }
63     }
64     
65     /**
66      * Performs the operation specific to this action.
67      *
68      * @param target adapter to toggle breakpoints
69      * @param part the active part
70      * @param selection the seleciton in the active part
71      * @exception CoreException if an exception occurrs
72      */

73     protected abstract void performAction(IToggleBreakpointsTarget target, IWorkbenchPart part, ISelection selection) throws CoreException;
74     
75     /* (non-Javadoc)
76      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
77      */

78     public void selectionChanged(IAction action, ISelection selection) {
79         boolean enabled = false;
80         if (selection instanceof IStructuredSelection) {
81             IStructuredSelection ss = (IStructuredSelection) selection;
82             this.fSelection = ss;
83             if (!ss.isEmpty()) {
84                 Object JavaDoc object = ss.getFirstElement();
85                 if (object instanceof IAdaptable) {
86                     IAdaptable adaptable = (IAdaptable) object;
87                     IToggleBreakpointsTarget target = (IToggleBreakpointsTarget) adaptable.getAdapter(IToggleBreakpointsTarget.class);
88                     if (target == null) {
89                         IAdapterManager adapterManager = Platform.getAdapterManager();
90                         enabled = adapterManager.hasAdapter(adaptable, IToggleBreakpointsTarget.class.getName());
91                     } else {
92                         enabled = true;
93                     }
94                 }
95             }
96         }
97         action.setEnabled(enabled);
98     }
99     /* (non-Javadoc)
100      * @see org.eclipse.ui.IActionDelegate2#init(org.eclipse.jface.action.IAction)
101      */

102     public void init(IAction action) {
103     }
104     
105     /* (non-Javadoc)
106      * @see org.eclipse.ui.IActionDelegate2#dispose()
107      */

108     public void dispose() {
109         fSelection = null;
110         fPart = null;
111     }
112     /* (non-Javadoc)
113      * @see org.eclipse.ui.IActionDelegate2#runWithEvent(org.eclipse.jface.action.IAction, org.eclipse.swt.widgets.Event)
114      */

115     public void runWithEvent(IAction action, Event event) {
116         run(action);
117     }
118 }
119
Popular Tags