Drop-down a chevron menu containing hidden tool items : CoolBar « SWT-JFace-Eclipse « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » SWT-JFace-Eclipse » CoolBar屏幕截图 
Drop-down a chevron menu containing hidden tool items
Drop-down a chevron menu containing hidden tool items


/*
 * CoolBar example snippet: drop-down a chevron menu containing hidden tool items
 *
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class Snippet140 {
  static Display display;

  static Shell shell;

  static CoolBar coolBar;

  static Menu chevronMenu = null;

  public static void main(String[] args) {
    display = new Display();
    shell = new Shell(display);
    shell.setLayout(new GridLayout());
    coolBar = new CoolBar(shell, SWT.FLAT | SWT.BORDER);
    coolBar.setLayoutData(new GridData(GridData.FILL_BOTH));
    ToolBar toolBar = new ToolBar(coolBar, SWT.FLAT | SWT.WRAP);
    int minWidth = 0;
    for (int j = 0; j < 5; j++) {
      int width = 0;
      ToolItem item = new ToolItem(toolBar, SWT.PUSH);
      item.setText("B" + j);
      width = item.getWidth();
      /* find the width of the widest tool */
      if (width > minWidth)
        minWidth = width;
    }
    CoolItem coolItem = new CoolItem(coolBar, SWT.DROP_DOWN);
    coolItem.setControl(toolBar);
    Point size = toolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    Point coolSize = coolItem.computeSize(size.x, size.y);
    coolItem.setMinimumSize(minWidth, coolSize.y);
    coolItem.setPreferredSize(coolSize);
    coolItem.setSize(coolSize);
    coolItem.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        if (event.detail == SWT.ARROW) {
          CoolItem item = (CoolItemevent.widget;
          Rectangle itemBounds = item.getBounds();
          Point pt = coolBar.toDisplay(new Point(itemBounds.x,
              itemBounds.y));
          itemBounds.x = pt.x;
          itemBounds.y = pt.y;
          ToolBar bar = (ToolBaritem.getControl();
          ToolItem[] tools = bar.getItems();

          int i = 0;
          while (i < tools.length) {
            Rectangle toolBounds = tools[i].getBounds();
            pt = bar
                .toDisplay(new Point(toolBounds.x, toolBounds.y));
            toolBounds.x = pt.x;
            toolBounds.y = pt.y;

            /*
             * Figure out the visible portion of the tool by looking
             * at the intersection of the tool bounds with the cool
             * item bounds.
             */
            Rectangle intersection = itemBounds
                .intersection(toolBounds);

            /*
             * If the tool is not completely within the cool item
             * bounds, then it is partially hidden, and all
             * remaining tools are completely hidden.
             */
            if (!intersection.equals(toolBounds))
              break;
            i++;
          }

          /*
           * Create a menu with items for each of the completely
           * hidden buttons.
           */
          if (chevronMenu != null)
            chevronMenu.dispose();
          chevronMenu = new Menu(coolBar);
          for (int j = i; j < tools.length; j++) {
            MenuItem menuItem = new MenuItem(chevronMenu, SWT.PUSH);
            menuItem.setText(tools[j].getText());
          }

          /*
           * Drop down the menu below the chevron, with the left edges
           * aligned.
           */
          pt = coolBar.toDisplay(new Point(event.x, event.y));
          chevronMenu.setLocation(pt.x, pt.y);
          chevronMenu.setVisible(true);
        }
      }
    });

    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}


           
       
Related examples in the same category
1. SWT CoolBar试验示范SWT CoolBar试验示范
2. CoolBar实例CoolBar实例
3. CoolBarTestCoolBarTest
4. Coolbar示例2
5. Coolbar Example
6. SWT演示工具栏SWT演示工具栏
7. SWY CoolBarClassSWY CoolBarClass
8. 创建一个coolbar ( relayout时调整)创建一个coolbar ( relayout时调整)
9. CoolBar例如片段CoolBar例如片段
10. Control example snippet: print mouse state and button (down, move, up)Control example snippet: print mouse state and button (down, move, up)
11. 控制片段:打印键盘码,状态和字符控制片段:打印键盘码,状态和字符
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.