1 22 23 package org.gjt.sp.jedit.gui; 24 25 import bsh.NameSpace; 27 import java.awt.event.*; 28 import java.awt.*; 29 import java.util.ArrayList ; 30 import javax.swing.event.*; 31 import javax.swing.*; 32 import org.gjt.sp.jedit.*; 33 35 38 public class ActionBar extends JPanel 39 { 40 public ActionBar(View view, boolean temp) 42 { 43 setLayout(new BoxLayout(this,BoxLayout.X_AXIS)); 44 45 this.view = view; 46 this.temp = temp; 47 48 add(Box.createHorizontalStrut(2)); 49 50 JLabel label = new JLabel(jEdit.getProperty("view.action.prompt")); 51 add(label); 52 add(Box.createHorizontalStrut(12)); 53 add(action = new ActionTextField()); 54 action.setEnterAddsToHistory(false); 55 Dimension max = action.getPreferredSize(); 56 max.width = Integer.MAX_VALUE; 57 action.setMaximumSize(max); 58 action.addActionListener(new ActionHandler()); 59 action.getDocument().addDocumentListener(new DocumentHandler()); 60 61 if(temp) 62 { 63 close = new RolloverButton(GUIUtilities.loadIcon("closebox.gif")); 64 close.addActionListener(new ActionHandler()); 65 close.setToolTipText(jEdit.getProperty( 66 "view.action.close-tooltip")); 67 add(close); 68 } 69 70 this.temp = temp; 72 } 74 public HistoryTextField getField() 76 { 77 return action; 78 } 80 public void goToActionBar() 82 { 83 repeatCount = view.getInputHandler().getRepeatCount(); 84 action.setText(null); 85 action.requestFocus(); 86 } 88 90 private static NameSpace namespace = new NameSpace( 91 BeanShell.getNameSpace(),"action bar namespace"); 92 93 private View view; 95 private boolean temp; 96 private int repeatCount; 97 private HistoryTextField action; 98 private CompletionPopup popup; 99 private RolloverButton close; 100 102 private void invoke() 104 { 105 String cmd; 106 if(popup != null) 107 cmd = popup.list.getSelectedValue().toString(); 108 else 109 { 110 cmd = action.getText().trim(); 111 int index = cmd.indexOf('='); 112 if(index != -1) 113 { 114 action.addCurrentToHistory(); 115 String propName = cmd.substring(0,index).trim(); 116 String propValue = cmd.substring(index + 1).trim(); 117 String code; 118 121 if(propName.startsWith("buffer.")) 122 { 123 if(propName.equals("buffer.mode")) 124 { 125 code = "buffer.setMode(\"" 126 + MiscUtilities.charsToEscapes( 127 propValue) + "\");"; 128 } 129 else 130 { 131 code = "buffer.setStringProperty(\"" 132 + MiscUtilities.charsToEscapes( 133 propName.substring("buffer.".length()) 134 ) + "\",\"" 135 + MiscUtilities.charsToEscapes( 136 propValue) + "\");"; 137 } 138 139 code += "\nbuffer.propertiesChanged();"; 140 } 141 else if(propName.startsWith("!buffer.")) 142 { 143 code = "jEdit.setProperty(\"" 144 + MiscUtilities.charsToEscapes( 145 propName.substring(1)) + "\",\"" 146 + MiscUtilities.charsToEscapes( 147 propValue) + "\");\n" 148 + "jEdit.propertiesChanged();"; 149 } 150 else 151 { 152 code = "jEdit.setProperty(\"" 153 + MiscUtilities.charsToEscapes( 154 propName) + "\",\"" 155 + MiscUtilities.charsToEscapes( 156 propValue) + "\");\n" 157 + "jEdit.propertiesChanged();"; 158 } 159 160 Macros.Recorder recorder = view.getMacroRecorder(); 161 if(recorder != null) 162 recorder.record(code); 163 BeanShell.eval(view,namespace,code); 164 cmd = null; 165 } 166 else if(cmd.length() != 0) 167 { 168 String [] completions = getCompletions(cmd); 169 if(completions.length != 0) 170 { 171 cmd = completions[0]; 172 } 173 } 174 else 175 cmd = null; 176 } 177 178 if(popup != null) 179 { 180 popup.dispose(); 181 popup = null; 182 } 183 184 final String finalCmd = cmd; 185 final EditAction act = (finalCmd == null ? null : jEdit.getAction(finalCmd)); 186 if(temp) 187 view.removeToolBar(this); 188 189 SwingUtilities.invokeLater(new Runnable () 190 { 191 public void run() 192 { 193 view.getTextArea().requestFocus(); 194 if(act == null) 195 { 196 if(finalCmd != null) 197 { 198 view.getStatus().setMessageAndClear( 199 jEdit.getProperty( 200 "view.action.no-completions")); 201 } 202 } 203 else 204 { 205 view.getInputHandler().setRepeatCount(repeatCount); 206 view.getInputHandler().invokeAction(act); 207 } 208 } 209 }); 210 } 212 private String [] getCompletions(String str) 214 { 215 str = str.toLowerCase(); 216 String [] actions = jEdit.getActionNames(); 217 ArrayList <String > returnValue = new ArrayList <String >(actions.length); 218 for(int i = 0; i < actions.length; i++) 219 { 220 if(actions[i].toLowerCase().contains(str)) 221 returnValue.add(actions[i]); 222 } 223 224 return returnValue.toArray(new String [returnValue.size()]); 225 } 227 private void complete(boolean insertLongestPrefix) 229 { 230 String text = action.getText().trim(); 231 String [] completions = getCompletions(text); 232 if(completions.length == 1) 233 { 234 if(insertLongestPrefix) 235 action.setText(completions[0]); 236 } 237 else if(completions.length != 0) 238 { 239 if(insertLongestPrefix) 240 { 241 String prefix = MiscUtilities.getLongestPrefix( 242 completions,true); 243 if(prefix.contains(text)) 244 action.setText(prefix); 245 } 246 247 if(popup != null) 248 popup.setModel(completions); 249 else 250 popup = new CompletionPopup(completions); 251 return; 252 } 253 254 if(popup != null) 255 { 256 popup.dispose(); 257 popup = null; 258 } 259 } 261 263 265 class ActionHandler implements ActionListener 267 { 268 public void actionPerformed(ActionEvent evt) 269 { 270 if(evt.getSource() == close) 271 view.removeToolBar(ActionBar.this); 272 else 273 invoke(); 274 } 275 } 277 class DocumentHandler implements DocumentListener 279 { 280 public void insertUpdate(DocumentEvent evt) 282 { 283 if(popup != null) 284 complete(false); 285 } 287 public void removeUpdate(DocumentEvent evt) 289 { 290 if(popup != null) 291 complete(false); 292 } 294 public void changedUpdate(DocumentEvent evt) {} 296 } 299 class ActionTextField extends HistoryTextField 301 { 302 boolean repeat; 303 boolean nonDigit; 304 305 ActionTextField() 306 { 307 super("action"); 308 setSelectAllOnFocus(true); 309 } 310 311 public boolean isManagingFocus() 312 { 313 return false; 314 } 315 316 public boolean getFocusTraversalKeysEnabled() 317 { 318 return false; 319 } 320 321 public void processKeyEvent(KeyEvent evt) 322 { 323 evt = KeyEventWorkaround.processKeyEvent(evt); 324 if(evt == null) 325 return; 326 327 switch(evt.getID()) 328 { 329 case KeyEvent.KEY_TYPED: 330 char ch = evt.getKeyChar(); 331 if(!nonDigit && Character.isDigit(ch)) 332 { 333 super.processKeyEvent(evt); 334 repeat = true; 335 repeatCount = Integer.parseInt(action.getText()); 336 } 337 else 338 { 339 nonDigit = true; 340 if(repeat) 341 { 342 passToView(evt); 343 } 344 else 345 super.processKeyEvent(evt); 346 } 347 break; 348 case KeyEvent.KEY_PRESSED: 349 int keyCode = evt.getKeyCode(); 350 if(evt.isActionKey() 351 || evt.isControlDown() 352 || evt.isAltDown() 353 || evt.isMetaDown() 354 || keyCode == KeyEvent.VK_BACK_SPACE 355 || keyCode == KeyEvent.VK_DELETE 356 || keyCode == KeyEvent.VK_ENTER 357 || keyCode == KeyEvent.VK_TAB 358 || keyCode == KeyEvent.VK_ESCAPE) 359 { 360 nonDigit = true; 361 if(repeat) 362 { 363 passToView(evt); 364 break; 365 } 366 else if(keyCode == KeyEvent.VK_TAB) 367 { 368 complete(true); 369 evt.consume(); 370 } 371 else if(keyCode == KeyEvent.VK_ESCAPE) 372 { 373 evt.consume(); 374 if(popup != null) 375 { 376 popup.dispose(); 377 popup = null; 378 action.requestFocus(); 379 } 380 else 381 { 382 if(temp) 383 view.removeToolBar(ActionBar.this); 384 view.getEditPane().focusOnTextArea(); 385 } 386 break; 387 } 388 else if((keyCode == KeyEvent.VK_UP 389 || keyCode == KeyEvent.VK_DOWN) 390 && popup != null) 391 { 392 popup.list.processKeyEvent(evt); 393 break; 394 } 395 } 396 super.processKeyEvent(evt); 397 break; 398 } 399 } 400 401 private void passToView(final KeyEvent evt) 402 { 403 if(temp) 404 view.removeToolBar(ActionBar.this); 405 view.getTextArea().requestFocus(); 406 SwingUtilities.invokeLater(new Runnable () 407 { 408 public void run() 409 { 410 view.getTextArea().requestFocus(); 411 view.getInputHandler().setRepeatCount(repeatCount); 412 view.getInputHandler().processKeyEvent(evt, 413 View.ACTION_BAR, false); 414 } 415 }); 416 } 417 418 public void addNotify() 419 { 420 super.addNotify(); 421 repeat = nonDigit = false; 422 } 423 } 425 class CompletionPopup extends JWindow 427 { 428 CompletionList list; 429 430 CompletionPopup(String [] actions) 432 { 433 super(view); 434 435 setContentPane(new JPanel(new BorderLayout()) 436 { 437 441 public boolean isManagingFocus() 442 { 443 return false; 444 } 445 446 449 public boolean getFocusTraversalKeysEnabled() 450 { 451 return false; 452 } 453 }); 454 455 list = new CompletionList(actions); 456 list.setVisibleRowCount(8); 457 list.addMouseListener(new MouseHandler()); 458 list.setSelectedIndex(0); 459 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 460 461 JScrollPane scroller = new JScrollPane(list, 464 ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, 465 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 466 467 getContentPane().add(scroller, BorderLayout.CENTER); 468 469 GUIUtilities.requestFocus(this,list); 470 471 pack(); 472 Point p = new Point(0,-getHeight()); 473 SwingUtilities.convertPointToScreen(p,action); 474 setLocation(p); 475 setVisible(true); 476 477 KeyHandler keyHandler = new KeyHandler(); 478 addKeyListener(keyHandler); 479 list.addKeyListener(keyHandler); 480 } 482 void setModel(String [] actions) 484 { 485 list.setListData(actions); 486 list.setSelectedIndex(0); 487 } 489 class MouseHandler extends MouseAdapter 491 { 492 public void mouseClicked(MouseEvent evt) 493 { 494 invoke(); 495 } 496 } 498 class CompletionList extends JList 500 { 501 CompletionList(Object [] data) 502 { 503 super(data); 504 } 505 506 public void processKeyEvent(KeyEvent evt) 508 { 509 super.processKeyEvent(evt); 510 } 511 } 513 class KeyHandler extends KeyAdapter 515 { 516 public void keyTyped(KeyEvent evt) 517 { 518 action.processKeyEvent(evt); 519 } 520 521 public void keyPressed(KeyEvent evt) 522 { 523 int keyCode = evt.getKeyCode(); 524 if(keyCode == KeyEvent.VK_ESCAPE) 525 action.processKeyEvent(evt); 526 else if(keyCode == KeyEvent.VK_ENTER) 527 invoke(); 528 else if(keyCode == KeyEvent.VK_UP) 529 { 530 int selected = list.getSelectedIndex(); 531 if(selected == 0) 532 { 533 list.setSelectedIndex( 534 list.getModel().getSize() 535 - 1); 536 evt.consume(); 537 } 538 } 539 else if(keyCode == KeyEvent.VK_DOWN) 540 { 541 int selected = list.getSelectedIndex(); 542 if(selected == list.getModel().getSize() - 1) 543 { 544 list.setSelectedIndex(0); 545 evt.consume(); 546 } 547 } 548 } 549 } } 552 } 554 | Popular Tags |