1 36 37 package com.sun.demo.scripting.jconsole; 38 39 import java.awt.*; 40 import java.awt.event.*; 41 import java.util.concurrent.ExecutorService ; 42 import java.util.concurrent.Executors ; 43 import javax.swing.*; 44 import javax.swing.event.*; 45 import javax.swing.text.*; 46 47 48 52 class ScriptShellPanel extends JPanel { 53 54 interface CommandProcessor { 56 public String executeCommand(String cmd); 58 public String getPrompt(); 60 } 61 62 private CommandProcessor commandProcessor; 64 private JTextComponent editor; 66 67 private final ExecutorService commandExecutor = 68 Executors.newSingleThreadExecutor(); 69 70 private boolean updating; 72 73 public ScriptShellPanel(CommandProcessor cmdProc) { 74 setLayout(new BorderLayout()); 75 this.commandProcessor = cmdProc; 76 this.editor = new JTextArea(); 77 editor.setDocument(new EditableAtEndDocument()); 78 JScrollPane scroller = new JScrollPane(); 79 scroller.getViewport().add(editor); 80 add(scroller, BorderLayout.CENTER); 81 82 editor.getDocument().addDocumentListener(new DocumentListener() { 83 public void changedUpdate(DocumentEvent e) { 84 } 85 86 public void insertUpdate(DocumentEvent e) { 87 if (updating) return; 88 beginUpdate(); 89 editor.setCaretPosition(editor.getDocument().getLength()); 90 if (insertContains(e, '\n')) { 91 String cmd = getMarkedText(); 92 if ((cmd.length() == 0) || 94 (cmd.charAt(cmd.length() - 1) != '\\')) { 95 final String cmd1 = trimContinuations(cmd); 97 commandExecutor.execute(new Runnable () { 98 public void run() { 99 final String result = executeCommand(cmd1); 100 101 SwingUtilities.invokeLater(new Runnable () { 102 public void run() { 103 if (result != null) { 104 print(result + "\n"); 105 } 106 printPrompt(); 107 setMark(); 108 endUpdate(); 109 } 110 }); 111 } 112 }); 113 } else { 114 endUpdate(); 115 } 116 } else { 117 endUpdate(); 118 } 119 } 120 121 public void removeUpdate(DocumentEvent e) { 122 } 123 }); 124 125 editor.addCaretListener(new CaretListener() { 129 public void caretUpdate(CaretEvent e) { 130 int len = editor.getDocument().getLength(); 131 if (e.getDot() > len) { 132 editor.setCaretPosition(len); 133 } 134 } 135 }); 136 137 Box hbox = Box.createHorizontalBox(); 138 hbox.add(Box.createGlue()); 139 JButton button = new JButton("Clear"); button.addActionListener(new ActionListener() { 141 public void actionPerformed(ActionEvent e) { 142 clear(); 143 } 144 }); 145 hbox.add(button); 146 hbox.add(Box.createGlue()); 147 add(hbox, BorderLayout.SOUTH); 148 149 clear(); 150 } 151 152 public void dispose() { 153 commandExecutor.shutdown(); 154 } 155 156 public void requestFocus() { 157 editor.requestFocus(); 158 } 159 160 public void clear() { 161 clear(true); 162 } 163 164 public void clear(boolean prompt) { 165 EditableAtEndDocument d = (EditableAtEndDocument) editor.getDocument(); 166 d.clear(); 167 if (prompt) printPrompt(); 168 setMark(); 169 editor.requestFocus(); 170 } 171 172 public void setMark() { 173 ((EditableAtEndDocument) editor.getDocument()).setMark(); 174 } 175 176 public String getMarkedText() { 177 try { 178 String s = ((EditableAtEndDocument) editor.getDocument()).getMarkedText(); 179 int i = s.length(); 180 while ((i > 0) && (s.charAt(i - 1) == '\n')) { 181 i--; 182 } 183 return s.substring(0, i); 184 } catch (BadLocationException e) { 185 e.printStackTrace(); 186 return null; 187 } 188 } 189 190 public void print(String s) { 191 Document d = editor.getDocument(); 192 try { 193 d.insertString(d.getLength(), s, null); 194 } catch (BadLocationException e) { 195 e.printStackTrace(); 196 } 197 } 198 199 200 204 private String executeCommand(String cmd) { 205 return commandProcessor.executeCommand(cmd); 206 } 207 208 private String getPrompt() { 209 return commandProcessor.getPrompt(); 210 } 211 212 private void beginUpdate() { 213 editor.setEditable(false); 214 updating = true; 215 } 216 217 private void endUpdate() { 218 editor.setEditable(true); 219 updating = false; 220 } 221 222 private void printPrompt() { 223 print(getPrompt()); 224 } 225 226 private boolean insertContains(DocumentEvent e, char c) { 227 String s = null; 228 try { 229 s = editor.getText(e.getOffset(), e.getLength()); 230 for (int i = 0; i < e.getLength(); i++) { 231 if (s.charAt(i) == c) { 232 return true; 233 } 234 } 235 } catch (BadLocationException ex) { 236 ex.printStackTrace(); 237 } 238 return false; 239 } 240 241 private String trimContinuations(String text) { 242 int i; 243 while ((i = text.indexOf("\\\n")) >= 0) { 244 text = text.substring(0, i) + text.substring(i+1, text.length()); 245 } 246 return text; 247 } 248 } 249 | Popular Tags |