KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-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.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.ResourceBundle JavaDoc;
19 import org.eclipse.core.runtime.IAdaptable;
20 import org.eclipse.debug.core.DebugEvent;
21 import org.eclipse.debug.core.DebugPlugin;
22 import org.eclipse.debug.core.IDebugEventSetListener;
23 import org.eclipse.debug.core.model.IDebugTarget;
24 import org.eclipse.debug.core.model.IProcess;
25 import org.eclipse.debug.internal.ui.DebugUIPlugin;
26 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
27 import org.eclipse.debug.internal.ui.actions.FollowHyperlinkAction;
28 import org.eclipse.debug.internal.ui.actions.KeyBindingFollowHyperlinkAction;
29 import org.eclipse.debug.internal.ui.views.DebugUIViewsMessages;
30 import org.eclipse.debug.ui.DebugUITools;
31 import org.eclipse.debug.ui.IDebugUIConstants;
32 import org.eclipse.jface.action.IAction;
33 import org.eclipse.jface.action.IMenuListener;
34 import org.eclipse.jface.action.IMenuManager;
35 import org.eclipse.jface.action.IToolBarManager;
36 import org.eclipse.jface.action.MenuManager;
37 import org.eclipse.jface.action.Separator;
38 import org.eclipse.jface.text.IFindReplaceTarget;
39 import org.eclipse.jface.text.ITextListener;
40 import org.eclipse.jface.text.ITextOperationTarget;
41 import org.eclipse.jface.text.TextEvent;
42 import org.eclipse.jface.viewers.ISelection;
43 import org.eclipse.jface.viewers.ISelectionChangedListener;
44 import org.eclipse.jface.viewers.SelectionChangedEvent;
45 import org.eclipse.jface.viewers.StructuredSelection;
46 import org.eclipse.swt.widgets.Composite;
47 import org.eclipse.swt.widgets.Control;
48 import org.eclipse.swt.widgets.Menu;
49 import org.eclipse.swt.widgets.Widget;
50 import org.eclipse.ui.IActionBars;
51 import org.eclipse.ui.ISelectionListener;
52 import org.eclipse.ui.ISharedImages;
53 import org.eclipse.ui.IWorkbenchActionConstants;
54 import org.eclipse.ui.IWorkbenchPart;
55 import org.eclipse.ui.PlatformUI;
56 import org.eclipse.ui.actions.ActionFactory;
57 import org.eclipse.ui.console.IConsoleConstants;
58 import org.eclipse.ui.console.IConsoleView;
59 import org.eclipse.ui.console.actions.ClearOutputAction;
60 import org.eclipse.ui.console.actions.TextViewerAction;
61 import org.eclipse.ui.console.actions.TextViewerGotoLineAction;
62 import org.eclipse.ui.part.IPageBookViewPage;
63 import org.eclipse.ui.part.IPageSite;
64 import org.eclipse.ui.part.IShowInSource;
65 import org.eclipse.ui.part.IShowInTargetList;
66 import org.eclipse.ui.part.ShowInContext;
67 import org.eclipse.ui.texteditor.FindReplaceAction;
68 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
69 import org.eclipse.ui.texteditor.IUpdate;
70
71 /**
72  * A page for a console connected to I/O streams of a process
73  *
74  * @since 3.0
75  */

