KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > console > MessageConsolePage


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.ui.internal.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
20 import org.eclipse.core.runtime.IAdaptable;
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.action.IMenuListener;
23 import org.eclipse.jface.action.IMenuManager;
24 import org.eclipse.jface.action.IToolBarManager;
25 import org.eclipse.jface.action.MenuManager;
26 import org.eclipse.jface.action.Separator;
27 import org.eclipse.jface.text.IFindReplaceTarget;
28 import org.eclipse.jface.text.ITextOperationTarget;
29 import org.eclipse.jface.util.IPropertyChangeListener;
30 import org.eclipse.jface.util.PropertyChangeEvent;
31 import org.eclipse.jface.viewers.ISelectionChangedListener;
32 import org.eclipse.jface.viewers.SelectionChangedEvent;
33 import org.eclipse.swt.graphics.Font;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.Menu;
37 import org.eclipse.swt.widgets.Widget;
38 import org.eclipse.ui.IActionBars;
39 import org.eclipse.ui.ISharedImages;
40 import org.eclipse.ui.IWorkbenchActionConstants;
41 import org.eclipse.ui.PlatformUI;
42 import org.eclipse.ui.actions.ActionFactory;
43 import org.eclipse.ui.console.ConsolePlugin;
44 import org.eclipse.ui.console.IConsoleConstants;
45 import org.eclipse.ui.console.IConsoleView;
46 import org.eclipse.ui.console.MessageConsole;
47 import org.eclipse.ui.console.MessageConsoleStream;
48 import org.eclipse.ui.console.actions.ClearOutputAction;
49 import org.eclipse.ui.console.actions.TextViewerAction;
50 import org.eclipse.ui.console.actions.TextViewerGotoLineAction;
51 import org.eclipse.ui.part.IPageBookViewPage;
52 import org.eclipse.ui.part.IPageSite;
53 import org.eclipse.ui.texteditor.FindReplaceAction;
54 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
55 import org.eclipse.ui.texteditor.IUpdate;
56
57 /**
58  * A page for a console connected to I/O streams of a process
59  *
60  * @since 3.0
61  */

