1 11 12 package org.eclipse.ui.console; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 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.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 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 fGlobalActions = new HashMap (); 80 protected ArrayList fSelectionActions = new ArrayList (); 81 protected ClearOutputAction fClearOutputAction; 82 83 private ISelectionChangedListener selectionChangedListener = new ISelectionChangedListener() { 85 public void selectionChanged(SelectionChangedEvent event) { 86 updateSelectionDependentActions(); 87 } 88 }; 89 90 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 106 public TextConsolePage(TextConsole console, IConsoleView view) { 107 fConsole = console; 108 fConsoleView = view; 109 } 110 111 117 protected TextConsoleViewer createViewer(Composite parent) { 118 return new TextConsoleViewer(parent, fConsole); 119 } 120 121 125 public IPageSite getSite() { 126 return fSite; 127 } 128 129 133 public void init(IPageSite pageSite) throws PartInitException { 134 fSite = pageSite; 135 } 136 137 140 protected void updateSelectionDependentActions() { 141 Iterator iterator= fSelectionActions.iterator(); 142 while (iterator.hasNext()) { 143 updateAction((String )iterator.next()); 144 } 145 } 146 147 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 id = "#ContextMenu"; if (getConsole().getType() != null) { 160 id = getConsole().getType() + "." + id; } 162 fMenuManager= new MenuManager("#ContextMenu", id); 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 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 207 public Control getControl() { 208 return fViewer != null ? fViewer.getControl() : null; 209 } 210 211 214 public void setActionBars(IActionBars actionBars) { 215 } 216 217 220 public void setFocus() { 221 if (fViewer != null) { 222 fViewer.getTextWidget().setFocus(); 223 } 224 } 225 226 230 public void propertyChange(PropertyChangeEvent event) { 231 if (fViewer != null) { 232 Object source = event.getSource(); 233 String 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 tabSize = (Integer )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 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 bundle = ConsoleResourceBundleMessages.getBundle(); 287 FindReplaceAction fraction = new FindReplaceAction(bundle, "find_replace_action_", fConsoleView); 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 306 protected void setGlobalAction(IActionBars actionBars, String actionID, IAction action) { 307 fGlobalActions.put(actionID, action); 308 actionBars.setGlobalActionHandler(actionID, action); 309 } 310 311 314 public Object getAdapter(Class 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 329 protected IConsoleView getConsoleView() { 330 return fConsoleView; 331 } 332 333 338 protected IConsole getConsole() { 339 return fConsole; 340 } 341 342 347 protected void updateAction(String actionId) { 348 IAction action= (IAction)fGlobalActions.get(actionId); 349 if (action instanceof IUpdate) { 350 ((IUpdate) action).update(); 351 } 352 } 353 354 355 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")); 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 389 public TextConsoleViewer getViewer() { 390 return fViewer; 391 } 392 393 398 public void setViewer(TextConsoleViewer viewer) { 399 this.fViewer = viewer; 400 } 401 } 402 | Popular Tags |