Search in sources :

Example 1 with Popup

use of javax.swing.Popup in project jna by java-native-access.

the class BalloonManagerDemo method main.

public static void main(String[] args) {
    try {
        System.setProperty("sun.java2d.noddraw", "true");
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
    }
    JFrame f = new JFrame("Balloon Test");
    final String BALLOON_TEXT = "<html><center>" + "This is some sample balloon text<br>" + "which has been formatted with html.<br>" + "Click to dismiss.</center></html>";
    final JLabel content = new JLabel(BALLOON_TEXT);
    content.setIconTextGap(10);
    content.setBorder(new EmptyBorder(0, 8, 0, 8));
    content.setSize(content.getPreferredSize());
    content.setIcon(new InfoIcon());
    JLabel label = new JLabel("Click anywhere for more information");
    label.setHorizontalAlignment(SwingConstants.CENTER);
    label.addMouseListener(new MouseAdapter() {

        private MouseListener listener = new MouseAdapter() {

            public void mousePressed(MouseEvent e) {
                hidePopup(e);
            }
        };

        private Popup popup;

        private void hidePopup(MouseEvent e) {
            e.getComponent().removeMouseListener(listener);
            if (popup != null)
                popup.hide();
        }

        public void mousePressed(MouseEvent e) {
            hidePopup(e);
            popup = BalloonManager.getBalloon(e.getComponent(), content, e.getX(), e.getY());
            popup.show();
            content.getParent().addMouseListener(listener);
        }
    });
    f.getContentPane().add(label);
    f.pack();
    f.setSize(new Dimension(300, 300));
    f.setLocation(100, 100);
    try {
        // Force a load of JNA
        WindowUtils.setWindowMask(f, WindowUtils.MASK_NONE);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } catch (UnsatisfiedLinkError e) {
        e.printStackTrace();
        String msg = e.getMessage() + "\nError loading the JNA library";
        JTextArea area = new JTextArea(msg);
        area.setOpaque(false);
        area.setFont(UIManager.getFont("Label.font"));
        area.setEditable(false);
        area.setColumns(80);
        area.setRows(8);
        area.setWrapStyleWord(true);
        area.setLineWrap(true);
        JOptionPane.showMessageDialog(null, new JScrollPane(area), "Library Load Error: " + System.getProperty("os.name") + "/" + System.getProperty("os.arch"), JOptionPane.ERROR_MESSAGE);
        System.exit(1);
    }
}
Also used : JScrollPane(javax.swing.JScrollPane) MouseEvent(java.awt.event.MouseEvent) JTextArea(javax.swing.JTextArea) MouseAdapter(java.awt.event.MouseAdapter) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) MouseListener(java.awt.event.MouseListener) JFrame(javax.swing.JFrame) Popup(javax.swing.Popup) EmptyBorder(javax.swing.border.EmptyBorder)

Example 2 with Popup

use of javax.swing.Popup in project jna by java-native-access.

the class BalloonTipManager method getBalloonTip.

/**
   * Returns the popup window of the balloon tip.
   * @param owner the owner component for the balloon tip
   * @param content the text string to display in the balloon tip
   * @param x the x coordinate for the origin for the balloon tip in relation
   * to the owner component
   * @param y the y coordinate for the origin for the balloon tip in relation
   * to the owner component
   * @param position the position for the balloon; either above or below the
     * owner component
   * @param duration the duration in milliseconds to display balloon tip
   * @param bordercolor the background color for the balloon tip
   * @param backgroundcolor the border color for the balloon tip
   * @param textcolor the text color for the balloon tip
   * @return the popup window of the balloon tip
   */
