View Javadoc

    1   package net.sourceforge.pmd.util.designer;
    2
    3   import java.awt.GridBagConstraints;
    4   import java.awt.GridBagLayout;
    5   import java.awt.event.ActionEvent;
    6   import java.awt.event.ActionListener;
    7
    8   import javax.swing.JButton;
    9   import javax.swing.JLabel;
    10  import javax.swing.JPanel;
    11  import javax.swing.JScrollPane;
    12  import javax.swing.JTextArea;
    13  import javax.swing.JTextField;
    14
    15  import net.sourceforge.pmd.PMD;
    16
    17  /***
    18   * This class is responsible for creating the
    19   * contentpanel for the Create Rule XML Frame.
    20   */
    21  public class CreateXMLRulePanel extends JPanel implements ActionListener{
    22
    23  	private JTextField rulenameField = new JTextField(30);
    24  	private JTextField rulemsgField = new JTextField(30);
    25  	private JTextArea ruledescField = new JTextArea(5,30);
    26  	private JTextArea ruleXMLArea = new JTextArea(30, 30);
    27  	private JTextArea xpathQueryArea = new JTextArea();
    28  	private CodeEditorTextPane codeEditorPane = new CodeEditorTextPane();
    29
    30  	public CreateXMLRulePanel(JTextArea xpathQueryArea, CodeEditorTextPane codeEditorPane){
    31  		super();
    32  		this.xpathQueryArea = xpathQueryArea;
    33  		this.codeEditorPane = codeEditorPane;
    34  		GridBagConstraints gbc = new GridBagConstraints();
    35  		// We use a gridbaglayout for a nice and sturdy look and feel
    36  		GridBagLayout gbl = new GridBagLayout();
    37  	    setLayout(gbl);
    38
    39  	    gbc.gridx = 0;
    40  	    gbc.gridy = 0;
    41  	    gbc.fill = GridBagConstraints.NONE;
    42  	    gbc.anchor = GridBagConstraints.EAST;
    43  	    gbc.weightx = 0.5;
    44  	    JLabel rulenameLabel = new JLabel("Rule name : ");
    45  	    gbl.setConstraints(rulenameLabel, gbc);
    46  	    add(rulenameLabel);
    47  	    gbc.weightx = 0.5;
    48  	    gbc.anchor = GridBagConstraints.WEST;
    49  	    gbc.gridx = 1;
    50  	    gbl.setConstraints(rulenameField, gbc);
    51  	    add(rulenameField);
    52
    53  	    gbc.gridx = 0;
    54  	    gbc.gridy = 1;
    55  	    gbc.anchor = GridBagConstraints.EAST;
    56  	    gbc.weightx = 0.5;
    57  	    JLabel rulemsgLabel = new JLabel("Rule msg : ");
    58  	    gbl.setConstraints(rulemsgLabel, gbc);
    59  	    add(rulemsgLabel);
    60  	    gbc.gridx = 1;
    61  	    gbc.anchor = GridBagConstraints.WEST;
    62  	    gbc.weightx = 0.5;
    63  	    gbl.setConstraints(rulemsgField, gbc);
    64  	    add(rulemsgField);
    65
    66  	    gbc.gridx = 0;
    67  	    gbc.gridy = 2;
    68  	    gbc.anchor = GridBagConstraints.EAST;
    69  	    gbc.weightx = 0.5;
    70  	    JLabel ruledescLabel = new JLabel("Rule desc : ");
    71  	    gbl.setConstraints(ruledescLabel,gbc);
    72  	    add(ruledescLabel);
    73  	    gbc.gridx = 1;
    74  	    gbc.anchor = GridBagConstraints.WEST;
    75  	    gbc.weightx = 0.5;
    76  	    gbl.setConstraints(ruledescField,gbc);
    77  	    add(ruledescField);
    78
    79  	    gbc.gridx = 0;
    80  	    gbc.gridy = 3;
    81  	    gbc.gridwidth = 2;
    82  	    gbc.anchor = GridBagConstraints.NORTH;
    83  	    JButton createRuleBtn = new JButton("Create rule XML");
    84  	    createRuleBtn.addActionListener(this);
    85  	    gbl.setConstraints(createRuleBtn, gbc);
    86  	    add(createRuleBtn);
    87
    88  	    gbc.gridx = 0;
    89  	    gbc.gridy = 4;
    90  	    gbc.anchor = GridBagConstraints.NORTH;
    91  	    gbc.fill = GridBagConstraints.BOTH;
    92  	    gbc.weightx = 1.0;
    93  	    gbc.weighty = 1.0;
    94  	    JScrollPane ruleXMLPane = new JScrollPane(ruleXMLArea);
    95  	    gbl.setConstraints(ruleXMLPane, gbc);
    96  	    add(ruleXMLPane);
    97
    98  	    repaint();
    99  	}
    100
    101 	private void appendLn(StringBuffer sb, String text) {
    102 		sb.append(text).append(PMD.EOL);
    103 	}
    104
    105 	/***
    106 	 * We let our class implement the ActionListener interface
    107 	 * and use it to generate the xml code when the user presses
    108 	 * the "Create rule XML" button.
    109 	 *
    110 	 */
    111 	public void actionPerformed(ActionEvent exception) {
    112 	    StringBuffer buffer = new StringBuffer(200);
    113 	    appendLn(buffer, "<rule  name=\"" + rulenameField.getText() + '\"');
    114 	    appendLn(buffer, "  message=\"" + rulemsgField.getText() + '\"');
    115 	    appendLn(buffer, "  class=\"" + (xpathQueryArea.getText().length() == 0 ? "" : "net.sourceforge.pmd.rules.XPathRule") + "\">");
    116 	    appendLn(buffer, "  <description>");
    117 	    appendLn(buffer, "  " + ruledescField.getText());
    118 	    appendLn(buffer, "  </description>");
    119 	    if (xpathQueryArea.getText().length() != 0) {
    120 	    	appendLn(buffer, "  <properties>");
    121 	    	appendLn(buffer, "    <property name=\"xpath\">");
    122 	    	appendLn(buffer, "    <value>");
    123 	    	appendLn(buffer, "<![CDATA[");
    124 	    	appendLn(buffer, xpathQueryArea.getText());
    125 	    	appendLn(buffer, "]]>");
    126 	    	appendLn(buffer, "    </value>");
    127 	    	appendLn(buffer, "    </property>");
    128 	    	appendLn(buffer, "  </properties>");
    129 	    }
    130 	    appendLn(buffer, "  <priority>3</priority>");
    131 	    appendLn(buffer, "  <example>");
    132 	    appendLn(buffer, "<![CDATA[");
    133 	    appendLn(buffer, codeEditorPane.getText());
    134 	    appendLn(buffer, "]]>");
    135 	    appendLn(buffer, "  </example>");
    136 	    appendLn(buffer, "</rule>");
    137
    138 	    ruleXMLArea.setText(buffer.toString());
    139 	    repaint();
    140 	}
    141
    142 }
    143