Changeset 32528 in osm for applications/editors/josm/plugins/imagery_offset_db/src/iodb/OffsetDialogButton.java
- Timestamp:
- 2016-07-02T03:55:03+02:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/imagery_offset_db/src/iodb/OffsetDialogButton.java
r29432 r32528 1 // License: WTFPL. For details, see LICENSE file. 1 2 package iodb; 2 3 3 import java.awt.*; 4 import javax.swing.*; 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 6 import java.awt.AlphaComposite; 7 import java.awt.BasicStroke; 8 import java.awt.BorderLayout; 9 import java.awt.Color; 10 import java.awt.Component; 11 import java.awt.Font; 12 import java.awt.Graphics; 13 import java.awt.Graphics2D; 14 import java.awt.Point; 15 import java.awt.RenderingHints; 16 17 import javax.swing.Box; 18 import javax.swing.BoxLayout; 19 import javax.swing.Icon; 20 import javax.swing.ImageIcon; 21 import javax.swing.JButton; 22 import javax.swing.JLabel; 23 import javax.swing.JPanel; 24 import javax.swing.SwingConstants; 25 5 26 import org.openstreetmap.josm.Main; 6 27 import org.openstreetmap.josm.data.coor.EastNorth; … … 9 30 import org.openstreetmap.josm.gui.layer.ImageryLayer; 10 31 import org.openstreetmap.josm.tools.ImageProvider; 11 import static org.openstreetmap.josm.tools.I18n.tr;12 32 13 33 /** 14 34 * A button which shows offset information. Must be spectacular, since it's the only 15 35 * non-JOptionPane GUI in the plugin. 16 * 36 * 17 37 * @author Zverik 18 38 * @license WTFPL 19 39 */ 20 40 public class OffsetDialogButton extends JButton { 21 41 22 42 private ImageryOffsetBase offset; 23 43 … … 29 49 * @param offset An offset to display on the button. 30 50 */ 31 public OffsetDialogButton( ImageryOffsetBase offset) {51 public OffsetDialogButton(ImageryOffsetBase offset) { 32 52 this.offset = offset; 33 53 layoutComponents(); … … 57 77 String authorAndDate = offset.isDeprecated() 58 78 ? tr("Deprecated by {0} on {1}", offset.getAbandonAuthor(), 59 OffsetInfoAction.DATE_FORMAT.format(offset.getAbandonDate())) 60 : tr("Created by {0} on {1}", offset.getAuthor(), 61 OffsetInfoAction.DATE_FORMAT.format(offset.getDate())); 62 JLabel authorAndDateLabel = new JLabel(authorAndDate); 63 Font authorFont = new Font(authorAndDateLabel.getFont().getName(), Font.ITALIC, authorAndDateLabel.getFont().getSize()); 64 authorAndDateLabel.setFont(authorFont); 65 66 directionArrow = new DirectionIcon(offset.getPosition()); 67 distanceLabel = new JLabel("", directionArrow, SwingConstants.RIGHT); 68 distanceLabel.setHorizontalTextPosition(SwingConstants.LEFT); 69 Font distanceFont = new Font(distanceLabel.getFont().getName(), Font.PLAIN, distanceLabel.getFont().getSize()); 70 distanceLabel.setFont(distanceFont); 71 updateLocation(); 72 73 String description = offset.isDeprecated() ? offset.getAbandonReason() : offset.getDescription(); 74 description = description.replace("<", "<").replace(">", ">"); 75 JLabel descriptionLabel = new JLabel("<html><div style=\"width: 300px;\">"+description+"</div></html>"); 76 Font descriptionFont = new Font(descriptionLabel.getFont().getName(), Font.BOLD, descriptionLabel.getFont().getSize()); 77 descriptionLabel.setFont(descriptionFont); 78 79 OffsetIcon offsetIcon = new OffsetIcon(offset); 80 double offsetDistance = offset instanceof ImageryOffset 81 ? offsetIcon.getDistance() : 0.0; 82 // ? ((ImageryOffset)offset).getImageryPos().greatCircleDistance(offset.getPosition()) : 0.0; 83 JLabel offsetLabel = new JLabel(offsetDistance > 0.2 ? ImageryOffsetTools.formatDistance(offsetDistance) : "", 84 offsetIcon, SwingConstants.CENTER); 85 Font offsetFont = new Font(offsetLabel.getFont().getName(), Font.PLAIN, offsetLabel.getFont().getSize() - 2); 86 offsetLabel.setFont(offsetFont); 87 offsetLabel.setHorizontalTextPosition(SwingConstants.CENTER); 88 offsetLabel.setVerticalTextPosition(SwingConstants.BOTTOM); 89 90 Box topLine = new Box(BoxLayout.X_AXIS); 91 topLine.add(authorAndDateLabel); 92 topLine.add(Box.createHorizontalGlue()); 93 topLine.add(Box.createHorizontalStrut(10)); 94 topLine.add(distanceLabel); 95 96 JPanel p = new JPanel(new BorderLayout(10, 5)); 97 p.setOpaque(false); 98 p.add(topLine, BorderLayout.NORTH); 99 p.add(offsetLabel, BorderLayout.WEST); 100 p.add(descriptionLabel, BorderLayout.CENTER); 101 add(p); 79 OffsetInfoAction.DATE_FORMAT.format(offset.getAbandonDate())) 80 : tr("Created by {0} on {1}", offset.getAuthor(), 81 OffsetInfoAction.DATE_FORMAT.format(offset.getDate())); 82 JLabel authorAndDateLabel = new JLabel(authorAndDate); 83 Font authorFont = new Font(authorAndDateLabel.getFont().getName(), Font.ITALIC, authorAndDateLabel.getFont().getSize()); 84 authorAndDateLabel.setFont(authorFont); 85 86 directionArrow = new DirectionIcon(offset.getPosition()); 87 distanceLabel = new JLabel("", directionArrow, SwingConstants.RIGHT); 88 distanceLabel.setHorizontalTextPosition(SwingConstants.LEFT); 89 Font distanceFont = new Font(distanceLabel.getFont().getName(), Font.PLAIN, distanceLabel.getFont().getSize()); 90 distanceLabel.setFont(distanceFont); 91 updateLocation(); 92 93 String description = offset.isDeprecated() ? offset.getAbandonReason() : offset.getDescription(); 94 description = description.replace("<", "<").replace(">", ">"); 95 JLabel descriptionLabel = new JLabel("<html><div style=\"width: 300px;\">"+description+"</div></html>"); 96 Font descriptionFont = new Font(descriptionLabel.getFont().getName(), Font.BOLD, descriptionLabel.getFont().getSize()); 97 descriptionLabel.setFont(descriptionFont); 98 99 OffsetIcon offsetIcon = new OffsetIcon(offset); 100 double offsetDistance = offset instanceof ImageryOffset 101 ? offsetIcon.getDistance() : 0.0; 102 // ? ((ImageryOffset)offset).getImageryPos().greatCircleDistance(offset.getPosition()) : 0.0; 103 JLabel offsetLabel = new JLabel(offsetDistance > 0.2 ? ImageryOffsetTools.formatDistance(offsetDistance) : "", 104 offsetIcon, SwingConstants.CENTER); 105 Font offsetFont = new Font(offsetLabel.getFont().getName(), Font.PLAIN, offsetLabel.getFont().getSize() - 2); 106 offsetLabel.setFont(offsetFont); 107 offsetLabel.setHorizontalTextPosition(SwingConstants.CENTER); 108 offsetLabel.setVerticalTextPosition(SwingConstants.BOTTOM); 109 110 Box topLine = new Box(BoxLayout.X_AXIS); 111 topLine.add(authorAndDateLabel); 112 topLine.add(Box.createHorizontalGlue()); 113 topLine.add(Box.createHorizontalStrut(10)); 114 topLine.add(distanceLabel); 115 116 JPanel p = new JPanel(new BorderLayout(10, 5)); 117 p.setOpaque(false); 118 p.add(topLine, BorderLayout.NORTH); 119 p.add(offsetLabel, BorderLayout.WEST); 120 p.add(descriptionLabel, BorderLayout.CENTER); 121 add(p); 102 122 } 103 123 104 124 /** 105 125 * Calculates length and direction for two points in the imagery offset object. 106 * @see #getLengthAndDirection(iodb.ImageryOffset, double, double) 107 */ 108 private double[] getLengthAndDirection( ImageryOffset offset) {126 * @see #getLengthAndDirection(iodb.ImageryOffset, double, double) 127 */ 128 private double[] getLengthAndDirection(ImageryOffset offset) { 109 129 ImageryLayer layer = ImageryOffsetTools.getTopImageryLayer(); 110 130 double[] dxy = layer == null ? new double[] {0.0, 0.0} : new double[] {layer.getDx(), layer.getDy()}; … … 118 138 * @see #getLengthAndDirection(iodb.ImageryOffset) 119 139 */ 120 public static double[] getLengthAndDirection( ImageryOffset offset, double dx, double dy) {140 public static double[] getLengthAndDirection(ImageryOffset offset, double dx, double dy) { 121 141 Projection proj = Main.getProjection(); 122 142 EastNorth pos = proj.latlon2eastNorth(offset.getPosition()); … … 124 144 double length = correctedCenterLL.greatCircleDistance(offset.getImageryPos()); 125 145 double direction = length < 1e-2 ? 0.0 : correctedCenterLL.heading(offset.getImageryPos()); 126 if (direction < 0)146 if (direction < 0) 127 147 direction += Math.PI * 2; 128 148 return new double[] {length, direction}; 129 149 } 130 150 131 private static void drawArrow( Graphics g, int cx, int cy, double length, double direction) {132 int dx = (int)Math.round(Math.sin(direction) * length / 2); 133 int dy = (int)Math.round(Math.cos(direction) * length / 2); 151 private static void drawArrow(Graphics g, int cx, int cy, double length, double direction) { 152 int dx = (int) Math.round(Math.sin(direction) * length / 2); 153 int dy = (int) Math.round(Math.cos(direction) * length / 2); 134 154 g.drawLine(cx - dx, cy - dy, cx + dx, cy + dy); 135 155 double wingLength = Math.max(length / 3, 4); 136 156 double d1 = direction - Math.PI / 6; 137 int dx1 = (int)Math.round(Math.sin(d1) * wingLength); 138 int dy1 = (int)Math.round(Math.cos(d1) * wingLength); 157 int dx1 = (int) Math.round(Math.sin(d1) * wingLength); 158 int dy1 = (int) Math.round(Math.cos(d1) * wingLength); 139 159 g.drawLine(cx + dx, cy + dy, cx + dx - dx1, cy + dy - dy1); 140 160 double d2 = direction + Math.PI / 6; 141 int dx2 = (int)Math.round(Math.sin(d2) * wingLength); 142 int dy2 = (int)Math.round(Math.cos(d2) * wingLength); 161 int dx2 = (int) Math.round(Math.sin(d2) * wingLength); 162 int dy2 = (int) Math.round(Math.cos(d2) * wingLength); 143 163 g.drawLine(cx + dx, cy + dy, cx + dx - dx2, cy + dy - dy2); 144 164 } … … 159 179 * of an arrow if they're needed. 160 180 */ 161 publicOffsetIcon(ImageryOffsetBase offset) {181 OffsetIcon(ImageryOffsetBase offset) { 162 182 isDeprecated = offset.isDeprecated(); 163 183 isCalibration = offset instanceof CalibrationObject; 164 if (offset instanceof ImageryOffset) {184 if (offset instanceof ImageryOffset) { 165 185 background = ImageProvider.get("offset"); 166 double[] ld = getLengthAndDirection((ImageryOffset)offset); 186 double[] ld = getLengthAndDirection((ImageryOffset) offset); 167 187 distance = ld[0]; 168 188 direction = ld[1]; … … 179 199 * Paints the base image and adds to it according to the offset. 180 200 */ 181 public void paintIcon( Component comp, Graphics g, int x, int y ) { 201 @Override 202 public void paintIcon(Component comp, Graphics g, int x, int y) { 182 203 background.paintIcon(comp, g, x, y); 183 204 184 Graphics2D g2 = (Graphics2D)g.create(); 205 Graphics2D g2 = (Graphics2D) g.create(); 185 206 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 186 if (!isCalibration) {207 if (!isCalibration) { 187 208 g2.setColor(Color.black); 188 209 Point c = new Point(x + getIconWidth() / 2, y + getIconHeight() / 2); 189 if (distance < 1e-2) {210 if (distance < 1e-2) { 190 211 // no offset 191 212 g2.fillOval(c.x - 3, c.y - 3, 7, 7); … … 197 218 } 198 219 } 199 if (isDeprecated) {220 if (isDeprecated) { 200 221 // big red X 201 222 g2.setColor(Color.red); … … 207 228 } 208 229 230 @Override 209 231 public int getIconWidth() { 210 232 return background.getIconWidth(); 211 233 } 212 234 235 @Override 213 236 public int getIconHeight() { 214 237 return background.getIconHeight(); … … 223 246 private double direction; 224 247 225 publicDirectionIcon(LatLon to) {248 DirectionIcon(LatLon to) { 226 249 this.to = to; 227 250 } 228 251 229 public void updateIcon( LatLon from) {252 public void updateIcon(LatLon from) { 230 253 distance = from.greatCircleDistance(to); 231 254 direction = to.heading(from); … … 235 258 * Paints the base image and adds to it according to the offset. 236 259 */ 237 public void paintIcon( Component comp, Graphics g, int x, int y ) { 238 Graphics2D g2 = (Graphics2D)g.create(); 260 @Override 261 public void paintIcon(Component comp, Graphics g, int x, int y) { 262 Graphics2D g2 = (Graphics2D) g.create(); 239 263 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 240 264 g2.setColor(Color.black); 241 265 Point c = new Point(x + getIconWidth() / 2, y + getIconHeight() / 2); 242 if (distance < 1) {266 if (distance < 1) { 243 267 // no offset 244 268 int r = 2; … … 251 275 } 252 276 277 @Override 253 278 public int getIconWidth() { 254 279 return SIZE; 255 280 } 256 281 282 @Override 257 283 public int getIconHeight() { 258 284 return SIZE;
Note:
See TracChangeset
for help on using the changeset viewer.
