Changeset 29582 in osm


Ignore:
Timestamp:
2013-05-11T20:59:05+02:00 (11 years ago)
Author:
zverik
Message:

[josm_geochat] font size, autologin, copy-paste, users layer

Location:
applications/editors/josm
Files:
5 edited

Legend:

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

    r29576 r29582  
    133133        chatPane.setEditable(false);
    134134        Font font = chatPane.getFont();
    135         chatPane.setFont(font.deriveFont(font.getSize2D() - 2));
     135        float size = -1.0f; // Main.pref.getInteger("geochat.fontsize", -1); <- we don't need this
     136        if( size < 8 )
     137            size += font.getSize2D();
     138        chatPane.setFont(font.deriveFont(size));
    136139//        DefaultCaret caret = (DefaultCaret)chatPane.getCaret(); // does not work
    137140//        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
  • applications/editors/josm/plugins/geochat/src/geochat/ChatServerConnection.java

    r29576 r29582  
    6565     */
    6666    public void checkLogin() {
     67        autoLogin(null);
     68    }
     69
     70    /**
     71     * Test that userId is still active, if not, tries to login with given user name.
     72     * Does not autologin, if userName is null, obviously.
     73     */
     74    public void autoLogin( final String userName ) {
    6775        final int uid = Main.pref.getInteger("geochat.lastuid", 0);
    68         if( uid <= 0 ) return;
    69         String query = "whoami&uid=" + uid;
    70         JsonQueryUtil.queryAsync(query, new JsonQueryCallback() {
    71             public void processJson( JSONObject json ) {
    72                 if( json != null && json.has("name") )
    73                     login(uid, json.getString("name"));
    74             }
    75         });
     76        if( uid <= 0 ) {
     77            if( userName != null && userName.length() > 1 )
     78                login(userName);
     79        } else {
     80            String query = "whoami&uid=" + uid;
     81            JsonQueryUtil.queryAsync(query, new JsonQueryCallback() {
     82                public void processJson( JSONObject json ) {
     83                    if( json != null && json.has("name") )
     84                        login(uid, json.getString("name"));
     85                    else if( userName != null && userName.length() > 1 )
     86                        login(userName);
     87                }
     88            });
     89        }
     90    }
     91
     92    /**
     93     * Waits until {@link #getPosition()} is not null, then calls {@link #autoLogin(java.lang.String)}.
     94     * If two seconds have passed, stops the waiting. Doesn't wait if userName is empty.
     95     */
     96    public void autoLoginWithDelay( final String userName ) {
     97        if( userName == null || userName.length() == 0 ) {
     98            checkLogin();
     99            return;
     100        }
     101        new Thread(new Runnable() {
     102            public void run() {
     103                try {
     104                    int cnt = 10;
     105                    while( getPosition() == null && cnt-- > 0 )
     106                        Thread.sleep(200);
     107                } catch( InterruptedException e ) {}
     108                autoLogin(userName);
     109            }
     110        }).start();
    76111    }
    77112
  • applications/editors/josm/plugins/geochat/src/geochat/GeoChatPanel.java

    r29581 r29582  
    33import java.awt.*;
    44import java.awt.event.*;
    5 import java.awt.geom.Rectangle2D;
    65import java.text.SimpleDateFormat;
    76import java.util.*;
     
    5756        };
    5857
    59         loginPanel = createLoginPanel();
     58        String defaultUserName = constructUserName();
     59        loginPanel = createLoginPanel(defaultUserName);
    6060
    6161        gcPanel = new JPanel(new BorderLayout());
     
    6767        connection = ChatServerConnection.getInstance();
    6868        connection.addListener(this);
    69         connection.checkLogin();
    70         updateTitleAlarm();
    71     }
    72 
    73     private JPanel createLoginPanel() {
    74         final JTextField nameField = new JPanelTextField() {
    75             @Override
    76             protected void processEnter( String text ) {
    77                 connection.login(text);
    78             }
    79         };
    80         String userName = Main.pref.get("geochat.username", JosmUserIdentityManager.getInstance().getUserName());
     69        boolean autoLogin = Main.pref.get("geochat.username", null) == null ? false : Main.pref.getBoolean("geochat.autologin", true);
     70        connection.autoLoginWithDelay(autoLogin ? defaultUserName : null);
     71        updateTitleAlarm();
     72    }
     73
     74    private String constructUserName() {
     75        String userName = Main.pref.get("geochat.username", null); // so the default is null
     76        if( userName == null )
     77            userName = JosmUserIdentityManager.getInstance().getUserName();
    8178        if( userName == null )
    8279            userName = "";
     
    8481            userName = userName.substring(0, userName.indexOf('@'));
    8582        userName = userName.replace(' ', '_');
    86         nameField.setText(userName);
     83        return userName;
     84    }
     85
     86    private JPanel createLoginPanel( String defaultUserName ) {
     87        final JTextField nameField = new JPanelTextField() {
     88            @Override
     89            protected void processEnter( String text ) {
     90                connection.login(text);
     91            }
     92        };
     93        nameField.setText(defaultUserName);
    8794
    8895        JButton loginButton = new JButton(tr("Login"));
     
    94101        nameField.setPreferredSize(new Dimension(nameField.getPreferredSize().width, loginButton.getPreferredSize().height));
    95102
     103        final JCheckBox autoLoginBox = new JCheckBox(tr("Enable autologin"), Main.pref.getBoolean("geochat.autologin", true));
     104        autoLoginBox.addActionListener(new ActionListener() {
     105            public void actionPerformed( ActionEvent e ) {
     106                Main.pref.put("geochat.autologin", autoLoginBox.isSelected());
     107            }
     108        });
     109
    96110        JPanel panel = new JPanel(new GridBagLayout());
    97111        panel.add(nameField, GBC.std().fill(GridBagConstraints.HORIZONTAL).insets(15, 0, 5, 0));
    98         panel.add(loginButton, GBC.std().fill(GridBagConstraints.NONE).insets(0, 0, 15, 0));
     112        panel.add(loginButton, GBC.eol().fill(GridBagConstraints.NONE).insets(0, 0, 15, 0));
     113        panel.add(autoLoginBox, GBC.std().insets(15, 0, 15, 0));
    99114        return panel;
    100115    }
     
    143158    public void paint( Graphics2D g, MapView mv, Bounds bbox ) {
    144159        Graphics2D g2d = (Graphics2D)g.create();
    145         g2d.setColor(Color.yellow);
    146         g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
    147 
    148         int zoom = ChatServerConnection.getCurrentZoom();
    149         int radius = Math.max(zoom, 1) * 10;
    150         if( zoom < 14 )
    151             radius /= 2;
    152 
    153         Font font = g2d.getFont().deriveFont(Font.BOLD, Math.max(zoom * 2, 8));
     160        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     161        Composite ac04 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f);
     162        Composite ac10 = g2d.getComposite();
     163
     164        Font font = g2d.getFont().deriveFont(Font.BOLD, g2d.getFont().getSize2D() + 2.0f);
    154165        g2d.setFont(font);
    155166        FontMetrics fm = g2d.getFontMetrics();
    156167
    157168        for( String user : users.keySet() ) {
     169            int stringWidth = fm.stringWidth(user);
     170            int radius = stringWidth / 2 + 10;
    158171            Point p = mv.getPoint(users.get(user));
    159             g2d.setColor(Color.yellow);
     172
     173            g2d.setComposite(ac04);
     174            g2d.setColor(Color.white);
    160175            g2d.fillOval(p.x - radius, p.y - radius, radius * 2 + 1, radius * 2 + 1);
    161176
     177            g2d.setComposite(ac10);
    162178            g2d.setColor(Color.black);
    163             Rectangle2D rect = fm.getStringBounds(user, g2d);
    164             g2d.drawString(user, p.x - Math.round(rect.getWidth() / 2), p.y);
     179            g2d.drawString(user, p.x - stringWidth / 2, p.y + fm.getDescent());
    165180        }
    166181    }
  • applications/editors/josm/plugins/geochat/src/geochat/JPanelTextField.java

    r29576 r29582  
    44import java.awt.event.KeyEvent;
    55import java.util.*;
    6 import javax.swing.JTextField;
     6import org.openstreetmap.josm.gui.widgets.JosmTextField;
    77import javax.swing.KeyStroke;
    88import org.openstreetmap.josm.Main;
     
    1414 * @author zverik
    1515 */
    16 public class JPanelTextField extends JTextField {
     16public class JPanelTextField extends JosmTextField {
    1717
    1818    public JPanelTextField() {
Note: See TracChangeset for help on using the changeset viewer.