天天看点

ArcEngine java 如何创建自定义菜单、环境菜单、调色板以及动态菜单项

ps: 该文主要翻译arcEngine提供的英文帮助文档

1.自定义的菜单和子菜单通常用来把具有相似功能的菜单项放在一起。这些菜单项可以是ArcEngine提供的标准的命令或者工具,也可以是自定义的命令或者工具。

     自定义的菜单项有两种创建方式:           1).实现IMenuDef 接口。            2). 利用ToolbarMenu实现。      下表列出了这两种方法的不同之处。

ImenuDef ToolBarMenu
只能包含ArcEngine提供的标准的命令或者工具作为菜单项

可以包含Arcengine提供的标准组件;

也可以包含继承了下面接口的自定义组件:

ICommand、IMultiItem、IMenuDef、

IPaletteDef、ToolbarMenu、ToolbarMenu、ToobarPalette

只能放在toolBar上作为菜单

可以放在toolbar上作为菜单

可以作为对键盘或者鼠标相应的弹出菜单而出现在任何地方

示例代码:

class NavigationMenu implements IMenuDef{
    // Set the menu caption.

    public String getCaption(){
        return "Navigation";
    }

    //Set the menu name.
    public String getName(){
        return "Navigation";
    }


    // Set the number of menu items.
    public int getItemCount(){
        return 5;
    }

    /*
     *Set the implementation for every menu item.
     *Note that the number of cases in the getItemInfo() is equivalent 
     * to the number of items returned by getItemCount() method.
     */

    public void getItemInfo(int pos, IItemDef itemDef){
        try{
            switch (pos){

                case 0:
                    itemDef.setID(ControlsMapZoomInFixedCommand.getClsid());
                    break;
                case 1:
                    itemDef.setID(ControlsMapZoomOutFixedCommand.getClsid());
                    break;
                case 2:
                    itemDef.setID(ControlsMapFullExtentCommand.getClsid());
                    break;
                case 3:
                    itemDef.setID(ControlsMapZoomToLastExtentBackCommand.getClsid());
                    break;
                case 4:
                    itemDef.setID(ControlsMapZoomToLastExtentForwardCommand.getClsid
                        ());
                    break;
                default:
                    //Do nothing.
            }
        }
        catch (IOException ex){
            System.out.println("Exception in Custom Menu : " + ex);
            ex.printStackTrace();
        }
    }
}
           
利用toolbarMenu实现菜单
           
//Instantiate ToolbarMenu.
ToolbarMenu myToolbarMenu = new ToolbarMenu();
myToolbarMenu.setCaption(� � �CustomMenu ");

//Add standard components.
myToolbarMenu.addItem(ControlsMapMeasureTool.getClsid(), 0,  - 1, false,
    esriCommandStyles.esriCommandStyleTextOnly); 

//Add custom created commands or tools. 
ICommand myCommand = new MyCommand(); myToolbarMenu.addItem(myCommand, 0,  - 1,
    false, esriCommandStyles.esriCommandStyleTextOnly); 

//Add to the toolbar: variable pos specifies the position of the menu in the toolbar items.
//The value -1 adds it to the end of the toolbar items.

toolbarBean.addMenuItem(myToolbarMenu, pos, true, 0);
           

2.环境菜单(也称上下文菜单)      环境菜单的创建以及属性的设置与自定义菜单类似,区别在于环境菜单通常通过在某种环境下(比如鼠标点击)调用toolbarMenu.popup()来实现。 3. 创建自定义的调色板      右图显示的测试调色板菜单点击后的显示

     调色板与上面的菜单是类似的,不同的是调色板把所有的菜单项放在一个矩形框中,而菜单是把所有的菜单项从上到下依次排列显示。      与自定义菜单的创建完全相似,调色板的创建也有两种方式:分别通过实现IPaletteDef接口,或者用ToolbarPalette类实现。 4. 创建动态菜单项      当菜单的菜单项的具体内容在程序运行前是不确定的,或者需要根据程序的状态进行变动的,MultiItems(动态菜单项)就有了用武之地。举个例子:一般word程序的File菜单的底部都会列出最近打开的文件,这些文件名就可以看作是动态菜单项。      动态菜单项的创建需要继承IMultiItem接口,然后实现其中的四个函数:      getName();getCaption();       getItemCaption(int pos){在这用代码实现并返回根据系统运行状态变化的菜单项};      onItemClikc(int pos){与菜单项对应的点击事件}。 示例代码:

public class SpatialBookmarks implements IMultiItem{
    private int activeBookmarkIndex =  - 1;

    //Return the MultiItem name.
    public String getName()throws IOException, AutomationException{
        return "Spatial Bookmarks";
    }

    //Return the MultiItem caption.
    public String getCaption()throws IOException, AutomationException{
        return "Spatial Bookmarks";
    }

    //Return the item caption specified by pos.
    public String getItemCaption(int pos)throws IOException, AutomationException{
        try{
            // Get focus map bookmarks.
            IMapBookmarks mapBookmarks = (IMapBookmarks)hookHelper.getFocusMap();
            // Get enumerator bookmarks.
            IEnumSpatialBookmark enumSpatialBookmarks = mapBookmarks.getBookmarks();
            enumSpatialBookmarks.reset();
            // Loop through the bookmarks to get bookmark names.
            ISpatialBookmark spatialBookmark = enumSpatialBookmarks.next();
            int bookmarkCount = 0;
            while (spatialBookmark != null){
                // Get the correct bookmark.
                if (bookmarkCount == pos){
                    // Return the bookmark name.
                    return spatialBookmark.getName();
                }
                bookmarkCount = bookmarkCount + 1;
                spatialBookmark = enumSpatialBookmarks.next();
            }
        }
        catch (Exception e){}
        return "";
    }

    //Provide implementation for the onClick event for every item.
    public void onItemClick(int index)throws IOException, AutomationException{
        //Set the current active bookmark.
        this.activeBookmarkIndex = index;
        // Get the focus map bookmarks.
        IMapBookmarks mapBookmarks = (IMapBookmarks)hookHelper.getFocusMap();
        // Get enumerator bookmarks. 
        IEnumSpatialBookmark enumSpatialBookmarks = mapBookmarks.getBookmarks();
        enumSpatialBookmarks.reset();
        // Loop through the bookmarks to get bookmark to zoom to.
        ISpatialBookmark spatialBookmark = enumSpatialBookmarks.next();
        int bookmarkCount = 0;
        while (spatialBookmark != null){
            // Get the correct bookmark.
            if (bookmarkCount == index){
                // Zoom to the bookmark.
                spatialBookmark.zoomTo(hookHelper.getFocusMap());
                // Refresh the map.
                hookHelper.getActiveView().refresh();
            }
            bookmarkCount = bookmarkCount + 1;
            spatialBookmark = enumSpatialBookmarks.next();
        }
    }

}