KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > util > designer > CreateXMLRulePanel


1 package net.sourceforge.pmd.util.designer;
2
3 import java.awt.GridBagConstraints JavaDoc;
4 import java.awt.GridBagLayout JavaDoc;
5 import java.awt.event.ActionEvent JavaDoc;
6 import java.awt.event.ActionListener JavaDoc;
7
8 import javax.swing.JButton JavaDoc;
9 import javax.swing.JLabel JavaDoc;
10 import javax.swing.JPanel JavaDoc;
11 import javax.swing.JScrollPane JavaDoc;
12 import javax.swing.JTextArea JavaDoc;
13 import javax.swing.JTextField JavaDoc;
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 JavaDoc implements ActionListener JavaDoc{
22     
23     private JTextField JavaDoc rulenameField = new JTextField JavaDoc(30);
24     private JTextField JavaDoc rulemsgField = new JTextField JavaDoc(30);
25     private JTextArea JavaDoc ruledescField = new JTextArea JavaDoc(5,30);
26     private JTextArea JavaDoc ruleXMLArea = new JTextArea JavaDoc(30, 30);
27     private JTextArea JavaDoc xpathQueryArea = new JTextArea JavaDoc();
28     private CodeEditorTextPane codeEditorPane = new CodeEditorTextPane();
29     
30     public CreateXMLRulePanel(JTextArea JavaDoc xpathQueryArea, CodeEditorTextPane codeEditorPane){
31         super();
32         this.xpathQueryArea = xpathQueryArea;
33         this.codeEditorPane = codeEditorPane;
34         GridBagConstraints JavaDoc gbc = new GridBagConstraints JavaDoc();
35         // We use a gridbaglayout for a nice and sturdy look and feel
36
GridBagLayout JavaDoc gbl = new GridBagLayout JavaDoc();
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 JavaDoc rulenameLabel = new JLabel JavaDoc("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 JavaDoc rulemsgLabel = new JLabel JavaDoc("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 JavaDoc ruledescLabel = new JLabel JavaDoc("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 JavaDoc createRuleBtn = new JButton JavaDoc("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 JavaDoc ruleXMLPane = new JScrollPane JavaDoc(ruleXMLArea);
95         gbl.setConstraints(ruleXMLPane, gbc);
96         add(ruleXMLPane);
97         
98         repaint();
99     }
100
101     private void appendLn(StringBuffer JavaDoc sb, String JavaDoc 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 JavaDoc exception) {
112         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(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
144
Popular Tags