예제 #1
0
  public PopupMenuButton(
      Application app,
      Object[] data,
      Integer rows,
      Integer columns,
      Dimension iconSize,
      Integer mode,
      final boolean hasTable,
      boolean hasSlider) {
    super();
    this.app = app;
    this.hasTable = hasTable;
    this.mode = mode;
    this.iconSize = iconSize;
    this.thisButton = this;

    this.setFocusable(false);

    // create the popup
    myPopup = new JPopupMenu();
    myPopup.setFocusable(false);
    myPopup.setBackground(Color.WHITE);
    myPopup.setBorder(
        BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(Color.GRAY),
            BorderFactory.createEmptyBorder(3, 3, 3, 3)));

    // add a mouse listener to our button that triggers the popup
    addMouseListener(
        new MouseAdapter() {

          @Override
          public void mouseEntered(MouseEvent e) {
            popupIsVisible = myPopup.isShowing();
          }

          @Override
          public void mousePressed(MouseEvent e) {

            if (!thisButton.isEnabled()) return;
            if (popupIsVisible == true && !myPopup.isVisible()) {
              popupIsVisible = false;
              return;
            }

            if (prepareToShowPopup() == false) return;

            Point locButton = getLocation();

            // trigger popup
            // default: trigger only when the mouse is over the right side of the button
            // if isStandardButton: pressing anywhere triggers the popup
            if (isStandardButton || e.getX() >= getWidth() - 16 && e.getX() <= getWidth()) {
              if (hasTable) myTable.updateFonts();
              if (isDownwardPopup)
                // popup appears below the button
                myPopup.show(getParent(), locButton.x, locButton.y + getHeight());
              else
                // popup appears above the button
                myPopup.show(
                    getParent(),
                    locButton.x - myPopup.getPreferredSize().width + thisButton.getWidth(),
                    locButton.y - myPopup.getPreferredSize().height - 2);
            }

            popupIsVisible = myPopup.isShowing();
          }
        });

    // place text to the left of drop down icon
    this.setHorizontalTextPosition(SwingConstants.LEFT);
    this.setHorizontalAlignment(SwingConstants.LEFT);

    // create selection table
    if (hasTable) {
      this.data = data;

      myTable = new SelectionTable(app, data, rows, columns, iconSize, mode);
      setSelectedIndex(0);

      // add a mouse listener to handle table selection
      myTable.addMouseListener(
          new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
              handlePopupActionEvent();
            }
          });

      /*
      // if displaying text only, then adjust the width
      if(mode == SelectionTable.MODE_TEXT){
      	 Dimension d = this.getPreferredSize();
      	 d.width = myTable.getColumnWidth();
      	 setMinimumSize(d);
      	 setMaximumSize(d);
       }
       */

      myTable.setBackground(myPopup.getBackground());
      myPopup.add(myTable);
    }

    // create slider
    if (hasSlider) getMySlider();

    isIniting = false;

    if (mode == SelectionTable.MODE_TEXT && iconSize.width == -1) {
      iconSize.width = myTable.getColumnWidth() - 4;
      iconSize.height = myTable.getRowHeight() - 4;
    }
  }