Changeset 29386 in osm


Ignore:
Timestamp:
2013-03-23T09:45:46+01:00 (12 years ago)
Author:
zverik
Message:

offset button redesign

Location:
applications/editors/josm/plugins/imagery_offset_db/src/iodb
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/imagery_offset_db/src/iodb/OffsetDialog.java

    r29384 r29386  
    192192     */
    193193    public ImageryOffsetBase showDialog() {
    194         // todo: add a temporary layer showing all offsets
    195194        selectedOffset = null;
    196195        prepareDialog();
  • applications/editors/josm/plugins/imagery_offset_db/src/iodb/OffsetDialogButton.java

    r29384 r29386  
    22
    33import java.awt.*;
    4 import javax.swing.Icon;
    5 import javax.swing.ImageIcon;
    6 import javax.swing.JButton;
     4import javax.swing.*;
    75import org.openstreetmap.josm.Main;
    86import org.openstreetmap.josm.data.coor.EastNorth;
     
    119import org.openstreetmap.josm.gui.layer.ImageryLayer;
    1210import org.openstreetmap.josm.tools.ImageProvider;
     11import static org.openstreetmap.josm.tools.I18n.tr;
    1312
    1413/**
     
    2221   
    2322    private ImageryOffsetBase offset;
    24     private double offsetLength;
    25     private double distance;
    26     private double direction;
     23
     24    private JLabel distanceLabel;
     25    private DirectionIcon directionArrow;
    2726
    2827    /**
     
    3130     */
    3231    public OffsetDialogButton( ImageryOffsetBase offset ) {
    33         super();
    3432        this.offset = offset;
    35 //        setMinimumSize(new Dimension(500, 10));
    36 //        setMaximumSize(new Dimension(500, 150));
    37         setRelevantText();
    38         setIcon(new OffsetIcon(offset));
    39 
    40         offsetLength = offset instanceof ImageryOffset ? getLengthAndDirection((ImageryOffset)offset)[0] : 0.0;
    41         // todo: layout, info, map distance and direction
    42         // http://stackoverflow.com/questions/1048224/get-height-of-multi-line-text-with-fixed-width-to-make-dialog-resize-properly
    43         // http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html#size
    44         // http://stackoverflow.com/questions/8012646/setting-size-of-jbutton-inside-jpanel-with-boxlayout-doesnt-work-as-expected
    45         // http://blog.nobel-joergensen.com/2009/01/18/changing-preferred-size-of-a-html-jlabel/
    46         // http://thebadprogrammer.com/swing-layout-manager-sizing/
    47         // http://stackoverflow.com/questions/3692987/why-will-boxlayout-not-allow-me-to-change-the-width-of-a-jbutton-but-let-me-chan
    48 
    49         // http://stackoverflow.com/questions/2158/creating-a-custom-button-in-java
    50     }
    51 
    52     /**
    53      * Update text on the button. This method is to be deleted by release.
    54      */
    55     public void setRelevantText() {
    56         setText("<html><div style=\"width: 400px;\">"
    57                 + ImageryOffsetTools.formatDistance(offset.getPosition().greatCircleDistance(ImageryOffsetTools.getMapCenter()))
    58                 + ": " + offset.getDescription() + "</div></html>");
     33        layoutComponents();
    5934    }
    6035
     
    6641    }
    6742
    68 /*    @Override
    69     public Dimension getPreferredSize() {
    70         Dimension size = super.getPreferredSize();
    71         size.width = 500;
    72         size.height = 70;
    73         return size;
    74     }*/
    75 
    7643    /**
    7744     * Update arrow for the offset location.
    7845     */
    7946    public void updateLocation() {
    80         // map was moved, update arrow.
    81         setRelevantText();
     47        LatLon center = ImageryOffsetTools.getMapCenter();
     48        directionArrow.updateIcon(center);
     49        double distance = center.greatCircleDistance(offset.getPosition());
     50        distanceLabel.setText(ImageryOffsetTools.formatDistance(distance));
     51    }
     52
     53    /**
     54     * Adds a layout to this button and places labels, images and icons.
     55     */
     56    private void layoutComponents() {
     57        String authorAndDate = offset.isDeprecated()
     58                ? tr("Deprecated by {0} on {1}\n", offset.getAbandonAuthor(),
     59                OffsetInfoAction.DATE_FORMAT.format(offset.getAbandonDate()))
     60                : tr("Created by {0} on {1}\n", 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        JLabel descriptionLabel = new JLabel("<html><div style=\"width: 300px;\">"+description+"</div></html>");
     75
     76        double offsetDistance = offset instanceof ImageryOffset
     77                ? ((ImageryOffset)offset).getImageryPos().greatCircleDistance(offset.getPosition()) : 0.0;
     78        JLabel offsetLabel = new JLabel(offsetDistance > 1 ? ImageryOffsetTools.formatDistance(offsetDistance) : "",
     79                new OffsetIcon(offset), SwingConstants.CENTER);
     80        Font offsetFont = new Font(offsetLabel.getFont().getName(), Font.PLAIN, offsetLabel.getFont().getSize() - 2);
     81        offsetLabel.setFont(offsetFont);
     82        offsetLabel.setHorizontalTextPosition(SwingConstants.CENTER);
     83        offsetLabel.setVerticalTextPosition(SwingConstants.BOTTOM);
     84
     85        Box topLine = new Box(BoxLayout.X_AXIS);
     86        topLine.add(authorAndDateLabel);
     87        topLine.add(Box.createHorizontalGlue());
     88        topLine.add(distanceLabel);
     89
     90        JPanel p = new JPanel(new BorderLayout(10, 5));
     91        p.setOpaque(false);
     92        p.add(topLine, BorderLayout.NORTH);
     93        p.add(offsetLabel, BorderLayout.WEST);
     94        p.add(descriptionLabel, BorderLayout.CENTER);
     95        add(p);
    8296    }
    8397
     
    104118        double length = correctedCenterLL.greatCircleDistance(offset.getImageryPos());
    105119        double direction = length < 1e-2 ? 0.0 : correctedCenterLL.heading(offset.getImageryPos());
    106         // todo: north vs south. Meanwhile, let's fix this dirty:
    107 //        direction = Math.PI - direction;
    108120        if( direction < 0 )
    109121            direction += Math.PI * 2;
     
    111123    }
    112124
     125    private static void drawArrow( Graphics g, int cx, int cy, double length, double direction ) {
     126        int dx = (int)Math.round(Math.sin(direction) * length / 2);
     127        int dy = (int)Math.round(Math.cos(direction) * length / 2);
     128        g.drawLine(cx - dx, cy - dy, cx + dx, cy + dy);
     129        double wingLength = Math.max(length / 3, 4);
     130        double d1 = direction - Math.PI / 6;
     131        int dx1 = (int)Math.round(Math.sin(d1) * wingLength);
     132        int dy1 = (int)Math.round(Math.cos(d1) * wingLength);
     133        g.drawLine(cx + dx, cy + dy, cx + dx - dx1, cy + dy - dy1);
     134        double d2 = direction + Math.PI / 6;
     135        int dx2 = (int)Math.round(Math.sin(d2) * wingLength);
     136        int dy2 = (int)Math.round(Math.cos(d2) * wingLength);
     137        g.drawLine(cx + dx, cy + dy, cx + dx - dx2, cy + dy - dy2);
     138    }
     139
    113140    /**
    114141     * An offset icon. Displays a plain calibration icon for a geometry
    115142     * and an arrow for an imagery offset.
    116143     */
    117     class OffsetIcon implements Icon {
     144    private class OffsetIcon implements Icon {
    118145        private boolean isDeprecated;
    119146        private boolean isCalibration;
     
    131158            if( offset instanceof ImageryOffset ) {
    132159                background = ImageProvider.get("offset");
    133                 ImageryLayer layer = ImageryOffsetTools.getTopImageryLayer();
    134                 double[] dxy = layer == null ? new double[] {0.0, 0.0} : new double[] { layer.getDx(), layer.getDy() };
    135                 double[] ld = getLengthAndDirection((ImageryOffset)offset, dxy[0], dxy[1]);
     160                double[] ld = getLengthAndDirection((ImageryOffset)offset);
    136161                length = ld[0];
    137162                direction = ld[1];
     
    159184                    double arrowLength = length < 10 ? getIconWidth() / 2 - 1 : getIconWidth() - 4;
    160185                    g2.setStroke(new BasicStroke(2));
    161                     int dx = (int)Math.round(Math.sin(direction) * arrowLength / 2);
    162                     int dy = (int)Math.round(Math.cos(direction) * arrowLength / 2);
    163                     g2.drawLine(c.x - dx, c.y - dy, c.x + dx, c.y + dy);
    164                     double wingLength = arrowLength / 3;
    165                     double d1 = direction - Math.PI / 6;
    166                     int dx1 = (int)Math.round(Math.sin(d1) * wingLength);
    167                     int dy1 = (int)Math.round(Math.cos(d1) * wingLength);
    168                     g2.drawLine(c.x + dx, c.y + dy, c.x + dx - dx1, c.y + dy - dy1);
    169                     double d2 = direction + Math.PI / 6;
    170                     int dx2 = (int)Math.round(Math.sin(d2) * wingLength);
    171                     int dy2 = (int)Math.round(Math.cos(d2) * wingLength);
    172                     g2.drawLine(c.x + dx, c.y + dy, c.x + dx - dx2, c.y + dy - dy2);
     186                    drawArrow(g2, c.x, c.y, arrowLength, direction);
    173187                }
    174188            }
     
    191205        }
    192206    }
     207
     208    private class DirectionIcon implements Icon {
     209        private static final int SIZE = 10;
     210
     211        private LatLon to;
     212        private double distance;
     213        private double direction;
     214
     215        public DirectionIcon( LatLon to ) {
     216            this.to = to;
     217        }
     218
     219        public void updateIcon( LatLon from ) {
     220            distance = from.greatCircleDistance(to);
     221            direction = to.heading(from);
     222        }
     223
     224        /**
     225         * Paints the base image and adds to it according to the offset.
     226         */
     227        public void paintIcon( Component comp, Graphics g, int x, int y ) {
     228            Graphics2D g2 = (Graphics2D)g.create();
     229            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     230            g2.setColor(Color.black);
     231            Point c = new Point(x + getIconWidth() / 2, y + getIconHeight() / 2);
     232            if( distance < 1 ) {
     233                // no offset
     234                int r = 2;
     235                g2.fillOval(c.x - r, c.y - r, r * 2 + 1, r * 2 + 1);
     236            } else {
     237                // draw an arrow
     238                g2.setStroke(new BasicStroke(1));
     239                drawArrow(g2, c.x, c.y, SIZE, direction);
     240            }
     241        }
     242
     243        public int getIconWidth() {
     244            return SIZE;
     245        }
     246
     247        public int getIconHeight() {
     248            return SIZE;
     249        }
     250    }
    193251}
  • applications/editors/josm/plugins/imagery_offset_db/src/iodb/OffsetInfoAction.java

    r29384 r29386  
    5858       
    5959        sb.append('\n').append('\n');
    60         sb.append("Created by ").append(offset.getAuthor());
    61         sb.append(" on ").append(DATE_FORMAT.format(offset.getDate())).append('\n');
     60        sb.append(tr("Created by {0} on {1}\n", offset.getAuthor(),
     61                DATE_FORMAT.format(offset.getDate()))).append('\n');
    6262        sb.append("Description: ").append(offset.getDescription());
    6363       
    6464        if( offset.isDeprecated() ) {
    6565            sb.append('\n').append('\n');
    66             sb.append("This geometry was marked obsolete").append('\n');
    67             sb.append("by ").append(offset.getAbandonAuthor());
    68             sb.append(" on ").append(DATE_FORMAT.format(offset.getAbandonDate())).append('\n');
     66            sb.append(tr("Deprecated by {0} on {1}\n",offset.getAbandonAuthor(),
     67                    DATE_FORMAT.format(offset.getAbandonDate()))).append('\n');
    6968            sb.append("Reason: ").append(offset.getAbandonReason());
    7069        }
Note: See TracChangeset for help on using the changeset viewer.