2014-02-05 86 views
0

注意無法通過perspectiveExtensions

例外在以前版本中的事實,插件ID用數結束因在plugin.xml中定義視圖。這在Eclipse RCP中可能不太可能。

更新版本

我想顯示在plugin.xml定義的角度擴展視圖。

這裏是5 plugin.xml

<?xml version="1.0" encoding="UTF-8"?> 
<?eclipse version="3.4"?> 
<plugin> 

    <extension 
     id="application" 
     point="org.eclipse.core.runtime.applications"> 
     <application> 
     <run 
       class="try_03_eclipseplugin.Application"> 
     </run> 
     </application> 
    </extension> 
    <extension 
     point="org.eclipse.ui.perspectiveExtensions"> 
     <perspectiveExtension 
      targetID="*"> 
     <view 
       id="try_03_eclipseplugin.SampleView" 
       minimized="false" 
       ratio="0.20" 
       relationship="left" 
       relative="org.eclipse.ui.editorss"> 
     </view> 
     </perspectiveExtension> 
    </extension> 
    <extension 
     point="org.eclipse.ui.views"> 
     <view 
      allowMultiple="false" 
      class="try_03_eclipseplugin.SampleView" 
      icon="icons/sample.gif" 
      id="try_03_eclipseplugin.SampleView" 
      name="Sample View"> 
     </view> 
    </extension> 
    <extension 
     point="org.eclipse.ui.handlers"> 
     <handler 
      class="try_03_eclipseplugin.Handler" 
      commandId="Try_03_EclipsePlugin.command1"> 
     </handler> 
    </extension> 
    <extension 
     point="org.eclipse.ui.menus"> 
     <menuContribution 
      allPopups="false" 
      locationURI="menu:org.eclipse.ui.main.menu"> 
     <command 
       commandId="Try_03_EclipsePlugin.command1" 
       label="Try" 
       style="push"> 
     </command> 
     </menuContribution> 
    </extension> 

</plugin> 

這裏爲Application類整個代碼(所有顧問被製成內部類):

package try_03_eclipseplugin; 

import org.eclipse.equinox.app.IApplication; 
import org.eclipse.equinox.app.IApplicationContext; 
import org.eclipse.swt.graphics.Point; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.ui.IWorkbench; 
import org.eclipse.ui.PlatformUI; 
import org.eclipse.ui.application.IWorkbenchWindowConfigurer; 
import org.eclipse.ui.application.WorkbenchAdvisor; 
import org.eclipse.ui.application.WorkbenchWindowAdvisor; 

/** 
* This class controls all aspects of the application's execution 
*/ 
public class Application implements IApplication { 

    /* 
    * (non-Javadoc) 
    * 
    * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app. 
    * IApplicationContext) 
    */ 
    public Object start(IApplicationContext context) throws Exception { 
     Display display = PlatformUI.createDisplay(); 
     try { 

      int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor()); 

      /* 
      int returnCode = PlatformUI.createAndRunWorkbench(display, new WorkbenchAdvisor() { 
       @Override 
       public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) { 
        return new WorkbenchWindowAdvisor(configurer) { 
         public void preWindowOpen() { 
          IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); 
          configurer.setInitialSize(new Point(400, 300)); 
          configurer.setShowCoolBar(false); 
          configurer.setShowStatusLine(false); 
          configurer.setTitle("Hello RCP"); //$NON-NLS-1$ 
         } 
        }; 
       } 

       @Override 
       public String getInitialWindowPerspectiveId() { 
        return null; 
       } 

      }); 
      */ 

      if (returnCode == PlatformUI.RETURN_RESTART) 
       return IApplication.EXIT_RESTART; 
      else 
       return IApplication.EXIT_OK; 
     } finally { 
      display.dispose(); 
     } 
    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see org.eclipse.equinox.app.IApplication#stop() 
    */ 
    public void stop() { 
     if (!PlatformUI.isWorkbenchRunning()) 
      return; 
     final IWorkbench workbench = PlatformUI.getWorkbench(); 
     final Display display = workbench.getDisplay(); 
     display.syncExec(new Runnable() { 
      public void run() { 
       if (!display.isDisposed()) 
        workbench.close(); 
      } 
     }); 
    } 
} 

SampleView該代碼是下面的:

package try_03_eclipseplugin; 


import org.eclipse.swt.widgets.Composite; 
import org.eclipse.ui.part.*; 
import org.eclipse.jface.viewers.*; 
import org.eclipse.swt.graphics.Image; 
import org.eclipse.jface.action.*; 
import org.eclipse.jface.dialogs.MessageDialog; 
import org.eclipse.ui.*; 
import org.eclipse.swt.widgets.Menu; 
import org.eclipse.swt.SWT; 


/** 
* This sample class demonstrates how to plug-in a new 
* workbench view. The view shows data obtained from the 
* model. The sample creates a dummy model on the fly, 
* but a real implementation would connect to the model 
* available either in this or another plug-in (e.g. the workspace). 
* The view is connected to the model using a content provider. 
* <p> 
* The view uses a label provider to define how model 
* objects should be presented in the view. Each 
* view can present the same model objects using 
* different labels and icons, if needed. Alternatively, 
* a single label provider can be shared between views 
* in order to ensure that objects of the same type are 
* presented in the same way everywhere. 
* <p> 
*/ 

public class SampleView extends ViewPart { 

    /** 
    * The ID of the view as specified by the extension. 
    */ 
    public static final String ID = "try_03_eclipseplugin.SampleView"; 

    private TableViewer viewer; 
    private Action action1; 
    private Action action2; 
    private Action doubleClickAction; 

