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          ruledescField.setLineWrap(true);
77          gbl.setConstraints(ruledescField, gbc);
78          add(ruledescField);
79  
80          gbc.gridx = 0;
81          gbc.gridy = 3;
82          gbc.gridwidth = 2;
83          gbc.anchor = GridBagConstraints.NORTH;
84          JButton createRuleBtn = new JButton("Create rule XML");
85          createRuleBtn.addActionListener(this);
86          gbl.setConstraints(createRuleBtn, gbc);
87          add(createRuleBtn);
88  
89          gbc.gridx = 0;
90          gbc.gridy = 4;
91          gbc.anchor = GridBagConstraints.NORTH;
92          gbc.fill = GridBagConstraints.BOTH;
93          gbc.weightx = 1.0;
94          gbc.weighty = 1.0;
95          JScrollPane ruleXMLPane = new JScrollPane(ruleXMLArea);
96          gbl.setConstraints(ruleXMLPane, gbc);
97          add(ruleXMLPane);
98  
99          repaint();
100     }
101 
102     private void appendLn(StringBuffer sb, String text) {
103         sb.append(text).append(PMD.EOL);
104     }
105 
106     /**
107      * We let our class implement the ActionListener interface
108      * and use it to generate the xml code when the user presses
109      * the "Create rule XML" button.
110      *
111      */
112     public void actionPerformed(ActionEvent exception) {
113         StringBuffer buffer = new StringBuffer(200);
114         appendLn(buffer, "<rule  name=\"" + rulenameField.getText() + '\"');
115         appendLn(buffer, "  message=\"" + rulemsgField.getText() + '\"');
116         appendLn(buffer, "  class=\"" + (xpathQueryArea.getText().length() == 0 ? "" : "net.sourceforge.pmd.rules.XPathRule") + "\">");
117         appendLn(buffer, "  <description>");
118         appendLn(buffer, "  " + ruledescField.getText());
119         appendLn(buffer, "  </description>");
120         if (xpathQueryArea.getText().length() != 0) {
121             appendLn(buffer, "  <properties>");
122             appendLn(buffer, "    <property name=\"xpath\">");
123             appendLn(buffer, "    <value>");
124             appendLn(buffer, "<![CDATA[");
125             appendLn(buffer, xpathQueryArea.getText());
126             appendLn(buffer, "]]>");
127             appendLn(buffer, "    </value>");
128             appendLn(buffer, "    </property>");
129             appendLn(buffer, "  </properties>");
130         }
131         appendLn(buffer, "  <priority>3</priority>");
132         appendLn(buffer, "  <example>");
133         appendLn(buffer, "<![CDATA[");
134         appendLn(buffer, codeEditorPane.getText());
135         appendLn(buffer, "]]>");
136         appendLn(buffer, "  </example>");
137         appendLn(buffer, "</rule>");
138 
139         ruleXMLArea.setText(buffer.toString());
140         repaint();
141     }
142 }