📄 swinggui.java
字号:
if (sourceName == null || sourceName.equals("<stdin>")) { if (console.isVisible()) { console.show(); } } else { int lineNumber = frame.getLineNumber(); FileWindow w = getFileWindow(sourceName); if (w != null) { setFilePosition(w, lineNumber); } else { Dim.SourceInfo si = frame.sourceInfo(); createFileWindow(si, lineNumber); } } } /** * Creates and shows a new {@link FileWindow} for the given source. */ void createFileWindow(Dim.SourceInfo sourceInfo, int line) { boolean activate = true; String url = sourceInfo.url(); FileWindow w = new FileWindow(this, sourceInfo); fileWindows.put(url, w); if (line != -1) { if (currentWindow != null) { currentWindow.setPosition(-1); } try { w.setPosition(w.textArea.getLineStartOffset(line-1)); } catch (BadLocationException exc) { try { w.setPosition(w.textArea.getLineStartOffset(0)); } catch (BadLocationException ee) { w.setPosition(-1); } } } desk.add(w); if (line != -1) { currentWindow = w; } menubar.addFile(url); w.setVisible(true); if (activate) { try { w.setMaximum(true); w.setSelected(true); w.moveToFront(); } catch (Exception exc) { } } } /** * Moves the current position in the given {@link FileWindow} to the * given line. */ private void setFilePosition(FileWindow w, int line) { boolean activate = true; JTextArea ta = w.textArea; try { if (line == -1) { w.setPosition(-1); if (currentWindow == w) { currentWindow = null; } } else { int loc = ta.getLineStartOffset(line-1); if (currentWindow != null && currentWindow != w) { currentWindow.setPosition(-1); } w.setPosition(loc); currentWindow = w; } } catch (BadLocationException exc) { // fix me } if (activate) { if (w.isIcon()) { desk.getDesktopManager().deiconifyFrame(w); } desk.getDesktopManager().activateFrame(w); try { w.show(); w.toFront(); // required for correct frame layering (JDK 1.4.1) w.setSelected(true); } catch (Exception exc) { } } } /** * Handles script interruption. */ void enterInterruptImpl(Dim.StackFrame lastFrame, String threadTitle, String alertMessage) { statusBar.setText("Thread: " + threadTitle); showStopLine(lastFrame); if (alertMessage != null) { MessageDialogWrapper.showMessageDialog(this, alertMessage, "Exception in Script", JOptionPane.ERROR_MESSAGE); } updateEnabled(true); Dim.ContextData contextData = lastFrame.contextData(); JComboBox ctx = context.context; Vector toolTips = context.toolTips; context.disableUpdate(); int frameCount = contextData.frameCount(); ctx.removeAllItems(); // workaround for JDK 1.4 bug that caches selected value even after // removeAllItems() is called ctx.setSelectedItem(null); toolTips.removeAllElements(); for (int i = 0; i < frameCount; i++) { Dim.StackFrame frame = contextData.getFrame(i); String url = frame.getUrl(); int lineNumber = frame.getLineNumber(); String shortName = url; if (url.length() > 20) { shortName = "..." + url.substring(url.length() - 17); } String location = "\"" + shortName + "\", line " + lineNumber; ctx.insertItemAt(location, i); location = "\"" + url + "\", line " + lineNumber; toolTips.addElement(location); } context.enableUpdate(); ctx.setSelectedIndex(0); ctx.setMinimumSize(new Dimension(50, ctx.getMinimumSize().height)); } /** * Returns the 'Window' menu. */ private JMenu getWindowMenu() { return menubar.getMenu(3); } /** * Displays a {@link JFileChooser} and returns the selected filename. */ private String chooseFile(String title) { dlg.setDialogTitle(title); File CWD = null; String dir = System.getProperty("user.dir"); if (dir != null) { CWD = new File(dir); } if (CWD != null) { dlg.setCurrentDirectory(CWD); } int returnVal = dlg.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { try { String result = dlg.getSelectedFile().getCanonicalPath(); CWD = dlg.getSelectedFile().getParentFile(); Properties props = System.getProperties(); props.put("user.dir", CWD.getPath()); System.setProperties(props); return result; } catch (IOException ignored) { } catch (SecurityException ignored) { } } return null; } /** * Returns the current selected internal frame. */ private JInternalFrame getSelectedFrame() { JInternalFrame[] frames = desk.getAllFrames(); for (int i = 0; i < frames.length; i++) { if (frames[i].isShowing()) { return frames[i]; } } return frames[frames.length - 1]; } /** * Enables or disables the menu and tool bars with respect to the * state of script execution. */ private void updateEnabled(boolean interrupted) { ((Menubar)getJMenuBar()).updateEnabled(interrupted); for (int ci = 0, cc = toolBar.getComponentCount(); ci < cc; ci++) { boolean enableButton; if (ci == 0) { // Break enableButton = !interrupted; } else { enableButton = interrupted; } toolBar.getComponent(ci).setEnabled(enableButton); } if (interrupted) { toolBar.setEnabled(true); // raise the debugger window int state = getExtendedState(); if (state == Frame.ICONIFIED) { setExtendedState(Frame.NORMAL); } toFront(); context.enable(); } else { if (currentWindow != null) currentWindow.setPosition(-1); context.disable(); } } /** * Calls {@link JSplitPane#setResizeWeight} via reflection. * For compatibility, since JDK < 1.3 does not have this method. */ static void setResizeWeight(JSplitPane pane, double weight) { try { Method m = JSplitPane.class.getMethod("setResizeWeight", new Class[]{double.class}); m.invoke(pane, new Object[]{new Double(weight)}); } catch (NoSuchMethodException exc) { } catch (IllegalAccessException exc) { } catch (java.lang.reflect.InvocationTargetException exc) { } } /** * Reads the file with the given name and returns its contents as a String. */ private String readFile(String fileName) { String text; try { Reader r = new FileReader(fileName); try { text = Kit.readReader(r); } finally { r.close(); } } catch (IOException ex) { MessageDialogWrapper.showMessageDialog(this, ex.getMessage(), "Error reading "+fileName, JOptionPane.ERROR_MESSAGE); text = null; } return text; } // GuiCallback /** * Called when the source text for a script has been updated. */ public void updateSourceText(Dim.SourceInfo sourceInfo) { RunProxy proxy = new RunProxy(this, RunProxy.UPDATE_SOURCE_TEXT); proxy.sourceInfo = sourceInfo; SwingUtilities.invokeLater(proxy); } /** * Called when the interrupt loop has been entered. */ public void enterInterrupt(Dim.StackFrame lastFrame, String threadTitle, String alertMessage) { if (SwingUtilities.isEventDispatchThread()) { enterInterruptImpl(lastFrame, threadTitle, alertMessage); } else { RunProxy proxy = new RunProxy(this, RunProxy.ENTER_INTERRUPT); proxy.lastFrame = lastFrame; proxy.threadTitle = threadTitle; proxy.alertMessage = alertMessage; SwingUtilities.invokeLater(proxy); } } /** * Returns whether the current thread is the GUI event thread. */ public boolean isGuiEventThread() { return SwingUtilities.isEventDispatchThread(); } /** * Processes the next GUI event. */ public void dispatchNextGuiEvent() throws InterruptedException { EventQueue queue = awtEventQueue; if (queue == null) { queue = Toolkit.getDefaultToolkit().getSystemEventQueue(); awtEventQueue = queue; } AWTEvent event = queue.getNextEvent(); if (event instanceof ActiveEvent) { ((ActiveEvent)event).dispatch(); } else { Object source = event.getSource(); if (source instanceof Component) { Component comp = (Component)source; comp.dispatchEvent(event); } else if (source instanceof MenuComponent) { ((MenuComponent)source).dispatchEvent(event); } } } // ActionListener /** * Performs an action from the menu or toolbar. */ public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); int returnValue = -1; if (cmd.equals("Cut") || cmd.equals("Copy") || cmd.equals("Paste")) { JInternalFrame f = getSelectedFrame(); if (f != null && f instanceof ActionListener) { ((ActionListener)f).actionPerformed(e); } } else if (cmd.equals("Step Over")) { returnValue = Dim.STEP_OVER; } else if (cmd.equals("Step Into")) { returnValue = Dim.STEP_INTO; } else if (cmd.equals("Step Out")) { returnValue = Dim.STEP_OUT; } else if (cmd.equals("Go")) { returnValue = Dim.GO; } else if (cmd.equals("Break")) { dim.setBreak(); } else if (cmd.equals("Exit")) { exit(); } else if (cmd.equals("Open")) { String fileName = chooseFile("Select a file to compile"); if (fileName != null) { String text = readFile(fileName); if (text != null) { RunProxy proxy = new RunProxy(this, RunProxy.OPEN_FILE); proxy.fileName = fileName; proxy.text = text; new Thread(proxy).start(); } } } else if (cmd.equals("Load")) { String fileName = chooseFile("Select a file to execute"); if (fileName != null) { String text = readFile(fileName); if (text != null) { RunProxy proxy = new RunProxy(this, RunProxy.LOAD_FILE); proxy.fileName = fileName; proxy.text = text; new Thread(proxy).start(); } } } else if (cmd.equals("More Windows...")) { MoreWindows dlg = new MoreWindows(this, fileWindows, "Window", "Files"); dlg.showDialog(this); } else if (cmd.equals("Console")) { if (console.isIcon()) { desk.getDesktopManager().deiconifyFrame(console); } console.show(); desk.getDesktopManager().activateFrame(console); console.consoleTextArea.requestFocus(); } else if (cmd.equals("Cut")) { } else if (cmd.equals("Copy")) { } else if (cmd.equals("Paste")) { } else if (cmd.equals("Go to function...")) { FindFunction dlg = new FindFunction(this, "Go to function", "Function"); dlg.showDialog(this); } else if (cmd.equals("Tile")) { JInternalFrame[] frames = desk.getAllFrames(); int count = frames.length; int rows, cols; rows = cols = (int)Math.sqrt(count); if (rows*cols < count) { cols++; if (rows * cols < count) { rows++; } } Dimension size = desk.getSize(); int w = size.width/cols; int h = size.height/rows; int x = 0; int y = 0; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { int index = (i*cols) + j; if (index >= frames.length) { break; } JInternalFrame f = frames[index]; try { f.setIcon(false); f.setMaximum(false); } catch (Exception exc) { } desk.getDesktopManager().setBoundsForFrame(f, x, y, w, h); x += w; } y += h; x = 0; } } else if (cmd.equals("Cascade")) { JInternalFrame[] frames = desk.getAllFrames(); int count = frames.length; int x, y, w, h; x = y = 0; h = desk.getHeight(); int d = h / count; if (d > 30) d = 30; for (int i = count -1; i >= 0; i--, x += d, y += d) { JInternalFrame f = frames[i]; try { f.setIcon(false); f.setMaximum(false); } catch (Exception exc) { } Dimension dimen = f.getPreferredSize(); w = dimen.width;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -