1 11 package org.eclipse.debug.internal.ui.views.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 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 76 public class ProcessConsolePage implements IPageBookViewPage, ISelectionListener, IAdaptable, IShowInSource, IShowInTargetList, IDebugEventSetListener, ITextListener { 77 78 private IPageSite fSite = null; 80 81 private ConsoleViewer fViewer = null; 83 84 private IConsoleView fView; 86 87 private ProcessConsole fConsole; 89 90 private boolean fIsLocked = DebugUIPlugin.getDefault().getPreferenceStore().getBoolean(IInternalDebugUIConstants.PREF_CONSOLE_SCROLL_LOCK); 92 93 private ISelectionChangedListener fTextListener = new ISelectionChangedListener() { 95 public void selectionChanged(SelectionChangedEvent event) { 96 updateSelectionDependentActions(); 97 }}; 98 99 private ClearOutputAction fClearOutputAction; 101 private Map fGlobalActions= new HashMap (10); 102 protected List fSelectionActions = new ArrayList (3); 103 private FollowHyperlinkAction fFollowLinkAction; 104 private ScrollLockAction fScrollLockAction; 105 private ConsoleTerminateAction fTerminate; 106 private ConsoleRemoveAllTerminatedAction fRemoveTerminated; 107 private KeyBindingFollowHyperlinkAction fKeyBindingFollowLinkAction; 108 109 private Menu fMenu; 111 112 115 public ProcessConsolePage(IConsoleView view, ProcessConsole console) { 116 fView = view; 117 fConsole = console; 118 } 119 120 123 public IPageSite getSite() { 124 return fSite; 125 } 126 127 130 public void init(IPageSite site) { 131 fSite = site; 132 133 } 134 135 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"); 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()); 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 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")); 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 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 233 public Control getControl() { 234 if (fViewer != null) { 235 return fViewer.getControl(); 236 } 237 return null; 238 } 239 240 243 public void setActionBars(IActionBars actionBars) { 244 } 245 246 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 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")); 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")); 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")); 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")); setGlobalAction(actionBars, ActionFactory.SELECT_ALL.getId(), action); 280 281 ResourceBundle bundle= ResourceBundle.getBundle("org.eclipse.debug.internal.ui.views.DebugUIViewsMessages"); setGlobalAction(actionBars, ActionFactory.FIND.getId(), new FindReplaceAction(bundle, "find_replace_action.", getConsoleView())); 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"); 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 iterator= fSelectionActions.iterator(); 311 while (iterator.hasNext()) { 312 updateAction((String )iterator.next()); 313 } 314 } 315 316 protected void updateAction(String 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 actionID, IAction action) { 324 fGlobalActions.put(actionID, action); 325 actionBars.setGlobalActionHandler(actionID, action); 326 } 327 328 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.appendToGroup(IConsoleConstants.OUTPUT_GROUP, fScrollLockAction); 342 mgr.appendToGroup(IConsoleConstants.OUTPUT_GROUP, fClearOutputAction); 343 } 344 345 350 protected IProcess getProcess() { 351 return getConsole().getProcess(); 352 } 353 354 359 protected IConsoleView getConsoleView() { 360 return fView; 361 } 362 363 368 protected ProcessConsole getConsole() { 369 return fConsole; 370 } 371 372 375 public void selectionChanged(IWorkbenchPart part, ISelection selection) { 376 if (getProcess().equals(DebugUITools.getCurrentProcess())) { 377 getConsoleView().display(getConsole()); 378 } 379 } 380 381 384 public Object getAdapter(Class 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 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 422 public String [] getShowInTargetIds() { 423 return new String [] {IDebugUIConstants.ID_DEBUG_VIEW}; 424 } 425 426 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 r = new Runnable () { 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 454 protected boolean isAvailable() { 455 return getControl() != null; 456 } 457 458 461 public void textChanged(TextEvent event) { 462 IUpdate findReplace = (IUpdate)fGlobalActions.get(ActionFactory.FIND.getId()); 464 if (findReplace != null) { 465 findReplace.update(); 466 } 467 } 468 } 469 | Popular Tags |