KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > console > TextConsolePage


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
12 package org.eclipse.ui.console;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator 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.resource.JFaceResources;
28 import org.eclipse.jface.text.IDocument;
29 import org.eclipse.jface.text.IFindReplaceTarget;
30 import org.eclipse.jface.text.ITextListener;
31 import org.eclipse.jface.text.ITextOperationTarget;
32 import org.eclipse.jface.text.TextEvent;
33 import org.eclipse.jface.util.IPropertyChangeListener;
34 import org.eclipse.jface.util.PropertyChangeEvent;
35 import org.eclipse.jface.viewers.ISelectionChangedListener;
36 import org.eclipse.jface.viewers.SelectionChangedEvent;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.Menu;
40 import org.eclipse.swt.widgets.Widget;
41 import org.eclipse.ui.IActionBars;
42 import org.eclipse.ui.ISharedImages;
43 import org.eclipse.ui.IWorkbenchActionConstants;
44 import org.eclipse.ui.PartInitException;
45 import org.eclipse.ui.PlatformUI;
46 import org.eclipse.ui.actions.ActionFactory;
47 import org.eclipse.ui.console.actions.ClearOutputAction;
48 import org.eclipse.ui.console.actions.TextViewerAction;
49 import org.eclipse.ui.internal.console.ConsoleMessages;
50 import org.eclipse.ui.internal.console.ConsoleResourceBundleMessages;
51 import org.eclipse.ui.internal.console.FollowHyperlinkAction;
52 import org.eclipse.ui.internal.console.IConsoleHelpContextIds;
53 import org.eclipse.ui.part.IPageBookViewPage;
54 import org.eclipse.ui.part.IPageSite;
55 import org.eclipse.ui.texteditor.FindReplaceAction;
56 import org.eclipse.ui.texteditor.IUpdate;
57 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds;
58
59 /**
60  * A page for a text console.
61  * <p>
62  * Clients may contribute actions to the context menu of a text console page
63  * using the <code>org.eclipse.ui.popupMenus</code> extension point. The context
64  * menu identifier for a text console page is the associated console's type
65  * suffixed with <code>.#ContextMenu</code>. When a console does not specify
66  * a type, the context menu id is <code>#ContextMenu</code>.
67  * </p>
68  * <p>
69  * Clients may subclass this class.
70  * </p>
71  * @since 3.1
72  */

