Changeset 7434 in josm
- Timestamp:
- 2014-08-20T03:07:15+02:00 (10 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 2 added
- 39 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/Main.java
r7420 r7434 25 25 import java.util.Collections; 26 26 import java.util.HashMap; 27 import java.util.HashSet; 27 28 import java.util.Iterator; 28 29 import java.util.List; 29 30 import java.util.Map; 30 31 import java.util.Objects; 32 import java.util.Set; 31 33 import java.util.StringTokenizer; 32 34 import java.util.concurrent.Callable; … … 91 93 import org.openstreetmap.josm.gui.widgets.JMultilineLabel; 92 94 import org.openstreetmap.josm.io.FileWatcher; 95 import org.openstreetmap.josm.io.OnlineResource; 93 96 import org.openstreetmap.josm.io.OsmApi; 94 97 import org.openstreetmap.josm.tools.CheckParameterUtil; … … 219 222 private static final List<String> ERRORS_AND_WARNINGS = Collections.<String>synchronizedList(new ArrayList<String>()); 220 223 224 private static final Set<OnlineResource> OFFLINE_RESOURCES = new HashSet<>(); 225 221 226 /** 222 227 * Logging level (5 = trace, 4 = debug, 3 = info, 2 = warn, 1 = error, 0 = none). … … 543 548 List<Callable<Void>> tasks = new ArrayList<>(); 544 549 545 tasks.add(new InitializationTask(tr("Initializing OSM API")) { 546 547 @Override 548 public void initialize() throws Exception { 549 // We try to establish an API connection early, so that any API 550 // capabilities are already known to the editor instance. However 551 // if it goes wrong that's not critical at this stage. 552 try { 553 OsmApi.getOsmApi().initialize(null, true); 554 } catch (Exception e) { 555 Main.warn(getErrorMessage(Utils.getRootCause(e))); 550 if (isOffline(OnlineResource.OSM_API)) { 551 Main.warn(tr("{0} not available (offline mode)", tr("OSM API"))); 552 } else { 553 tasks.add(new InitializationTask(tr("Initializing OSM API")) { 554 555 @Override 556 public void initialize() throws Exception { 557 // We try to establish an API connection early, so that any API 558 // capabilities are already known to the editor instance. However 559 // if it goes wrong that's not critical at this stage. 560 try { 561 OsmApi.getOsmApi().initialize(null, true); 562 } catch (Exception e) { 563 Main.warn(getErrorMessage(Utils.getRootCause(e))); 564 } 556 565 } 557 } 558 } );566 }); 567 } 559 568 560 569 tasks.add(new InitializationTask(tr("Initializing validator")) { … … 1553 1562 return Main.platform instanceof PlatformHookWindows; 1554 1563 } 1564 1565 /** 1566 * Determines if the given online resource is currently offline. 1567 * @param r the online resource 1568 * @return {@code true} if {@code r} is offline and should not be accessed 1569 * @since 7434 1570 */ 1571 public static boolean isOffline(OnlineResource r) { 1572 return OFFLINE_RESOURCES.contains(r) || OFFLINE_RESOURCES.contains(OnlineResource.ALL); 1573 } 1574 1575 /** 1576 * Sets the given online resource to offline state. 1577 * @param r the online resource 1578 * @return {@code true} if {@code r} was not already offline 1579 * @since 7434 1580 */ 1581 public static boolean setOffline(OnlineResource r) { 1582 return OFFLINE_RESOURCES.add(r); 1583 } 1584 1585 /** 1586 * Replies the set of online resources currently offline. 1587 * @return the set of online resources currently offline 1588 * @since 7434 1589 */ 1590 public static Set<OnlineResource> getOfflineResources() { 1591 return new HashSet<>(OFFLINE_RESOURCES); 1592 } 1555 1593 } -
trunk/src/org/openstreetmap/josm/actions/CloseChangesetAction.java
r6084 r7434 23 23 import org.openstreetmap.josm.gui.io.CloseChangesetTask; 24 24 import org.openstreetmap.josm.io.ChangesetQuery; 25 import org.openstreetmap.josm.io.OnlineResource; 25 26 import org.openstreetmap.josm.io.OsmServerChangesetReader; 26 27 import org.openstreetmap.josm.io.OsmServerUserInfoReader; … … 29 30 import org.xml.sax.SAXException; 30 31 31 public class CloseChangesetAction extends JosmAction {32 public class CloseChangesetAction extends JosmAction { 32 33 34 /** 35 * Constructs a new {@code CloseChangesetAction}. 36 */ 33 37 public CloseChangesetAction() { 34 38 super(tr("Close open changesets"), … … 41 45 ); 42 46 putValue("help", ht("/Action/CloseChangeset")); 47 setEnabled(!Main.isOffline(OnlineResource.OSM_API)); 43 48 44 49 } -
trunk/src/org/openstreetmap/josm/actions/DeleteAction.java
r6380 r7434 13 13 import org.openstreetmap.josm.tools.Shortcut; 14 14 15 /** 16 * Action that deletes selected objects. 17 * @since 770 18 */ 15 19 public final class DeleteAction extends JosmAction { 16 20 21 /** 22 * Constructs a new {@code DeleteAction}. 23 */ 17 24 public DeleteAction() { 18 25 super(tr("Delete"), "dialogs/delete", tr("Delete selected objects."), … … 23 30 @Override 24 31 public void actionPerformed(ActionEvent e) { 25 if (!isEnabled()) 26 return; 27 if(!Main.map.mapView.isActiveLayerVisible()) 32 if (!isEnabled() || !Main.map.mapView.isActiveLayerVisible()) 28 33 return; 29 34 org.openstreetmap.josm.actions.mapmode.DeleteAction.doActionPerformed(e); -
trunk/src/org/openstreetmap/josm/actions/HelpAction.java
r6380 r7434 14 14 import org.openstreetmap.josm.gui.help.HelpBrowser; 15 15 import org.openstreetmap.josm.gui.help.HelpUtil; 16 import org.openstreetmap.josm.io.OnlineResource; 16 17 import org.openstreetmap.josm.tools.ImageProvider; 17 18 18 19 /** 19 20 * Open a help browser and displays lightweight online help. 20 * 21 * @since 155 21 22 */ 22 23 public class HelpAction extends AbstractAction { 23 24 25 /** 26 * Constructs a new {@code HelpAction}. 27 */ 24 28 public HelpAction() { 25 29 super(tr("Help"), ImageProvider.get("help")); 26 30 putValue("toolbar", "help"); 31 setEnabled(!Main.isOffline(OnlineResource.JOSM_WEBSITE)); 27 32 } 28 33 -
trunk/src/org/openstreetmap/josm/actions/HistoryInfoAction.java
r6449 r7434 1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.actions; 3 4 import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 5 import static org.openstreetmap.josm.tools.I18n.tr; 3 6 4 7 import java.awt.event.ActionEvent; … … 9 12 import org.openstreetmap.josm.gui.dialogs.OsmIdSelectionDialog; 10 13 import org.openstreetmap.josm.gui.history.HistoryBrowserDialogManager; 14 import org.openstreetmap.josm.io.OnlineResource; 11 15 import org.openstreetmap.josm.tools.Shortcut; 12 13 import static org.openstreetmap.josm.tools.I18n.tr;14 import static org.openstreetmap.josm.gui.help.HelpUtil.ht;15 16 16 17 /** … … 61 62 init(); 62 63 } 64 65 @Override 66 public void setupDialog() { 67 super.setupDialog(); 68 buttons.get(0).setEnabled(!Main.isOffline(OnlineResource.OSM_API)); 69 } 63 70 } 64 71 } -
trunk/src/org/openstreetmap/josm/actions/OpenLocationAction.java
r7005 r7434 73 73 protected void restoreUploadAddressHistory(HistoryComboBox cbHistory) { 74 74 List<String> cmtHistory = new LinkedList<>(Main.pref.getCollection(getClass().getName() + ".uploadAddressHistory", new LinkedList<String>())); 75 // we have to reverse the history, because ComboBoxHistory will reverse it again 76 // in addElement() 75 // we have to reverse the history, because ComboBoxHistory will reverse it again in addElement() 77 76 // 78 77 Collections.reverse(cmtHistory); -
trunk/src/org/openstreetmap/josm/actions/UpdateDataAction.java
r7005 r7434 15 15 import org.openstreetmap.josm.actions.downloadtasks.DownloadTaskList; 16 16 import org.openstreetmap.josm.data.osm.DataSource; 17 import org.openstreetmap.josm.gui.layer.OsmDataLayer;18 17 import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor; 18 import org.openstreetmap.josm.io.OnlineResource; 19 19 import org.openstreetmap.josm.tools.Shortcut; 20 20 21 public class UpdateDataAction extends JosmAction{ 21 public class UpdateDataAction extends JosmAction { 22 23 /** 24 * Constructs a new {@code UpdateDataAction}. 25 */ 22 26 public UpdateDataAction() { 23 27 super(tr("Update data"), … … 33 37 /** 34 38 * Refreshes the enabled state 35 *36 39 */ 37 40 @Override 38 41 protected void updateEnabledState() { 39 setEnabled(getEditLayer() != null); 40 } 41 42 public void updateLayer(OsmDataLayer layer) { 43 42 setEnabled(getEditLayer() != null && !Main.isOffline(OnlineResource.OSM_API)); 44 43 } 45 44 -
trunk/src/org/openstreetmap/josm/actions/UpdateModifiedAction.java
r6814 r7434 9 9 import java.util.Collections; 10 10 11 import org.openstreetmap.josm.Main; 11 12 import org.openstreetmap.josm.data.osm.OsmPrimitive; 13 import org.openstreetmap.josm.io.OnlineResource; 12 14 import org.openstreetmap.josm.tools.Shortcut; 13 15 … … 44 46 @Override 45 47 protected void updateEnabledState() { 46 setEnabled(getCurrentDataSet() != null );48 setEnabled(getCurrentDataSet() != null && !Main.isOffline(OnlineResource.OSM_API)); 47 49 } 48 50 -
trunk/src/org/openstreetmap/josm/actions/UpdateSelectionAction.java
r6814 r7434 22 22 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 23 23 import org.openstreetmap.josm.io.MultiFetchServerObjectReader; 24 import org.openstreetmap.josm.io.OnlineResource; 24 25 import org.openstreetmap.josm.tools.Shortcut; 25 26 … … 122 123 @Override 123 124 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { 124 setEnabled(selection != null && !selection.isEmpty() );125 setEnabled(selection != null && !selection.isEmpty() && !Main.isOffline(OnlineResource.OSM_API)); 125 126 } 126 127 -
trunk/src/org/openstreetmap/josm/actions/mapmode/DeleteAction.java
r7217 r7434 129 129 } 130 130 131 /** 132 * Invoked when the action occurs. 133 * @param e Action event 134 */ 131 135 public static void doActionPerformed(ActionEvent e) { 132 if (!Main.map.mapView.isActiveLayerDrawable())136 if (!Main.map.mapView.isActiveLayerDrawable()) 133 137 return; 134 138 boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0; … … 149 153 } 150 154 151 @Override public void mouseDragged(MouseEvent e) { 155 @Override 156 public void mouseDragged(MouseEvent e) { 152 157 mouseMoved(e); 153 158 } … … 157 162 * @param e The mouse event that has been captured 158 163 */ 159 @Override public void mouseMoved(MouseEvent e) { 164 @Override 165 public void mouseMoved(MouseEvent e) { 160 166 oldEvent = e; 161 167 giveUserFeedback(e); … … 245 251 Main.map.mapView.setNewCursor(parameters.mode.cursor(), this); 246 252 } 253 247 254 /** 248 255 * Gives the user feedback for the action he/she is about to do. Currently … … 274 281 * position. 275 282 */ 276 @Override public void mouseReleased(MouseEvent e) { 283 @Override 284 public void mouseReleased(MouseEvent e) { 277 285 if (e.getButton() != MouseEvent.BUTTON1) 278 286 return; … … 293 301 } 294 302 295 @Override public String getModeHelpText() { 303 @Override 304 public String getModeHelpText() { 296 305 return tr("Click to delete. Shift: delete way segment. Alt: do not delete unused nodes when deleting a way. Ctrl: delete referring objects."); 297 306 } 298 307 299 @Override public boolean layerIsSupported(Layer l) { 308 @Override 309 public boolean layerIsSupported(Layer l) { 300 310 return l instanceof OsmDataLayer; 301 311 } … … 391 401 @Override 392 402 public void modifiersChanged(int modifiers) { 393 if(oldEvent == null) 394 return; 395 // We don't have a mouse event, so we pass the old mouse event but the 396 // new modifiers. 403 if (oldEvent == null) 404 return; 405 // We don't have a mouse event, so we pass the old mouse event but the new modifiers. 397 406 giveUserFeedback(oldEvent, modifiers); 398 407 } -
trunk/src/org/openstreetmap/josm/actions/relation/DownloadMembersAction.java
r6336 r7434 12 12 import org.openstreetmap.josm.data.osm.Relation; 13 13 import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationTask; 14 import org.openstreetmap.josm.io.OnlineResource; 14 15 import org.openstreetmap.josm.tools.ImageProvider; 15 16 import org.openstreetmap.josm.tools.Predicate; … … 31 32 putValue("help", ht("/Dialog/RelationList#DownloadMembers")); 32 33 } 33 34 34 35 @Override 35 36 public void actionPerformed(ActionEvent e) { … … 47 48 updateEnabledState(); 48 49 } 50 51 @Override 52 protected void updateEnabledState() { 53 setEnabled(!relations.isEmpty() && !Main.isOffline(OnlineResource.OSM_API)); 54 } 49 55 } -
trunk/src/org/openstreetmap/josm/actions/relation/DownloadSelectedIncompleteMembersAction.java
r7005 r7434 13 13 import org.openstreetmap.josm.data.osm.Relation; 14 14 import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask; 15 import org.openstreetmap.josm.io.OnlineResource; 15 16 import org.openstreetmap.josm.tools.ImageProvider; 16 17 import org.openstreetmap.josm.tools.Predicate; … … 63 64 updateEnabledState(); 64 65 } 66 67 @Override 68 protected void updateEnabledState() { 69 setEnabled(!relations.isEmpty() && !Main.isOffline(OnlineResource.OSM_API)); 70 } 65 71 } -
trunk/src/org/openstreetmap/josm/actions/upload/ApiPreconditionCheckerHook.java
r6248 r7434 15 15 import org.openstreetmap.josm.gui.ExceptionDialogUtil; 16 16 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 17 import org.openstreetmap.josm.io.OnlineResource; 17 18 import org.openstreetmap.josm.io.OsmApi; 18 19 import org.openstreetmap.josm.io.OsmApiInitializationException; … … 25 26 OsmApi api = OsmApi.getOsmApi(); 26 27 try { 28 if (Main.isOffline(OnlineResource.OSM_API)) { 29 return false; 30 } 27 31 // FIXME: this should run asynchronously and a progress monitor 28 32 // should be displayed. … … 40 44 return false; 41 45 } 42 } catch (OsmTransferCanceledException e){46 } catch (OsmTransferCanceledException e) { 43 47 return false; 44 48 } catch (OsmApiInitializationException e) { -
trunk/src/org/openstreetmap/josm/data/imagery/ImageryLayerInfo.java
r7248 r7434 14 14 import java.util.Set; 15 15 import java.util.TreeSet; 16 16 17 import org.openstreetmap.josm.Main; 17 18 import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry; 18 19 import org.openstreetmap.josm.io.CachedFile; 20 import org.openstreetmap.josm.io.OfflineAccessException; 21 import org.openstreetmap.josm.io.OnlineResource; 19 22 import org.openstreetmap.josm.io.imagery.ImageryReader; 20 23 import org.xml.sax.SAXException; … … 34 37 Main.getJOSMWebsite()+"/maps" 35 38 }; 39 40 /** 41 * Returns the list of imagery layers sites. 42 * @return the list of imagery layers sites 43 * @since 7434 44 */ 45 public static Collection<String> getImageryLayersSites() { 46 return Main.pref.getCollection("imagery.layers.sites", Arrays.asList(DEFAULT_LAYER_SITES)); 47 } 36 48 37 49 private ImageryLayerInfo() { … … 76 88 defaultLayers.clear(); 77 89 defaultLayerIds.clear(); 78 for (String source : Main.pref.getCollection("imagery.layers.sites", Arrays.asList(DEFAULT_LAYER_SITES))) { 79 if (clearCache) { 90 for (String source : getImageryLayersSites()) { 91 boolean online = true; 92 try { 93 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(source, Main.getJOSMWebsite()); 94 } catch (OfflineAccessException e) { 95 Main.warn(e.getMessage()); 96 online = false; 97 } 98 if (clearCache && online) { 80 99 CachedFile.cleanup(source); 81 100 } … … 96 115 buildIdMap(layers, layerIds); 97 116 } 98 117 99 118 /** 100 119 * Build the mapping of unique ids to {@link ImageryInfo}s. … … 120 139 } 121 140 } 122 141 123 142 /** 124 143 * Update user entries according to the list of default entries. … … 179 198 } 180 199 Main.pref.putCollection("imagery.layers.addedIds", newAddedIds); 181 200 182 201 // automatically update user entries with same id as a default entry 183 202 for (int i=0; i<layers.size(); i++) { … … 192 211 } 193 212 } 194 213 195 214 if (changed) { 196 215 save(); … … 202 221 return isSimilar(iiA.getUrl(), iiB.getUrl()); 203 222 } 204 223 205 224 // some additional checks to respect extended URLs in preferences (legacy workaround) 206 225 private boolean isSimilar(String a, String b) { 207 226 return Objects.equals(a, b) || (a != null && b != null && !a.isEmpty() && !b.isEmpty() && (a.contains(b) || b.contains(a))); 208 227 } 209 228 210 229 public void add(ImageryInfo info) { 211 230 layers.add(info); … … 244 263 Collections.sort(instance.layers); 245 264 } 246 265 247 266 /** 248 267 * Get unique id for ImageryInfo. 249 * 268 * 250 269 * This takes care, that no id is used twice (due to a user error) 251 270 * @param info the ImageryInfo to look up -
trunk/src/org/openstreetmap/josm/gui/ExceptionDialogUtil.java
r7187 r7434 20 20 import org.openstreetmap.josm.io.IllegalDataException; 21 21 import org.openstreetmap.josm.io.MissingOAuthAccessTokenException; 22 import org.openstreetmap.josm.io.OfflineAccessException; 22 23 import org.openstreetmap.josm.io.OsmApi; 23 24 import org.openstreetmap.josm.io.OsmApiException; … … 154 155 * @param e the exception 155 156 */ 156 157 157 public static void explainNestedIllegalDataException(OsmTransferException e) { 158 158 HelpAwareOptionPane.showOptionDialog( … … 166 166 167 167 /** 168 * Explains a {@link OfflineAccessException} which has caused an {@link OsmTransferException}. 169 * This is most likely happening when JOSM tries to access OSM API or JOSM website while in offline mode. 170 * 171 * @param e the exception 172 * @since 7434 173 */ 174 public static void explainNestedOfflineAccessException(OsmTransferException e) { 175 HelpAwareOptionPane.showOptionDialog( 176 Main.parent, 177 ExceptionUtil.explainOfflineAccessException(e), 178 tr("Offline mode"), 179 JOptionPane.ERROR_MESSAGE, 180 ht("/ErrorMessages#OfflineAccessException") 181 ); 182 } 183 184 /** 168 185 * Explains a {@link InvocationTargetException } 169 186 * 170 187 * @param e the exception 171 188 */ 172 173 189 public static void explainNestedInvocationTargetException(Exception e) { 174 190 InvocationTargetException ex = getNestedException(e, InvocationTargetException.class); … … 435 451 return; 436 452 } 453 if (getNestedException(e, OfflineAccessException.class) != null) { 454 explainNestedOfflineAccessException(e); 455 return; 456 } 437 457 if (e instanceof OsmApiInitializationException) { 438 458 explainOsmApiInitializationException((OsmApiInitializationException) e); -
trunk/src/org/openstreetmap/josm/gui/ExtendedDialog.java
r7407 r7434 30 30 import javax.swing.UIManager; 31 31 32 import org.openstreetmap.josm.Main; 32 33 import org.openstreetmap.josm.gui.help.HelpBrowser; 33 34 import org.openstreetmap.josm.gui.help.HelpUtil; 34 35 import org.openstreetmap.josm.gui.util.GuiHelper; 35 36 import org.openstreetmap.josm.gui.widgets.JMultilineLabel; 37 import org.openstreetmap.josm.io.OnlineResource; 36 38 import org.openstreetmap.josm.tools.GBC; 37 39 import org.openstreetmap.josm.tools.ImageProvider; … … 642 644 putValue(NAME, tr("Help")); 643 645 putValue(SMALL_ICON, ImageProvider.get("help")); 644 } 645 646 @Override public void actionPerformed(ActionEvent e) { 646 setEnabled(!Main.isOffline(OnlineResource.JOSM_WEBSITE)); 647 } 648 649 @Override 650 public void actionPerformed(ActionEvent e) { 647 651 HelpBrowser.setUrlForHelpTopic(helpTopic); 648 652 } -
trunk/src/org/openstreetmap/josm/gui/GettingStarted.java
r7082 r7434 30 30 import org.openstreetmap.josm.gui.widgets.JosmEditorPane; 31 31 import org.openstreetmap.josm.io.CacheCustomContent; 32 import org.openstreetmap.josm.io.OnlineResource; 32 33 import org.openstreetmap.josm.tools.LanguageInfo; 33 34 import org.openstreetmap.josm.tools.OpenBrowser; … … 95 96 } 96 97 98 @Override 99 protected void checkOfflineAccess() { 100 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(new WikiReader().getBaseUrlWiki(), Main.getJOSMWebsite()); 101 } 102 97 103 /** 98 104 * Additionally check if JOSM has been updated and refresh MOTD … … 151 157 } 152 158 153 EventQueue.invokeLater(new Runnable() { 154 @Override 155 public void run() { 156 lg.setText(fixImageLinks(content)); 157 } 158 }); 159 if (content != null) { 160 EventQueue.invokeLater(new Runnable() { 161 @Override 162 public void run() { 163 lg.setText(fixImageLinks(content)); 164 } 165 }); 166 } 159 167 } 160 168 }, "MOTD-Loader"); -
trunk/src/org/openstreetmap/josm/gui/JosmUserIdentityManager.java
r7081 r7434 14 14 import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder; 15 15 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 16 import org.openstreetmap.josm.io.OnlineResource; 16 17 import org.openstreetmap.josm.io.OsmApi; 17 18 import org.openstreetmap.josm.io.OsmServerUserInfoReader; … … 61 62 if (instance == null) { 62 63 instance = new JosmUserIdentityManager(); 63 if (OsmApi.isUsingOAuth() && OAuthAccessTokenHolder.getInstance().containsAccessToken()) { 64 if (OsmApi.isUsingOAuth() && OAuthAccessTokenHolder.getInstance().containsAccessToken() && 65 !Main.isOffline(OnlineResource.OSM_API)) { 64 66 try { 65 67 instance.initFromOAuth(Main.parent); -
trunk/src/org/openstreetmap/josm/gui/MainApplication.java
r7383 r7434 27 27 import java.security.cert.CertificateException; 28 28 import java.util.ArrayList; 29 import java.util.Arrays; 29 30 import java.util.Collection; 30 31 import java.util.HashMap; … … 54 55 import org.openstreetmap.josm.io.DefaultProxySelector; 55 56 import org.openstreetmap.josm.io.MessageNotifier; 57 import org.openstreetmap.josm.io.OnlineResource; 56 58 import org.openstreetmap.josm.io.auth.CredentialsManager; 57 59 import org.openstreetmap.josm.io.auth.DefaultAuthenticator; … … 87 89 mainFrame.setJMenuBar(menu); 88 90 geometry.applySafe(mainFrame); 89 Li nkedList<Image> l = new LinkedList<>();91 List<Image> l = new LinkedList<>(); 90 92 l.add(ImageProvider.get("logo_16x16x32").getImage()); 91 93 l.add(ImageProvider.get("logo_16x16x8").getImage()); … … 131 133 "\t--version "+tr("Displays the JOSM version and exits")+"\n\n"+ 132 134 "\t--debug "+tr("Print debugging messages to console")+"\n\n"+ 135 "\t--offline=<osm_api|josm_website|all> "+tr("Disable access to the given resource(s), separated by comma")+"\n\n"+ 133 136 tr("options provided as Java system properties")+":\n"+ 134 137 "\t-Djosm.home="+tr("/PATH/TO/JOSM/FOLDER/ ")+tr("Change the folder for all user settings")+"\n\n"+ … … 154 157 */ 155 158 public enum Option { 156 /** --help|-h Show this help */159 /** --help|-h Show this help */ 157 160 HELP(false), 158 /** --version Displays the JOSM version and exits */161 /** --version Displays the JOSM version and exits */ 159 162 VERSION(false), 160 /** --debug Print debugging messages to console */163 /** --debug Print debugging messages to console */ 161 164 DEBUG(false), 162 /** --trace Print detailed debugging messages to console */165 /** --trace Print detailed debugging messages to console */ 163 166 TRACE(false), 164 /** --language=<language> Set the language */167 /** --language=<language> Set the language */ 165 168 LANGUAGE(true), 166 /** --reset-preferences Reset the preferences to default */169 /** --reset-preferences Reset the preferences to default */ 167 170 RESET_PREFERENCES(false), 168 /** --load-preferences=<url-to-xml> Changes preferences according to the XML file */171 /** --load-preferences=<url-to-xml> Changes preferences according to the XML file */ 169 172 LOAD_PREFERENCES(true), 170 /** --set=<key>=<value> Set preference key to value */173 /** --set=<key>=<value> Set preference key to value */ 171 174 SET(true), 172 /** --geometry=widthxheight(+|-)x(+|-)y Standard unix geometry argument */175 /** --geometry=widthxheight(+|-)x(+|-)y Standard unix geometry argument */ 173 176 GEOMETRY(true), 174 /** --no-maximize Do not launch in maximized mode */177 /** --no-maximize Do not launch in maximized mode */ 175 178 NO_MAXIMIZE(false), 176 /** --maximize Launch in maximized mode */179 /** --maximize Launch in maximized mode */ 177 180 MAXIMIZE(false), 178 /** --download=minlat,minlon,maxlat,maxlon Download the bounding box <br>179 * --download=<URL> Download the location at the URL (with lat=x&lon=y&zoom=z) <br>180 * --download=<filename> Open a file (any file type that can be opened with File/Open) */181 /** --download=minlat,minlon,maxlat,maxlon Download the bounding box <br> 182 * --download=<URL> Download the location at the URL (with lat=x&lon=y&zoom=z) <br> 183 * --download=<filename> Open a file (any file type that can be opened with File/Open) */ 181 184 DOWNLOAD(true), 182 /** --downloadgps=minlat,minlon,maxlat,maxlon Download the bounding box as raw GPS <br>183 * --downloadgps=<URL> Download the location at the URL (with lat=x&lon=y&zoom=z) as raw GPS */185 /** --downloadgps=minlat,minlon,maxlat,maxlon Download the bounding box as raw GPS <br> 186 * --downloadgps=<URL> Download the location at the URL (with lat=x&lon=y&zoom=z) as raw GPS */ 184 187 DOWNLOADGPS(true), 185 /** --selection=<searchstring> Select with the given search */ 186 SELECTION(true); 188 /** --selection=<searchstring> Select with the given search */ 189 SELECTION(true), 190 /** --offline=<osm_api|josm_website|all> Disable access to the given resource(s), delimited by comma */ 191 OFFLINE(true); 187 192 188 193 private String name; … … 284 289 } catch (IllegalArgumentException e) { 285 290 System.exit(1); 291 return; 286 292 } 287 293 … … 347 353 Main.pref.updateSystemProperties(); 348 354 355 processOffline(args); 356 349 357 FontsManager.initialize(); 350 358 351 359 final JFrame mainFrame = new JFrame(tr("Java OpenStreetMap Editor")); 352 360 Main.parent = mainFrame; … … 466 474 info("Enabled EDT checker, wrongful access to gui from non EDT thread will be printed to console"); 467 475 RepaintManager.setCurrentManager(new CheckThreadViolationRepaintManager()); 476 } 477 } 478 479 private static void processOffline(Map<Option, Collection<String>> args) { 480 if (args.containsKey(Option.OFFLINE)) { 481 for (String s : args.get(Option.OFFLINE).iterator().next().split(",")) { 482 try { 483 Main.setOffline(OnlineResource.valueOf(s.toUpperCase())); 484 } catch (IllegalArgumentException e) { 485 Main.error(tr("''{0}'' is not a valid value for argument ''{1}''. Possible values are {2}, possibly delimited by commas.", 486 s.toUpperCase(), Option.OFFLINE.getName(), Arrays.toString(OnlineResource.values()))); 487 System.exit(1); 488 return; 489 } 490 } 491 Set<OnlineResource> offline = Main.getOfflineResources(); 492 if (!offline.isEmpty()) { 493 Main.warn(trn("JOSM is running in offline mode. This resource will not be available: {0}", 494 "JOSM is running in offline mode. These resources will not be available: {0}", 495 offline.size(), offline.size() == 1 ? offline.iterator().next() : Arrays.toString(offline.toArray()))); 496 } 468 497 } 469 498 } -
trunk/src/org/openstreetmap/josm/gui/dialogs/ChangesetDialog.java
r7005 r7434 52 52 import org.openstreetmap.josm.gui.io.CloseChangesetTask; 53 53 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 54 import org.openstreetmap.josm.gui.util.GuiHelper; 54 55 import org.openstreetmap.josm.gui.widgets.ListPopupMenu; 55 56 import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher; 57 import org.openstreetmap.josm.io.OnlineResource; 56 58 import org.openstreetmap.josm.tools.BugReportExceptionHandler; 57 59 import org.openstreetmap.josm.tools.ImageProvider; … … 363 365 364 366 protected void updateEnabledState() { 365 setEnabled(getCurrentChangesetList().getSelectedIndices().length > 0 );367 setEnabled(getCurrentChangesetList().getSelectedIndices().length > 0 && !Main.isOffline(OnlineResource.OSM_API)); 366 368 } 367 369 … … 486 488 Set<Integer> sel = model.getSelectedChangesetIds(); 487 489 final Set<Integer> toDownload = new HashSet<>(); 488 ChangesetCache cc = ChangesetCache.getInstance(); 489 for (int id: sel) { 490 if (!cc.contains(id)) { 491 toDownload.add(id); 490 if (!Main.isOffline(OnlineResource.OSM_API)) { 491 ChangesetCache cc = ChangesetCache.getInstance(); 492 for (int id: sel) { 493 if (!cc.contains(id)) { 494 toDownload.add(id); 495 } 492 496 } 493 497 } … … 506 510 @Override 507 511 public void run() { 508 // first, wait for the download task to finish, if a download 509 // task was launched 512 // first, wait for the download task to finish, if a download task was launched 510 513 if (future != null) { 511 514 try { … … 521 524 if (task != null) { 522 525 if (task.isCanceled()) 523 // don't launch the changeset manager if the download task 524 // was canceled 526 // don't launch the changeset manager if the download task was canceled 525 527 return; 526 528 if (task.isFailed()) { … … 529 531 } 530 532 // launch the task 531 launchChangesetManager(toDownload); 533 GuiHelper.runInEDT(new Runnable() { 534 @Override 535 public void run() { 536 launchChangesetManager(toDownload); 537 } 538 }); 532 539 } 533 540 }; -
trunk/src/org/openstreetmap/josm/gui/dialogs/HistoryDialog.java
r7005 r7434 42 42 import org.openstreetmap.josm.gui.history.HistoryBrowserDialogManager; 43 43 import org.openstreetmap.josm.gui.history.HistoryLoadTask; 44 import org.openstreetmap.josm.io.OnlineResource; 44 45 import org.openstreetmap.josm.tools.ImageProvider; 45 46 import org.openstreetmap.josm.tools.InputMapUtils; … … 286 287 287 288 protected void updateEnabledState() { 288 setEnabled(historyTable.getSelectedRowCount() > 0 );289 setEnabled(historyTable.getSelectedRowCount() > 0 && !Main.isOffline(OnlineResource.OSM_API)); 289 290 } 290 291 -
trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetCacheManager.java
r7005 r7434 49 49 import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher; 50 50 import org.openstreetmap.josm.io.ChangesetQuery; 51 import org.openstreetmap.josm.io.OnlineResource; 51 52 import org.openstreetmap.josm.tools.ImageProvider; 52 53 import org.openstreetmap.josm.tools.WindowGeometry; … … 364 365 putValue(SMALL_ICON, ImageProvider.get("dialogs","search")); 365 366 putValue(SHORT_DESCRIPTION, tr("Launch the dialog for querying changesets")); 367 setEnabled(!Main.isOffline(OnlineResource.OSM_API)); 366 368 } 367 369 … … 476 478 477 479 protected void updateEnabledState() { 478 setEnabled(model.hasSelectedChangesets() );480 setEnabled(model.hasSelectedChangesets() && !Main.isOffline(OnlineResource.OSM_API)); 479 481 } 480 482 … … 504 506 505 507 protected void updateEnabledState() { 506 setEnabled(model.hasSelectedChangesets() );508 setEnabled(model.hasSelectedChangesets() && !Main.isOffline(OnlineResource.OSM_API)); 507 509 } 508 510 … … 532 534 putValue(SMALL_ICON, ImageProvider.get("dialogs/changeset", "downloadchangeset")); 533 535 putValue(SHORT_DESCRIPTION, tr("Download my changesets from the OSM server (max. 100 changesets)")); 536 setEnabled(!Main.isOffline(OnlineResource.OSM_API)); 534 537 } 535 538 -
trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetDetailPanel.java
r7299 r7434 40 40 import org.openstreetmap.josm.gui.widgets.JosmTextArea; 41 41 import org.openstreetmap.josm.gui.widgets.JosmTextField; 42 import org.openstreetmap.josm.io.OnlineResource; 42 43 import org.openstreetmap.josm.tools.ImageProvider; 43 44 import org.openstreetmap.josm.tools.date.DateUtils; … … 345 346 346 347 public void initProperties(Changeset cs) { 347 if (cs == null) { 348 setEnabled(false); 349 return; 350 } else { 351 setEnabled(true); 352 } 348 setEnabled(cs != null && !Main.isOffline(OnlineResource.OSM_API)); 353 349 } 354 350 } -
trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/SingleChangesetDownloadPanel.java
r6603 r7434 20 20 import org.openstreetmap.josm.gui.widgets.ChangesetIdTextField; 21 21 import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator; 22 import org.openstreetmap.josm.io.OnlineResource; 22 23 23 24 /** … … 98 99 99 100 protected void updateEnabledState() { 100 setEnabled(tfChangesetId.readIds() );101 setEnabled(tfChangesetId.readIds() && !Main.isOffline(OnlineResource.OSM_API)); 101 102 } 102 103 -
trunk/src/org/openstreetmap/josm/gui/dialogs/relation/GenericRelationEditor.java
r7128 r7434 83 83 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField; 84 84 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList; 85 import org.openstreetmap.josm.io.OnlineResource; 85 86 import org.openstreetmap.josm.tools.CheckParameterUtil; 86 87 import org.openstreetmap.josm.tools.ImageProvider; … … 212 213 } 213 214 ); 214 registerCopyPasteAction(tagEditorPanel.getPasteAction(), 215 registerCopyPasteAction(tagEditorPanel.getPasteAction(), 215 216 "PASTE_TAGS", 216 217 Shortcut.registerShortcut("system:pastestyle", tr("Edit: {0}", tr("Paste Tags")), KeyEvent.VK_V, Shortcut.CTRL_SHIFT).getKeyStroke()); … … 1475 1476 1476 1477 protected void updateEnabledState() { 1477 setEnabled(memberTableModel.hasIncompleteMembers() );1478 setEnabled(memberTableModel.hasIncompleteMembers() && !Main.isOffline(OnlineResource.OSM_API)); 1478 1479 } 1479 1480 … … 1507 1508 1508 1509 protected void updateEnabledState() { 1509 setEnabled(memberTableModel.hasIncompleteSelectedMembers() );1510 setEnabled(memberTableModel.hasIncompleteSelectedMembers() && !Main.isOffline(OnlineResource.OSM_API)); 1510 1511 } 1511 1512 -
trunk/src/org/openstreetmap/josm/gui/download/DownloadDialog.java
r7005 r7434 39 39 import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction; 40 40 import org.openstreetmap.josm.gui.help.HelpUtil; 41 import org.openstreetmap.josm.io.OnlineResource; 41 42 import org.openstreetmap.josm.plugins.PluginHandler; 42 43 import org.openstreetmap.josm.tools.GBC; … … 103 104 104 105 slippyMapChooser = new SlippyMapChooser(); 105 106 106 107 // predefined download selections 107 108 downloadSelections.add(slippyMapChooser); … … 305 306 } 306 307 } 307 308 308 309 /** 309 310 * Remembers the current settings in the download dialog. … … 349 350 } 350 351 } 351 352 352 353 /** 353 354 * Returns the previously saved bounding box from preferences. … … 444 445 putValue(SMALL_ICON, ImageProvider.get("download")); 445 446 putValue(SHORT_DESCRIPTION, tr("Click to download the currently selected area")); 447 setEnabled(!Main.isOffline(OnlineResource.OSM_API)); 446 448 } 447 449 -
trunk/src/org/openstreetmap/josm/gui/download/DownloadObjectDialog.java
r6448 r7434 16 16 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 17 17 import org.openstreetmap.josm.gui.dialogs.OsmIdSelectionDialog; 18 import org.openstreetmap.josm.io.OnlineResource; 18 19 19 20 /** … … 49 50 } 50 51 52 @Override 53 public void setupDialog() { 54 super.setupDialog(); 55 buttons.get(0).setEnabled(!Main.isOffline(OnlineResource.OSM_API)); 56 } 57 58 @Override 51 59 protected Collection<Component> getComponentsBeforeHelp() { 52 60 newLayer.setToolTipText(tr("Select if the data should be downloaded into a new layer")); -
trunk/src/org/openstreetmap/josm/gui/help/ContextSensitiveHelpAction.java
r6084 r7434 2 2 package org.openstreetmap.josm.gui.help; 3 3 4 import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 5 import static org.openstreetmap.josm.tools.I18n.tr; 6 4 7 import java.awt.event.ActionEvent; 5 import static org.openstreetmap.josm.gui.help.HelpUtil.ht;6 8 7 9 import javax.swing.AbstractAction; 8 import static org.openstreetmap.josm.tools.I18n.tr;9 10 11 import org.openstreetmap.josm.Main; 12 import org.openstreetmap.josm.io.OnlineResource; 10 13 import org.openstreetmap.josm.tools.ImageProvider; 11 14 … … 13 16 * This is the standard help action to be used with help buttons for 14 17 * context sensitive help 15 * 18 * @since 2289 16 19 */ 17 20 public class ContextSensitiveHelpAction extends AbstractAction { 18 21 19 /** the help topic */20 22 private String helpTopic; 21 23 22 24 /** 23 * Sets the help topic 25 * Sets the help topic. 24 26 * 25 27 * @param relativeHelpTopic the relative help topic … … 32 34 33 35 /** 34 * Creates a help topic for the root help topic 35 * 36 * Constructs a new {@code ContextSensitiveHelpAction} for the root help topic. 36 37 */ 37 38 public ContextSensitiveHelpAction() { … … 40 41 41 42 /** 42 * 43 * @param helpTopic 43 * Constructs a new {@code ContextSensitiveHelpAction} for a given help topic. 44 * @param helpTopic The help topic 44 45 */ 45 46 public ContextSensitiveHelpAction(String helpTopic) { … … 48 49 putValue(SMALL_ICON, ImageProvider.get("help")); 49 50 this.helpTopic = helpTopic; 51 setEnabled(!Main.isOffline(OnlineResource.JOSM_WEBSITE)); 50 52 } 51 53 -
trunk/src/org/openstreetmap/josm/gui/preferences/SourceEditor.java
r7248 r7434 87 87 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 88 88 import org.openstreetmap.josm.gui.util.FileFilterAllFiles; 89 import org.openstreetmap.josm.gui.util.GuiHelper; 89 90 import org.openstreetmap.josm.gui.util.TableHelper; 90 91 import org.openstreetmap.josm.gui.widgets.JFileChooserManager; 91 92 import org.openstreetmap.josm.gui.widgets.JosmTextField; 92 93 import org.openstreetmap.josm.io.CachedFile; 94 import org.openstreetmap.josm.io.OnlineResource; 93 95 import org.openstreetmap.josm.io.OsmTransferException; 94 96 import org.openstreetmap.josm.tools.GBC; … … 1041 1043 this.url = url; 1042 1044 this.sourceProviders = sourceProviders; 1045 setEnabled(!Main.isOffline(OnlineResource.JOSM_WEBSITE)); 1043 1046 } 1044 1047 … … 1256 1259 String emsg = e.getMessage() != null ? e.getMessage() : e.toString(); 1257 1260 emsg = emsg.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">"); 1258 String msg = tr(getStr(I18nString.FAILED_TO_LOAD_SOURCES_FROM), url, emsg); 1259 1260 HelpAwareOptionPane.showOptionDialog( 1261 Main.parent, 1262 msg, 1263 tr("Error"), 1264 JOptionPane.ERROR_MESSAGE, 1265 ht(getStr(I18nString.FAILED_TO_LOAD_SOURCES_FROM_HELP_TOPIC)) 1266 ); 1261 final String msg = tr(getStr(I18nString.FAILED_TO_LOAD_SOURCES_FROM), url, emsg); 1262 1263 GuiHelper.runInEDT(new Runnable() { 1264 @Override 1265 public void run() { 1266 HelpAwareOptionPane.showOptionDialog( 1267 Main.parent, 1268 msg, 1269 tr("Error"), 1270 JOptionPane.ERROR_MESSAGE, 1271 ht(getStr(I18nString.FAILED_TO_LOAD_SOURCES_FROM_HELP_TOPIC)) 1272 ); 1273 } 1274 }); 1267 1275 } 1268 1276 -
trunk/src/org/openstreetmap/josm/gui/preferences/plugin/PluginPreference.java
r7021 r7434 50 50 import org.openstreetmap.josm.gui.widgets.JosmTextField; 51 51 import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator; 52 import org.openstreetmap.josm.io.OfflineAccessException; 53 import org.openstreetmap.josm.io.OnlineResource; 52 54 import org.openstreetmap.josm.plugins.PluginDownloadTask; 53 55 import org.openstreetmap.josm.plugins.PluginInformation; … … 114 116 return sb.toString(); 115 117 } 116 118 117 119 /** 118 120 * Notifies user about result of a finished plugin download task. … … 313 315 } 314 316 317 private static Collection<String> getOnlinePluginSites() { 318 Collection<String> pluginSites = new ArrayList<>(Main.pref.getPluginSites()); 319 for (Iterator<String> it = pluginSites.iterator(); it.hasNext();) { 320 try { 321 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(it.next(), Main.getJOSMWebsite()); 322 } catch (OfflineAccessException ex) { 323 Main.warn(ex.getMessage()); 324 it.remove(); 325 } 326 } 327 return pluginSites; 328 } 329 315 330 /** 316 331 * The action for downloading the list of available plugins 317 *318 332 */ 319 333 class DownloadAvailablePluginsAction extends AbstractAction { … … 327 341 @Override 328 342 public void actionPerformed(ActionEvent e) { 329 final ReadRemotePluginInformationTask task = new ReadRemotePluginInformationTask(Main.pref.getPluginSites()); 343 Collection<String> pluginSites = getOnlinePluginSites(); 344 if (pluginSites.isEmpty()) { 345 return; 346 } 347 final ReadRemotePluginInformationTask task = new ReadRemotePluginInformationTask(pluginSites); 330 348 Runnable continuation = new Runnable() { 331 349 @Override … … 345 363 Main.worker.submit(continuation); 346 364 } 347 } 348 349 /** 350 * The action for downloading the list of available plugins351 * 365 366 } 367 368 /** 369 * The action for updating the list of selected plugins 352 370 */ 353 371 class UpdateSelectedPluginsAction extends AbstractAction { … … 387 405 ); 388 406 // the async task for downloading plugin information 389 final ReadRemotePluginInformationTask pluginInfoDownloadTask = new ReadRemotePluginInformationTask( Main.pref.getPluginSites());407 final ReadRemotePluginInformationTask pluginInfoDownloadTask = new ReadRemotePluginInformationTask(getOnlinePluginSites()); 390 408 391 409 // to be run asynchronously after the plugin download -
trunk/src/org/openstreetmap/josm/io/CacheCustomContent.java
r7082 r7434 80 80 } 81 81 82 private boolean needsUpdate() { 83 if (isOffline()) { 84 return false; 85 } 86 return Main.pref.getInteger("cache." + ident, 0) + updateInterval < System.currentTimeMillis()/1000 87 || !isCacheValid(); 88 } 89 90 private boolean isOffline() { 91 try { 92 checkOfflineAccess(); 93 return false; 94 } catch (OfflineAccessException e) { 95 return true; 96 } 97 } 98 99 protected void checkOfflineAccess() { 100 // To be overriden by subclasses 101 } 102 82 103 /** 83 104 * Updates data if required … … 85 106 */ 86 107 public byte[] updateIfRequired() throws T { 87 if (Main.pref.getInteger("cache." + ident, 0) + updateInterval < System.currentTimeMillis()/1000 88 || !isCacheValid()) 108 if (needsUpdate()) 89 109 return updateForce(); 90 110 return getData(); … … 96 116 */ 97 117 public String updateIfRequiredString() throws T { 98 if (Main.pref.getInteger("cache." + ident, 0) + updateInterval < System.currentTimeMillis()/1000 99 || !isCacheValid()) 118 if (needsUpdate()) 100 119 return updateForceString(); 101 120 return getDataString(); … … 138 157 */ 139 158 public String getDataString() throws T { 140 return new String(getData(), StandardCharsets.UTF_8); 159 byte[] array = getData(); 160 if (array == null) { 161 return null; 162 } 163 return new String(array, StandardCharsets.UTF_8); 141 164 } 142 165 143 166 /** 144 * Tries to load the data using the given ident from disk. If this fails, data will be updated 167 * Tries to load the data using the given ident from disk. If this fails, data will be updated, unless run in offline mode 145 168 */ 146 169 private void loadFromDisk() throws T { … … 149 172 input.read(this.data); 150 173 } catch (IOException e) { 151 this.data = updateForce(); 174 if (!isOffline()) { 175 this.data = updateForce(); 176 } 152 177 } 153 178 } … … 166 191 167 192 /** 168 * Flushes the data from memory. Class automatically reloads it from disk or updateData() if 169 * required 193 * Flushes the data from memory. Class automatically reloads it from disk or updateData() if required 170 194 */ 171 195 public void flushData() { -
trunk/src/org/openstreetmap/josm/io/CachedFile.java
r7282 r7434 24 24 25 25 import org.openstreetmap.josm.Main; 26 import org.openstreetmap.josm.tools.CheckParameterUtil; 26 27 import org.openstreetmap.josm.tools.Pair; 27 28 import org.openstreetmap.josm.tools.Utils; … … 29 30 /** 30 31 * Downloads a file and caches it on disk in order to reduce network load. 31 * 32 * 32 33 * Supports URLs, local files, and a custom scheme (<code>resource:</code>) to get 33 34 * resources from the current *.jar file. (Local caching is only done for URLs.) … … 49 50 * consider the cache stale and try to download the file again. 50 51 */ 51 MaxAge, 52 MaxAge, 52 53 /** 53 54 * Similar to MaxAge, considers the cache stale when a certain age is … … 56 57 * as a full download. 57 58 */ 58 IfModifiedSince 59 IfModifiedSince 59 60 } 60 61 protected String name; … … 63 64 protected String httpAccept; 64 65 protected CachingStrategy cachingStrategy; 65 66 66 67 protected File cacheFile = null; 67 68 boolean initialized = false; … … 98 99 return this; 99 100 } 100 101 101 102 /** 102 103 * Set maximum age of cache file. Only applies to URLs. … … 201 202 cacheFile = checkLocal(url); 202 203 } 203 } catch ( java.net.MalformedURLException e) {204 } catch (MalformedURLException e) { 204 205 if (name.startsWith("resource://")) { 205 206 return null; … … 211 212 } 212 213 if (cacheFile == null) 213 throw new IOException( );214 throw new IOException("Unable to get cache file for "+name); 214 215 return cacheFile; 215 216 } 216 217 217 218 /** 218 219 * Looks for a certain entry inside a zip file and returns the entry path. … … 292 293 * Clear the cache for the given resource. 293 294 * This forces a fresh download. 294 * @param name the URL 295 * @param name the URL 295 296 */ 296 297 public static void cleanup(String name) { … … 341 342 private File checkLocal(URL url) throws IOException { 342 343 String prefKey = getPrefKey(url, destDir); 344 String urlStr = url.toExternalForm(); 343 345 long age = 0L; 344 346 long lMaxAge = maxAge; … … 346 348 File localFile = null; 347 349 List<String> localPathEntry = new ArrayList<>(Main.pref.getCollection(prefKey)); 350 boolean offline = false; 351 try { 352 checkOfflineAccess(urlStr); 353 } catch (OfflineAccessException e) { 354 offline = true; 355 } 348 356 if (localPathEntry.size() == 2) { 349 357 localFile = new File(localPathEntry.get(1)); 350 if (!localFile.exists())358 if (!localFile.exists()) { 351 359 localFile = null; 352 else {360 } else { 353 361 if ( maxAge == DEFAULT_MAXTIME 354 362 || maxAge <= 0 // arbitrary value <= 0 is deprecated … … 357 365 } 358 366 age = System.currentTimeMillis() - Long.parseLong(localPathEntry.get(0)); 359 if ( age < lMaxAge*1000) {367 if (offline || age < lMaxAge*1000) { 360 368 return localFile; 361 369 } … … 373 381 destDirFile.mkdirs(); 374 382 } 375 376 String a = url.toString().replaceAll("[^A-Za-z0-9_.-]", "_"); 383 384 // No local file + offline => nothing to do 385 if (offline) { 386 return null; 387 } 388 389 String a = urlStr.replaceAll("[^A-Za-z0-9_.-]", "_"); 377 390 String localPath = "mirror_" + a; 378 391 destDirFile = new File(destDir, localPath + ".tmp"); … … 380 393 HttpURLConnection con = connectFollowingRedirect(url, httpAccept, ifModifiedSince); 381 394 if (ifModifiedSince != null && con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { 382 Main.debug("304 Not Modified ("+url+")"); 383 if (localFile == null) throw new AssertionError(); 384 Main.pref.putCollection(prefKey, 395 if (Main.isDebugEnabled()) { 396 Main.debug("304 Not Modified ("+urlStr+")"); 397 } 398 if (localFile == null) 399 throw new AssertionError(); 400 Main.pref.putCollection(prefKey, 385 401 Arrays.asList(Long.toString(System.currentTimeMillis()), localPathEntry.get(1))); 386 402 return localFile; 387 } 403 } 388 404 try ( 389 405 InputStream bis = new BufferedInputStream(con.getInputStream()); … … 398 414 } 399 415 localFile = new File(destDir, localPath); 400 if (Main.platform.rename(destDirFile, localFile)) {401 Main.pref.putCollection(prefKey, 416 if (Main.platform.rename(destDirFile, localFile)) { 417 Main.pref.putCollection(prefKey, 402 418 Arrays.asList(Long.toString(System.currentTimeMillis()), localFile.toString())); 403 419 } else { … … 407 423 } catch (IOException e) { 408 424 if (age >= lMaxAge*1000 && age < lMaxAge*1000*2) { 409 Main.warn(tr("Failed to load {0}, use cached file and retry next time: {1}", url , e));425 Main.warn(tr("Failed to load {0}, use cached file and retry next time: {1}", urlStr, e)); 410 426 return localFile; 411 427 } else { … … 415 431 416 432 return localFile; 433 } 434 435 private static void checkOfflineAccess(String urlString) { 436 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(urlString, Main.getJOSMWebsite()); 437 OnlineResource.OSM_API.checkOfflineAccess(urlString, Main.pref.get("osm-server.url", OsmApi.DEFAULT_API_URL)); 417 438 } 418 439 … … 424 445 * is going from a http to a https URL, see <a href="https://bugs.openjdk.java.net/browse/JDK-4620571">bug report</a>. 425 446 * <p> 426 * This can cause sproblems when downloading from certain GitHub URLs.447 * This can cause problems when downloading from certain GitHub URLs. 427 448 * 428 449 * @param downloadUrl The resource URL to download … … 432 453 * @throws MalformedURLException If a redirected URL is wrong 433 454 * @throws IOException If any I/O operation goes wrong 455 * @throws OfflineAccessException if resource is accessed in offline mode, in any protocol 434 456 * @since 6867 435 457 */ 436 458 public static HttpURLConnection connectFollowingRedirect(URL downloadUrl, String httpAccept, Long ifModifiedSince) throws MalformedURLException, IOException { 459 CheckParameterUtil.ensureParameterNotNull(downloadUrl, "downloadUrl"); 460 String downloadString = downloadUrl.toExternalForm(); 461 462 checkOfflineAccess(downloadString); 463 437 464 HttpURLConnection con = null; 438 465 int numRedirects = 0; 439 466 while(true) { 440 467 con = Utils.openHttpConnection(downloadUrl); 468 if (con == null) { 469 throw new IOException("Cannot open http connection to "+downloadString); 470 } 441 471 if (ifModifiedSince != null) { 442 472 con.setIfModifiedSince(ifModifiedSince); … … 445 475 con.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000); 446 476 con.setReadTimeout(Main.pref.getInteger("socket.timeout.read",30)*1000); 447 Main.debug("GET "+downloadUrl); 477 if (Main.isDebugEnabled()) { 478 Main.debug("GET "+downloadString); 479 } 448 480 if (httpAccept != null) { 449 Main.debug("Accept: "+httpAccept); 481 if (Main.isTraceEnabled()) { 482 Main.trace("Accept: "+httpAccept); 483 } 450 484 con.setRequestProperty("Accept", httpAccept); 451 485 } … … 466 500 case HttpURLConnection.HTTP_SEE_OTHER: 467 501 String redirectLocation = con.getHeaderField("Location"); 468 if (downloadUrl == null) { 469 /* I18n: argument is HTTP response code */ String msg = tr("Unexpected response from HTTP server. Got {0} response without ''Location'' header. Can''t redirect. Aborting.", con.getResponseCode()); 502 if (redirectLocation == null) { 503 /* I18n: argument is HTTP response code */ 504 String msg = tr("Unexpected response from HTTP server. Got {0} response without ''Location'' header."+ 505 " Can''t redirect. Aborting.", con.getResponseCode()); 470 506 throw new IOException(msg); 471 507 } 472 508 downloadUrl = new URL(redirectLocation); 509 downloadString = downloadUrl.toExternalForm(); 473 510 // keep track of redirect attempts to break a redirect loops if it happens 474 511 // to occur for whatever reason … … 478 515 throw new IOException(msg); 479 516 } 480 Main.info(tr("Download redirected to ''{0}''", download Url));517 Main.info(tr("Download redirected to ''{0}''", downloadString)); 481 518 break; 482 519 default: 483 String msg = tr("Failed to read from ''{0}''. Server responded with status code {1}.", download Url, con.getResponseCode());520 String msg = tr("Failed to read from ''{0}''. Server responded with status code {1}.", downloadString, con.getResponseCode()); 484 521 throw new IOException(msg); 485 522 } 486 523 } 487 524 } 488 489 525 } -
trunk/src/org/openstreetmap/josm/io/MessageNotifier.java
r6897 r7434 40 40 // Hide default constructor for utils classes 41 41 } 42 42 43 43 /** Property defining if this task is enabled or not */ 44 44 public static final BooleanProperty PROP_NOTIFIER_ENABLED = new BooleanProperty("message.notifier.enabled", true); 45 45 /** Property defining the update interval in minutes */ 46 46 public static final IntegerProperty PROP_INTERVAL = new IntegerProperty("message.notifier.interval", 5); 47 47 48 48 private static final ScheduledExecutorService EXECUTOR = Executors.newSingleThreadScheduledExecutor(); 49 49 50 50 private static final Runnable WORKER = new Worker(); 51 51 52 52 private static ScheduledFuture<?> task = null; 53 53 54 54 private static class Worker implements Runnable { 55 55 … … 82 82 } 83 83 } 84 84 85 85 /** 86 86 * Starts the message notifier task if not already started and if user is fully identified … … 88 88 public static void start() { 89 89 int interval = PROP_INTERVAL.get(); 90 if (!isRunning() && interval > 0 && isUserEnoughIdentified()) { 90 if (Main.isOffline(OnlineResource.OSM_API)) { 91 Main.info(tr("{0} not available (offline mode)", tr("Message notifier"))); 92 } else if (!isRunning() && interval > 0 && isUserEnoughIdentified()) { 91 93 task = EXECUTOR.scheduleAtFixedRate(WORKER, 0, interval * 60, TimeUnit.SECONDS); 92 94 Main.info("Message notifier active (checks every "+interval+" minute"+(interval>1?"s":"")+")"); … … 104 106 } 105 107 } 106 108 107 109 /** 108 110 * Determines if the message notifier is currently running … … 112 114 return task != null; 113 115 } 114 116 115 117 /** 116 118 * Determines if user set enough information in JOSM preferences to make the request to OSM API without -
trunk/src/org/openstreetmap/josm/io/OsmApi.java
r7082 r7434 98 98 } 99 99 100 private static String getServerUrlFromPref() { 101 return Main.pref.get("osm-server.url", DEFAULT_API_URL); 102 } 103 100 104 /** 101 105 * Replies the {@link OsmApi} for the URL given by the preference <code>osm-server.url</code> … … 104 108 */ 105 109 public static OsmApi getOsmApi() { 106 String serverUrl = Main.pref.get("osm-server.url", DEFAULT_API_URL); 107 return getOsmApi(serverUrl); 110 return getOsmApi(getServerUrlFromPref()); 108 111 } 109 112 … … 180 183 private class CapabilitiesCache extends CacheCustomContent<OsmTransferException> { 181 184 185 private static final String CAPABILITIES = "capabilities"; 186 182 187 ProgressMonitor monitor; 183 188 boolean fastFail; 184 189 185 190 public CapabilitiesCache(ProgressMonitor monitor, boolean fastFail) { 186 super( "capabilities"+ getBaseUrl().hashCode(), CacheCustomContent.INTERVAL_WEEKLY);191 super(CAPABILITIES + getBaseUrl().hashCode(), CacheCustomContent.INTERVAL_WEEKLY); 187 192 this.monitor = monitor; 188 193 this.fastFail = fastFail; … … 190 195 191 196 @Override 197 protected void checkOfflineAccess() { 198 OnlineResource.OSM_API.checkOfflineAccess(getBaseUrl(getServerUrlFromPref(), "0.6")+CAPABILITIES, getServerUrlFromPref()); 199 } 200 201 @Override 192 202 protected byte[] updateData() throws OsmTransferException { 193 return sendRequest("GET", "capabilities", null, monitor, false, fastFail).getBytes(StandardCharsets.UTF_8);203 return sendRequest("GET", CAPABILITIES, null, monitor, false, fastFail).getBytes(StandardCharsets.UTF_8); 194 204 } 195 205 } … … 217 227 if (initialized) 218 228 return; 229 if (Main.isOffline(OnlineResource.OSM_API)) { 230 // At this point this is an error because all automatic or UI actions requiring OSM API should have been disabled earlier 231 throw new OfflineAccessException(tr("{0} not available (offline mode)", OnlineResource.OSM_API.getLocName())); 232 } 219 233 cancel = false; 220 234 try { … … 324 338 } 325 339 326 /** 327 * Returns the base URL for API requests, including the negotiated version number. 328 * @return base URL string 329 */ 330 public String getBaseUrl() { 340 private static String getBaseUrl(String serverUrl, String version) { 331 341 StringBuilder rv = new StringBuilder(serverUrl); 332 342 if (version != null) { … … 339 349 int p; while ((p = rv.indexOf("//", rv.indexOf("://")+2)) > -1) { rv.delete(p, p + 1); } 340 350 return rv.toString(); 351 } 352 353 /** 354 * Returns the base URL for API requests, including the negotiated version number. 355 * @return base URL string 356 */ 357 public String getBaseUrl() { 358 return getBaseUrl(serverUrl, version); 341 359 } 342 360 -
trunk/src/org/openstreetmap/josm/io/OsmServerReader.java
r7267 r7434 118 118 protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor, String reason, boolean uncompressAccordingToContentDisposition) throws OsmTransferException { 119 119 try { 120 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(urlStr, Main.getJOSMWebsite()); 121 OnlineResource.OSM_API.checkOfflineAccess(urlStr, Main.pref.get("osm-server.url", OsmApi.DEFAULT_API_URL)); 122 120 123 URL url = null; 121 124 try { -
trunk/src/org/openstreetmap/josm/plugins/PluginHandler.java
r7424 r7434 63 63 import org.openstreetmap.josm.gui.widgets.JMultilineLabel; 64 64 import org.openstreetmap.josm.gui.widgets.JosmTextArea; 65 import org.openstreetmap.josm.io.OfflineAccessException; 66 import org.openstreetmap.josm.io.OnlineResource; 65 67 import org.openstreetmap.josm.tools.GBC; 66 68 import org.openstreetmap.josm.tools.I18n; … … 303 305 */ 304 306 public static boolean checkAndConfirmPluginUpdate(Component parent) { 307 if (!checkOfflineAccess()) { 308 Main.info(tr("{0} not available (offline mode)", tr("Plugin update"))); 309 return false; 310 } 305 311 String message = null; 306 312 String togglePreferenceKey = null; … … 403 409 } 404 410 return ret == 0; 411 } 412 413 private static boolean checkOfflineAccess() { 414 if (Main.isOffline(OnlineResource.ALL)) { 415 return false; 416 } 417 if (Main.isOffline(OnlineResource.JOSM_WEBSITE)) { 418 for (String updateSite : Main.pref.getPluginSites()) { 419 try { 420 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(updateSite, Main.getJOSMWebsite()); 421 } catch (OfflineAccessException e) { 422 if (Main.isTraceEnabled()) { 423 Main.trace(e.getMessage()); 424 } 425 return false; 426 } 427 } 428 } 429 return true; 405 430 } 406 431 -
trunk/src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java
r7082 r7434 60 60 protected enum CacheType {PLUGIN_LIST, ICON_LIST} 61 61 62 protected final void init(Collection<String> sites, boolean displayErrMsg) {62 protected final void init(Collection<String> sites, boolean displayErrMsg) { 63 63 this.sites = sites; 64 64 if (sites == null) { … … 68 68 this.displayErrMsg = displayErrMsg; 69 69 } 70 /** 71 * Creates the task 70 71 /** 72 * Constructs a new {@code ReadRemotePluginInformationTask}. 72 73 * 73 74 * @param sites the collection of download sites. Defaults to the empty collection if null. … … 79 80 80 81 /** 81 * C reates the task82 * Constructs a new {@code ReadRemotePluginInformationTask}. 82 83 * 83 84 * @param monitor the progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null … … 104 105 105 106 /** 106 * Creates the file name for the cached plugin list and the icon cache 107 * file. 107 * Creates the file name for the cached plugin list and the icon cache file. 108 108 * 109 109 * @param site the name of the site … … 403 403 siteCacheFiles.remove(createSiteCacheFile(pluginDir, site, CacheType.PLUGIN_LIST)); 404 404 siteCacheFiles.remove(createSiteCacheFile(pluginDir, site, CacheType.ICON_LIST)); 405 if(list != null) 406 { 405 if (list != null) { 407 406 getProgressMonitor().worked(1); 408 407 cachePluginList(site, list); … … 416 415 downloadPluginIcons(site+"-icons.zip", createSiteCacheFile(pluginDir, site, CacheType.ICON_LIST), getProgressMonitor().createSubTaskMonitor(0, false)); 417 416 } 418 for (File file: siteCacheFiles) /* remove old stuff or whole update process is broken */419 {417 // remove old stuff or whole update process is broken 418 for (File file: siteCacheFiles) { 420 419 file.delete(); 421 420 } -
trunk/src/org/openstreetmap/josm/tools/ExceptionUtil.java
r7299 r7434 28 28 import org.openstreetmap.josm.io.IllegalDataException; 29 29 import org.openstreetmap.josm.io.MissingOAuthAccessTokenException; 30 import org.openstreetmap.josm.io.OfflineAccessException; 30 31 import org.openstreetmap.josm.io.OsmApi; 31 32 import org.openstreetmap.josm.io.OsmApiException; … … 496 497 497 498 /** 499 * Explains a {@link OfflineAccessException} which has caused an {@link OsmTransferException}. 500 * This is most likely happening when JOSM tries to access OSM API or JOSM website while in offline mode. 501 * 502 * @param e the exception 503 * @return The HTML formatted error message to display 504 * @since 7434 505 */ 506 public static String explainOfflineAccessException(OsmTransferException e) { 507 OfflineAccessException oae = getNestedException(e, OfflineAccessException.class); 508 Main.error(e); 509 return tr("<html>Failed to download data.<br>" 510 + "<br>Details: {0}</html>", oae.getMessage()); 511 } 512 513 /** 498 514 * Explains a {@link OsmApiException} which was thrown because of an internal server 499 515 * error in the OSM API server.. -
trunk/src/org/openstreetmap/josm/tools/WikiReader.java
r7401 r7434 30 30 */ 31 31 public WikiReader() { 32 this.baseurl = Main.pref.get("help.baseurl", Main.getJOSMWebsite()); 32 this(Main.pref.get("help.baseurl", Main.getJOSMWebsite())); 33 } 34 35 /** 36 * Returns the base URL of wiki. 37 * @return the base URL of wiki 38 * @since 7434 39 */ 40 public final String getBaseUrlWiki() { 41 return baseurl + "/wiki/"; 33 42 } 34 43 … … 46 55 try (BufferedReader in = Utils.openURLReader(u)) { 47 56 boolean txt = url.endsWith("?format=txt"); 48 if (url.startsWith( baseurl) && !txt)57 if (url.startsWith(getBaseUrlWiki()) && !txt) 49 58 return readFromTrac(in, u); 50 59 return readNormal(in, !txt); … … 64 73 languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.DEFAULTNOTENGLISH); 65 74 if(languageCode != null) { 66 res = readLang(new URL( baseurl + "/wiki/"+ languageCode + text));75 res = readLang(new URL(getBaseUrlWiki() + languageCode + text)); 67 76 } 68 77 … … 70 79 languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.BASELANGUAGE); 71 80 if(languageCode != null) { 72 res = readLang(new URL( baseurl + "/wiki/"+ languageCode + text));81 res = readLang(new URL(getBaseUrlWiki() + languageCode + text)); 73 82 } 74 83 } … … 77 86 languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.ENGLISH); 78 87 if(languageCode != null) { 79 res = readLang(new URL( baseurl + "/wiki/"+ languageCode + text));88 res = readLang(new URL(getBaseUrlWiki() + languageCode + text)); 80 89 } 81 90 }
Note:
See TracChangeset
for help on using the changeset viewer.