KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > core > commands > DebugCommand


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.core.commands;
12
13 import java.util.LinkedHashSet JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.core.runtime.jobs.ISchedulingRule;
20 import org.eclipse.core.runtime.jobs.Job;
21 import org.eclipse.debug.core.IRequest;
22 import org.eclipse.debug.core.commands.IDebugCommandHandler;
23 import org.eclipse.debug.core.commands.IDebugCommandRequest;
24 import org.eclipse.debug.core.commands.IEnabledStateRequest;
25 import org.eclipse.debug.internal.core.DebugOptions;
26
27 /**
28  * Common function for standard debug commands.
29  *
30  * @since 3.3
31  *
32  */

33 public abstract class DebugCommand implements IDebugCommandHandler {
34     
35     /**
36      * Scheduling rule to serialize commands on an object
37      */

38    class SerialPerObjectRule implements ISchedulingRule {
39
40         private Object JavaDoc fObject = null;
41
42         public SerialPerObjectRule(Object JavaDoc lock) {
43             fObject = lock;
44         }
45
46         /*
47          * (non-Javadoc)
48          *
49          * @see org.eclipse.core.runtime.jobs.ISchedulingRule#contains(org.eclipse.core.runtime.jobs.ISchedulingRule)
50          */

51         public boolean contains(ISchedulingRule rule) {
52             return rule == this;
53         }
54
55         /*
56          * (non-Javadoc)
57          *
58          * @see org.eclipse.core.runtime.jobs.ISchedulingRule#isConflicting(org.eclipse.core.runtime.jobs.ISchedulingRule)
59          */

60         public boolean isConflicting(ISchedulingRule rule) {
61             if (rule instanceof SerialPerObjectRule) {
62                 SerialPerObjectRule vup = (SerialPerObjectRule) rule;
63                 return fObject == vup.fObject;
64             }
65             return false;
66         }
67
68     }
69    
70     public boolean execute(final IDebugCommandRequest request) {
71         Job job = new Job(getExecuteTaskName()) {
72             protected IStatus run(IProgressMonitor monitor) {
73                 if (DebugOptions.DEBUG_COMMANDS) {
74                     System.out.println("execute: " + DebugCommand.this); //$NON-NLS-1$
75
}
76                 Object JavaDoc[] elements = request.getElements();
77                 Object JavaDoc[] targets = new Object JavaDoc[elements.length];
78                 for (int i = 0; i < elements.length; i++) {
79                     targets[i]= getTarget(elements[i]);
80                 }
81                 targets = coalesce(targets);
82                 monitor.beginTask(getExecuteTaskName(), targets.length);
83                 try {
84                     doExecute(targets, monitor, request);
85                 } catch (CoreException e) {
86                     request.setStatus(e.getStatus());
87                     if (DebugOptions.DEBUG_COMMANDS) {
88                         System.out.println("\t" + e.getStatus().getMessage()); //$NON-NLS-1$
89
}
90                 }
91                 request.done();
92                 monitor.setCanceled(request.isCanceled());
93                 monitor.done();
94                 return Status.OK_STATUS;
95             }
96         };
97         job.setSystem(true);
98         job.schedule();
99         return isRemainEnabled();
100     }
101     
102     /**
103      * Returns whether this command should remain enabled after execution is invoked.
104      *
105      * @return whether to remain enabled
106      */

107     protected boolean isRemainEnabled() {
108         return false;
109     }
110     
111     public void canExecute(final IEnabledStateRequest request) {
112         Job job = new Job(getEnablementTaskName()) {
113             protected IStatus run(IProgressMonitor monitor) {
114                 if (DebugOptions.DEBUG_COMMANDS) {
115                     System.out.print("can execute command: " + DebugCommand.this); //$NON-NLS-1$
116
}
117                 Object JavaDoc[] elements = request.getElements();
118                 Object JavaDoc[] targets = new Object JavaDoc[elements.length];
119                 for (int i = 0; i < elements.length; i++) {
120                     targets[i] = getTarget(elements[i]);
121                     if (targets[i] == null) {
122                         request.setEnabled(false);
123                         request.cancel();
124                         if (DebugOptions.DEBUG_COMMANDS) {
125                             System.out.println(" >> false (no adapter)"); //$NON-NLS-1$
126
}
127                     }
128                 }
129                 if (!request.isCanceled()) {
130                     targets = coalesce(targets);
131                     monitor.beginTask(getEnablementTaskName(), targets.length);
132                     try {
133                         boolean executable = isExecutable(targets, monitor, request);
134                         if (DebugOptions.DEBUG_COMMANDS) {
135                             System.out.println(" >> " + executable); //$NON-NLS-1$
136
}
137                         request.setEnabled(executable);
138                     } catch (CoreException e) {
139                         request.setStatus(e.getStatus());
140                         request.setEnabled(false);
141                         if (DebugOptions.DEBUG_COMMANDS) {
142                             System.out.println(" >> ABORTED"); //$NON-NLS-1$
143
System.out.println("\t" + e.getStatus().getMessage()); //$NON-NLS-1$
144
}
145                     }
146                 }
147                 monitor.setCanceled(request.isCanceled());
148                 request.done();
149                 monitor.done();
150                 return Status.OK_STATUS;
151             }
152         };
153         job.setSystem(true);
154         job.setRule(createUpdateSchedulingRule(request));
155         job.schedule();
156         
157     }
158     
159     /**
160      * Returns the name to use for jobs and progress monitor task names when checking
161      * enabled state.
162      *
163      * @return task name
164      */

165     protected String JavaDoc getEnablementTaskName() {
166         // this is a system job name and does not need to be NLS'd
167
return "Check Debug Command"; //$NON-NLS-1$
168
}
169     
170     /**
171      * Returns the name to use for jobs and progress monitor task names when executing
172      * a debug command
173      *
174      * @return task name
175      */

176     protected String JavaDoc getExecuteTaskName() {
177         // this is a system job name and does not need to be NLS'd
178
return "Execute Debug Command"; //$NON-NLS-1$
179
}
180
181     /**
182      * Executes the actual operation.
183      *
184      * @param targets objects to perform on
185      * @param request request
186      */

187     protected abstract void doExecute(Object JavaDoc[] targets, IProgressMonitor monitor, IRequest request) throws CoreException;
188
189     /**
190      * Returns whether this command is executable.
191      *
192      * @param targets objects to check command for
193      * @param monitor progress monitor
194      * @param request request
195      * @return whether this command can be executed
196      */

197     protected abstract boolean isExecutable(Object JavaDoc[] targets, IProgressMonitor monitor, IEnabledStateRequest request) throws CoreException;
198     
199     /**
200      * Returns the appropriate command adapter from the given object.
201      *
202      * @param element object to obtain adapter from
203      * @return adapter
204      */

205     protected abstract Object JavaDoc getTarget(Object JavaDoc element);
206     
207     /**
208      * Scheduling rule for updating command enabled state.
209      *
210      * @return scheduling rule or <code>null</code>
211      */

212     protected ISchedulingRule createUpdateSchedulingRule(IDebugCommandRequest request) {
213         return new SerialPerObjectRule(request.getElements()[0]);
214     }
215     
216     private Object JavaDoc[] coalesce(Object JavaDoc[] objects) {
217         if (objects.length == 1) {
218             return objects;
219         } else {
220             LinkedHashSet JavaDoc set = new LinkedHashSet JavaDoc(objects.length);
221             for (int i = 0; i < objects.length; i++) {
222                 set.add(objects[i]);
223             }
224             return set.toArray();
225         }
226     }
227 }
228
Popular Tags