Changeset 33228 in osm for applications/editors/josm/plugins
- Timestamp:
- 2017-04-12T21:24:02+02:00 (8 years ago)
- 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 5 5 6 6 import java.awt.BorderLayout; 7 import java.awt.Color;8 import java.awt.Graphics;9 import java.awt.Image;10 import java.awt.event.ActionEvent;11 7 import java.awt.event.KeyEvent; 12 8 13 9 import javax.swing.Action; 14 10 import javax.swing.JButton; 15 import javax.swing.JComponent;16 11 import javax.swing.JPanel; 17 12 13 import org.insignificant.josm.plugins.imagewaypoint.actions.NextAction; 14 import org.insignificant.josm.plugins.imagewaypoint.actions.PreviousAction; 15 import org.insignificant.josm.plugins.imagewaypoint.actions.RotateLeftAction; 16 import org.insignificant.josm.plugins.imagewaypoint.actions.RotateRightAction; 18 17 import org.openstreetmap.josm.Main; 19 import org.openstreetmap.josm.actions.JosmAction;20 18 import org.openstreetmap.josm.gui.dialogs.ToggleDialog; 21 19 import org.openstreetmap.josm.tools.Shortcut; 22 20 23 21 public 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 @Override34 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 height51 final int resizedWidth;52 final int resizedHeight;53 if (widthIfHeightIsMax > maxWidth) {54 // oops - burst the width - so width should be the max, and55 // work out the resulting height56 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 implements81 IImageChangeListener {82 private final ImageWayPointDialog dialog;83 84 ImageChangeListener(final ImageWayPointDialog dialog) {85 this.dialog = dialog;86 }87 88 @Override89 public void onAvailableImageEntriesChanged(90 final ImageEntries entries) {91 this.dialog.imageDisplay.setImage(entries.getCurrentImage());92 this.dialog.updateUI();93 }94 95 @Override96 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 @Override113 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 @Override127 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 @Override145 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 @Override161 public void actionPerformed(final ActionEvent actionEvent) {162 ImageEntries.getInstance().rotateCurrentImageRight();163 }164 }165 166 22 private static final ImageWayPointDialog INSTANCE = new ImageWayPointDialog(); 167 23 private final ToggleDialog dialog; 168 privatefinal ImageComponent imageDisplay;24 final ImageComponent imageDisplay; 169 25 private final Action previousAction; 170 26 private final Action nextAction; … … 175 31 176 32 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); 183 39 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(); 188 44 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); 193 49 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); 200 56 201 final JPanel mainPanel = new JPanel(); 202 mainPanel.setLayout(new BorderLayout()); 57 final JPanel mainPanel = new JPanel(); 58 mainPanel.setLayout(new BorderLayout()); 203 59 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); 207 63 208 this.listener = new ImageChangeListener(this); 209 ImageEntries.getInstance().addListener(this.listener); 64 this.listener = new ImageChangeListener(this); 65 ImageEntries.getInstance().addListener(this.listener); 210 66 211 this.updateUI(); 212 dialog.add(mainPanel); 67 this.updateUI(); 68 dialog.add(mainPanel); 213 69 } 214 70 215 privatevoid updateUI() {71 void updateUI() { 216 72 this.previousAction.setEnabled(ImageEntries.getInstance().hasPrevious()); 217 73 this.nextAction.setEnabled(ImageEntries.getInstance().hasNext());
Note:
See TracChangeset
for help on using the changeset viewer.