Changeset 29544 in osm for applications/editors/josm/plugins/geochat
- Timestamp:
- 2013-04-30T06:57:29+02:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/geochat/src/geochat/GeoChatPanel.java
r29541 r29544 17 17 import org.openstreetmap.josm.gui.dialogs.ToggleDialog; 18 18 import static org.openstreetmap.josm.tools.I18n.tr; 19 import static org.openstreetmap.josm.tools.I18n.trn; 19 20 20 21 /** … … 31 32 private JPanel gcPanel; 32 33 private ChatServerConnection connection; 34 private Map<String, LatLon> users; 33 35 34 36 public GeoChatPanel() { … … 43 45 44 46 tabs = new JTabbedPane(); 45 tabs.addTab(tr("Public"), chatPane);46 47 input = new J TextField() {47 tabs.addTab(tr("Public"), new JScrollPane(chatPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER)); 48 49 input = new JPanelTextField() { 48 50 @Override 49 protected void processKeyEvent( KeyEvent e ) { 50 if( e.getID() == KeyEvent.KEY_PRESSED ) { 51 int code = e.getKeyCode(); 52 if( code == KeyEvent.VK_ENTER ) { 53 String text = input.getText(); 54 if( text.length() > 0 ) { 55 connection.postMessage(text); 56 input.setText(""); 57 } 58 } else if( code == KeyEvent.VK_TAB ) { 59 // todo: autocomplete name 60 } else if( code == KeyEvent.VK_ESCAPE ) { 61 if( Main.map != null && Main.map.mapView != null ) 62 Main.map.mapView.requestFocus(); 63 } 64 // Do not pass other events to JOSM 65 if( code != KeyEvent.VK_LEFT && code != KeyEvent.VK_HOME && code != KeyEvent.VK_RIGHT 66 && code != KeyEvent.VK_END && code != KeyEvent.VK_BACK_SPACE && code != KeyEvent.VK_DELETE ) 67 e.consume(); 68 } 69 super.processKeyEvent(e); 70 } 71 51 protected void processEnter( String text ) { 52 connection.postMessage(text); 53 } 54 55 @Override 56 protected String autoComplete( String word ) { 57 return word; 58 } 72 59 }; 73 60 74 final JTextField nameField = new JTextField(); 61 final JTextField nameField = new JPanelTextField() { 62 @Override 63 protected void processEnter( String text ) { 64 connection.login(text); 65 } 66 }; 75 67 String userName = JosmUserIdentityManager.getInstance().getUserName(); 76 68 if( userName == null ) … … 97 89 createLayout(gcPanel, false, null); 98 90 91 users = new HashMap<String, LatLon>(); 99 92 // Start threads 100 93 connection = ChatServerConnection.getInstance(); 101 94 connection.addListener(this); 102 95 connection.checkLogin(); 96 } 97 98 private void addLineToPublic( String line ) { 99 Document doc = chatPane.getDocument(); 100 try { 101 doc.insertString(doc.getLength(), line, null); 102 } catch( BadLocationException ex ) { 103 // whatever 104 } 105 } 106 107 private String cachedTitle = ""; 108 private int cachedAlarm = 0; 109 110 public void setTitle( String title ) { 111 setTitle(title, -1); 112 } 113 114 private void setTitleAlarm( int alarmLevel ) { 115 setTitle(null, alarmLevel); 116 } 117 118 private void setTitle( String title, int alarmLevel ) { 119 if( title != null ) 120 cachedTitle = title; 121 if( alarmLevel >= 0 ) 122 cachedAlarm = alarmLevel; 123 String alarm = cachedAlarm <= 0 ? "" : cachedAlarm == 1 ? "* " : "[!] "; 124 super.setTitle(alarm + cachedTitle); 103 125 } 104 126 … … 124 146 125 147 public void updateUsers( Map<String, LatLon> users ) { 148 for( String name : this.users.keySet() ) { 149 if( !users.containsKey(name) ) 150 addLineToPublic(tr("User {0} has left", name)); 151 } 152 for( String name : users.keySet() ) { 153 if( !this.users.containsKey(name) ) 154 addLineToPublic(tr("User {0} is mapping nearby", name)); 155 } 156 // todo: update header with user count 157 setTitle(trn("GeoChat ({0} user)", "GeoChat({0} users)", users.size(), users.size())); 158 // todo: update users location 159 this.users = users; 126 160 } 127 161 … … 137 171 sb.append(msg.getAuthor()).append(": ").append(msg.getMessage()); 138 172 } 139 140 Document doc = chatPane.getDocument(); 141 try { 142 doc.insertString(doc.getLength(), sb.toString(), null); 143 } catch( BadLocationException ex ) { 144 // whatever 145 } 173 addLineToPublic(sb.toString()); 146 174 } 147 175 148 176 public void receivedPrivateMessages( boolean replace, List<ChatMessage> messages ) { 149 177 } 178 179 private class JPanelTextField extends JTextField { 180 @Override 181 protected void processKeyEvent( KeyEvent e ) { 182 if( e.getID() == KeyEvent.KEY_PRESSED ) { 183 int code = e.getKeyCode(); 184 if( code == KeyEvent.VK_ENTER ) { 185 String text = input.getText(); 186 if( text.length() > 0 ) { 187 processEnter(text); 188 input.setText(""); 189 } 190 } else if( code == KeyEvent.VK_TAB ) { 191 autoComplete(""); // todo 192 } else if( code == KeyEvent.VK_ESCAPE ) { 193 if( Main.map != null && Main.map.mapView != null ) 194 Main.map.mapView.requestFocus(); 195 } 196 // Do not pass other events to JOSM 197 if( code != KeyEvent.VK_LEFT && code != KeyEvent.VK_HOME && code != KeyEvent.VK_RIGHT 198 && code != KeyEvent.VK_END && code != KeyEvent.VK_BACK_SPACE && code != KeyEvent.VK_DELETE ) 199 e.consume(); 200 } 201 super.processKeyEvent(e); 202 } 203 204 protected void processEnter( String text ) { } 205 206 protected String autoComplete( String word ) { return word; } 207 } 150 208 }
Note:
See TracChangeset
for help on using the changeset viewer.