Ignore:
Timestamp:
2015-08-05T16:28:33+02:00 (9 years ago)
Author:
nokutu
Message:

Added logout button and improved javadoc

Location:
applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary
Files:
11 edited
1 moved

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/MapillaryData.java

    r31453 r31457  
    22
    33import org.openstreetmap.josm.Main;
    4 import org.openstreetmap.josm.plugins.mapillary.cache.Utils;
     4import org.openstreetmap.josm.plugins.mapillary.cache.CacheUtils;
    55import org.openstreetmap.josm.plugins.mapillary.gui.MapillaryMainDialog;
    66
     
    173173   * following visible MapillaryImage is selected. In case there is none, does
    174174   * nothing.
     175   *
     176   * @throws IllegalStateException
     177   *           if the selected image is null or the selected image doesn't
     178   *           belong to a sequence.
    175179   */
    176180  public void selectNext() {
    177     if (getSelectedImage() == null)
    178       return;
    179     if (getSelectedImage().getSequence() == null)
    180       return;
    181     MapillaryAbstractImage tempImage = this.selectedImage;
    182     while (tempImage.next() != null) {
    183       tempImage = tempImage.next();
    184       if (tempImage.isVisible()) {
    185         setSelectedImage(tempImage,
    186             Main.pref.getBoolean("mapillary.move-to-picture", true));
    187         break;
    188       }
    189     }
     181    selectNext(Main.pref.getBoolean("mapillary.move-to-picture", true));
    190182  }
    191183
     
    197189   * @param moveToPicture
    198190   *          True if the view must me moved to the next picture.
     191   * @throws IllegalStateException
     192   *           if the selected image is null or the selected image doesn't
     193   *           belong to a sequence.
    199194   */
    200195  public void selectNext(boolean moveToPicture) {
    201196    if (getSelectedImage() == null)
    202       return;
     197      throw new IllegalStateException();
    203198    if (getSelectedImage().getSequence() == null)
    204       return;
     199      throw new IllegalStateException();
    205200    MapillaryAbstractImage tempImage = this.selectedImage;
    206201    while (tempImage.next() != null) {
     
    217212   * previous visible MapillaryImage is selected. In case there is none, does
    218213   * nothing.
     214   *
     215   * @throws IllegalStateException
     216   *           if the selected image is null or the selected image doesn't
     217   *           belong to a sequence.
    219218   */
    220219  public void selectPrevious() {
    221     if (getSelectedImage() == null)
    222       return;
    223     if (getSelectedImage().getSequence() == null)
    224       throw new IllegalStateException();
    225     MapillaryAbstractImage tempImage = this.selectedImage;
    226     while (tempImage.previous() != null) {
    227       tempImage = tempImage.previous();
    228       if (tempImage.isVisible()) {
    229         setSelectedImage(tempImage,
    230             Main.pref.getBoolean("mapillary.move-to-picture", true));
    231         break;
    232       }
    233     }
     220    selectPrevious(Main.pref.getBoolean("mapillary.move-to-picture", true));
    234221  }
    235222
     
    237224   * If the selected MapillaryImage is part of a MapillarySequence then the
    238225   * previous visible MapillaryImage is selected. In case there is none, does
    239    * nothing.
     226   * nothing. * @throws IllegalStateException if the selected image is null or
     227   * the selected image doesn't belong to a sequence.
    240228   *
    241229   * @param moveToPicture
    242230   *          True if the view must me moved to the previous picture.
     231   * @throws IllegalStateException
     232   *           if the selected image is null or the selected image doesn't
     233   *           belong to a sequence.
    243234   */
    244235  public void selectPrevious(boolean moveToPicture) {
     
    262253   * @param image
    263254   *          The MapillaryImage which is going to be selected
     255   *
    264256   */
    265257  public void setSelectedImage(MapillaryAbstractImage image) {
     
    286278        // Downloading thumbnails of surrounding pictures.
    287279        if (mapillaryImage.next() != null) {
    288           Utils.downloadPicture((MapillaryImage) mapillaryImage.next());
     280          CacheUtils.downloadPicture((MapillaryImage) mapillaryImage.next());
    289281          if (mapillaryImage.next().next() != null)
    290             Utils
     282            CacheUtils
    291283                .downloadPicture((MapillaryImage) mapillaryImage.next().next());
    292284        }
    293285        if (mapillaryImage.previous() != null) {
    294           Utils.downloadPicture((MapillaryImage) mapillaryImage.previous());
     286          CacheUtils.downloadPicture((MapillaryImage) mapillaryImage.previous());
    295287          if (mapillaryImage.previous().previous() != null)
    296             Utils.downloadPicture((MapillaryImage) mapillaryImage.previous()
     288            CacheUtils.downloadPicture((MapillaryImage) mapillaryImage.previous()
    297289                .previous());
    298290        }
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/MapillaryImportedImage.java

    r31445 r31457  
    6969   */
    7070  public BufferedImage getImage() throws IOException {
    71     return this.file == null ? null : ImageIO.read(this.file);
     71    return ImageIO.read(this.file);
    7272  }
    7373
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/MapillaryLayer.java

    r31456 r31457  
    44import static org.openstreetmap.josm.tools.I18n.marktr;
    55
    6 import org.openstreetmap.josm.plugins.mapillary.cache.Utils;
     6import org.openstreetmap.josm.plugins.mapillary.cache.CacheUtils;
    77import org.openstreetmap.josm.plugins.mapillary.downloads.MapillaryDownloader;
    88import org.openstreetmap.josm.plugins.mapillary.gui.MapillaryFilterDialog;
     
    278278    if (Main.map.mapView.getActiveLayer() == this) {
    279279      Rectangle b = mv.getBounds();
    280       // on some platforms viewport bounds seem to be offset from the
    281       // left,
     280      // on some platforms viewport bounds seem to be offset from the left,
    282281      // over-grow it just to be sure
    283282      b.grow(100, 100);
     
    486485    // Predownloads the thumbnails
    487486    if (ret[0] != null)
    488       Utils.downloadPicture(ret[0]);
     487      CacheUtils.downloadPicture(ret[0]);
    489488    if (ret[1] != null)
    490       Utils.downloadPicture(ret[1]);
     489      CacheUtils.downloadPicture(ret[1]);
    491490    return ret;
    492491  }
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/MapillaryPlugin.java

    r31452 r31457  
    2020import org.openstreetmap.josm.plugins.mapillary.gui.MapillaryPreferenceSetting;
    2121import org.openstreetmap.josm.plugins.mapillary.gui.MapillaryMainDialog;
     22import org.openstreetmap.josm.plugins.mapillary.oauth.MapillaryUser;
    2223import org.openstreetmap.josm.plugins.mapillary.actions.*;
    2324import org.openstreetmap.josm.tools.ImageProvider;
     
    142143      Main.error(e);
    143144    }
     145
     146    if (Main.pref.get("mapillary.access-token") == null)
     147      MapillaryUser.isTokenValid = false;
    144148  }
    145149
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/MapillarySequence.java

    r31445 r31457  
    107107   *          going to be returned.
    108108   * @return The next {@link MapillaryAbstractImage} object in the sequence.
     109   * @throws IllegalArgumentException
     110   *           if the given {@link MapillaryAbstractImage} object doesn't belong
     111   *           the this sequence.
    109112   */
    110113  public MapillaryAbstractImage next(MapillaryAbstractImage image) {
     
    124127   *          going to be returned.
    125128   * @return The previous {@link MapillaryAbstractImage} object in the sequence.
     129   * @throws IllegalArgumentException
     130   *           if the given {@link MapillaryAbstractImage} object doesn't belong
     131   *           the this sequence.
    126132   */
    127133  public MapillaryAbstractImage previous(MapillaryAbstractImage image) {
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/actions/WalkThread.java

    r31452 r31457  
    1212import org.openstreetmap.josm.plugins.mapillary.MapillaryImage;
    1313import org.openstreetmap.josm.plugins.mapillary.MapillaryLayer;
    14 import org.openstreetmap.josm.plugins.mapillary.cache.Utils;
     14import org.openstreetmap.josm.plugins.mapillary.cache.CacheUtils;
    1515import org.openstreetmap.josm.plugins.mapillary.gui.MapillaryMainDialog;
    1616
     
    6666              break;
    6767            image = image.next();
    68             Utils.downloadPicture((MapillaryImage) image,
    69                 Utils.PICTURE.THUMBNAIL);
     68            CacheUtils.downloadPicture((MapillaryImage) image,
     69                CacheUtils.PICTURE.THUMBNAIL);
    7070          }
    7171          if (this.waitForFullQuality)
     
    7575                break;
    7676              image = image.next();
    77               Utils.downloadPicture((MapillaryImage) image,
    78                   Utils.PICTURE.FULL_IMAGE);
     77              CacheUtils.downloadPicture((MapillaryImage) image,
     78                  CacheUtils.PICTURE.FULL_IMAGE);
    7979            }
    8080        }
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/cache/CacheUtils.java

    r31456 r31457  
    1212 *
    1313 */
    14 public class Utils {
     14public class CacheUtils {
    1515
    1616  private static IgnoreDownload IGNORE_DOWNLOAD = new IgnoreDownload();
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/gui/MapillaryMainDialog.java

    r31454 r31457  
    269269  }
    270270
     271  /** Disables all the buttons in the dialog */
    271272  private void disableAllButtons() {
    272273    this.nextButton.setEnabled(false);
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/gui/MapillaryPreferenceSetting.java

    r31445 r31457  
    77import java.awt.event.ActionEvent;
    88import java.io.IOException;
    9 import java.net.MalformedURLException;
    109import java.net.URI;
    1110import java.net.URISyntaxException;
     
    4443  private JCheckBox moveTo = new JCheckBox(
    4544      tr("Move to picture's location with next/previous buttons"));
     45  private JButton login;
    4646
    4747  @Override
     
    5656    this.reverseButtons.setSelected(Main.pref
    5757        .getBoolean("mapillary.reverse-buttons"));
    58     this.displayHour.setSelected(Main.pref
    59         .getBoolean("mapillary.display-hour", true));
     58    this.displayHour.setSelected(Main.pref.getBoolean("mapillary.display-hour",
     59        true));
    6060    this.format24.setSelected(Main.pref.getBoolean("mapillary.format-24"));
    61     this.moveTo.setSelected(Main.pref.getBoolean("mapillary.move-to-picture", true));
     61    this.moveTo.setSelected(Main.pref.getBoolean("mapillary.move-to-picture",
     62        true));
    6263
    6364    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
     
    8283    panel.add(this.format24);
    8384    panel.add(this.moveTo);
    84     JButton oauth = new JButton(new OAuthAction());
     85    this.login = new JButton(new LoginAction());
    8586    if (Main.pref.get("mapillary.access-token") == null)
    86       oauth.setText("Login");
     87      this.login.setText("Login");
    8788    else
    88       try {
    89         oauth.setText("Logged as: " + MapillaryUser.getUsername() + ". Click to relogin.");
    90       } catch (MalformedURLException e) {
    91         // TODO Auto-generated catch block
    92         e.printStackTrace();
    93       } catch (IOException e) {
    94         // TODO Auto-generated catch block
    95         e.printStackTrace();
    96       }
    97     panel.add(oauth);
     89      this.login.setText("Logged as: " + MapillaryUser.getUsername()
     90          + ". Click to relogin.");
     91
     92    panel.add(this.login);
     93    if (MapillaryUser.getUsername() != null) {
     94      JButton logout = new JButton(new LogoutAction());
     95      logout.setText("Logout");
     96
     97      panel.add(logout);
     98    }
    9899    gui.getDisplayPreference().addSubTab(this, "Mapillary", panel);
    99100  }
     
    102103  public boolean ok() {
    103104    boolean mod = false;
    104     Main.pref.put("mapillary.reverse-buttons", this.reverseButtons.isSelected());
     105    Main.pref
     106        .put("mapillary.reverse-buttons", this.reverseButtons.isSelected());
    105107
    106108    MapillaryPlugin.setMenuEnabled(MapillaryPlugin.DOWNLOAD_VIEW_MENU, false);
    107     if (this.downloadMode.getSelectedItem().equals(MapillaryDownloader.MODES[0]))
     109    if (this.downloadMode.getSelectedItem()
     110        .equals(MapillaryDownloader.MODES[0]))
    108111      Main.pref.put("mapillary.download-mode", MapillaryDownloader.MODES[0]);
    109     if (this.downloadMode.getSelectedItem().equals(MapillaryDownloader.MODES[1]))
     112    if (this.downloadMode.getSelectedItem()
     113        .equals(MapillaryDownloader.MODES[1]))
    110114      Main.pref.put("mapillary.download-mode", MapillaryDownloader.MODES[1]);
    111     if (this.downloadMode.getSelectedItem().equals(MapillaryDownloader.MODES[2])) {
     115    if (this.downloadMode.getSelectedItem()
     116        .equals(MapillaryDownloader.MODES[2])) {
    112117      Main.pref.put("mapillary.download-mode", MapillaryDownloader.MODES[2]);
    113118      MapillaryPlugin.setMenuEnabled(MapillaryPlugin.DOWNLOAD_VIEW_MENU, true);
     
    131136   *
    132137   */
    133   public class OAuthAction extends AbstractAction {
     138  public class LoginAction extends AbstractAction {
    134139
    135140    private static final long serialVersionUID = -3908477563072057344L;
     
    158163    }
    159164  }
     165
     166  /**
     167   * Logs the user out.
     168   *
     169   * @author nokutu
     170   *
     171   */
     172  public class LogoutAction extends AbstractAction {
     173
     174    private static final long serialVersionUID = 3434780936404707219L;
     175
     176    @Override
     177    public void actionPerformed(ActionEvent arg0) {
     178      MapillaryUser.reset();
     179      Main.pref.put("mapillary.access-token", null);
     180      MapillaryPreferenceSetting.this.login.setText("Login");
     181    }
     182  }
     183
    160184}
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/oauth/MapillaryUser.java

    r31445 r31457  
    22
    33import java.io.IOException;
    4 import java.net.MalformedURLException;
    54import java.net.URL;
    65import java.util.HashMap;
     6
     7import org.openstreetmap.josm.Main;
    78
    89/**
     
    1516  private static String images_policy;
    1617  private static String images_hash;
     18  /** If the stored token is valid or not. */
     19  public static boolean isTokenValid = true;
    1720
    1821  /**
     
    2023   *
    2124   * @return The username of the logged in user.
    22    * @throws MalformedURLException
    23    * @throws IOException
    2425   */
    25   public static String getUsername() throws MalformedURLException, IOException {
     26  public static String getUsername() {
     27    if (!isTokenValid)
     28      return null;
    2629    if (username == null)
    27       username = OAuthUtils
    28           .getWithHeader(
    29               new URL(
    30                   "https://a.mapillary.com/v2/me?client_id=T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz"))
    31           .getString("username");
     30      try {
     31        username = OAuthUtils
     32            .getWithHeader(
     33                new URL(
     34                    "https://a.mapillary.com/v2/me?client_id=T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz"))
     35            .getString("username");
     36      } catch (IOException e) {
     37        Main.error(e);
     38        isTokenValid = false;
     39      }
    3240
    3341    return username;
     
    3745   * @return A HashMap object containing the images_policy and images_hash
    3846   *         strings.
    39    * @throws MalformedURLException
    40    * @throws IOException
    4147   */
    42   public static HashMap<String, String> getSecrets()
    43       throws MalformedURLException, IOException {
     48  public static HashMap<String, String> getSecrets() {
     49    if (!isTokenValid)
     50      return null;
    4451    HashMap<String, String> hash = new HashMap<>();
    4552    if (images_hash == null)
    46       images_hash = OAuthUtils
    47           .getWithHeader(
    48               new URL(
    49                   "https://a.mapillary.com/v2/me/uploads/secrets?client_id=T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz"))
    50           .getString("images_hash");
     53      try {
     54        images_hash = OAuthUtils
     55            .getWithHeader(
     56                new URL(
     57                    "https://a.mapillary.com/v2/me/uploads/secrets?client_id=T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz"))
     58            .getString("images_hash");
     59      } catch (IOException e) {
     60        Main.error(e);
     61        isTokenValid = false;
     62        isTokenValid = false;
     63      }
    5164    hash.put("images_hash", images_hash);
    5265    if (images_policy == null)
    53       images_policy = OAuthUtils
    54           .getWithHeader(
    55               new URL(
    56                   "https://a.mapillary.com/v2/me/uploads/secrets?client_id=T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz"))
    57           .getString("images_policy");
     66      try {
     67        images_policy = OAuthUtils
     68            .getWithHeader(
     69                new URL(
     70                    "https://a.mapillary.com/v2/me/uploads/secrets?client_id=T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz"))
     71            .getString("images_policy");
     72      } catch (IOException e) {
     73        Main.error(e);
     74        isTokenValid = false;
     75      }
    5876    hash.put("images_policy", images_policy);
    5977    return hash;
     
    6785    images_policy = null;
    6886    images_hash = null;
     87    isTokenValid = true;
    6988  }
    7089
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/oauth/UploadUtils.java

    r31456 r31457  
    77import java.io.IOException;
    88import java.io.OutputStream;
    9 import java.io.UnsupportedEncodingException;
    10 import java.security.InvalidKeyException;
    11 import java.security.NoSuchAlgorithmException;
    129import java.util.HashMap;
    1310import java.util.List;
     
    5552public class UploadUtils {
    5653
     54  /** Required keys for POST */
    5755  private static final String[] keys = { "key", "AWSAccessKeyId", "acl",
    5856      "policy", "signature", "Content-Type" };
     57  /** Mapillary upload URL */
    5958  private static final String UPLOAD_URL = "https://s3-eu-west-1.amazonaws.com/mapillary.uploads.manual.images";
    6059  /** Count to name temporal files. */
     
    6665   * @param image
    6766   *
    68    * @throws NoSuchAlgorithmException
    69    * @throws UnsupportedEncodingException
    70    * @throws InvalidKeyException
    71    *
    72    */
    73   public static void upload(MapillaryImportedImage image)
    74       throws InvalidKeyException, UnsupportedEncodingException,
    75       NoSuchAlgorithmException {
    76     try {
    77       upload(image, UUID.randomUUID());
    78     } catch (IOException e) {
    79       Main.error(e);
    80     }
     67   */
     68  public static void upload(MapillaryImportedImage image) {
     69    upload(image, UUID.randomUUID());
     70
    8171  }
    8272
     
    8575   * @param uuid
    8676   *          The UUID used to create the sequence.
    87    * @throws NoSuchAlgorithmException
    88    * @throws InvalidKeyException
    89    * @throws IOException
    90    */
    91   public static void upload(MapillaryImportedImage image, UUID uuid)
    92       throws NoSuchAlgorithmException, InvalidKeyException, IOException {
     77   */
     78  public static void upload(MapillaryImportedImage image, UUID uuid) {
    9379    String key = MapillaryUser.getUsername() + "/" + uuid.toString() + "/"
    9480        + image.getLatLon().lat() + "_" + image.getLatLon().lon() + "_"
     
    11096    try {
    11197      uploadFile(updateFile(image), hash);
    112     } catch (ImageReadException | ImageWriteException e) {
     98    } catch (ImageReadException | ImageWriteException | IOException e) {
    11399      Main.error(e);
    114100    }
    115 
    116101  }
    117102
     
    120105   * @param hash
    121106   * @throws IOException
     107   * @throws IllegalArgumentException
     108   *           if the hash doesn't contain all the needed keys.
    122109   */
    123110  public static void uploadFile(File file, HashMap<String, String> hash)
     
    129116    MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
    130117    for (String key : keys) {
     118      if (hash.get(key) == null)
     119        throw new IllegalArgumentException();
    131120      entityBuilder.addPart(key, new StringBody(hash.get(key),
    132121          ContentType.TEXT_PLAIN));
     
    153142   */
    154143  public static void uploadSequence(MapillarySequence sequence) {
    155     new SequenceUploadThread(sequence.getImages()).start();
     144    Main.worker.submit(new SequenceUploadThread(sequence.getImages()));
    156145  }
    157146
     
    197186    @Override
    198187    public void run() {
    199       try {
    200         upload(this.image, this.uuid);
    201       } catch (InvalidKeyException | NoSuchAlgorithmException | IOException e) {
    202         Main.error(e);
    203       }
     188      upload(this.image, this.uuid);
     189
    204190    }
    205191  }
     
    213199   *         EXIF tags.
    214200   * @throws ImageReadException
     201   *           if there are errors reading the image from the file.
    215202   * @throws IOException
     203   *           if there are errors getting the metadata from the file or writing
     204   *           the output.
    216205   * @throws ImageWriteException
     206   *           if there are errors writing the image in the file.
    217207   */
    218208  public static File updateFile(MapillaryImportedImage image)
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/utils/MapillaryUtils.java

    r31456 r31457  
    6161   *           not one of the values mentioned above
    6262   */
    63   // TODO: Maybe move into a separate utility class?
    6463  public static double degMinSecToDouble(RationalNumber[] degMinSec, String ref) {
    6564    if (degMinSec == null || degMinSec.length != 3) {
Note: See TracChangeset for help on using the changeset viewer.