Changeset 29851 in osm for applications/editors/josm


Ignore:
Timestamp:
2013-08-20T23:00:22+02:00 (11 years ago)
Author:
zverik
Message:

fix EDT, async -> sync

Location:
applications/editors/josm/plugins/geochat/src/geochat
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/geochat/src/geochat/ChatPaneManager.java

    r29590 r29851  
    1111import javax.swing.text.*;
    1212import org.openstreetmap.josm.Main;
     13import org.openstreetmap.josm.gui.util.GuiHelper;
    1314import static org.openstreetmap.josm.tools.I18n.tr;
    1415
     
    8081    private static Color COLOR_ATTENTION = new Color(0, 0, 192);
    8182
    82     private void addLineToChatPane( String userName, String line, int messageType ) {
     83    private void addLineToChatPane( String userName, String line, final int messageType ) {
    8384        if( line.length() == 0 )
    8485            return;
    8586        if( !chatPanes.containsKey(userName) )
    8687            createChatPane(userName);
    87         if( !line.startsWith("\n") )
    88             line = "\n" + line;
    89         Document doc = chatPanes.get(userName).pane.getDocument();
    90         try {
    91             SimpleAttributeSet attrs = null;
    92             if( messageType != MESSAGE_TYPE_DEFAULT ) {
    93                 attrs = new SimpleAttributeSet();
    94                 if( messageType == MESSAGE_TYPE_INFORMATION )
    95                     StyleConstants.setItalic(attrs, true);
    96                 else if( messageType == MESSAGE_TYPE_ATTENTION )
    97                     StyleConstants.setForeground(attrs, COLOR_ATTENTION);
     88        final String nline = line.startsWith("\n") ? line : "\n" + line;
     89        final JTextPane thepane = chatPanes.get(userName).pane;
     90        GuiHelper.runInEDT(new Runnable() {
     91            public void run() {
     92                Document doc = thepane.getDocument();
     93                try {
     94                    SimpleAttributeSet attrs = null;
     95                    if( messageType != MESSAGE_TYPE_DEFAULT ) {
     96                        attrs = new SimpleAttributeSet();
     97                        if( messageType == MESSAGE_TYPE_INFORMATION )
     98                            StyleConstants.setItalic(attrs, true);
     99                        else if( messageType == MESSAGE_TYPE_ATTENTION )
     100                            StyleConstants.setForeground(attrs, COLOR_ATTENTION);
     101                    }
     102                    doc.insertString(doc.getLength(), nline, attrs);
     103                } catch( BadLocationException ex ) {
     104                    // whatever
     105                }
     106                thepane.setCaretPosition(doc.getLength());
    98107            }
    99             doc.insertString(doc.getLength(), line, attrs);
    100         } catch( BadLocationException ex ) {
    101             // whatever
    102         }
    103         chatPanes.get(userName).pane.setCaretPosition(doc.getLength());
     108        });
    104109    }
    105110
  • applications/editors/josm/plugins/geochat/src/geochat/ChatServerConnection.java

    r29592 r29851  
    66import java.net.URLEncoder;
    77import java.util.*;
     8import java.util.logging.*;
    89import org.json.JSONArray;
    910import org.json.JSONException;
     
    348349                    + "&lon=" + pos.lonToString(CoordinateFormat.DECIMAL_DEGREES)
    349350                    + "&uid=" + userId + "&last=" + lastId;
    350             JsonQueryUtil.queryAsync(query, new JsonQueryCallback() {
    351                 public void processJson( JSONObject json ) {
    352                     if( json == null ) {
    353                         // do nothing?
    354 //                        fireLoginFailed(tr("Could not get server response, check logs"));
    355 //                        logoutIntl(); // todo: uncomment?
    356                     } else if( json.has("error") ) {
    357                         fireLoginFailed(tr("Failed to get messages as {0}:", userName) + "\n" + json.getString("error"));
    358                         logoutIntl();
    359                     } else {
    360                         if( json.has("users") ) {
    361                             Map<String, LatLon> users = parseUsers(json.getJSONArray("users"));
    362                             for( ChatServerConnectionListener listener : listeners )
    363                                 listener.updateUsers(users);
    364                         }
    365                         if( json.has("messages") ) {
    366                             List<ChatMessage> messages = parseMessages(json.getJSONArray("messages"), false);
    367                             for( ChatMessage m : messages )
    368                                 if( m.getId() > lastId )
    369                                     lastId = m.getId();
    370                             for( ChatServerConnectionListener listener : listeners )
    371                                 listener.receivedMessages(needReset, messages);
    372                         }
    373                         if( json.has("private") ) {
    374                             List<ChatMessage> messages = parseMessages(json.getJSONArray("private"), true);
    375                             for( ChatMessage m : messages )
    376                                 if( m.getId() > lastId )
    377                                     lastId = m.getId();
    378                             for( ChatServerConnectionListener listener : listeners )
    379                                 listener.receivedPrivateMessages(needFullReset, messages);
    380                         }
    381                     }
     351            JSONObject json;
     352            try {
     353                json = JsonQueryUtil.query(query);
     354            } catch( IOException ex ) {
     355                json = null; // ?
     356            }
     357            if( json == null ) {
     358                // do nothing?
     359//              fireLoginFailed(tr("Could not get server response, check logs"));
     360//              logoutIntl(); // todo: uncomment?
     361            } else if( json.has("error") ) {
     362                fireLoginFailed(tr("Failed to get messages as {0}:", userName) + "\n" + json.getString("error"));
     363                logoutIntl();
     364            } else {
     365                if( json.has("users") ) {
     366                    Map<String, LatLon> users = parseUsers(json.getJSONArray("users"));
     367                    for( ChatServerConnectionListener listener : listeners )
     368                        listener.updateUsers(users);
     369                }
     370                if( json.has("messages") ) {
     371                    List<ChatMessage> messages = parseMessages(json.getJSONArray("messages"), false);
     372                    for( ChatMessage m : messages )
     373                        if( m.getId() > lastId )
     374                            lastId = m.getId();
     375                    for( ChatServerConnectionListener listener : listeners )
     376                        listener.receivedMessages(needReset, messages);
     377                }
     378                if( json.has("private") ) {
     379                    List<ChatMessage> messages = parseMessages(json.getJSONArray("private"), true);
     380                    for( ChatMessage m : messages )
     381                        if( m.getId() > lastId )
     382                            lastId = m.getId();
     383                    for( ChatServerConnectionListener listener : listeners )
     384                        listener.receivedPrivateMessages(needFullReset, messages);
     385                }
     386            }
    382387//                    if( lastId > 0 && Main.pref.getBoolean("geochat.store.lastid", true) )
    383388//                        Main.pref.putLong("geochat.lastid", lastId);
    384                 }
    385             });
    386389        }
    387390
  • applications/editors/josm/plugins/geochat/src/geochat/GeoChatPanel.java

    r29590 r29851  
    1515import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
    1616import org.openstreetmap.josm.gui.layer.MapViewPaintable;
     17import org.openstreetmap.josm.gui.util.GuiHelper;
    1718import org.openstreetmap.josm.tools.GBC;
    1819import static org.openstreetmap.josm.tools.I18n.tr;
     
    202203        if( comment != null )
    203204            title = title + " (" + comment + ")";
    204         String alarm = alarmLevel <= 0 ? "" : alarmLevel == 1 ? "* " : "!!! ";
    205         setTitle(alarm + title);
     205        final String alarm = (alarmLevel <= 0 ? "" : alarmLevel == 1 ? "* " : "!!! ") + title;
     206        GuiHelper.runInEDT(new Runnable() {
     207            public void run() {
     208                setTitle(alarm);
     209            }
     210        });
    206211    }
    207212
Note: See TracChangeset for help on using the changeset viewer.