View Javadoc

1   /*
2       Jiffie Plugin for Jameleon - An Internet Explorer plug-in for Jameleon
3       Copyright (C) 2004-2006 Christian W. Hargraves (engrean@hotmail.com) and
4                          Matthias Marschall (matthias@marschalls.de)
5   
6       This program is free software; you can redistribute it and/or modify
7       it under the terms of the GNU General Public License as published by
8       the Free Software Foundation; either version 2 of the License, or
9       (at your option) any later version.
10  
11      This program is distributed in the hope that it will be useful,
12      but WITHOUT ANY WARRANTY; without even the implied warranty of
13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14      GNU General Public License for more details.
15  
16      You should have received a copy of the GNU General Public License
17      along with this program; if not, write to the Free Software
18      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20  package net.sf.jameleon.plugin.jiffie.ui;
21  
22  import java.awt.event.ActionEvent;
23  import java.awt.event.ActionListener;
24  import java.io.File;
25  import java.util.HashMap;
26  import java.util.LinkedList;
27  import java.util.List;
28  import java.util.Map;
29  
30  import javax.swing.JButton;
31  import javax.swing.JComboBox;
32  import javax.swing.JFrame;
33  import javax.swing.JLabel;
34  import javax.swing.JOptionPane;
35  import javax.swing.JTextField;
36  import javax.swing.SpringLayout;
37  import javax.swing.table.DefaultTableModel;
38  
39  import net.sf.jameleon.ui.SpringUtilities;
40  import net.sf.jameleon.util.Configurator;
41  import net.sf.jameleon.util.TemplateProcessor;
42  
43  
44  public class IECodeGenerator {
45  
46      private DefaultTableModel formFields;
47      privateong> JTextField packageF;
48      private JTextField sourceDirF;
49      private JTextField classF;
50      private JTextField tagNameF;
51      private JComboBox templatesCB;
52      private JButton generateCode;
53      private String formIndex;
54      private JFrame options;
55      private Configurator config;
56  
57  
58      public IECodeGenerator(DefaultTableModel formFields, String formIndex){
59          this.formFields = formFields;
60          this.formIndex = formIndex;
61          config = Configurator.getInstance();
62          createGenActionPointUI();
63      }
64  
65      private void createGenActionPointUI(){
66          options = new JFrame("Action Point Options");
67          options.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
68          SpringLayout optionsLO = new SpringLayout();
69          options.getContentPane().setLayout(optionsLO);
70  
71          JLabel packageL = new JLabel("package");
72          packageF = new JTextField();
73          packageL.setLabelFor(packageF);
74          packageF.setColumns(30);
75  
76          options.getContentPane().add(packageL);
77          options.getContentPane().add(packageF);
78  
79          JLabel sourceDirL = new JLabel("Source Directory");
80          String sourceDir = config.getValue("sourceDir");
81          sourceDirF = new JTextField(sourceDir);
82          sourceDirL.setLabelFor(sourceDirF);
83          sourceDirF.setColumns(30);
84  
85          options.getContentPane().add(sourceDirL);
86          options.getContentPane().add(sourceDirF);
87  
88          JLabel classL = new JLabel("Class Name");
89          classF = new JTextField();
90          classL.setLabelFor(packageF);
91          classF.setColumns(30);
92  
93          options.getContentPane().add(classL);
94          options.getContentPane().add(classF);
95  
96          JLabel tagNameL = new JLabel("Tag Name");
97          tagNameF = new JTextField();
98          tagNameL.setLabelFor(tagNameF);
99          tagNameF.setColumns(30);
100         tagNameF.setToolTipText("The name to be use in your test case script.");
101 
102         options.getContentPane().add(tagNameL);
103         options.getContentPane().add(tagNameF);
104 
105         JLabel templateL = new JLabel("Choose a Template");
106         templatesCB = new JComboBox();
107         templateL.setLabelFor(templatesCB);
108         templateL.setToolTipText("Select a file type to generate.");
109 
110         populateTemplates();
111         if (templatesCB.getItemCount() > 0) {
112             options.getContentPane().add(templateL);
113             options.getContentPane().add(templatesCB);
114 
115             generateCode = new JButton("Generate Code");
116             generateCode.setToolTipText("Generate an Action Point Java file based on the above settings");
117             addActionListenerToButton();
118             JLabel spacer = new JLabel();
119 
120             options.getContentPane().add(generateCode);
121             options.getContentPane().add(spacer);
122 
123             SpringUtilities.makeCompactGrid(options.getContentPane(),
124                                             6, 2,   //rows, cols
125                                             6, 6,   //initX, initY
126                                             6, 6);  //xPad, yPad
127             options.pack();
128             options.setVisible(true);
129         }
130         Configurator.clearInstance();
131     }
132 
133     private void populateTemplates(){
134         String templateNames = config.getValue("ui.generator.template");
135         if (templateNames == null || templateNames.length() == 0) {
136             JOptionPane.showMessageDialog(
137                 options,
138                 "Please set ui.generator.template in jameleon-gui.properties!",
139                 "Please set ui.generator.template in jameleon-gui.properties!",
140                 JOptionPane.ERROR_MESSAGE);            
141         }else{
142             String[] templates = templateNames.split(" ");
143             TemplateType t;
144             String name, fileName;
145             for (int i = 0; i < templates.length; i++) {
146                 name = config.getValue(templates[i]+".template.fieldValue");
147                 fileName = config.getValue(templates[i]+".template.file");
148                 if ( name != null && 
149                      name.length() > 0 && 
150                      fileName != null && 
151                      fileName.length() > 0) {
152                     t = new TemplateType(name, fileName);
153                     templatesCB.addItem(t);
154                 }else{
155                     System.out.println("name :"+name);
156                     System.out.println("file :"+fileName);
157                 }
158             }
159         }
160     }
161 
162     private void addActionListenerToButton(){
163         generateCode.addActionListener(
164             new ActionListener(){
165                 public void actionPerformed(ActionEvent e) {
166                     String sourceDirS = sourceDirF.getText();
167                     String packageS = packageF.getText();
168                     String classS = classF.getText();
169                     String tagNameS = tagNameF.getText();
170                     List fields = new LinkedList();
171                     IEField field;
172                     for (int i = 0; i < formFields.getRowCount(); i++) {
173                         if (((Boolean)formFields.getValueAt(i, IEActionPointGenerator.INCLUDE)).booleanValue()) {
174                             field = new IEField();
175                             field.name = (String)formFields.getValueAt(i,IEActionPointGenerator.FIELD_NAME);
176                             field.type = (String)formFields.getValueAt(i,IEActionPointGenerator.FIELD_TYPE);
177                             field.varName = (String)formFields.getValueAt(i, IEActionPointGenerator.VAR_NAME);
178                             if (field.varName == null || field.varName.length() == 0) {
179                                 field.varName = field.name;
180                             }
181                             field.description = (String)formFields.getValueAt(i,IEActionPointGenerator.VAR_DESC);
182                             if (field.description == null || field.description.length() == 0) {
183                                 field.description = field.name;
184                             }
185                             field.required = (Boolean)formFields.getValueAt(i,IEActionPointGenerator.REQUIRED);
186                             fields.add(field);
187                         }
188                     }
189                     genActionPoint(sourceDirS, packageS, classS, tagNameS, fields);
190                 }
191             });
192     }
193 
194     private void genActionPoint(
195         String sourceDirS, 
196         String packageS, 
197         String classS, 
198         String tagNameS, 
199         List fields){
200 
201         File dir = new File(new File(sourceDirS), packageS.replace('.', File.separatorChar));
202         //String filename = getClass().getResource("/IEActionPointTemplate.txt").toExternalForm();
203         TemplateType temp = (TemplateType)templatesCB.getSelectedItem();
204         String filename = temp.fileName;
205 
206         File sourceFile = new File(dir, classS+".java");
207         TemplateProcessor tcdt = new TemplateProcessor(filename);
208         Map context = new HashMap();
209         context.put( "package", packageS );
210         context.put( "className", classS );
211         context.put( "tagName", tagNameS );
212         context.put( "fields", fields );
213         context.put( "formIndex", formIndex );
214         tcdt.transform(sourceFile, context);
215 
216         JOptionPane.showMessageDialog(
217             options,
218             sourceFile.getPath() + " was generated successfully!",
219             "Generation Successful",
220             JOptionPane.PLAIN_MESSAGE);
221 
222 
223     }
224 
225     public class IEField{
226         public String name;
227         public String varName;
228         public String type;
229         public String description;
230         public Boolean required;
231         public String defaultValue;
232 
233         public String getName(){
234             return name;
235         }
236 
237         public String getVarName(){
238             return varName;
239         }
240 
241         public String getType(){
242             return type;
243         }
244 
245         public String getDescription(){
246             return description;
247         }
248 
249         public Boolean getRequired(){
250             return required;
251         }
252 
253         public String getDefaultValue(){
254             return defaultValue;
255         }
256 
257     }
258 
259     public class TemplateType{
260         public String name;
261         public String fileName;
262 
263         public TemplateType(String name, String fileName){
264             this.name = name;
265             this.fileName = fileName;
266         }
267 
268         public String toString(){
269             return name;
270         }
271     }
272 
273 }