下面列出了怎么用org.eclipse.ui.part.IPage的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 static ITextViewer getViewer(IPage page) {
if(page == null){
return null;
}
if(page instanceof TextConsolePage) {
return ((TextConsolePage)page).getViewer();
}
if(page.getClass().equals(MessagePage.class)){
// empty page placeholder
return null;
}
try {
/*
* org.eclipse.cdt.internal.ui.buildconsole.BuildConsolePage does not
* extend TextConsolePage, so we get access to the viewer with dirty tricks
*/
Method method = page.getClass().getDeclaredMethod("getViewer", (Class<?>[])null);
method.setAccessible(true);
return (ITextViewer) method.invoke(page, (Object[])null);
} catch (Exception e) {
// AnyEditToolsPlugin.logError("Can't get page viewer from the console page", e);
}
return null;
}
public Object execute(ExecutionEvent event) throws ExecutionException
{
// assumes to expand to level 1 if not specified
int level = 1;
String levelStr = event.getParameter(LEVEL);
if (levelStr != null)
{
level = Integer.parseInt(levelStr);
}
IWorkbenchPart part = HandlerUtil.getActivePart(event);
if (part instanceof ContentOutline)
{
IPage page = ((ContentOutline) part).getCurrentPage();
if (page instanceof CommonOutlinePage)
{
CommonOutlinePage outlinePage = (CommonOutlinePage) page;
// we want to expand to the specified level and collapse everything below
outlinePage.collapseAll();
outlinePage.expandToLevel(level);
}
}
return null;
}
protected void hijackConsole(IViewPart view)
{
if (view instanceof ConsoleView)
{
IPage currentPage = ((ConsoleView) view).getCurrentPage();
if (currentPage != null)
{
hookTheme(currentPage.getControl(), false);
}
}
}
public Object execute(ExecutionEvent event) throws ExecutionException
{
IWorkbenchPart part = HandlerUtil.getActivePart(event);
if (part instanceof ContentOutline)
{
IPage page = ((ContentOutline) part).getCurrentPage();
if (page instanceof CommonOutlinePage)
{
((CommonOutlinePage) page).collapseAll();
}
}
return null;
}
public Object execute(ExecutionEvent event) throws ExecutionException
{
IWorkbenchPart part = HandlerUtil.getActivePart(event);
if (part instanceof ContentOutline)
{
IPage page = ((ContentOutline) part).getCurrentPage();
if (page instanceof CommonOutlinePage)
{
((CommonOutlinePage) page).expandAll();
}
}
return null;
}
@Override
protected IPage createDefaultPage(PageBook book) {
MessagePage page = new MessagePage();
initPage(page);
page.createControl(book);
page.setMessage(CamelDesignerMessages.getString("SpringConfigurationView_defaultMessage")); //$NON-NLS-1$
return page;
}
/**
* Creates and returns the default page for this view.
*
* @param book
* the pagebook control
* @return the default page
*/
protected IPage createDefaultPage( PageBook book )
{
MessagePage page = new MessagePage( );
initPage( page );
page.createControl( book );
page.setMessage( defaultText );
return page;
}
/**
* Destroys a page in the pagebook for a particular part. This page was
* returned as a result from <code>doCreatePage</code>.
*
* @param part
* the input part
* @param pageRecord
* a page record for the part
* @see #doCreatePage
*/
protected void doDestroyPage( IWorkbenchPart part, PageRec pageRecord )
{
if ( treeViewPage != null && prefs != null )
{
prefs.removePreferenceChangeListener( treeViewPage );
}
this.resourceFolder = null;
IPage page = pageRecord.page;
page.dispose( );
pageRecord.dispose( );
}
/**
* Creates and returns the default page for this view.
*
* @param book
* the pagebook control
* @return the default page
*/
protected IPage createDefaultPage( PageBook book )
{
MessagePage page = new MessagePage( );
initPage( page );
page.createControl( book );
page.setMessage( defaultText );
return page;
}
protected IPage createDefaultPage( PageBook book )
{
MessagePage page = new MessagePage( );
initPage( page );
page.createControl( book );
page.setMessage( defaultText );
if ( defaultPartName == null )
{
defaultPartName = getPartName( );
}
return page;
}
@Override
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
boolean isForeignSelection = true;
if (part instanceof ContentOutline) {
ContentOutline contentOutline = (ContentOutline) part;
IPage currentPage = contentOutline.getCurrentPage();
if (currentPage instanceof OutlinePageBook) {
OutlinePageBook pageBook = (OutlinePageBook) currentPage;
isForeignSelection = pageBook.getCasViewPage() != this;
}
}
if (isForeignSelection && getSite().getPage().getActiveEditor() == editor) {
if (selection instanceof StructuredSelection) {
AnnotationSelection annotations = new AnnotationSelection((StructuredSelection) selection);
if (!annotations.isEmpty()) {
ISelection tableSelection = new StructuredSelection(new AnnotationTreeNode(editor
.getDocument(), annotations.getFirst()));
mTableViewer.setSelection(tableSelection, true);
}
}
}
}
@Override
protected IPage createDefaultPage(PageBook book) {
MessagePage page = new MessagePage();
initPage(page);
page.createControl(book);
page.setMessage(editorNotAvailableMessage);
return page;
}
@Override
protected IPage createDefaultPage(PageBook book) {
MessagePage page = new MessagePage();
initPage(page);
page.createControl(book);
page.setMessage(editorNotAvailableMessage);
return page;
}
/**
* @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();
}
}
}
/**
* Destroys a page in the pagebook for a particular part. This page was
* returned as a result from <code>doCreatePage</code>.
*
* @param part
* the input part
* @param pageRecord
* a page record for the part
* @see #doCreatePage
*/
protected void doDestroyPage( IWorkbenchPart part, PageRec pageRecord )
{
IPage page = pageRecord.page;
page.dispose( );
pageRecord.dispose( );
}
/**
* Destroys a page in the pagebook for a particular part. This page was
* returned as a result from <code>doCreatePage</code>.
*
* @param part
* the input part
* @param pageRecord
* a page record for the part
* @see #doCreatePage
*/
protected void doDestroyPage( IWorkbenchPart part, PageRec pageRecord )
{
IPage page = pageRecord.page;
page.dispose( );
pageRecord.dispose( );
}