⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swinggui.java

📁 java中比较著名的js引擎当属mozilla开源的rhino
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Rhino JavaScript Debugger code, released * November 21, 2000. * * The Initial Developer of the Original Code is * SeeBeyond Corporation. * Portions created by the Initial Developer are Copyright (C) 2000 * the Initial Developer. All Rights Reserved. * * Contributor(s): *   Igor Bukanov *   Matt Gould *   Christopher Oliver *   Cameron McCormack * * Alternatively, the contents of this file may be used under the terms of * the GNU General Public License Version 2 or later (the "GPL"), in which * case the provisions of the GPL are applicable instead of those above. If * you wish to allow use of your version of this file only under the terms of * the GPL and not to allow others to use your version of this file under the * MPL, indicate your decision by deleting the provisions above and replacing * them with the notice and other provisions required by the GPL. If you do * not delete the provisions above, a recipient may use your version of this * file under either the MPL or the GPL. * * ***** END LICENSE BLOCK ***** */package org.mozilla.javascript.tools.debugger;import javax.swing.*;import javax.swing.text.*;import javax.swing.event.*;import javax.swing.table.*;import java.awt.*;import java.awt.event.*;import java.util.*;import java.io.*;import javax.swing.tree.DefaultTreeCellRenderer;import javax.swing.tree.TreePath;import java.lang.reflect.Method;import org.mozilla.javascript.Kit;import org.mozilla.javascript.tools.shell.ConsoleTextArea;import org.mozilla.javascript.tools.debugger.downloaded.JTreeTable;import org.mozilla.javascript.tools.debugger.downloaded.TreeTableModel;import org.mozilla.javascript.tools.debugger.downloaded.TreeTableModelAdapter;/** * GUI for the Rhino debugger. */public class SwingGui extends JFrame implements GuiCallback {    /**     * Serializable magic number.     */    private static final long serialVersionUID = -8217029773456711621L;    /**     * The debugger.     */    Dim dim;    /**     * The action to run when the 'Exit' menu item is chosen or the     * frame is closed.     */    private Runnable exitAction;    /**     * The {@link JDesktopPane} that holds the script windows.     */    private JDesktopPane desk;    /**     * The {@link JPanel} that shows information about the context.     */    private ContextWindow context;    /**     * The menu bar.     */    private Menubar menubar;    /**     * The tool bar.     */    private JToolBar toolBar;    /**     * The console that displays I/O from the script.     */    private JSInternalConsole console;    /**     * The script evaluation internal frame.     */    private EvalWindow evalWindow;    /**     * The {@link JSplitPane} that separates {@link #desk} from {@link context}.     */    private JSplitPane split1;    /**     * The status bar.     */    private JLabel statusBar;    /**     * Hash table of internal frame names to the internal frames themselves.     */    private Hashtable toplevels = new Hashtable();    /**     * Hash table of script URLs to their internal frames.     */    private Hashtable fileWindows = new Hashtable();    /**     * The {@link FileWindow} that last had the focus.     */    private FileWindow currentWindow;    /**     * File choose dialog for loading a script.     */    JFileChooser dlg;    /**     * The AWT EventQueue.  Used for manually pumping AWT events from     * {@link #dispatchNextGuiEvent()}.     */    private EventQueue awtEventQueue;    /**     * Creates a new SwingGui.     */    public SwingGui(Dim dim, String title) {        super(title);        this.dim = dim;        init();        dim.setGuiCallback(this);    }    /**     * Returns the Menubar of this debugger frame.     */    public Menubar getMenubar() {        return menubar;    }    /**     * Sets the {@link Runnable} that will be run when the "Exit" menu     * item is chosen.     */    public void setExitAction(Runnable r) {        exitAction = r;    }    /**     * Returns the debugger console component.     */    public JSInternalConsole getConsole() {        return console;    }    /**     * Sets the visibility of the debugger GUI.     */    public void setVisible(boolean b) {        super.setVisible(b);        if (b) {            // this needs to be done after the window is visible            console.consoleTextArea.requestFocus();            context.split.setDividerLocation(0.5);            try {                console.setMaximum(true);                console.setSelected(true);                console.show();                console.consoleTextArea.requestFocus();            } catch (Exception exc) {            }        }    }    /**     * Records a new internal frame.     */    void addTopLevel(String key, JFrame frame) {        if (frame != this) {            toplevels.put(key, frame);        }    }    /**     * Constructs the debugger GUI.     */    private void init() {        menubar = new Menubar(this);        setJMenuBar(menubar);        toolBar = new JToolBar();        JButton button;        JButton breakButton, goButton, stepIntoButton,            stepOverButton, stepOutButton;        String [] toolTips = {"Break (Pause)",                              "Go (F5)",                              "Step Into (F11)",                              "Step Over (F7)",                              "Step Out (F8)"};        int count = 0;        button = breakButton = new JButton("Break");        JButton focusButton = button;        button.setToolTipText("Break");        button.setActionCommand("Break");        button.addActionListener(menubar);        button.setEnabled(true);        button.setToolTipText(toolTips[count++]);        button = goButton = new JButton("Go");        button.setToolTipText("Go");        button.setActionCommand("Go");        button.addActionListener(menubar);        button.setEnabled(false);        button.setToolTipText(toolTips[count++]);        button = stepIntoButton = new JButton("Step Into");        button.setToolTipText("Step Into");        button.setActionCommand("Step Into");        button.addActionListener(menubar);        button.setEnabled(false);        button.setToolTipText(toolTips[count++]);        button = stepOverButton = new JButton("Step Over");        button.setToolTipText("Step Over");        button.setActionCommand("Step Over");        button.setEnabled(false);        button.addActionListener(menubar);        button.setToolTipText(toolTips[count++]);        button = stepOutButton = new JButton("Step Out");        button.setToolTipText("Step Out");        button.setActionCommand("Step Out");        button.setEnabled(false);        button.addActionListener(menubar);        button.setToolTipText(toolTips[count++]);        Dimension dim = stepOverButton.getPreferredSize();        breakButton.setPreferredSize(dim);        breakButton.setMinimumSize(dim);        breakButton.setMaximumSize(dim);        breakButton.setSize(dim);        goButton.setPreferredSize(dim);        goButton.setMinimumSize(dim);        goButton.setMaximumSize(dim);        stepIntoButton.setPreferredSize(dim);        stepIntoButton.setMinimumSize(dim);        stepIntoButton.setMaximumSize(dim);        stepOverButton.setPreferredSize(dim);        stepOverButton.setMinimumSize(dim);        stepOverButton.setMaximumSize(dim);        stepOutButton.setPreferredSize(dim);        stepOutButton.setMinimumSize(dim);        stepOutButton.setMaximumSize(dim);        toolBar.add(breakButton);        toolBar.add(goButton);        toolBar.add(stepIntoButton);        toolBar.add(stepOverButton);        toolBar.add(stepOutButton);        JPanel contentPane = new JPanel();        contentPane.setLayout(new BorderLayout());        getContentPane().add(toolBar, BorderLayout.NORTH);        getContentPane().add(contentPane, BorderLayout.CENTER);        desk = new JDesktopPane();        desk.setPreferredSize(new Dimension(600, 300));        desk.setMinimumSize(new Dimension(150, 50));        desk.add(console = new JSInternalConsole("JavaScript Console"));        context = new ContextWindow(this);        context.setPreferredSize(new Dimension(600, 120));        context.setMinimumSize(new Dimension(50, 50));        split1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, desk,                                          context);        split1.setOneTouchExpandable(true);        SwingGui.setResizeWeight(split1, 0.66);        contentPane.add(split1, BorderLayout.CENTER);        statusBar = new JLabel();        statusBar.setText("Thread: ");        contentPane.add(statusBar, BorderLayout.SOUTH);        dlg = new JFileChooser();        javax.swing.filechooser.FileFilter filter =            new javax.swing.filechooser.FileFilter() {                    public boolean accept(File f) {                        if (f.isDirectory()) {                            return true;                        }                        String n = f.getName();                        int i = n.lastIndexOf('.');                        if (i > 0 && i < n.length() -1) {                            String ext = n.substring(i + 1).toLowerCase();                            if (ext.equals("js")) {                                return true;                            }                        }                        return false;                    }                    public String getDescription() {                        return "JavaScript Files (*.js)";                    }                };        dlg.addChoosableFileFilter(filter);        addWindowListener(new WindowAdapter() {                public void windowClosing(WindowEvent e) {                    exit();                }            });    }    /**     * Runs the {@link #exitAction}.     */    private void exit() {        if (exitAction != null) {            SwingUtilities.invokeLater(exitAction);        }        dim.setReturnValue(Dim.EXIT);    }    /**     * Returns the {@link FileWindow} for the given URL.     */    FileWindow getFileWindow(String url) {        if (url == null || url.equals("<stdin>")) {            return null;        }        return (FileWindow)fileWindows.get(url);    }    /**     * Returns a short version of the given URL.     */    static String getShortName(String url) {        int lastSlash = url.lastIndexOf('/');        if (lastSlash < 0) {            lastSlash = url.lastIndexOf('\\');        }        String shortName = url;        if (lastSlash >= 0 && lastSlash + 1 < url.length()) {            shortName = url.substring(lastSlash + 1);        }        return shortName;    }    /**     * Closes the given {@link FileWindow}.     */    void removeWindow(FileWindow w) {        fileWindows.remove(w.getUrl());        JMenu windowMenu = getWindowMenu();        int count = windowMenu.getItemCount();        JMenuItem lastItem = windowMenu.getItem(count -1);        String name = getShortName(w.getUrl());        for (int i = 5; i < count; i++) {            JMenuItem item = windowMenu.getItem(i);            if (item == null) continue; // separator            String text = item.getText();            //1 D:\foo.js            //2 D:\bar.js            int pos = text.indexOf(' ');            if (text.substring(pos + 1).equals(name)) {                windowMenu.remove(item);                // Cascade    [0]                // Tile       [1]                // -------    [2]                // Console    [3]                // -------    [4]                if (count == 6) {                    // remove the final separator                    windowMenu.remove(4);                } else {                    int j = i - 4;                    for (;i < count -1; i++) {                        JMenuItem thisItem = windowMenu.getItem(i);                        if (thisItem != null) {                            //1 D:\foo.js                            //2 D:\bar.js                            text = thisItem.getText();                            if (text.equals("More Windows...")) {                                break;                            } else {                                pos = text.indexOf(' ');                                thisItem.setText((char)('0' + j) + " " +                                                 text.substring(pos + 1));                                thisItem.setMnemonic('0' + j);                                j++;                            }                        }                    }                    if (count - 6 == 0 && lastItem != item) {                        if (lastItem.getText().equals("More Windows...")) {                            windowMenu.remove(lastItem);                        }                    }                }                break;            }        }        windowMenu.revalidate();    }    /**     * Shows the line at which execution in the given stack frame just stopped.     */    void showStopLine(Dim.StackFrame frame) {        String sourceName = frame.getUrl();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -