下面列出了怎么用org.eclipse.ui.console.IConsoleView的API类实例代码及写法,或者点击链接到github查看源代码。
public static IConsole getConsole(IWorkbenchPart part) {
if(!(part instanceof IViewPart)){
return null;
}
IViewPart vp =(IViewPart) part;
if (vp instanceof PageBookView) {
IPage page = ((PageBookView) vp).getCurrentPage();
ITextViewer viewer = getViewer(page);
if (viewer == null || viewer.getDocument() == null)
return null;
}
IConsole con = null;
try {
con = ((IConsoleView)part).getConsole();
} catch (Exception e) {
}
return con;
}
public void show() {
Runnable runnable = new Runnable() {
public void run() {
// this should only be called from GUI thread
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window == null) {
CppcheclipsePlugin.logError("Could not show console because there is no active workbench window");
return;
}
IWorkbenchPage page = window.getActivePage();
if (page == null) {
CppcheclipsePlugin.logError("Could not show console because there is no active page");
return;
}
try {
IConsoleView view = (IConsoleView) page.showView(IConsoleConstants.ID_CONSOLE_VIEW);
view.display(messageConsole);
} catch (PartInitException e) {
CppcheclipsePlugin.logError("Could not show console", e);
}
}
};
Display.getDefault().asyncExec(runnable);
}
/**
* Support exchange for simple mark on TextConsole
*
* @see com.mulgasoft.emacsplus.commands.IConsoleDispatch#consoleDispatch(org.eclipse.ui.console.TextConsoleViewer, org.eclipse.ui.console.IConsoleView, org.eclipse.core.commands.ExecutionEvent)
*/
public Object consoleDispatch(TextConsoleViewer viewer, IConsoleView activePart, ExecutionEvent event) {
int mark = viewer.getMark();
StyledText st = viewer.getTextWidget();
if (mark != -1) {
try {
st.setRedraw(false);
int offset = st.getCaretOffset();
viewer.setMark(offset);
st.setCaretOffset(mark);
int len = offset - mark;
viewer.setSelectedRange(offset, -len);
} finally {
st.setRedraw(true);
}
}
return null;
}
/**
* @see com.mulgasoft.emacsplus.commands.SexpBaseBackwardHandler#consoleDispatch(TextConsoleViewer, IConsoleView, ExecutionEvent)
*/
public Object consoleDispatch(final TextConsoleViewer viewer, final IConsoleView activePart, ExecutionEvent event) {
IDocument doc = viewer.getDocument();
boolean isBackup = getUniversalCount() > 0; // normal direction
ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
try {
int offset = doTransform(doc, selection, viewer.getTextWidget().getCaretOffset(),isBackup);
if (offset == NO_OFFSET) {
unbalanced(activePart,false);
} else {
endTransform(viewer, offset, selection, new TextSelection(null,offset,offset - selection.getOffset()));
}
} catch (BadLocationException e) {}
return null;
}
/**
* @see com.mulgasoft.emacsplus.commands.IConsoleDispatch#consoleDispatch(TextConsoleViewer, IConsoleView, ExecutionEvent)
*/
public Object consoleDispatch(TextConsoleViewer viewer, IConsoleView activePart, ExecutionEvent event) {
IDocument document = viewer.getDocument();
ITextSelection currentSelection = (ITextSelection)viewer.getSelectionProvider().getSelection();
ITextSelection selection = null;
try {
selection = getNextSexp(document, currentSelection);
if (selection == null) {
selection = currentSelection;
unbalanced(activePart,true);
return null;
} else {
return endTransform(viewer, selection.getOffset(), currentSelection, selection);
}
} catch (BadLocationException e) {
}
return null;
}
/**
* @see com.mulgasoft.emacsplus.commands.IConsoleDispatch#consoleDispatch(TextConsoleViewer, IConsoleView, ExecutionEvent)
*/
public Object consoleDispatch(TextConsoleViewer viewer, IConsoleView activePart, ExecutionEvent event) {
StyledText st = viewer.getTextWidget();
String id = event.getCommand().getId();
boolean isSelect = isMarkEnabled(viewer,(ITextSelection)viewer.getSelection());
int action = getDispatchId(id,isSelect);
if (action > -1) {
st.invokeAction(action);
} else if ((id = getId(isSelect)) != null) {
// support sexps
try {
EmacsPlusUtils.executeCommand(id, null, activePart);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* @see com.mulgasoft.emacsplus.commands.IConsoleDispatch#consoleDispatch(org.eclipse.ui.console.TextConsoleViewer, org.eclipse.ui.console.IConsoleView, org.eclipse.core.commands.ExecutionEvent)
*/
public Object consoleDispatch(TextConsoleViewer viewer, IConsoleView activePart, ExecutionEvent event) {
IDocument document = viewer.getDocument();
ITextSelection currentSelection = (ITextSelection)viewer.getSelectionProvider().getSelection();
ITextSelection selection = new TextSelection(document, viewer.getTextWidget().getCaretOffset(), 0);
try {
selection = getNextSexp(document, selection);
if (selection == null) {
selection = currentSelection;
unbalanced(activePart,true);
return null;
} else {
return endTransform(viewer, selection.getOffset() + selection.getLength(), currentSelection, selection);
}
} catch (BadLocationException e) {
}
return null;
}
/**
* @see com.mulgasoft.emacsplus.commands.IConsoleDispatch#consoleDispatch(org.eclipse.ui.console.TextConsoleViewer, IConsoleView, ExecutionEvent)
*/
public Object consoleDispatch(TextConsoleViewer viewer, IConsoleView activePart, ExecutionEvent event) {
IDocument doc = viewer.getDocument();
int action = -1;
try {
StyledText st = viewer.getTextWidget();
action = getDispatchId(getId(event, viewer));
if (action > -1) {
// set up for kill ring
doc.addDocumentListener(KillRing.getInstance());
// setUpUndo(viewer);
st.invokeAction(action);
}
} finally {
// remove kill ring behavior
if (action > -1) {
doc.removeDocumentListener(KillRing.getInstance());
}
KillRing.getInstance().setKill(null, false);
}
return null;
}
public static Iterable<IConsoleView> iterConsoles() {
List<IConsoleView> consoles = new ArrayList<>();
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window != null) {
IWorkbenchPage page = window.getActivePage();
if (page != null) {
List<IViewPart> consoleParts = getConsoleParts(page, false);
if (consoleParts.size() == 0) {
consoleParts = getConsoleParts(page, true);
}
for (IViewPart iViewPart : consoleParts) {
if (iViewPart instanceof IConsoleView) {
consoles.add((IConsoleView) iViewPart);
}
}
}
}
return consoles;
}
/**
* Reveal the given console. If necessary, this will open the console view, bring it to the front, and reveal the
* given console inside the console view.
*/
public static void revealConsole(IConsole console) {
PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
try {
final IConsoleView view = (IConsoleView) page.showView(IConsoleConstants.ID_CONSOLE_VIEW);
view.display(console);
} catch (PartInitException e) {
// ignore
}
}
});
}
public XdsConsolePage(TextConsole console, IConsoleView view) {
super(console, view);
fPropertyChangeListener = new IPropertyChangeListener() {
public void propertyChange(PropertyChangeEvent event) {
String property = event.getProperty();
if (property.equals(IConsoleConstants.P_CONSOLE_OUTPUT_COMPLETE)) {
setReadOnly();
}
}
};
console.addPropertyChangeListener(fPropertyChangeListener);
}
/**
* (non-Javadoc)
*
* @see IStreamingTextWidget#display()
*/
@Override
public void display() {
// Must sync with the display thread
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
// Get the currently active page
IWorkbenchPage page = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage();
try {
// Load the console view
consoleView = (IConsoleView) page
.showView(IConsoleConstants.ID_CONSOLE_VIEW);
// Create the console instance that will be used to display
// text from this widget.
console = new MessageConsole("CLI", null);
// Add the console to the console manager
ConsolePlugin.getDefault().getConsoleManager()
.addConsoles(new IConsole[] { console });
// Show the console in the view
consoleView.display(console);
console.activate();
// Get an output stream for the console
msgStream = console.newMessageStream();
msgStream.setActivateOnWrite(true);
msgStream.println("Streaming output console activated.");
} catch (PartInitException e) {
// Complain
logger.error("EclipseStreamingTextWidget Message: "
+ "Unable to stream text!");
logger.error(getClass().getName() + " Exception!", e);
}
}
});
return;
}
/**
* Support simple mark on TextConsole
*
* @see com.mulgasoft.emacsplus.commands.IConsoleDispatch#consoleDispatch(TextConsoleViewer, IConsoleView, ExecutionEvent)
*/
public Object consoleDispatch(TextConsoleViewer viewer, IConsoleView activePart, ExecutionEvent event) {
int offset = viewer.getTextWidget().getCaretOffset();
viewer.setSelectedRange(offset, 0);
viewer.setMark(offset);
return null;
}
/**
* @see com.mulgasoft.emacsplus.commands.ConsoleCmdHandler#consoleDispatch(TextConsoleViewer, IConsoleView, ExecutionEvent)
*/
@Override
public Object consoleDispatch(TextConsoleViewer viewer, IConsoleView activePart, ExecutionEvent event) {
Object result = null;
IDocument doc = viewer.getDocument();
try {
IWorkbenchPartSite site = activePart.getSite();
if (site != null) {
IHandlerService service = (IHandlerService) site.getService(IHandlerService.class);
if (doc != null && service != null) {
doc.addDocumentListener(KillRing.getInstance());
String cmdId = getId(event, viewer);
if (cmdId != null) {
result = service.executeCommand(cmdId, null);
}
}
}
} catch (CommandException e) {
// Shouldn't happen as the Command id will be null or valid
e.printStackTrace();
} finally {
if (doc != null) {
doc.removeDocumentListener(KillRing.getInstance());
}
// clear kill command flag
KillRing.getInstance().setKill(null, false);
}
MarkUtils.clearConsoleMark(viewer);
return result;
}
/**
* When called from a console context, will use ST.CUT
*
* @see com.mulgasoft.emacsplus.commands.ConsoleCmdHandler#consoleDispatch(TextConsoleViewer,
* IConsoleView, ExecutionEvent)
*/
public Object consoleDispatch(TextConsoleViewer viewer, IConsoleView activePart, ExecutionEvent event) {
if (viewer.isEditable()) {
IDocument doc = viewer.getDocument();
StyledText st = viewer.getTextWidget();
int offset = st.getCaretOffset();
try {
IRegion info = doc.getLineInformationOfOffset(offset);
int noffset = info.getOffset() + info.getLength();
if (offset == noffset) {
int line = doc.getLineOfOffset(offset);
if (++line < doc.getNumberOfLines()) {
noffset = doc.getLineOffset(line);
if (noffset == doc.getLength()) {
noffset = offset;
}
}
}
if (offset != noffset) {
st.redraw();
st.setSelection(offset, noffset);
KillRing.getInstance().setKill(CUT_LINE_TO_END, false);
return super.consoleDispatch(viewer, activePart, event);
}
viewer.refresh();
} catch (BadLocationException e) {
}
}
return null;
}
/**
* @see com.mulgasoft.emacsplus.commands.IConsoleDispatch#consoleDispatch(TextConsoleViewer, IConsoleView, ExecutionEvent)
*/
public Object consoleDispatch(TextConsoleViewer viewer, IConsoleView activePart, ExecutionEvent event) {
CS saveState = state;
try {
state = CS.C;
StyledText st = viewer.getTextWidget();
st.redraw();
recenter(st);
} finally {
state = saveState;
}
return null;
}
/**
* When called from a console context, use paste
*
* @see com.mulgasoft.emacsplus.commands.IConsoleDispatch#consoleDispatch(org.eclipse.ui.console.TextConsoleViewer, org.eclipse.ui.console.IConsoleView, org.eclipse.core.commands.ExecutionEvent)
*/
public Object consoleDispatch(TextConsoleViewer viewer, IConsoleView activePart, ExecutionEvent event) {
StyledText st = viewer.getTextWidget();
try {
// set directly from the widget
widgetEol = st.getLineDelimiter();
paste(event,st,activePart.getConsole() instanceof IConsole);
} finally {
st.redraw();
widgetEol = null;
}
return null;
}
/**
* @see com.mulgasoft.emacsplus.commands.SexpBaseForwardHandler#consoleDispatch(TextConsoleViewer, IConsoleView, ExecutionEvent)
*/
public Object consoleDispatch(final TextConsoleViewer viewer, final IConsoleView activePart, ExecutionEvent event) {
IDocument doc = viewer.getDocument();
ITextSelection currentSelection = (ITextSelection) viewer.getSelectionProvider().getSelection();
ITextSelection selection = downList(doc, currentSelection);
if (selection == null) {
unbalanced(activePart,false);
} else {
endTransform(viewer,selection.getOffset() + selection.getLength(), currentSelection, selection);
}
return null;
}
/**
* @see org.eclipse.ui.console.IConsole#createPage(org.eclipse.ui.console.IConsoleView)
*/
public IPageBookViewPage createPage(IConsoleView view) {
IPageBookViewPage page = super.createPage(view);
if (page instanceof TextConsolePage) {
myPage = (TextConsolePage)page;
}
return page;
}
@Override
public void run(IAction action) {
for (IConsoleView c : ScriptConsole.iterConsoles()) {
IConsole console = c.getConsole();
if (console instanceof IOConsole) {
IOConsole ioConsole = (IOConsole) console;
processIOConsole(ioConsole);
break;
}
}
}
@Override
public void init(IPageBookViewPage page, IConsole console) {
this.page = page;
this.console = (PydevConsole) console;
view = (IConsoleView) page.getSite().getPage().findView(IConsoleConstants.ID_CONSOLE_VIEW);
DebugUITools.getDebugContextManager().getContextService(page.getSite().getWorkbenchWindow())
.addDebugContextListener(this);
}
public ScrollLockAction(IConsoleView consoleView) {
super(ConsoleMessages.ScrollLockAction_Name);
fConsoleView = consoleView;
setToolTipText(ConsoleMessages.ScrollLockAction_Tooltip);
setImageDescriptor(LangImages.IMG_SCROLL_LOCK.getDescriptor());
boolean checked = fConsoleView.getScrollLock();
setChecked(checked);
}
@Override
public IPageBookViewPage createPage(IConsoleView view) {
xdsConsolePage = new XdsConsolePage(this, view);
return xdsConsolePage;
}
@Override
public IPageBookViewPage createPage(IConsoleView view) {
page = new CppStyleConsolePage(this, view);
return page;
}
public CppStyleConsolePage(CppStyleMessageConsole console, IConsoleView view) {
super(console, view);
this.console = console;
}
/**
* @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
*/
@SuppressWarnings("unchecked")
public Object execute(ExecutionEvent event) throws ExecutionException {
ITextEditor editor = getTextEditor(event);
if (editor == null) {
if (isWindowCommand()) {
Object result = checkExecute(event);
if (result == Check.Fail) {
beep();
result = null;
}
return result;
} else if (isConsoleCommand()) {
// intercept and dispatch execution if console supported and used in a console view
IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
if (activePart != null && (activePart instanceof IConsoleView) && (activePart instanceof PageBookView)) {
IPage textPage = ((PageBookView)activePart).getCurrentPage();
if (textPage instanceof TextConsolePage) {
return ((IConsoleDispatch)this).consoleDispatch(((TextConsolePage)textPage).getViewer(),(IConsoleView)activePart,event);
}
}
}
}
try {
setThisEditor(editor);
isEditable = getEditable();
if (editor == null || isBlocked()) {
beep();
asyncShowMessage(editor, INEDITABLE_BUFFER, true);
return null;
}
// Retrieve the universal-argument parameter value if passed
if (extractUniversalCount(event) != 1) {
// check if we should dispatch a related command based on the universal argument
String dispatchId = checkDispatchId(event.getCommand().getId());
if (dispatchId != null) {
// recurse on new id (inverse or arg value driven)
return dispatchId(editor, dispatchId, getParams(event.getCommand(), event.getParameters()));
}
}
setThisDocument(editor.getDocumentProvider().getDocument(editor.getEditorInput()));
// Get the current selection
ISelectionProvider selectionProvider = editor.getSelectionProvider();
ITextSelection selection = (ITextSelection) selectionProvider.getSelection();
preTransform(editor, selection);
return transformWithCount(editor, getThisDocument(), selection, event);
} finally {
// normal commands clean up here
if (isTransform()) {
postExecute();
}
}
}
public ScriptConsolePage(ScriptConsole console, IConsoleView view, SourceViewerConfiguration cfg) {
super(console, view);
this.cfg = cfg;
}
/**
* Creates the actual page to be shown to the user.
*/
@Override
public IPageBookViewPage createPage(IConsoleView view) {
page = new ScriptConsolePage(this, view, createSourceViewerConfiguration());
return page;
}
@Override
public IPageBookViewPage createPage(IConsoleView view) {
return new ToolsConsolePage(this, view);
}
public ToolsConsolePage(TextConsole console, IConsoleView view) {
super(console, view);
setReadOnly();
}