KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jaggenerator > modules > BusinessMethod


1 /* Copyright (C) 2005 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jaggenerator.modules;
19
20 import com.finalist.jaggenerator.*;
21 import com.finalist.jag.util.TemplateString;
22
23 import java.util.*;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import javax.swing.*;
27 import javax.swing.table.DefaultTableModel JavaDoc;
28 import javax.swing.tree.*;
29 import javax.xml.parsers.*;
30
31 import org.w3c.dom.*;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35
36 /**
37  * Helper class for representing business methods.
38  *
39  * @author Rudie Ekkelenkamp - Finalist IT Group
40  * @version $Revision: 1.6 $, $Date: 2005/11/25 12:45:19 $
41  */

42 public class BusinessMethod extends DefaultMutableTreeNode implements JagBean {
43    
44    static Log log = LogFactory.getLog(BusinessMethod.class);
45     
46    private Session parentEntity;
47
48    /** Creates new form BeanForm */
49    public BusinessMethod(Session parent) {
50         initComponents();
51       parentEntity = parent;
52    }
53    
54    public BusinessMethod() {
55         initComponents();
56    }
57    
58    /** Use for building up the entity field gui component */
59    public BusinessMethod(Session parent,Element node) {
60         initComponents();
61       parentEntity = parent;
62       try {
63             NodeList rt = node.getElementsByTagName("return-type");
64             if (rt.getLength() == 1) {
65                String JavaDoc returnType = "";
66                if (rt.item(0).getFirstChild() != null) {
67                   returnType = rt.item(0).getFirstChild().getNodeValue();
68                }
69                log.debug("The return type is: " + returnType);
70                setReturnType(returnType);
71             }
72             NodeList mn = node.getElementsByTagName("method-name");
73             if (mn.getLength() == 1) {
74                String JavaDoc methodName = "";
75                if (mn.item(0).getFirstChild() != null) {
76                   methodName = mn.item(0).getFirstChild().getNodeValue();
77                }
78                log.debug("The method name is: " + methodName);
79                setMethodName(methodName);
80             }
81             NodeList md = node.getElementsByTagName("description");
82             if (md.getLength() == 1) {
83
84                String JavaDoc methodDescription = "";
85                if (md.item(0).getFirstChild() != null) {
86                   methodDescription = md.item(0).getFirstChild().getNodeValue();
87                }
88                log.debug("The method description is: " + methodDescription);
89                setDescription(methodDescription);
90             }
91             ArrayList businessArguments = new ArrayList();
92             NodeList params = node.getElementsByTagName("parameter");
93             for (int k = 0; k < params.getLength(); k++) {
94                BusinessArgument businessArgument = new BusinessArgument();
95                Element param = (Element) params.item(k);
96                NodeList type = param.getElementsByTagName("type");
97                if (type.getLength() == 1) {
98                   String JavaDoc baType = "";
99                   if (type.item(0).getFirstChild() != null) {
100                      baType = type.item(0).getFirstChild().getNodeValue();
101                   }
102                   log.debug("The param type is: " + baType);
103                   businessArgument.setType(baType);
104                }
105                NodeList name = param.getElementsByTagName("name");
106                if (name.getLength() == 1) {
107                   String JavaDoc baName = "";
108                   if (name.item(0).getFirstChild() != null) {
109                      baName = name.item(0).getFirstChild().getNodeValue();
110                   }
111                   log.debug("The param name is: " + baName);
112                   businessArgument.setName(baName);
113                }
114                businessArguments.add(businessArgument);
115             }
116             setArgumentList(businessArguments);
117             log.debug("Added a businessmethod to the list");
118       } catch (Exception JavaDoc e) {
119          e.printStackTrace();
120       }
121    }
122     
123     
124    public javax.swing.JPanel JavaDoc getPanel() {
125        return panel;
126    }
127
128    
129    public String JavaDoc getRefName() {
130        return getSignature();
131    }
132
133    
134    public String JavaDoc getSignature() {
135        StringBuffer JavaDoc signature = new StringBuffer JavaDoc();
136        signature.append(getReturnType());
137        signature.append(" ");
138        signature.append(getMethodName());
139        signature.append("(");
140        for(Iterator i = getArgumentList().iterator(); i.hasNext(); ) {
141            BusinessArgument next = (BusinessArgument) i.next();
142            signature.append(next.getType());
143            signature.append(" ");
144            signature.append(next.getName());
145            if(i.hasNext()) {
146                signature.append(",");
147            }
148        }
149        signature.append(")");
150        return signature.toString();
151    }
152
153    public void getXML(Element el) throws ParserConfigurationException {
154       Document doc = el.getOwnerDocument();
155         Element businessMethod = doc.createElement("business-method");
156
157         Element returnType = doc.createElement("return-type");
158         if (getReturnType() == null) {
159            returnType.appendChild(doc.createTextNode("void"));
160         } else {
161            returnType.appendChild(doc.createTextNode(getReturnType()));
162         }
163         businessMethod.appendChild(returnType);
164
165         Element methodName = doc.createElement("method-name");
166         String JavaDoc methodString = getMethodName();
167         if (methodString == null) {
168            methodString = "";
169         }
170         methodName.appendChild(doc.createTextNode(methodString));
171         businessMethod.appendChild(methodName);
172
173         Element desc = doc.createElement("description");
174         String JavaDoc descString = getDescription();
175         if (descString == null) {
176            descString = "";
177         }
178         desc.appendChild(doc.createTextNode(descString));
179         businessMethod.appendChild(desc);
180
181         for (int j = 0; j < getArgumentList().size(); j++) {
182            Element parameter = doc.createElement("parameter");
183            BusinessArgument arg = (BusinessArgument) getArgumentList().get(j);
184
185            Element type = doc.createElement("type");
186            String JavaDoc typeString = arg.getType();
187            if (typeString == null) {
188               typeString = "";
189            }
190
191            type.appendChild(doc.createTextNode(typeString));
192            parameter.appendChild(type);
193
194            Element pname = doc.createElement("name");
195            String JavaDoc nameString = arg.getName();
196            if (nameString == null) {
197               nameString = "";
198            }
199            pname.appendChild(doc.createTextNode(nameString));
200            parameter.appendChild(pname);
201
202            businessMethod.appendChild(parameter);
203         }
204         el.appendChild(businessMethod);
205    }
206     
207    
208     
209     /**
210      * the return type of the method.
211      * @return
212      */

213     public String JavaDoc getReturnType() {
214         return returnTypeInput.getText();
215     }
216     
217     /**
218      * Set the return type of the business method.
219      *
220      * @param returnType
221      */

222     public void setReturnType(String JavaDoc returnType) {
223         returnTypeInput.setText(returnType);
224     }
225     
226     /**
227      * Get the business method name.
228      *
229      * @return the name.
230      */

231     public String JavaDoc getMethodName() {
232         return methodNameInput.getText();
233     }
234     
235     /**
236      * Get the business method name in uppercase notation.
237      *
238      * @return the name.
239      */

240     public String JavaDoc getMethodNameUpper() {
241         String JavaDoc methodName = getMethodName();
242         if (methodName == null) {
243             return methodName;
244         }
245         if (methodName.length() > 1) {
246             return new String JavaDoc(methodName.substring(0, 1).toUpperCase() + methodName.substring(1));
247         }
248         return methodName.toUpperCase();
249         
250     }
251     
252     /**
253      * Set the method name.
254      *
255      * @param methodName the name.
256      */

257     public void setMethodName(String JavaDoc methodName) {
258         methodNameInput.setText(methodName);
259     }
260     
261     /**
262      * Get the method description.
263      * @return description.
264      */

265     public String JavaDoc getDescription() {
266         return descriptionInput.getText();
267     }
268     
269     /**
270      * Set the method description.
271      *
272      * @param description
273      */

274     public void setDescription(String JavaDoc description) {
275         descriptionInput.setText(description);
276     }
277     
278     /**
279      * get a list of all arguments of the BusinessArgument class.
280      *
281      * @return Collection of BusinessArgument classes.
282      */

283     public ArrayList getArgumentList() {
284         DefaultTableModel JavaDoc model = (DefaultTableModel JavaDoc)parametersTable.getModel();
285         
286         ArrayList argumentList = new ArrayList();
287         
288         for(int count = 0; count < model.getRowCount(); count++) {
289            BusinessArgument argument = new BusinessArgument();
290            if (model.getValueAt(count, 0) != null) {
291             argument.setName(model.getValueAt(count, 0).toString());
292            }
293            if (model.getValueAt(count, 1) != null) {
294             argument.setType(model.getValueAt(count, 1).toString());
295            }
296            argumentList.add(argument);
297         }
298         
299         return argumentList;
300     }
301     
302     /**
303      * Set the argument list.
304      * @param argumentList
305      */

306     public void setArgumentList(ArrayList argumentList) {
307         Object JavaDoc data[][] = new Object JavaDoc[argumentList.size()][2];
308         
309         for(int count = 0; count < argumentList.size(); count++) {
310             BusinessArgument arg = (BusinessArgument) argumentList.get(count);
311             data[count][0] = arg.getName();
312             data[count][1] = arg.getType();
313         }
314         
315         parametersTable.setModel(new javax.swing.table.DefaultTableModel JavaDoc(
316             data,
317             new String JavaDoc [] {
318                 "Parameter", "Type"
319             }
320         ));
321     }
322     
323     public String JavaDoc toString() {
324         return getRefName();
325     }
326
327
328    /** This method is called from within the constructor to
329     * initialize the form.
330     * WARNING: Do NOT modify this code. The content of this method is
331     * always regenerated by the Form Editor.
332     */

333     private void initComponents() {//GEN-BEGIN:initComponents
334
panel = new javax.swing.JPanel JavaDoc();
335         returnTypeLabel = new javax.swing.JLabel JavaDoc();
336         methodNameLabel = new javax.swing.JLabel JavaDoc();
337         descriptionLabel = new javax.swing.JLabel JavaDoc();
338         parametersLabel = new javax.swing.JLabel JavaDoc();
339         returnTypeInput = new javax.swing.JTextArea JavaDoc();
340         methodNameScrollPane = new javax.swing.JScrollPane JavaDoc();
341         methodNameInput = new javax.swing.JTextArea JavaDoc();
342         descriptionScrollPane = new javax.swing.JScrollPane JavaDoc();
343         descriptionInput = new javax.swing.JTextArea JavaDoc();
344         parametersScrollPane = new javax.swing.JScrollPane JavaDoc();
345         parametersTable = new javax.swing.JTable JavaDoc();
346         addParameterButton = new javax.swing.JButton JavaDoc();
347         deleteParameterButton = new javax.swing.JButton JavaDoc();
348
349         panel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
350
351         returnTypeLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
352         returnTypeLabel.setText("Return type:");
353         panel.add(returnTypeLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, 90, -1));
354
355         methodNameLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
356         methodNameLabel.setText("Method name:");
357         panel.add(methodNameLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 90, -1));
358
359         descriptionLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
360         descriptionLabel.setText("Description:");
361         panel.add(descriptionLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 100, 90, -1));
362
363         parametersLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
364         parametersLabel.setText("Parameters:");
365         panel.add(parametersLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 190, 90, -1));
366
367         returnTypeInput.setFont(new java.awt.Font JavaDoc("Microsoft Sans Serif", 0, 11));
368         returnTypeInput.setAutoscrolls(false);
369         returnTypeInput.setBorder(new javax.swing.border.EtchedBorder JavaDoc());
370         returnTypeInput.addKeyListener(new java.awt.event.KeyAdapter JavaDoc() {
371             public void keyReleased(java.awt.event.KeyEvent JavaDoc evt) {
372                 inputKeyReleased(evt);
373             }
374         });
375
376         panel.add(returnTypeInput, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 10, 260, -1));
377
378         methodNameScrollPane.setBorder(null);
379         methodNameInput.setFont(new java.awt.Font JavaDoc("Microsoft Sans Serif", 0, 11));
380         methodNameInput.setLineWrap(true);
381         methodNameInput.setWrapStyleWord(true);
382         methodNameInput.setBorder(new javax.swing.border.EtchedBorder JavaDoc());
383         methodNameScrollPane.setViewportView(methodNameInput);
384
385         panel.add(methodNameScrollPane, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 40, 260, 40));
386
387         descriptionScrollPane.setBorder(null);
388         descriptionScrollPane.setFocusable(false);
389         descriptionScrollPane.setHorizontalScrollBar(descriptionScrollPane.getHorizontalScrollBar());
390         descriptionInput.setFont(new java.awt.Font JavaDoc("Microsoft Sans Serif", 0, 11));
391         descriptionInput.setLineWrap(true);
392         descriptionInput.setWrapStyleWord(true);
393         descriptionInput.setBorder(new javax.swing.border.EtchedBorder JavaDoc());
394         descriptionScrollPane.setViewportView(descriptionInput);
395
396         panel.add(descriptionScrollPane, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 100, 260, 80));
397
398         parametersTable.setModel(new javax.swing.table.DefaultTableModel JavaDoc(
399             new Object JavaDoc [][] {
400
401             },
402             new String JavaDoc [] {
403                 "Parameter", "Type"
404             }
405         ));
406         parametersScrollPane.setViewportView(parametersTable);
407
408         panel.add(parametersScrollPane, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 190, 260, 110));
409
410         addParameterButton.setText("Add");
411         addParameterButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
412             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
413                 addParameterButtonActionPerformed(evt);
414             }
415         });
416
417         panel.add(addParameterButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 310, -1, -1));
418
419         deleteParameterButton.setText("Delete");
420         deleteParameterButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
421             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
422                 deleteParameterButtonActionPerformed(evt);
423             }
424         });
425
426         panel.add(deleteParameterButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 310, -1, -1));
427
428     }//GEN-END:initComponents
429

