001: /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
002: *
003: * ***** BEGIN LICENSE BLOCK *****
004: * Version: MPL 1.1/GPL 2.0
005: *
006: * The contents of this file are subject to the Mozilla Public License Version
007: * 1.1 (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: * http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the
014: * License.
015: *
016: * The Original Code is Rhino JavaScript Debugger code, released
017: * November 21, 2000.
018: *
019: * The Initial Developer of the Original Code is
020: * See Beyond Corporation.
021: * Portions created by the Initial Developer are Copyright (C) 2000
022: * the Initial Developer. All Rights Reserved.
023: *
024: * Contributor(s):
025: * Christopher Oliver
026: *
027: * Alternatively, the contents of this file may be used under the terms of
028: * the GNU General Public License Version 2 or later (the "GPL"), in which
029: * case the provisions of the GPL are applicable instead of those above. If
030: * you wish to allow use of your version of this file only under the terms of
031: * the GPL and not to allow others to use your version of this file under the
032: * MPL, indicate your decision by deleting the provisions above and replacing
033: * them with the notice and other provisions required by the GPL. If you do
034: * not delete the provisions above, a recipient may use your version of this
035: * file under either the MPL or the GPL.
036: *
037: * ***** END LICENSE BLOCK ***** */
038: package org.mozilla.javascript.tools.shell;
039:
040: import java.awt.event.ActionEvent;
041: import java.awt.event.ActionListener;
042: import java.awt.event.WindowAdapter;
043: import java.awt.event.WindowEvent;
044: import java.io.File;
045:
046: import javax.swing.ButtonGroup;
047: import javax.swing.JFileChooser;
048: import javax.swing.JFrame;
049: import javax.swing.JMenu;
050: import javax.swing.JMenuBar;
051: import javax.swing.JMenuItem;
052: import javax.swing.JOptionPane;
053: import javax.swing.JRadioButtonMenuItem;
054: import javax.swing.JScrollPane;
055: import javax.swing.SwingUtilities;
056: import javax.swing.UIManager;
057:
058: import org.mozilla.javascript.SecurityUtilities;
059:
060: public class JSConsole extends JFrame implements ActionListener {
061: static final long serialVersionUID = 2551225560631876300L;
062:
063: private File CWD;
064: private JFileChooser dlg;
065: private ConsoleTextArea consoleTextArea;
066:
067: public String chooseFile() {
068: if (CWD == null) {
069: String dir = SecurityUtilities
070: .getSystemProperty("user.dir");
071: if (dir != null) {
072: CWD = new File(dir);
073: }
074: }
075: if (CWD != null) {
076: dlg.setCurrentDirectory(CWD);
077: }
078: dlg.setDialogTitle("Select a file to load");
079: int returnVal = dlg.showOpenDialog(this );
080: if (returnVal == JFileChooser.APPROVE_OPTION) {
081: String result = dlg.getSelectedFile().getPath();
082: CWD = new File(dlg.getSelectedFile().getParent());
083: return result;
084: }
085: return null;
086: }
087:
088: public static void main(String args[]) {
089: new JSConsole(args);
090: }
091:
092: public void createFileChooser() {
093: dlg = new JFileChooser();
094: javax.swing.filechooser.FileFilter filter = new javax.swing.filechooser.FileFilter() {
095: public boolean accept(File f) {
096: if (f.isDirectory()) {
097: return true;
098: }
099: String name = f.getName();
100: int i = name.lastIndexOf('.');
101: if (i > 0 && i < name.length() - 1) {
102: String ext = name.substring(i + 1).toLowerCase();
103: if (ext.equals("js")) {
104: return true;
105: }
106: }
107: return false;
108: }
109:
110: public String getDescription() {
111: return "JavaScript Files (*.js)";
112: }
113: };
114: dlg.addChoosableFileFilter(filter);
115:
116: }
117:
118: public JSConsole(String[] args) {
119: super ("Rhino JavaScript Console");
120: JMenuBar menubar = new JMenuBar();
121: createFileChooser();
122: String[] fileItems = { "Load...", "Exit" };
123: String[] fileCmds = { "Load", "Exit" };
124: char[] fileShortCuts = { 'L', 'X' };
125: String[] editItems = { "Cut", "Copy", "Paste" };
126: char[] editShortCuts = { 'T', 'C', 'P' };
127: String[] plafItems = { "Metal", "Windows", "Motif" };
128: boolean[] plafState = { true, false, false };
129: JMenu fileMenu = new JMenu("File");
130: fileMenu.setMnemonic('F');
131: JMenu editMenu = new JMenu("Edit");
132: editMenu.setMnemonic('E');
133: JMenu plafMenu = new JMenu("Platform");
134: plafMenu.setMnemonic('P');
135: for (int i = 0; i < fileItems.length; ++i) {
136: JMenuItem item = new JMenuItem(fileItems[i],
137: fileShortCuts[i]);
138: item.setActionCommand(fileCmds[i]);
139: item.addActionListener(this );
140: fileMenu.add(item);
141: }
142: for (int i = 0; i < editItems.length; ++i) {
143: JMenuItem item = new JMenuItem(editItems[i],
144: editShortCuts[i]);
145: item.addActionListener(this );
146: editMenu.add(item);
147: }
148: ButtonGroup group = new ButtonGroup();
149: for (int i = 0; i < plafItems.length; ++i) {
150: JRadioButtonMenuItem item = new JRadioButtonMenuItem(
151: plafItems[i], plafState[i]);
152: group.add(item);
153: item.addActionListener(this );
154: plafMenu.add(item);
155: }
156: menubar.add(fileMenu);
157: menubar.add(editMenu);
158: menubar.add(plafMenu);
159: setJMenuBar(menubar);
160: consoleTextArea = new ConsoleTextArea(args);
161: JScrollPane scroller = new JScrollPane(consoleTextArea);
162: setContentPane(scroller);
163: consoleTextArea.setRows(24);
164: consoleTextArea.setColumns(80);
165: addWindowListener(new WindowAdapter() {
166: public void windowClosing(WindowEvent e) {
167: System.exit(0);
168: }
169: });
170: pack();
171: setVisible(true);
172: // System.setIn(consoleTextArea.getIn());
173: // System.setOut(consoleTextArea.getOut());
174: // System.setErr(consoleTextArea.getErr());
175: Main.setIn(consoleTextArea.getIn());
176: Main.setOut(consoleTextArea.getOut());
177: Main.setErr(consoleTextArea.getErr());
178: Main.main(args);
179: }
180:
181: public void actionPerformed(ActionEvent e) {
182: String cmd = e.getActionCommand();
183: String plaf_name = null;
184: if (cmd.equals("Load")) {
185: String f = chooseFile();
186: if (f != null) {
187: f = f.replace('\\', '/');
188: consoleTextArea.eval("load(\"" + f + "\");");
189: }
190: } else if (cmd.equals("Exit")) {
191: System.exit(0);
192: } else if (cmd.equals("Cut")) {
193: consoleTextArea.cut();
194: } else if (cmd.equals("Copy")) {
195: consoleTextArea.copy();
196: } else if (cmd.equals("Paste")) {
197: consoleTextArea.paste();
198: } else {
199: if (cmd.equals("Metal")) {
200: plaf_name = "javax.swing.plaf.metal.MetalLookAndFeel";
201: } else if (cmd.equals("Windows")) {
202: plaf_name = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
203: } else if (cmd.equals("Motif")) {
204: plaf_name = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
205: }
206: if (plaf_name != null) {
207: try {
208: UIManager.setLookAndFeel(plaf_name);
209: SwingUtilities.updateComponentTreeUI(this );
210: consoleTextArea.postUpdateUI();
211: // updateComponentTreeUI seems to mess up the file
212: // chooser dialog, so just create a new one
213: createFileChooser();
214: } catch (Exception exc) {
215: JOptionPane.showMessageDialog(this , exc
216: .getMessage(), "Platform",
217: JOptionPane.ERROR_MESSAGE);
218: }
219: }
220: }
221:
222: }
223:
224: }
|