Ignore:
Timestamp:
2013-05-09T23:29:49+02:00 (11 years ago)
Author:
zverik
Message:

[josm_geochat] fixed some things, added logout and copy

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

Legend:

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

    r29574 r29576  
    11package geochat;
    22
     3import java.awt.Color;
    34import java.awt.Component;
    45import java.awt.Font;
     
    7273    }
    7374
    74     private void addLineToChatPane( String userName, String line, boolean italic ) {
     75    public static int MESSAGE_TYPE_DEFAULT = 0;
     76    public static int MESSAGE_TYPE_INFORMATION = 1;
     77    public static int MESSAGE_TYPE_ATTENTION = 2;
     78    private static Color COLOR_ATTENTION = new Color(0, 0, 192);
     79
     80    private void addLineToChatPane( String userName, String line, int messageType ) {
    7581        if( line.length() == 0 )
    7682            return;
     
    8288        try {
    8389            SimpleAttributeSet attrs = null;
    84             if( italic ) {
     90            if( messageType != MESSAGE_TYPE_DEFAULT ) {
    8591                attrs = new SimpleAttributeSet();
    86                 StyleConstants.setItalic(attrs, true);
     92                if( messageType == MESSAGE_TYPE_INFORMATION )
     93                    StyleConstants.setItalic(attrs, true);
     94                else if( messageType == MESSAGE_TYPE_ATTENTION )
     95                    StyleConstants.setForeground(attrs, COLOR_ATTENTION);
    8796            }
    8897            doc.insertString(doc.getLength(), line, attrs);
     
    94103
    95104    public void addLineToChatPane( String userName, String line ) {
    96         addLineToChatPane(userName, line, false);
     105        addLineToChatPane(userName, line, MESSAGE_TYPE_DEFAULT);
    97106    }
    98107
     
    101110    }
    102111
     112    public void addLineToPublic( String line, int messageType ) {
     113        addLineToChatPane(PUBLIC_PANE, line, messageType);
     114    }
     115
    103116    public void clearPublicChatPane() {
    104117        chatPanes.get(PUBLIC_PANE).pane.setText("");
    105         showNearbyUsers();
    106     }
    107 
    108     private void showNearbyUsers() {
    109         if( !panel.users.isEmpty() ) {
    110             StringBuilder sb = new StringBuilder(tr("Users mapping nearby:"));
    111             boolean first = true;
    112             for( String user : panel.users.keySet() ) {
    113                 sb.append(first ? " " : ", ");
    114                 sb.append(user);
    115             }
    116             addLineToPublic(sb.toString());
    117         }
    118118    }
    119119
     
    192192    }
    193193   
     194    public boolean hasSelectedText() {
     195        String user = getActiveChatPane();
     196        if( user != null ) {
     197            JTextPane pane = chatPanes.get(user).pane;
     198            return pane.getSelectedText() != null;
     199        }
     200        return false;
     201    }
     202
     203    public void copySelectedText() {
     204        String user = getActiveChatPane();
     205        if( user != null )
     206            chatPanes.get(user).pane.copy();
     207    }
     208   
    194209
    195210    private class ChatTabTitleComponent extends JLabel {
  • applications/editors/josm/plugins/geochat/src/geochat/ChatServerConnection.java

    r29568 r29576  
    11package geochat;
    22
     3import java.io.IOException;
    34import java.io.UnsupportedEncodingException;
    45import java.net.URLEncoder;
    56import java.util.*;
    6 import javax.swing.SwingUtilities;
    77import org.json.JSONArray;
    88import org.json.JSONException;
     
    143143    }
    144144
     145    /**
     146     * Unregister the current user and do not call listeners.
     147     * Makes synchronous request to the server.
     148     */
     149    public void bruteLogout() throws IOException {
     150        if( isLoggedIn() )
     151            JsonQueryUtil.query("logout&uid=" + userId);
     152    }
     153
    145154    private void fireMessageFailed( String reason ) {
    146155        for( ChatServerConnectionListener listener : listeners )
     
    248257
    249258        public void run() {
     259//            lastId = Main.pref.getLong("geochat.lastid", 0);
    250260            int interval = Main.pref.getInteger("geochat.interval", 2);
    251261            while( !stopping ) {
     
    281291                // reset messages
    282292                lastId = 0;
     293//                Main.pref.put("geochat.lastid", null);
    283294                needReset = true;
    284295            } else
     
    322333                        }
    323334                    }
     335//                    if( lastId > 0 && Main.pref.getBoolean("geochat.store.lastid", true) )
     336//                        Main.pref.putLong("geochat.lastid", lastId);
    324337                }
    325338            });
  • applications/editors/josm/plugins/geochat/src/geochat/GeoChatPanel.java

    r29572 r29576  
    5252
    5353            @Override
    54             protected String autoComplete( String word ) {
    55                 return autoCompleteUser(word);
     54            protected String autoComplete( String word, boolean atStart ) {
     55                return autoCompleteUser(word, atStart);
    5656            }
    5757        };
     
    103103    }
    104104
    105     private String autoCompleteUser( String word ) {
     105    @Override
     106    public void destroy() {
     107        try {
     108            if( Main.pref.getBoolean("geochat.logout.on.close", true) ) {
     109                connection.removeListener(this);
     110                connection.bruteLogout();
     111            }
     112        } catch( Throwable e ) {
     113            Main.warn("Failed to logout from geochat server: " + e.getMessage());
     114        }
     115        super.destroy();
     116    }
     117
     118    private String autoCompleteUser( String word, boolean atStart ) {
    106119        String result = null;
     120        boolean singleUser = true;
    107121        for( String user : users.keySet() ) {
    108122            if( user.startsWith(word) ) {
     
    110124                    result = user;
    111125                else {
     126                    singleUser = false;
    112127                    int i = word.length();
    113128                    while( i < result.length() && i < user.length() && result.charAt(i) == user.charAt(i) )
     
    118133            }
    119134        }
    120         return result;
     135        return result == null ? null : !singleUser ? result : atStart ? result + ": " : result + " ";
    121136    }
    122137
     
    159174        if( !isDialogInCollapsedView() && alarmLevel > 1 )
    160175            alarmLevel = 1;
    161         String name = users.isEmpty() ? tr("GeoChat") :
     176        String title = users.isEmpty() ? tr("GeoChat") :
    162177                trn("GeoChat ({0} user)", "GeoChat ({0} users)", users.size(), users.size());
    163178        String alarm = alarmLevel <= 0 ? "" : alarmLevel == 1 ? "* " : "!!! ";
    164         setTitle(alarm + name);
     179        setTitle(alarm + title);
    165180    }
    166181
     
    218233        for( String uname : this.users.keySet() ) {
    219234            if( !newUsers.containsKey(uname) )
    220                 chatPanes.addLineToPublic(tr("User {0} has left", uname));
     235                chatPanes.addLineToPublic(tr("User {0} has left", uname), ChatPaneManager.MESSAGE_TYPE_INFORMATION);
    221236        }
    222237        for( String uname : newUsers.keySet() ) {
    223238            if( !this.users.containsKey(uname) )
    224                 chatPanes.addLineToPublic(tr("User {0} is mapping nearby", uname));
     239                chatPanes.addLineToPublic(tr("User {0} is mapping nearby", uname), ChatPaneManager.MESSAGE_TYPE_INFORMATION);
    225240        }
    226241        this.users = newUsers;
     
    254269                    sb.setLength(0);
    255270                    formatMessage(sb, msg);
    256                     chatPanes.addLineToPublicEm(sb.toString());
     271                    chatPanes.addLineToPublic(sb.toString(), ChatPaneManager.MESSAGE_TYPE_ATTENTION);
    257272                    sb.setLength(0);
    258273                } else
     
    262277            if( alarm > 0 )
    263278                chatPanes.notify(null, alarm);
     279        }
     280        if( replace )
     281            showNearbyUsers();
     282    }
     283
     284    private void showNearbyUsers() {
     285        if( !users.isEmpty() ) {
     286            StringBuilder sb = new StringBuilder(tr("Users mapping nearby:"));
     287            boolean first = true;
     288            for( String user : users.keySet() ) {
     289                sb.append(first ? " " : ", ");
     290                sb.append(user);
     291            }
     292            chatPanes.addLineToPublic(sb.toString(), ChatPaneManager.MESSAGE_TYPE_INFORMATION);
    264293        }
    265294    }
  • applications/editors/josm/plugins/geochat/src/geochat/GeoChatPopupAdapter.java

    r29568 r29576  
    4343
    4444        JPopupMenu menu = new JPopupMenu();
     45        if( panel.chatPanes.hasSelectedText() )
     46            menu.add(new CopyTextAction());
    4547        menu.add(new JCheckBoxMenuItem(new ToggleUserLayerAction()));
    4648        if( userMenu.getItemCount() > 0 )
     
    4951            menu.add(new CloseTabAction());
    5052//        menu.add(new ClearPaneAction());
    51 //        menu.add(new LogoutAction());
     53        menu.add(new LogoutAction());
    5254        return menu;
    5355    }
     
    118120        }
    119121    }
     122
     123    private class CopyTextAction extends AbstractAction {
     124        public CopyTextAction() {
     125            super(tr("Copy"));
     126//            putValue(SMALL_ICON, ImageProvider.get("help"));
     127        }
     128
     129        public void actionPerformed( ActionEvent e ) {
     130            panel.chatPanes.copySelectedText();
     131        }
     132    }
    120133}
  • applications/editors/josm/plugins/geochat/src/geochat/JPanelTextField.java

    r29571 r29576  
    3737                    start--;
    3838                start++;
    39                 System.out.println("Autocomplete! Word " + start + "-" + caret + " (not inclusive)");
    4039                if( start < caret ) {
    4140                    String word = text.substring(start, caret);
    42                     String complete = word == null ? null : autoComplete(word);
     41                    String complete = word == null ? null : autoComplete(word, start == 0);
    4342                    if( complete != null && !complete.equals(word) ) {
    4443                        StringBuilder sb = new StringBuilder();
     
    7574     * @return The whole word.
    7675     */
    77     protected String autoComplete( String word ) {
     76    protected String autoComplete( String word, boolean atStart ) {
    7877        return word;
    7978    }
Note: See TracChangeset for help on using the changeset viewer.