62 public class MessageConsolePage implements IPageBookViewPage, IAdaptable, IPropertyChangeListener {
63
64     //page site
65
private IPageSite fSite = null;
66     
67     // viewer
68
private MessageConsoleViewer fViewer = null;
69
70     // the view this page is contained in
71
private IConsoleView fView;
72     
73     // the console this page displays
74
private MessageConsole fConsole;
75     
76     // text selection listener
77
private ISelectionChangedListener fTextListener = new ISelectionChangedListener() {
78         public void selectionChanged(SelectionChangedEvent event) {
79             updateSelectionDependentActions();
80         }};
81
82     // actions
83
private ClearOutputAction fClearOutputAction;
84     private Map JavaDoc fGlobalActions= new HashMap JavaDoc(10);
85     private List JavaDoc fSelectionActions = new ArrayList JavaDoc(3);
86     
87     // menus
88
private Menu fMenu;
89     
90     /**
91      * Constructs a new process page
92      */

93     public MessageConsolePage(IConsoleView view, MessageConsole console) {
94         fView = view;
95         fConsole = console;
96     }
97
98     /* (non-Javadoc)
99      * @see org.eclipse.ui.part.IPageBookViewPage#getSite()
100      */

101     public IPageSite getSite() {
102         return fSite;
103     }
104
105     /* (non-Javadoc)
106      * @see org.eclipse.ui.part.IPageBookViewPage#init(org.eclipse.ui.part.IPageSite)
107      */

108     public void init(IPageSite site) {
109         fSite = site;
110         
111     }
112
113     /* (non-Javadoc)
114      * @see org.eclipse.ui.part.IPage#createControl(org.eclipse.swt.widgets.Composite)
115      */

116     public void createControl(Composite parent) {
117         fViewer = new MessageConsoleViewer(parent);
118         fViewer.setDocument(getConsole().getDocument());
119         fViewer.getTextWidget().setTabs(getConsole().getTabWidth());
120         
121         MenuManager manager= new MenuManager("#MessageConsole", "#MessageConsole"); //$NON-NLS-1$ //$NON-NLS-2$
122
manager.setRemoveAllWhenShown(true);
123         manager.addMenuListener(new IMenuListener() {
124             public void menuAboutToShow(IMenuManager m) {
125                 contextMenuAboutToShow(m);
126             }
127         });
128         fMenu= manager.createContextMenu(getControl());
129         getControl().setMenu(fMenu);
130         
131         IPageSite site= getSite();
132         site.registerContextMenu(ConsolePlugin.getUniqueIdentifier() + ".messageConsole", manager, getViewer()); //$NON-NLS-1$
133
site.setSelectionProvider(getViewer());
134         
135         createActions();
136         configureToolBar(getSite().getActionBars().getToolBarManager());
137         
138         fViewer.getSelectionProvider().addSelectionChangedListener(fTextListener);
139         setFont(getConsole().getFont());
140         getConsole().addPropertyChangeListener(this);
141     }
142     
143     /* (non-Javadoc)
144      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
145      */

146     public void propertyChange(PropertyChangeEvent event) {
147         Object JavaDoc source = event.getSource();
148         String JavaDoc property = event.getProperty();
149         if (source.equals(getConsole()) && MessageConsole.P_FONT.equals(property)) {
150             setFont(getConsole().getFont());
151         } else if (MessageConsole.P_STREAM_COLOR.equals(property) && source instanceof MessageConsoleStream) {
152             MessageConsoleStream stream = (MessageConsoleStream)source;
153             if (stream.getConsole().equals(getConsole())) {
154                 getViewer().getTextWidget().redraw();
155             }
156         } else if (source.equals(getConsole()) && property.equals(MessageConsole.P_TAB_SIZE)) {
157             if (fViewer != null) {
158                 fViewer.getTextWidget().setTabs(getConsole().getTabWidth());
159                 fViewer.getTextWidget().redraw();
160             }
161         }
162     }
163
164     
165     /* (non-Javadoc)
166      * @see org.eclipse.ui.part.IPage#dispose()
167      */

168     public void dispose() {
169         getConsole().removePropertyChangeListener(this);
170         fViewer.getSelectionProvider().removeSelectionChangedListener(fTextListener);
171         
172         if (fMenu != null && !fMenu.isDisposed()) {
173             fMenu.dispose();
174             fMenu= null;
175         }
176         
177         if (fViewer != null) {
178             fViewer.dispose();
179             fViewer = null;
180         }
181         fSite = null;
182         fSelectionActions.clear();
183     }
184     
185
186     /**
187      * Fill the context menu
188      *
189      * @param menu menu
190      */

191     protected void contextMenuAboutToShow(IMenuManager menu) {
192         menu.add((IAction)fGlobalActions.get(ActionFactory.COPY.getId()));
193         menu.add((IAction)fGlobalActions.get(ActionFactory.SELECT_ALL.getId()));
194         menu.add(new Separator("FIND")); //$NON-NLS-1$
195
menu.add((IAction)fGlobalActions.get(ActionFactory.FIND.getId()));
196         menu.add((IAction)fGlobalActions.get(ITextEditorActionConstants.GOTO_LINE));
197         menu.add(fClearOutputAction);
198         menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
199     }
200
201     /* (non-Javadoc)
202      * @see org.eclipse.ui.part.IPage#getControl()
203      */

204     public Control getControl() {
205         if (fViewer != null) {
206             return fViewer.getControl();
207         }
208         return null;
209     }
210
211     /* (non-Javadoc)
212      * @see org.eclipse.ui.part.IPage#setActionBars(org.eclipse.ui.IActionBars)
213      */

214     public void setActionBars(IActionBars actionBars) {
215     }
216
217     /* (non-Javadoc)
218      * @see org.eclipse.ui.part.IPage#setFocus()
219      */

220     public void setFocus() {
221         Control control = getControl();
222         if (control != null) {
223             control.setFocus();
224         }
225         updateSelectionDependentActions();
226     }
227     
228     protected void createActions() {
229         fClearOutputAction= new ClearOutputAction(getViewer());
230         
231         // In order for the clipboard actions to accessible via their shortcuts
232
// (e.g., Ctrl-C, Ctrl-V), we *must* set a global action handler for
233
// each action
234
IActionBars actionBars= getSite().getActionBars();
235         TextViewerAction action= new TextViewerAction(getViewer(), ITextOperationTarget.COPY);
236         action.configureAction(ConsoleMessages.getString("MessageConsolePage.&Copy@Ctrl+C_6"), ConsoleMessages.getString("MessageConsolePage.Copy_7"), ConsoleMessages.getString("MessageConsolePage.Copy_7")); //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$
237
action.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
238         setGlobalAction(actionBars, ActionFactory.COPY.getId(), action);
239         action= new TextViewerAction(getViewer(), ITextOperationTarget.SELECT_ALL);
240         action.configureAction(ConsoleMessages.getString("MessageConsolePage.Select_&All@Ctrl+A_12"), ConsoleMessages.getString("MessageConsolePage.Select_All"), ConsoleMessages.getString("MessageConsolePage.Select_All")); //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$
241
setGlobalAction(actionBars, ActionFactory.SELECT_ALL.getId(), action);
242         
243         //XXX Still using "old" resource access
244
ResourceBundle JavaDoc bundle= ResourceBundle.getBundle("org.eclipse.ui.internal.console.ConsoleMessages"); //$NON-NLS-1$
245
setGlobalAction(actionBars, ActionFactory.FIND.getId(), new FindReplaceAction(bundle, "find_replace_action.", getConsoleView())); //$NON-NLS-1$
246

247         action= new TextViewerGotoLineAction(getViewer());
248         setGlobalAction(actionBars, ITextEditorActionConstants.GOTO_LINE, action);
249                                 
250         actionBars.updateActionBars();
251                 
252         fSelectionActions.add(ActionFactory.COPY.getId());
253         fSelectionActions.add(ActionFactory.FIND.getId());
254     }
255     
256     protected void updateSelectionDependentActions() {
257         Iterator JavaDoc iterator= fSelectionActions.iterator();
258         while (iterator.hasNext()) {
259             updateAction((String JavaDoc)iterator.next());
260         }
261     }
262     
263     protected void updateAction(String JavaDoc actionId) {
264         IAction action= (IAction)fGlobalActions.get(actionId);
265         if (action instanceof IUpdate) {
266             ((IUpdate) action).update();
267         }
268     }
269         
270     protected void setGlobalAction(IActionBars actionBars, String JavaDoc actionID, IAction action) {
271         fGlobalActions.put(actionID, action);
272         actionBars.setGlobalActionHandler(actionID, action);
273     }
274         
275     /**
276      * Returns the viewer in this page.
277      *
278      * @return the viewer in this page
279      */

280     protected MessageConsoleViewer getViewer() {
281         return fViewer;
282     }
283
284     protected void configureToolBar(IToolBarManager mgr) {
285         mgr.appendToGroup(IConsoleConstants.OUTPUT_GROUP, fClearOutputAction);
286     }
287
288     /**
289      * Returns the view this page is contained in
290      *
291      * @return the view this page is contained in
292      */

293     protected IConsoleView getConsoleView() {
294         return fView;
295     }
296     
297     /**
298      * Returns the console this page is displaying
299      *
300      * @return the console this page is displaying
301      */

302     protected MessageConsole getConsole() {
303         return fConsole;
304     }
305     
306     /* (non-Javadoc)
307      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
308      */

309     public Object JavaDoc getAdapter(Class JavaDoc required) {
310         if (IFindReplaceTarget.class.equals(required)) {
311             return getViewer().getFindReplaceTarget();
312         }
313         if (Widget.class.equals(required)) {
314             return getViewer().getTextWidget();
315         }
316         return null;
317     }
318
319     /**
320      * Sets the font for this page.
321      *
322      * @param font font
323      */

324     protected void setFont(Font font) {
325         getViewer().getTextWidget().setFont(font);
326     }
327
328     /**
329      * Refreshes this page
330      */

331     protected void refresh() {
332         getViewer().refresh();
333     }
334 }
335
Popular Tags