`
wislin
  • 浏览: 76197 次
  • 性别: Icon_minigender_1
  • 来自: 四川
社区版块
存档分类
最新评论

Creating an Eclipse View (2)

阅读更多
下一个例子:The Word View
添加一个view extension到plugin.xml中去
与上面例子相似,这个extension拥有id,name, icon 和class。此外,extension还包括category元素。category用于对view进行分组,分组的结果在Show view对话框里面显示。
<extension point = "org.eclipse.ui.views">
      <category id = "org.eclipse.ui.article" name = "Articlle">
      </category>
      <view id = "org.eclipse.ui.articles.views.wordview"
            name = "World View"
            icon = "icons\view.gif"
            category = "org.eclipse.ui.article"
            class = "org.eclipse.ui.articles.views.WordView"/>
</extension>

在插件里面为该extension定义一个View Class
该类继承了类ViewPart
public class WordView extends ViewPart
{
       WordFile input;
       ListViewer viewer;
       Action addItemAction, deleteItemAction, selectAllAction;
       IMemento memento;
       
       public WordView(){
            super();
            input = new WordFile(new File("list.lst"));
       }
       /**
        * @see IViewPart.int(IViewSite)
        */
       public void init(IViewSite site) throws PartInitException{
             super.init(site);
             //Nomally we might do other stuff here.
       }

        
       public void createPartControl(Composite parent){
          // Create viewer.
          viewer = new ListViewer(parent);
          viewer.setContentProvider (new WordContentProvider());
          viewer.setLabelProvider(new LabelProvider());
          viewer.setInput(input);
         
          //Create menu and toolbars.
          createAction();
          createMenu();
          createToolbar();
          createContextMenu();
          hookGlobalActions();

          //Restore state from the previous session.
          restoreState();
       }
        
       public void setFocus(){
             viewer.getConstrol().setFocus();
       }
}        


Menus and Toolbars
每个view都有一个局部菜单和局部工具条(local menu & local toolbar)。局部工具条位于view标题条的右边。局部菜单在最初是没有显示出来,但是当你点击菜单按钮的时候菜单就会显示出来,局部菜单位于关闭按钮的左边。例子如下图:

view的菜单和工具条控件最初是空的和不可见的。只有当往工具条和菜单加入元素时,它们才变得可见。View同样可以往位于父窗体底部的状态条(status bar)添加元素。总之,局部菜单、局部工具条和状态条统称为动作条(action bars)

在代码里面,是通过view site来访问动作条动作条(action bars)view siteview partviewPart外部世界的主要接口。如果你的view是ViewPart的直接子类,那么可以通过ViewPart.getViewSite方法来获得view site。如果不是ViewPart的子类,则需要你自己控制好site数据的保存和提取,这些数据要被传进去通过IViewPart.init()。通过site,你就可以调用sitegetActionBars().getMenumanager来获取IMenuManager。或通过getActionBars.getToolBarManager()来获取IToolBarManager

其中,IMenuManagerIToolBarManager接口是JFace的抽象。它们包装了SWT菜单和工具条对象,所以作为插件开发者,可以把这种设计为动作和动作实现。一个动作表示一个用户可以通过菜单和工具条激活的命令。在JFace里面,要创建菜单和工具条,需要通过创建org.eclipse.jface.action.IAction的实例,并把实例添加到IMenuManagerIToolBarManager

定义动作
在Word view例子里,我们将要局部工具条条里加入“Add...”和“Delete”两个动作,往局部菜单里加入“Select All"动作。通常情况下,view的最初动作的实现是通过java code来实现,而其他插件开发者要扩展此局部菜单和局部工具条则需要XML来实现。word view 例子动作是org.eclipse.jface.actions.Action的匿名子类。
public void createActions(){
       addItemAction = new Action("Add...."){
           public void run(){
                  addItem()
           }
       };
       addItemAction.setImageDescriptor(getImageDescription("add.gif"));

       deleteItemAction = new Action("Delecte"){
            public void run(){
                   deleteItem();
            }
       };
       deleteItemActon.setImageDescripton(getImageDescription("delete.gif");

       selectAllAction = new Action("Select All") {
            public void run() {
                 selectAll();
            }
       };

       //Add selection listener;
       viewer.addSelectionChangedListener(new ISelectionChangedListener(){
                public void selectionChanged(SelectionChangedEvent e){
                     updateActionEnablement();
               }
        });
}

获取图片的方法:
/**
  *Return the image descriptor with the given relative path
  */
public ImageDescriptor getImageDescriptor(String relativePath){
       String iconPath = "icons/";
       try{
             ViewsPlugin plugin = ViewsPlugin.getDefault();
             URL installURL = plugin.getDescriptor().getInstallURL();
             URL url = new URL(installURL, iconPath+relativePath);
             return ImageDescriptor.createFromURL(url);
       }catch (MalformedURLException e){
             return ImageDescriptor.getMissingImageDescriptor();
       }
}
private void updateActionEnablement() {
                IStructuredSelection sel = 
                        (IStructuredSelection)viewer.getSelection();
                deleteItemAction.setEnabled(sel.size() > 0);
}



Adding Actions
/**
         * Create menu.
         */
        private void createMenu() {
                IMenuManager mgr =  getViewSite().getActionBars().getMenuManager();
                mgr.add(selectAllAction);
        }
        
        /**
         * Create toolbar.
         */
        private void createToolbar() {
                IToolBarManager mgr = getViewSite().getActionBars().getToolBarManager();
                mgr.add(addItemAction);
                mgr.add(deleteItemAction);
        }
分享到:
评论

相关推荐

    Eclipse 4 Plug-in Development by Example: Beginner's Guide

    "Eclipse Plugin Development by Example: Beginner's Guide" takes the reader through the full journey of plug-in development, starting with an introduction to Eclipse plug-ins, continued through ...

    EclipseHTMLEditor jar

    Web Browser (It works as an Eclipse's editor) Image Viewer Tag Palette CSS code completion and outline DTD code completion, outline and validation JavaScript code completion, outline and ...

    Learning Android: Develop Mobile Apps Using Java and Eclipse(第二版)

    Anatomy of an Android Project Drawable Resources Building the Project Android Emulator Summary Chapter 5 Main Building Blocks A Real-World Example Activities Intents Services Content Providers ...

    EGit用户指南

    Eclipse Git用户指南 目录 1 Getting Started 1.1 Overview 1.2 Basic Tutorial: Adding a project to version control 1.2.1 Configuration 1.2.1.1 Identifying yourself 1.2.1.2 Setting up the Home ...

    Introduction.to.Android.Application.Development(4th,2013.12) pdf

    Creating an AVD for Your Snake Project 61 Creating a Launch Configuration for Your Snake Project 62 Running the Snake Application in the Android Emulator 66 Building Your First Android Application ...

    Apache Geronimo 2.1_ Quick Reference.pdf

    Creating an application-scoped database pool 62 Creating a client-scoped pool 64 Editing an existing pool 66 Importing a pool from another application server 67 Creating an XA pool 69 Using a ...

    android-a programmer's guide

    Creating Your First Android Project in Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Examining the Android-Created Files . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    ORACLE OSB开发指南

    Creating Oracle Service Bus Configuration Projects..................................................................... 2-3 Creating Oracle Service Bus Projects...........................................

    The Definitive Guide to NetBeans Platform

    ■CHAPTER 16 From Eclipse RCP to the NetBeans Platform . . . . . . . . . . . . . . . . . . 279 ■CHAPTER 17 Tips and Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    AndroidEssentials

    Customizing the View.................................................................................67 Creating the Game Loop............................................................................

Global site tag (gtag.js) - Google Analytics