Changeset 32544 in osm for applications/editors/josm/plugins/geochat/src
- Timestamp:
- 2016-07-03T21:26:31+02:00 (8 years ago)
- Location:
- applications/editors/josm/plugins/geochat/src/geochat
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/geochat/src/geochat/ChatMessage.java
r29854 r32544 1 // License: WTFPL 1 // License: WTFPL. For details, see LICENSE file. 2 2 package geochat; 3 3 4 4 import java.util.Date; 5 5 6 import org.openstreetmap.josm.data.coor.LatLon; 6 7 7 8 /** 8 9 * One message. 9 * 10 * 10 11 * @author zverik 11 12 */ … … 20 21 private boolean incoming; 21 22 22 public ChatMessage( long id, LatLon pos, String author, boolean incoming, String message, Date time) {23 public ChatMessage(long id, LatLon pos, String author, boolean incoming, String message, Date time) { 23 24 this.id = id; 24 25 this.author = author; … … 31 32 } 32 33 33 public void setRecipient( String recipient) {34 public void setRecipient(String recipient) { 34 35 this.recipient = recipient; 35 36 } 36 37 37 public void setPrivate( boolean priv) {38 public void setPrivate(boolean priv) { 38 39 this.priv = priv; 39 40 } 40 41 41 42 public String getAuthor() { 42 43 return author; … … 61 62 return message; 62 63 } 63 64 64 65 public boolean isPrivate() { 65 66 return priv; … … 96 97 } 97 98 99 @Override 98 100 public int compareTo(ChatMessage o) { 99 101 long otherId = o.id; -
applications/editors/josm/plugins/geochat/src/geochat/ChatPaneManager.java
r30737 r32544 1 // License: WTFPL 1 // License: WTFPL. For details, see LICENSE file. 2 2 package geochat; 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 3 5 4 6 import java.awt.Color; 5 7 import java.awt.Component; 6 8 import java.awt.Font; 7 import java.util.*; 8 import javax.swing.*; 9 import java.util.ArrayList; 10 import java.util.HashMap; 11 import java.util.List; 12 import java.util.Map; 13 14 import javax.swing.JLabel; 15 import javax.swing.JScrollPane; 16 import javax.swing.JTabbedPane; 17 import javax.swing.JTextPane; 9 18 import javax.swing.event.ChangeEvent; 10 19 import javax.swing.event.ChangeListener; 11 import javax.swing.text.*; 20 import javax.swing.text.BadLocationException; 21 import javax.swing.text.Document; 22 import javax.swing.text.SimpleAttributeSet; 23 import javax.swing.text.StyleConstants; 24 12 25 import org.openstreetmap.josm.Main; 13 26 import org.openstreetmap.josm.gui.util.GuiHelper; 14 import static org.openstreetmap.josm.tools.I18n.tr;15 27 16 28 /** … … 26 38 private boolean collapsed; 27 39 28 public ChatPaneManager( GeoChatPanel panel, JTabbedPane tabs) {40 ChatPaneManager(GeoChatPanel panel, JTabbedPane tabs) { 29 41 this.panel = panel; 30 42 this.tabs = tabs; … … 33 45 createChatPane(null); 34 46 tabs.addChangeListener(new ChangeListener() { 35 public void stateChanged( ChangeEvent e ) { 47 @Override 48 public void stateChanged(ChangeEvent e) { 36 49 updateActiveTabStatus(); 37 50 } … … 39 52 } 40 53 41 public void setCollapsed( boolean collapsed) {54 public void setCollapsed(boolean collapsed) { 42 55 this.collapsed = collapsed; 43 56 updateActiveTabStatus(); 44 57 } 45 58 46 public boolean hasUser( String user) {59 public boolean hasUser(String user) { 47 60 return chatPanes.containsKey(user == null ? PUBLIC_PANE : user); 48 61 } … … 54 67 public int getNotifyLevel() { 55 68 int alarm = 0; 56 for ( ChatPane entry : chatPanes.values()) {57 if ( entry.notify > alarm)69 for (ChatPane entry : chatPanes.values()) { 70 if (entry.notify > alarm) 58 71 alarm = entry.notify; 59 72 } … … 62 75 63 76 public void updateActiveTabStatus() { 64 if ( tabs.getSelectedIndex() >= 0)65 ((ChatTabTitleComponent) tabs.getTabComponentAt(tabs.getSelectedIndex())).updateAlarm();66 } 67 68 public void notify( String user, int alarmLevel) {69 if ( alarmLevel <= 0 || !hasUser(user))77 if (tabs.getSelectedIndex() >= 0) 78 ((ChatTabTitleComponent) tabs.getTabComponentAt(tabs.getSelectedIndex())).updateAlarm(); 79 } 80 81 public void notify(String user, int alarmLevel) { 82 if (alarmLevel <= 0 || !hasUser(user)) 70 83 return; 71 84 ChatPane entry = chatPanes.get(user == null ? PUBLIC_PANE : user); 72 85 entry.notify = alarmLevel; 73 86 int idx = tabs.indexOfComponent(entry.component); 74 if ( idx >= 0)75 ((ChatTabTitleComponent) tabs.getTabComponentAt(idx)).updateAlarm();87 if (idx >= 0) 88 ((ChatTabTitleComponent) tabs.getTabComponentAt(idx)).updateAlarm(); 76 89 } 77 90 … … 81 94 private static Color COLOR_ATTENTION = new Color(0, 0, 192); 82 95 83 private void addLineToChatPane( String userName, String line, final int messageType) {84 if ( line.length() == 0)96 private void addLineToChatPane(String userName, String line, final int messageType) { 97 if (line.length() == 0) 85 98 return; 86 if ( !chatPanes.containsKey(userName))99 if (!chatPanes.containsKey(userName)) 87 100 createChatPane(userName); 88 101 final String nline = line.startsWith("\n") ? line : "\n" + line; 89 102 final JTextPane thepane = chatPanes.get(userName).pane; 90 103 GuiHelper.runInEDT(new Runnable() { 104 @Override 91 105 public void run() { 92 106 Document doc = thepane.getDocument(); 93 107 try { 94 108 SimpleAttributeSet attrs = null; 95 if ( messageType != MESSAGE_TYPE_DEFAULT) {109 if (messageType != MESSAGE_TYPE_DEFAULT) { 96 110 attrs = new SimpleAttributeSet(); 97 if ( messageType == MESSAGE_TYPE_INFORMATION)111 if (messageType == MESSAGE_TYPE_INFORMATION) 98 112 StyleConstants.setItalic(attrs, true); 99 else if ( messageType == MESSAGE_TYPE_ATTENTION)113 else if (messageType == MESSAGE_TYPE_ATTENTION) 100 114 StyleConstants.setForeground(attrs, COLOR_ATTENTION); 101 115 } 102 116 doc.insertString(doc.getLength(), nline, attrs); 103 } catch ( BadLocationException ex) {104 // whatever117 } catch (BadLocationException ex) { 118 Main.warn(ex); 105 119 } 106 120 thepane.setCaretPosition(doc.getLength()); … … 109 123 } 110 124 111 public void addLineToChatPane( String userName, String line) {125 public void addLineToChatPane(String userName, String line) { 112 126 addLineToChatPane(userName, line, MESSAGE_TYPE_DEFAULT); 113 127 } 114 128 115 public void addLineToPublic( String line) {129 public void addLineToPublic(String line) { 116 130 addLineToChatPane(PUBLIC_PANE, line); 117 131 } 118 132 119 public void addLineToPublic( String line, int messageType) {133 public void addLineToPublic(String line, int messageType) { 120 134 addLineToChatPane(PUBLIC_PANE, line, messageType); 121 135 } … … 125 139 } 126 140 127 public void clearChatPane( 128 if ( userName == null || userName.equals(PUBLIC_PANE))141 public void clearChatPane(String userName) { 142 if (userName == null || userName.equals(PUBLIC_PANE)) 129 143 clearPublicChatPane(); 130 144 else … … 136 150 } 137 151 138 public ChatPane createChatPane( String userName) {152 public ChatPane createChatPane(String userName) { 139 153 JTextPane chatPane = new JTextPane(); 140 154 chatPane.setEditable(false); 141 155 Font font = chatPane.getFont(); 142 156 float size = Main.pref.getInteger("geochat.fontsize", -1); 143 if ( size < 6)157 if (size < 6) 144 158 size += font.getSize2D(); 145 159 chatPane.setFont(font.deriveFont(size)); 146 // DefaultCaret caret = (DefaultCaret)chatPane.getCaret(); // does not work147 // caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);160 // DefaultCaret caret = (DefaultCaret)chatPane.getCaret(); // does not work 161 // caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); 148 162 JScrollPane scrollPane = new JScrollPane(chatPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 149 163 chatPane.addMouseListener(new GeoChatPopupAdapter(panel)); … … 169 183 public String getActiveChatPane() { 170 184 Component c = tabs.getSelectedComponent(); 171 if ( c == null)185 if (c == null) 172 186 return null; 173 for ( String user : chatPanes.keySet() )174 if ( c.equals(chatPanes.get(user).component))187 for (String user : chatPanes.keySet()) { 188 if (c.equals(chatPanes.get(user).component)) 175 189 return user; 190 } 176 191 return null; 177 192 } … … 182 197 } 183 198 184 public void closeChatPane( String user) {185 if ( user == null || user.equals(PUBLIC_PANE) || !chatPanes.containsKey(user))199 public void closeChatPane(String user) { 200 if (user == null || user.equals(PUBLIC_PANE) || !chatPanes.containsKey(user)) 186 201 return; 187 202 tabs.remove(chatPanes.get(user).component); … … 191 206 public void closeSelectedPrivatePane() { 192 207 String pane = getRecipient(); 193 if ( pane != null)208 if (pane != null) 194 209 closeChatPane(pane); 195 210 } … … 197 212 public void closePrivateChatPanes() { 198 213 List<String> entries = new ArrayList<>(chatPanes.keySet()); 199 for ( String user : entries )200 if ( !user.equals(PUBLIC_PANE))214 for (String user : entries) { 215 if (!user.equals(PUBLIC_PANE)) 201 216 closeChatPane(user); 202 } 203 217 } 218 } 219 204 220 public boolean hasSelectedText() { 205 221 String user = getActiveChatPane(); 206 if ( user != null) {222 if (user != null) { 207 223 JTextPane pane = chatPanes.get(user).pane; 208 224 return pane.getSelectedText() != null; … … 213 229 public void copySelectedText() { 214 230 String user = getActiveChatPane(); 215 if ( user != null)231 if (user != null) 216 232 chatPanes.get(user).pane.copy(); 217 233 } 218 219 234 220 235 private class ChatTabTitleComponent extends JLabel { 221 236 private ChatPane entry; 222 237 223 public ChatTabTitleComponent( ChatPane entry) {238 ChatTabTitleComponent(ChatPane entry) { 224 239 super(entry.isPublic ? tr("Public") : entry.userName); 225 240 this.entry = entry; … … 230 245 231 246 public void updateAlarm() { 232 if ( normalFont == null) {247 if (normalFont == null) { 233 248 // prepare cached fonts 234 249 normalFont = getFont().deriveFont(Font.PLAIN); 235 250 boldFont = getFont().deriveFont(Font.BOLD); 236 251 } 237 if ( entry.notify > 0 && !collapsed && tabs.getSelectedIndex() == tabs.indexOfComponent(entry.component))252 if (entry.notify > 0 && !collapsed && tabs.getSelectedIndex() == tabs.indexOfComponent(entry.component)) 238 253 entry.notify = 0; 239 254 setFont(entry.notify > 0 ? boldFont : normalFont); -
applications/editors/josm/plugins/geochat/src/geochat/ChatServerConnection.java
r30737 r32544 1 // License: WTFPL 1 // License: WTFPL. For details, see LICENSE file. 2 2 package geochat; 3 3 … … 27 27 /** 28 28 * This class holds all the chat data and periodically polls the server. 29 * 29 * 30 30 * @author zverik 31 31 */ 32 class ChatServerConnection {32 final class ChatServerConnection { 33 33 public static final String TOKEN_PREFIX = "="; 34 34 private static final String TOKEN_PATTERN = "^[a-zA-Z0-9]{10}$"; 35 35 36 36 private int userId; 37 37 private String userName; … … 47 47 new Thread(requestThread).start(); 48 48 } 49 49 50 50 public static ChatServerConnection getInstance() { 51 if ( instance == null)51 if (instance == null) 52 52 instance = new ChatServerConnection(); 53 53 return instance; 54 54 } 55 55 56 public void addListener( ChatServerConnectionListener listener) {56 public void addListener(ChatServerConnectionListener listener) { 57 57 listeners.add(listener); 58 58 } 59 59 60 public void removeListener( ChatServerConnectionListener listener) {60 public void removeListener(ChatServerConnectionListener listener) { 61 61 listeners.remove(listener); 62 62 } … … 85 85 * Does not autologin, if userName is null, obviously. 86 86 */ 87 public void autoLogin( final String userName) {87 public void autoLogin(final String userName) { 88 88 final int uid = Main.pref.getInteger("geochat.lastuid", 0); 89 if ( uid <= 0) {90 if ( userName != null && userName.length() > 1)89 if (uid <= 0) { 90 if (userName != null && userName.length() > 1) 91 91 login(userName); 92 92 } else { 93 93 String query = "whoami&uid=" + uid; 94 94 JsonQueryUtil.queryAsync(query, new JsonQueryCallback() { 95 public void processJson( JsonObject json ) { 96 if( json != null && json.get("name") != null ) 95 @Override 96 public void processJson(JsonObject json) { 97 if (json != null && json.get("name") != null) 97 98 login(uid, json.getString("name")); 98 else if ( userName != null && userName.length() > 1)99 else if (userName != null && userName.length() > 1) 99 100 login(userName); 100 101 } … … 107 108 * If two seconds have passed, stops the waiting. Doesn't wait if userName is empty. 108 109 */ 109 public void autoLoginWithDelay( final String userName) {110 if ( userName == null || userName.length() == 0) {110 public void autoLoginWithDelay(final String userName) { 111 if (userName == null || userName.length() == 0) { 111 112 checkLogin(); 112 113 return; 113 114 } 114 115 new Thread(new Runnable() { 116 @Override 115 117 public void run() { 116 118 try { 117 119 int cnt = 10; 118 while ( getPosition() == null && cnt-- > 0 )120 while (getPosition() == null && cnt-- > 0) { 119 121 Thread.sleep(200); 120 } catch( InterruptedException e ) {} 122 } 123 } catch (InterruptedException e) { 124 Main.warn(e); 125 } 121 126 autoLogin(userName); 122 127 } … … 124 129 } 125 130 126 public void login( final String userName) {127 if ( userName == null)131 public void login(final String userName) { 132 if (userName == null) 128 133 throw new IllegalArgumentException("userName is null"); 129 134 LatLon pos = getPosition(); 130 if ( pos == null) {135 if (pos == null) { 131 136 fireLoginFailed("Zoom level is too low"); 132 137 return; 133 138 } 134 139 String token = userName.startsWith(TOKEN_PREFIX) ? userName.substring(TOKEN_PREFIX.length()) : null; 135 if ( token != null && !token.matches(TOKEN_PATTERN)) {140 if (token != null && !token.matches(TOKEN_PATTERN)) { 136 141 fireLoginFailed("Incorrect token format"); 137 142 return; … … 141 146 String nameAttr = token != null ? "&token=" + token : "&name=" + URLEncoder.encode(userName, "UTF-8"); 142 147 String query = "register&lat=" + pos.latToString(CoordinateFormat.DECIMAL_DEGREES) 143 144 148 + "&lon=" + pos.lonToString(CoordinateFormat.DECIMAL_DEGREES) 149 + nameAttr; 145 150 JsonQueryUtil.queryAsync(query, new JsonQueryCallback() { 146 public void processJson( JsonObject json ) { 147 if( json == null ) 151 @Override 152 public void processJson(JsonObject json) { 153 if (json == null) 148 154 fireLoginFailed(tr("Could not get server response, check logs")); 149 else if ( json.get("error") != null)155 else if (json.get("error") != null) 150 156 fireLoginFailed(tr("Failed to login as {0}:", userName) + "\n" + json.getString("error")); 151 else if (json.get("uid") == null)157 else if (json.get("uid") == null) 152 158 fireLoginFailed(tr("The server did not return user ID")); 153 159 else { … … 157 163 } 158 164 }); 159 } catch ( UnsupportedEncodingException e) {160 // wut161 } 162 } 163 164 private void login( int userId, String userName) {165 } catch (UnsupportedEncodingException e) { 166 Main.error(e); 167 } 168 } 169 170 private void login(int userId, String userName) { 165 171 this.userId = userId; 166 172 this.userName = userName; 167 173 Main.pref.putInteger("geochat.lastuid", userId); 168 for ( ChatServerConnectionListener listener : listeners )174 for (ChatServerConnectionListener listener : listeners) { 169 175 listener.loggedIn(userName); 176 } 170 177 } 171 178 … … 174 181 ChatServerConnection.this.userName = null; 175 182 Main.pref.put("geochat.lastuid", null); 176 for ( ChatServerConnectionListener listener : listeners )183 for (ChatServerConnectionListener listener : listeners) { 177 184 listener.notLoggedIn(null); 178 } 179 180 private void fireLoginFailed( String reason ) { 181 for( ChatServerConnectionListener listener : listeners ) 185 } 186 } 187 188 private void fireLoginFailed(String reason) { 189 for (ChatServerConnectionListener listener : listeners) { 182 190 listener.notLoggedIn(reason); 191 } 183 192 } 184 193 … … 187 196 */ 188 197 public void logout() { 189 if ( !isLoggedIn())198 if (!isLoggedIn()) 190 199 return; 191 200 String query = "logout&uid=" + userId; 192 201 JsonQueryUtil.queryAsync(query, new JsonQueryCallback() { 193 public void processJson( JsonObject json ) { 194 if( json != null && json.get("message") != null) { 202 @Override 203 public void processJson(JsonObject json) { 204 if (json != null && json.get("message") != null) { 195 205 logoutIntl(); 196 206 } … … 204 214 */ 205 215 public void bruteLogout() throws IOException { 206 if ( isLoggedIn())216 if (isLoggedIn()) 207 217 JsonQueryUtil.query("logout&uid=" + userId); 208 218 } 209 219 210 private void fireMessageFailed( String reason) {211 for ( ChatServerConnectionListener listener : listeners )220 private void fireMessageFailed(String reason) { 221 for (ChatServerConnectionListener listener : listeners) { 212 222 listener.messageSendFailed(reason); 223 } 213 224 } 214 225 … … 218 229 * @see #postMessage(java.lang.String, java.lang.String) 219 230 */ 220 public void postMessage( String message) {231 public void postMessage(String message) { 221 232 postMessage(message, null); 222 233 } … … 228 239 * @param targetUser null if sending to everyone, name of user otherwise. 229 240 */ 230 public void postMessage( String message, String targetUser) {231 if ( !isLoggedIn()) {241 public void postMessage(String message, String targetUser) { 242 if (!isLoggedIn()) { 232 243 fireMessageFailed("Not logged in"); 233 244 return; 234 245 } 235 246 LatLon pos = getPosition(); 236 if ( pos == null) {247 if (pos == null) { 237 248 fireMessageFailed("Zoom level is too low"); 238 249 return; … … 240 251 try { 241 252 String query = "post&lat=" + pos.latToString(CoordinateFormat.DECIMAL_DEGREES) 242 243 244 245 if (targetUser != null && targetUser.length() > 0)246 253 + "&lon=" + pos.lonToString(CoordinateFormat.DECIMAL_DEGREES) 254 + "&uid=" + userId 255 + "&message=" + URLEncoder.encode(message, "UTF8"); 256 if (targetUser != null && targetUser.length() > 0) 257 query += "&to=" + URLEncoder.encode(targetUser, "UTF8"); 247 258 JsonQueryUtil.queryAsync(query, new JsonQueryCallback() { 248 public void processJson( JsonObject json ) { 249 if( json == null ) 259 @Override 260 public void processJson(JsonObject json) { 261 if (json == null) 250 262 fireMessageFailed(tr("Could not get server response, check logs")); 251 else if ( json.get("error") != null)263 else if (json.get("error") != null) 252 264 fireMessageFailed(json.getString("error")); 253 265 } 254 266 }); 255 } catch ( UnsupportedEncodingException e) {256 // wut257 } 258 } 259 267 } catch (UnsupportedEncodingException e) { 268 Main.error(e); 269 } 270 } 271 260 272 /** 261 273 * Returns current coordinates or null if there is no map, or zoom is too low. 262 274 */ 263 275 private static LatLon getPosition() { 264 if ( Main.map == null || Main.map.mapView == null)276 if (Main.map == null || Main.map.mapView == null) 265 277 return null; 266 if ( getCurrentZoom() < 10)278 if (getCurrentZoom() < 10) 267 279 return null; 268 280 Projection proj = Main.getProjection(); … … 271 283 272 284 // Following three methods were snatched from TMSLayer 273 private static double latToTileY( double lat, int zoom) {285 private static double latToTileY(double lat, int zoom) { 274 286 double l = lat / 180 * Math.PI; 275 287 double pf = Math.log(Math.tan(l) + (1 / Math.cos(l))); … … 277 289 } 278 290 279 private static double lonToTileX( double lon, int zoom) {291 private static double lonToTileX(double lon, int zoom) { 280 292 return Math.pow(2.0, zoom - 3) * (lon + 180.0) / 45.0; 281 293 } 282 294 283 295 public static int getCurrentZoom() { 284 if ( Main.map == null || Main.map.mapView == null) {296 if (Main.map == null || Main.map.mapView == null) { 285 297 return 1; 286 298 } … … 295 307 int screenPixels = mv.getWidth() * mv.getHeight(); 296 308 double tilePixels = Math.abs((y2 - y1) * (x2 - x1) * 256 * 256); 297 if ( screenPixels == 0 || tilePixels == 0) {309 if (screenPixels == 0 || tilePixels == 0) { 298 310 return 1; 299 311 } 300 312 double factor = screenPixels / tilePixels; 301 313 double result = Math.log(factor) / Math.log(2) / 2 + 1; 302 int intResult = (int) Math.floor(result);314 int intResult = (int) Math.floor(result); 303 315 return intResult; 304 316 } … … 312 324 private boolean stopping = false; 313 325 326 @Override 314 327 public void run() { 315 // lastId = Main.pref.getLong("geochat.lastid", 0);328 // lastId = Main.pref.getLong("geochat.lastid", 0); 316 329 int interval = Main.pref.getInteger("geochat.interval", 2); 317 while ( !stopping) {330 while (!stopping) { 318 331 process(); 319 332 try { 320 333 Thread.sleep(interval * 1000); 321 } catch ( InterruptedException e) {334 } catch (InterruptedException e) { 322 335 stopping = true; 323 336 } … … 330 343 331 344 public void process() { 332 if ( !isLoggedIn()) {345 if (!isLoggedIn()) { 333 346 fireStatusChanged(false); 334 347 return; … … 336 349 337 350 LatLon pos = getPosition(); 338 if ( pos == null) {351 if (pos == null) { 339 352 fireStatusChanged(false); 340 353 return; 341 354 } 342 355 fireStatusChanged(true); 343 356 344 357 final boolean needReset; 345 358 final boolean needFullReset = lastUserId != userId; 346 if ( needFullReset || (lastPosition != null && pos.greatCircleDistance(lastPosition) > MAX_JUMP)) {359 if (needFullReset || (lastPosition != null && pos.greatCircleDistance(lastPosition) > MAX_JUMP)) { 347 360 // reset messages 348 361 lastId = 0; 349 // Main.pref.put("geochat.lastid", null);362 // Main.pref.put("geochat.lastid", null); 350 363 needReset = true; 351 364 } else … … 353 366 lastUserId = userId; 354 367 lastPosition = pos; 355 368 356 369 String query = "get&lat=" + pos.latToString(CoordinateFormat.DECIMAL_DEGREES) 357 358 370 + "&lon=" + pos.lonToString(CoordinateFormat.DECIMAL_DEGREES) 371 + "&uid=" + userId + "&last=" + lastId; 359 372 JsonObject json; 360 373 try { 361 374 json = JsonQueryUtil.query(query); 362 } catch ( IOException ex) {375 } catch (IOException ex) { 363 376 json = null; // ? 364 377 } 365 if ( json == null) {378 if (json == null) { 366 379 // do nothing? 367 // fireLoginFailed(tr("Could not get server response, check logs"));368 // logoutIntl(); // todo: uncomment?369 } else if (json.get("error") != null) {380 // fireLoginFailed(tr("Could not get server response, check logs")); 381 // logoutIntl(); // todo: uncomment? 382 } else if (json.get("error") != null) { 370 383 fireLoginFailed(tr("Failed to get messages as {0}:", userName) + "\n" + json.getString("error")); 371 384 logoutIntl(); 372 385 } else { 373 if (json.get("users") != null) {386 if (json.get("users") != null) { 374 387 Map<String, LatLon> users = parseUsers(json.getJsonArray("users")); 375 for ( ChatServerConnectionListener listener : listeners )388 for (ChatServerConnectionListener listener : listeners) { 376 389 listener.updateUsers(users); 377 } 378 if( json.get("messages") != null) { 390 } 391 } 392 if (json.get("messages") != null) { 379 393 List<ChatMessage> messages = parseMessages(json.getJsonArray("messages"), false); 380 for ( ChatMessage m : messages )381 if ( m.getId() > lastId)394 for (ChatMessage m : messages) { 395 if (m.getId() > lastId) 382 396 lastId = m.getId(); 383 for( ChatServerConnectionListener listener : listeners ) 397 } 398 for (ChatServerConnectionListener listener : listeners) { 384 399 listener.receivedMessages(needReset, messages); 385 } 386 if( json.get("private") != null) { 400 } 401 } 402 if (json.get("private") != null) { 387 403 List<ChatMessage> messages = parseMessages(json.getJsonArray("private"), true); 388 for ( ChatMessage m : messages )389 if ( m.getId() > lastId)404 for (ChatMessage m : messages) { 405 if (m.getId() > lastId) 390 406 lastId = m.getId(); 391 for( ChatServerConnectionListener listener : listeners ) 407 } 408 for (ChatServerConnectionListener listener : listeners) { 392 409 listener.receivedPrivateMessages(needFullReset, messages); 393 } 394 } 395 // if( lastId > 0 && Main.pref.getBoolean("geochat.store.lastid", true) ) 396 // Main.pref.putLong("geochat.lastid", lastId); 397 } 398 399 private List<ChatMessage> parseMessages( JsonArray messages, boolean priv ) { 410 } 411 } 412 } 413 // if (lastId > 0 && Main.pref.getBoolean("geochat.store.lastid", true) ) 414 // Main.pref.putLong("geochat.lastid", lastId); 415 } 416 417 private List<ChatMessage> parseMessages(JsonArray messages, boolean priv) { 400 418 List<ChatMessage> result = new ArrayList<>(); 401 for ( int i = 0; i < messages.size(); i++) {419 for (int i = 0; i < messages.size(); i++) { 402 420 try { 403 421 JsonObject msg = messages.getJsonObject(i); 404 422 long id = Long.parseLong(msg.getString("id")); 405 423 double lat = Double.parseDouble(msg.getString("lat")); … … 412 430 incoming, message, new Date(timeStamp * 1000)); 413 431 cm.setPrivate(priv); 414 if ( msg.get("recipient") != null && !incoming)432 if (msg.get("recipient") != null && !incoming) 415 433 cm.setRecipient(msg.getString("recipient")); 416 434 result.add(cm); 417 } catch ( JsonException e) {418 // do nothing, just skip this message435 } catch (JsonException e) { 436 Main.trace(e); 419 437 } 420 438 } … … 422 440 } 423 441 424 private Map<String, LatLon> parseUsers( JsonArray users) {442 private Map<String, LatLon> parseUsers(JsonArray users) { 425 443 Map<String, LatLon> result = new HashMap<>(); 426 for ( int i = 0; i < users.size(); i++) {444 for (int i = 0; i < users.size(); i++) { 427 445 try { 428 446 JsonObject user = users.getJsonObject(i); 429 447 String name = user.getString("user"); 430 448 double lat = Double.parseDouble(user.getString("lat")); 431 449 double lon = Double.parseDouble(user.getString("lon")); 432 450 result.put(name, new LatLon(lat, lon)); 433 } catch ( JsonException e) {434 // do nothing, just skip this user451 } catch (JsonException e) { 452 Main.trace(e); 435 453 } 436 454 } … … 438 456 } 439 457 440 private void fireStatusChanged( boolean newStatus) {441 if ( newStatus == lastStatus)458 private void fireStatusChanged(boolean newStatus) { 459 if (newStatus == lastStatus) 442 460 return; 443 461 lastStatus = newStatus; 444 for ( ChatServerConnectionListener listener : listeners )462 for (ChatServerConnectionListener listener : listeners) { 445 463 listener.statusChanged(newStatus); 464 } 446 465 } 447 466 } -
applications/editors/josm/plugins/geochat/src/geochat/ChatServerConnectionListener.java
r29584 r32544 1 // License: WTFPL 1 // License: WTFPL. For details, see LICENSE file. 2 2 package geochat; 3 3 4 import java.util.*; 4 import java.util.List; 5 import java.util.Map; 6 5 7 import org.openstreetmap.josm.data.coor.LatLon; 6 8 … … 15 17 * @param userName Name of the logged in user. 16 18 */ 17 void loggedIn( String userName);18 19 void loggedIn(String userName); 20 19 21 /** 20 22 * User tried to log in, but failed. 21 23 * @param reason Why. <tt>null</tt> if it is intended logout. 22 24 */ 23 void notLoggedIn( String reason);25 void notLoggedIn(String reason); 24 26 25 27 /** … … 27 29 * @param reason Why. 28 30 */ 29 void messageSendFailed( String reason);31 void messageSendFailed(String reason); 30 32 31 33 /** … … 33 35 * @param active Is the chat active. 34 36 */ 35 void statusChanged( boolean active);37 void statusChanged(boolean active); 36 38 37 39 /** … … 39 41 * @param users a hash of user names and coordinates. 40 42 */ 41 void updateUsers( Map<String, LatLon> users);43 void updateUsers(Map<String, LatLon> users); 42 44 43 45 /** … … 46 48 * @param messages new messages array. 47 49 */ 48 void receivedMessages( boolean replace, List<ChatMessage> messages);50 void receivedMessages(boolean replace, List<ChatMessage> messages); 49 51 50 52 /** … … 55 57 * @param messages list of new private messages. 56 58 */ 57 void receivedPrivateMessages( boolean replace, List<ChatMessage> messages);59 void receivedPrivateMessages(boolean replace, List<ChatMessage> messages); 58 60 } -
applications/editors/josm/plugins/geochat/src/geochat/GeoChatPanel.java
r30737 r32544 1 // License: WTFPL 1 // License: WTFPL. For details, see LICENSE file. 2 2 package geochat; 3 3 4 import java.awt.*; 5 import java.awt.event.*; 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 import static org.openstreetmap.josm.tools.I18n.trn; 6 7 import java.awt.AlphaComposite; 8 import java.awt.BorderLayout; 9 import java.awt.Color; 10 import java.awt.Composite; 11 import java.awt.Dimension; 12 import java.awt.Font; 13 import java.awt.FontMetrics; 14 import java.awt.Graphics2D; 15 import java.awt.GridBagConstraints; 16 import java.awt.GridBagLayout; 17 import java.awt.Point; 18 import java.awt.RenderingHints; 19 import java.awt.event.ActionEvent; 20 import java.awt.event.ActionListener; 6 21 import java.io.IOException; 7 22 import java.text.SimpleDateFormat; 8 import java.util.*;9 23 import java.util.List; 10 import javax.swing.*; 24 import java.util.Map; 25 import java.util.TreeMap; 26 27 import javax.swing.JButton; 28 import javax.swing.JCheckBox; 29 import javax.swing.JComponent; 30 import javax.swing.JLabel; 31 import javax.swing.JPanel; 32 import javax.swing.JTabbedPane; 33 import javax.swing.JTextField; 34 import javax.swing.SwingConstants; 35 11 36 import org.openstreetmap.josm.Main; 12 37 import org.openstreetmap.josm.data.Bounds; 13 38 import org.openstreetmap.josm.data.coor.LatLon; 14 import org.openstreetmap.josm.gui.*; 39 import org.openstreetmap.josm.gui.JosmUserIdentityManager; 40 import org.openstreetmap.josm.gui.MapView; 41 import org.openstreetmap.josm.gui.Notification; 15 42 import org.openstreetmap.josm.gui.dialogs.ToggleDialog; 16 43 import org.openstreetmap.josm.gui.layer.MapViewPaintable; 17 44 import org.openstreetmap.josm.gui.util.GuiHelper; 18 45 import org.openstreetmap.josm.tools.GBC; 19 import static org.openstreetmap.josm.tools.I18n.tr;20 import static org.openstreetmap.josm.tools.I18n.trn;21 46 22 47 /** … … 36 61 ChatPaneManager chatPanes; 37 62 boolean userLayerActive; 38 63 39 64 public GeoChatPanel() { 40 65 super(tr("GeoChat"), "geochat", tr("Open GeoChat panel"), null, 200, true); … … 48 73 input = new JPanelTextField() { 49 74 @Override 50 protected void processEnter( String text) {75 protected void processEnter(String text) { 51 76 connection.postMessage(text, chatPanes.getRecipient()); 52 77 } 53 78 54 79 @Override 55 protected String autoComplete( String word, boolean atStart) {80 protected String autoComplete(String word, boolean atStart) { 56 81 return autoCompleteUser(word, atStart); 57 82 } 58 83 }; 59 84 60 85 String defaultUserName = constructUserName(); 61 86 loginPanel = createLoginPanel(defaultUserName); … … 76 101 private String constructUserName() { 77 102 String userName = Main.pref.get("geochat.username", null); // so the default is null 78 if ( userName == null)103 if (userName == null) 79 104 userName = JosmUserIdentityManager.getInstance().getUserName(); 80 if ( userName == null)105 if (userName == null) 81 106 userName = ""; 82 if ( userName.contains("@"))107 if (userName.contains("@")) 83 108 userName = userName.substring(0, userName.indexOf('@')); 84 109 userName = userName.replace(' ', '_'); … … 86 111 } 87 112 88 private JPanel createLoginPanel( String defaultUserName) {113 private JPanel createLoginPanel(String defaultUserName) { 89 114 final JTextField nameField = new JPanelTextField() { 90 115 @Override 91 protected void processEnter( String text) {116 protected void processEnter(String text) { 92 117 connection.login(text); 93 118 } … … 98 123 loginButton.addActionListener(new ActionListener() { 99 124 @Override 100 public void actionPerformed( ActionEvent e) {125 public void actionPerformed(ActionEvent e) { 101 126 connection.login(nameField.getText()); 102 127 } … … 107 132 autoLoginBox.addActionListener(new ActionListener() { 108 133 @Override 109 public void actionPerformed( ActionEvent e) {134 public void actionPerformed(ActionEvent e) { 110 135 Main.pref.put("geochat.autologin", autoLoginBox.isSelected()); 111 136 } … … 126 151 public void destroy() { 127 152 try { 128 if ( Main.pref.getBoolean("geochat.logout.on.close", true)) {153 if (Main.pref.getBoolean("geochat.logout.on.close", true)) { 129 154 connection.removeListener(this); 130 155 connection.bruteLogout(); 131 156 } 132 } catch ( IOException e) {157 } catch (IOException e) { 133 158 Main.warn("Failed to logout from geochat server: " + e.getMessage()); 134 159 } … … 136 161 } 137 162 138 private String autoCompleteUser( String word, boolean atStart) {163 private String autoCompleteUser(String word, boolean atStart) { 139 164 String result = null; 140 165 boolean singleUser = true; 141 for ( String user : users.keySet()) {142 if ( user.startsWith(word)) {143 if ( result == null)166 for (String user : users.keySet()) { 167 if (user.startsWith(word)) { 168 if (result == null) 144 169 result = user; 145 170 else { 146 171 singleUser = false; 147 172 int i = word.length(); 148 while ( i < result.length() && i < user.length() && result.charAt(i) == user.charAt(i) )173 while (i < result.length() && i < user.length() && result.charAt(i) == user.charAt(i)) { 149 174 i++; 150 if( i < result.length() ) 175 } 176 if (i < result.length()) 151 177 result = result.substring(0, i); 152 178 } … … 161 187 */ 162 188 @Override 163 public void paint( Graphics2D g, MapView mv, Bounds bbox) {164 Graphics2D g2d = (Graphics2D) g.create();189 public void paint(Graphics2D g, MapView mv, Bounds bbox) { 190 Graphics2D g2d = (Graphics2D) g.create(); 165 191 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 166 192 Composite ac04 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f); … … 171 197 FontMetrics fm = g2d.getFontMetrics(); 172 198 173 for ( String user : users.keySet()) {199 for (String user : users.keySet()) { 174 200 int stringWidth = fm.stringWidth(user); 175 201 int radius = stringWidth / 2 + 10; … … 193 219 protected void updateTitleAlarm() { 194 220 int alarmLevel = connection.isLoggedIn() ? chatPanes.getNotifyLevel() : 0; 195 if ( !isDialogInCollapsedView() && alarmLevel > 1)221 if (!isDialogInCollapsedView() && alarmLevel > 1) 196 222 alarmLevel = 1; 197 223 198 224 String comment; 199 if ( connection.isLoggedIn()) {225 if (connection.isLoggedIn()) { 200 226 comment = trn("{0} user", "{0} users", users.size() + 1, users.size() + 1); 201 227 } else { … … 204 230 205 231 String title = tr("GeoChat"); 206 if ( comment != null)232 if (comment != null) 207 233 title = title + " (" + comment + ")"; 208 234 final String alarm = (alarmLevel <= 0 ? "" : alarmLevel == 1 ? "* " : "!!! ") + title; … … 219 245 */ 220 246 @Override 221 protected void setIsCollapsed( boolean val) {247 protected void setIsCollapsed(boolean val) { 222 248 super.setIsCollapsed(val); 223 249 chatPanes.setCollapsed(val); … … 228 254 229 255 @Override 230 public void loggedIn( String userName) {256 public void loggedIn(String userName) { 231 257 Main.pref.put("geochat.username", userName); 232 if ( gcPanel.getComponentCount() == 1) {258 if (gcPanel.getComponentCount() == 1) { 233 259 GuiHelper.runInEDTAndWait(new Runnable() { 234 260 @Override … … 244 270 245 271 @Override 246 public void notLoggedIn( final String reason) {247 if ( reason != null) {272 public void notLoggedIn(final String reason) { 273 if (reason != null) { 248 274 GuiHelper.runInEDT(new Runnable() { 249 275 @Override … … 254 280 } else { 255 281 // regular logout 256 if ( gcPanel.getComponentCount() > 1) {282 if (gcPanel.getComponentCount() > 1) { 257 283 gcPanel.removeAll(); 258 284 gcPanel.add(loginPanel, BorderLayout.CENTER); … … 263 289 264 290 @Override 265 public void messageSendFailed( final String reason) {291 public void messageSendFailed(final String reason) { 266 292 GuiHelper.runInEDT(new Runnable() { 267 293 @Override … … 273 299 274 300 @Override 275 public void statusChanged( boolean active) {301 public void statusChanged(boolean active) { 276 302 // only the public tab, because private chats don't rely on coordinates 277 303 tabs.setComponentAt(0, active ? chatPanes.getPublicChatComponent() : noData); … … 280 306 281 307 @Override 282 public void updateUsers( Map<String, LatLon> newUsers) {283 for ( String uname : this.users.keySet()) {284 if ( !newUsers.containsKey(uname))308 public void updateUsers(Map<String, LatLon> newUsers) { 309 for (String uname : this.users.keySet()) { 310 if (!newUsers.containsKey(uname)) 285 311 chatPanes.addLineToPublic(tr("User {0} has left", uname), ChatPaneManager.MESSAGE_TYPE_INFORMATION); 286 312 } 287 for ( String uname : newUsers.keySet()) {288 if ( !this.users.containsKey(uname))313 for (String uname : newUsers.keySet()) { 314 if (!this.users.containsKey(uname)) 289 315 chatPanes.addLineToPublic(tr("User {0} is mapping nearby", uname), ChatPaneManager.MESSAGE_TYPE_INFORMATION); 290 316 } 291 317 this.users = newUsers; 292 318 updateTitleAlarm(); 293 if ( userLayerActive && Main.map.mapView != null)319 if (userLayerActive && Main.map.mapView != null) 294 320 Main.map.mapView.repaint(); 295 321 } … … 297 323 private final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm"); 298 324 299 private void formatMessage( StringBuilder sb, ChatMessage msg) {325 private void formatMessage(StringBuilder sb, ChatMessage msg) { 300 326 sb.append("\n"); 301 327 sb.append('[').append(TIME_FORMAT.format(msg.getTime())).append("] "); … … 304 330 305 331 @Override 306 public void receivedMessages( boolean replace, List<ChatMessage> messages) {307 if ( replace)332 public void receivedMessages(boolean replace, List<ChatMessage> messages) { 333 if (replace) 308 334 chatPanes.clearPublicChatPane(); 309 if ( !messages.isEmpty()) {335 if (!messages.isEmpty()) { 310 336 int alarm = 0; 311 337 StringBuilder sb = new StringBuilder(); 312 for ( ChatMessage msg : messages) {338 for (ChatMessage msg : messages) { 313 339 boolean important = msg.isIncoming() && containsName(msg.getMessage()); 314 if ( msg.isIncoming() && alarm < 2) {340 if (msg.isIncoming() && alarm < 2) { 315 341 alarm = important ? 2 : 1; 316 342 } 317 if ( important) {343 if (important) { 318 344 // add buffer, then add current line with italic, then clear buffer 319 345 chatPanes.addLineToPublic(sb.toString()); … … 326 352 } 327 353 chatPanes.addLineToPublic(sb.toString()); 328 if ( alarm > 0)354 if (alarm > 0) 329 355 chatPanes.notify(null, alarm); 330 356 } 331 if ( replace)357 if (replace) 332 358 showNearbyUsers(); 333 359 } 334 360 335 361 private void showNearbyUsers() { 336 if ( !users.isEmpty()) {362 if (!users.isEmpty()) { 337 363 StringBuilder sb = new StringBuilder(tr("Users mapping nearby:")); 338 364 boolean first = true; 339 for ( String user : users.keySet()) {365 for (String user : users.keySet()) { 340 366 sb.append(first ? " " : ", "); 341 367 sb.append(user); … … 345 371 } 346 372 347 private boolean containsName( String message) {373 private boolean containsName(String message) { 348 374 String userName = connection.getUserName(); 349 375 int length = userName.length(); 350 376 int i = message.indexOf(userName); 351 while ( i >= 0) {352 if ((i == 0 || !Character.isJavaIdentifierPart(message.charAt(i - 1)))353 && (i + length >= message.length() || !Character.isJavaIdentifierPart(message.charAt(i + length))) 377 while (i >= 0) { 378 if ((i == 0 || !Character.isJavaIdentifierPart(message.charAt(i - 1))) 379 && (i + length >= message.length() || !Character.isJavaIdentifierPart(message.charAt(i + length)))) 354 380 return true; 355 381 i = message.indexOf(userName, i + 1); … … 359 385 360 386 @Override 361 public void receivedPrivateMessages( boolean replace, List<ChatMessage> messages) {362 if ( replace)387 public void receivedPrivateMessages(boolean replace, List<ChatMessage> messages) { 388 if (replace) 363 389 chatPanes.closePrivateChatPanes(); 364 for ( ChatMessage msg : messages) {390 for (ChatMessage msg : messages) { 365 391 StringBuilder sb = new StringBuilder(); 366 392 formatMessage(sb, msg); 367 393 chatPanes.addLineToChatPane(msg.isIncoming() ? msg.getAuthor() : msg.getRecipient(), sb.toString()); 368 if ( msg.isIncoming())394 if (msg.isIncoming()) 369 395 chatPanes.notify(msg.getAuthor(), 2); 370 396 } -
applications/editors/josm/plugins/geochat/src/geochat/GeoChatPlugin.java
r29584 r32544 1 // License: WTFPL 1 // License: WTFPL. For details, see LICENSE file. 2 2 package geochat; 3 3 … … 8 8 /** 9 9 * Create chat panel. 10 * 10 * 11 11 * @author zverik 12 12 */ 13 13 public class GeoChatPlugin extends Plugin { 14 public GeoChatPlugin( PluginInformation info) {14 public GeoChatPlugin(PluginInformation info) { 15 15 super(info); 16 16 } 17 17 18 18 @Override 19 public void mapFrameInitialized( MapFrame oldFrame, MapFrame newFrame) {20 if ( oldFrame == null && newFrame != null) {19 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 20 if (oldFrame == null && newFrame != null) { 21 21 newFrame.addToggleDialog(new GeoChatPanel()); 22 22 } -
applications/editors/josm/plugins/geochat/src/geochat/GeoChatPopupAdapter.java
r29584 r32544 1 // License: WTFPL 1 // License: WTFPL. For details, see LICENSE file. 2 2 package geochat; 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 3 5 4 6 import java.awt.event.ActionEvent; 5 7 import java.awt.event.MouseAdapter; 6 8 import java.awt.event.MouseEvent; 7 import javax.swing.*; 9 10 import javax.swing.AbstractAction; 11 import javax.swing.JCheckBoxMenuItem; 12 import javax.swing.JMenu; 13 import javax.swing.JPopupMenu; 14 8 15 import org.openstreetmap.josm.Main; 9 import static org.openstreetmap.josm.tools.I18n.tr;10 16 11 17 /** … … 16 22 private GeoChatPanel panel; 17 23 18 public GeoChatPopupAdapter( GeoChatPanel panel) {24 GeoChatPopupAdapter(GeoChatPanel panel) { 19 25 this.panel = panel; 20 26 } 21 27 22 28 @Override 23 public void mousePressed( MouseEvent e) {29 public void mousePressed(MouseEvent e) { 24 30 check(e); 25 31 } 26 32 27 33 @Override 28 public void mouseReleased( MouseEvent e) {34 public void mouseReleased(MouseEvent e) { 29 35 check(e); 30 36 } 31 37 32 private void check( MouseEvent e) {33 if ( e.isPopupTrigger()) {38 private void check(MouseEvent e) { 39 if (e.isPopupTrigger()) { 34 40 createPopupMenu().show(e.getComponent(), e.getX(), e.getY()); 35 41 } … … 38 44 private JPopupMenu createPopupMenu() { 39 45 JMenu userMenu = new JMenu(tr("Private chat")); 40 for ( String user : panel.users.keySet()) {41 if ( !panel.chatPanes.hasUser(user))46 for (String user : panel.users.keySet()) { 47 if (!panel.chatPanes.hasUser(user)) 42 48 userMenu.add(new PrivateChatAction(user)); 43 49 } 44 50 45 51 JPopupMenu menu = new JPopupMenu(); 46 if ( panel.chatPanes.hasSelectedText())52 if (panel.chatPanes.hasSelectedText()) 47 53 menu.add(new CopyTextAction()); 48 54 menu.add(new JCheckBoxMenuItem(new ToggleUserLayerAction())); 49 if ( userMenu.getItemCount() > 0)55 if (userMenu.getItemCount() > 0) 50 56 menu.add(userMenu); 51 if ( panel.chatPanes.getRecipient() != null)57 if (panel.chatPanes.getRecipient() != null) 52 58 menu.add(new CloseTabAction()); 53 // menu.add(new ClearPaneAction());54 59 menu.add(new LogoutAction()); 55 60 return menu; … … 59 64 private String userName; 60 65 61 public PrivateChatAction( String userName) {66 PrivateChatAction(String userName) { 62 67 super(userName); 63 68 this.userName = userName; 64 69 } 65 70 66 public void actionPerformed( ActionEvent e ) { 67 if( !panel.chatPanes.hasUser(userName) ) { 71 @Override 72 public void actionPerformed(ActionEvent e) { 73 if (!panel.chatPanes.hasUser(userName)) { 68 74 panel.chatPanes.createChatPane(userName); 69 75 } … … 72 78 73 79 private class CloseTabAction extends AbstractAction { 74 publicCloseTabAction() {80 CloseTabAction() { 75 81 super(tr("Close tab")); 76 // putValue(SMALL_ICON, ImageProvider.get("help"));77 82 } 78 83 79 public void actionPerformed( ActionEvent e ) { 84 @Override 85 public void actionPerformed(ActionEvent e) { 80 86 panel.chatPanes.closeSelectedPrivatePane(); 81 87 } … … 83 89 84 90 private class LogoutAction extends AbstractAction { 85 publicLogoutAction() {91 LogoutAction() { 86 92 super(tr("Logout")); 87 // putValue(SMALL_ICON, ImageProvider.get("help"));93 // putValue(SMALL_ICON, ImageProvider.get("help")); 88 94 } 89 95 90 public void actionPerformed( ActionEvent e ) { 96 @Override 97 public void actionPerformed(ActionEvent e) { 91 98 panel.logout(); 92 99 } … … 94 101 95 102 private class ClearPaneAction extends AbstractAction { 96 publicClearPaneAction() {103 ClearPaneAction() { 97 104 super(tr("Clear log")); 98 // putValue(SMALL_ICON, ImageProvider.get("help"));99 105 } 100 106 101 public void actionPerformed( ActionEvent e ) { 107 @Override 108 public void actionPerformed(ActionEvent e) { 102 109 panel.chatPanes.clearActiveChatPane(); 103 110 } … … 105 112 106 113 private class ToggleUserLayerAction extends AbstractAction { 107 publicToggleUserLayerAction() {114 ToggleUserLayerAction() { 108 115 super(tr("Show users on map")); 109 // putValue(SMALL_ICON, ImageProvider.get("help"));110 116 putValue(SELECTED_KEY, Boolean.valueOf(panel.userLayerActive)); 111 117 } 112 118 113 public void actionPerformed( ActionEvent e ) { 114 if( Main.map == null || Main.map.mapView == null ) 119 @Override 120 public void actionPerformed(ActionEvent e) { 121 if (Main.map == null || Main.map.mapView == null) 115 122 return; 116 123 boolean wasAdded = Main.map.mapView.addTemporaryLayer(panel); 117 if ( !wasAdded)124 if (!wasAdded) 118 125 Main.map.mapView.removeTemporaryLayer(panel); 119 126 panel.userLayerActive = wasAdded; … … 124 131 125 132 private class CopyTextAction extends AbstractAction { 126 publicCopyTextAction() {133 CopyTextAction() { 127 134 super(tr("Copy")); 128 // putValue(SMALL_ICON, ImageProvider.get("help"));129 135 } 130 136 131 public void actionPerformed( ActionEvent e ) { 137 @Override 138 public void actionPerformed(ActionEvent e) { 132 139 panel.chatPanes.copySelectedText(); 133 140 } -
applications/editors/josm/plugins/geochat/src/geochat/JPanelTextField.java
r29588 r32544 1 // License: WTFPL 1 // License: WTFPL. For details, see LICENSE file. 2 2 package geochat; 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 3 5 4 6 import java.awt.KeyboardFocusManager; … … 6 8 import java.awt.event.KeyEvent; 7 9 import java.util.HashSet; 8 import javax.swing.*; 10 11 import javax.swing.JComponent; 12 import javax.swing.JMenuItem; 13 import javax.swing.JPopupMenu; 14 import javax.swing.JTextField; 15 import javax.swing.KeyStroke; 9 16 import javax.swing.text.DefaultEditorKit; 17 10 18 import org.openstreetmap.josm.Main; 11 19 import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher; 12 import static org.openstreetmap.josm.tools.I18n.tr;13 20 14 21 /** … … 19 26 */ 20 27 public class JPanelTextField extends JTextField { 21 28 22 29 public JPanelTextField() { 23 30 setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, new HashSet<KeyStroke>()); … … 36 43 } 37 44 38 private JMenuItem createMenuItem( String action, String label) {45 private JMenuItem createMenuItem(String action, String label) { 39 46 JMenuItem item = new JMenuItem(getActionMap().get(action)); 40 47 item.setText(label); … … 44 51 // list of "standard" OS keys for JTextFiels = cursor moving, selection, copy/paste 45 52 private final KeyStroke[] standardKeys; 46 private static final int MODIFIERS_MASK = InputEvent.META_DOWN_MASK | InputEvent.CTRL_DOWN_MASK | InputEvent.ALT_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK; 53 private static final int MODIFIERS_MASK = 54 InputEvent.META_DOWN_MASK | InputEvent.CTRL_DOWN_MASK | InputEvent.ALT_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK; 47 55 48 56 @Override 49 protected void processKeyEvent( KeyEvent e) {50 if ( e.getID() == KeyEvent.KEY_PRESSED) {57 protected void processKeyEvent(KeyEvent e) { 58 if (e.getID() == KeyEvent.KEY_PRESSED) { 51 59 int code = e.getKeyCode(); 52 if ( code == KeyEvent.VK_ENTER) {60 if (code == KeyEvent.VK_ENTER) { 53 61 String text = getText(); 54 if ( text.length() > 0) {62 if (text.length() > 0) { 55 63 processEnter(text); 56 64 setText(""); 57 65 } 58 } else if ( code == KeyEvent.VK_TAB) {66 } else if (code == KeyEvent.VK_TAB) { 59 67 String text = getText(); 60 68 int caret = getCaretPosition(); 61 69 int start = caret - 1; 62 while ( start >= 0 && Character.isJavaIdentifierPart(text.charAt(start)) )70 while (start >= 0 && Character.isJavaIdentifierPart(text.charAt(start))) { 63 71 start--; 72 } 64 73 start++; 65 if ( start < caret) {74 if (start < caret) { 66 75 String word = text.substring(start, caret); 67 76 String complete = word == null ? null : autoComplete(word, start == 0); 68 if ( complete != null && !complete.equals(word)) {77 if (complete != null && !complete.equals(word)) { 69 78 StringBuilder sb = new StringBuilder(); 70 if ( start > 0)79 if (start > 0) 71 80 sb.append(text.substring(0, start)); 72 81 sb.append(complete); 73 if ( caret < text.length())82 if (caret < text.length()) 74 83 sb.append(text.substring(caret)); 75 84 setText(sb.toString()); … … 77 86 } 78 87 } 79 } else if ( code == KeyEvent.VK_ESCAPE) {80 if ( Main.map != null && Main.map.mapView != null)88 } else if (code == KeyEvent.VK_ESCAPE) { 89 if (Main.map != null && Main.map.mapView != null) 81 90 Main.map.mapView.requestFocus(); 82 } 91 } 83 92 84 93 boolean keyIsStandard = false; 85 94 for (KeyStroke ks: standardKeys) { 86 if (code == ks.getKeyCode() && 87 (e.getModifiersEx() & MODIFIERS_MASK) == (ks.getModifiers() & MODIFIERS_MASK)) {95 if (code == ks.getKeyCode() && 96 (e.getModifiersEx() & MODIFIERS_MASK) == (ks.getModifiers() & MODIFIERS_MASK)) { 88 97 keyIsStandard = true; 89 98 break; … … 91 100 } 92 101 // Do not pass other events to JOSM 93 if ( !keyIsStandard) {102 if (!keyIsStandard) { 94 103 e.consume(); 95 104 } … … 100 109 /** 101 110 * Process VK_ENTER. Override this to submit the text. 102 * 111 * 103 112 * @param text Contents of the text field. 104 113 */ 105 protected void processEnter( String text) { }114 protected void processEnter(String text) { } 106 115 107 116 /** … … 110 119 * @return The whole word. 111 120 */ 112 protected String autoComplete( String word, boolean atStart) {121 protected String autoComplete(String word, boolean atStart) { 113 122 return word; 114 123 } -
applications/editors/josm/plugins/geochat/src/geochat/JsonQueryCallback.java
r30234 r32544 1 // License: WTFPL 1 // License: WTFPL. For details, see LICENSE file. 2 2 package geochat; 3 3 … … 17 17 * @param json JSON parsed response or null if the query was unsuccessful. 18 18 */ 19 void processJson( JsonObject json);19 void processJson(JsonObject json); 20 20 } -
applications/editors/josm/plugins/geochat/src/geochat/JsonQueryUtil.java
r30234 r32544 1 // License: WTFPL 1 // License: WTFPL. For details, see LICENSE file. 2 2 package geochat; 3 3 … … 20 20 * @author zverik 21 21 */ 22 public class JsonQueryUtil implements Runnable {22 public final class JsonQueryUtil implements Runnable { 23 23 24 24 /** … … 28 28 * @throws IOException There was a problem connecting to the server or parsing JSON. 29 29 */ 30 public static JsonObject query( String query) throws IOException {30 public static JsonObject query(String query) throws IOException { 31 31 try { 32 32 String serverURL = Main.pref.get("geochat.server", "http://zverik.dev.openstreetmap.org/osmochat.php?action="); 33 33 URL url = new URL(serverURL + query); 34 // System.out.println("GeoChat URL = " + url.toString()); 35 HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 34 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 36 35 connection.connect(); 37 if ( connection.getResponseCode() != 200) {36 if (connection.getResponseCode() != 200) { 38 37 throw new IOException("HTTP Response code " + connection.getResponseCode() + " (" + connection.getResponseMessage() + ")"); 39 38 } 40 39 InputStream inp = connection.getInputStream(); 41 if ( inp == null)40 if (inp == null) 42 41 throw new IOException("Empty response"); 43 42 try { 44 43 return Json.createReader(inp).readObject(); 45 } catch ( JsonException e) {44 } catch (JsonException e) { 46 45 throw new IOException("Failed to parse JSON: " + e.getMessage()); 47 46 } finally { 48 47 connection.disconnect(); 49 48 } 50 } catch ( MalformedURLException ex) {49 } catch (MalformedURLException ex) { 51 50 throw new IOException("Malformed URL: " + ex.getMessage()); 52 51 } … … 60 59 private JsonQueryUtil() {} 61 60 62 private JsonQueryUtil( String query, JsonQueryCallback callback) {61 private JsonQueryUtil(String query, JsonQueryCallback callback) { 63 62 this.query = query; 64 63 this.callback = callback; … … 70 69 * @param callback Callback listener to process the JSON response. 71 70 */ 72 public static void queryAsync( String query, JsonQueryCallback callback) {71 public static void queryAsync(String query, JsonQueryCallback callback) { 73 72 Main.worker.submit(new JsonQueryUtil(query, callback)); 74 73 } 75 74 76 75 private void doRealRun() { 77 76 JsonObject obj; 78 77 try { 79 78 obj = query(query); 80 } catch ( IOException e) {79 } catch (IOException e) { 81 80 Main.warn(e.getClass().getName() + " while connecting to a chat server: " + e.getMessage()); 82 81 obj = null; 83 82 } 84 if ( callback != null)83 if (callback != null) 85 84 callback.processJson(obj); 86 85 } 87 86 87 @Override 88 88 public void run() { 89 if ( EventQueue.isDispatchThread()) {89 if (EventQueue.isDispatchThread()) { 90 90 new Thread(new Runnable() { 91 @Override 91 92 public void run() { 92 93 doRealRun();
Note:
See TracChangeset
for help on using the changeset viewer.