密猟オンラインクライアントプログラム JAVAベース
入力ボックスに履歴機能を追加
それに伴い、送信作業後に入力ボックスの内容を消去するようにした
入力ボックスでESCを推すと入力を初期化してメインビューへフォーカスを移動するようにした
| @@ -2,15 +2,21 @@ | ||
| 2 | 2 | |
| 3 | 3 | import java.awt.*; |
| 4 | 4 | import java.awt.event.*; |
| 5 | +import java.util.*; | |
| 5 | 6 | |
| 6 | 7 | public class HuntMessageView extends Frame implements ActionListener { |
| 7 | 8 | |
| 9 | + static public final int HISTORY_MAX = 100; | |
| 8 | 10 | TextArea messageList; |
| 9 | 11 | TextField editWnd; |
| 12 | + ArrayList<String> historyText; | |
| 13 | + int historyPos; | |
| 10 | 14 | |
| 11 | 15 | @SuppressWarnings("LeakingThisInConstructor") |
| 12 | 16 | HuntMessageView() { |
| 13 | 17 | super(Hunt.getBundle().getString("TITLE_MESSAGE")); |
| 18 | + historyText = new ArrayList<>(); | |
| 19 | + historyPos = -1; | |
| 14 | 20 | messageList = new TextArea(10, 80); |
| 15 | 21 | messageList.setEditable(false); |
| 16 | 22 | messageList.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14)); |
| @@ -19,6 +25,7 @@ | ||
| 19 | 25 | editWnd.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14)); |
| 20 | 26 | add(editWnd, BorderLayout.SOUTH); |
| 21 | 27 | editWnd.addActionListener(this); |
| 28 | + editWnd.addKeyListener(new HuntMessageViewKeyEvent(this)); | |
| 22 | 29 | setResizable(true); |
| 23 | 30 | pack(); |
| 24 | 31 | } |
| @@ -45,6 +52,34 @@ | ||
| 45 | 52 | editWnd.setText(s); |
| 46 | 53 | } |
| 47 | 54 | |
| 55 | + public String getNextHistory() { | |
| 56 | + String wkstr; | |
| 57 | + if (historyText.isEmpty()) { | |
| 58 | + wkstr = ""; | |
| 59 | + } else { | |
| 60 | + historyPos++; | |
| 61 | + if (historyPos == historyText.size()) { | |
| 62 | + historyPos = 0; | |
| 63 | + } | |
| 64 | + wkstr = historyText.get(historyPos); | |
| 65 | + } | |
| 66 | + return wkstr; | |
| 67 | + } | |
| 68 | + | |
| 69 | + public String getPrevHistory() { | |
| 70 | + String wkstr; | |
| 71 | + if (historyText.isEmpty()) { | |
| 72 | + wkstr = ""; | |
| 73 | + } else { | |
| 74 | + historyPos--; | |
| 75 | + if (historyPos < 0) { | |
| 76 | + historyPos = historyText.size() - 1; | |
| 77 | + } | |
| 78 | + wkstr = historyText.get(historyPos); | |
| 79 | + } | |
| 80 | + return wkstr; | |
| 81 | + } | |
| 82 | + | |
| 48 | 83 | @Override |
| 49 | 84 | public void actionPerformed(ActionEvent e) { |
| 50 | 85 | /* send message or command */ |
| @@ -52,7 +87,47 @@ | ||
| 52 | 87 | String mesg = hedit.getText(); |
| 53 | 88 | hedit.selectAll(); |
| 54 | 89 | Hunt.send_message(mesg); |
| 90 | + if (historyText.size() >= HuntMessageView.HISTORY_MAX) { | |
| 91 | + //need to clear most old string | |
| 92 | + historyText.remove(0); | |
| 93 | + } | |
| 94 | + historyText.add(mesg); | |
| 95 | + historyPos = -1; | |
| 55 | 96 | } |
| 56 | 97 | } |
| 57 | 98 | |
| 99 | +class HuntMessageViewKeyEvent extends KeyAdapter { | |
| 100 | + | |
| 101 | + HuntMessageView hview; | |
| 102 | + | |
| 103 | + public HuntMessageViewKeyEvent(HuntMessageView v) { | |
| 104 | + hview = v; | |
| 105 | + } | |
| 106 | + | |
| 107 | + @Override | |
| 108 | + public void keyPressed(KeyEvent e) { | |
| 109 | + String wkstr; | |
| 110 | + switch (e.getKeyCode()) { | |
| 111 | + case KeyEvent.VK_UP: | |
| 112 | + case KeyEvent.VK_PAGE_UP: | |
| 113 | + wkstr = hview.getPrevHistory(); | |
| 114 | + hview.setEditText(wkstr); | |
| 115 | + break; | |
| 116 | + case KeyEvent.VK_DOWN: | |
| 117 | + case KeyEvent.VK_PAGE_DOWN: | |
| 118 | + wkstr = hview.getNextHistory(); | |
| 119 | + hview.setEditText(wkstr); | |
| 120 | + break; | |
| 121 | + case KeyEvent.VK_ESCAPE: | |
| 122 | + hview.setEditText(""); | |
| 123 | + Hunt.getMainWnd().toFront(); | |
| 124 | + break; | |
| 125 | + default: | |
| 126 | + super.keyPressed(e); | |
| 127 | + break; | |
| 128 | + } | |
| 129 | + } | |
| 130 | + | |
| 131 | +} | |
| 132 | + | |
| 58 | 133 | /* EOF */ |
| @@ -245,6 +245,7 @@ | ||
| 245 | 245 | case 'S': |
| 246 | 246 | /* just say */ |
| 247 | 247 | |
| 248 | + bneed_clearedit = true; | |
| 248 | 249 | sayCommand(s); |
| 249 | 250 | break; |
| 250 | 251 |
| @@ -252,6 +253,7 @@ | ||
| 252 | 253 | case 'B': |
| 253 | 254 | /* broadcast */ |
| 254 | 255 | |
| 256 | + bneed_clearedit = true; | |
| 255 | 257 | broadcastCommand(s); |
| 256 | 258 | break; |
| 257 | 259 |
| @@ -259,6 +261,7 @@ | ||
| 259 | 261 | case 'T': |
| 260 | 262 | /* tell anyone */ |
| 261 | 263 | |
| 264 | + bneed_clearedit = true; | |
| 262 | 265 | tellCommand(s); |
| 263 | 266 | break; |
| 264 | 267 |
| @@ -268,6 +271,7 @@ | ||
| 268 | 271 | default: |
| 269 | 272 | /* perhaps say... */ |
| 270 | 273 | |
| 274 | + bneed_clearedit = true; | |
| 271 | 275 | doSendMessage(s); |
| 272 | 276 | break; |
| 273 | 277 | } |