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       This program 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       This program 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 this program; if not, write to the Free Software
15      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17  package net.sf.jameleon.plugin.jiffie.ui;
18  import java.awt.BorderLayout;
19  import java.awt.event.ActionEvent;
20  import java.awt.event.ActionListener;
21  import java.awt.event.MouseEvent;
22  import java.awt.event.MouseListener;
23  import java.awt.event.WindowAdapter;
24  import java.awt.event.WindowEvent;
25  import java.util.HashSet;
26  import java.util.Iterator;
27  import java.util.Set;
28  import java.util.Vector;
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.JPanel;
35  import javax.swing.JScrollPane;
36  import javax.swing.JTable;
37  import javax.swing.JTextField;
38  import javax.swing.SwingUtilities;
39  import javax.swing.table.DefaultTableModel;
40  
41  import net.sf.jameleon.plugin.jiffie.util.BrowserCache;
42  import net.sf.jameleon.plugin.jiffie.util.DocumentDelegate;
43  import net.sf.jameleon.plugin.jiffie.util.IHTMLElementFinder;
44  
45  import net.sf.jiffie.ElementList;
46  import net.sf.jiffie.IHTMLDocument2;
47  import net.sf.jiffie.IHTMLElement;
48  import net.sf.jiffie.IHTMLFormElement;
49  import net.sf.jiffie.IHTMLInputElement;
50  import net.sf.jiffie.InternetExplorer;
51  import net.sf.jiffie.JiffieException;
52  import net.sf.jiffie.JiffieUtility;
53  
54  public class IEActionPointGenerator implements DocumentDelegate, BrowserCache {
55      private String currentFormStyle;
56      private String currentFieldStyle;
57      private IHTMLElement currentField = null;
58      private IHTMLFormElement currentForm;
59      private InternetExplorer ie;
60      private IHTMLElementFinder elementFinder;
61      private DefaultTableModel formInfo;
62      private JComboBox formOptions;
63      private Vector browsers;
64      private static final boolean WAIT = true;
65      private static final String FORM_CSS = "border : 5px solid #FF00FF;";
66      private static final String FIELD_CSS = "background : #CCFF33;";
67      public static final int FIELD_NAME = 0;
68      public static final int FIELD_TYPE = 1;
69      public static final int VAR_NAME   = 2;
70      public static final int VAR_DESC   = 3;
71      public static final int REQUIRED   = 4;
72      public static final int INCLUDE    = 5;
73      private WindowAdapter externalCloseListener;
74      public IEActionPointGenerator(){
75          elementFinder = new IHTMLElementFinder(this);
76          browsers = new Vector();
77          JiffieUtility.setEventHandlerFactory(new JiffieUIEventHandlerFactory());
78          createNewIE();
79          ie.setVisible(false);
80      }
81      public void setExternalCloseListener(WindowAdapter cl){
82          externalCloseListener = cl;
83      }
84      public static void main (String args[]) {
85          final IEActionPointGenerator agp = new IEActionPointGenerator();
86          try {
87              SwingUtilities.invokeLater(new Runnable() {
88                  public void run() {
89                      agp.genGUI();
90                  }
91              });
92          } catch (Throwable t) {
93              t.printStackTrace();
94          }
95      }
96      public IHTMLDocument2 getDocument(){
97          IHTMLDocument2 doc = null;
98          try {
99              if (ie != null) {
100                 doc = ie.getDocument(WAIT);
101             }
102         } catch (JiffieException je) {
103             je.printStackTrace();
104         }
105         return doc;
106     }
107     public IHTMLFormElement getWorkingForm(){
108         return currentForm;
109     }
110     public boolean highlightActiveElement(){
111         return false;
112     }
113     public void genGUI(){
114         final JFrame formsWindow = new JFrame("Jiffie Plug-in Action Point Generator");
115         formsWindow.getContentPane().setLayout(new BorderLayout());
116         formsWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
117         formsWindow.addWindowListener(new WindowAdapter(){
118                 public void windowClosing(WindowEvent e) {
119                     ie.release();
120                     formsWindow.dispose();
121                 }
122                 });
123         if (externalCloseListener != null) {
124             formsWindow.addWindowListener(externalCloseListener);
125         }
126         JPanel north = new JPanel(new BorderLayout());
127         JPanel urlPanel = new JPanel(new BorderLayout());
128         JLabel urlLabel = new JLabel("URL");
129         final JTextField url = new JTextField();
130         JButton go = new JButton("Go");
131         url.setColumns(40);
132         urlLabel.setLabelFor(url);
133         urlPanel.add(urlLabel, BorderLayout.WEST);
134         urlPanel.add(url, BorderLayout.CENTER);
135         urlPanel.add(go, BorderLayout.EAST);
136         JLabel whichForm = new JLabel("Choose a form");
137         formOptions = new JComboBox();
138         whichForm.setLabelFor(formOptions);
139         addActionListenerForUrl(url, go);
140         north.add(urlPanel, BorderLayout.NORTH);
141         north.add(whichForm, BorderLayout.WEST);
142         north.add(formOptions, BorderLayout.EAST);
143         formInfo = new HTMLFormTableModel(
144                                         new Object[]{
145                                             "Field Name",
146                                             "Field Type",
147                                             "Variable Name",
148                                             "Variable Description",
149                                             "Required",
150                                             "Include"}
151                                         ,0);
152         final JTable formFields = new JTable(formInfo);
153         addMouseListenerToFormFields(formFields);
154         JPanel center = new JPanel(new BorderLayout());
155         JScrollPane formFieldsScrollPane = new JScrollPane(formFields);
156         center.add(formFieldsScrollPane, BorderLayout.NORTH);
157         JPanel south = new JPanel(new BorderLayout());
158         JButton genActionPoint = new JButton("Generate Action Point");
159         genActionPoint.setToolTipText("Generate an action point for the form selected");
160         addActionListenerToGenActionApoint(genActionPoint);
161         JButton getFields = new JButton("Get Fields");
162         addActionListenerToGetFieldButton(getFields, genActionPoint);
163         south.add(genActionPoint, BorderLayout.CENTER);
164         south.add(getFields, BorderLayout.EAST);
165         addActionFormSelectionActionListener(formOptions);
166         formsWindow.getContentPane().add(north, BorderLayout.NORTH);
167         formsWindow.getContentPane().add(center, BorderLayout.CENTER);
168         formsWindow.getContentPane().add(south, BorderLayout.SOUTH);
169         formsWindow.pack();
170         formsWindow.setVisible(true);
171     }
172     private void createGenActionPointUI(){
173         String formIndex = (String)formOptions.getSelectedItem();
174         new IECodeGenerator(formInfo, formIndex);
175     }
176     public InternetExplorer getCurrentIE(){
177         return ie;
178     }
179     public void setCurrentIE(InternetExplorer ie){
180         this.ie = ie;
181     }
182     private void createNewIE(){
183         ie = new InternetExplorer();
184         new IEListener(ie, this);
185     }
186     private void resetFieldStyle(){
187         if (currentField != null) {
188             try {
189                 currentField.getStyle().setStringProperty("cssText", currentFieldStyle);
190             } catch (Throwable t) {
191                 //This just means that the currentField is no longer available
192             }
193         }
194     }
195     private void addRowToTable(IHTMLFormElement form, DefaultTableModel formRows, Set fieldNames, String tag){
196         try {
197             ElementList fields = form.getElementListByTag(tag);
198             Iterator i = fields.iterator();
199             IHTMLElement field;
200             String name, type;
201 //WHAT ABOUT RADIO BUTTONS WITH THE SAME NAME BUT DIFFERENT VALUE?
202             Object[] columns = new Object[6];
203             columns[VAR_NAME] = "";
204             columns[VAR_DESC] = "";
205             columns[REQUIRED] = new Boolean(false);
206             columns[INCLUDE] = new Boolean(true);
207             while ( i.hasNext() ) {
208                 field = (IHTMLElement) i.next();
209                 name = field.getName();
210                 type = null;
211                 if ( "select".equals(tag) ) {
212                     type = "select";
213                 } else if ( "textarea".equals(tag) ) {
214                     type = "textarea";
215                 } else if ( "input".equals(tag) ) {
216                     IHTMLInputElement input = (IHTMLInputElement)field;
217                     type = input.getType();
218                 }
219                 if ( fieldNames.add(name) ) {
220                     columns[FIELD_NAME] = name;
221                     columns[VAR_NAME] = getVarName(name);
222                     columns[FIELD_TYPE] = type;
223                     if ( "text".equals(type)     ||
224                          "checkbox".equals(type) ||
225                          "password".equals(type) ||
226                          "radio".equals(type)    ||
227                          "select".equals(type)   ||
228                          "textarea".equals(type) ) {
229                         formRows.addRow(columns);
230                     }
231                 }
232             }
233         } catch ( JiffieException je ) {
234             je.printStackTrace();
235         }
236     }
237     private String getVarName(String fieldName){
238         String newField = "";
239         if (!fieldName.matches("^[a-zA-Z_][//w]+$")) {
240             newField = fieldName.replaceAll("[^//w]","_");
241         }
242         //If the field name doesn't start with a valid character
243         if ( ( (newField == null) && (!fieldName.matches("^[a-zA-Z_].*$")) ) ||
244              ( (newField.trim().length() > 0) && (!newField.matches("^[a-zA-Z_].*$")) ) ) {
245             if (newField == null) {
246                 newField = fieldName;
247             }
248             if (!newField.startsWith("_")) {
249                 newField = "_" + newField;
250             }
251             newField = "f" + newField;
252         }
253         return newField;
254     }
255     public void findFields(IHTMLFormElement form){
256         Set fieldNames = new HashSet();
257         resetFieldStyle();
258         currentField = null;
259         currentFieldStyle = null;
260         formInfo.setRowCount(0);
261         addRowToTable(form, formInfo, fieldNames, "input");
262         addRowToTable(form, formInfo, fieldNames, "select");
263         addRowToTable(form, formInfo, fieldNames, "textarea");
264     }
265     public void findForms(){
266         currentForm = null;
267         currentFormStyle = null;
268         resetFieldStyle();
269         currentField = null;
270         currentFieldStyle = null;
271         ElementList forms = elementFinder.getForms();
272         formInfo.setRowCount(0);
273         addFormsToList(formOptions, forms);
274     }
275     protected void addFormsToList(JComboBox formOptions, ElementList forms){
276         Iterator it = forms.iterator();
277         formOptions.removeAllItems();
278         int count = 0;
279         while ( it.hasNext() ) {
280             it.next();
281             formOptions.addItem(count +"");
282             count ++;
283         }
284     }
285     private void addActionListenerToGetFieldButton(final JButton getFields, final JButton genActionPoint){
286     getFields.addActionListener(
287                                new ActionListener(){
288                                    public void actionPerformed(ActionEvent e) {
289                                        String formIndex = (String)formOptions.getSelectedItem();
290                                        if ( formIndex != null && formIndex.length() > 0 ) {
291                                            IHTMLFormElement form = elementFinder.getFormWithIndex(Integer.parseInt(formIndex));
292                                            findFields(form);
293                                        }
294                                    }
295                                }
296                                );
297     }
298     private void addActionListenerToGenActionApoint(final JButton genActionPoint){
299         genActionPoint.addActionListener(
300                                         new ActionListener(){
301                                             public void actionPerformed(ActionEvent e) {
302                                                 createGenActionPointUI();
303                                             }
304                                         }
305                                         );
306     }
307     protected void addActionFormSelectionActionListener(final JComboBox formOptions){
308         formOptions.addActionListener(
309                                      new ActionListener(){
310                                          public void actionPerformed(ActionEvent e) {
311                                              String formId = (String)formOptions.getSelectedItem();
312                                              if ( formId != null && formId.length() > 0 ) {
313                                                  int index = Integer.parseInt(formId);
314                                                  try {
315                                                      if ( currentForm != null ) {
316                                                          currentForm.getStyle().setStringProperty("cssText", currentFormStyle);
317                                                          //currentForm.setInnerHtml(currentFormHtml);
318                                                      }
319                                                      currentForm = elementFinder.getFormWithIndex(index);
320                                                      currentFormStyle = currentForm.getStyle().getStringProperty("cssText");
321                                                      //currentFormHtml = currentForm.getInnerHtml();
322                                                      currentForm.getStyle().setStringProperty("cssText", FORM_CSS);
323                                                      //currentForm.setInnerHtml("<table style='border : 2px solid #FF00FF;'><tr><td>"+currentFormHtml+"</td></tr></table>");
324                                                      findFields(currentForm);
325                                                  } catch (Throwable t) {
326                                                      t.printStackTrace();
327                                                  }
328                                              }
329                                          }
330                                      }
331                                      );
332     }
333     private void addMouseListenerToFormFields(final JTable formFields){
334         formFields.addMouseListener(
335                                    new MouseListener(){
336                                        public void mouseClicked(MouseEvent e) {
337                                            int selectedRow = formFields.getSelectedRow();
338                                            resetFieldStyle();
339                                            String fieldName;
340                                            if (selectedRow >= 0) {
341                                                fieldName = (String)formInfo.getValueAt(selectedRow, FIELD_NAME);
342                                                try {
343                                                    currentField = (IHTMLElement)currentForm.getElementByName(fieldName);
344                                                    if (currentField != null) {
345                                                        currentFieldStyle = currentField.getStyle().getStringProperty("cssText");
346                                                        currentField.getStyle().setStringProperty("cssText", FIELD_CSS);
347                                                    }
348                                                } catch (JiffieException je) {
349                                                    je.printStackTrace();
350                                                }
351                                            }
352                                        }
353                                        public void mousePressed(MouseEvent e) {
354                                        }
355                                        public void mouseReleased(MouseEvent e) {
356                                        }
357                                        public void mouseEntered(MouseEvent e) {
358                                        }
359                                        public void mouseExited(MouseEvent e) {
360                                        }
361                                    });
362     }
363     private void addActionListenerForUrl(final JTextField url, JButton go){
364         ActionListener urlAction = new ActionListener(){
365                 public void actionPerformed(ActionEvent e) {
366                     String urlString = url.getText();
367                     if (urlString != null && urlString.length() > 0) {
368                         if (ie == null || ie.isReleased()) {
369                             createNewIE();
370                         }
371                         try {
372                             if (!ie.getVisible()) {
373                                 ie.setVisible(true);
374                             }
375                             ie.navigate(urlString, false);
376                         } catch (JiffieException je) {
377                             je.printStackTrace();
378                         }
379                     }
380                 }
381                 };
382         go.addActionListener(urlAction);
383         url.addActionListener(urlAction);
384     }
385     public Vector getBrowserCache() {
386         return browsers;
387     }
388     public void addBrowser(InternetExplorer ie) {
389         browsers.addElement(ie);
390         this.ie = ie;
391     }
392     public void removeBrowser(InternetExplorer ie) {
393         browsers.remove(ie);
394         if (browsers.size() > 0) {
395             this.ie = (InternetExplorer) browsers.lastElement();
396         }
397     }
398     public InternetExplorer getFirstBrowser() {
399         return(InternetExplorer) browsers.firstElement();
400     }
401     public InternetExplorer getBrowserAt(int i) {
402         return(InternetExplorer) browsers.elementAt(i);
403     }
404     /***
405      * This method returns null
406      */
407     public InternetExplorer getBrowserWithTitle(String title){
408         return null;
409     }
410     public class HTMLFormTableModel extends DefaultTableModel{
411         public HTMLFormTableModel(Object[] data, int rows){
412             super(data, rows);
413         }
414         /*
415          *  Returns the Class representing the column regardless of columnIndex.
416          *
417          *  @param columnIndex - the column being queried.
418          *  @return the Object Class
419          */
420         public Class getColumnClass(int columnIndex) {
421             Class dataType = super.getColumnClass(columnIndex);
422             if (columnIndex == REQUIRED || columnIndex == INCLUDE) {
423                 dataType = Boolean.class;
424             }
425             return dataType;
426         }
427     }
428 }