1 11 package org.eclipse.ui.internal.console; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.ResourceBundle ; 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 62 public class MessageConsolePage implements IPageBookViewPage, IAdaptable, IPropertyChangeListener { 63 64 private IPageSite fSite = null; 66 67 private MessageConsoleViewer fViewer = null; 69 70 private IConsoleView fView; 72 73 private MessageConsole fConsole; 75 76 private ISelectionChangedListener fTextListener = new ISelectionChangedListener() { 78 public void selectionChanged(SelectionChangedEvent event) { 79 updateSelectionDependentActions(); 80 }}; 81 82 private ClearOutputAction fClearOutputAction; 84 private Map fGlobalActions= new HashMap (10); 85 private List fSelectionActions = new ArrayList (3); 86 87 private Menu fMenu; 89 90 93 public MessageConsolePage(IConsoleView view, MessageConsole console) { 94 fView = view; 95 fConsole = console; 96 } 97 98 101 public IPageSite getSite() { 102 return fSite; 103 } 104 105 108 public void init(IPageSite site) { 109 fSite = site; 110 111 } 112 113 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"); 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()); 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 146 public void propertyChange(PropertyChangeEvent event) { 147 Object source = event.getSource(); 148 String 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 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 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")); 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 204 public Control getControl() { 205 if (fViewer != null) { 206 return fViewer.getControl(); 207 } 208 return null; 209 } 210 211 214 public void setActionBars(IActionBars actionBars) { 215 } 216 217 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 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")); 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")); setGlobalAction(actionBars, ActionFactory.SELECT_ALL.getId(), action); 242 243 ResourceBundle bundle= ResourceBundle.getBundle("org.eclipse.ui.internal.console.ConsoleMessages"); setGlobalAction(actionBars, ActionFactory.FIND.getId(), new FindReplaceAction(bundle, "find_replace_action.", getConsoleView())); 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 iterator= fSelectionActions.iterator(); 258 while (iterator.hasNext()) { 259 updateAction((String )iterator.next()); 260 } 261 } 262 263 protected void updateAction(String 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 actionID, IAction action) { 271 fGlobalActions.put(actionID, action); 272 actionBars.setGlobalActionHandler(actionID, action); 273 } 274 275 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 293 protected IConsoleView getConsoleView() { 294 return fView; 295 } 296 297 302 protected MessageConsole getConsole() { 303 return fConsole; 304 } 305 306 309 public Object getAdapter(Class 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 324 protected void setFont(Font font) { 325 getViewer().getTextWidget().setFont(font); 326 } 327 328 331 protected void refresh() { 332 getViewer().refresh(); 333 } 334 } 335 | Popular Tags |