73 public class TextConsolePage implements IPageBookViewPage, IPropertyChangeListener, IAdaptable {
74     private IPageSite fSite;
75     private TextConsole fConsole;
76     private IConsoleView fConsoleView;
77     private TextConsoleViewer fViewer;
78     private MenuManager fMenuManager;
79     protected Map JavaDoc fGlobalActions = new HashMap JavaDoc();
80     protected ArrayList JavaDoc fSelectionActions = new ArrayList JavaDoc();
81     protected ClearOutputAction fClearOutputAction;
82     
83     // text selection listener, used to update selection dependent actions on selection changes
84
private ISelectionChangedListener selectionChangedListener = new ISelectionChangedListener() {
85         public void selectionChanged(SelectionChangedEvent event) {
86             updateSelectionDependentActions();
87         }
88     };
89     
90     // updates the find replace action if the document length is > 0
91
private ITextListener textListener = new ITextListener() {
92         public void textChanged(TextEvent event) {
93             IUpdate findReplace = (IUpdate)fGlobalActions.get(ActionFactory.FIND.getId());
94             if (findReplace != null) {
95                 findReplace.update();
96             }
97         }
98     };
99     
100     /**
101      * Constructs a text console page for the given console in the given view.
102      *
103      * @param console text console
104      * @param view console view the page is contained in
105      */

106     public TextConsolePage(TextConsole console, IConsoleView view) {
107         fConsole = console;
108         fConsoleView = view;
109     }
110     
111     /**
112      * Returns a viewer used to display the contents of this page's console.
113      *
114      * @param parent container for the viewer
115      * @return a viewer used to display the contents of this page's console
116      */

117     protected TextConsoleViewer createViewer(Composite parent) {
118         return new TextConsoleViewer(parent, fConsole);
119     }
120     
121     /*
122      * (non-Javadoc)
123      * @see org.eclipse.ui.part.IPageBookViewPage#getSite()
124      */

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

133     public void init(IPageSite pageSite) throws PartInitException {
134         fSite = pageSite;
135     }
136
137     /**
138      * Updates selection dependent actions.
139      */

140     protected void updateSelectionDependentActions() {
141         Iterator JavaDoc iterator= fSelectionActions.iterator();
142         while (iterator.hasNext()) {
143             updateAction((String JavaDoc)iterator.next());
144         }
145     }
146
147     /*
148      * (non-Javadoc)
149      * @see org.eclipse.ui.part.IPage#createControl(org.eclipse.swt.widgets.Composite)
150      */

151     public void createControl(Composite parent) {
152         fViewer = createViewer(parent);
153         fViewer.setConsoleWidth(fConsole.getConsoleWidth());
154         fViewer.setTabWidth(fConsole.getTabWidth());
155         fConsole.addPropertyChangeListener(this);
156         JFaceResources.getFontRegistry().addListener(this);
157         
158         String JavaDoc id = "#ContextMenu"; //$NON-NLS-1$
159
if (getConsole().getType() != null) {
160             id = getConsole().getType() + "." + id; //$NON-NLS-1$
161
}
162         fMenuManager= new MenuManager("#ContextMenu", id); //$NON-NLS-1$
163
fMenuManager.setRemoveAllWhenShown(true);
164         fMenuManager.addMenuListener(new IMenuListener() {
165             public void menuAboutToShow(IMenuManager m) {
166                 contextMenuAboutToShow(m);
167             }
168         });
169         Menu menu = fMenuManager.createContextMenu(getControl());
170         getControl().setMenu(menu);
171         
172         createActions();
173         configureToolBar(getSite().getActionBars().getToolBarManager());
174         
175         getSite().registerContextMenu(id, fMenuManager, fViewer);
176         getSite().setSelectionProvider(fViewer);
177         
178         fViewer.getSelectionProvider().addSelectionChangedListener(selectionChangedListener);
179         fViewer.addTextListener(textListener);
180     }
181
182     /*
183      * (non-Javadoc)
184      * @see org.eclipse.ui.part.IPage#dispose()
185      */

186     public void dispose() {
187         fConsole.removePropertyChangeListener(this);
188         JFaceResources.getFontRegistry().removeListener(this);
189         
190         if (fMenuManager != null) {
191             fMenuManager.dispose();
192         }
193         fClearOutputAction = null;
194         fSelectionActions.clear();
195         fGlobalActions.clear();
196         
197         fViewer.getSelectionProvider().removeSelectionChangedListener(selectionChangedListener);
198         fViewer.removeTextListener(textListener);
199         fViewer = null;
200     }
201
202
203     /*
204      * (non-Javadoc)
205      * @see org.eclipse.ui.part.IPage#getControl()
206      */

207     public Control getControl() {
208         return fViewer != null ? fViewer.getControl() : 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         if (fViewer != null) {
222             fViewer.getTextWidget().setFocus();
223         }
224     }
225
226     /*
227      * (non-Javadoc)
228      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
229      */

230     public void propertyChange(PropertyChangeEvent event) {
231         if (fViewer != null) {
232             Object JavaDoc source = event.getSource();
233             String JavaDoc property = event.getProperty();
234             
235             if (source.equals(fConsole) && IConsoleConstants.P_FONT.equals(property)) {
236                 fViewer.setFont(fConsole.getFont());
237             } else if (IConsoleConstants.P_FONT_STYLE.equals(property)) {
238                 fViewer.getTextWidget().redraw();
239             } else if (property.equals(IConsoleConstants.P_STREAM_COLOR)) {
240                 fViewer.getTextWidget().redraw();
241             } else if (source.equals(fConsole) && property.equals(IConsoleConstants.P_TAB_SIZE)) {
242                 Integer JavaDoc tabSize = (Integer JavaDoc)event.getNewValue();
243                 fViewer.setTabWidth(tabSize.intValue());
244             } else if (source.equals(fConsole) && property.equals(IConsoleConstants.P_CONSOLE_WIDTH)) {
245                 fViewer.setConsoleWidth(fConsole.getConsoleWidth());
246             } else if (IConsoleConstants.P_BACKGROUND_COLOR.equals(property)) {
247                 fViewer.getTextWidget().setBackground(fConsole.getBackground());
248             }
249         }
250     }
251
252     /**
253      * Creates actions.
254      */

255     protected void createActions() {
256         IActionBars actionBars= getSite().getActionBars();
257         TextViewerAction action= new TextViewerAction(fViewer, ITextOperationTarget.SELECT_ALL);
258         action.configureAction(ConsoleMessages.TextConsolePage_SelectAllText, ConsoleMessages.TextConsolePage_SelectAllDescrip, ConsoleMessages.TextConsolePage_SelectAllDescrip);
259         action.setActionDefinitionId(IWorkbenchActionDefinitionIds.SELECT_ALL);
260         PlatformUI.getWorkbench().getHelpSystem().setHelp(action, IConsoleHelpContextIds.CONSOLE_SELECT_ALL_ACTION);
261         setGlobalAction(actionBars, ActionFactory.SELECT_ALL.getId(), action);
262         
263         action= new TextViewerAction(fViewer, ITextOperationTarget.CUT);
264         action.configureAction(ConsoleMessages.TextConsolePage_CutText, ConsoleMessages.TextConsolePage_CutDescrip, ConsoleMessages.TextConsolePage_CutDescrip);
265         action.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_CUT));
266         action.setActionDefinitionId(IWorkbenchActionDefinitionIds.CUT);
267         PlatformUI.getWorkbench().getHelpSystem().setHelp(action, IConsoleHelpContextIds.CONSOLE_CUT_ACTION);
268         setGlobalAction(actionBars, ActionFactory.CUT.getId(), action);
269         
270         action= new TextViewerAction(fViewer, ITextOperationTarget.COPY);
271         action.configureAction(ConsoleMessages.TextConsolePage_CopyText, ConsoleMessages.TextConsolePage_CopyDescrip, ConsoleMessages.TextConsolePage_CopyDescrip);
272         action.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
273         action.setActionDefinitionId(IWorkbenchActionDefinitionIds.COPY);
274         PlatformUI.getWorkbench().getHelpSystem().setHelp(action, IConsoleHelpContextIds.CONSOLE_COPY_ACTION);
275         setGlobalAction(actionBars, ActionFactory.COPY.getId(), action);
276         
277         action= new TextViewerAction(fViewer, ITextOperationTarget.PASTE);
278         action.configureAction(ConsoleMessages.TextConsolePage_PasteText, ConsoleMessages.TextConsolePage_PasteDescrip, ConsoleMessages.TextConsolePage_PasteDescrip);
279         action.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_PASTE));
280         action.setActionDefinitionId(IWorkbenchActionDefinitionIds.PASTE);
281         PlatformUI.getWorkbench().getHelpSystem().setHelp(action, IConsoleHelpContextIds.CONSOLE_PASTE_ACTION);
282         setGlobalAction(actionBars, ActionFactory.PASTE.getId(), action);
283         
284         fClearOutputAction = new ClearOutputAction(fConsole);
285         
286         ResourceBundle JavaDoc bundle = ConsoleResourceBundleMessages.getBundle();
287         FindReplaceAction fraction = new FindReplaceAction(bundle, "find_replace_action_", fConsoleView); //$NON-NLS-1$
288
PlatformUI.getWorkbench().getHelpSystem().setHelp(fraction, IConsoleHelpContextIds.CONSOLE_FIND_REPLACE_ACTION);
289         setGlobalAction(actionBars, ActionFactory.FIND.getId(), fraction);
290
291         fSelectionActions.add(ActionFactory.CUT.getId());
292         fSelectionActions.add(ActionFactory.COPY.getId());
293         fSelectionActions.add(ActionFactory.PASTE.getId());
294         fSelectionActions.add(ActionFactory.FIND.getId());
295         
296         actionBars.updateActionBars();
297     }
298     
299     /**
300      * Configures an action for key bindings.
301      *
302      * @param actionBars action bars for this page
303      * @param actionID action definition id
304      * @param action associated action
305      */

306     protected void setGlobalAction(IActionBars actionBars, String JavaDoc actionID, IAction action) {
307         fGlobalActions.put(actionID, action);
308         actionBars.setGlobalActionHandler(actionID, action);
309     }
310
311     /* (non-Javadoc)
312      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
313      */

314     public Object JavaDoc getAdapter(Class JavaDoc required) {
315         if (IFindReplaceTarget.class.equals(required)) {
316             return fViewer.getFindReplaceTarget();
317         }
318         if (Widget.class.equals(required)) {
319             return fViewer.getTextWidget();
320         }
321         return null;
322     }
323     
324     /**
325      * Returns the view this page is contained in.
326      *
327      * @return the view this page is contained in
328      */

329     protected IConsoleView getConsoleView() {
330         return fConsoleView;
331     }
332     
333     /**
334      * Returns the console this page is displaying.
335      *
336      * @return the console this page is displaying
337      */

338     protected IConsole getConsole() {
339         return fConsole;
340     }
341     
342     /**
343      * Updates the global action with the given id
344      *
345      * @param actionId action definition id
346      */

347     protected void updateAction(String JavaDoc actionId) {
348         IAction action= (IAction)fGlobalActions.get(actionId);
349         if (action instanceof IUpdate) {
350             ((IUpdate) action).update();
351         }
352     }
353
354     
355     /**
356      * Fill the context menu
357      *
358      * @param menuManager menu
359      */

