1 | package org.openstreetmap.josm.gui.bbox;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Font;
|
---|
5 | import java.awt.FontMetrics;
|
---|
6 | import java.awt.Graphics2D;
|
---|
7 | import java.awt.Point;
|
---|
8 | import java.awt.RenderingHints;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import javax.swing.ImageIcon;
|
---|
12 |
|
---|
13 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
---|
14 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
15 |
|
---|
16 | public class SourceButton {
|
---|
17 |
|
---|
18 | // Filled in paint, used in hit
|
---|
19 | private int barX;
|
---|
20 | private int barY;
|
---|
21 | private int barWidth;
|
---|
22 | private int layerHeight;
|
---|
23 |
|
---|
24 | private final TileSource[] sources;
|
---|
25 |
|
---|
26 | private ImageIcon enlargeImage;
|
---|
27 | private ImageIcon shrinkImage;
|
---|
28 |
|
---|
29 | private boolean isEnlarged = false;
|
---|
30 |
|
---|
31 | private int currentMap;
|
---|
32 |
|
---|
33 | public static final int HIDE_OR_SHOW = 1;
|
---|
34 |
|
---|
35 | public SourceButton(List<TileSource> sources) {
|
---|
36 | this.sources = sources.toArray(new TileSource[sources.size()]);
|
---|
37 | this.currentMap = 2;
|
---|
38 | enlargeImage = ImageProvider.get("layer-switcher-maximize.png");
|
---|
39 | shrinkImage = ImageProvider.get("layer-switcher-minimize.png");
|
---|
40 | }
|
---|
41 |
|
---|
42 | public void paint(Graphics2D g) {
|
---|
43 | if (isEnlarged) {
|
---|
44 | g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
---|
45 | int leftPadding = 5;
|
---|
46 | int radioButtonSize = 10;
|
---|
47 | int topPadding = 5;
|
---|
48 | int bottomPadding = 5;
|
---|
49 |
|
---|
50 | int textWidth = 0;
|
---|
51 |
|
---|
52 | g.setFont(g.getFont().deriveFont(Font.BOLD).deriveFont(15.0f));
|
---|
53 | FontMetrics fm = g.getFontMetrics();
|
---|
54 | for (TileSource source: sources) {
|
---|
55 | int width = fm.stringWidth(source.getName());
|
---|
56 | if (width > textWidth) {
|
---|
57 | textWidth = width;
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | barWidth = textWidth + 50;
|
---|
62 | barX = g.getClipBounds().width - barWidth - shrinkImage.getIconWidth();
|
---|
63 | barY = 30;
|
---|
64 | layerHeight = 20;
|
---|
65 |
|
---|
66 | g.setColor(new Color(0, 0, 139, 179));
|
---|
67 | g.fillRoundRect(barX, barY, barWidth + shrinkImage.getIconWidth(), sources.length * layerHeight + topPadding + bottomPadding, 10, 10);
|
---|
68 | for (int i=0; i<sources.length; i++) {
|
---|
69 | g.setColor(Color.WHITE);
|
---|
70 | g.fillOval(barX + leftPadding, barY + topPadding + i * layerHeight + 6, radioButtonSize, radioButtonSize);
|
---|
71 | g.drawString(sources[i].getName(), barX + leftPadding + radioButtonSize + leftPadding, barY + topPadding + i * layerHeight + g.getFontMetrics().getHeight());
|
---|
72 | if (currentMap == i + 2) {
|
---|
73 | g.setColor(Color.BLACK);
|
---|
74 | g.fillOval(barX + leftPadding + 1, barY + topPadding + 7 + i * layerHeight, radioButtonSize - 2, radioButtonSize - 2);
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | g.drawImage(shrinkImage.getImage(), barX + barWidth, barY, null);
|
---|
79 | } else {
|
---|
80 | barWidth = 0;
|
---|
81 | barX = g.getClipBounds().width - shrinkImage.getIconWidth();
|
---|
82 | barY = 30;
|
---|
83 | g.drawImage(enlargeImage.getImage(), barX + barWidth, barY, null);
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | public void toggle() {
|
---|
88 | this.isEnlarged = !this.isEnlarged;
|
---|
89 |
|
---|
90 | }
|
---|
91 |
|
---|
92 | public int hit(Point point) {
|
---|
93 | if (isEnlarged) {
|
---|
94 | if (barX + barWidth < point.x) {
|
---|
95 | if (barY < point.y && point.y < barY + shrinkImage.getIconHeight())
|
---|
96 | return HIDE_OR_SHOW;
|
---|
97 | } else if (barX < point.x && point.x < barX + barWidth) {
|
---|
98 | int result = (point.y - barY - 5) / layerHeight;
|
---|
99 | if (result >= 0 && result < sources.length) {
|
---|
100 | currentMap = result + 2;
|
---|
101 | return currentMap;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | } else {
|
---|
105 | if (barX + barWidth < point.x) {
|
---|
106 | if (barY < point.y && point.y < barY + shrinkImage.getIconHeight())
|
---|
107 | return HIDE_OR_SHOW;
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | return 0;
|
---|
112 | }
|
---|
113 |
|
---|
114 | public TileSource hitToTileSource(int hit) {
|
---|
115 | if (hit >= 2 && hit < sources.length + 2)
|
---|
116 | return sources[hit - 2];
|
---|
117 | else
|
---|
118 | return null;
|
---|
119 | }
|
---|
120 |
|
---|
121 | public void setCurrentMap(TileSource tileSource) {
|
---|
122 | for (int i=0; i<sources.length; i++) {
|
---|
123 | if (sources[i].equals(tileSource)) {
|
---|
124 | currentMap = i + 2;
|
---|
125 | return;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | currentMap = 2;
|
---|
129 | }
|
---|
130 | }
|
---|