Changeset 32544 in osm for applications/editors/josm/plugins/geochat/src/geochat/GeoChatPanel.java
- Timestamp:
- 2016-07-03T21:26:31+02:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note:
See TracChangeset
for help on using the changeset viewer.