public static Popup getBalloonTip(final Component owner, final String content, int x, int y, final Integer duration, final Color bordercolor, final Color backgroundcolor, final Color textcolor) {
    final Point origin = owner == null ? new Point(0, 0) : owner.getLocationOnScreen();
    final Window parent = owner != null ? SwingUtilities.getWindowAncestor(owner) : null;
    final String text = content != null ? content : "";
    final Integer timerDuration = duration != null ? duration : 10000;
    origin.translate(x, y);
    vpos = VPOS_BELOW;
    hpos = HPOS_LEFT;
    return new Popup() {

        private BalloonTip bt = null;

        final ComponentEar componentEar = new ComponentEar();

        final MouseEar mouseEar = new MouseEar();

        final FocusEar focusEar = new FocusEar();

        /*
       * (non-Javadoc)
       * @see javax.swing.Popup#show()
       */
        public void show() {
            hidePopupTimer = new Timer(timerDuration, new TimerAction());
            bt = new BalloonTip(parent, text, origin, bordercolor, backgroundcolor, textcolor);
            bt.pack();
            Point pt = new Point(origin);
            pt.translate(10, owner.getHeight());
            bt.setLocation(getAdjustedOrigin(pt));
            bt.setWindowMask();
            bt.setVisible(true);
            owner.addFocusListener(focusEar);
            owner.addMouseListener(mouseEar);
            parent.addMouseListener(mouseEar);
            parent.addComponentListener(componentEar);
            hidePopupTimer.start();
            isShowing = true;
        }

        /*
       * (non-Javadoc)
       * @see javax.swing.Popup#hide()
       */
        public void hide() {
            if (bt != null) {
                isShowing = false;
                hidePopupTimer.stop();
                parent.removeComponentListener(componentEar);
                parent.removeMouseListener(mouseEar);
                owner.removeMouseListener(mouseEar);
                owner.removeFocusListener(focusEar);
                bt.setVisible(false);
                bt.dispose();
            }
        }

        /*
       * Adjust the location of the balloon popup so that is drawn completely
       * on the screen and specify the orientation.
       */
        private Point getAdjustedOrigin(Point pt) {
            Point ret = new Point(pt.x, pt.y);
            GraphicsConfiguration gc = owner.getGraphicsConfiguration();
            Rectangle sBounds = gc.getBounds();
            Insets sInsets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
            sBounds.x += sInsets.left;
            sBounds.y += sInsets.top;
            sBounds.width -= (sInsets.left + sInsets.right);
            sBounds.height -= (sInsets.top + sInsets.bottom);
            if (ret.x < sBounds.x) {
                ret.x = sBounds.x;
            } else if (ret.x - sBounds.x + bt.getWidth() > sBounds.width) {
                ret.x = owner.getLocationOnScreen().x - bt.getWidth() + 43;
            }
            if (ret.x >= pt.x) {
                hpos = HPOS_LEFT;
            } else {
                hpos = HPOS_RIGHT;
            }
            if (ret.y < sBounds.y) {
                ret.y = sBounds.y;
            } else if (ret.y - sBounds.y + bt.getHeight() > sBounds.height) {
                ret.y = owner.getLocationOnScreen().y - bt.getHeight();
            }
            if (ret.y >= pt.y) {
                vpos = VPOS_BELOW;
            } else {
                vpos = VPOS_ABOVE;
            }
            return ret;
        }

        /*
       * This class handles actions from the balloon tip timer.
       */
        @SuppressWarnings("serial")
        final class TimerAction extends AbstractAction {

            /*
         * (non-Javadoc)
         * @see java.awt.event.ActionListener#actionPerformed(
         * java.awt.event.ActionEvent)
         */
            public void actionPerformed(ActionEvent e) {
                hide();
            }
        }

        /*
       * This class handles events spawned from moving the component.
       */
        final class ComponentEar extends ComponentAdapter {

            /*
         * (non-Javadoc)
         * @see java.awt.event.ComponentAdapter#componentMoved(
         * java.awt.event.ComponentEvent)
         */
            public void componentMoved(ComponentEvent e) {
                hide();
            }
        }

        /*
       * This class handles events spawned when a mouse button is pressed.
       */
        final class MouseEar extends MouseAdapter {

            /*
         * (non-Javadoc)
         * @see java.awt.event.MouseAdapter#mousePressed(
         * java.awt.event.MouseEvent)
         */
            public void mousePressed(MouseEvent e) {
                hide();
            }
        }

        /*
       * This class handles events spawned when the component loses focus.
       */
        final class FocusEar extends FocusAdapter {

            /*
         * (non-Javadoc)
         * @see java.awt.event.FocusAdapter#focusLost(
         * java.awt.event.FocusEvent)
         */
            public void focusLost(FocusEvent e) {
                hide();
            }
        }
    };
}
Also used : Window(java.awt.Window) JWindow(javax.swing.JWindow) Insets(java.awt.Insets) MouseEvent(java.awt.event.MouseEvent) ActionEvent(java.awt.event.ActionEvent) Rectangle(java.awt.Rectangle) Point(java.awt.Point) FocusEvent(java.awt.event.FocusEvent) GraphicsConfiguration(java.awt.GraphicsConfiguration) Timer(javax.swing.Timer) Popup(javax.swing.Popup) ComponentEvent(java.awt.event.ComponentEvent)

Example 3 with Popup

use of javax.swing.Popup in project jna by java-native-access.

the class BalloonManager method getBalloon.

/** Get a balloon pointing to the given location.  The coordinates are 
     * relative to <code>owner</code>, which if null, indicates the coordinates
     * are absolute.
     */
public static Popup getBalloon(final Component owner, final Component content, int x, int y) {
    // Simulate PopupFactory, ensuring we get a heavyweight "popup"
    final Point origin = owner == null ? new Point(0, 0) : (owner.isShowing() ? owner.getLocationOnScreen() : owner.getLocation());
    final Window parent = owner != null ? SwingUtilities.getWindowAncestor(owner) : null;
    origin.translate(x, y);
    return new Popup() {

        private BubbleWindow w;

        public void show() {
            w = new BubbleWindow(parent, content);
            w.pack();
            Point where = new Point(origin);
            where.translate(-w.getWidth() / 3, -w.getHeight());
            w.setAnchorLocation(where.x, where.y);
            w.setVisible(true);
        }

        public void hide() {
            w.setVisible(false);
            w.dispose();
        }
    };
}
Also used : Window(java.awt.Window) JWindow(javax.swing.JWindow) Popup(javax.swing.Popup) Point(java.awt.Point)

Aggregations

Popup (javax.swing.Popup)3 Point (java.awt.Point)2 Window (java.awt.Window)2 MouseEvent (java.awt.event.MouseEvent)2 JWindow (javax.swing.JWindow)2 Dimension (java.awt.Dimension)1 GraphicsConfiguration (java.awt.GraphicsConfiguration)1 Insets (java.awt.Insets)1 Rectangle (java.awt.Rectangle)1 ActionEvent (java.awt.event.ActionEvent)1 ComponentEvent (java.awt.event.ComponentEvent)1 FocusEvent (java.awt.event.FocusEvent)1 MouseAdapter (java.awt.event.MouseAdapter)1 MouseListener (java.awt.event.MouseListener)1 JFrame (javax.swing.JFrame)1 JLabel (javax.swing.JLabel)1 JScrollPane (javax.swing.JScrollPane)1 JTextArea (javax.swing.JTextArea)1 Timer (javax.swing.Timer)1 EmptyBorder (javax.swing.border.EmptyBorder)1