From 5841093c2444f8aaf9ea6bd53cc9cdcdf59d79d8 Mon Sep 17 00:00:00 2001 From: Tom Copeland Date: Wed, 19 Feb 2003 15:54:43 +0000 Subject: [PATCH] A nice splitpane thx to Vladimir Bossicard git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1451 51baf565-9d33-0410-a72c-fc3788e3496d --- .../net/sourceforge/pmd/util/ASTViewer.java | 87 ++++++++++++++----- 1 file changed, 67 insertions(+), 20 deletions(-) diff --git a/pmd/src/net/sourceforge/pmd/util/ASTViewer.java b/pmd/src/net/sourceforge/pmd/util/ASTViewer.java index 482617b47a..b607954a2c 100644 --- a/pmd/src/net/sourceforge/pmd/util/ASTViewer.java +++ b/pmd/src/net/sourceforge/pmd/util/ASTViewer.java @@ -5,23 +5,28 @@ */ package net.sourceforge.pmd.util; -import net.sourceforge.pmd.ast.ASTCompilationUnit; -import net.sourceforge.pmd.ast.JavaParser; -import net.sourceforge.pmd.ast.ParseException; - -import javax.swing.JButton; -import javax.swing.JEditorPane; -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JScrollPane; -import javax.swing.JTextArea; import java.awt.BorderLayout; -import java.awt.Dimension; +import java.awt.Component; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.PrintStream; import java.io.StringReader; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JSplitPane; +import javax.swing.JTextArea; +import javax.swing.JTextPane; + +import net.sourceforge.pmd.ast.ASTCompilationUnit; +import net.sourceforge.pmd.ast.JavaParser; +import net.sourceforge.pmd.ast.ParseException; + public class ASTViewer { private class MyPrintStream extends PrintStream { @@ -60,30 +65,31 @@ public class ASTViewer { } } - private JEditorPane codeEditorPane = new JEditorPane(); + private JTextPane codeEditorPane = new JTextPane(); private JTextArea astArea = new JTextArea(); private JFrame frame = new JFrame("AST Viewer"); public ASTViewer() { - JPanel codePanel = new JPanel(); - codeEditorPane.setPreferredSize(new Dimension(500, 700)); + JSmartPanel codePanel = new JSmartPanel(); JScrollPane codeScrollPane = new JScrollPane(codeEditorPane); - codePanel.add(codeScrollPane); + codePanel.add(codeScrollPane, 0, 0, 1, 1, 1.0, 1.0, + GridBagConstraints.NORTH, GridBagConstraints.BOTH, + new Insets(0, 0, 0, 0)); - JPanel astPanel = new JPanel(); + JSmartPanel astPanel = new JSmartPanel(); astArea.setRows(40); astArea.setColumns(40); JScrollPane astScrollPane = new JScrollPane(astArea); - astPanel.add(astScrollPane); - + astPanel.add(astScrollPane, 0, 0, 1, 1, 1.0, 1.0, + GridBagConstraints.NORTH, GridBagConstraints.BOTH, + new Insets(0, 0, 0, 0)); JButton showButton = new JButton("Show AST"); showButton.setMnemonic('s'); showButton.addActionListener(new ShowListener()); frame.getContentPane().setLayout(new BorderLayout()); - frame.getContentPane().add(codePanel, BorderLayout.WEST); - frame.getContentPane().add(astPanel, BorderLayout.EAST); + frame.getContentPane().add(new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, codePanel, astPanel), BorderLayout.NORTH); frame.getContentPane().add(showButton, BorderLayout.SOUTH); frame.setSize(1000, 800); @@ -95,4 +101,45 @@ public class ASTViewer { public static void main(String[] args) { new ASTViewer(); } + + public class JSmartPanel extends JPanel { + + private GridBagConstraints constraints; + + /** + * Create a JPanel with a GridBagLayout layout + * + * @see java.awt.GridBagLayout + */ + public JSmartPanel () { + super(new GridBagLayout()); + constraints = new GridBagConstraints(); + } + + /** + * Add a component to the layout + * + * @see java.awt.GridBagLayout + * @see java.awt.GridBagConstraints + */ + public void add (Component comp, + int gridx, int gridy, int gridwidth, int gridheight, + double weightx, double weighty, + int anchor, int fill, + Insets insets) { + constraints.gridx = gridx; + constraints.gridy = gridy; + constraints.gridwidth = gridwidth; + constraints.gridheight = gridheight; + constraints.weightx = weightx; + constraints.weighty = weighty; + constraints.anchor = anchor; + constraints.fill = fill; + constraints.insets = insets; + + add(comp, constraints); + } + + } + }