Changeset 18665 in josm
- Timestamp:
- 2023-02-16T16:01:49+01:00 (14 months ago)
- Location:
- trunk
- Files:
-
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/mapmode/SelectAction.java
r18554 r18665 20 20 import java.util.LinkedList; 21 21 import java.util.List; 22 import java.util.Objects; 22 23 import java.util.Optional; 23 24 … … 195 196 * set would have to be checked. 196 197 */ 197 private transient O ptional<OsmPrimitive> currentHighlight = Optional.empty();198 private transient OsmPrimitive currentHighlight; 198 199 199 200 /** … … 276 277 determineMapMode(c.isPresent()); 277 278 278 Optional<OsmPrimitive> newHighlight = Optional.empty();279 280 279 virtualManager.clear(); 281 280 if (mode == Mode.MOVE && !dragInProgress() && virtualManager.activateVirtualNodeNearPoint(e.getPoint())) { … … 286 285 mv.setNewCursor(SelectActionCursor.virtual_node.cursor(), this); 287 286 // don't highlight anything else if a virtual node will be 288 return repaintIfRequired(n ewHighlight);287 return repaintIfRequired(null); 289 288 } 290 289 … … 293 292 // return early if there can't be any highlights 294 293 if (!drawTargetHighlight || (mode != Mode.MOVE && mode != Mode.SELECT) || !c.isPresent()) 295 return repaintIfRequired(n ewHighlight);294 return repaintIfRequired(null); 296 295 297 296 // CTRL toggles selection, but if while dragging CTRL means merge 298 297 final boolean isToggleMode = platformMenuShortcutKeyMask && !dragInProgress(); 299 if (c.isPresent() && (isToggleMode || !c.get().isSelected())) { 298 OsmPrimitive newHighlight = null; 299 if (isToggleMode || !c.get().isSelected()) { 300 300 // only highlight primitives that will change the selection 301 301 // when clicked. I.e. don't highlight selected elements unless 302 302 // we are in toggle mode. 303 newHighlight = c ;303 newHighlight = c.get(); 304 304 } 305 305 return repaintIfRequired(newHighlight); … … 377 377 ds.clearHighlightedVirtualNodes(); 378 378 } 379 if ( !currentHighlight.isPresent()) {379 if (currentHighlight == null) { 380 380 return needsRepaint; 381 } else { 382 currentHighlight.get().setHighlighted(false); 383 } 384 currentHighlight = Optional.empty(); 381 } 382 currentHighlight.setHighlighted(false); 383 currentHighlight = null; 385 384 return true; 386 385 } 387 386 388 private boolean repaintIfRequired(O ptional<OsmPrimitive>newHighlight) {389 if (!drawTargetHighlight || currentHighlight.equals(newHighlight))387 private boolean repaintIfRequired(OsmPrimitive newHighlight) { 388 if (!drawTargetHighlight || Objects.equals(currentHighlight, newHighlight)) 390 389 return false; 391 currentHighlight.ifPresent(osm -> osm.setHighlighted(false)); 392 newHighlight.ifPresent(osm -> osm.setHighlighted(true)); 390 if (currentHighlight != null) { 391 currentHighlight.setHighlighted(false); 392 } 393 if (newHighlight != null) { 394 newHighlight.setHighlighted(true); 395 } 393 396 currentHighlight = newHighlight; 394 397 return true; … … 533 536 if (p != null) { 534 537 p.setHighlighted(true); 535 currentHighlight = Optional.of(p);538 currentHighlight = p; 536 539 needsRepaint = true; 537 540 } … … 905 908 906 909 static void checkCommandForLargeDistance(Command lastCommand) { 910 if (lastCommand == null) { 911 return; 912 } 907 913 final int moveCount = lastCommand.getParticipatingPrimitives().size(); 908 914 if (lastCommand instanceof MoveCommand) { -
trunk/src/org/openstreetmap/josm/data/oauth/IOAuthAuthorization.java
r18650 r18665 2 2 package org.openstreetmap.josm.data.oauth; 3 3 4 import java.util.Optional; 4 5 import java.util.function.Consumer; 5 6 … … 16 17 * @param scopes The scopes to ask for 17 18 */ 18 void authorize(IOAuthParameters parameters, Consumer< IOAuthToken> consumer, Enum<?>... scopes);19 void authorize(IOAuthParameters parameters, Consumer<Optional<IOAuthToken>> consumer, Enum<?>... scopes); 19 20 } -
trunk/src/org/openstreetmap/josm/data/oauth/IOAuthParameters.java
r18650 r18665 91 91 } 92 92 93 /** 94 * Store the preferences for these parameters 95 */ 93 96 void rememberPreferences(); 94 97 } -
trunk/src/org/openstreetmap/josm/data/oauth/IOAuthToken.java
r18650 r18665 13 13 * Sign a client 14 14 * @param client The client to sign 15 * @throws OAuthException if the OAuth token type is unknown (AKA we do't know how to handle it) 15 16 */ 16 17 void sign(HttpClient client) throws OAuthException; -
trunk/src/org/openstreetmap/josm/data/oauth/OAuth20Authorization.java
r18662 r18665 13 13 import java.util.Map; 14 14 import java.util.Objects; 15 import java.util.Optional; 15 16 import java.util.UUID; 16 17 import java.util.function.Consumer; … … 45 46 46 47 @Override 47 public void authorize(IOAuthParameters parameters, Consumer< IOAuthToken> consumer, Enum<?>... scopes) {48 public void authorize(IOAuthParameters parameters, Consumer<Optional<IOAuthToken>> consumer, Enum<?>... scopes) { 48 49 final String state = UUID.randomUUID().toString(); 49 50 final String codeVerifier = UUID.randomUUID().toString(); // Cryptographically random string (ASCII) … … 62 63 private final String state; 63 64 private final IOAuthParameters parameters; 64 private final Consumer< IOAuthToken> consumer;65 private final Consumer<Optional<IOAuthToken>> consumer; 65 66 private final String codeVerifier; 66 67 67 OAuth20AuthorizationHandler(String state, String codeVerifier, IOAuthParameters parameters, Consumer< IOAuthToken> consumer) {68 OAuth20AuthorizationHandler(String state, String codeVerifier, IOAuthParameters parameters, Consumer<Optional<IOAuthToken>> consumer) { 68 69 this.state = state; 69 70 this.parameters = parameters; … … 98 99 HttpClient.Response response = tradeCodeForToken.getResponse(); 99 100 OAuth20Token oAuth20Token = new OAuth20Token(parameters, response.getContentReader()); 100 consumer.accept( oAuth20Token);101 consumer.accept(Optional.of(oAuth20Token)); 101 102 } catch (IOException | OAuth20Exception e) { 102 consumer.accept( null);103 consumer.accept(Optional.empty()); 103 104 throw new JosmRuntimeException(e); 104 105 } finally { -
trunk/src/org/openstreetmap/josm/data/oauth/OAuth20Token.java
r18650 r18665 20 20 import org.openstreetmap.josm.tools.HttpClient; 21 21 import org.openstreetmap.josm.tools.JosmRuntimeException; 22 import org.openstreetmap.josm.tools.Utils; 22 23 23 24 /** … … 81 82 @Override 82 83 public void sign(HttpClient client) throws OAuthException { 83 if (!this.oauthParameters.getApiUrl().contains(client.getURL().getHost())) { 84 if (!Utils.isBlank(this.oauthParameters.getApiUrl()) 85 && !this.oauthParameters.getApiUrl().contains(client.getURL().getHost())) { 84 86 String host = URI.create(this.oauthParameters.getAccessTokenUrl()).getHost(); 85 87 throw new IllegalArgumentException("Cannot sign URL with token for different host: Expected " + host -
trunk/src/org/openstreetmap/josm/gui/datatransfer/importers/FilePaster.java
r17556 r18665 4 4 import java.awt.datatransfer.DataFlavor; 5 5 import java.awt.datatransfer.UnsupportedFlavorException; 6 import java.io.Closeable; 6 7 import java.io.File; 7 8 import java.io.IOException; … … 15 16 import org.openstreetmap.josm.gui.io.importexport.Options; 16 17 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 18 import org.openstreetmap.josm.tools.Utils; 17 19 18 20 /** … … 32 34 public boolean importData(TransferSupport support, OsmDataLayer layer, EastNorth pasteAt) 33 35 throws UnsupportedFlavorException, IOException { 34 @SuppressWarnings("unchecked") 35 List<File> files = (List<File>) support.getTransferable().getTransferData(df); 36 OpenFileAction.OpenFileTask task = new OpenFileAction.OpenFileTask(files, null); 37 task.setOptions(Options.RECORD_HISTORY); 38 MainApplication.worker.submit(task); 39 return true; 36 final Object data = support.getTransferable().getTransferData(df); 37 if (data instanceof List) { 38 @SuppressWarnings("unchecked") 39 List<File> files = (List<File>) data; 40 OpenFileAction.OpenFileTask task = new OpenFileAction.OpenFileTask(files, null); 41 task.setOptions(Options.RECORD_HISTORY); 42 MainApplication.worker.submit(task); 43 return true; 44 } 45 // We should never hit this code -- Coverity thinks that it is possible for this to be called with a 46 // StringSelection transferable, which is not currently possible with our code. It *could* be done from 47 // a plugin though. 48 if (data instanceof Closeable) { 49 Utils.close((Closeable) data); 50 } 51 throw new UnsupportedFlavorException(df); 40 52 } 41 53 } -
trunk/src/org/openstreetmap/josm/gui/datatransfer/importers/TagTransferPaster.java
r10604 r18665 3 3 4 4 import java.awt.datatransfer.UnsupportedFlavorException; 5 import java.io.Closeable; 5 6 import java.io.IOException; 6 7 import java.util.Map; … … 9 10 10 11 import org.openstreetmap.josm.gui.datatransfer.data.TagTransferData; 12 import org.openstreetmap.josm.tools.Utils; 11 13 12 14 /** … … 25 27 @Override 26 28 protected Map<String, String> getTags(TransferSupport support) throws UnsupportedFlavorException, IOException { 27 TagTransferData data = (TagTransferData) support.getTransferable().getTransferData(df); 28 return data.getTags(); 29 final Object data = support.getTransferable().getTransferData(df); 30 if (data instanceof TagTransferData) { 31 return ((TagTransferData) data).getTags(); 32 } 33 // We should never hit this code -- Coverity thinks that it is possible for this to be called with a 34 // StringSelection transferable, which is not currently possible with our code. It *could* be done from 35 // a plugin though. 36 if (data instanceof Closeable) { 37 Utils.close((Closeable) data); 38 } 39 throw new UnsupportedFlavorException(df); 29 40 } 30 41 } -
trunk/src/org/openstreetmap/josm/gui/dialogs/layer/LayerListTransferHandler.java
r16438 r18665 6 6 import java.awt.datatransfer.Transferable; 7 7 import java.awt.datatransfer.UnsupportedFlavorException; 8 import java.io.Closeable; 8 9 import java.io.IOException; 9 10 import java.util.ArrayList; … … 22 23 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 23 24 import org.openstreetmap.josm.tools.Logging; 25 import org.openstreetmap.josm.tools.Utils; 24 26 25 27 /** … … 32 34 */ 33 35 public class LayerListTransferHandler extends TransferHandler { 36 private static final long serialVersionUID = -5924044609120394316L; 37 34 38 @Override 35 39 public int getSourceActions(JComponent c) { … … 48 52 49 53 private static boolean onlyDataLayersSelected(LayerListModel tableModel) { 50 return tableModel.getSelectedLayers().stream().allMatch( l -> l instanceof OsmDataLayer);54 return tableModel.getSelectedLayers().stream().allMatch(OsmDataLayer.class::isInstance); 51 55 } 52 56 … … 79 83 LayerListModel tableModel = (LayerListModel) ((JTable) support.getComponent()).getModel(); 80 84 81 LayerTransferable.Data layers = (LayerTransferable.Data)support.getTransferable()85 final Object data = support.getTransferable() 82 86 .getTransferData(LayerTransferable.LAYER_DATA); 87 final LayerTransferable.Data layers; 88 if (data instanceof LayerTransferable.Data) { 89 layers = (LayerTransferable.Data) data; 90 } else if (data instanceof Closeable) { 91 // We should never hit this code -- Coverity thinks that it is possible for this to be called with a 92 // StringSelection transferable, which is not currently possible with our code. It *could* be done from 93 // a plugin though. 94 Utils.close((Closeable) data); 95 throw new UnsupportedFlavorException(LayerTransferable.LAYER_DATA); 96 } else { 97 throw new UnsupportedFlavorException(LayerTransferable.LAYER_DATA); 98 } 83 99 84 100 int dropLocation; -
trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Functions.java
r18664 r18665 56 56 * @since 15245 (extracted from {@link ExpressionFactory}) 57 57 */ 58 @SuppressWarnings( "UnusedDeclaration")58 @SuppressWarnings({"UnusedDeclaration", "squid:S100"}) 59 59 public final class Functions { 60 60 … … 313 313 * @param args arguments. First one is used as separator 314 314 * @return assembled string 315 * @see String#join 315 * @see String#join(CharSequence, CharSequence...) 316 316 */ 317 317 @NullableArguments … … 325 325 * @param values collection of objects 326 326 * @return assembled string 327 * @see String#join 327 * @see String#join(CharSequence, Iterable) 328 328 */ 329 329 public static String join_list(final String separator, final List<String> values) { … … 739 739 * @see Object#equals(Object) 740 740 */ 741 @SuppressWarnings("squid:S1221") 741 742 public static boolean equal(Object a, Object b) { 742 743 if (a.getClass() == b.getClass()) return a.equals(b); … … 775 776 776 777 /** 777 * Obtains the JOSM 'key {@link org.openstreetmap.josm.data.Preferences} string for key {@code key},778 * Obtains the JOSM key {@link org.openstreetmap.josm.data.Preferences} string for key {@code key}, 778 779 * and defaults to {@code def} if that is null. 779 780 * -
trunk/src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java
r18662 r18665 118 118 public final void initFromPreferences() { 119 119 final String authMethod = OsmApi.getAuthMethod(); 120 if ("basic".equals(authMethod)) { 121 rbBasicAuthentication.setSelected(true); 122 } else if ("oauth".equals(authMethod)) { 123 rbOAuth.setSelected(true); 124 } else if ("oauth20".equals(authMethod)) { 125 rbOAuth20.setSelected(true); 126 } else { 127 Logging.warn(tr("Unsupported value in preference ''{0}'', got ''{1}''. Using authentication method ''Basic Authentication''.", 128 "osm-server.auth-method", authMethod)); 129 rbBasicAuthentication.setSelected(true); 120 switch (authMethod) { 121 case "basic": 122 rbBasicAuthentication.setSelected(true); 123 break; 124 case "oauth": 125 rbOAuth.setSelected(true); 126 break; 127 case "oauth20": 128 rbOAuth20.setSelected(true); 129 break; 130 default: 131 Logging.warn(tr("Unsupported value in preference ''{0}'', got ''{1}''. Using authentication method ''Basic Authentication''.", 132 "osm-server.auth-method", authMethod)); 133 rbBasicAuthentication.setSelected(true); 130 134 } 131 135 pnlBasicAuthPreferences.initFromPreferences(); -
trunk/src/org/openstreetmap/josm/gui/preferences/server/OAuthAuthenticationPreferencesPanel.java
r18650 r18665 374 374 // Clean up old token/password 375 375 OAuthAccessTokenHolder.getInstance().setAccessToken(null); 376 OAuthAccessTokenHolder.getInstance().setAccessToken(OsmApi.getOsmApi().getServerUrl(), token );376 OAuthAccessTokenHolder.getInstance().setAccessToken(OsmApi.getOsmApi().getServerUrl(), token.orElse(null)); 377 377 OAuthAccessTokenHolder.getInstance().save(CredentialsManager.getInstance()); 378 378 GuiHelper.runInEDT(OAuthAuthenticationPreferencesPanel.this::refreshView); -
trunk/src/org/openstreetmap/josm/io/OsmConnection.java
r18651 r18665 11 11 import java.util.Base64; 12 12 import java.util.Objects; 13 import java.util.Optional; 13 14 import java.util.concurrent.CountDownLatch; 14 15 import java.util.concurrent.TimeUnit; … … 215 216 } 216 217 CountDownLatch done = new CountDownLatch(1); 217 Consumer< IOAuthToken> consumer = authToken -> {218 Consumer<Optional<IOAuthToken>> consumer = authToken -> { 218 219 if (!remoteControlIsRunning) { 219 220 RemoteControl.stop(); … … 221 222 // Clean up old token/password 222 223 OAuthAccessTokenHolder.getInstance().setAccessToken(null); 223 OAuthAccessTokenHolder.getInstance().setAccessToken(OsmApi.getOsmApi().getServerUrl(), authToken );224 OAuthAccessTokenHolder.getInstance().setAccessToken(OsmApi.getOsmApi().getServerUrl(), authToken.orElse(null)); 224 225 OAuthAccessTokenHolder.getInstance().save(CredentialsManager.getInstance()); 225 226 done.countDown(); … … 229 230 OsmScopes.read_prefs, OsmScopes.write_prefs, 230 231 OsmScopes.write_api, OsmScopes.write_notes); 231 synchronized (done) { 232 // Only wait at most 5 minutes 233 int counter = 0; 234 while (done.getCount() >= 0 && counter < 5) { 235 try { 236 if (done.await(1, TimeUnit.MINUTES)) { 237 break; 238 } 239 } catch (InterruptedException e) { 240 Thread.currentThread().interrupt(); 241 Logging.trace(e); 242 consumer.accept(null); 243 throw new MissingOAuthAccessTokenException(e); 232 // Only wait at most 5 minutes 233 int counter = 0; 234 while (done.getCount() >= 0 && counter < 5) { 235 try { 236 if (done.await(1, TimeUnit.MINUTES)) { 237 break; 244 238 } 245 counter++; 246 } 239 } catch (InterruptedException e) { 240 Thread.currentThread().interrupt(); 241 Logging.trace(e); 242 consumer.accept(null); 243 throw new MissingOAuthAccessTokenException(e); 244 } 245 counter++; 247 246 } 248 247 } -
trunk/test/unit/org/openstreetmap/josm/data/oauth/OAuth20AuthorizationTest.java
r18650 r18665 3 3 4 4 import static org.junit.jupiter.api.Assertions.assertEquals; 5 import static org.junit.jupiter.api.Assertions.assertFalse; 5 6 import static org.junit.jupiter.api.Assertions.assertNotNull; 6 import static org.junit.jupiter.api.Assertions.assertNull;7 7 import static org.junit.jupiter.api.Assertions.assertTrue; 8 8 … … 10 10 import java.util.HashMap; 11 11 import java.util.Map; 12 import java.util.Optional; 12 13 import java.util.concurrent.atomic.AtomicReference; 13 14 import java.util.stream.Collectors; … … 166 167 void testAuthorize(WireMockRuntimeInfo wireMockRuntimeInfo) throws IOException { 167 168 final OAuth20Authorization authorization = new OAuth20Authorization(); 168 final AtomicReference< IOAuthToken> consumer = new AtomicReference<>();169 final AtomicReference<Optional<IOAuthToken>> consumer = new AtomicReference<>(); 169 170 OAuth20Parameters parameters = (OAuth20Parameters) OAuthParameters.createDefault(OsmApi.getOsmApi().getBaseUrl(), OAuthVersion.OAuth20); 170 171 RemoteControl.start(); … … 181 182 } 182 183 assertNotNull(consumer.get()); 183 assertEquals(OAuthVersion.OAuth20, consumer.get().getOAuthType()); 184 OAuth20Token token = (OAuth20Token) consumer.get(); 184 assertTrue(consumer.get().isPresent()); 185 assertEquals(OAuthVersion.OAuth20, consumer.get().get().getOAuthType()); 186 OAuth20Token token = (OAuth20Token) consumer.get().get(); 185 187 assertEquals("test_access_token", token.getBearerToken()); 186 188 } … … 190 192 oauthServer.stateToReturn = "Bad_State"; 191 193 final OAuth20Authorization authorization = new OAuth20Authorization(); 192 final AtomicReference< IOAuthToken> consumer = new AtomicReference<>();194 final AtomicReference<Optional<IOAuthToken>> consumer = new AtomicReference<>(); 193 195 OAuth20Parameters parameters = (OAuth20Parameters) OAuthParameters.createDefault(OsmApi.getOsmApi().getBaseUrl(), OAuthVersion.OAuth20); 194 196 RemoteControl.start(); … … 206 208 client.disconnect(); 207 209 } 208 assert Null(consumer.get());210 assertFalse(consumer.get().isPresent()); 209 211 } 210 212 }
Note:
See TracChangeset
for help on using the changeset viewer.