source: josm/trunk/src/org/openstreetmap/josm/gui/bbox/SourceButton.java@ 8056

Last change on this file since 8056 was 8056, checked in by bastiK, 9 years ago

fixed #11096 - png -> svg

  • Property svn:eol-style set to native
File size: 5.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.bbox;
3
4import java.awt.Color;
5import java.awt.Dimension;
6import java.awt.Font;
7import java.awt.FontMetrics;
8import java.awt.Graphics;
9import java.awt.Graphics2D;
10import java.awt.Point;
11import java.awt.RenderingHints;
12import java.awt.event.MouseAdapter;
13import java.awt.event.MouseEvent;
14import java.awt.event.MouseListener;
15import java.util.Collection;
16
17import javax.swing.ImageIcon;
18import javax.swing.JComponent;
19
20import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
21import org.openstreetmap.josm.tools.CheckParameterUtil;
22import org.openstreetmap.josm.tools.ImageProvider;
23
24public class SourceButton extends JComponent {
25
26 private static final int LAYER_HEIGHT = 20;
27 private static final int LEFT_PADDING = 5;
28 private static final int TOP_PADDING = 5;
29 private static final int BOTTOM_PADDING = 5;
30
31 private TileSource[] sources;
32
33 private final ImageIcon enlargeImage;
34 private final ImageIcon shrinkImage;
35 private final Dimension hiddenDimension;
36
37 // Calculated after component is added to container
38 private int barWidth;
39 private Dimension shownDimension;
40 private Font font;
41
42 private boolean isEnlarged = false;
43
44 private int currentMap;
45 private final SlippyMapBBoxChooser slippyMapBBoxChooser;
46
47 public SourceButton(SlippyMapBBoxChooser slippyMapBBoxChooser, Collection<TileSource> sources) {
48 this.slippyMapBBoxChooser = slippyMapBBoxChooser;
49 setSources(sources);
50 enlargeImage = ImageProvider.get("layer-switcher-maximize");
51 shrinkImage = ImageProvider.get("layer-switcher-minimize");
52
53 hiddenDimension= new Dimension(enlargeImage.getIconWidth(), enlargeImage.getIconHeight());
54 setPreferredSize(hiddenDimension);
55
56 addMouseListener(mouseListener);
57 }
58
59 private final MouseListener mouseListener = new MouseAdapter() {
60 @Override
61 public void mouseReleased(MouseEvent e) {
62 if (e.getButton() == MouseEvent.BUTTON1) {
63 Point point = e.getPoint();
64 if (isEnlarged) {
65 if (barWidth < point.x && point.y < shrinkImage.getIconHeight()) {
66 toggle();
67 } else {
68 int result = (point.y - 5) / LAYER_HEIGHT;
69 if (result >= 0 && result < SourceButton.this.sources.length) {
70 SourceButton.this.slippyMapBBoxChooser.toggleMapSource(SourceButton.this.sources[result]);
71 currentMap = result;
72 toggle();
73 }
74 }
75 } else {
76 toggle();
77 }
78 }
79 }
80 };
81
82 /**
83 * Set the tile sources.
84 * @param sources The tile sources to display
85 * @since 6364
86 */
87 public final void setSources(Collection<TileSource> sources) {
88 CheckParameterUtil.ensureParameterNotNull(sources, "sources");
89 this.sources = sources.toArray(new TileSource[sources.size()]);
90 shownDimension = null;
91 }
92
93 @Override
94 protected void paintComponent(Graphics graphics) {
95 Graphics2D g = (Graphics2D) graphics.create();
96 try {
97 calculateShownDimension();
98 g.setFont(font);
99 if (isEnlarged) {
100 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
101 int radioButtonSize = 10;
102
103 g.setColor(new Color(0, 0, 139, 179));
104 g.fillRoundRect(0, 0, barWidth + shrinkImage.getIconWidth(), sources.length * LAYER_HEIGHT + TOP_PADDING + BOTTOM_PADDING, 10, 10);
105 for (int i=0; i<sources.length; i++) {
106 g.setColor(Color.WHITE);
107 g.fillOval(LEFT_PADDING, TOP_PADDING + i * LAYER_HEIGHT + 6, radioButtonSize, radioButtonSize);
108 g.drawString(sources[i].getName(), LEFT_PADDING + radioButtonSize + LEFT_PADDING, TOP_PADDING + i * LAYER_HEIGHT + g.getFontMetrics().getHeight());
109 if (currentMap == i) {
110 g.setColor(Color.BLACK);
111 g.fillOval(LEFT_PADDING + 1, TOP_PADDING + 7 + i * LAYER_HEIGHT, radioButtonSize - 2, radioButtonSize - 2);
112 }
113 }
114
115 g.drawImage(shrinkImage.getImage(), barWidth, 0, null);
116 } else {
117 g.drawImage(enlargeImage.getImage(), 0, 0, null);
118 }
119 } finally {
120 g.dispose();
121 }
122 }
123
124 public void toggle() {
125 this.isEnlarged = !this.isEnlarged;
126 calculateShownDimension();
127 setPreferredSize(isEnlarged?shownDimension:hiddenDimension);
128 revalidate();
129 }
130
131
132 public void setCurrentMap(TileSource tileSource) {
133 for (int i=0; i<sources.length; i++) {
134 if (sources[i].equals(tileSource)) {
135 currentMap = i;
136 return;
137 }
138 }
139 currentMap = 0;
140 }
141
142 private void calculateShownDimension() {
143 if (shownDimension == null) {
144 font = getFont().deriveFont(Font.BOLD).deriveFont(15.0f);
145 int textWidth = 0;
146 FontMetrics fm = getFontMetrics(font);
147 for (TileSource source: sources) {
148 int width = fm.stringWidth(source.getName());
149 if (width > textWidth) {
150 textWidth = width;
151 }
152 }
153 barWidth = textWidth + 50;
154 shownDimension = new Dimension(barWidth + shrinkImage.getIconWidth(), sources.length * LAYER_HEIGHT + TOP_PADDING + BOTTOM_PADDING);
155 }
156 }
157}
Note: See TracBrowser for help on using the repository browser.