Esempio n. 1
0
  public TrayIconPopup(TrayExtension trayExtension) {
    super();
    this.extension = trayExtension;
    // resizecomps = new ArrayList<AbstractButton>();
    setVisible(false);
    setLayout(new MigLayout("ins 0", "[grow,fill]", "[grow,fill]"));
    addMouseListener(this);
    this.setUndecorated(true);
    // initEntryPanel();
    // initQuickConfigPanel();
    // initBottomPanel();
    // initExitPanel();
    JPanel content = new JPanel(new MigLayout("ins 5, wrap 1", "[fill]", "[]5[]"));
    add(content);
    JButton header;
    content.add(
        header = new JButton("<html><b>" + JDUtilities.getJDTitle(0) + "</b></html>"),
        "align center");
    header.setBorderPainted(false);
    header.addActionListener(
        new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent e) {
            JDGui.getInstance().setWindowToTray(false);
            dispose();
          }
        });
    AbstractButton ab;
    // System.out.println(this.getColConstraints(list.length));
    MenuItemData last = null;
    for (final MenuItemData menudata : MenuManagerTrayIcon.getInstance().getMenuData().getItems()) {
      AbstractButton bt = null;
      AppAction action;
      try {
        if (!menudata.isVisible()) {
          continue;
        }
        if (menudata instanceof SeparatorData) {
          if (last != null && last instanceof SeparatorData) {
            // no separator dupes
            continue;
          }
          content.add(new JSeparator(SwingConstants.HORIZONTAL), "growx,spanx");
          last = menudata;
          continue;
        }
        if (menudata._getValidateException() != null) {
          continue;
        }

        if (menudata.getType()
            == org.jdownloader.controlling.contextmenu.MenuItemData.Type.CONTAINER) {

          bt =
              new JToggleButton() {

                protected void paintComponent(Graphics g) {

                  super.paintComponent(g);

                  Graphics2D g2 = (Graphics2D) g;
                  g2.setRenderingHint(
                      RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                  g2.setRenderingHint(
                      RenderingHints.KEY_INTERPOLATION,
                      RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                  g2.setRenderingHint(
                      RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                  g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
                  g2.setColor(Color.BLACK);
                  g2.fillPolygon(
                      new int[] {getWidth() - 5, getWidth() - 5 - 6, getWidth() - 5 - 6},
                      new int[] {getHeight() / 2, getHeight() / 2 - 4, getHeight() / 2 + 4},
                      3);
                }
              };

          bt.setText(menudata.getName());
          bt.setOpaque(false);
          bt.setContentAreaFilled(false);
          bt.setBorderPainted(false);
          bt.addActionListener(
              new ActionListener() {
                private ExtPopupMenu root = null;

                public void actionPerformed(ActionEvent e) {
                  hideThreadrunning = false;
                  if (root != null && root.isShowing()) {
                    return;
                  }
                  root = new ExtPopupMenu();

                  new MenuBuilder(
                      MenuManagerMainToolbar.getInstance(), root, (MenuContainer) menudata) {
                    protected void addAction(
                        final JComponent root, final MenuItemData inst, int index, int size)
                        throws InstantiationException, IllegalAccessException,
                            InvocationTargetException, ClassNotFoundException,
                            NoSuchMethodException, ExtensionNotLoadedException {
                      final JComponent ret = inst.addTo(root);
                      if (ret instanceof AbstractButton) {
                        ((AbstractButton) ret)
                            .addActionListener(
                                new ActionListener() {
                                  public void actionPerformed(ActionEvent evt) {
                                    ((AbstractButton) ret).getAction().actionPerformed(evt);
                                    TrayIconPopup.this.dispose();
                                  }
                                });
                      }
                    }
                  }.run();
                  Object src = e.getSource();
                  if (e.getSource() instanceof Component) {
                    Component button = (Component) e.getSource();
                    Dimension prefSize = root.getPreferredSize();
                    int[] insets = LAFOptions.getInstance().getPopupBorderInsets();
                    root.show(button, button.getWidth(), -insets[0]);
                  }
                }
              });
          bt.setIcon(MenuItemData.getIcon(menudata.getIconKey(), ICON_SIZE));
          bt.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
          bt.setFocusPainted(false);
          bt.setHorizontalAlignment(JButton.LEFT);
          bt.setIconTextGap(5);
          bt.addMouseListener(new HoverEffect(bt));
          final AbstractButton finalBt = bt;
          bt.addMouseListener(
              new MouseListener() {

                private Timer timer;

                @Override
                public void mouseReleased(MouseEvent e) {
                  if (timer != null) {
                    timer.stop();
                    timer = null;
                  }
                }

                @Override
                public void mousePressed(MouseEvent e) {
                  if (timer != null) {
                    timer.stop();
                    timer = null;
                  }
                }

                @Override
                public void mouseExited(MouseEvent e) {
                  if (timer != null) {
                    timer.stop();
                    timer = null;
                  }
                }

                @Override
                public void mouseEntered(MouseEvent e) {

                  timer =
                      new Timer(
                          500,
                          new ActionListener() {

                            @Override
                            public void actionPerformed(ActionEvent e) {
                              finalBt.doClick();
                            }
                          });
                  timer.setRepeats(false);
                  timer.start();
                }

                @Override
                public void mouseClicked(MouseEvent e) {
                  if (timer != null) {
                    timer.stop();
                    timer = null;
                  }
                }
              });
          content.add(bt);

          continue;
        } else if (menudata instanceof MenuLink) {
          final JComponent item = menudata.createItem();
          if (StringUtils.isNotEmpty(menudata.getIconKey())) {
            if (item instanceof AbstractButton) {
              ((AbstractButton) item)
                  .setIcon(MenuItemData.getIcon(menudata.getIconKey(), ICON_SIZE));
            }
          }
          content.add(item, "");

        } else if (menudata.getActionData() != null) {

          action = menudata.createAction();
          if (!action.isVisible()) {
            continue;
          }
          if (StringUtils.isNotEmpty(menudata.getShortcut())
              && KeyStroke.getKeyStroke(menudata.getShortcut()) != null) {
            action.setAccelerator(KeyStroke.getKeyStroke(menudata.getShortcut()));
          } else if (MenuItemData.isEmptyValue(menudata.getShortcut())) {
            action.setAccelerator(null);
          }
          content.add(getMenuEntry(action));
          last = menudata;
        }

      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    // content.add(new JSeparator(), "growx, spanx");
    // content.add(entryPanel);
    // content.add(new JSeparator(), "growx, spanx");
    // content.add(quickConfigPanel);
    // content.add(new JSeparator(), "growx, spanx");
    // content.add(bottomPanel, "pushx,growx");
    // content.add(new JSeparator(), "growx, spanx");
    // content.add(exitPanel);
    //

    content.setBorder(BorderFactory.createLineBorder(content.getBackground().darker()));
    // Dimension size = new Dimension(getPreferredSize().width,
    // resizecomps.get(0).getPreferredSize().height);
    // for (AbstractButton c : resizecomps) {
    // c.setPreferredSize(size);
    // c.setMinimumSize(size);
    // c.setMaximumSize(size);
    // }
    setAlwaysOnTop(true);
    pack();
    hideThread =
        new Thread() {
          /*
           * this thread handles closing of popup because enter/exit/move events are too slow and can miss the exitevent
           */
          public void run() {
            while (true && hideThreadrunning) {
              try {
                sleep(500);
              } catch (InterruptedException e) {
              }
              if (enteredPopup && hideThreadrunning) {
                PointerInfo mouse = MouseInfo.getPointerInfo();
                Point current = TrayIconPopup.this.getLocation();
                if (mouse.getLocation().x < current.x
                    || mouse.getLocation().x > current.x + TrayIconPopup.this.getSize().width) {
                  dispose();
                  break;
                } else if (mouse.getLocation().y < current.y
                    || mouse.getLocation().y > current.y + TrayIconPopup.this.getSize().height) {
                  dispose();
                  break;
                }
              }
            }
          }
        };
    hideThreadrunning = true;
    hideThread.start();
  }
 public String getIconKey() {
   if (StringUtils.isEmpty(super.getIconKey())) return IconKey.ICON_QUESTION;
   if (MenuItemData.isEmptyValue(super.getIconKey())) return IconKey.ICON_QUESTION;
   return iconKey;
 }