/**
   * Attaches this tooltip to a certain component
   *
   * @param component See above.
   * @param xOffset The x axis offset where the tooltip is to be shown
   * @param yOffset The y axis offset where the tooltiop is to be shown
   */
  public void attach(final JComponent component, final int xOffset, final int yOffset) {
    this.component = component;

    component.addMouseListener(
        new MouseAdapter() {
          public void mouseEntered(MouseEvent e) {
            location = e.getPoint();
            location.translate(xOffset, yOffset);
            showTimer.restart();
            hideTimer.stop();
          }

          public void mouseExited(MouseEvent e) {
            showTimer.stop();
            hideTimer.restart();
          }
        });

    popupMenu.addMouseListener(
        new MouseAdapter() {
          public void mouseEntered(MouseEvent e) {
            hideTimer.stop();
          }

          public void mouseExited(MouseEvent e) {
            Rectangle componentBounds =
                new Rectangle(
                    component.getLocationOnScreen(),
                    new Dimension(component.getWidth(), component.getHeight()));
            if (!componentBounds.contains(e.getLocationOnScreen())) {
              hideTimer.restart();
            }
          }
        });
  }
示例#2
0
  /** initializes the popup menus */
  private void initPopupMenus() {
    // header popup
    m_PopupHeader = new JPopupMenu();
    m_PopupHeader.addMouseListener(this);
    m_PopupHeader.add(menuItemMean);
    if (!isReadOnly()) {
      m_PopupHeader.addSeparator();
      m_PopupHeader.add(menuItemSetAllValues);
      m_PopupHeader.add(menuItemSetMissingValues);
      m_PopupHeader.add(menuItemReplaceValues);
      m_PopupHeader.addSeparator();
      m_PopupHeader.add(menuItemRenameAttribute);
      m_PopupHeader.add(menuItemAttributeAsClass);
      m_PopupHeader.add(menuItemDeleteAttribute);
      m_PopupHeader.add(menuItemDeleteAttributes);
      m_PopupHeader.add(menuItemSortInstances);
    }
    m_PopupHeader.addSeparator();
    m_PopupHeader.add(menuItemOptimalColWidth);
    m_PopupHeader.add(menuItemOptimalColWidths);

    // row popup
    m_PopupRows = new JPopupMenu();
    m_PopupRows.addMouseListener(this);
    if (!isReadOnly()) {
      m_PopupRows.add(menuItemUndo);
      m_PopupRows.addSeparator();
    }
    m_PopupRows.add(menuItemCopy);
    m_PopupRows.addSeparator();
    m_PopupRows.add(menuItemSearch);
    m_PopupRows.add(menuItemClearSearch);
    if (!isReadOnly()) {
      m_PopupRows.addSeparator();
      m_PopupRows.add(menuItemDeleteSelectedInstance);
      m_PopupRows.add(menuItemDeleteAllSelectedInstances);
    }
  }
  public void initGUI() {
    try {
      {
        this.addMouseListener(
            new MouseAdapter() {
              public void mouseExited(MouseEvent evt) {
                rootMouseExited(evt);
              }

              public void mouseEntered(MouseEvent evt) {
                rootMouseEntered(evt);
              }

              public void mouseClicked(MouseEvent evt) {
                rootMouseClicked(evt);
              }
            });
        //				this.addActionListener(new ActionListener(){
        //					public void actionPerformed(ActionEvent evt){
        //						System.out.println(""+evt.toString());
        //					}
        //				});
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    this.popupMenu = this.getPopupMenu();
    popupMenu.addMouseListener(
        new MouseAdapter() {
          public void mouseExited(MouseEvent evt) {
            popupMenuExited(evt);
          }

          public void mouseEntered(MouseEvent evt) {
            popupMenuEntered(evt);
          }
        });
    this.menuEnterCount = 0;
    //		this.mainMenu=(ControlledMenu)this.getParent();
  }
  private void setupPopup() {
    // Menu if something isnt selected
    unselectedRightClickMenu = new JPopupMenu();
    unselectedRightClickMenu.addMouseListener(frameListener);

    JMenu mnAdd = new JMenu("Add");
    unselectedRightClickMenu.add(mnAdd);

    JMenu mnEdit = new JMenu("Edit");
    unselectedRightClickMenu.add(mnEdit);

    JMenuItem mntmPaste1 = new JMenuItem("Paste");
    mnEdit.add(mntmPaste1);
    mntmPaste1.addActionListener(frameListener);

    JMenuItem mntmRectangle = new JMenuItem("Rectangle");
    mnAdd.add(mntmRectangle);
    mntmRectangle.addActionListener(frameListener);

    JMenuItem mntmElipse = new JMenuItem("Ellipse");
    mnAdd.add(mntmElipse);
    mntmElipse.addActionListener(frameListener);

    JMenuItem mntmText = new JMenuItem("Text");
    mnAdd.add(mntmText);
    mntmText.addActionListener(frameListener);

    JMenuItem mntmLatex = new JMenuItem("Latex");
    mnAdd.add(mntmLatex);
    mntmLatex.addActionListener(frameListener);

    JMenuItem mntmImage = new JMenuItem("Image");
    mnAdd.add(mntmImage);
    mntmImage.addActionListener(frameListener);

    JMenuItem mntmShape = new JMenuItem("Custom...");
    mntmShape.setActionCommand("Custom");
    mnAdd.add(mntmShape);
    mntmShape.addActionListener(frameListener);

    // menu if something is selected

    selectedRightClickMenu = new JPopupMenu();
    selectedRightClickMenu.addMouseListener(frameListener);

    JMenuItem mntmModify = new JMenuItem("Modify");
    selectedRightClickMenu.add(mntmModify);
    mntmModify.addActionListener(frameListener);

    JMenuItem mntmCopy = new JMenuItem("Copy");
    selectedRightClickMenu.add(mntmCopy);
    mntmCopy.addActionListener(frameListener);

    JMenuItem mntmCut = new JMenuItem("Cut");
    selectedRightClickMenu.add(mntmCut);
    mntmCut.addActionListener(frameListener);

    JMenuItem mntmPaste = new JMenuItem("Paste");
    selectedRightClickMenu.add(mntmPaste);
    mntmPaste.addActionListener(frameListener);

    JMenuItem mntmDelete = new JMenuItem("Delete");
    selectedRightClickMenu.add(mntmDelete);
    mntmDelete.addActionListener(frameListener);
  }
  private void trayIconListener(final JPopupMenu popup, TrayIcon icon) {

    Timer notificationTimer = new Timer();
    notificationTimer.schedule(
        new TimerTask() {
          @Override
          public void run() {
            if (visible && !inBound) {
              visible = !visible;
              popup.setVisible(visible);
            }
            inBound = false;
          }
        },
        250,
        1500);

    icon.addMouseMotionListener(
        new MouseMotionAdapter() {
          @Override
          public void mouseMoved(MouseEvent e) {
            inBound = true;
          }
        });

    popup.addMouseMotionListener(
        new MouseMotionAdapter() {
          @Override
          public void mouseMoved(MouseEvent e) {
            inBound = true;
          }
        });

    icon.addMouseListener(
        new MouseAdapter() {

          @Override
          public void mouseReleased(MouseEvent e) {

            if (e.getButton() != MouseEvent.BUTTON1) {
              return;
            }

            Point point = e.getPoint();
            Rectangle bounds = getScreenViewableBounds(getGraphicsDeviceAt(point));
            int x = point.x;
            int y = point.y;
            if (y < bounds.y) {
              y = bounds.y;
            } else if (y > bounds.y + bounds.height) {
              y = bounds.y + bounds.height;
            }
            if (x < bounds.x) {
              x = bounds.x;
            } else if (x > bounds.x + bounds.width) {
              x = bounds.x + bounds.width;
            }

            if (x + popup.getWidth() > bounds.x + bounds.width) {
              x = (bounds.x + bounds.width) - popup.getWidth();
            }
            if (y + popup.getWidth() > bounds.y + bounds.height) {
              y = (bounds.y + bounds.height) - popup.getHeight();
            }

            visible = !visible;

            if (visible) {
              popup.setLocation(x, y);
            }
            popup.setVisible(visible);
          }

          @Override
          public void mouseExited(MouseEvent e) {
            visible = false;
            popup.setVisible(visible);
          }
        });

    popup.addMouseListener(
        new MouseAdapter() {
          @Override
          public void mouseExited(MouseEvent e) {
            if ((e.getX() < popup.getBounds().getMaxX())
                && (e.getX() >= popup.getBounds().getMinX())
                && (e.getY() < popup.getBounds().getMaxY())
                && (e.getY() >= popup.getBounds().getMinY())) {
              return;
            }
            visible = false;
            popup.setVisible(visible);
          }
        });
  }