RCP tree Menu

/*******************************************************************************
 * Copyright (c) 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.snippets;

/*
 * Tree example snippet: show a menu in a tree header
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 *
 * @since 3.5
 */

import org.eclipse.swt.*;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet312 {

    static String[][] files = {
            { "ver.txt", "1 KB", "Text Document", "28/09/2005 9:57 AM",
                    "admin", },
            { "Thumbs.db", "76 KB", "Data Base file", "13/03/2006 3:56 PM",
                    "john", },
            { "daddy.bmp", "148 MB", "Bitmap", "27/10/2008 1:34 PM", "bill", },
            { "io.sys", "48 KB", "File System", "16/12/2008 6:14 AM", "admin", },
            { "Programs", "0 KB", "File Folder", "04/02/2009 12:18 PM", "anne", },
            { "test.rnd", "55 MB", "RND File", "19/02/2009 5:49 PM", "john", },
            { "arial.ttf", "94 KB", "True Type Font", "25/08/2008 1:25 PM",
                    "john", }, };

    static void createMenuItem(Menu parent, final TreeColumn column) {
        final MenuItem itemName = new MenuItem(parent, SWT.CHECK);
        itemName.setText(column.getText());
        itemName.setSelection(column.getResizable());
        itemName.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                if (itemName.getSelection()) {
                    column.setWidth(150);
                    column.setResizable(true);
                } else {
                    column.setWidth(0);
                    column.setResizable(false);
                }
            }
        });
    }

    static void createMenuItem(Menu parent, final Tree tree1) {
        final MenuItem itemName = new MenuItem(parent, SWT.CHECK);

    }

    public static void tree1MouseDown(MouseEvent e, final Tree tree1, Shell shell) {
        TreeItem selected = tree1.getItem(new Point(e.x, e.y)); // 取节点控件
        if (selected != null && e.button == 3) // 如果取到节点控件,且是鼠标右键
        {
            Menu menu = new Menu(shell, SWT.POP_UP); // 为节点建POP UP菜单
            if (selected.getParentItem() != null) // 如果不是根节点
            {

            } else { // 如果,是根节点,即selected.getParentItem()==null为真。
                System.out.println("@@@@$#");
                MenuItem mi4 = new MenuItem(menu, SWT.PUSH); // 为菜单,建菜单项
                mi4.setText("向[" + selected.getText() + "]节点,发送即时消息!");
                mi4.setData(selected.getText()); // 向响应菜单项事件的代码,传递值。
                mi4.addListener(SWT.Selection, new Listener() {
                    public void handleEvent(Event e) { // 向指定用户发送消息!
                        MenuItem mi = (MenuItem) e.widget; // 取菜单项Widget
                        String selected = (String) mi.getData(); // 取,在建立菜单项时,传过来的对象。
                        if (selected != null) { // 添加,响应代码
                            System.out.println("aaa");
                            tree1.setMenu(null); // 取消弹出菜单
                        }
                    }
                });
                tree1.setMenu(menu); // 设置弹出菜单
            }

        } else if (e.button == 3) // 选中的是树形控件的空白区
        {
            // 响应代码段,略
            tree1.setMenu(null); // 取消弹出菜单
        }
    }

    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());

        final Tree tree = new Tree(shell, SWT.V_SCROLL | SWT.H_SCROLL
                | SWT.BORDER);
        tree.setHeaderVisible(true);
        tree.addMouseListener(new MouseListener() {

            @Override
            public void mouseUp(MouseEvent e) {
            }

            @Override
            public void mouseDown(MouseEvent e) {
                tree1MouseDown(e, tree, shell);
            }

            @Override
            public void mouseDoubleClick(MouseEvent e) {

            }
        });
        final Menu headerMenu = new Menu(shell, SWT.POP_UP);
        final TreeColumn columnName = new TreeColumn(tree, SWT.NONE);
        columnName.setText("Name");
        columnName.setWidth(150);
        createMenuItem(headerMenu, columnName);
        final TreeColumn columnSize = new TreeColumn(tree, SWT.NONE);
        columnSize.setText("Size");
        columnSize.setWidth(150);
        createMenuItem(headerMenu, columnSize);
        /*
         * final TreeColumn columnType = new TreeColumn(tree, SWT.NONE);
         * columnType.setText("Type"); columnType.setWidth(150);
         * createMenuItem(headerMenu, columnType); final TreeColumn columnDate =
         * new TreeColumn(tree, SWT.NONE); columnDate.setText("Date");
         * columnDate.setWidth(150); createMenuItem(headerMenu, columnDate);
         * final TreeColumn columnOwner = new TreeColumn(tree, SWT.NONE);
         * columnOwner.setText("Owner"); columnOwner.setWidth(0);
         * columnOwner.setResizable(false); createMenuItem(headerMenu,
         * columnOwner);
         */

        for (int i = 0; i < files.length; i++) {
            TreeItem item = new TreeItem(tree, SWT.NONE);
            item.setText(files[i]);
            TreeItem subItem = new TreeItem(item, SWT.NONE);
            subItem.setText("node"+i);
        }

        /*final Menu treeMenu = new Menu(shell, SWT.POP_UP);
        MenuItem item = new MenuItem(treeMenu, SWT.PUSH);
        item.setText("Open");
        item = new MenuItem(treeMenu, SWT.PUSH);
        item.setText("Open With");

        new MenuItem(treeMenu, SWT.SEPARATOR);// 分隔符

        item = new MenuItem(treeMenu, SWT.PUSH);
        item.setText("Cut");
        item = new MenuItem(treeMenu, SWT.PUSH);
        item.setText("Copy");
        item = new MenuItem(treeMenu, SWT.PUSH);
        item.setText("Paste");

        new MenuItem(treeMenu, SWT.SEPARATOR);// 分隔符
        item = new MenuItem(treeMenu, SWT.PUSH);
        item.setText("Delete");

        tree.addListener(SWT.MenuDetect, new Listener() {
            @Override
            public void handleEvent(Event event) {
                Point pt = display.map(null, tree, new Point(event.x, event.y));
                Rectangle clientArea = tree.getClientArea();
                boolean header = clientArea.y <= pt.y
                        && pt.y < (clientArea.y + tree.getHeaderHeight());
                tree.setMenu(header ? headerMenu : treeMenu);
            }
        });*/

        /*
         * IMPORTANT: Dispose the menus (only the current menu, set with
         * setMenu(), will be automatically disposed)
         */
    /*    tree.addListener(SWT.Dispose, new Listener() {
            @Override
            public void handleEvent(Event event) {
                headerMenu.dispose();
                treeMenu.dispose();
            }
        });*/

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}
时间: 2024-10-27 06:44:30

