KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > console > ProcessConsolePageParticipant


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.views.console;
12
13 import java.io.IOException JavaDoc;
14
15 import org.eclipse.core.commands.AbstractHandler;
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.debug.core.DebugEvent;
18 import org.eclipse.debug.core.DebugPlugin;
19 import org.eclipse.debug.core.IDebugEventSetListener;
20 import org.eclipse.debug.core.ILaunch;
21 import org.eclipse.debug.core.ILaunchConfiguration;
22 import org.eclipse.debug.core.model.IDebugTarget;
23 import org.eclipse.debug.core.model.IProcess;
24 import org.eclipse.debug.core.model.IStreamsProxy;
25 import org.eclipse.debug.core.model.IStreamsProxy2;
26 import org.eclipse.debug.internal.ui.DebugUIPlugin;
27 import org.eclipse.debug.ui.DebugUITools;
28 import org.eclipse.debug.ui.IDebugUIConstants;
29 import org.eclipse.debug.ui.contexts.DebugContextEvent;
30 import org.eclipse.debug.ui.contexts.IDebugContextListener;
31 import org.eclipse.jface.action.IToolBarManager;
32 import org.eclipse.jface.viewers.ISelection;
33 import org.eclipse.jface.viewers.TreePath;
34 import org.eclipse.jface.viewers.TreeSelection;
35 import org.eclipse.ui.IActionBars;
36 import org.eclipse.ui.console.IConsole;
37 import org.eclipse.ui.console.IConsoleConstants;
38 import org.eclipse.ui.console.IConsolePageParticipant;
39 import org.eclipse.ui.console.IConsoleView;
40 import org.eclipse.ui.contexts.IContextActivation;
41 import org.eclipse.ui.contexts.IContextService;
42 import org.eclipse.ui.handlers.IHandlerActivation;
43 import org.eclipse.ui.handlers.IHandlerService;
44 import org.eclipse.ui.part.IPageBookViewPage;
45 import org.eclipse.ui.part.IPageSite;
46 import org.eclipse.ui.part.IShowInSource;
47 import org.eclipse.ui.part.IShowInTargetList;
48 import org.eclipse.ui.part.ShowInContext;
49
50 /**
51  * Creates and manages process console specific actions
52  *
53  * @since 3.1
54  */

