- Timestamp:
- 2013-12-26T19:42:23+01:00 (11 years ago)
- 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 2 2 package org.openstreetmap.josm.gui.bbox; 3 3 4 import java.awt.Dimension; 4 5 import java.awt.Graphics; 5 6 import java.awt.Point; 7 import java.awt.event.MouseAdapter; 8 import java.awt.event.MouseEvent; 6 9 7 10 import javax.swing.ImageIcon; 11 import javax.swing.JComponent; 8 12 9 13 import org.openstreetmap.josm.tools.ImageProvider; … … 12 16 * @author Tim Haussmann 13 17 */ 14 public class SizeButton{ 18 public class SizeButton extends JComponent { 15 19 16 20 private int x = 0; … … 20 24 private ImageIcon shrinkImage; 21 25 private boolean isEnlarged = false; 26 private final SlippyMapBBoxChooser slippyMapBBoxChooser; 22 27 23 public SizeButton(){ 28 public SizeButton(SlippyMapBBoxChooser slippyMapBBoxChooser){ 29 super(); 30 this.slippyMapBBoxChooser = slippyMapBBoxChooser; 24 31 enlargeImage = ImageProvider.get("view-fullscreen.png"); 25 32 shrinkImage = ImageProvider.get("view-fullscreen-revert.png"); 33 setPreferredSize(new Dimension(enlargeImage.getIconWidth(), enlargeImage.getIconHeight())); 34 addMouseListener(mouseListener); 26 35 } 27 36 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) { 29 50 if(isEnlarged) { 30 51 if(shrinkImage != null) -
trunk/src/org/openstreetmap/josm/gui/bbox/SlippyMapBBoxChooser.java
r6378 r6539 7 7 import java.awt.Dimension; 8 8 import java.awt.Graphics; 9 import java.awt.Graphics2D;10 9 import java.awt.Image; 11 10 import java.awt.Point; … … 21 20 22 21 import javax.swing.JOptionPane; 22 import javax.swing.SpringLayout; 23 23 24 24 import org.openstreetmap.gui.jmapviewer.Coordinate; … … 170 170 private OsmTileLoader uncachedLoader; 171 171 172 private final SizeButton iSizeButton = new SizeButton();172 private final SizeButton iSizeButton; 173 173 private final SourceButton iSourceButton; 174 174 private Bounds bbox; … … 182 182 */ 183 183 public SlippyMapBBoxChooser() { 184 SpringLayout springLayout = new SpringLayout(); 185 setLayout(springLayout); 184 186 TMSLayer.setMaxWorkers(); 185 187 cachedLoader = TMSLayer.loaderFactory.makeTileLoader(this); … … 202 204 List<TileSource> tileSources = getAllTileSources(); 203 205 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); 205 213 206 214 String mapStyle = PROP_MAPSTYLE.get(); … … 219 227 } 220 228 221 new SlippyMapControler(this, this , iSizeButton, iSourceButton);222 } 223 229 new SlippyMapControler(this, this); 230 } 231 224 232 private List<TileSource> getAllTileSources() { 225 233 List<TileSource> tileSources = new ArrayList<TileSource>(); … … 264 272 g.drawRect(x_min, y_min, w, h); 265 273 } 266 267 iSizeButton.paint(g);268 iSourceButton.paint((Graphics2D)g);269 274 } catch (Exception e) { 270 275 e.printStackTrace(); … … 394 399 repaint(); 395 400 } 396 401 397 402 /** 398 403 * Refreshes the tile sources -
trunk/src/org/openstreetmap/josm/gui/bbox/SlippyMapControler.java
r6438 r6539 51 51 private final SlippyMapBBoxChooser iSlippyMapChooser; 52 52 53 private SizeButton iSizeButton = null;54 private SourceButton iSourceButton = null;55 56 53 private boolean isSelecting; 57 54 … … 59 56 * Constructs a new {@code SlippyMapControler}. 60 57 */ 61 public SlippyMapControler(SlippyMapBBoxChooser navComp, JPanel contentPane , SizeButton sizeButton, SourceButton sourceButton) {58 public SlippyMapControler(SlippyMapBBoxChooser navComp, JPanel contentPane) { 62 59 iSlippyMapChooser = navComp; 63 60 iSlippyMapChooser.addMouseListener(this); … … 74 71 } 75 72 } 76 iSizeButton = sizeButton;77 iSourceButton = sourceButton;78 79 73 isSelecting = false; 80 74 … … 124 118 public void mousePressed(MouseEvent e) { 125 119 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 } 132 123 } 133 124 … … 160 151 161 152 } 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); 175 154 } 176 155 } -
trunk/src/org/openstreetmap/josm/gui/bbox/SourceButton.java
r6364 r6539 3 3 4 4 import java.awt.Color; 5 import java.awt.Dimension; 5 6 import java.awt.Font; 6 7 import java.awt.FontMetrics; 8 import java.awt.Graphics; 7 9 import java.awt.Graphics2D; 8 10 import java.awt.Point; 9 11 import java.awt.RenderingHints; 12 import java.awt.event.MouseAdapter; 13 import java.awt.event.MouseEvent; 14 import java.awt.event.MouseListener; 10 15 import java.util.Collection; 11 16 12 17 import javax.swing.ImageIcon; 18 import javax.swing.JComponent; 13 19 14 20 import org.openstreetmap.gui.jmapviewer.interfaces.TileSource; … … 16 22 import org.openstreetmap.josm.tools.ImageProvider; 17 23 18 public class SourceButton { 24 public class SourceButton extends JComponent { 19 25 20 // Filled in paint, used in hit21 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 25 31 26 32 private TileSource[] sources; 27 33 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; 30 42 31 43 private boolean isEnlarged = false; 32 44 33 45 private int currentMap; 46 private final SlippyMapBBoxChooser slippyMapBBoxChooser; 34 47 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; 38 51 setSources(sources); 39 this.currentMap = 2;40 52 enlargeImage = ImageProvider.get("layer-switcher-maximize.png"); 41 53 shrinkImage = ImageProvider.get("layer-switcher-minimize.png"); 54 55 hiddenDimension= new Dimension(enlargeImage.getIconWidth(), enlargeImage.getIconHeight()); 56 setPreferredSize(hiddenDimension); 57 58 addMouseListener(mouseListener); 42 59 } 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 44 85 /** 45 86 * Set the tile sources. … … 50 91 CheckParameterUtil.ensureParameterNotNull(sources, "sources"); 51 92 this.sources = sources.toArray(new TileSource[sources.size()]); 93 shownDimension = null; 52 94 } 53 95 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; 61 105 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); 62 148 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); 66 150 for (TileSource source: sources) { 67 151 int width = fm.stringWidth(source.getName()); … … 70 154 } 71 155 } 72 73 156 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); 96 158 } 97 159 } 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 else130 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 }142 160 }
Note:
See TracChangeset
for help on using the changeset viewer.