Ignore:
Timestamp:
2017-04-12T21:24:02+02:00 (8 years ago)
Author:
donvip
Message:

refactor code

Location:
applications/editors/josm/plugins/imagewaypoint/src/org/insignificant/josm/plugins/imagewaypoint
Files:
7 added
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/imagewaypoint/src/org/insignificant/josm/plugins/imagewaypoint/ImageWayPointDialog.java

    r33086 r33228  
    55
    66import java.awt.BorderLayout;
    7 import java.awt.Color;
    8 import java.awt.Graphics;
    9 import java.awt.Image;
    10 import java.awt.event.ActionEvent;
    117import java.awt.event.KeyEvent;
    128
    139import javax.swing.Action;
    1410import javax.swing.JButton;
    15 import javax.swing.JComponent;
    1611import javax.swing.JPanel;
    1712
     13import org.insignificant.josm.plugins.imagewaypoint.actions.NextAction;
     14import org.insignificant.josm.plugins.imagewaypoint.actions.PreviousAction;
     15import org.insignificant.josm.plugins.imagewaypoint.actions.RotateLeftAction;
     16import org.insignificant.josm.plugins.imagewaypoint.actions.RotateRightAction;
    1817import org.openstreetmap.josm.Main;
    19 import org.openstreetmap.josm.actions.JosmAction;
    2018import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
    2119import org.openstreetmap.josm.tools.Shortcut;
    2220
    2321public final class ImageWayPointDialog {
    24     private static final class ImageComponent extends JComponent {
    25         private static final long serialVersionUID = -5207198660736375133L;
    26 
    27         private Image image;
    28 
    29         ImageComponent() {
    30             this.image = null;
    31         }
    32 
    33         @Override
    34         public void paint(final Graphics g) {
    35             if (null == this.image || 0 >= this.image.getWidth(null)
    36                 || 0 >= this.image.getHeight(null)) {
    37             g.setColor(Color.white);
    38             g.fillRect(0, 0, this.getSize().width, this.getSize().height);
    39             } else {
    40             final int maxWidth = this.getSize().width;
    41             final int maxHeight = this.getSize().height;
    42             final int imageWidth = this.image.getWidth(null);
    43             final int imageHeight = this.image.getHeight(null);
    44 
    45             final double aspect = 1.0 * imageWidth / imageHeight;
    46 
    47             // what's the width if the height is 100%?
    48             final int widthIfHeightIsMax = (int) (aspect * maxHeight);
    49 
    50             // now find the real width and height
    51             final int resizedWidth;
    52             final int resizedHeight;
    53             if (widthIfHeightIsMax > maxWidth) {
    54                 // oops - burst the width - so width should be the max, and
    55                 // work out the resulting height
    56                 resizedWidth = maxWidth;
    57                 resizedHeight = (int) (resizedWidth / aspect);
    58             } else {
    59                 // that'll do...
    60                 resizedWidth = widthIfHeightIsMax;
    61                 resizedHeight = maxHeight;
    62             }
    63 
    64             g.drawImage(this.image,
    65                 (maxWidth - resizedWidth) / 2,
    66                 (maxHeight - resizedHeight) / 2,
    67                 resizedWidth,
    68                 resizedHeight,
    69                 Color.black,
    70                 null);
    71             }
    72         }
    73 
    74         public void setImage(final Image image) {
    75             this.image = image;
    76             this.repaint();
    77         }
    78     }
    79 
    80     private static final class ImageChangeListener implements
    81         IImageChangeListener {
    82         private final ImageWayPointDialog dialog;
    83 
    84         ImageChangeListener(final ImageWayPointDialog dialog) {
    85             this.dialog = dialog;
    86         }
    87 
    88         @Override
    89         public void onAvailableImageEntriesChanged(
    90             final ImageEntries entries) {
    91             this.dialog.imageDisplay.setImage(entries.getCurrentImage());
    92             this.dialog.updateUI();
    93         }
    94 
    95         @Override
    96         public void onSelectedImageEntryChanged(final ImageEntries entries) {
    97             this.dialog.imageDisplay.setImage(entries.getCurrentImage());
    98             this.dialog.updateUI();
    99         }
    100     }
    101 
    102     private static final class PreviousAction extends JosmAction {
    103 
    104         PreviousAction() {
    105             super(tr("Previous"),
    106             null,
    107             tr("Previous image"),
    108             null,
    109             false);
    110         }
    111 
    112         @Override
    113         public void actionPerformed(final ActionEvent actionEvent) {
    114             if (ImageEntries.getInstance().hasPrevious()) {
    115                 ImageEntries.getInstance().previous();
    116             }
    117         }
    118     }
    119 
    120     private static final class NextAction extends JosmAction {
    121 
    122         NextAction() {
    123             super(tr("Next"), null, tr("Next image"), null, false);
    124         }
    125 
    126         @Override
    127         public void actionPerformed(final ActionEvent actionEvent) {
    128             if (ImageEntries.getInstance().hasNext()) {
    129                 ImageEntries.getInstance().next();
    130             }
    131         }
    132     }
    133 
    134     private static final class RotateLeftAction extends JosmAction {
    135 
    136         RotateLeftAction() {
    137             super(tr("Rotate left"),
    138             null,
    139             tr("Rotate image left"),
    140             null,
    141             false);
    142         }
    143 
    144         @Override
    145         public void actionPerformed(final ActionEvent actionEvent) {
    146             ImageEntries.getInstance().rotateCurrentImageLeft();
    147         }
    148     }
    149 
    150     private static final class RotateRightAction extends JosmAction {
    151 
    152         RotateRightAction() {
    153             super(tr("Rotate right"),
    154             null,
    155             tr("Rotate image right"),
    156             null,
    157             false);
    158         }
    159 
    160         @Override
    161         public void actionPerformed(final ActionEvent actionEvent) {
    162             ImageEntries.getInstance().rotateCurrentImageRight();
    163         }
    164     }
    165 
    16622    private static final ImageWayPointDialog INSTANCE = new ImageWayPointDialog();
    16723    private final ToggleDialog dialog;
    168     private final ImageComponent imageDisplay;
     24    final ImageComponent imageDisplay;
    16925    private final Action previousAction;
    17026    private final Action nextAction;
     
    17531
    17632    private ImageWayPointDialog() {
    177     this.dialog = new ToggleDialog(tr("WayPoint Image"),
    178         "imagewaypoint",
    179         tr("Display non-geotagged photos"),
    180         Shortcut.registerShortcut("subwindow:imagewaypoint", tr("Toggle: {0}", tr("WayPoint Image")),
    181         KeyEvent.VK_Y, Shortcut.ALT_SHIFT),
    182         200);
     33        this.dialog = new ToggleDialog(tr("WayPoint Image"),
     34            "imagewaypoint",
     35            tr("Display non-geotagged photos"),
     36            Shortcut.registerShortcut("subwindow:imagewaypoint", tr("Toggle: {0}", tr("WayPoint Image")),
     37            KeyEvent.VK_Y, Shortcut.ALT_SHIFT),
     38            200);
    18339
    184     this.previousAction = new PreviousAction();
    185     this.nextAction = new NextAction();
    186     this.rotateLeftAction = new RotateLeftAction();
    187     this.rotateRightAction = new RotateRightAction();
     40        this.previousAction = new PreviousAction();
     41        this.nextAction = new NextAction();
     42        this.rotateLeftAction = new RotateLeftAction();
     43        this.rotateRightAction = new RotateRightAction();
    18844
    189     final JButton previousButton = new JButton(this.previousAction);
    190     final JButton nextButton = new JButton(this.nextAction);
    191     final JButton rotateLeftButton = new JButton(this.rotateLeftAction);
    192     final JButton rotateRightButton = new JButton(this.rotateRightAction);
     45        final JButton previousButton = new JButton(this.previousAction);
     46        final JButton nextButton = new JButton(this.nextAction);
     47        final JButton rotateLeftButton = new JButton(this.rotateLeftAction);
     48        final JButton rotateRightButton = new JButton(this.rotateRightAction);
    19349
    194     // default layout, FlowLayout, is fine
    195     final JPanel buttonPanel = new JPanel();
    196     buttonPanel.add(previousButton);
    197     buttonPanel.add(nextButton);
    198     buttonPanel.add(rotateLeftButton);
    199     buttonPanel.add(rotateRightButton);
     50        // default layout, FlowLayout, is fine
     51        final JPanel buttonPanel = new JPanel();
     52        buttonPanel.add(previousButton);
     53        buttonPanel.add(nextButton);
     54        buttonPanel.add(rotateLeftButton);
     55        buttonPanel.add(rotateRightButton);
    20056
    201     final JPanel mainPanel = new JPanel();
    202     mainPanel.setLayout(new BorderLayout());
     57        final JPanel mainPanel = new JPanel();
     58        mainPanel.setLayout(new BorderLayout());
    20359
    204     this.imageDisplay = new ImageComponent();
    205     mainPanel.add(buttonPanel, BorderLayout.SOUTH);
    206     mainPanel.add(this.imageDisplay, BorderLayout.CENTER);
     60        this.imageDisplay = new ImageComponent();
     61        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
     62        mainPanel.add(this.imageDisplay, BorderLayout.CENTER);
    20763
    208     this.listener = new ImageChangeListener(this);
    209     ImageEntries.getInstance().addListener(this.listener);
     64        this.listener = new ImageChangeListener(this);
     65        ImageEntries.getInstance().addListener(this.listener);
    21066
    211     this.updateUI();
    212     dialog.add(mainPanel);
     67        this.updateUI();
     68        dialog.add(mainPanel);
    21369    }
    21470
    215     private void updateUI() {
     71    void updateUI() {
    21672        this.previousAction.setEnabled(ImageEntries.getInstance().hasPrevious());
    21773        this.nextAction.setEnabled(ImageEntries.getInstance().hasNext());
Note: See TracChangeset for help on using the changeset viewer.