Changeset 19877
- Timestamp:
- 09/25/08 09:38:01 (5 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
lang/java/misc/memcachedclient/src/net/kaoriya/mattn/memcached/jMemCachedClient.java
r19822 r19877 10 10 import java.awt.event.ActionEvent; 11 11 import java.awt.event.ActionListener; 12 import java.awt.event.KeyAdapter; 13 import java.awt.event.KeyEvent; 12 14 import java.awt.event.WindowAdapter; 13 15 import java.awt.event.WindowEvent; 14 16 import java.io.IOException; 15 17 import java.net.InetSocketAddress; 18 import java.util.ArrayList; 16 19 import java.util.HashMap; 20 import java.util.List; 17 21 import java.util.Map; 18 22 import java.util.concurrent.Future; … … 39 43 /** memcached client. */ 40 44 private MemcachedClient mc; 41 45 42 46 /** attribute set of JTextPane. */ 43 47 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; 44 54 45 55 /** … … 58 68 59 69 this.setLayout(new BorderLayout()); 60 70 61 71 GridBagConstraints gbc = new GridBagConstraints(); 62 72 gbc.gridwidth = 1; 63 73 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(); 67 78 panel.setLayout(layout); 68 79 69 80 final JTextPane textpane = new JTextPane(); 70 81 textpane.setEditable(false); 71 82 textpane.setMinimumSize(new Dimension(400, 400)); 72 83 textpane.setBorder(new LineBorder(Color.GRAY)); 73 gbc.weightx = 1.0d;74 84 gbc.weighty = 1.0d; 75 85 gbc.insets = new Insets(5, 5, 5, 5); … … 87 97 try { 88 98 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 89 107 Document doc = textpane.getDocument(); 90 108 doc.insertString( 91 doc.getLength(),92 text + "\n",93 attrs.get("command"));94 109 doc.getLength(), 110 text + "\n", 111 attrs.get("command")); 112 95 113 Matcher matcher = Pattern.compile("^(get|set|delete)\\s+(\\S+)((\\s+)(\\S+))?").matcher(text); 96 114 if (matcher.matches()) { … … 109 127 } 110 128 } 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 } 118 156 } else { 119 157 doc.insertString( 120 158 doc.getLength(), 121 " Not found.\n",159 "Unknown command '" + text + "'.\n", 122 160 attrs.get("error")); 123 161 } 124 } else125 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 }144 162 } else { 145 163 doc.insertString( … … 154 172 } 155 173 }); 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 }); 156 196 panel.add(textfield); 157 197 … … 164 204 }); 165 205 } 166 206 167 207 /** 168 208 * main entry.
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)