2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 import org.jmol.api.*;
24 import java.awt.event.*;
26 import javax.swing.text.*;
27 import java.util.Vector;
29 import org.jmol.i18n.GT;
30 import org.jmol.util.Logger;
31 import org.jmol.util.CommandHistory;
33 public final class ScriptWindow extends JPanel implements ActionListener,
37 private ConsoleTextPane console;
39 private JButton closeButton;
41 private JButton runButton;
43 private JButton haltButton;
45 private JButton clearButton;
47 private JButton historyButton;
49 private JButton stateButton;
55 public ScriptWindow(AppJmol appJmol)
57 this.viewer = appJmol.viewer;
58 this.appJmol = appJmol;
60 setLayout(new BorderLayout());
62 console = new ConsoleTextPane(this);
65 add(new JScrollPane(console), BorderLayout.CENTER);
67 JPanel buttonPanel = new JPanel();
68 add(buttonPanel, BorderLayout.SOUTH);
70 runButton = new JButton(GT._("Run"));
71 haltButton = new JButton(GT._("Halt"));
72 runButton.addActionListener(this);
73 // buttonPanel.add(runButton);
74 haltButton.addActionListener(this);
75 // buttonPanel.add(haltButton);
76 haltButton.setEnabled(false);
78 clearButton = new JButton(GT._("Clear"));
79 clearButton.addActionListener(this);
80 buttonPanel.add(clearButton);
82 historyButton = new JButton(GT._("History"));
83 historyButton.addActionListener(this);
84 buttonPanel.add(historyButton);
86 stateButton = new JButton(GT._("State"));
87 stateButton.addActionListener(this);
88 buttonPanel.add(stateButton);
90 closeButton = new JButton(GT._("Close"));
91 closeButton.addActionListener(this);
92 buttonPanel.add(closeButton);
94 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
96 // ((JButton)buttonPanel.getComponent(i))
97 // .setMargin(new Insets(0, 0, 0, 0));
102 public void sendConsoleEcho(String strEcho)
104 if (strEcho != null && !isError)
107 console.outputEcho(strEcho);
113 boolean isError = false;
115 void setError(boolean TF)
119 // console.recallCommand(true);
122 public void sendConsoleMessage(String strStatus)
124 if (strStatus == null)
126 console.clearContent();
127 console.outputStatus("");
129 else if (strStatus.indexOf("ERROR:") >= 0)
131 console.outputError(strStatus);
136 console.outputStatus(strStatus);
140 public void notifyScriptTermination(String strMsg, int msWalltime)
142 if (strMsg != null && strMsg.indexOf("ERROR") >= 0)
144 console.outputError(strMsg);
146 runButton.setEnabled(true);
147 haltButton.setEnabled(false);
150 public void enterPressed()
152 runButton.doClick(100);
156 class ExecuteCommandThread extends Thread
161 ExecuteCommandThread(String command)
163 strCommand = command;
170 executeCommand(strCommand);
171 } catch (Exception ie)
173 Logger.debug("execution command interrupted!" + ie);
178 ExecuteCommandThread execThread;
180 void executeCommandAsThread()
182 String strCommand = console.getCommandString().trim();
183 if (strCommand.length() > 0)
185 execThread = new ExecuteCommandThread(strCommand);
190 void executeCommand(String strCommand)
194 console.appendNewline();
196 if (strCommand.length() > 0)
198 String strErrorMessage = null;
199 doWait = (strCommand.indexOf("WAIT ") == 0);
201 { // for testing, mainly
202 // demonstrates using the statusManager system.
203 runButton.setEnabled(false);
204 haltButton.setEnabled(true);
206 Vector info = (Vector) viewer
207 .scriptWaitStatus(strCommand.substring(5),
208 "+fileLoaded,+scriptStarted,+scriptStatus,+scriptEcho,+scriptTerminated");
209 runButton.setEnabled(true);
210 haltButton.setEnabled(false);
212 * info = [ statusRecortSet0, statusRecortSet1, statusRecortSet2, ...]
213 * statusRecordSet = [ statusRecord0, statusRecord1, statusRecord2, ...]
214 * statusRecord = [int msgPtr, String statusName, int intInfo, String
217 for (int i = 0; i < info.size(); i++)
219 Vector statusRecordSet = (Vector) info.get(i);
220 for (int j = 0; j < statusRecordSet.size(); j++)
222 Vector statusRecord = (Vector) statusRecordSet.get(j);
223 Logger.info("msg#=" + statusRecord.get(0) + " "
224 + statusRecord.get(1) + " intInfo="
225 + statusRecord.get(2) + " stringInfo="
226 + statusRecord.get(3));
229 console.appendNewline();
233 boolean isScriptExecuting = viewer.isScriptExecuting();
234 if (viewer.checkHalt(strCommand))
235 strErrorMessage = (isScriptExecuting ? "string execution halted with "
237 : "no script was executing");
239 strErrorMessage = "";// viewer.scriptCheck(strCommand);
240 // the problem is that scriptCheck is synchronized, so these might get
242 if (strErrorMessage != null && strErrorMessage.length() > 0)
244 console.outputError(strErrorMessage);
248 // runButton.setEnabled(false);
249 haltButton.setEnabled(true);
250 viewer.script(strCommand);
257 public void actionPerformed(ActionEvent e)
259 Object source = e.getSource();
260 if (source == closeButton)
262 appJmol.showConsole(false);
264 else if (source == runButton)
266 executeCommandAsThread();
268 else if (source == clearButton)
270 console.clearContent();
272 else if (source == historyButton)
274 console.clearContent(viewer.getSetHistory(Integer.MAX_VALUE));
276 else if (source == stateButton)
278 console.clearContent(viewer.getStateInfo());
280 else if (source == haltButton)
282 viewer.haltScriptExecution();
284 console.grabFocus(); // always grab the focus (e.g., after clear)
288 class ConsoleTextPane extends JTextPane
291 ConsoleDocument consoleDoc;
293 EnterListener enterListener;
297 ConsoleTextPane(ScriptWindow scriptWindow)
299 super(new ConsoleDocument());
300 consoleDoc = (ConsoleDocument) getDocument();
301 consoleDoc.setConsoleTextPane(this);
302 this.enterListener = (EnterListener) scriptWindow;
303 this.viewer = scriptWindow.viewer;
306 public String getCommandString()
308 String cmd = consoleDoc.getCommandString();
312 public void setPrompt()
314 consoleDoc.setPrompt();
317 public void appendNewline()
319 consoleDoc.appendNewline();
322 public void outputError(String strError)
324 consoleDoc.outputError(strError);
327 public void outputErrorForeground(String strError)
329 consoleDoc.outputErrorForeground(strError);
332 public void outputEcho(String strEcho)
334 consoleDoc.outputEcho(strEcho);
337 public void outputStatus(String strStatus)
339 consoleDoc.outputStatus(strStatus);
342 public void enterPressed()
344 if (enterListener != null)
345 enterListener.enterPressed();
348 public void clearContent()
353 public void clearContent(String text)
355 consoleDoc.clearContent();
357 consoleDoc.outputEcho(text);
364 * @see java.awt.Component#processKeyEvent(java.awt.event.KeyEvent)
368 * Custom key event processing for command 0 implementation.
370 * Captures key up and key down strokes to call command history and redefines
371 * the same events with control down to allow caret vertical shift.
373 * @see java.awt.Component#processKeyEvent(java.awt.event.KeyEvent)
375 protected void processKeyEvent(KeyEvent ke)
377 // Id Control key is down, captures events does command
378 // history recall and inhibits caret vertical shift.
379 if (ke.getKeyCode() == KeyEvent.VK_UP
380 && ke.getID() == KeyEvent.KEY_PRESSED && !ke.isControlDown())
384 else if (ke.getKeyCode() == KeyEvent.VK_DOWN
385 && ke.getID() == KeyEvent.KEY_PRESSED && !ke.isControlDown())
387 recallCommand(false);
389 // If Control key is down, redefines the event as if it
390 // where a key up or key down stroke without modifiers.
391 // This allows to move the caret up and down
392 // with no command history recall.
393 else if ((ke.getKeyCode() == KeyEvent.VK_DOWN || ke.getKeyCode() == KeyEvent.VK_UP)
394 && ke.getID() == KeyEvent.KEY_PRESSED && ke.isControlDown())
396 super.processKeyEvent(new KeyEvent((Component) ke.getSource(), ke
397 .getID(), ke.getWhen(), 0, // No modifiers
398 ke.getKeyCode(), ke.getKeyChar(), ke.getKeyLocation()));
400 // Standard processing for other events.
403 super.processKeyEvent(ke);
404 // check command for compiler-identifyable syntax issues
405 // this may have to be taken out if people start complaining
406 // that only some of the commands are being checked
407 // that is -- that the script itself is not being fully checked
409 // not perfect -- help here?
410 if (ke.getID() == KeyEvent.KEY_RELEASED
411 && (ke.getKeyCode() > KeyEvent.VK_DOWN)
412 || ke.getKeyCode() == KeyEvent.VK_BACK_SPACE)
418 * Recall command history.
423 void recallCommand(boolean up)
425 String cmd = viewer.getSetHistory(up ? -1 : 1);
432 if (cmd.endsWith(CommandHistory.ERROR_FLAG))
434 cmd = cmd.substring(0, cmd.indexOf(CommandHistory.ERROR_FLAG));
435 consoleDoc.replaceCommand(cmd, true);
439 consoleDoc.replaceCommand(cmd, false);
441 } catch (BadLocationException e)
449 String strCommand = consoleDoc.getCommandString();
450 if (strCommand.length() == 0)
453 .colorCommand(viewer.scriptCheck(strCommand) == null ? consoleDoc.attUserInput
454 : consoleDoc.attError);
459 class ConsoleDocument extends DefaultStyledDocument
462 ConsoleTextPane consoleTextPane;
464 SimpleAttributeSet attError;
466 SimpleAttributeSet attEcho;
468 SimpleAttributeSet attPrompt;
470 SimpleAttributeSet attUserInput;
472 SimpleAttributeSet attStatus;
478 attError = new SimpleAttributeSet();
479 StyleConstants.setForeground(attError, Color.red);
481 attPrompt = new SimpleAttributeSet();
482 StyleConstants.setForeground(attPrompt, Color.magenta);
484 attUserInput = new SimpleAttributeSet();
485 StyleConstants.setForeground(attUserInput, Color.black);
487 attEcho = new SimpleAttributeSet();
488 StyleConstants.setForeground(attEcho, Color.blue);
489 StyleConstants.setBold(attEcho, true);
491 attStatus = new SimpleAttributeSet();
492 StyleConstants.setForeground(attStatus, Color.black);
493 StyleConstants.setItalic(attStatus, true);
496 void setConsoleTextPane(ConsoleTextPane consoleTextPane)
498 this.consoleTextPane = consoleTextPane;
501 Position positionBeforePrompt; // starts at 0, so first time isn't tracked
502 // (at least on Mac OS X)
504 Position positionAfterPrompt; // immediately after $, so this will track
506 int offsetAfterPrompt; // only still needed for the insertString override and
510 * Removes all content of the script window, and add a new prompt.
516 super.remove(0, getLength());
517 } catch (BadLocationException exception)
519 System.out.println("Could not clear script window content: "
520 + exception.getMessage());
528 super.insertString(getLength(), "$ ", attPrompt);
529 setOffsetPositions();
530 consoleTextPane.setCaretPosition(offsetAfterPrompt);
531 } catch (BadLocationException e)
537 void setOffsetPositions()
541 offsetAfterPrompt = getLength();
542 positionBeforePrompt = createPosition(offsetAfterPrompt - 2);
543 // after prompt should be immediately after $ otherwise tracks the end
544 // of the line (and no command will be found) at least on Mac OS X it did.
545 positionAfterPrompt = createPosition(offsetAfterPrompt - 1);
546 } catch (BadLocationException e)
556 offsetAfterPrompt = getLength();
557 positionAfterPrompt = positionBeforePrompt = createPosition(offsetAfterPrompt);
558 consoleTextPane.setCaretPosition(offsetAfterPrompt);
559 } catch (BadLocationException e)
565 // it looks like the positionBeforePrompt does not track when it started out
567 // and a insertString at location 0 occurs. It may be better to track the
568 // position after the prompt in stead
569 void outputBeforePrompt(String str, SimpleAttributeSet attribute)
573 int pt = consoleTextPane.getCaretPosition();
574 Position caretPosition = createPosition(pt);
575 pt = positionBeforePrompt.getOffset();
576 super.insertString(pt, str + "\n", attribute);
577 setOffsetPositions();
578 pt = caretPosition.getOffset();
579 consoleTextPane.setCaretPosition(pt);
580 } catch (BadLocationException e)
586 void outputError(String strError)
588 outputBeforePrompt(strError, attError);
591 void outputErrorForeground(String strError)
595 super.insertString(getLength(), strError + "\n", attError);
596 consoleTextPane.setCaretPosition(getLength());
597 } catch (BadLocationException e)
604 void outputEcho(String strEcho)
606 outputBeforePrompt(strEcho, attEcho);
609 void outputStatus(String strStatus)
611 outputBeforePrompt(strStatus, attStatus);
618 super.insertString(getLength(), "\n", attUserInput);
619 consoleTextPane.setCaretPosition(getLength());
620 } catch (BadLocationException e)
626 // override the insertString to make sure everything typed ends up at the end
627 // or in the 'command line' using the proper font, and the newline is
629 public void insertString(int offs, String str, AttributeSet a)
630 throws BadLocationException
632 int ichNewline = str.indexOf('\n');
634 str = str.substring(0, ichNewline);
637 if (offs < offsetAfterPrompt)
641 super.insertString(offs, str, a == attError ? a : attUserInput);
642 consoleTextPane.setCaretPosition(offs + str.length());
646 consoleTextPane.enterPressed();
650 String getCommandString()
652 String strCommand = "";
655 int cmdStart = positionAfterPrompt.getOffset();
656 strCommand = getText(cmdStart, getLength() - cmdStart);
657 while (strCommand.length() > 0 && strCommand.charAt(0) == ' ')
658 strCommand = strCommand.substring(1);
659 } catch (BadLocationException e)
666 public void remove(int offs, int len) throws BadLocationException
668 if (offs < offsetAfterPrompt)
670 len -= offsetAfterPrompt - offs;
673 offs = offsetAfterPrompt;
675 super.remove(offs, len);
676 // consoleTextPane.setCaretPosition(offs);
679 public void replace(int offs, int length, String str, AttributeSet attrs)
680 throws BadLocationException
682 if (offs < offsetAfterPrompt)
684 if (offs + length < offsetAfterPrompt)
691 length -= offsetAfterPrompt - offs;
692 offs = offsetAfterPrompt;
695 super.replace(offs, length, str, attrs);
696 // consoleTextPane.setCaretPosition(offs + str.length());
700 * Replaces current command on script.
705 * true to set error color ends with #??
707 * @throws BadLocationException
709 void replaceCommand(String newCommand, boolean isError)
710 throws BadLocationException
712 if (positionAfterPrompt == positionBeforePrompt)
714 replace(offsetAfterPrompt, getLength() - offsetAfterPrompt, newCommand,
715 isError ? attError : attUserInput);
718 void colorCommand(SimpleAttributeSet att)
720 if (positionAfterPrompt == positionBeforePrompt)
722 setCharacterAttributes(offsetAfterPrompt, getLength()
723 - offsetAfterPrompt, att, true);
727 interface EnterListener
729 public void enterPressed();