Changeset 29544 in osm for applications/editors/josm


Ignore:
Timestamp:
2013-04-30T06:57:29+02:00 (11 years ago)
Author:
zverik
Message:

small fixes

File:
1 edited

Legend:

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

    r29541 r29544  
    1717import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
    1818import static org.openstreetmap.josm.tools.I18n.tr;
     19import static org.openstreetmap.josm.tools.I18n.trn;
    1920
    2021/**
     
    3132    private JPanel gcPanel;
    3233    private ChatServerConnection connection;
     34    private Map<String, LatLon> users;
    3335   
    3436    public GeoChatPanel() {
     
    4345
    4446        tabs = new JTabbedPane();
    45         tabs.addTab(tr("Public"), chatPane);
    46 
    47         input = new JTextField() {
     47        tabs.addTab(tr("Public"), new JScrollPane(chatPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
     48
     49        input = new JPanelTextField() {
    4850            @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            }
    7259        };
    7360
    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        };
    7567        String userName = JosmUserIdentityManager.getInstance().getUserName();
    7668        if( userName == null )
     
    9789        createLayout(gcPanel, false, null);
    9890
     91        users = new HashMap<String, LatLon>();
    9992        // Start threads
    10093        connection = ChatServerConnection.getInstance();
    10194        connection.addListener(this);
    10295        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);
    103125    }
    104126
     
    124146
    125147    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;
    126160    }
    127161
     
    137171            sb.append(msg.getAuthor()).append(": ").append(msg.getMessage());
    138172        }
    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());
    146174    }
    147175
    148176    public void receivedPrivateMessages( boolean replace, List<ChatMessage> messages ) {
    149177    }
     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    }
    150208}
Note: See TracChangeset for help on using the changeset viewer.