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

Last change on this file since 8509 was 8509, checked in by Don-vip, 9 years ago

fix many checkstyle violations

  • Property svn:eol-style set to native
File size: 5.7 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 transient 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 transient 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(),
105 sources.length * LAYER_HEIGHT + TOP_PADDING + BOTTOM_PADDING, 10, 10);
106 for (int i=0; i<sources.length; i++) {
107 g.setColor(Color.WHITE);
108 g.fillOval(LEFT_PADDING, TOP_PADDING + i * LAYER_HEIGHT + 6, radioButtonSize, radioButtonSize);
109 g.drawString(sources[i].getName(), LEFT_PADDING + radioButtonSize + LEFT_PADDING,
110 TOP_PADDING + i * LAYER_HEIGHT + g.getFontMetrics().getHeight());
111 if (currentMap == i) {
112 g.setColor(Color.BLACK);
113 g.fillOval(LEFT_PADDING + 1, TOP_PADDING + 7 + i * LAYER_HEIGHT, radioButtonSize - 2, radioButtonSize - 2);
114 }
115 }
116
117 g.drawImage(shrinkImage.getImage(), barWidth, 0, null);
118 } else {
119 g.drawImage(enlargeImage.getImage(), 0, 0, null);
120 }
121 } finally {
122 g.dispose();
123 }
124 }
125
126 public void toggle() {
127 this.isEnlarged = !this.isEnlarged;
128 calculateShownDimension();
129 setPreferredSize(isEnlarged?shownDimension:hiddenDimension);
130 revalidate();
131 }
132
133
134 public void setCurrentMap(TileSource tileSource) {
135 for (int i=0; i<sources.length; i++) {
136 if (sources[i].equals(tileSource)) {
137 currentMap = i;
138 return;
139 }
140 }
141 currentMap = 0;
142 }
143
144 private void calculateShownDimension() {
145 if (shownDimension == null) {
146 font = getFont().deriveFont(Font.BOLD).deriveFont(15.0f);
147 int textWidth = 0;
148 FontMetrics fm = getFontMetrics(font);
149 for (TileSource source: sources) {
150 int width = fm.stringWidth(source.getName());
151 if (width > textWidth) {
152 textWidth = width;
153 }
154 }
155 barWidth = textWidth + 50;
156 shownDimension = new Dimension(barWidth + shrinkImage.getIconWidth(), sources.length * LAYER_HEIGHT + TOP_PADDING + BOTTOM_PADDING);
157 }
158 }
159}
Note: See TracBrowser for help on using the repository browser.