GuiConsole.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package kawa;
  2. import gnu.kawa.io.CheckConsole;
  3. import gnu.kawa.io.InPort;
  4. import gnu.kawa.io.OutPort;
  5. import gnu.mapping.*;
  6. import gnu.expr.Language;
  7. import java.awt.*;
  8. import javax.swing.*;
  9. import java.awt.event.*;
  10. /** A Frame containing a Kawa read-eval-print loop.
  11. * @author Albert Ting alt@artisan.com (original base)
  12. * @author Per Bothner (extensive changes).
  13. */
  14. public class GuiConsole extends JFrame
  15. implements ActionListener, ReplDocument.DocumentCloseListener {
  16. private static String CLOSE = "Close";
  17. private static String EXIT = "Exit";
  18. private static String NEW = "New";
  19. private static String NEW_SHARED = "New (Shared)";
  20. private static String PURGE_MESSAGE = "Purge Buffer";
  21. static int window_number = 0;
  22. ReplPane pane;
  23. ReplDocument document;
  24. public static void main(String[] args) {
  25. CheckConsole.setHaveConsole(true);
  26. int iArg = repl.processArgs(args, 0, args.length);
  27. repl.getLanguage();
  28. repl.setArgs(args, iArg);
  29. repl.checkInitFile();
  30. new GuiConsole();
  31. }
  32. public GuiConsole()
  33. {
  34. this(Language.getDefaultLanguage(), Environment.getCurrent(), false);
  35. }
  36. public GuiConsole (ReplDocument doc)
  37. {
  38. super("Kawa");
  39. init(doc);
  40. }
  41. void init(ReplDocument doc)
  42. {
  43. document = doc;
  44. document.addDocumentCloseListener(this);
  45. pane = new ReplPane(document);
  46. window_number++;
  47. this.setLayout(new BorderLayout(0,0));
  48. this.add("Center", new JScrollPane(pane));
  49. setupMenus();
  50. //pack();
  51. setLocation(100 * window_number, 50 * window_number);
  52. setSize(700,500);
  53. EventQueue.invokeLater(new Runnable() {
  54. public void run() { setVisible(true); }});
  55. }
  56. public GuiConsole(Language language, Environment penvironment, boolean shared)
  57. {
  58. super("Kawa");
  59. repl.getLanguage(); // In case a new GuiConsole is created from Java.
  60. init(new ReplDocument(language, penvironment, shared));
  61. // Uncomment to test same ReplDocument in two JFrames.
  62. // new GuiConsole(document);
  63. }
  64. public void closed (ReplDocument doc)
  65. {
  66. close();
  67. }
  68. void close () {
  69. document.removeDocumentCloseListener(this);
  70. dispose();
  71. }
  72. private void setupMenus() {
  73. MenuBar menubar;
  74. Menu fileMenu;
  75. Menu utilitiesMenu;
  76. MenuItem menuItem;
  77. WindowListener windowExitCmd = new WindowAdapter() {
  78. public void windowClosing(WindowEvent e) {
  79. close();
  80. }
  81. };
  82. // Create the menubar
  83. menubar = new MenuBar();
  84. fileMenu = new Menu("File");
  85. utilitiesMenu = new Menu("Utilities");
  86. menubar.add(fileMenu);
  87. menubar.add(utilitiesMenu);
  88. menuItem = new MenuItem(NEW);
  89. menuItem.addActionListener(this);
  90. fileMenu.add(menuItem);
  91. menuItem = new MenuItem(NEW_SHARED);
  92. menuItem.addActionListener(this);
  93. fileMenu.add(menuItem);
  94. menuItem = new MenuItem(CLOSE);
  95. menuItem.addActionListener(this);
  96. fileMenu.add(menuItem);
  97. menuItem = new MenuItem(EXIT);
  98. menuItem.addActionListener(this);
  99. this.addWindowListener(windowExitCmd);
  100. fileMenu.add(menuItem);
  101. menuItem = new MenuItem(PURGE_MESSAGE);
  102. menuItem.addActionListener(this);
  103. utilitiesMenu.add(menuItem);
  104. this.setMenuBar(menubar);
  105. }
  106. public void actionPerformed(ActionEvent e)
  107. {
  108. String cmd = e.getActionCommand();
  109. if (cmd.equals(NEW))
  110. new GuiConsole(document.language, Environment.getGlobal(), false);
  111. else if (cmd.equals(NEW_SHARED))
  112. new GuiConsole(document.language, document.environment, true);
  113. else if (cmd.equals(EXIT))
  114. System.exit(0);
  115. else if (cmd.equals(CLOSE))
  116. close();
  117. else if (cmd.equals(PURGE_MESSAGE)) {
  118. pane.document.deleteOldText();
  119. }
  120. else
  121. OutPort.outDefault().println("Unknown menu action: "+cmd);
  122. }
  123. }