source: osm/applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/MapillaryPlugin.java@ 31799

Last change on this file since 31799 was 31799, checked in by floscher, 10 years ago

[mapillary] More sonar issues resolved, some tests added and v1.1.0 released

File size: 10.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.plugins.mapillary;
3
4import java.io.IOException;
5
6import javax.swing.ImageIcon;
7import javax.swing.JMenuItem;
8import javax.swing.SwingUtilities;
9
10import org.apache.commons.jcs.access.CacheAccess;
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
13import org.openstreetmap.josm.data.cache.JCSCacheManager;
14import org.openstreetmap.josm.gui.MainMenu;
15import org.openstreetmap.josm.gui.MapFrame;
16import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
17import org.openstreetmap.josm.plugins.Plugin;
18import org.openstreetmap.josm.plugins.PluginInformation;
19import org.openstreetmap.josm.plugins.mapillary.actions.MapillaryDownloadAction;
20import org.openstreetmap.josm.plugins.mapillary.actions.MapillaryDownloadViewAction;
21import org.openstreetmap.josm.plugins.mapillary.actions.MapillaryExportAction;
22import org.openstreetmap.josm.plugins.mapillary.actions.MapillaryImportAction;
23import org.openstreetmap.josm.plugins.mapillary.actions.MapillaryImportIntoSequenceAction;
24import org.openstreetmap.josm.plugins.mapillary.actions.MapillaryJoinAction;
25import org.openstreetmap.josm.plugins.mapillary.actions.MapillaryUploadAction;
26import org.openstreetmap.josm.plugins.mapillary.actions.MapillaryWalkAction;
27import org.openstreetmap.josm.plugins.mapillary.actions.MapillaryZoomAction;
28import org.openstreetmap.josm.plugins.mapillary.gui.MapillaryFilterDialog;
29import org.openstreetmap.josm.plugins.mapillary.gui.MapillaryHistoryDialog;
30import org.openstreetmap.josm.plugins.mapillary.gui.MapillaryMainDialog;
31import org.openstreetmap.josm.plugins.mapillary.gui.MapillaryPreferenceSetting;
32import org.openstreetmap.josm.plugins.mapillary.io.download.MapillaryDownloader;
33import org.openstreetmap.josm.plugins.mapillary.oauth.MapillaryUser;
34import org.openstreetmap.josm.tools.ImageProvider;
35
36/**
37 * This is the main class of the Mapillary plugin.
38 *
39 * @author nokutu
40 *
41 */
42public class MapillaryPlugin extends Plugin {
43 public static final String CLIENT_ID = "T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz";
44
45 /** OS route separator */
46 public static final String SEPARATOR = System.getProperty("file.separator");
47 /** 24x24 icon. */
48 public static final ImageIcon ICON24 = new ImageProvider("icon24.png").get();
49 /** 16x16 icon. */
50 public static final ImageIcon ICON16 = new ImageProvider("icon16.png").get();
51 /** Icon representing an image in the map. */
52 public static final ImageIcon MAP_ICON = new ImageProvider("mapicon.png").get();
53 /** Icon representing a selected image in the map. */
54 public static final ImageIcon MAP_ICON_SELECTED = new ImageProvider("mapiconselected.png").get();
55 /** Icon representing an imported image in the map. */
56 public static final ImageIcon MAP_ICON_IMPORTED = new ImageProvider("mapiconimported.png").get();
57 /** Icon used to identify which images have signs on them */
58 public static final ImageIcon MAP_SIGN = new ImageProvider("sign.png").get();
59
60 /** Cache that stores the pictures the downloaded pictures. */
61 public static CacheAccess<String, BufferedImageCacheEntry> CACHE;
62
63 private final MapillaryDownloadAction downloadAction;
64 private final MapillaryExportAction exportAction;
65 /** Import action */
66 private final MapillaryImportAction importAction;
67 /** Zoom action */
68 private static MapillaryZoomAction zoomAction;
69 private final MapillaryDownloadViewAction downloadViewAction;
70 private final MapillaryImportIntoSequenceAction importIntoSequenceAction;
71 private final MapillaryJoinAction joinAction;
72 /** Walk action */
73 private static MapillaryWalkAction walkAction;
74 /** Upload action */
75 private static MapillaryUploadAction uploadAction;
76
77 /** Menu button for the {@link MapillaryDownloadAction} action. */
78 private JMenuItem downloadMenu;
79 /** Menu button for the {@link MapillaryExportAction} action. */
80 private static JMenuItem exportMenu;
81 /** Menu button for the {@link MapillaryImportAction} action. */
82 private JMenuItem importMenu;
83 /** Menu button for the {@link MapillaryZoomAction} action. */
84 private static JMenuItem zoomMenu;
85 /** Menu button for the {@link MapillaryDownloadViewAction} action. */
86 private static JMenuItem downloadViewMenu;
87 /** Menu button for the {@link MapillaryImportIntoSequenceAction} action. */
88 private JMenuItem importIntoSequenceMenu;
89 /** Menu button for the {@link MapillaryJoinAction} action. */
90 private static JMenuItem joinMenu;
91 /** Menu button for the {@link MapillaryWalkAction} action. */
92 private static JMenuItem walkMenu;
93 /** Menu button for the {@link MapillaryUploadAction} action. */
94 private static JMenuItem uploadMenu;
95
96 /**
97 * Main constructor.
98 *
99 * @param info
100 * Required information of the plugin. Obtained from the jar file.
101 */
102 public MapillaryPlugin(PluginInformation info) {
103 super(info);
104
105 downloadAction = new MapillaryDownloadAction();
106 walkAction = new MapillaryWalkAction();
107 exportAction = new MapillaryExportAction();
108 importAction = new MapillaryImportAction();
109 zoomAction = new MapillaryZoomAction();
110 downloadViewAction = new MapillaryDownloadViewAction();
111 importIntoSequenceAction = new MapillaryImportIntoSequenceAction();
112 joinAction = new MapillaryJoinAction();
113 uploadAction = new MapillaryUploadAction();
114
115 if (Main.main != null) { // important for headless mode
116 downloadMenu = MainMenu.add(Main.main.menu.imageryMenu, this.downloadAction, false);
117 exportMenu = MainMenu.add(Main.main.menu.fileMenu, exportAction, false, 14);
118 importIntoSequenceMenu = MainMenu.add(Main.main.menu.fileMenu, importIntoSequenceAction, false, 14);
119 importMenu = MainMenu.add(Main.main.menu.fileMenu, importAction, false, 14);
120 uploadMenu = MainMenu.add(Main.main.menu.fileMenu, uploadAction, false, 14);
121 zoomMenu = MainMenu.add(Main.main.menu.viewMenu, zoomAction, false, 15);
122 downloadViewMenu = MainMenu.add(Main.main.menu.fileMenu, this.downloadViewAction, false, 14);
123 joinMenu = MainMenu.add(Main.main.menu.dataMenu, this.joinAction, false);
124 walkMenu = MainMenu.add(Main.main.menu.moreToolsMenu, walkAction, false);
125
126 exportMenu.setEnabled(false);
127 downloadMenu.setEnabled(false);
128 importMenu.setEnabled(false);
129 importIntoSequenceMenu.setEnabled(false);
130 zoomMenu.setEnabled(false);
131 downloadViewMenu.setEnabled(false);
132 joinMenu.setEnabled(false);
133 walkMenu.setEnabled(false);
134 }
135
136 try {
137 CACHE = JCSCacheManager.getCache("mapillary", 10, 10000, this.getPluginDir() + "/cache/");
138 } catch (IOException e) {
139 Main.error(e);
140 }
141
142 if (Main.pref.get("mapillary.access-token") == null)
143 MapillaryUser.isTokenValid = false;
144 }
145
146 /**
147 * @return the menu-item associated with the {@link MapillaryDownloadViewAction}
148 */
149 public static JMenuItem getDownloadViewMenu() {
150 return downloadViewMenu;
151 }
152
153 /**
154 * @return the menu-item associated with the {@link MapillaryExportAction}
155 */
156 public static JMenuItem getExportMenu() {
157 return exportMenu;
158 }
159
160 /**
161 * @return the menu-item associated with the {@link MapillaryJoinAction}
162 */
163 public static JMenuItem getJoinMenu() {
164 return joinMenu;
165 }
166
167 /**
168 * @return the {@link MapillaryUploadAction} for the plugin
169 */
170 public static MapillaryDataListener getUploadAction() {
171 return uploadAction;
172 }
173
174 /**
175 * @return the menu-item associated with the {@link MapillaryUploadAction}
176 */
177 public static JMenuItem getUploadMenu() {
178 return uploadMenu;
179 }
180
181 /**
182 * @return the {@link MapillaryWalkAction} for the plugin
183 */
184 public static MapillaryWalkAction getWalkAction() {
185 return walkAction;
186 }
187
188 /**
189 * @return the menu-item associated with the {@link MapillaryWalkAction}
190 */
191 public static JMenuItem getWalkMenu() {
192 return walkMenu;
193 }
194
195 /**
196 * @return the {@link MapillaryZoomAction} for the plugin
197 */
198 public static MapillaryDataListener getZoomAction() {
199 return zoomAction;
200 }
201
202 /**
203 * @return the menu-item associated with the {@link MapillaryZoomAction}
204 */
205 public static JMenuItem getZoomMenu() {
206 return zoomMenu;
207 }
208
209 /**
210 * Called when the JOSM map frame is created or destroyed.
211 */
212 @Override
213 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
214 if (oldFrame == null && newFrame != null) { // map frame added
215 Main.map.addToggleDialog(MapillaryMainDialog.getInstance(), false);
216 Main.map.addToggleDialog(MapillaryHistoryDialog.getInstance(), false);
217 Main.map.addToggleDialog(MapillaryFilterDialog.getInstance(), false);
218 setMenuEnabled(downloadMenu, true);
219 if (MapillaryDownloader.getMode() == MapillaryDownloader.MODES.Manual)
220 setMenuEnabled(downloadViewMenu, true);
221 setMenuEnabled(importMenu, true);
222 setMenuEnabled(importIntoSequenceMenu, true);
223 }
224 if (oldFrame != null && newFrame == null) { // map frame destroyed
225 MapillaryMainDialog.destroyInstance();
226 MapillaryHistoryDialog.destroyInstance();
227 MapillaryFilterDialog.destroyInstance();
228 setMenuEnabled(downloadMenu, false);
229 setMenuEnabled(downloadViewMenu, false);
230 setMenuEnabled(importMenu, false);
231 setMenuEnabled(importIntoSequenceMenu, false);
232 }
233 }
234
235 /**
236 * Enables/disables a {@link JMenuItem}.
237 *
238 * @param menu
239 * The JMenuItem object that is going to be enabled or disabled.
240 * @param value
241 * true to enable the JMenuItem; false to disable it.
242 */
243 public static void setMenuEnabled(final JMenuItem menu, final boolean value) {
244 if (!SwingUtilities.isEventDispatchThread()) {
245 SwingUtilities.invokeLater(new Runnable() {
246 @Override
247 public void run() {
248 setMenuEnabled(menu, value);
249 }
250 });
251 } else {
252 menu.setEnabled(value);
253 menu.getAction().setEnabled(value);
254 }
255 }
256
257 @Override
258 public PreferenceSetting getPreferenceSetting() {
259 return new MapillaryPreferenceSetting();
260 }
261
262 /**
263 * Returns a ImageProvider for the given string or null if in headless mode.
264 *
265 * @param s
266 * The name of the file where the picture is.
267 * @return A ImageProvider object for the given string or null if in headless
268 * mode.
269 */
270 public static ImageProvider getProvider(String s) {
271 if (Main.main == null)
272 return null;
273 else
274 return new ImageProvider(s);
275 }
276}
Note: See TracBrowser for help on using the repository browser.