76 public class ProcessConsolePage implements IPageBookViewPage, ISelectionListener, IAdaptable, IShowInSource, IShowInTargetList, IDebugEventSetListener, ITextListener {
77
78     //page site
79
private IPageSite fSite = null;
80     
81     // viewer
82
private ConsoleViewer fViewer = null;
83
84     // the view this page is contained in
85
private IConsoleView fView;
86     
87     // the console this page displays
88
private ProcessConsole fConsole;
89     
90     // scroll lock
91
private boolean fIsLocked = DebugUIPlugin.getDefault().getPreferenceStore().getBoolean(IInternalDebugUIConstants.PREF_CONSOLE_SCROLL_LOCK);
92     
93     // text selection listener
94
private ISelectionChangedListener fTextListener = new ISelectionChangedListener() {
95         public void selectionChanged(SelectionChangedEvent event) {
96             updateSelectionDependentActions();
97         }};
98
99     // actions
100
private ClearOutputAction fClearOutputAction;
101     private Map JavaDoc fGlobalActions= new HashMap JavaDoc(10);
102     protected List JavaDoc fSelectionActions = new ArrayList JavaDoc(3);
103     private FollowHyperlinkAction fFollowLinkAction;
104     private ScrollLockAction fScrollLockAction;
105     private ConsoleTerminateAction fTerminate;
106     private ConsoleRemoveAllTerminatedAction fRemoveTerminated;
107     private KeyBindingFollowHyperlinkAction fKeyBindingFollowLinkAction;
108     
109     // menus
110
private Menu fMenu;
111
112     /**
113      * Constructs a new process page
114      */

115     public ProcessConsolePage(IConsoleView view, ProcessConsole console) {
116         fView = view;
117         fConsole = console;
118     }
119
120     /* (non-Javadoc)
121      * @see org.eclipse.ui.part.IPageBookViewPage#getSite()
122      */

123     public IPageSite getSite() {
124         return fSite;
125     }
126
127     /* (non-Javadoc)
128      * @see org.eclipse.ui.part.IPageBookViewPage#init(org.eclipse.ui.part.IPageSite)
129      */

130     public void init(IPageSite site) {
131         fSite = site;
132         
133     }
134
135     /* (non-Javadoc)
136      * @see org.eclipse.ui.part.IPage#createControl(org.eclipse.swt.widgets.Composite)
137      */

138     public void createControl(Composite parent) {
139         fViewer = new ConsoleViewer(parent);
140         fViewer.setDocument(DebugUIPlugin.getDefault().getConsoleDocumentManager().getConsoleDocument(getProcess()));
141         
142         MenuManager manager= new MenuManager("#ProcessConsole", "#ProcessConsole"); //$NON-NLS-1$ //$NON-NLS-2$
143
manager.setRemoveAllWhenShown(true);
144         manager.addMenuListener(new IMenuListener() {
145             public void menuAboutToShow(IMenuManager m) {
146                 contextMenuAboutToShow(m);
147             }
148         });
149         fMenu= manager.createContextMenu(getControl());
150         getControl().setMenu(fMenu);
151         
152         IPageSite site= getSite();
153         site.registerContextMenu(DebugUIPlugin.getUniqueIdentifier() + ".processConsole", manager, getConsoleViewer()); //$NON-NLS-1$
154
site.setSelectionProvider(getConsoleViewer());
155         
156         createActions();
157         configureToolBar(getSite().getActionBars().getToolBarManager());
158         
159         getSite().getPage().addSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this);
160         fViewer.getSelectionProvider().addSelectionChangedListener(fTextListener);
161         fViewer.addTextListener(this);
162     }
163
164     /**
165      * Fill the context menu
166      *
167      * @param menu menu
168      */

169     protected void contextMenuAboutToShow(IMenuManager menu) {
170         ConsoleDocument doc= (ConsoleDocument)getConsoleViewer().getDocument();
171         if (doc == null) {
172             return;
173         }
174         if (doc.isReadOnly()) {
175             menu.add((IAction)fGlobalActions.get(ActionFactory.COPY.getId()));
176             menu.add((IAction)fGlobalActions.get(ActionFactory.SELECT_ALL.getId()));
177         } else {
178             updateAction(ActionFactory.PASTE.getId());
179             menu.add((IAction)fGlobalActions.get(ActionFactory.CUT.getId()));
180             menu.add((IAction)fGlobalActions.get(ActionFactory.COPY.getId()));
181             menu.add((IAction)fGlobalActions.get(ActionFactory.PASTE.getId()));
182             menu.add((IAction)fGlobalActions.get(ActionFactory.SELECT_ALL.getId()));
183         }
184
185         menu.add(new Separator("FIND")); //$NON-NLS-1$
186
menu.add((IAction)fGlobalActions.get(ActionFactory.FIND.getId()));
187         menu.add((IAction)fGlobalActions.get(ITextEditorActionConstants.GOTO_LINE));
188         fFollowLinkAction.setEnabled(fFollowLinkAction.getHyperLink() != null);
189         menu.add(fFollowLinkAction);
190         menu.add(fClearOutputAction);
191         menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
192         menu.add(fTerminate);
193         
194     }
195
196     /* (non-Javadoc)
197      * @see org.eclipse.ui.part.IPage#dispose()
198      */