430     private void inputKeyReleased(java.awt.event.KeyEvent JavaDoc evt) {//GEN-FIRST:event_inputKeyReleased
431
JagGenerator.stateChanged(false);
432     }//GEN-LAST:event_inputKeyReleased
433

434     private void deleteParameterButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_deleteParameterButtonActionPerformed
435
int selectedRows[] = parametersTable.getSelectedRows();
436         
437         ArrayList argumentList = getArgumentList();
438         for(int count = selectedRows.length -1; count >= 0; count--) {
439             int deleteCol = selectedRows[count];
440             System.out.println("deleting: "+deleteCol);
441             
442             argumentList.remove(deleteCol);
443         }
444         setArgumentList(argumentList);
445       JagGenerator.stateChanged(false);
446         
447     }//GEN-LAST:event_deleteParameterButtonActionPerformed
448

449     private void addParameterButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addParameterButtonActionPerformed
450
BusinessArgument ba = new BusinessArgument();
451         ArrayList argumentList = getArgumentList();
452         argumentList.add(ba);
453         setArgumentList(argumentList);
454       JagGenerator.stateChanged(false);
455         
456     }//GEN-LAST:event_addParameterButtonActionPerformed
457

458    private void refNameTextFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_refNameTextFocusLost
459
JagGenerator.stateChanged(false);
460    }//GEN-LAST:event_refNameTextFocusLost
461

