Changeset 19877 for lang/java

Show
Ignore:
Timestamp:
09/25/08 09:38:01 (2 months ago)
Author:
mattn
Message:

added command history.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/java/misc/memcachedclient/src/net/kaoriya/mattn/memcached/jMemCachedClient.java

    r19822 r19877  
    1010import java.awt.event.ActionEvent; 
    1111import java.awt.event.ActionListener; 
     12import java.awt.event.KeyAdapter; 
     13import java.awt.event.KeyEvent; 
    1214import java.awt.event.WindowAdapter; 
    1315import java.awt.event.WindowEvent; 
    1416import java.io.IOException; 
    1517import java.net.InetSocketAddress; 
     18import java.util.ArrayList; 
    1619import java.util.HashMap; 
     20import java.util.List; 
    1721import java.util.Map; 
    1822import java.util.concurrent.Future; 
     
    3943        /** memcached client. */ 
    4044        private MemcachedClient mc; 
    41          
     45 
    4246        /** attribute set of JTextPane. */ 
    4347        private Map<String, MutableAttributeSet> attrs = new HashMap<String, MutableAttributeSet>(); 
     48 
     49        /** command history. */ 
     50        List<String> cmds = new ArrayList<String>(); 
     51 
     52        /** command history index. */ 
     53        int cmd_cur = -1; 
    4454 
    4555        /** 
     
    5868 
    5969                this.setLayout(new BorderLayout()); 
    60                  
     70 
    6171                GridBagConstraints gbc = new GridBagConstraints(); 
    6272                gbc.gridwidth = 1; 
    6373                gbc.gridheight = 10; 
    64                    
    65             JPanel panel = new JPanel(); 
    66             GridBagLayout layout = new GridBagLayout(); 
     74                gbc.weightx = 1.0d; 
     75 
     76                JPanel panel = new JPanel(); 
     77                GridBagLayout layout = new GridBagLayout(); 
    6778                panel.setLayout(layout); 
    68                  
     79 
    6980                final JTextPane textpane = new JTextPane(); 
    7081                textpane.setEditable(false); 
    7182                textpane.setMinimumSize(new Dimension(400, 400)); 
    7283                textpane.setBorder(new LineBorder(Color.GRAY)); 
    73                 gbc.weightx = 1.0d; 
    7484                gbc.weighty = 1.0d; 
    7585                gbc.insets = new Insets(5, 5, 5, 5); 
     
    8797                                try { 
    8898                                        String text = textfield.getText(); 
     99 
     100                                        if (cmd_cur >= 0 && text.equals(cmds.get(cmd_cur))) { 
     101                                                cmds.remove(cmds.size() - 1); 
     102                                        } 
     103 
     104                                        cmds.add(0, text); 
     105                                        cmd_cur = -1; 
     106 
    89107                                        Document doc = textpane.getDocument();  
    90108                                        doc.insertString( 
    91                                                         doc.getLength(), 
    92                                                         text + "\n", 
    93                                                         attrs.get("command")); 
    94                                          
     109                                                doc.getLength(), 
     110                                                text + "\n", 
     111                                                attrs.get("command")); 
     112 
    95113                                        Matcher matcher = Pattern.compile("^(get|set|delete)\\s+(\\S+)((\\s+)(\\S+))?").matcher(text); 
    96114                                        if (matcher.matches()) { 
     
    109127                                                        } 
    110128                                                } else 
    111                                                 if (matcher.group(1).toLowerCase().equals("set") && matcher.start(5) > 0) { 
    112                                                         Future ret = mc.set(matcher.group(2), 0, matcher.group(5)); 
    113                                                         if (!ret.isCancelled()) { 
    114                                                                 doc.insertString( 
    115                                                                                 doc.getLength(), 
    116                                                                                 "Ok.\n", 
    117                                                                                 attrs.get("data")); 
     129                                                        if (matcher.group(1).toLowerCase().equals("set") && matcher.start(5) > 0) { 
     130                                                                Future ret = mc.set(matcher.group(2), 0, matcher.group(5)); 
     131                                                                if (!ret.isCancelled()) { 
     132                                                                        doc.insertString( 
     133                                                                                        doc.getLength(), 
     134                                                                                        "Ok.\n", 
     135                                                                                        attrs.get("data")); 
     136                                                                } else { 
     137                                                                        doc.insertString( 
     138                                                                                        doc.getLength(), 
     139                                                                                        "Not found.\n", 
     140                                                                                        attrs.get("error")); 
     141                                                                } 
     142                                                        } else 
     143                                                        if (matcher.group(1).toLowerCase().equals("delete") && matcher.start(2) > 0) { 
     144                                                                Object val = mc.delete(matcher.group(2)); 
     145                                                                if (val != null) { 
     146                                                                        doc.insertString( 
     147                                                                                        doc.getLength(), 
     148                                                                                        val.toString() + "\n", 
     149                                                                                        attrs.get("data")); 
     150                                                                } else { 
     151                                                                        doc.insertString( 
     152                                                                                        doc.getLength(), 
     153                                                                                        "Not found.\n", 
     154                                                                                        attrs.get("error")); 
     155                                                                } 
    118156                                                        } else { 
    119157                                                                doc.insertString( 
    120158                                                                                doc.getLength(), 
    121                                                                                 "Not found.\n", 
     159                                                                                "Unknown command '" + text + "'.\n", 
    122160                                                                                attrs.get("error")); 
    123161                                                        } 
    124                                                 } else 
    125                                                 if (matcher.group(1).toLowerCase().equals("delete") && matcher.start(2) > 0) { 
    126                                                         Object val = mc.delete(matcher.group(2)); 
    127                                                         if (val != null) { 
    128                                                                 doc.insertString( 
    129                                                                                 doc.getLength(), 
    130                                                                                 val.toString() + "\n", 
    131                                                                                 attrs.get("data")); 
    132                                                         } else { 
    133                                                                 doc.insertString( 
    134                                                                                 doc.getLength(), 
    135                                                                                 "Not found.\n", 
    136                                                                                 attrs.get("error")); 
    137                                                         } 
    138                                                 } else { 
    139                                                         doc.insertString( 
    140                                                                         doc.getLength(), 
    141                                                                         "Unknown command '" + text + "'.\n", 
    142                                                                         attrs.get("error")); 
    143                                                 } 
    144162                                        } else { 
    145163                                                doc.insertString( 
     
    154172                        } 
    155173                }); 
     174                textfield.addKeyListener(new KeyAdapter() { 
     175                        public void keyReleased(KeyEvent ev) { 
     176                                if (ev.getKeyCode() == KeyEvent.VK_UP) { 
     177                                        if (cmd_cur < cmds.size() - 1) { 
     178                                                cmd_cur++; 
     179                                        } 
     180                                        if (cmds.get(cmd_cur).length() > 0) { 
     181                                                textfield.setText(cmds.get(cmd_cur)); 
     182                                        } 
     183                                } else 
     184                                if (ev.getKeyCode() == KeyEvent.VK_DOWN) { 
     185                                        if (cmd_cur >= 0) { 
     186                                                cmd_cur--; 
     187                                        } 
     188                                        if (cmd_cur >= 0 && cmds.get(cmd_cur).length() > 0) { 
     189                                                textfield.setText(cmds.get(cmd_cur)); 
     190                                        } else { 
     191                                                textfield.setText(""); 
     192                                        } 
     193                                } 
     194                        } 
     195                }); 
    156196                panel.add(textfield); 
    157197 
     
    164204                }); 
    165205        } 
    166          
     206 
    167207        /** 
    168208         * main entry.