199     public void dispose() {
200         DebugPlugin.getDefault().removeDebugEventListener(this);
201         getSite().getPage().removeSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this);
202         fViewer.getSelectionProvider().removeSelectionChangedListener(fTextListener);
203         fViewer.removeTextListener(this);
204
205         if (fKeyBindingFollowLinkAction != null) {
206             getConsoleView().getSite().getKeyBindingService().unregisterAction(fKeyBindingFollowLinkAction);
207         }
208         
209         if (fRemoveTerminated != null) {
210             fRemoveTerminated.dispose();
211         }
212         
213         if (fScrollLockAction != null) {
214             fScrollLockAction.dispose();
215         }
216         
217         if (fMenu != null && !fMenu.isDisposed()) {
218             fMenu.dispose();
219             fMenu= null;
220         }
221         
222         if (fViewer != null) {
223             fViewer.dispose();
224             fViewer = null;
225         }
226         fSite = null;
227         fSelectionActions.clear();
228     }
229
230     /* (non-Javadoc)
231      * @see org.eclipse.ui.part.IPage#getControl()
232      */

233     public Control getControl() {
234         if (fViewer != null) {
235             return fViewer.getControl();
236         }
237         return null;
238     }
239
240     /* (non-Javadoc)
241      * @see org.eclipse.ui.part.IPage#setActionBars(org.eclipse.ui.IActionBars)
242      */

243     public void setActionBars(IActionBars actionBars) {
244     }
245
246     /* (non-Javadoc)
247      * @see org.eclipse.ui.part.IPage#setFocus()
248      */

249     public void setFocus() {
250         Control control = getControl();
251         if (control != null) {
252             control.setFocus();
253         }
254         updateSelectionDependentActions();
255     }
256     
257     protected void createActions() {
258         fClearOutputAction= new ClearOutputAction(getConsoleViewer());
259         fRemoveTerminated = new ConsoleRemoveAllTerminatedAction();
260         
261         // In order for the clipboard actions to accessible via their shortcuts
262
// (e.g., Ctrl-C, Ctrl-V), we *must* set a global action handler for
263
// each action
264
IActionBars actionBars= getSite().getActionBars();
265         TextViewerAction action= new TextViewerAction(getConsoleViewer(), ITextOperationTarget.CUT);
266         action.configureAction(DebugUIViewsMessages.getString("ConsoleView.Cu&t@Ctrl+X_3"), DebugUIViewsMessages.getString("ConsoleView.Cut_4"), DebugUIViewsMessages.getString("ConsoleView.Cut_4")); //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$
267
action.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_CUT));
268         setGlobalAction(actionBars, ActionFactory.CUT.getId(), action);
269         action= new TextViewerAction(getConsoleViewer(), ITextOperationTarget.COPY);
270         action.configureAction(DebugUIViewsMessages.getString("ConsoleView.&Copy@Ctrl+C_6"), DebugUIViewsMessages.getString("ConsoleView.Copy_7"), DebugUIViewsMessages.getString("ConsoleView.Copy_7")); //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$
271
action.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
272         setGlobalAction(actionBars, ActionFactory.COPY.getId(), action);
273         action= new TextViewerAction(getConsoleViewer(), ITextOperationTarget.PASTE);
274         action.configureAction(DebugUIViewsMessages.getString("ConsoleView.&Paste@Ctrl+V_9"), DebugUIViewsMessages.getString("ConsoleView.Paste_10"), DebugUIViewsMessages.getString("ConsoleView.Paste_Clipboard_Text_11")); //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$
275
action.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_PASTE));
276         setGlobalAction(actionBars, ActionFactory.PASTE.getId(), action);
277         action= new TextViewerAction(getConsoleViewer(), ITextOperationTarget.SELECT_ALL);
278         action.configureAction(DebugUIViewsMessages.getString("ConsoleView.Select_&All@Ctrl+A_12"), DebugUIViewsMessages.getString("ConsoleView.Select_All"), DebugUIViewsMessages.getString("ConsoleView.Select_All")); //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$
279
setGlobalAction(actionBars, ActionFactory.SELECT_ALL.getId(), action);
280         
281         //XXX Still using "old" resource access
282
ResourceBundle JavaDoc bundle= ResourceBundle.getBundle("org.eclipse.debug.internal.ui.views.DebugUIViewsMessages"); //$NON-NLS-1$
283
setGlobalAction(actionBars, ActionFactory.FIND.getId(), new FindReplaceAction(bundle, "find_replace_action.", getConsoleView())); //$NON-NLS-1$
284