RCP tree Menu的相关文章

Create Hierarchical Tree To Control Records In Oracle Forms

Download Source Code Providing an example form for creating hierarchical trees in Oracle Forms to control data block records. In this form whenever user selects any node in tree menu then corresponding record is displayed at right side. This form is

10+ 最流行的 jQuery Tree 菜单插件

jstree – jQuery Tree Plugin With HTML & JSON Data jstree is a lightweight and flexible jQuery plugin to create tree menu from HTML & JSON data sources sources.This jQuery Plugin Support AJAX & async callback loading.It is absolutely free, open

实用的树形菜单控件tree

 jQuery plugin: Treeview 这个插件能够把无序列表转换成可展开与收缩的Tree. jQuery plugin: Treeview  jQuery  jstree jsTree是一个基于jQuery的Tree控件.支持XML,JSON,Html三种数据源.提供创建,重命名,移动,删除,拖\放节点操作.可以自己自定义创建,删除,嵌套,重命名,选择节点的规则.在这些操作上可以添加多种监听事件.  jstree  jQuery UI Widgets  FileTreePanel F

Android无限极树状(tree)结构Demo

上篇文章写了一下ExpandableListView,就是两层结构的点击,Android中没有三层结构的组件,这个时候需要自己去扩展一下,可以扩展ExpandableListView,也可以选择扩展ListView.为了是实现这个效果,也是在搜了很多文章(中国的文章你懂的),有个哥们在GitHub开源了项目,奈何封装的太多,看的头晕目眩.看到有人扩展ListView的更符合自己的想法,因为做服务器端的时候也是先加载顶层元素,然后控制点击事件加载父级下面的子元素,有这个理解就可以去是实现一颗所谓的

4、easyUI-七种布局(layout)

1.为网页创建边框布局 边框布局(border layout)提供五个区域:east.west.north.south.center.以下是一些通常用法: north 区域可以用来显示网站的标语. south 区域可以用来显示版权以及一些说明. west 区域可以用来显示导航菜单. east 区域可以用来显示一些推广的项目. center 区域可以用来显示主要的内容. <html> <head> <meta http-equiv="Content-Type"

MVC+Bootstrap 企业通用框架搭建--左侧导航菜单的实现--导航菜单的增删改查(3)

补充上面的点击菜单表信息: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace SqlServer.Entity{ public class Menu_Operation { private int _operation_id; public int Operation_id { get { return

布局 - 折叠面板

在本教程中,您将会学习到关于 easyui 折叠面板(Accordion)的知识. 折叠面板(Accordion)包含一系列的面板(panel). 所有面板(panel)的头部(header)都是可见的,但是一次仅仅显示一个面板(panel)的 body 内容. 当用户点击面板(panel)的头部(header)时,该面板(panel)的 body 内容将可见,同时其他面板(panel)的 body 内容将隐藏不可见. <div class="easyui-accordion"

bootstrap——free bootstrap admin dashboard templates

Gentellela Gentelella a responsive, flat and full featured admin template build. This template is build on Bootstrap 3 framework using modern techniques like HTML5 and CSS3 to be used for backend solutions of any size. This is by far the most advance

easyUI的基础布局easyui-accordion

---恢复内容开始--- <html> <head> <meta charset="UTF-8"> <title>树状图</title> <link rel="stylesheet" href="easy/themes/default/easyui.css" /> <link rel="stylesheet" href="easy/them