A better 'create rule XML' panel from Sven Jacob

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4673 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2006-10-16 12:52:11 +00:00
parent ee22ca56c2
commit 0560184d06
2 changed files with 161 additions and 79 deletions

View File

@ -0,0 +1,140 @@
package net.sourceforge.pmd.util.designer;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import net.sourceforge.pmd.PMD;
/**
* This class is responsible for creating the
* contentpanel for the Create Rule XML Frame.
*/
public class CreateXMLRulePanel extends JPanel implements ActionListener{
private static final JTextField rulenameField = new JTextField(30);
private static final JTextField rulemsgField = new JTextField(30);
private static final JTextArea ruledescField = new JTextArea(5,30);
private static final JTextArea ruleXMLArea = new JTextArea(30, 30);
private JTextArea xpathQueryArea = new JTextArea();
private CodeEditorTextPane codeEditorPane = new CodeEditorTextPane();
public CreateXMLRulePanel(JTextArea xpathQueryArea, CodeEditorTextPane codeEditorPane){
super();
this.xpathQueryArea = xpathQueryArea;
this.codeEditorPane = codeEditorPane;
GridBagConstraints gbc = new GridBagConstraints();
// We use a gridbaglayout for a nice and sturdy look and feel
GridBagLayout gbl = new GridBagLayout();
setLayout(gbl);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.EAST;
gbc.weightx = 0.5;
JLabel rulenameLabel = new JLabel("Rule name : ");
gbl.setConstraints(rulenameLabel, gbc);
add(rulenameLabel);
gbc.weightx = 0.5;
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 1;
gbl.setConstraints(rulenameField, gbc);
add(rulenameField);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.EAST;
gbc.weightx = 0.5;
JLabel rulemsgLabel = new JLabel("Rule msg : ");
gbl.setConstraints(rulemsgLabel, gbc);
add(rulemsgLabel);
gbc.gridx = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 0.5;
gbl.setConstraints(rulemsgField, gbc);
add(rulemsgField);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.EAST;
gbc.weightx = 0.5;
JLabel ruledescLabel = new JLabel("Rule desc : ");
gbl.setConstraints(ruledescLabel,gbc);
add(ruledescLabel);
gbc.gridx = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 0.5;
gbl.setConstraints(ruledescField,gbc);
add(ruledescField);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.NORTH;
JButton createRuleBtn = new JButton("Create rule XML");
createRuleBtn.addActionListener(this);
gbl.setConstraints(createRuleBtn, gbc);
add(createRuleBtn);
gbc.gridx = 0;
gbc.gridy = 4;
gbc.anchor = GridBagConstraints.NORTH;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
JScrollPane ruleXMLPane = new JScrollPane(ruleXMLArea);
gbl.setConstraints(ruleXMLPane, gbc);
add(ruleXMLPane);
repaint();
}
/**
* We let our class implement the ActionListener interface
* and use it to generate the xml code when the user presses
* the "Create rule XML" button.
*
*/
public void actionPerformed(ActionEvent exception) {
StringBuffer buffer = new StringBuffer();
buffer.append("<rule name=\"" + rulenameField.getText() + "\"" + PMD.EOL);
buffer.append(" message=\"" + rulemsgField.getText() + "\"" + PMD.EOL);
buffer.append(" class=\"" + (xpathQueryArea.getText().length() == 0 ? "" : "net.sourceforge.pmd.rules.XPathRule") + "\">" + PMD.EOL);
buffer.append(" <description>" + PMD.EOL);
buffer.append(" " + ruledescField.getText() + PMD.EOL);
buffer.append(" </description>" + PMD.EOL);
if (xpathQueryArea.getText().length() != 0) {
buffer.append(" <properties>" + PMD.EOL);
buffer.append(" <property name=\"xpath\">" + PMD.EOL);
buffer.append(" <value>" + PMD.EOL);
buffer.append("<![CDATA[" + PMD.EOL);
buffer.append(xpathQueryArea.getText() + PMD.EOL);
buffer.append("]]>" + PMD.EOL);
buffer.append(" </value>" + PMD.EOL);
buffer.append(" </property>" + PMD.EOL);
buffer.append(" </properties>" + PMD.EOL);
}
buffer.append(" <priority>3</priority>" + PMD.EOL);
buffer.append(" <example>" + PMD.EOL);
buffer.append("<![CDATA[" + PMD.EOL);
buffer.append(codeEditorPane.getText());
buffer.append("]]>" + PMD.EOL);
buffer.append(" </example>" + PMD.EOL);
buffer.append("</rule>" + PMD.EOL);
ruleXMLArea.setText(buffer.toString());
repaint();
}
}

View File

