1 | // License: WTFPL
|
---|
2 | package geochat;
|
---|
3 |
|
---|
4 | import java.awt.*;
|
---|
5 | import java.awt.event.*;
|
---|
6 | import java.text.SimpleDateFormat;
|
---|
7 | import java.util.*;
|
---|
8 | import java.util.List;
|
---|
9 | import javax.swing.*;
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.data.Bounds;
|
---|
12 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
13 | import org.openstreetmap.josm.gui.JosmUserIdentityManager;
|
---|
14 | import org.openstreetmap.josm.gui.MapView;
|
---|
15 | import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
|
---|
16 | import org.openstreetmap.josm.gui.layer.MapViewPaintable;
|
---|
17 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
18 | 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 |
|
---|
22 | /**
|
---|
23 | * Chat Panel. Contains of one public chat pane and multiple private ones.
|
---|
24 | *
|
---|
25 | * @author zverik
|
---|
26 | */
|
---|
27 | public class GeoChatPanel extends ToggleDialog implements ChatServerConnectionListener, MapViewPaintable {
|
---|
28 | private JTextField input;
|
---|
29 | private JTabbedPane tabs;
|
---|
30 | private JComponent noData;
|
---|
31 | private JPanel loginPanel;
|
---|
32 | private JPanel gcPanel;
|
---|
33 | private ChatServerConnection connection;
|
---|
34 | // those fields should be visible to popup menu actions
|
---|
35 | protected Map<String, LatLon> users;
|
---|
36 | protected ChatPaneManager chatPanes;
|
---|
37 | protected boolean userLayerActive;
|
---|
38 |
|
---|
39 | public GeoChatPanel() {
|
---|
40 | super(tr("GeoChat"), "geochat", tr("Open GeoChat panel"), null, 200, true);
|
---|
41 |
|
---|
42 | noData = new JLabel(tr("Zoom in to see messages"), SwingConstants.CENTER);
|
---|
43 |
|
---|
44 | tabs = new JTabbedPane();
|
---|
45 | tabs.addMouseListener(new GeoChatPopupAdapter(this));
|
---|
46 | chatPanes = new ChatPaneManager(this, tabs);
|
---|
47 |
|
---|
48 | input = new JPanelTextField() {
|
---|
49 | @Override
|
---|
50 | protected void processEnter( String text ) {
|
---|
51 | connection.postMessage(text, chatPanes.getRecipient());
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | protected String autoComplete( String word, boolean atStart ) {
|
---|
56 | return autoCompleteUser(word, atStart);
|
---|
57 | }
|
---|
58 | };
|
---|
59 |
|
---|
60 | String defaultUserName = constructUserName();
|
---|
61 | loginPanel = createLoginPanel(defaultUserName);
|
---|
62 |
|
---|
63 | gcPanel = new JPanel(new BorderLayout());
|
---|
64 | gcPanel.add(loginPanel, BorderLayout.CENTER);
|
---|
65 | createLayout(gcPanel, false, null);
|
---|
66 |
|
---|
67 | users = new TreeMap<String, LatLon>();
|
---|
68 | // Start threads
|
---|
69 | connection = ChatServerConnection.getInstance();
|
---|
70 | connection.addListener(this);
|
---|
71 | boolean autoLogin = Main.pref.get("geochat.username", null) == null ? false : Main.pref.getBoolean("geochat.autologin", true);
|
---|
72 | connection.autoLoginWithDelay(autoLogin ? defaultUserName : null);
|
---|
73 | updateTitleAlarm();
|
---|
74 | }
|
---|
75 |
|
---|
76 | private String constructUserName() {
|
---|
77 | String userName = Main.pref.get("geochat.username", null); // so the default is null
|
---|
78 | if( userName == null )
|
---|
79 | userName = JosmUserIdentityManager.getInstance().getUserName();
|
---|
80 | if( userName == null )
|
---|
81 | userName = "";
|
---|
82 | if( userName.contains("@") )
|
---|
83 | userName = userName.substring(0, userName.indexOf('@'));
|
---|
84 | userName = userName.replace(' ', '_');
|
---|
85 | return userName;
|
---|
86 | }
|
---|
87 |
|
---|
88 | private JPanel createLoginPanel( String defaultUserName ) {
|
---|
89 | final JTextField nameField = new JPanelTextField() {
|
---|
90 | @Override
|
---|
91 | protected void processEnter( String text ) {
|
---|
92 | connection.login(text);
|
---|
93 | }
|
---|
94 | };
|
---|
95 | nameField.setText(defaultUserName);
|
---|
96 |
|
---|
97 | JButton loginButton = new JButton(tr("Login"));
|
---|
98 | loginButton.addActionListener(new ActionListener() {
|
---|
99 | public void actionPerformed( ActionEvent e ) {
|
---|
100 | connection.login(nameField.getText());
|
---|
101 | }
|
---|
102 | });
|
---|
103 | nameField.setPreferredSize(new Dimension(nameField.getPreferredSize().width, loginButton.getPreferredSize().height));
|
---|
104 |
|
---|
105 | final JCheckBox autoLoginBox = new JCheckBox(tr("Enable autologin"), Main.pref.getBoolean("geochat.autologin", true));
|
---|
106 | autoLoginBox.addActionListener(new ActionListener() {
|
---|
107 | public void actionPerformed( ActionEvent e ) {
|
---|
108 | Main.pref.put("geochat.autologin", autoLoginBox.isSelected());
|
---|
109 | }
|
---|
110 | });
|
---|
111 |
|
---|
112 | JPanel panel = new JPanel(new GridBagLayout());
|
---|
113 | panel.add(nameField, GBC.std().fill(GridBagConstraints.HORIZONTAL).insets(15, 0, 5, 0));
|
---|
114 | panel.add(loginButton, GBC.eol().fill(GridBagConstraints.NONE).insets(0, 0, 15, 0));
|
---|
115 | panel.add(autoLoginBox, GBC.std().insets(15, 0, 15, 0));
|
---|
116 | return panel;
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected void logout() {
|
---|
120 | connection.logout();
|
---|
121 | }
|
---|
122 |
|
---|
123 | @Override
|
---|
124 | public void destroy() {
|
---|
125 | try {
|
---|
126 | if( Main.pref.getBoolean("geochat.logout.on.close", true) ) {
|
---|
127 | connection.removeListener(this);
|
---|
128 | connection.bruteLogout();
|
---|
129 | }
|
---|
130 | } catch( Throwable e ) {
|
---|
131 | Main.warn("Failed to logout from geochat server: " + e.getMessage());
|
---|
132 | }
|
---|
133 | super.destroy();
|
---|
134 | }
|
---|
135 |
|
---|
136 | private String autoCompleteUser( String word, boolean atStart ) {
|
---|
137 | String result = null;
|
---|
138 | boolean singleUser = true;
|
---|
139 | for( String user : users.keySet() ) {
|
---|
140 | if( user.startsWith(word) ) {
|
---|
141 | if( result == null )
|
---|
142 | result = user;
|
---|
143 | else {
|
---|
144 | singleUser = false;
|
---|
145 | int i = word.length();
|
---|
146 | while( i < result.length() && i < user.length() && result.charAt(i) == user.charAt(i) )
|
---|
147 | i++;
|
---|
148 | if( i < result.length() )
|
---|
149 | result = result.substring(0, i);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 | return result == null ? null : !singleUser ? result : atStart ? result + ": " : result + " ";
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * This is implementation of a "temporary layer". It paints circles
|
---|
158 | * for all users nearby.
|
---|
159 | */
|
---|
160 | public void paint( Graphics2D g, MapView mv, Bounds bbox ) {
|
---|
161 | Graphics2D g2d = (Graphics2D)g.create();
|
---|
162 | g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
---|
163 | Composite ac04 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f);
|
---|
164 | Composite ac10 = g2d.getComposite();
|
---|
165 |
|
---|
166 | Font font = g2d.getFont().deriveFont(Font.BOLD, g2d.getFont().getSize2D() + 2.0f);
|
---|
167 | g2d.setFont(font);
|
---|
168 | FontMetrics fm = g2d.getFontMetrics();
|
---|
169 |
|
---|
170 | for( String user : users.keySet() ) {
|
---|
171 | int stringWidth = fm.stringWidth(user);
|
---|
172 | int radius = stringWidth / 2 + 10;
|
---|
173 | Point p = mv.getPoint(users.get(user));
|
---|
174 |
|
---|
175 | g2d.setComposite(ac04);
|
---|
176 | g2d.setColor(Color.white);
|
---|
177 | g2d.fillOval(p.x - radius, p.y - radius, radius * 2 + 1, radius * 2 + 1);
|
---|
178 |
|
---|
179 | g2d.setComposite(ac10);
|
---|
180 | g2d.setColor(Color.black);
|
---|
181 | g2d.drawString(user, p.x - stringWidth / 2, p.y + fm.getDescent());
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | /* ================== Notifications in the title ======================= */
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Display number of users and notifications in the panel title.
|
---|
189 | */
|
---|
190 | protected void updateTitleAlarm() {
|
---|
191 | int alarmLevel = connection.isLoggedIn() ? chatPanes.getNotifyLevel() : 0;
|
---|
192 | if( !isDialogInCollapsedView() && alarmLevel > 1 )
|
---|
193 | alarmLevel = 1;
|
---|
194 |
|
---|
195 | String comment;
|
---|
196 | if( connection.isLoggedIn() ) {
|
---|
197 | comment = trn("{0} user", "{0} users", users.size() + 1, users.size() + 1);
|
---|
198 | } else {
|
---|
199 | comment = tr("not logged in");
|
---|
200 | }
|
---|
201 |
|
---|
202 | String title = tr("GeoChat");
|
---|
203 | if( comment != null )
|
---|
204 | title = title + " (" + comment + ")";
|
---|
205 | final String alarm = (alarmLevel <= 0 ? "" : alarmLevel == 1 ? "* " : "!!! ") + title;
|
---|
206 | GuiHelper.runInEDT(new Runnable() {
|
---|
207 | public void run() {
|
---|
208 | setTitle(alarm);
|
---|
209 | }
|
---|
210 | });
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Track panel collapse events.
|
---|
215 | */
|
---|
216 | @Override
|
---|
217 | protected void setIsCollapsed( boolean val ) {
|
---|
218 | super.setIsCollapsed(val);
|
---|
219 | chatPanes.setCollapsed(val);
|
---|
220 | updateTitleAlarm();
|
---|
221 | }
|
---|
222 |
|
---|
223 | /* ============ ChatServerConnectionListener methods ============= */
|
---|
224 |
|
---|
225 | public void loggedIn( String userName ) {
|
---|
226 | Main.pref.put("geochat.username", userName);
|
---|
227 | if( gcPanel.getComponentCount() == 1 ) {
|
---|
228 | GuiHelper.runInEDTAndWait(new Runnable() {
|
---|
229 | @Override
|
---|
230 | public void run() {
|
---|
231 | gcPanel.remove(0);
|
---|
232 | gcPanel.add(tabs, BorderLayout.CENTER);
|
---|
233 | gcPanel.add(input, BorderLayout.SOUTH);
|
---|
234 | }
|
---|
235 | });
|
---|
236 | }
|
---|
237 | updateTitleAlarm();
|
---|
238 | }
|
---|
239 |
|
---|
240 | public void notLoggedIn( final String reason ) {
|
---|
241 | if( reason != null ) {
|
---|
242 | SwingUtilities.invokeLater(new Runnable() {
|
---|
243 | public void run() {
|
---|
244 | JOptionPane.showMessageDialog(Main.parent, reason);
|
---|
245 | }
|
---|
246 | });
|
---|
247 | } else {
|
---|
248 | // regular logout
|
---|
249 | if( gcPanel.getComponentCount() > 1 ) {
|
---|
250 | gcPanel.removeAll();
|
---|
251 | gcPanel.add(loginPanel, BorderLayout.CENTER);
|
---|
252 | }
|
---|
253 | }
|
---|
254 | updateTitleAlarm();
|
---|
255 | }
|
---|
256 |
|
---|
257 | public void messageSendFailed( final String reason ) {
|
---|
258 | SwingUtilities.invokeLater(new Runnable() {
|
---|
259 | public void run() {
|
---|
260 | JOptionPane.showMessageDialog(Main.parent, reason);
|
---|
261 | }
|
---|
262 | });
|
---|
263 | }
|
---|
264 |
|
---|
265 | public void statusChanged( boolean active ) {
|
---|
266 | // only the public tab, because private chats don't rely on coordinates
|
---|
267 | tabs.setComponentAt(0, active ? chatPanes.getPublicChatComponent() : noData);
|
---|
268 | repaint();
|
---|
269 | }
|
---|
270 |
|
---|
271 | public void updateUsers( Map<String, LatLon> newUsers ) {
|
---|
272 | for( String uname : this.users.keySet() ) {
|
---|
273 | if( !newUsers.containsKey(uname) )
|
---|
274 | chatPanes.addLineToPublic(tr("User {0} has left", uname), ChatPaneManager.MESSAGE_TYPE_INFORMATION);
|
---|
275 | }
|
---|
276 | for( String uname : newUsers.keySet() ) {
|
---|
277 | if( !this.users.containsKey(uname) )
|
---|
278 | chatPanes.addLineToPublic(tr("User {0} is mapping nearby", uname), ChatPaneManager.MESSAGE_TYPE_INFORMATION);
|
---|
279 | }
|
---|
280 | this.users = newUsers;
|
---|
281 | updateTitleAlarm();
|
---|
282 | if( userLayerActive && Main.map.mapView != null )
|
---|
283 | Main.map.mapView.repaint();
|
---|
284 | }
|
---|
285 |
|
---|
286 | private final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm");
|
---|
287 |
|
---|
288 | private void formatMessage( StringBuilder sb, ChatMessage msg ) {
|
---|
289 | sb.append("\n");
|
---|
290 | sb.append('[').append(TIME_FORMAT.format(msg.getTime())).append("] ");
|
---|
291 | sb.append(msg.getAuthor()).append(": ").append(msg.getMessage());
|
---|
292 | }
|
---|
293 |
|
---|
294 | public void receivedMessages( boolean replace, List<ChatMessage> messages ) {
|
---|
295 | if( replace )
|
---|
296 | chatPanes.clearPublicChatPane();
|
---|
297 | if( !messages.isEmpty() ) {
|
---|
298 | int alarm = 0;
|
---|
299 | StringBuilder sb = new StringBuilder();
|
---|
300 | for( ChatMessage msg : messages ) {
|
---|
301 | boolean important = msg.isIncoming() && containsName(msg.getMessage());
|
---|
302 | if( msg.isIncoming() && alarm < 2 ) {
|
---|
303 | alarm = important ? 2 : 1;
|
---|
304 | }
|
---|
305 | if( important ) {
|
---|
306 | // add buffer, then add current line with italic, then clear buffer
|
---|
307 | chatPanes.addLineToPublic(sb.toString());
|
---|
308 | sb.setLength(0);
|
---|
309 | formatMessage(sb, msg);
|
---|
310 | chatPanes.addLineToPublic(sb.toString(), ChatPaneManager.MESSAGE_TYPE_ATTENTION);
|
---|
311 | sb.setLength(0);
|
---|
312 | } else
|
---|
313 | formatMessage(sb, msg);
|
---|
314 | }
|
---|
315 | chatPanes.addLineToPublic(sb.toString());
|
---|
316 | if( alarm > 0 )
|
---|
317 | chatPanes.notify(null, alarm);
|
---|
318 | }
|
---|
319 | if( replace )
|
---|
320 | showNearbyUsers();
|
---|
321 | }
|
---|
322 |
|
---|
323 | private void showNearbyUsers() {
|
---|
324 | if( !users.isEmpty() ) {
|
---|
325 | StringBuilder sb = new StringBuilder(tr("Users mapping nearby:"));
|
---|
326 | boolean first = true;
|
---|
327 | for( String user : users.keySet() ) {
|
---|
328 | sb.append(first ? " " : ", ");
|
---|
329 | sb.append(user);
|
---|
330 | }
|
---|
331 | chatPanes.addLineToPublic(sb.toString(), ChatPaneManager.MESSAGE_TYPE_INFORMATION);
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|
335 | private boolean containsName( String message ) {
|
---|
336 | String userName = connection.getUserName();
|
---|
337 | int length = userName.length();
|
---|
338 | int i = message.indexOf(userName);
|
---|
339 | while( i >= 0 ) {
|
---|
340 | if( (i == 0 || !Character.isJavaIdentifierPart(message.charAt(i - 1)))
|
---|
341 | && (i + length >= message.length() || !Character.isJavaIdentifierPart(message.charAt(i + length))) )
|
---|
342 | return true;
|
---|
343 | i = message.indexOf(userName, i + 1);
|
---|
344 | }
|
---|
345 | return false;
|
---|
346 | }
|
---|
347 |
|
---|
348 | public void receivedPrivateMessages( boolean replace, List<ChatMessage> messages ) {
|
---|
349 | if( replace )
|
---|
350 | chatPanes.closePrivateChatPanes();
|
---|
351 | for( ChatMessage msg : messages ) {
|
---|
352 | StringBuilder sb = new StringBuilder();
|
---|
353 | formatMessage(sb, msg);
|
---|
354 | chatPanes.addLineToChatPane(msg.isIncoming() ? msg.getAuthor() : msg.getRecipient(), sb.toString());
|
---|
355 | if( msg.isIncoming() )
|
---|
356 | chatPanes.notify(msg.getAuthor(), 2);
|
---|
357 | }
|
---|
358 | }
|
---|
359 | }
|
---|