462    private void rootPackageTextFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_rootPackageTextFocusLost
463
JagGenerator.stateChanged(false);
464    }//GEN-LAST:event_rootPackageTextFocusLost
465

466    private void descriptionTextFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_descriptionTextFocusLost
467
JagGenerator.stateChanged(false);
468    }//GEN-LAST:event_descriptionTextFocusLost
469

470    private void nameTextFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_nameTextFocusLost
471
JagGenerator.stateChanged(false);
472
473    }//GEN-LAST:event_nameTextFocusLost
474

475
476    private void addButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addButtonActionPerformed
477
}//GEN-LAST:event_addButtonActionPerformed
478

479    
480
481    private void removeButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonActionPerformed
482
}//GEN-LAST:event_removeButtonActionPerformed
483

484     // Variables declaration - do not modify//GEN-BEGIN:variables
485
private javax.swing.JButton JavaDoc addParameterButton;
486     private javax.swing.JButton JavaDoc deleteParameterButton;
487     private javax.swing.JTextArea JavaDoc descriptionInput;
488     private javax.swing.JLabel JavaDoc descriptionLabel;
489     private javax.swing.JScrollPane JavaDoc descriptionScrollPane;
490     private javax.swing.JTextArea JavaDoc methodNameInput;
491     private javax.swing.JLabel JavaDoc methodNameLabel;
492     private javax.swing.JScrollPane JavaDoc methodNameScrollPane;
493     private javax.swing.JPanel JavaDoc panel;
494     private javax.swing.JLabel JavaDoc parametersLabel;
495     private javax.swing.JScrollPane JavaDoc parametersScrollPane;
496     private javax.swing.JTable JavaDoc parametersTable;
497     private javax.swing.JTextArea JavaDoc returnTypeInput;
498     private javax.swing.JLabel JavaDoc returnTypeLabel;
499     // End of variables declaration//GEN-END:variables
500
}
501
Popular Tags