001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.plugins.streetside.utils; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import javax.swing.JOptionPane; 007 008import org.openstreetmap.josm.Main; 009import org.openstreetmap.josm.tools.Logging; 010 011/** 012 * @author nokutu 013 * 014 */ 015public final class PluginState { 016 017 private static boolean submittingChangeset; 018 019 private static int runningDownloads; 020 /** Images that have to be uploaded. */ 021 private static int imagesToUpload; 022 /** Images that have been uploaded. */ 023 private static int imagesUploaded; 024 025 private PluginState() { 026 // Empty constructor to avoid instantiation 027 } 028 029 /** 030 * Called when a download is started. 031 */ 032 public static void startDownload() { 033 runningDownloads++; 034 } 035 036 /** 037 * Called when a download is finished. 038 */ 039 public static void finishDownload() { 040 if (runningDownloads == 0) { 041 Logging.warn("The amount of running downloads is equal to 0"); 042 return; 043 } 044 runningDownloads--; 045 } 046 047 /** 048 * Checks if there is any running download. 049 * 050 * @return true if the plugin is downloading; false otherwise. 051 */ 052 public static boolean isDownloading() { 053 return runningDownloads > 0; 054 } 055 056 /** 057 * Checks if there is a changeset being submitted. 058 * 059 * @return true if the plugin is submitting a changeset false otherwise. 060 */ 061 public static boolean isSubmittingChangeset() { 062 return submittingChangeset; 063 } 064 /** 065 * Checks if there is any running upload. 066 * 067 * @return true if the plugin is uploading; false otherwise. 068 */ 069 public static boolean isUploading() { 070 return imagesToUpload > imagesUploaded; 071 } 072 073 /** 074 * Sets the amount of images that are going to be uploaded. 075 * 076 * @param amount 077 * The amount of images that are going to be uploaded. 078 */ 079 public static void addImagesToUpload(int amount) { 080 if (imagesToUpload <= imagesUploaded) { 081 imagesToUpload = 0; 082 imagesUploaded = 0; 083 } 084 imagesToUpload += amount; 085 } 086 087 public static int getImagesToUpload() { 088 return imagesToUpload; 089 } 090 091 public static int getImagesUploaded() { 092 return imagesUploaded; 093 } 094 095 /** 096 * Called when an image is uploaded. 097 */ 098 public static void imageUploaded() { 099 imagesUploaded++; 100 if (imagesToUpload == imagesUploaded && Main.main != null) { 101 finishedUploadDialog(imagesUploaded); 102 } 103 } 104 105 private static void finishedUploadDialog(int numImages) { 106 JOptionPane.showMessageDialog( 107 Main.parent, 108 tr("You have successfully uploaded {0} images to mapillary.com", numImages), 109 tr("Finished upload"), 110 JOptionPane.INFORMATION_MESSAGE 111 ); 112 } 113 114 public static void notLoggedInToMapillaryDialog() { 115 if (Main.main == null) { 116 return; 117 } 118 JOptionPane.showMessageDialog( 119 Main.parent, 120 tr("You are not logged in, please log in to Mapillary in the preferences"), 121 tr("Not Logged in to Mapillary"), 122 JOptionPane.WARNING_MESSAGE 123 ); 124 } 125 126 127 128 /** 129 * Returns the text to be written in the status bar. 130 * 131 * @return The {@code String} that is going to be written in the status bar. 132 */ 133 public static String getUploadString() { 134 return tr("Uploading: {0}", "(" + imagesUploaded + "/" + imagesToUpload + ")"); 135 } 136 137 public static void setSubmittingChangeset(boolean isSubmitting) { 138 submittingChangeset = isSubmitting; 139 } 140}