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