285         action= new TextViewerGotoLineAction(getConsoleViewer());
286         setGlobalAction(actionBars, ITextEditorActionConstants.GOTO_LINE, action);
287         
288         fFollowLinkAction = new FollowHyperlinkAction(getConsoleViewer());
289         
290         fKeyBindingFollowLinkAction= new KeyBindingFollowHyperlinkAction(getConsoleViewer(), actionBars);
291         fKeyBindingFollowLinkAction.setActionDefinitionId("org.eclipse.jdt.ui.edit.text.java.open.editor"); //$NON-NLS-1$
292
getConsoleView().getSite().getKeyBindingService().registerAction(fKeyBindingFollowLinkAction);
293         
294         fScrollLockAction = new ScrollLockAction();
295         fScrollLockAction.setChecked(fIsLocked);
296         getConsoleViewer().setAutoScroll(!fIsLocked);
297                         
298         actionBars.updateActionBars();
299                 
300         fTerminate = new ConsoleTerminateAction(getConsole());
301         DebugPlugin.getDefault().addDebugEventListener(this);
302         
303         fSelectionActions.add(ActionFactory.CUT.getId());
304         fSelectionActions.add(ActionFactory.COPY.getId());
305         fSelectionActions.add(ActionFactory.PASTE.getId());
306         fSelectionActions.add(ActionFactory.FIND.getId());
307     }
308     
309     protected void updateSelectionDependentActions() {
310         Iterator JavaDoc iterator= fSelectionActions.iterator();
311         while (iterator.hasNext()) {
312             updateAction((String JavaDoc)iterator.next());
313         }
314     }
315     
316     protected void updateAction(String JavaDoc actionId) {
317         IAction action= (IAction)fGlobalActions.get(actionId);
318         if (action instanceof IUpdate) {
319             ((IUpdate) action).update();
320         }
321     }
322         
323     protected void setGlobalAction(IActionBars actionBars, String JavaDoc actionID, IAction action) {
324         fGlobalActions.put(actionID, action);
325         actionBars.setGlobalActionHandler(actionID, action);
326     }
327         
328     /**
329      * Returns the console viewer in this page.
330      *
331      * @return the console viewer in this page
332      */

