Changeset 36146 in osm for applications/editors/josm
- Timestamp:
- 2023-09-20T17:53:20+02:00 (14 months ago)
- Location:
- applications/editors/josm/plugins/geochat/src/geochat
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/geochat/src/geochat/ChatMessage.java
r35162 r36146 2 2 package geochat; 3 3 4 import java. util.Date;4 import java.time.Instant; 5 5 6 6 import org.openstreetmap.josm.data.coor.LatLon; … … 12 12 */ 13 13 public final class ChatMessage implements Comparable<ChatMessage> { 14 private LatLon pos;15 private Datetime;16 private String author;14 private final LatLon pos; 15 private final Instant time; 16 private final String author; 17 17 private String recipient; 18 private String message;19 private long id;18 private final String message; 19 private final long id; 20 20 private boolean priv; 21 private boolean incoming;21 private final boolean incoming; 22 22 23 public ChatMessage(long id, LatLon pos, String author, boolean incoming, String message, Datetime) {23 public ChatMessage(long id, LatLon pos, String author, boolean incoming, String message, Instant time) { 24 24 this.id = id; 25 25 this.author = author; … … 72 72 } 73 73 74 public DategetTime() {74 public Instant getTime() { 75 75 return time; 76 76 } -
applications/editors/josm/plugins/geochat/src/geochat/ChatPaneManager.java
r35162 r36146 32 32 private static final String PUBLIC_PANE = "Public Pane"; 33 33 34 private GeoChatPanel panel;35 private JTabbedPane tabs;36 private Map<String, ChatPane> chatPanes;34 private final GeoChatPanel panel; 35 private final JTabbedPane tabs; 36 private final Map<String, ChatPane> chatPanes; 37 37 private boolean collapsed; 38 38 … … 80 80 int idx = tabs.indexOfComponent(entry.component); 81 81 if (idx >= 0) 82 GuiHelper.runInEDT(( ) -> ((ChatTabTitleComponent) tabs.getTabComponentAt(idx)).updateAlarm());82 GuiHelper.runInEDT(((ChatTabTitleComponent) tabs.getTabComponentAt(idx))::updateAlarm); 83 83 } 84 84 … … 177 177 if (c == null) 178 178 return null; 179 for ( String user : chatPanes.keySet()) {180 if (c.equals( chatPanes.get(user).component))181 return user;179 for (Map.Entry<String, ChatPaneManager.ChatPane> entry : chatPanes.entrySet()) { 180 if (c.equals(entry.getValue().component)) 181 return entry.getKey(); 182 182 } 183 183 return null; … … 226 226 227 227 private class ChatTabTitleComponent extends JLabel { 228 private ChatPane entry;228 private final ChatPane entry; 229 229 230 230 ChatTabTitleComponent(ChatPane entry) { -
applications/editors/josm/plugins/geochat/src/geochat/ChatServerConnection.java
r36122 r36146 7 7 import java.io.UnsupportedEncodingException; 8 8 import java.net.URLEncoder; 9 import java.net.UnknownHostException; 10 import java.time.Instant; 9 11 import java.util.ArrayList; 10 import java.util.Date;11 12 import java.util.HashMap; 12 13 import java.util.HashSet; … … 14 15 import java.util.Map; 15 16 import java.util.Set; 17 import java.util.concurrent.ScheduledThreadPoolExecutor; 18 import java.util.concurrent.TimeUnit; 16 19 17 20 import jakarta.json.JsonArray; … … 22 25 import org.openstreetmap.josm.data.coor.LatLon; 23 26 import org.openstreetmap.josm.data.coor.conversion.DecimalDegreesCoordinateFormat; 27 import org.openstreetmap.josm.data.preferences.JosmUrls; 24 28 import org.openstreetmap.josm.data.projection.Projection; 25 29 import org.openstreetmap.josm.data.projection.ProjectionRegistry; 26 30 import org.openstreetmap.josm.gui.MainApplication; 27 31 import org.openstreetmap.josm.gui.MapView; 32 import org.openstreetmap.josm.io.NetworkManager; 33 import org.openstreetmap.josm.io.OnlineResource; 28 34 import org.openstreetmap.josm.spi.preferences.Config; 29 35 import org.openstreetmap.josm.tools.Logging; 36 import org.openstreetmap.josm.tools.Utils; 30 37 31 38 /** … … 37 44 public static final String TOKEN_PREFIX = "="; 38 45 private static final String TOKEN_PATTERN = "^[a-zA-Z0-9]{10}$"; 46 private static final ScheduledThreadPoolExecutor EXECUTOR = new ScheduledThreadPoolExecutor(1); 39 47 40 48 private int userId; … … 42 50 private static ChatServerConnection instance; 43 51 private final Set<ChatServerConnectionListener> listeners; 44 private final LogRequest requestThread;45 52 46 53 private ChatServerConnection() { … … 48 55 userName = null; 49 56 listeners = new HashSet<>(); 50 requestThread = new LogRequest(); 51 new Thread(requestThread).start(); 57 LogRequest requestThread = new LogRequest(); 58 final int interval = Config.getPref().getInt("geochat.interval", 2); 59 EXECUTOR.scheduleAtFixedRate(requestThread, interval, interval, TimeUnit.SECONDS); 52 60 } 53 61 … … 116 124 return; 117 125 } 118 new Thread(() -> { 126 // Blocking the geochat executor here isn't a big deal, since we need to be logged in for chat anyway. 127 EXECUTOR.schedule(() -> { 119 128 try { 120 129 int cnt = 10; … … 127 136 } 128 137 autoLogin(userName); 129 } ).start();138 }, 200, TimeUnit.MILLISECONDS); 130 139 } 131 140 … … 315 324 private long lastId; 316 325 private boolean lastStatus; 317 private boolean stopping;318 326 319 327 @Override 320 328 public void run() { 321 329 // lastId = Config.getPref().getLong("geochat.lastid", 0); 322 int interval = Config.getPref().getInt("geochat.interval", 2); 323 while (!stopping) { 330 if (!NetworkManager.isOffline(OnlineResource.JOSM_WEBSITE) || !Utils.isRunningWebStart()) { 324 331 process(); 325 try { 326 Thread.sleep(interval * 1000L); 327 } catch (InterruptedException e) { 328 Thread.currentThread().interrupt(); 329 stopping = true; 330 Logging.trace(e); 331 } 332 } 333 } 334 335 public void stop() { 336 stopping = true; 337 } 338 339 public void process() { 332 } 333 } 334 335 private void process() { 340 336 if (!isLoggedIn()) { 341 337 fireStatusChanged(false); … … 371 367 Logging.trace(ex); 372 368 json = null; // ? 369 final Throwable root = Utils.getRootCause(ex); 370 if (root instanceof UnknownHostException) { 371 UnknownHostException uhe = (UnknownHostException) root; 372 NetworkManager.addNetworkError(uhe.getMessage(), uhe); 373 if (JosmUrls.getInstance().getJOSMWebsite().endsWith(uhe.getMessage())) { 374 NetworkManager.setOffline(OnlineResource.JOSM_WEBSITE); 375 } 376 } 373 377 } 374 378 if (json == null) { … … 424 428 boolean incoming = msg.getBoolean("incoming"); 425 429 ChatMessage cm = new ChatMessage(id, new LatLon(lat, lon), author, 426 incoming, message, new Date(timeStamp * 1000));430 incoming, message, Instant.ofEpochSecond(timeStamp)); 427 431 cm.setPrivate(priv); 428 432 if (msg.get("recipient") != null && !incoming) -
applications/editors/josm/plugins/geochat/src/geochat/GeoChatPanel.java
r35161 r36146 18 18 import java.awt.RenderingHints; 19 19 import java.io.IOException; 20 import java.text.SimpleDateFormat; 20 import java.time.ZoneId; 21 import java.time.format.DateTimeFormatter; 22 import java.time.format.FormatStyle; 21 23 import java.util.List; 22 24 import java.util.Map; … … 51 53 */ 52 54 public class GeoChatPanel extends ToggleDialog implements ChatServerConnectionListener, MapViewPaintable { 53 private JTextField input;54 private JTabbedPane tabs;55 private JComponent noData;56 private JPanel loginPanel;57 private JPanel gcPanel;58 private ChatServerConnection connection;55 private final JTextField input; 56 private final JTabbedPane tabs; 57 private final JComponent noData; 58 private final JPanel loginPanel; 59 private final JPanel gcPanel; 60 private final ChatServerConnection connection; 59 61 // those fields should be visible to popup menu actions 60 62 Map<String, LatLon> users; … … 62 64 boolean userLayerActive; 63 65 66 /** 67 * Create a new {@link GeoChatPanel} 68 */ 64 69 public GeoChatPanel() { 65 70 super(tr("GeoChat"), "geochat", tr("Open GeoChat panel"), null, 200, true); … … 94 99 connection = ChatServerConnection.getInstance(); 95 100 connection.addListener(this); 96 boolean autoLogin = Config.getPref().get("geochat.username", null) == null ? false :Config.getPref().getBoolean("geochat.autologin", true);101 boolean autoLogin = Config.getPref().get("geochat.username", null) != null && Config.getPref().getBoolean("geochat.autologin", true); 97 102 connection.autoLoginWithDelay(autoLogin ? defaultUserName : null); 98 103 updateTitleAlarm(); 99 104 } 100 105 101 private String constructUserName() {106 private static String constructUserName() { 102 107 String userName = Config.getPref().get("geochat.username", null); // so the default is null 103 108 if (userName == null) … … 187 192 FontMetrics fm = g2d.getFontMetrics(); 188 193 189 for ( String user : users.keySet()) {190 int stringWidth = fm.stringWidth( user);194 for (Map.Entry<String, LatLon> entry : users.entrySet()) { 195 int stringWidth = fm.stringWidth(entry.getKey()); 191 196 int radius = stringWidth / 2 + 10; 192 Point p = mv.getPoint( users.get(user));197 Point p = mv.getPoint(entry.getValue()); 193 198 194 199 g2d.setComposite(ac04); … … 198 203 g2d.setComposite(ac10); 199 204 g2d.setColor(Color.black); 200 g2d.drawString( user, p.x - stringWidth / 2, p.y + fm.getDescent());205 g2d.drawString(entry.getKey(), p.x - stringWidth / 2, p.y + fm.getDescent()); 201 206 } 202 207 } … … 214 219 String comment; 215 220 if (connection.isLoggedIn()) { 216 comment = trn("{0} user", "{0} users", users.size() + 1 , users.size() + 1);221 comment = trn("{0} user", "{0} users", users.size() + 1L, users.size() + 1); 217 222 } else { 218 223 comment = tr("not logged in"); … … 293 298 } 294 299 295 private final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm");296 297 private void formatMessage(StringBuilder sb, ChatMessage msg) {300 private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).withZone(ZoneId.systemDefault()); 301 302 private static void formatMessage(StringBuilder sb, ChatMessage msg) { 298 303 sb.append("\n"); 299 304 sb.append('[').append(TIME_FORMAT.format(msg.getTime())).append("] "); … … 338 343 sb.append(first ? " " : ", "); 339 344 sb.append(user); 345 first = false; 340 346 } 341 347 chatPanes.addLineToPublic(sb.toString(), ChatPaneManager.MESSAGE_TYPE_INFORMATION); -
applications/editors/josm/plugins/geochat/src/geochat/GeoChatPopupAdapter.java
r33545 r36146 20 20 */ 21 21 class GeoChatPopupAdapter extends MouseAdapter { 22 private GeoChatPanel panel;22 private final GeoChatPanel panel; 23 23 24 24 GeoChatPopupAdapter(GeoChatPanel panel) { … … 62 62 63 63 private class PrivateChatAction extends AbstractAction { 64 private String userName;64 private final String userName; 65 65 66 66 PrivateChatAction(String userName) { -
applications/editors/josm/plugins/geochat/src/geochat/JPanelTextField.java
r35163 r36146 21 21 public class JPanelTextField extends DisableShortcutsOnFocusGainedTextField { 22 22 23 /** 24 * Create a new {@link JPanelTextField} 25 */ 23 26 public JPanelTextField() { 24 setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, new HashSet< KeyStroke>());27 setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, new HashSet<>()); 25 28 standardKeys = getInputMap(JComponent.WHEN_FOCUSED).allKeys(); 26 29 } … … 51 54 if (start < caret) { 52 55 String word = text.substring(start, caret); 53 String complete = word == null ? null :autoComplete(word, start == 0);56 String complete = autoComplete(word, start == 0); 54 57 if (complete != null && !complete.equals(word)) { 55 58 StringBuilder sb = new StringBuilder(); 56 59 if (start > 0) 57 sb.append(text .substring(0, start));60 sb.append(text, 0, start); 58 61 sb.append(complete); 59 62 if (caret < text.length()) … … 63 66 } 64 67 } 65 } else if (code == KeyEvent.VK_ESCAPE) { 66 if (MainApplication.isDisplayingMapView()) 67 MainApplication.getMap().mapView.requestFocus(); 68 } else if (code == KeyEvent.VK_ESCAPE && MainApplication.isDisplayingMapView()) { 69 MainApplication.getMap().mapView.requestFocus(); 68 70 } 69 71 … … 89 91 * @param text Contents of the text field. 90 92 */ 91 protected void processEnter(String text) { } 93 protected void processEnter(String text) { 94 // Overridden where needed 95 } 92 96 93 97 /** -
applications/editors/josm/plugins/geochat/src/geochat/JsonQueryUtil.java
r36122 r36146 6 6 import java.io.InputStream; 7 7 import java.net.URI; 8 import java.util.ServiceConfigurationError; 8 9 9 10 import jakarta.json.Json; … … 52 53 if (inp == null) 53 54 throw new IOException("Empty response"); 54 try (JsonReader reader = Json.createReader(inp)) {55 try (JsonReader reader = Json.createReader(inp)) { 55 56 return reader.readObject(); 56 } catch ( JsonException e) {57 } catch (ServiceConfigurationError | JsonException e) { 57 58 throw new IOException("Failed to parse JSON: " + e.getMessage(), e); 58 59 } finally { … … 63 64 // Asynchronous operation 64 65 65 private String query; 66 private JsonQueryCallback callback; 67 68 private JsonQueryUtil() {} 66 private final String query; 67 private final JsonQueryCallback callback; 69 68 70 69 private JsonQueryUtil(String query, JsonQueryCallback callback) {
Note:
See TracChangeset
for help on using the changeset viewer.