Changeset 12955 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2017-10-09T00:17:55+02:00 (7 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/gui
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/bbox/SlippyMapBBoxChooser.java
r12931 r12955 22 22 import java.util.concurrent.CopyOnWriteArrayList; 23 23 24 import javax.swing.ButtonModel; 25 import javax.swing.JToggleButton; 24 26 import javax.swing.JOptionPane; 25 27 import javax.swing.SpringLayout; 28 import javax.swing.event.ChangeListener; 29 import javax.swing.event.ChangeEvent; 26 30 27 31 import org.openstreetmap.gui.jmapviewer.Coordinate; … … 44 48 import org.openstreetmap.josm.data.imagery.TileLoaderFactory; 45 49 import org.openstreetmap.josm.data.osm.BBox; 50 import org.openstreetmap.josm.data.preferences.BooleanProperty; 46 51 import org.openstreetmap.josm.data.preferences.StringProperty; 47 52 import org.openstreetmap.josm.gui.MainApplication; … … 56 61 * This panel displays a map and lets the user chose a {@link BBox}. 57 62 */ 58 public class SlippyMapBBoxChooser extends JMapViewer implements BBoxChooser, MainLayerManager.ActiveLayerChangeListener {63 public class SlippyMapBBoxChooser extends JMapViewer implements BBoxChooser, ChangeListener, MainLayerManager.ActiveLayerChangeListener { 59 64 60 65 /** … … 121 126 122 127 private static final StringProperty PROP_MAPSTYLE = new StringProperty("slippy_map_chooser.mapstyle", "Mapnik"); 128 private static final BooleanProperty PROP_SHOWDLAREA = new BooleanProperty("slippy_map_chooser.show_downloaded_area", true); 123 129 /** 124 130 * The property name used for the resize button. … … 131 137 132 138 private final SizeButton iSizeButton; 139 private final ButtonModel showDownloadAreaButtonModel; 133 140 private final SourceButton iSourceButton; 134 141 private transient Bounds bbox; … … 173 180 List<TileSource> tileSources = getAllTileSources(); 174 181 175 iSourceButton = new SourceButton(this, tileSources); 182 this.showDownloadAreaButtonModel = new JToggleButton.ToggleButtonModel(); 183 this.showDownloadAreaButtonModel.setSelected(PROP_SHOWDLAREA.get()); 184 this.showDownloadAreaButtonModel.addChangeListener(this); 185 iSourceButton = new SourceButton(this, tileSources, this.showDownloadAreaButtonModel); 176 186 add(iSourceButton); 177 springLayout.putConstraint(SpringLayout.EAST, iSourceButton, 0, SpringLayout.EAST, this);178 springLayout.putConstraint(SpringLayout.NORTH, iSourceButton, 30, SpringLayout.NORTH, this);187 springLayout.putConstraint(SpringLayout.EAST, iSourceButton, -2, SpringLayout.EAST, this); 188 springLayout.putConstraint(SpringLayout.NORTH, iSourceButton, 2, SpringLayout.NORTH, this); 179 189 180 190 iSizeButton = new SizeButton(this); … … 231 241 // enough to make sharing code impractical) 232 242 final OsmDataLayer editLayer = MainApplication.getLayerManager().getEditLayer(); 233 if (editLayer != null && Config.getPref().getBoolean("draw.data.downloaded_area", true) && !editLayer.data.getDataSources().isEmpty()) {243 if (editLayer != null && this.showDownloadAreaButtonModel.isSelected() && !editLayer.data.getDataSources().isEmpty()) { 234 244 // initialize area with current viewport 235 245 Rectangle b = this.getBounds(); … … 271 281 @Override 272 282 public void activeOrEditLayerChanged(MainLayerManager.ActiveLayerChangeEvent e) { 283 this.repaint(); 284 } 285 286 @Override 287 public void stateChanged(ChangeEvent e) { 288 // fired for the stateChanged event of this.showDownloadAreaButtonModel 289 PROP_SHOWDLAREA.put(this.showDownloadAreaButtonModel.isSelected()); 273 290 this.repaint(); 274 291 } … … 342 359 this.setTileSource(tileSource); 343 360 PROP_MAPSTYLE.put(tileSource.getName()); // TODO Is name really unique? 361 if (this.iSourceButton.getCurrentSource() != tileSource) { // prevent infinite recursion 362 this.iSourceButton.setCurrentMap(tileSource); 363 } 344 364 } 345 365 -
trunk/src/org/openstreetmap/josm/gui/bbox/SourceButton.java
r12597 r12955 2 2 package org.openstreetmap.josm.gui.bbox; 3 3 4 import java.awt.Color; 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt.Dimension; 6 import java.awt.Font; 7 import java.awt.FontMetrics; 8 import java.awt.Graphics; 9 import java.awt.Graphics2D; 10 import java.awt.Point; 11 import java.awt.RenderingHints; 12 import java.awt.event.MouseAdapter; 13 import java.awt.event.MouseEvent; 14 import java.awt.event.MouseListener; 7 import java.awt.event.ActionListener; 8 import java.awt.event.ActionEvent; 9 import java.util.ArrayList; 15 10 import java.util.Collection; 11 import java.util.Collections; 12 import java.util.Enumeration; 13 import java.util.List; 16 14 17 import javax.swing.ImageIcon; 18 import javax.swing.JComponent; 15 import javax.swing.AbstractButton; 16 import javax.swing.ButtonGroup; 17 import javax.swing.ButtonModel; 18 import javax.swing.JCheckBoxMenuItem; 19 import javax.swing.JPopupMenu; 20 import javax.swing.JRadioButtonMenuItem; 21 import javax.swing.JToggleButton; 19 22 20 23 import org.openstreetmap.gui.jmapviewer.interfaces.TileSource; 24 import org.openstreetmap.josm.gui.widgets.PopupMenuButton; 21 25 import org.openstreetmap.josm.tools.CheckParameterUtil; 22 26 import org.openstreetmap.josm.tools.ImageProvider; … … 26 30 * @since 1390 27 31 */ 28 public class SourceButton extends JComponent { 32 public class SourceButton extends PopupMenuButton { 33 protected class TileSourceButtonModel extends JToggleButton.ToggleButtonModel implements ActionListener { 34 protected final TileSource tileSource; 29 35 30 private static final int LAYER_HEIGHT = 20; 31 private static final int LEFT_PADDING = 5; 32 private static final int TOP_PADDING = 5; 33 private static final int BOTTOM_PADDING = 5; 36 public TileSourceButtonModel(TileSource tileSource_) { 37 super(); 38 this.tileSource = tileSource_; 39 this.addActionListener(this); 40 } 34 41 35 private transient TileSource[] sources; 42 @Override 43 public void actionPerformed(ActionEvent e) { 44 if (SourceButton.this.slippyMapBBoxChooser.getTileController().getTileSource() != this.tileSource) { // prevent infinite recursion 45 SourceButton.this.slippyMapBBoxChooser.toggleMapSource(this.tileSource); 46 } 47 } 48 } 36 49 37 private final ImageIcon enlargeImage; 38 private final ImageIcon shrinkImage; 39 private final Dimension hiddenDimension; 40 41 // Calculated after component is added to container 42 private int barWidth; 43 private Dimension shownDimension; 44 private Font font; 45 46 private boolean isEnlarged; 47 48 private int currentMap; 49 private final SlippyMapBBoxChooser slippyMapBBoxChooser; 50 protected final SlippyMapBBoxChooser slippyMapBBoxChooser; 51 protected final ButtonModel showDownloadAreaButtonModel; 52 private List<TileSource> sources; 53 private ButtonGroup sourceButtonGroup; 50 54 51 55 /** … … 54 58 * @param sources list of imagery sources to display 55 59 */ 56 public SourceButton( SlippyMapBBoxChooser slippyMapBBoxChooser, Collection<TileSource> sources) {57 this.slippyMapBBoxChooser = slippyMapBBoxChooser;58 setSources(sources);59 enlargeImage = ImageProvider.get("layer-switcher-maximize");60 shrinkImage = ImageProvider.get("layer-switcher-minimize");61 62 hiddenDimension = new Dimension(enlargeImage.getIconWidth(), enlargeImage.getIconHeight());63 setPreferredSize(hiddenDimension);64 65 addMouseListener(mouseListener);60 public SourceButton( 61 SlippyMapBBoxChooser slippyMapBBoxChooser_, 62 Collection<TileSource> sources_, 63 ButtonModel showDownloadAreaButtonModel_ 64 ) { 65 super(new ImageProvider("dialogs/layerlist").getResource().getImageIcon(new Dimension(16, 16))); 66 this.showDownloadAreaButtonModel = showDownloadAreaButtonModel_; 67 this.slippyMapBBoxChooser = slippyMapBBoxChooser_; 68 this.setPreferredSize(new Dimension(24, 24)); 69 this.setSources(sources_); 66 70 } 67 71 68 private final transient MouseListener mouseListener = new MouseAdapter() { 69 @Override 70 public void mouseReleased(MouseEvent e) { 71 if (e.getButton() == MouseEvent.BUTTON1) { 72 Point point = e.getPoint(); 73 if (isEnlarged) { 74 if (barWidth < point.x && point.y < shrinkImage.getIconHeight()) { 75 toggle(); 76 } else { 77 int result = (point.y - 5) / LAYER_HEIGHT; 78 if (result >= 0 && result < SourceButton.this.sources.length) { 79 SourceButton.this.slippyMapBBoxChooser.toggleMapSource(SourceButton.this.sources[result]); 80 currentMap = result; 81 toggle(); 82 } 83 } 84 } else { 85 toggle(); 86 } 87 } 72 protected void generatePopupMenu() { 73 JPopupMenu pm = new JPopupMenu(); 74 this.sourceButtonGroup = new ButtonGroup(); 75 for (TileSource ts : this.sources) { 76 JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(ts.getName()); 77 TileSourceButtonModel buttonModel = new TileSourceButtonModel(ts); 78 menuItem.setModel(buttonModel); 79 pm.add(menuItem); 80 this.sourceButtonGroup.add(menuItem); 81 82 // attempt to initialize button group matching current state of slippyMapBBoxChooser 83 buttonModel.setSelected(this.slippyMapBBoxChooser.getTileController().getTileSource() == ts); 88 84 } 89 }; 85 86 pm.addSeparator(); 87 88 JCheckBoxMenuItem showDownloadAreaItem = new JCheckBoxMenuItem(tr("Show downloaded area")); 89 showDownloadAreaItem.setModel(this.showDownloadAreaButtonModel); 90 pm.add(showDownloadAreaItem); 91 92 this.setPopupMenu(pm); 93 } 94 95 private void setSourceDefault() { 96 Enumeration<AbstractButton> elems = this.sourceButtonGroup.getElements(); 97 if (elems.hasMoreElements()) { 98 elems.nextElement().setSelected(true); 99 } 100 } 90 101 91 102 /** … … 94 105 * @since 6364 95 106 */ 96 public final void setSources(Collection<TileSource> sources) { 97 CheckParameterUtil.ensureParameterNotNull(sources, "sources"); 98 this.sources = sources.toArray(new TileSource[sources.size()]); 99 shownDimension = null; 100 } 101 102 @Override 103 protected void paintComponent(Graphics graphics) { 104 Graphics2D g = (Graphics2D) graphics.create(); 105 try { 106 calculateShownDimension(); 107 g.setFont(font); 108 if (isEnlarged) { 109 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 110 int radioButtonSize = 10; 111 112 g.setColor(new Color(0, 0, 139, 179)); 113 g.fillRoundRect(0, 0, barWidth + shrinkImage.getIconWidth(), 114 sources.length * LAYER_HEIGHT + TOP_PADDING + BOTTOM_PADDING, 10, 10); 115 for (int i = 0; i < sources.length; i++) { 116 g.setColor(Color.WHITE); 117 g.fillOval(LEFT_PADDING, TOP_PADDING + i * LAYER_HEIGHT + 6, radioButtonSize, radioButtonSize); 118 g.drawString(sources[i].getName(), LEFT_PADDING + radioButtonSize + LEFT_PADDING, 119 TOP_PADDING + i * LAYER_HEIGHT + g.getFontMetrics().getHeight()); 120 if (currentMap == i) { 121 g.setColor(Color.BLACK); 122 g.fillOval(LEFT_PADDING + 1, TOP_PADDING + 7 + i * LAYER_HEIGHT, radioButtonSize - 2, radioButtonSize - 2); 123 } 124 } 125 126 g.drawImage(shrinkImage.getImage(), barWidth, 0, null); 127 } else { 128 g.drawImage(enlargeImage.getImage(), 0, 0, null); 129 } 130 } finally { 131 g.dispose(); 107 public final void setSources(Collection<TileSource> sources_) { 108 CheckParameterUtil.ensureParameterNotNull(sources_, "sources_"); 109 this.sources = new ArrayList<TileSource>(sources_); 110 this.generatePopupMenu(); 111 if (this.sourceButtonGroup.getSelection() == null) { 112 this.setSourceDefault(); 132 113 } 133 114 } 134 115 135 116 /** 136 * Toggle the visibility of imagery source list. 117 * Get the tile sources. 118 * @return unmodifiable collection of tile sources 137 119 */ 138 public void toggle() { 139 this.isEnlarged = !this.isEnlarged; 140 calculateShownDimension(); 141 setPreferredSize(isEnlarged ? shownDimension : hiddenDimension); 142 revalidate(); 120 public final Collection<TileSource> getSources() { 121 return Collections.unmodifiableCollection(this.sources); 122 } 123 124 /** 125 * Get the currently-selected tile source. 126 */ 127 public final TileSource getCurrentSource() { 128 TileSourceButtonModel buttonModel = (TileSourceButtonModel) this.sourceButtonGroup.getSelection(); 129 if (buttonModel != null) { 130 return buttonModel.tileSource; 131 } 132 return null; 143 133 } 144 134 … … 148 138 */ 149 139 public void setCurrentMap(TileSource tileSource) { 150 for (int i = 0; i < sources.length; i++) { 151 if (sources[i].equals(tileSource)) { 152 currentMap = i; 140 Enumeration<AbstractButton> elems = this.sourceButtonGroup.getElements(); 141 while (elems.hasMoreElements()) { 142 AbstractButton b = elems.nextElement(); 143 if (((TileSourceButtonModel) b.getModel()).tileSource == tileSource) { 144 b.setSelected(true); 153 145 return; 154 146 } 155 147 } 156 currentMap = 0; 157 } 158 159 private void calculateShownDimension() { 160 if (shownDimension == null) { 161 font = getFont().deriveFont(Font.BOLD).deriveFont(15.0f); 162 int textWidth = 0; 163 FontMetrics fm = getFontMetrics(font); 164 for (TileSource source: sources) { 165 int width = fm.stringWidth(source.getName()); 166 if (width > textWidth) { 167 textWidth = width; 168 } 169 } 170 barWidth = textWidth + 50; 171 shownDimension = new Dimension(barWidth + shrinkImage.getIconWidth(), sources.length * LAYER_HEIGHT + TOP_PADDING + BOTTOM_PADDING); 172 } 148 // failed to find the correct one 149 this.setSourceDefault(); 173 150 } 174 151 }
Note:
See TracChangeset
for help on using the changeset viewer.