333     public ConsoleViewer getConsoleViewer() {
334         return fViewer;
335     }
336
337     protected void configureToolBar(IToolBarManager mgr) {
338         mgr.appendToGroup(IConsoleConstants.LAUNCH_GROUP, fTerminate);
339         mgr.appendToGroup(IConsoleConstants.LAUNCH_GROUP, fRemoveTerminated);
340         //mgr.add(fProcessDropDownAction);
341
mgr.appendToGroup(IConsoleConstants.OUTPUT_GROUP, fScrollLockAction);
342         mgr.appendToGroup(IConsoleConstants.OUTPUT_GROUP, fClearOutputAction);
343     }
344
345     /**
346      * Returns the process associated with this page
347      *
348      * @return the process associated with this page
349      */

350     protected IProcess getProcess() {
351         return getConsole().getProcess();
352     }
353     
354     /**
355      * Returns the view this page is contained in
356      *
357      * @return the view this page is contained in
358      */

359     protected IConsoleView getConsoleView() {
360         return fView;
361     }
362     
363     /**
364      * Returns the console this page is displaying
365      *
366      * @return the console this page is displaying
367      */

368     protected ProcessConsole getConsole() {
369         return fConsole;
370     }
371     
372     /* (non-Javadoc)
373      * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
374      */

375     public void selectionChanged(IWorkbenchPart part, ISelection selection) {
376         if (getProcess().equals(DebugUITools.getCurrentProcess())) {
377             getConsoleView().display(getConsole());
378         }
379     }
380     
381     /* (non-Javadoc)
382      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
383      */

384     public Object JavaDoc getAdapter(Class JavaDoc required) {
385         if (IFindReplaceTarget.class.equals(required)) {
386             return getConsoleViewer().getFindReplaceTarget();
387         }
388         if (Widget.class.equals(required)) {
389             return getConsoleViewer().getTextWidget();
390         }
391         if (IShowInSource.class.equals(required)) {
392             return this;
393         }
394         if (IShowInTargetList.class.equals(required)) {
395             return this;
396         }
397         return null;
398     }
399     
400     /* (non-Javadoc)
401      * @see org.eclipse.ui.part.IShowInSource#getShowInContext()
402      */

403     public ShowInContext getShowInContext() {
404         IProcess process = getProcess();
405         if (process == null) {
406             return null;
407         } else {
408             IDebugTarget target = (IDebugTarget)process.getAdapter(IDebugTarget.class);
409             ISelection selection = null;
410             if (target == null) {
411                 selection = new StructuredSelection(process);
412             } else {
413                 selection = new StructuredSelection(target);
414             }
415             return new ShowInContext(null, selection);
416         }
417     }
418     
419     /* (non-Javadoc)
420      * @see org.eclipse.ui.part.IShowInTargetList#getShowInTargetIds()
421      */

422     public String JavaDoc[] getShowInTargetIds() {
423         return new String JavaDoc[] {IDebugUIConstants.ID_DEBUG_VIEW};
424     }
425     
426     /**
427      * Update terminate action.
428      *
429      * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent[])
430      */

431     public void handleDebugEvents(DebugEvent[] events) {
432         for (int i = 0; i < events.length; i++) {
433             DebugEvent event = events[i];
434             if (event.getSource().equals(getProcess())) {
435                 Runnable JavaDoc r = new Runnable JavaDoc() {
436                     public void run() {
437                         if (isAvailable()) {
438                             fTerminate.update();
439                         }
440                     }
441                 };
442                 if (isAvailable()) {
443                     getControl().getDisplay().asyncExec(r);
444                 }
445             }
446         }
447     }
448
449     /**
450      * Returns whether this page's controls are available.
451      *
452      * @return whether this page's controls are available
453      */

454     protected boolean isAvailable() {
455         return getControl() != null;
456     }
457         
458     /* (non-Javadoc)
459      * @see org.eclipse.jface.text.ITextListener#textChanged(org.eclipse.jface.text.TextEvent)
460      */

461     public void textChanged(TextEvent event) {
462         // update the find replace action if the document length is > 0
463
IUpdate findReplace = (IUpdate)fGlobalActions.get(ActionFactory.FIND.getId());
464         if (findReplace != null) {
465             findReplace.update();
466         }
467     }
468 }
469
Popular Tags