Changeset 29571 in osm for applications/editors/josm


Ignore:
Timestamp:
2013-05-09T21:00:19+02:00 (11 years ago)
Author:
zverik
Message:

[josm_geochat] autocomplete works, notification was redone slightly

Location:
applications/editors/josm/plugins/geochat
Files:
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/geochat/build.xml

    r29540 r29571  
    2222    <property name="plugin.class" value="geochat.GeoChatPlugin"/>
    2323    <property name="plugin.description" value="Talk with users editing the map nearby, be notified when someone comes close."/>
    24     <property name="plugin.icon" value="images/geochat.png"/>
    25     <property name="plugin.link" value="http://wiki.openstreetmap.org/wiki/GeoChat"/>
     24    <property name="plugin.icon" value="images/dialogs/geochat.png"/>
     25    <property name="plugin.link" value="http://wiki.openstreetmap.org/wiki/JOSM/Plugins/GeoChat"/>
    2626
    2727    <target name="additional-manifest">
     
    2929            <attribute name="ru_Author" value="Илья Зверев"/>
    3030            <attribute name="ru_Plugin-Description" value="Панель для общения с редактирующими карту поблизости, а также способ узнать, когда кто-то собирается править окрестности."/>
     31            <attribute name="ru_Plugin_Link" value="http://wiki.openstreetmap.org/wiki/RU:JOSM/Plugins/GeoChat"/>
    3132        </manifest>
    3233    </target>
  • applications/editors/josm/plugins/geochat/src/geochat/ChatPaneManager.java

    r29568 r29571  
    77import javax.swing.event.ChangeEvent;
    88import javax.swing.event.ChangeListener;
    9 import javax.swing.text.BadLocationException;
    10 import javax.swing.text.DefaultCaret;
    11 import javax.swing.text.Document;
     9import javax.swing.text.*;
    1210import static org.openstreetmap.josm.tools.I18n.tr;
    1311
     
    5351        int alarm = 0;
    5452        for( ChatPane entry : chatPanes.values() ) {
    55             if( entry.notify ) {
    56                 if( entry.isPublic && alarm < 1 )
    57                     alarm = 1;
    58                 else if( !entry.isPublic )
    59                     alarm = 2;
    60             }
     53            if( entry.notify > alarm )
     54                alarm = entry.notify;
    6155        }
    6256        return alarm;
     
    6862    }
    6963
    70     public void notify( String user, boolean really ) {
    71 //        if( user == null && !really && !collapsed )
    72 //            return;
    73         if( !hasUser(user) )
     64    public void notify( String user, int alarmLevel ) {
     65        if( alarmLevel <= 0 || !hasUser(user) )
    7466            return;
    7567        ChatPane entry = chatPanes.get(user == null ? PUBLIC_PANE : user);
    76         System.out.println("Notifying " + user);
    77         entry.notify = true;
     68        entry.notify = alarmLevel;
    7869        int idx = tabs.indexOfComponent(entry.component);
    7970        if( idx >= 0 )
     
    8273
    8374    public void addLineToChatPane( String userName, String line ) {
     75        if( line.length() == 0 )
     76            return;
    8477        if( !chatPanes.containsKey(userName) )
    8578            createChatPane(userName);
     
    9891    }
    9992
     93    /**
     94     * Special case: the line contains username, so it must be highlighted.
     95     */
     96    public void addLineToPublicEm( String line ) {
     97        if( !line.startsWith("\n") )
     98            line = "\n" + line;
     99        Document doc = chatPanes.get(PUBLIC_PANE).pane.getDocument();
     100        try {
     101            SimpleAttributeSet attrs = new SimpleAttributeSet();
     102            StyleConstants.setItalic(attrs, true);
     103            doc.insertString(doc.getLength(), line, attrs);
     104        } catch( BadLocationException ex ) {
     105            // whatever
     106        }
     107    }
     108
    100109    public void clearPublicChatPane() {
    101110        chatPanes.get(PUBLIC_PANE).pane.setText("");
     
    139148        entry.pane = chatPane;
    140149        entry.component = scrollPane;
    141         entry.notify = false;
     150        entry.notify = 0;
    142151        entry.userName = userName;
    143152        entry.isPublic = userName == null;
     
    207216                boldFont = getFont().deriveFont(Font.BOLD);
    208217            }
    209             System.out.println("clauses: collapsed=" + collapsed + ", tabs:" + tabs.getSelectedIndex() + "=" + tabs.indexOfComponent(entry.component));
    210             if( entry.notify && !collapsed && tabs.getSelectedIndex() == tabs.indexOfComponent(entry.component) )
    211                 entry.notify = false;
    212             setFont(entry.notify ? boldFont : normalFont);
     218            if( entry.notify > 0 && !collapsed && tabs.getSelectedIndex() == tabs.indexOfComponent(entry.component) )
     219                entry.notify = 0;
     220            setFont(entry.notify > 0 ? boldFont : normalFont);
    213221            panel.updateTitleAlarm();
    214222        }
     
    220228        public JTextPane pane;
    221229        public JScrollPane component;
    222         public boolean notify;
     230        public int notify;
    223231
    224232    }
  • applications/editors/josm/plugins/geochat/src/geochat/GeoChatPanel.java

    r29568 r29571  
    104104
    105105    private String autoCompleteUser( String word ) {
    106         return word; // todo: write autocomplete
     106        String result = null;
     107        for( String user : users.keySet() ) {
     108            if( user.startsWith(word) ) {
     109                if( result == null )
     110                    result = user;
     111                else {
     112                    int i = word.length();
     113                    while( i < result.length() && i < user.length() && result.charAt(i) == user.charAt(i) )
     114                        i++;
     115                    if( i < result.length() )
     116                        result = result.substring(0, i);
     117                }
     118            }
     119        }
     120        return result;
    107121    }
    108122
     
    245259            StringBuilder sb = new StringBuilder();
    246260            for( ChatMessage msg : messages ) {
    247                 formatMessage(sb, msg);
    248                 if( msg.isIncoming() ) {
    249                     // todo: alarm=2 for private messages
    250                     alarm = 1;
     261                boolean important = msg.isIncoming() && containsName(msg.getMessage());
     262                if( msg.isIncoming() && alarm < 2 ) {
     263                    alarm = important ? 2 : 1;
    251264                }
     265                if( important ) {
     266                    // add buffer, then add current line with italic, then clear buffer
     267                    chatPanes.addLineToPublic(sb.toString());
     268                    sb.setLength(0);
     269                    formatMessage(sb, msg);
     270                    chatPanes.addLineToPublicEm(sb.toString());
     271                    sb.setLength(0);
     272                } else
     273                    formatMessage(sb, msg);
    252274            }
    253275            chatPanes.addLineToPublic(sb.toString());
    254276            if( alarm > 0 )
    255                 chatPanes.notify(null, alarm > 1);
    256         }
     277                chatPanes.notify(null, alarm);
     278        }
     279    }
     280
     281    private boolean containsName( String message ) {
     282        String userName = connection.getUserName();
     283        int length = userName.length();
     284        int i = message.indexOf(userName);
     285        while( i >= 0 ) {
     286            if( (i == 0 || !Character.isJavaIdentifierPart(message.charAt(i - 1)))
     287                    && (i + length >= message.length() || !Character.isJavaIdentifierPart(message.charAt(i + length))) )
     288                return true;
     289            i = message.indexOf(userName, i + 1);
     290        }
     291        return false;
    257292    }
    258293
     
    265300            chatPanes.addLineToChatPane(msg.isIncoming() ? msg.getAuthor() : msg.getRecipient(), sb.toString());
    266301            if( msg.isIncoming() )
    267                 chatPanes.notify(msg.getAuthor(), true);
     302                chatPanes.notify(msg.getAuthor(), 2);
    268303        }
    269304    }
  • applications/editors/josm/plugins/geochat/src/geochat/JPanelTextField.java

    r29568 r29571  
    11package geochat;
    22
     3import java.awt.KeyboardFocusManager;
    34import java.awt.event.KeyEvent;
     5import java.util.*;
    46import javax.swing.JTextField;
     7import javax.swing.KeyStroke;
    58import org.openstreetmap.josm.Main;
    69
     
    1215 */
    1316public class JPanelTextField extends JTextField {
     17
     18    public JPanelTextField() {
     19        setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, new HashSet<KeyStroke>());
     20    }
    1421
    1522    @Override
     
    2431                }
    2532            } else if( code == KeyEvent.VK_TAB ) {
    26                 String word = ""; // todo: get the word
    27                 String complete = word == null ? null : autoComplete(word);
    28                 if( complete != null && !complete.equals(word) ) {
    29                     // todo: replace the word
     33                String text = getText();
     34                int caret = getCaretPosition();
     35                int start = caret - 1;
     36                while( start >= 0 && Character.isJavaIdentifierPart(text.charAt(start)) )
     37                    start--;
     38                start++;
     39                System.out.println("Autocomplete! Word " + start + "-" + caret + " (not inclusive)");
     40                if( start < caret ) {
     41                    String word = text.substring(start, caret);
     42                    String complete = word == null ? null : autoComplete(word);
     43                    if( complete != null && !complete.equals(word) ) {
     44                        StringBuilder sb = new StringBuilder();
     45                        if( start > 0 )
     46                            sb.append(text.substring(0, start));
     47                        sb.append(complete);
     48                        if( caret < text.length() )
     49                            sb.append(text.substring(caret));
     50                        setText(sb.toString());
     51                        setCaretPosition(start + complete.length());
     52                    }
    3053                }
    3154            } else if( code == KeyEvent.VK_ESCAPE ) {
Note: See TracChangeset for help on using the changeset viewer.