📄 swinggui.java
字号:
private JButton refreshButton; /** * The "Cancel" button. */ private JButton cancelButton; /** * Creates a new FindFunction. */ public FindFunction(SwingGui debugGui, String title, String labelText) { super(debugGui, title, true); this.debugGui = debugGui; cancelButton = new JButton("Cancel"); setButton = new JButton("Select"); cancelButton.addActionListener(this); setButton.addActionListener(this); getRootPane().setDefaultButton(setButton); list = new JList(new DefaultListModel()); DefaultListModel model = (DefaultListModel)list.getModel(); model.clear(); String[] a = debugGui.dim.functionNames(); java.util.Arrays.sort(a); for (int i = 0; i < a.length; i++) { model.addElement(a[i]); } list.setSelectedIndex(0); setButton.setEnabled(a.length > 0); list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); list.addMouseListener(new MouseHandler()); JScrollPane listScroller = new JScrollPane(list); listScroller.setPreferredSize(new Dimension(320, 240)); listScroller.setMinimumSize(new Dimension(250, 80)); listScroller.setAlignmentX(LEFT_ALIGNMENT); //Create a container so that we can add a title around //the scroll pane. Can't add a title directly to the //scroll pane because its background would be white. //Lay out the label and scroll pane from top to button. JPanel listPane = new JPanel(); listPane.setLayout(new BoxLayout(listPane, BoxLayout.Y_AXIS)); JLabel label = new JLabel(labelText); label.setLabelFor (list); listPane.add(label); listPane.add(Box.createRigidArea(new Dimension(0,5))); listPane.add(listScroller); listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); //Lay out the buttons from left to right. JPanel buttonPane = new JPanel(); buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS)); buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10)); buttonPane.add(Box.createHorizontalGlue()); buttonPane.add(cancelButton); buttonPane.add(Box.createRigidArea(new Dimension(10, 0))); buttonPane.add(setButton); //Put everything together, using the content pane's BorderLayout. Container contentPane = getContentPane(); contentPane.add(listPane, BorderLayout.CENTER); contentPane.add(buttonPane, BorderLayout.SOUTH); pack(); addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent ke) { int code = ke.getKeyCode(); if (code == KeyEvent.VK_ESCAPE) { ke.consume(); value = null; setVisible(false); } } }); } /** * Shows the dialog. */ public String showDialog(Component comp) { value = null; setLocationRelativeTo(comp); setVisible(true); return value; } /** * Sets the last function selected. */ private void setValue(String newValue) { value = newValue; list.setSelectedValue(value, true); } // ActionListener /** * Performs an action. */ public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals("Cancel")) { setVisible(false); value = null; } else if (cmd.equals("Select")) { if (list.getSelectedIndex() < 0) { return; } try { value = (String)list.getSelectedValue(); } catch (ArrayIndexOutOfBoundsException exc) { return; } setVisible(false); Dim.FunctionSource item = debugGui.dim.functionSourceByName(value); if (item != null) { Dim.SourceInfo si = item.sourceInfo(); String url = si.url(); int lineNumber = item.firstLine(); FileWindow w = debugGui.getFileWindow(url); if (w == null) { debugGui.createFileWindow(si, lineNumber); w = debugGui.getFileWindow(url); w.setPosition(-1); } int start = w.getPosition(lineNumber-1); int end = w.getPosition(lineNumber)-1; w.textArea.select(start); w.textArea.setCaretPosition(start); w.textArea.moveCaretPosition(end); try { w.show(); debugGui.requestFocus(); w.requestFocus(); w.textArea.requestFocus(); } catch (Exception exc) { } } } } /** * MouseListener implementation for {@link #list}. */ class MouseHandler extends MouseAdapter { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { setButton.doClick(); } } };}/** * Gutter for FileWindows. */class FileHeader extends JPanel implements MouseListener { /** * Serializable magic number. */ private static final long serialVersionUID = -2858905404778259127L; /** * The line that the mouse was pressed on. */ private int pressLine = -1; /** * The owning FileWindow. */ private FileWindow fileWindow; /** * Creates a new FileHeader. */ public FileHeader(FileWindow fileWindow) { this.fileWindow = fileWindow; addMouseListener(this); update(); } /** * Updates the gutter. */ public void update() { FileTextArea textArea = fileWindow.textArea; Font font = textArea.getFont(); setFont(font); FontMetrics metrics = getFontMetrics(font); int h = metrics.getHeight(); int lineCount = textArea.getLineCount() + 1; String dummy = Integer.toString(lineCount); if (dummy.length() < 2) { dummy = "99"; } Dimension d = new Dimension(); d.width = metrics.stringWidth(dummy) + 16; d.height = lineCount * h + 100; setPreferredSize(d); setSize(d); } /** * Paints the component. */ public void paint(Graphics g) { super.paint(g); FileTextArea textArea = fileWindow.textArea; Font font = textArea.getFont(); g.setFont(font); FontMetrics metrics = getFontMetrics(font); Rectangle clip = g.getClipBounds(); g.setColor(getBackground()); g.fillRect(clip.x, clip.y, clip.width, clip.height); int left = getX(); int ascent = metrics.getMaxAscent(); int h = metrics.getHeight(); int lineCount = textArea.getLineCount() + 1; String dummy = Integer.toString(lineCount); if (dummy.length() < 2) { dummy = "99"; } int maxWidth = metrics.stringWidth(dummy); int startLine = clip.y / h; int endLine = (clip.y + clip.height) / h + 1; int width = getWidth(); if (endLine > lineCount) endLine = lineCount; for (int i = startLine; i < endLine; i++) { String text; int pos = -2; try { pos = textArea.getLineStartOffset(i); } catch (BadLocationException ignored) { } boolean isBreakPoint = fileWindow.isBreakPoint(i + 1); text = Integer.toString(i + 1) + " "; int w = metrics.stringWidth(text); int y = i * h; g.setColor(Color.blue); g.drawString(text, 0, y + ascent); int x = width - ascent; if (isBreakPoint) { g.setColor(new Color(0x80, 0x00, 0x00)); int dy = y + ascent - 9; g.fillOval(x, dy, 9, 9); g.drawOval(x, dy, 8, 8); g.drawOval(x, dy, 9, 9); } if (pos == fileWindow.currentPos) { Polygon arrow = new Polygon(); int dx = x; y += ascent - 10; int dy = y; arrow.addPoint(dx, dy + 3); arrow.addPoint(dx + 5, dy + 3); for (x = dx + 5; x <= dx + 10; x++, y++) { arrow.addPoint(x, y); } for (x = dx + 9; x >= dx + 5; x--, y++) { arrow.addPoint(x, y); } arrow.addPoint(dx + 5, dy + 7); arrow.addPoint(dx, dy + 7); g.setColor(Color.yellow); g.fillPolygon(arrow); g.setColor(Color.black); g.drawPolygon(arrow); } } } // MouseListener /** * Called when the mouse enters the component. */ public void mouseEntered(MouseEvent e) { } /** * Called when a mouse button is pressed. */ public void mousePressed(MouseEvent e) { Font font = fileWindow.textArea.getFont(); FontMetrics metrics = getFontMetrics(font); int h = metrics.getHeight(); pressLine = e.getY() / h; } /** * Called when the mouse is clicked. */ public void mouseClicked(MouseEvent e) { } /** * Called when the mouse exits the component. */ public void mouseExited(MouseEvent e) { } /** * Called when a mouse button is released. */ public void mouseReleased(MouseEvent e) { if (e.getComponent() == this && (e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) { int x = e.getX(); int y = e.getY(); Font font = fileWindow.textArea.getFont(); FontMetrics metrics = getFontMetrics(font); int h = metrics.getHeight(); int line = y/h; if (line == pressLine) { fileWindow.toggleBreakPoint(line + 1); } else { pressLine = -1; } } }};/** * An internal frame for script files. */class FileWindow extends JInternalFrame implements ActionListener { /** * Serializable magic number. */ private static final long serialVersionUID = -6212382604952082370L; /** * The debugger GUI. */ private SwingGui debugGui; /** * The SourceInfo object that describes the file. */ private Dim.SourceInfo sourceInfo; /** * The FileTextArea that displays the file. */ FileTextArea textArea; /** * The FileHeader that is the gutter for {@link #textArea}. */ private FileHeader fileHeader; /** * Scroll pane for containing {@link #textArea}. */ private JScrollPane p; /** * The current offset position. */ int currentPos; /** * The status bar. */ private JLabel statusBar; /** * Loads the file. */ void load() { String url = getUrl(); if (url != null) { RunProxy proxy = new RunProxy(debugGui, RunProxy.LOAD_FILE); proxy.fileName = url; proxy.text = sourceInfo.source(); new Thread(proxy).start(); } } /** * Returns the offset position for the given line. */ public int getPosition(int line) { int result = -1; try { result = textArea.getLineStartOffset(line); } catch (javax.swing.text.BadLocationException exc) { } return result; } /** * Returns whether the given line has a breakpoint. */ public boolean isBreakPoint(int line) { return sourceInfo.breakableLine(line) && sourceInfo.breakpoint(line); } /** * Toggles the breakpoint on the given line. */ public void toggleBreakPoint(int line) { if (!isBreakPoint(line)) { setBreakPoint(line); } else { clearBreakPoint(line); } } /** * Sets a breakpoint on the given line. */ public void setBreakPoint(int line) { if (sourceInfo.breakableLine(line)) { boolean changed = sourceInfo.breakpoint(line, true); if (changed) { fileHeader.repaint(); } } } /** * Clears a breakpoint from the given line. */ public void clearBreakPoint(int line) { if (sourceInfo.breakableLine(line)) { boolean changed = sourceInfo.breakpoint(line, false); if (changed) { fileHeader.repaint
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -