View Javadoc

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