Ticket #15414: v2-0001-add-PopupMenuButton-a-button-for-triggering-the-appe.patch

File v2-0001-add-PopupMenuButton-a-button-for-triggering-the-appe.patch, 4.4 KB (added by ris, 8 years ago)
  • new file src/org/openstreetmap/josm/gui/widgets/PopupMenuButton.java

    From 5845f37e65100e0e8de538d0dfd8dde3e7615abb Mon Sep 17 00:00:00 2001
    From: Robert Scott <code@humanleg.org.uk>
    Date: Sun, 8 Oct 2017 11:02:15 +0100
    Subject: [PATCH 1/2] add PopupMenuButton: a button for triggering the
     appearance of a JPopupMenu when activated
    
    ---
     .../josm/gui/widgets/PopupMenuButton.java          | 152 +++++++++++++++++++++
     1 file changed, 152 insertions(+)
     create mode 100644 src/org/openstreetmap/josm/gui/widgets/PopupMenuButton.java
    
    diff --git a/src/org/openstreetmap/josm/gui/widgets/PopupMenuButton.java b/src/org/openstreetmap/josm/gui/widgets/PopupMenuButton.java
    new file mode 100644
    index 0000000..63e0a7c
    - +  
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.gui.widgets;
     3
     4import java.awt.Color;
     5import java.awt.Dimension;
     6import java.awt.Graphics;
     7import java.awt.Graphics2D;
     8import java.awt.event.ActionListener;
     9import java.awt.event.ActionEvent;
     10import java.awt.geom.Path2D;
     11
     12import javax.swing.Action;
     13import javax.swing.Icon;
     14import javax.swing.JButton;
     15import javax.swing.JPopupMenu;
     16
     17
     18/**
     19 * Button triggering the appearance of a JPopupMenu when activated
     20 */
     21public class PopupMenuButton extends JButton implements ActionListener {
     22    private JPopupMenu menu;
     23
     24    /**
     25     * @see JButton#JButton()
     26     */
     27    public PopupMenuButton() {
     28        super();
     29        this.initialize();
     30    }
     31
     32    /**
     33     * @see JButton#JButton(Action)
     34     */
     35    public PopupMenuButton(Action a) {
     36        super(a);
     37        this.initialize();
     38    }
     39
     40    /**
     41     * @see JButton#JButton(Icon)
     42     */
     43    public PopupMenuButton(Icon i) {
     44        super(i);
     45        this.initialize();
     46    }
     47
     48    /**
     49     * @see JButton#JButton(String)
     50     */
     51    public PopupMenuButton(String t) {
     52        super(t);
     53        this.initialize();
     54    }
     55
     56    /**
     57     * @see JButton#JButton(String, Icon)
     58     */
     59    public PopupMenuButton(String t, Icon i) {
     60        super(t, i);
     61        this.initialize();
     62    }
     63
     64    /**
     65     * Pass-through to {@link JButton#JButton()} allowing associated popup menu to be set
     66     */
     67    public PopupMenuButton(JPopupMenu m) {
     68        super();
     69        this.initialize(m);
     70    }
     71
     72    /**
     73     * Pass-through to {@link JButton#JButton(Action)} allowing associated popup menu to be set
     74     */
     75    public PopupMenuButton(Action a, JPopupMenu m) {
     76        super(a);
     77        this.initialize(m);
     78    }
     79
     80    /**
     81     * Pass-through to {@link JButton#JButton(Icon)} allowing associated popup menu to be set
     82     */
     83    public PopupMenuButton(Icon i, JPopupMenu m) {
     84        super(i);
     85        this.initialize(m);
     86    }
     87
     88    /**
     89     * Pass-through to {@link JButton#JButton(String)} allowing associated popup menu to be set
     90     */
     91    public PopupMenuButton(String t, JPopupMenu m) {
     92        super(t);
     93        this.initialize(m);
     94    }
     95
     96    /**
     97     * Pass-through to {@link JButton#JButton(String, Icon)} allowing associated popup menu to be set
     98     */
     99    public PopupMenuButton(String t, Icon i, JPopupMenu m) {
     100        super(t, i);
     101        this.initialize(m);
     102    }
     103
     104    private void initialize(JPopupMenu m) {
     105        this.menu = m;
     106        this.initialize();
     107    }
     108
     109    private void initialize() {
     110        this.addActionListener(this);
     111    }
     112
     113    /**
     114     * Get the popup menu associated with this button
     115     */
     116    public JPopupMenu getPopupMenu() {
     117        return this.menu;
     118    }
     119
     120    /**
     121     * Set the popup menu associated with this button
     122     * @param m Menu to show when button is triggered
     123     */
     124    public void setPopupMenu(JPopupMenu m) {
     125        this.menu = m;
     126    }
     127
     128    @Override
     129    public void actionPerformed(ActionEvent e) {
     130        this.menu.show(this, 0, this.getHeight());
     131    }
     132
     133    @Override
     134    public void paint(Graphics g) {
     135        super.paint(g);
     136        Graphics2D g2d = (Graphics2D) g;
     137
     138        //
     139        // paint small arrow in bottom right corner
     140        //
     141        Dimension size = this.getSize();
     142
     143        Path2D p = new Path2D.Float();
     144        p.moveTo(size.getWidth() - 7, size.getHeight() - 4);
     145        p.lineTo(size.getWidth() - 1, size.getHeight() - 4);
     146        p.lineTo(size.getWidth() - 4, size.getHeight() - 1);
     147        p.closePath();
     148
     149        g2d.setPaint(Color.BLACK);
     150        g2d.fill(p);
     151    }
     152}