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