360     protected void contextMenuAboutToShow(IMenuManager menuManager) {
361         IDocument doc= fViewer.getDocument();
362         if (doc == null) {
363             return;
364         }
365
366         menuManager.add((IAction)fGlobalActions.get(ActionFactory.CUT.getId()));
367         menuManager.add((IAction)fGlobalActions.get(ActionFactory.COPY.getId()));
368         menuManager.add((IAction)fGlobalActions.get(ActionFactory.PASTE.getId()));
369         menuManager.add((IAction)fGlobalActions.get(ActionFactory.SELECT_ALL.getId()));
370         
371         menuManager.add(new Separator("FIND")); //$NON-NLS-1$
372
menuManager.add((IAction)fGlobalActions.get(ActionFactory.FIND.getId()));
373         menuManager.add(new FollowHyperlinkAction(fViewer));
374         menuManager.add(fClearOutputAction);
375         
376         menuManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
377     }
378
379     protected void configureToolBar(IToolBarManager mgr) {
380         mgr.appendToGroup(IConsoleConstants.OUTPUT_GROUP, fClearOutputAction);
381     }
382
383
384     /**
385      * Returns the viewer contained in this page.
386      *
387      * @return the viewer contained in this page
388      */

389     public TextConsoleViewer getViewer() {
390         return fViewer;
391     }
392     
393     /**
394      * Sets the viewer contained in this page.
395      *
396      * @param viewer text viewer
397      */

398     public void setViewer(TextConsoleViewer viewer) {
399         this.fViewer = viewer;
400     }
401 }
402
Popular Tags