@ -48,6 +48,7 @@ import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.io.StringReader;
@ -457,13 +458,12 @@ public class Designer implements ClipboardOwner {
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
frame.setSize(screenHeight - (screenHeight / 4), screenHeight - (screenHeight / 4));
frame.setLocation((screenWidth / 2) - frame.getWidth() / 2, (screenHeight / 2) - frame.getHeight() / 2);
frame.setVisible(true);
frame.pack();
frame.show();
frame.setSize((screenWidth*3/4),(screenHeight*3/4));
frame.setLocation((screenWidth -frame.getWidth()) / 2, (screenHeight - frame.getHeight()) / 2);
frame.setVisible(true);
resultsSplitPane.setDividerLocation(resultsSplitPane.getMaximumDividerLocation() - (resultsSplitPane.getMaximumDividerLocation() / 2));
//containerSplitPane.setDividerLocation(containerSplitPane.getMaximumDividerLocation() / 2);
containerSplitPane.setDividerLocation(containerSplitPane.getMaximumDividerLocation() / 2);
}
private JMenuBar createMenuBar() {
@ -501,82 +501,24 @@ public class Designer implements ClipboardOwner {
}
private void createRuleXML() {
JPanel rulenamePanel = new JPanel();
rulenamePanel.setLayout(new FlowLayout());
rulenamePanel.add(new JLabel("Rule name"));
final JTextField rulenameField = new JTextField(30);
rulenamePanel.add(rulenameField);
JPanel rulemsgPanel = new JPanel();
rulemsgPanel.setLayout(new FlowLayout());
rulemsgPanel.add(new JLabel("Rule msg"));
final JTextField rulemsgField = new JTextField(60);
rulemsgPanel.add(rulemsgField);
JPanel ruledescPanel = new JPanel();
ruledescPanel.setLayout(new FlowLayout());
ruledescPanel.add(new JLabel("Rule desc"));
final JTextField ruledescField = new JTextField(60);
ruledescPanel.add(ruledescField);
JPanel ruleXMLPanel = new JPanel();
final JTextArea ruleXMLArea = new JTextArea(30, 50);
makeTextComponentUndoable(ruleXMLArea);
ruleXMLPanel.add(ruleXMLArea);
JButton go = new JButton("Create rule XML");
go.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StringBuffer sb = new StringBuffer();
sb.append("<rule name=\"" + rulenameField.getText() + "\"" + PMD.EOL);
sb.append(" message=\"" + rulemsgField.getText() + "\"" + PMD.EOL);
sb.append(" class=\"" + (xpathQueryArea.getText().length() == 0 ? "" : "net.sourceforge.pmd.rules.XPathRule") + "\">" + PMD.EOL);
sb.append(" <description>" + PMD.EOL);
sb.append(" " + ruledescField.getText() + PMD.EOL);
sb.append(" </description>" + PMD.EOL);
if (xpathQueryArea.getText().length() != 0) {
sb.append(" <properties>" + PMD.EOL);
sb.append(" <property name=\"xpath\">" + PMD.EOL);
sb.append(" <value>" + PMD.EOL);
sb.append("<![CDATA[" + PMD.EOL);
sb.append(xpathQueryArea.getText() + PMD.EOL);
sb.append("]]>" + PMD.EOL);
sb.append(" </value>" + PMD.EOL);
sb.append(" </property>" + PMD.EOL);
sb.append(" </properties>" + PMD.EOL);
}
sb.append(" <priority>3</priority>" + PMD.EOL);
sb.append(" <example>" + PMD.EOL);
sb.append("<![CDATA[" + PMD.EOL);
sb.append(codeEditorPane.getText());
sb.append("]]>" + PMD.EOL);
sb.append(" </example>" + PMD.EOL);
sb.append("</rule>" + PMD.EOL);
ruleXMLArea.setText(sb.toString());
}
});
JPanel fieldsPanel = new JPanel();
fieldsPanel.setLayout(new BorderLayout());
fieldsPanel.add(rulenamePanel, BorderLayout.NORTH);
fieldsPanel.add(rulemsgPanel, BorderLayout.CENTER);
fieldsPanel.add(ruledescPanel, BorderLayout.SOUTH);
JPanel fieldBtnPanel = new JPanel();
fieldBtnPanel.setLayout(new BorderLayout());
fieldBtnPanel.add(fieldsPanel, BorderLayout.NORTH);
fieldBtnPanel.add(go, BorderLayout.SOUTH);
JPanel outer = new JPanel(new BorderLayout());
outer.add(fieldBtnPanel, BorderLayout.NORTH);
outer.add(ruleXMLPanel, BorderLayout.SOUTH);
JDialog d = new JDialog(frame);
d.setSize(200, 300);
d.getContentPane().add(outer);
CreateXMLRulePanel rulePanel = new CreateXMLRulePanel(xpathQueryArea, codeEditorPane);
JFrame xmlframe = new JFrame("Create XML Rule");
xmlframe.setContentPane(rulePanel);
xmlframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
xmlframe.setSize(new Dimension(600, 700));
xmlframe.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(ComponentEvent e) {
JFrame tmp = (JFrame)e.getSource();
if (tmp.getWidth()<600 || tmp.getHeight()<700) {
tmp.setSize(600, 700);
}
}
});
int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
d.setLocation((screenWidth / 2) - frame.getWidth() / 2, (screenHeight / 2) - frame.getHeight() / 2);
d.setVisible(true);
d.pack();
d.show();
xmlframe.pack();
xmlframe.setLocation((screenWidth - xmlframe.getWidth()) / 2, (screenHeight - xmlframe.getHeight()) / 2);
xmlframe.setVisible(true);
}
private JComponent createASTPanel() {