Changeset 6539 in josm for trunk


Ignore:
Timestamp:
2013-12-26T19:42:23+01:00 (11 years ago)
Author:
jttt
Message:

Convert SizeButton and SourceButton to swing compontents

Location:
trunk/src/org/openstreetmap/josm/gui/bbox
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/bbox/SizeButton.java

    r6380 r6539  
    22package org.openstreetmap.josm.gui.bbox;
    33
     4import java.awt.Dimension;
    45import java.awt.Graphics;
    56import java.awt.Point;
     7import java.awt.event.MouseAdapter;
     8import java.awt.event.MouseEvent;
    69
    710import javax.swing.ImageIcon;
     11import javax.swing.JComponent;
    812
    913import org.openstreetmap.josm.tools.ImageProvider;
     
    1216 * @author Tim Haussmann
    1317 */
    14 public class SizeButton{
     18public class SizeButton extends JComponent {
    1519
    1620    private int x = 0;
     
    2024    private ImageIcon shrinkImage;
    2125    private boolean isEnlarged = false;
     26    private final SlippyMapBBoxChooser slippyMapBBoxChooser;
    2227
    23     public SizeButton(){
     28    public SizeButton(SlippyMapBBoxChooser slippyMapBBoxChooser){
     29        super();
     30        this.slippyMapBBoxChooser = slippyMapBBoxChooser;
    2431        enlargeImage = ImageProvider.get("view-fullscreen.png");
    2532        shrinkImage = ImageProvider.get("view-fullscreen-revert.png");
     33        setPreferredSize(new Dimension(enlargeImage.getIconWidth(), enlargeImage.getIconHeight()));
     34        addMouseListener(mouseListener);
    2635    }
    2736
    28     public void paint(Graphics g) {
     37    private final MouseAdapter mouseListener = new MouseAdapter() {
     38        @Override
     39        public void mouseReleased(MouseEvent e) {
     40            if (e.getButton() == MouseEvent.BUTTON1) {
     41                toggle();
     42                slippyMapBBoxChooser.resizeSlippyMap();
     43            }
     44        }
     45    };
     46
     47
     48    @Override
     49    protected void paintComponent(Graphics g) {
    2950        if(isEnlarged) {
    3051            if(shrinkImage != null)
  • trunk/src/org/openstreetmap/josm/gui/bbox/SlippyMapBBoxChooser.java

    r6378 r6539  
    77import java.awt.Dimension;
    88import java.awt.Graphics;
    9 import java.awt.Graphics2D;
    109import java.awt.Image;
    1110import java.awt.Point;
     
    2120
    2221import javax.swing.JOptionPane;
     22import javax.swing.SpringLayout;
    2323
    2424import org.openstreetmap.gui.jmapviewer.Coordinate;
     
    170170    private OsmTileLoader uncachedLoader;
    171171
    172     private final SizeButton iSizeButton = new SizeButton();
     172    private final SizeButton iSizeButton;
    173173    private final SourceButton iSourceButton;
    174174    private Bounds bbox;
     
    182182     */
    183183    public SlippyMapBBoxChooser() {
     184        SpringLayout springLayout = new SpringLayout();
     185        setLayout(springLayout);
    184186        TMSLayer.setMaxWorkers();
    185187        cachedLoader = TMSLayer.loaderFactory.makeTileLoader(this);
     
    202204        List<TileSource> tileSources = getAllTileSources();
    203205
    204         iSourceButton = new SourceButton(tileSources);
     206        iSourceButton = new SourceButton(this, tileSources);
     207        add(iSourceButton);
     208        springLayout.putConstraint(SpringLayout.EAST, iSourceButton, 0, SpringLayout.EAST, this);
     209        springLayout.putConstraint(SpringLayout.NORTH, iSourceButton, 30, SpringLayout.NORTH, this);
     210
     211        iSizeButton = new SizeButton(this);
     212        add(iSizeButton);
    205213
    206214        String mapStyle = PROP_MAPSTYLE.get();
     
    219227        }
    220228
    221         new SlippyMapControler(this, this, iSizeButton, iSourceButton);
    222     }
    223    
     229        new SlippyMapControler(this, this);
     230    }
     231
    224232    private List<TileSource> getAllTileSources() {
    225233        List<TileSource> tileSources = new ArrayList<TileSource>();
     
    264272                g.drawRect(x_min, y_min, w, h);
    265273            }
    266 
    267             iSizeButton.paint(g);
    268             iSourceButton.paint((Graphics2D)g);
    269274        } catch (Exception e) {
    270275            e.printStackTrace();
     
    394399        repaint();
    395400    }
    396    
     401
    397402    /**
    398403     * Refreshes the tile sources
  • trunk/src/org/openstreetmap/josm/gui/bbox/SlippyMapControler.java

    r6438 r6539  
    5151    private final SlippyMapBBoxChooser iSlippyMapChooser;
    5252
    53     private SizeButton iSizeButton = null;
    54     private SourceButton iSourceButton = null;
    55 
    5653    private boolean isSelecting;
    5754
     
    5956     * Constructs a new {@code SlippyMapControler}.
    6057     */
    61     public SlippyMapControler(SlippyMapBBoxChooser navComp, JPanel contentPane, SizeButton sizeButton, SourceButton sourceButton) {
     58    public SlippyMapControler(SlippyMapBBoxChooser navComp, JPanel contentPane) {
    6259        iSlippyMapChooser = navComp;
    6360        iSlippyMapChooser.addMouseListener(this);
     
    7471            }
    7572        }
    76         iSizeButton = sizeButton;
    77         iSourceButton = sourceButton;
    78 
    7973        isSelecting = false;
    8074
     
    124118    public void mousePressed(MouseEvent e) {
    125119        if (e.getButton() == MouseEvent.BUTTON1) {
    126             if (!iSizeButton.hit(e.getPoint())) {
    127                 iStartSelectionPoint = e.getPoint();
    128                 iEndSelectionPoint = e.getPoint();
    129             }
    130         }
    131 
     120            iStartSelectionPoint = e.getPoint();
     121            iEndSelectionPoint = e.getPoint();
     122        }
    132123    }
    133124
     
    160151
    161152            } else {
    162                 int sourceButton = iSourceButton.hit(e.getPoint());
    163 
    164                 if (iSizeButton.hit(e.getPoint())) {
    165                     iSizeButton.toggle();
    166                     iSlippyMapChooser.resizeSlippyMap();
    167                 } else if (iSlippyMapChooser.handleAttribution(e.getPoint(), true)) {
    168                     /* do nothing, handleAttribution() already did the work */
    169                 } else if (sourceButton == SourceButton.HIDE_OR_SHOW) {
    170                     iSourceButton.toggle();
    171                     iSlippyMapChooser.repaint();
    172                 } else if (sourceButton != 0) {
    173                     iSlippyMapChooser.toggleMapSource(iSourceButton.hitToTileSource(sourceButton));
    174                 }
     153                iSlippyMapChooser.handleAttribution(e.getPoint(), true);
    175154            }
    176155        }
  • trunk/src/org/openstreetmap/josm/gui/bbox/SourceButton.java

    r6364 r6539  
    33
    44import java.awt.Color;
     5import java.awt.Dimension;
    56import java.awt.Font;
    67import java.awt.FontMetrics;
     8import java.awt.Graphics;
    79import java.awt.Graphics2D;
    810import java.awt.Point;
    911import java.awt.RenderingHints;
     12import java.awt.event.MouseAdapter;
     13import java.awt.event.MouseEvent;
     14import java.awt.event.MouseListener;
    1015import java.util.Collection;
    1116
    1217import javax.swing.ImageIcon;
     18import javax.swing.JComponent;
    1319
    1420import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
     
    1622import org.openstreetmap.josm.tools.ImageProvider;
    1723
    18 public class SourceButton {
     24public class SourceButton extends JComponent {
    1925
    20     // Filled in paint, used in hit
    21     private int barX;
    22     private int barY;
    23     private int barWidth;
    24     private int layerHeight;
     26    private final int layerHeight = 20;
     27    private final int leftPadding = 5;
     28    private final int topPadding = 5;
     29    private final int bottomPadding = 5;
     30
    2531
    2632    private TileSource[] sources;
    2733
    28     private ImageIcon enlargeImage;
    29     private ImageIcon shrinkImage;
     34    private final ImageIcon enlargeImage;
     35    private final ImageIcon shrinkImage;
     36    private final Dimension hiddenDimension;
     37
     38    // Calculated after component is added to container
     39    private int barWidth;
     40    private Dimension shownDimension;
     41    private Font font;
    3042
    3143    private boolean isEnlarged = false;
    3244
    3345    private int currentMap;
     46    private final SlippyMapBBoxChooser slippyMapBBoxChooser;
    3447
    35     public static final int HIDE_OR_SHOW = 1;
    36 
    37     public SourceButton(Collection<TileSource> sources) {
     48    public SourceButton(SlippyMapBBoxChooser slippyMapBBoxChooser, Collection<TileSource> sources) {
     49        super();
     50        this.slippyMapBBoxChooser = slippyMapBBoxChooser;
    3851        setSources(sources);
    39         this.currentMap = 2;
    4052        enlargeImage = ImageProvider.get("layer-switcher-maximize.png");
    4153        shrinkImage = ImageProvider.get("layer-switcher-minimize.png");
     54
     55        hiddenDimension= new Dimension(enlargeImage.getIconWidth(), enlargeImage.getIconHeight());
     56        setPreferredSize(hiddenDimension);
     57
     58        addMouseListener(mouseListener);
    4259    }
    43    
     60
     61    private final MouseListener mouseListener = new MouseAdapter() {
     62        @Override
     63        public void mouseReleased(MouseEvent e) {
     64            if (e.getButton() == MouseEvent.BUTTON1) {
     65                Point point = e.getPoint();
     66                if (isEnlarged) {
     67                    if (barWidth < point.x && point.y < shrinkImage.getIconHeight()) {
     68                        toggle();
     69                    } else {
     70                        int result = (point.y - 5) / layerHeight;
     71                        if (result >= 0 && result < SourceButton.this.sources.length) {
     72                            SourceButton.this.slippyMapBBoxChooser.toggleMapSource(SourceButton.this.sources[result]);
     73                            currentMap = result;
     74                            toggle();
     75                        }
     76                    }
     77                } else {
     78                    toggle();
     79                }
     80
     81            }
     82        }
     83    };
     84
    4485    /**
    4586     * Set the tile sources.
     
    5091        CheckParameterUtil.ensureParameterNotNull(sources, "sources");
    5192        this.sources = sources.toArray(new TileSource[sources.size()]);
     93        shownDimension = null;
    5294    }
    5395
    54     public void paint(Graphics2D g) {
    55         if (isEnlarged) {
    56             g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    57             int leftPadding = 5;
    58             int radioButtonSize = 10;
    59             int topPadding = 5;
    60             int bottomPadding = 5;
     96    @Override
     97    protected void paintComponent(Graphics graphics) {
     98        Graphics2D g = (Graphics2D) graphics.create();
     99        try {
     100            calculateShownDimension();
     101            g.setFont(font);
     102            if (isEnlarged) {
     103                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     104                int radioButtonSize = 10;
    61105
     106                g.setColor(new Color(0, 0, 139, 179));
     107                g.fillRoundRect(0, 0, barWidth + shrinkImage.getIconWidth(), sources.length * layerHeight + topPadding + bottomPadding, 10, 10);
     108                for (int i=0; i<sources.length; i++) {
     109                    g.setColor(Color.WHITE);
     110                    g.fillOval(leftPadding, topPadding + i * layerHeight + 6, radioButtonSize, radioButtonSize);
     111                    g.drawString(sources[i].getName(), leftPadding + radioButtonSize + leftPadding, topPadding + i * layerHeight + g.getFontMetrics().getHeight());
     112                    if (currentMap == i) {
     113                        g.setColor(Color.BLACK);
     114                        g.fillOval(leftPadding + 1, topPadding + 7 + i * layerHeight, radioButtonSize - 2, radioButtonSize - 2);
     115                    }
     116                }
     117
     118                g.drawImage(shrinkImage.getImage(), barWidth, 0, null);
     119            } else {
     120                g.drawImage(enlargeImage.getImage(), 0, 0, null);
     121            }
     122        } finally {
     123            g.dispose();
     124        }
     125    }
     126
     127    public void toggle() {
     128        this.isEnlarged = !this.isEnlarged;
     129        calculateShownDimension();
     130        setPreferredSize(isEnlarged?shownDimension:hiddenDimension);
     131        revalidate();
     132    }
     133
     134
     135    public void setCurrentMap(TileSource tileSource) {
     136        for (int i=0; i<sources.length; i++) {
     137            if (sources[i].equals(tileSource)) {
     138                currentMap = i;
     139                return;
     140            }
     141        }
     142        currentMap = 0;
     143    }
     144
     145    private void calculateShownDimension() {
     146        if (shownDimension == null) {
     147            font = getFont().deriveFont(Font.BOLD).deriveFont(15.0f);
    62148            int textWidth = 0;
    63 
    64             g.setFont(g.getFont().deriveFont(Font.BOLD).deriveFont(15.0f));
    65             FontMetrics fm = g.getFontMetrics();
     149            FontMetrics fm = getFontMetrics(font);
    66150            for (TileSource source: sources) {
    67151                int width = fm.stringWidth(source.getName());
     
    70154                }
    71155            }
    72 
    73156            barWidth = textWidth + 50;
    74             barX = g.getClipBounds().width  - barWidth - shrinkImage.getIconWidth();
    75             barY = 30;
    76             layerHeight = 20;
    77 
    78             g.setColor(new Color(0, 0, 139, 179));
    79             g.fillRoundRect(barX, barY, barWidth + shrinkImage.getIconWidth(), sources.length * layerHeight + topPadding + bottomPadding, 10, 10);
    80             for (int i=0; i<sources.length; i++) {
    81                 g.setColor(Color.WHITE);
    82                 g.fillOval(barX + leftPadding, barY + topPadding + i * layerHeight + 6, radioButtonSize, radioButtonSize);
    83                 g.drawString(sources[i].getName(), barX + leftPadding + radioButtonSize + leftPadding, barY + topPadding + i * layerHeight + g.getFontMetrics().getHeight());
    84                 if (currentMap == i + 2) {
    85                     g.setColor(Color.BLACK);
    86                     g.fillOval(barX + leftPadding + 1, barY + topPadding + 7 + i * layerHeight, radioButtonSize - 2, radioButtonSize - 2);
    87                 }
    88             }
    89 
    90             g.drawImage(shrinkImage.getImage(), barX + barWidth, barY, null);
    91         } else {
    92             barWidth = 0;
    93             barX = g.getClipBounds().width  - shrinkImage.getIconWidth();
    94             barY = 30;
    95             g.drawImage(enlargeImage.getImage(), barX + barWidth, barY, null);
     157            shownDimension = new Dimension(barWidth + shrinkImage.getIconWidth(), sources.length * layerHeight + topPadding + bottomPadding);
    96158        }
    97159    }
    98 
    99     public void toggle() {
    100         this.isEnlarged = !this.isEnlarged;
    101 
    102     }
    103 
    104     public int hit(Point point) {
    105         if (isEnlarged) {
    106             if (barX + barWidth < point.x) {
    107                 if (barY < point.y && point.y < barY + shrinkImage.getIconHeight())
    108                     return HIDE_OR_SHOW;
    109             } else if (barX < point.x && point.x < barX + barWidth) {
    110                 int result = (point.y - barY - 5) / layerHeight;
    111                 if (result >= 0 && result < sources.length) {
    112                     currentMap = result + 2;
    113                     return currentMap;
    114                 }
    115             }
    116         } else {
    117             if (barX + barWidth < point.x) {
    118                 if (barY < point.y && point.y < barY + shrinkImage.getIconHeight())
    119                     return HIDE_OR_SHOW;
    120             }
    121         }
    122 
    123         return 0;
    124     }
    125 
    126     public TileSource hitToTileSource(int hit) {
    127         if (hit >= 2 && hit < sources.length + 2)
    128             return sources[hit - 2];
    129         else
    130             return null;
    131     }
    132 
    133     public void setCurrentMap(TileSource tileSource) {
    134         for (int i=0; i<sources.length; i++) {
    135             if (sources[i].equals(tileSource)) {
    136                 currentMap = i + 2;
    137                 return;
    138             }
    139         }
    140         currentMap = 2;
    141     }
    142160}
Note: See TracChangeset for help on using the changeset viewer.