55 public class ProcessConsolePageParticipant implements IConsolePageParticipant, IShowInSource, IShowInTargetList, IDebugEventSetListener, IDebugContextListener {
56     
57     // actions
58
private ConsoleTerminateAction fTerminate;
59     private ConsoleRemoveLaunchAction fRemoveTerminated;
60     private ConsoleRemoveAllTerminatedAction fRemoveAllTerminated;
61     private ShowWhenContentChangesAction fStdOut;
62     private ShowWhenContentChangesAction fStdErr;
63
64     private ProcessConsole fConsole;
65
66     private IPageBookViewPage fPage;
67
68     private IConsoleView fView;
69     
70     private EOFHandler fEOFHandler;
71     private String JavaDoc fContextId = "org.eclipse.debug.ui.console"; //$NON-NLS-1$;
72
private IContextActivation fActivatedContext;
73     private IHandlerActivation fActivatedHandler;
74     /**
75      * Handler to send EOF
76      */

77     private class EOFHandler extends AbstractHandler {
78         public Object JavaDoc execute(ExecutionEvent event) throws org.eclipse.core.commands.ExecutionException {
79             IStreamsProxy proxy = getProcess().getStreamsProxy();
80             if (proxy instanceof IStreamsProxy2) {
81                 IStreamsProxy2 proxy2 = (IStreamsProxy2) proxy;
82                 try {
83                     proxy2.closeInputStream();
84                 } catch (IOException JavaDoc e1) {
85                 }
86             }
87             return null;
88         }
89         
90     }
91             
92     /* (non-Javadoc)
93      * @see org.eclipse.ui.console.IConsolePageParticipant#init(IPageBookViewPage, IConsole)
94      */

95     public void init(IPageBookViewPage page, IConsole console) {
96         fPage = page;
97         fConsole = (ProcessConsole) console;
98         
99         fRemoveTerminated = new ConsoleRemoveLaunchAction(fConsole.getProcess().getLaunch());
100         fRemoveAllTerminated = new ConsoleRemoveAllTerminatedAction();
101         fTerminate = new ConsoleTerminateAction(fConsole);
102         fStdOut = new ShowStandardOutAction();
103         fStdErr = new ShowStandardErrorAction();
104         
105         fView = (IConsoleView) fPage.getSite().getPage().findView(IConsoleConstants.ID_CONSOLE_VIEW);
106         
107         DebugPlugin.getDefault().addDebugEventListener(this);
108         DebugUITools.getDebugContextManager().getContextService(fPage.getSite().getWorkbenchWindow()).addDebugContextListener(this);
109         
110         // contribute to toolbar
111
IActionBars actionBars = fPage.getSite().getActionBars();
112         configureToolBar(actionBars.getToolBarManager());
113         
114         // create handler and submissions for EOF
115
fEOFHandler = new EOFHandler();
116     }
117     
118     /* (non-Javadoc)
119      * @see org.eclipse.ui.console.IConsolePageParticipant#dispose()
120      */

121     public void dispose() {
122         deactivated();
123         DebugUITools.getDebugContextManager().getContextService(fPage.getSite().getWorkbenchWindow()).removeDebugContextListener(this);
124         DebugPlugin.getDefault().removeDebugEventListener(this);
125         if (fRemoveTerminated != null) {
126             fRemoveTerminated.dispose();
127             fRemoveTerminated = null;
128         }
129         if (fRemoveAllTerminated != null) {
130             fRemoveAllTerminated.dispose();
131             fRemoveAllTerminated = null;
132         }
133         if (fTerminate != null) {
134             fTerminate.dispose();
135             fTerminate = null;
136         }
137         if (fStdOut != null) {
138             fStdOut.dispose();
139             fStdOut = null;
140         }
141         if (fStdErr != null) {
142             fStdErr.dispose();
143             fStdErr = null;
144         }
145         fConsole = null;
146     }
147
148     /**
149      * Contribute actions to the toolbar
150      */

151     protected void configureToolBar(IToolBarManager mgr) {
152         mgr.appendToGroup(IConsoleConstants.LAUNCH_GROUP, fTerminate);
153         mgr.appendToGroup(IConsoleConstants.LAUNCH_GROUP, fRemoveTerminated);
154         mgr.appendToGroup(IConsoleConstants.LAUNCH_GROUP, fRemoveAllTerminated);
155         mgr.appendToGroup(IConsoleConstants.OUTPUT_GROUP, fStdOut);
156         mgr.appendToGroup(IConsoleConstants.OUTPUT_GROUP, fStdErr);
157     }
158
159     /* (non-Javadoc)
160      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
161      */

162     public Object JavaDoc getAdapter(Class JavaDoc required) {
163         if (IShowInSource.class.equals(required)) {
164             return this;
165         }
166         if (IShowInTargetList.class.equals(required)) {
167             return this;
168         }
169         //CONTEXTLAUNCHING
170
if(ILaunchConfiguration.class.equals(required)) {
171             ILaunch launch = getProcess().getLaunch();
172             if(launch != null) {
173                 return launch.getLaunchConfiguration();
174             }
175             return null;
176         }
177         return null;
178     }
179
180     /* (non-Javadoc)
181      * @see org.eclipse.ui.part.IShowInSource#getShowInContext()
182      */

183     public ShowInContext getShowInContext() {
184         IProcess process = getProcess();
185         if (process == null) {
186             return null;
187         }
188         IDebugTarget target = (IDebugTarget)process.getAdapter(IDebugTarget.class);
189         ISelection selection = null;
190         if (target == null) {
191             selection = new TreeSelection(new TreePath(new Object JavaDoc[]{
192                     DebugPlugin.getDefault().getLaunchManager(),
193                     process.getLaunch(),
194                     process}));
195         } else {
196             selection = new TreeSelection(new TreePath(new Object JavaDoc[]{
197                     DebugPlugin.getDefault().getLaunchManager(),
198                     target.getLaunch(),
199                     target}));
200         }
201         return new ShowInContext(null, selection);
202     }
203
204     /* (non-Javadoc)
205      * @see org.eclipse.ui.part.IShowInTargetList#getShowInTargetIds()
206      */

207     public String JavaDoc[] getShowInTargetIds() {
208         return new String JavaDoc[] {IDebugUIConstants.ID_DEBUG_VIEW};
209     }
210
211     /* (non-Javadoc)
212      * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent[])
213      */

214     public void handleDebugEvents(DebugEvent[] events) {
215         for (int i = 0; i < events.length; i++) {
216             DebugEvent event = events[i];
217             if (event.getSource().equals(getProcess())) {
218                 Runnable JavaDoc r = new Runnable JavaDoc() {
219                     public void run() {
220                         if (fTerminate != null) {
221                             fTerminate.update();
222                         }
223                     }
224                 };
225                 
226                 DebugUIPlugin.getStandardDisplay().asyncExec(r);
227             }
228         }
229     }
230     
231     protected IProcess getProcess() {
232         return fConsole != null ? fConsole.getProcess() : null;
233     }
234
235     /* (non-Javadoc)
236      * @see org.eclipse.ui.console.IConsolePageParticipant#activated()
237      */

238     public void activated() {
239         // add EOF submissions
240
IPageSite site = fPage.getSite();
241         IHandlerService handlerService = (IHandlerService)site.getService(IHandlerService.class);
242         IContextService contextService = (IContextService)site.getService(IContextService.class);
243         fActivatedContext = contextService.activateContext(fContextId);
244         fActivatedHandler = handlerService.activateHandler("org.eclipse.debug.ui.commands.eof", fEOFHandler); //$NON-NLS-1$
245
}
246
247     /* (non-Javadoc)
248      * @see org.eclipse.ui.console.IConsolePageParticipant#deactivated()
249      */

250     public void deactivated() {
251         // remove EOF submissions
252
IPageSite site = fPage.getSite();
253         IHandlerService handlerService = (IHandlerService)site.getService(IHandlerService.class);
254         IContextService contextService = (IContextService)site.getService(IContextService.class);
255         handlerService.deactivateHandler(fActivatedHandler);
256         contextService.deactivateContext(fActivatedContext);
257     }
258
259     /* (non-Javadoc)
260      * @see org.eclipse.debug.internal.ui.contexts.provisional.IDebugContextListener#contextEvent(org.eclipse.debug.internal.ui.contexts.provisional.DebugContextEvent)
261      */

262     public void debugContextChanged(DebugContextEvent event) {
263         if ((event.getFlags() & DebugContextEvent.ACTIVATED) > 0) {
264             if (fView != null && getProcess().equals(DebugUITools.getCurrentProcess())) {
265                 fView.display(fConsole);
266             }
267         }
268         
269     }
270 }
271
Popular Tags