⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myactiongroup.java

📁 Eclipse从入门到精通源代码/第二篇 SWT_JFace篇(6-16章)
💻 JAVA
字号:
/**
 * @作者:陈刚
 * @Email:glchengang@yeah.net
 * @Blog:http://blog.csdn.net/glchengang
 */
package jface.treeviewer;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.actions.ActionGroup;

public class MyActionGroup extends ActionGroup {
    private TreeViewer tv;

    public MyActionGroup(TreeViewer treeViewer) {
        this.tv = treeViewer;
    }

    /**
     * 生成菜单Menu,并将两个Action传入
     */
    public void fillContextMenu(IMenuManager mgr) {
        /*
         * 加入两个Action对象到菜单管理器
         */
        MenuManager menuManager = (MenuManager) mgr; //类型转换一下,注意参数是接口
        menuManager.add(new OpenAction());
        menuManager.add(new RefreshAction());
        menuManager.add(new ExpandAction());
        menuManager.add(new CollapseAction());
        menuManager.add(new AddEntryAction());
        menuManager.add(new RemoveEntryAction());
        menuManager.add(new ModifyEntryAction());
        /*
         * 生成Menu并挂在树Tree上
         */
        Tree tree = tv.getTree();
        Menu menu = menuManager.createContextMenu(tree);
        tree.setMenu(menu);
    }

    /**
     * “打开”的Action类
     */
    private class OpenAction extends Action {
        public OpenAction() {
            setText("打开");
        }

        /**
         * 继承自Action的方法,动作代码写此方法中
         */
        public void run() {
            ITreeEntry obj = getSelTreeEntry();
            if (obj != null)
                MessageDialog.openInformation(null, null, obj.getName());
        }
    }

    /**
     * 刷新的Action类
     */
    private class RefreshAction extends Action {
        public RefreshAction() {
            setText("刷新");
        }

        public void run() {
            tv.refresh();//调用TreeViewer的刷新方法
        }
    }

    /**
     * 展开当前结点的Action类
     */
    private class ExpandAction extends Action {
        public ExpandAction() {
            setText("展开");
        }

        public void run() {
            ITreeEntry obj = getSelTreeEntry();
            if (obj != null)
                tv.expandToLevel(obj, 1); //只展开一层,设值可以超过实际树的层数
        }
    }

    /**
     * 收缩当前结点的Action类
     */
    private class CollapseAction extends Action {
        public CollapseAction() {
            setText("收缩");
        }

        public void run() {
            ITreeEntry obj = getSelTreeEntry();
            if (obj != null)
                tv.collapseToLevel(obj, -1); //-1为将当前结点的所有子结点收缩
        }
    }

    /**
     * 给当前结点增加一个子结点的Action类
     */
    private class AddEntryAction extends Action {
        public AddEntryAction() {
            setText("增加");
        }

        public void run() {
            ITreeEntry obj = getSelTreeEntry();
            if (obj == null || obj instanceof PeopleEntity)
                return;
            InputDialog dialog = new InputDialog(null, "给当前结点增加一个子结点", "输入名称", "abc", null);
            if (dialog.open() == InputDialog.OK) {//如果单击OK按钮
                String entryName = dialog.getValue(); //得到Dialog输入值
                /* 根据单击结点的不同类型生成相应的子结点 */
                ITreeEntry newEntry = null;
                if (obj instanceof CountryEntity)
                    newEntry = new CityEntity(entryName);
                else if (obj instanceof CityEntity)
                    newEntry = new PeopleEntity(entryName);
                /* 在增加子结点之前将父结点展开 */
                if (!tv.getExpandedState(obj))
                    tv.expandToLevel(obj, 1);
                tv.add(obj, newEntry);//增加结点
            }
        }
    }

    /**
     * 删除结点的Action类
     */
    private class RemoveEntryAction extends Action {
        public RemoveEntryAction() {
            setText("删除");
        }

        public void run() {
            ITreeEntry obj = getSelTreeEntry();
            if (obj == null)
                return;
            tv.remove(obj);
        }
    }

    /**
     * 修改结点名称的Action类
     */
    private class ModifyEntryAction extends Action {
        public ModifyEntryAction() {
            setText("修改");
        }

        public void run() {
            ITreeEntry obj = getSelTreeEntry();
            if (obj == null)
                return;
            InputDialog dialog = new InputDialog(null, "修改结点", "输入新名称", obj.getName(), null);
            if (dialog.open() == InputDialog.OK) {
                String entryName = dialog.getValue();
                obj.setName(entryName);
                tv.refresh(obj); //刷新结点
            }
        }
    }

    /**
     * 自定义方法:取得当前选择的结点
     */
    private ITreeEntry getSelTreeEntry() {
        IStructuredSelection selection = (IStructuredSelection) tv.getSelection();
        ITreeEntry entry = (ITreeEntry) (selection.getFirstElement());
        return entry;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -