KickJava   Java API By Example, From Geeks To Geeks.

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


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.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  * <p>
31  * EXPERIMENTAL
32  * </p>
33  * <p>
34  * Clients may subclass this class.
35  * </p>
36  * @since 3.0
37  */

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

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

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

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

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

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

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

118     public void runWithEvent(IAction action, Event event) {
119         run(action);
120     }
121 }
122
Popular Tags