    /* 
    * The content provider class is responsible for 
    * providing objects to the view. It can wrap 
    * existing objects in adapters or simply return 
    * objects as-is. These objects may be sensitive 
    * to the current input of the view, or ignore 
    * it and always show the same content 
    * (like Task List, for example). 
    */ 

    class ViewContentProvider implements IStructuredContentProvider { 
     public void inputChanged(Viewer v, Object oldInput, Object newInput) { 
     } 
     public void dispose() { 
     } 
     public Object[] getElements(Object parent) { 
      return new String[] { "One", "Two", "Three" }; 
     } 
    } 
    class ViewLabelProvider extends LabelProvider implements ITableLabelProvider { 
     public String getColumnText(Object obj, int index) { 
      return getText(obj); 
     } 
     public Image getColumnImage(Object obj, int index) { 
      return getImage(obj); 
     } 
     public Image getImage(Object obj) { 
      return PlatformUI.getWorkbench(). 
        getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT); 
     } 
    } 
    class NameSorter extends ViewerSorter { 
    } 

    /** 
    * The constructor. 
    */ 
    public SampleView() { 
    } 

    /** 
    * This is a callback that will allow us 
    * to create the viewer and initialize it. 
    */ 
    public void createPartControl(Composite parent) { 
     viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); 
     viewer.setContentProvider(new ViewContentProvider()); 
     viewer.setLabelProvider(new ViewLabelProvider()); 
     viewer.setSorter(new NameSorter()); 
     viewer.setInput(getViewSite()); 

     // Create the help context id for the viewer's control 
     PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "Try_EclipsePlugin_03.viewer"); 
     makeActions(); 
     hookContextMenu(); 
     hookDoubleClickAction(); 
     contributeToActionBars(); 
    } 

    private void hookContextMenu() { 
     MenuManager menuMgr = new MenuManager("#PopupMenu"); 
     menuMgr.setRemoveAllWhenShown(true); 
     menuMgr.addMenuListener(new IMenuListener() { 
      public void menuAboutToShow(IMenuManager manager) { 
       SampleView.this.fillContextMenu(manager); 
      } 
     }); 
     Menu menu = menuMgr.createContextMenu(viewer.getControl()); 
     viewer.getControl().setMenu(menu); 
     getSite().registerContextMenu(menuMgr, viewer); 
    } 

    private void contributeToActionBars() { 
     IActionBars bars = getViewSite().getActionBars(); 
     fillLocalPullDown(bars.getMenuManager()); 
     fillLocalToolBar(bars.getToolBarManager()); 
    } 

    private void fillLocalPullDown(IMenuManager manager) { 
     manager.add(action1); 
     manager.add(new Separator()); 
     manager.add(action2); 
    } 

    private void fillContextMenu(IMenuManager manager) { 
     manager.add(action1); 
     manager.add(action2); 
     // Other plug-ins can contribute there actions here 
     manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); 
    } 

    private void fillLocalToolBar(IToolBarManager manager) { 
     manager.add(action1); 
     manager.add(action2); 
    } 

    private void makeActions() { 
     action1 = new Action() { 
      public void run() { 
       showMessage("Action 1 executed"); 
      } 
     }; 
     action1.setText("Action 1"); 
     action1.setToolTipText("Action 1 tooltip"); 
     action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages(). 
      getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK)); 

     action2 = new Action() { 
      public void run() { 
       showMessage("Action 2 executed"); 
      } 
     }; 
     action2.setText("Action 2"); 
     action2.setToolTipText("Action 2 tooltip"); 
     action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages(). 
       getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK)); 
     doubleClickAction = new Action() { 
      public void run() { 
       ISelection selection = viewer.getSelection(); 
       Object obj = ((IStructuredSelection)selection).getFirstElement(); 
       showMessage("Double-click detected on "+obj.toString()); 
      } 
     }; 
    } 

    private void hookDoubleClickAction() { 
     viewer.addDoubleClickListener(new IDoubleClickListener() { 
      public void doubleClick(DoubleClickEvent event) { 
       doubleClickAction.run(); 
      } 
     }); 
    } 
    private void showMessage(String message) { 
     MessageDialog.openInformation(
      viewer.getControl().getShell(), 
      "Sample View", 
      message); 
    } 

    /** 
    * Passing the focus request to the viewer's control. 
    */ 
    public void setFocus() { 
     viewer.getControl().setFocus(); 
    } 
} 

我希望視圖出現imme在程序運行之後進行。但它沒有發生。

同時,我有一個代碼,以顯示與菜單命令視圖的新實例 - 和它的作品,並顯示視圖,仍然顯示圖標。

+0

你引用的Java透視 - 你包括JDT插件,所以,這是可用的? –

+0

對不起,這是寄生痕跡。固定。 – Dims

回答

0

這是因爲缺乏角度的id的。

當定義透視工廠

package try_03_eclipseplugin; 

import org.eclipse.ui.IPageLayout; 
import org.eclipse.ui.IPerspectiveFactory; 

public class PerspectiveFactory implements IPerspectiveFactory { 

    public static final String ID = "Try_03_EclipsePlugin.perspective"; 

    @Override 
    public void createInitialLayout(IPageLayout layout) { 
    } 

} 

plugin.xml描述它,然後返回它的id在顧問:

@Override 
       public String getInitialWindowPerspectiveId() { 
        return PerspectiveFactory.ID; 
        //return "org.eclipse.ui.resourcePerspective"; 
       } 

它開始顯示初始視圖。

還是不明白爲什麼它不與資源的角度來看的ID工作(評論)。