View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/devgui/XSLTransformPanel.java,v 1.17 2005/11/30 20:31:17 hkollmann Exp $
3    * $Revision: 1.17 $
4    * $Date: 2005/11/30 20:31:17 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  /*
25   *  WebAppPanel.java
26   *
27   *  Created on 26. April 2001, 15:42
28   */
29  package org.dbforms.devgui;
30  
31  import org.dbforms.xmldb.FileSplitter;
32  
33  import java.awt.BorderLayout;
34  import java.awt.GridBagConstraints;
35  import java.awt.GridBagLayout;
36  import java.awt.GridLayout;
37  import java.awt.event.ActionEvent;
38  import java.awt.event.ActionListener;
39  
40  import java.io.File;
41  
42  import javax.swing.JButton;
43  import javax.swing.JCheckBox;
44  import javax.swing.JFileChooser;
45  import javax.swing.JLabel;
46  import javax.swing.JList;
47  import javax.swing.JOptionPane;
48  import javax.swing.JPanel;
49  import javax.swing.JScrollPane;
50  import javax.swing.ListSelectionModel;
51  import javax.swing.SwingConstants;
52  import javax.swing.event.ListSelectionEvent;
53  import javax.swing.event.ListSelectionListener;
54  
55  
56  
57  /***
58   * DOCUMENT ME!
59   *
60   * @author Joachim Peer
61   * @version
62   *
63   */
64  public class XSLTransformPanel extends AbstractPropertyPanel implements ActionListener,
65                                                                  ListSelectionListener {
66     private DevGui              parent;
67     private EditorPanel         panel_editor;
68     private javax.swing.JButton b_browse;
69     private JButton             b_openInBrowser;
70     private JButton             b_refresh;
71     private JButton             b_refreshJSPs;
72     private JButton             b_start;
73     private JCheckBox           cb_useJsCalendar;
74     private javax.swing.JLabel  jLabel1;
75     private javax.swing.JLabel  jLabel2;
76  
77     // GUI Variables declaration
78     private JList list_results;
79  
80     // GUI Variables declaration
81     private JList                  list_xslStylesheets;
82     private javax.swing.JPanel     jPanel2;
83     private javax.swing.JTextField tf_stylesheetDir;
84     private javax.swing.JTextField tf_xsltEncoding;
85     private File[]                 JSPs;
86  
87     // other data
88     private File[] availableStylesheets;
89  
90     /***
91      * Creates new form WebAppPanel
92      *
93      * @param parent Description of the Parameter
94      */
95     public XSLTransformPanel(DevGui parent) {
96        super(parent.getProjectData());
97  
98        this.parent = parent;
99  
100       initComponents();
101 
102       initComponents2();
103 
104       refreshAvailableStylesheets();
105 
106       refreshJSPs();
107 
108       doLayout();
109    }
110 
111    /***
112     * Sets the newProjectData attribute of the XSLTransformPanel object
113     *
114     * @param projectData The new newProjectData value
115     */
116    public void setNewProjectData(ProjectData projectData) {
117       this.projectData = projectData;
118 
119       cb_useJsCalendar.setSelected(TRUESTRING.equalsIgnoreCase(projectData
120                                                                .getProperty(USE_JAVASCRIPT_CALENDAR)));
121 
122       tf_stylesheetDir.setText(projectData.getProperty(STYLESHEET_DIR));
123 
124       tf_xsltEncoding.setText(projectData.getProperty(XSLT_ENCODING));
125 
126       refreshAvailableStylesheets();
127 
128       refreshJSPs();
129    }
130 
131 
132    /***
133     * Description of the Method
134     *
135     * @param ev Description of the Parameter
136     */
137    public void actionPerformed(ActionEvent ev) {
138       if (ev.getSource() == b_openInBrowser) {
139          int selIndex = list_results.getSelectedIndex();
140 
141          if (selIndex != -1) {
142             String selFileName = ((File) list_results.getModel()
143                                                      .getElementAt(selIndex))
144                                  .getName();
145 
146             String webAppURLStr = projectData.getProperty("webAppURL")
147                                              .trim();
148 
149             StringBuffer webAppURL = new StringBuffer(webAppURLStr);
150 
151             if (!webAppURLStr.endsWith("/")) {
152                webAppURL.append("/");
153             }
154 
155             webAppURL.append(selFileName);
156 
157             try {
158                BrowserTool.openURL(webAppURL.toString());
159             } catch (Exception ioe) {
160                showExceptionDialog(ioe);
161             }
162          }
163       } else if (ev.getSource() == b_refresh) {
164          refreshAvailableStylesheets();
165       } else if (ev.getSource() == b_start) {
166          performXSLTransformation();
167       } else if (ev.getSource() == b_refreshJSPs) {
168          refreshJSPs();
169       } else if (ev.getSource() == b_browse) {
170          String stylesheetDir = projectData.getProperty(STYLESHEET_DIR);
171 
172          System.out.println("styleSheetDir=" + stylesheetDir + "!");
173 
174          File dlgFile;
175 
176          System.out.println("ps2");
177 
178          if (!"".equals(stylesheetDir)) {
179             System.out.println("ps3");
180 
181             dlgFile = new File(stylesheetDir);
182 
183             System.out.println("ps4");
184          } else {
185             System.out.println("ps5");
186 
187             dlgFile = null;
188          }
189 
190          //if (dlgFile != null && dlgFile.exists()) {
191          //  dlgFile = null;
192          //}
193          JFileChooser dlg_fileChooser = new JFileChooser(dlgFile);
194          dlg_fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
195 
196          dlg_fileChooser.setVisible(true);
197 
198          int returnVal = dlg_fileChooser.showOpenDialog(this);
199 
200          if (returnVal == JFileChooser.APPROVE_OPTION) {
201             String dirname = dlg_fileChooser.getSelectedFile()
202                                             .getAbsolutePath();
203             tf_stylesheetDir.setText(dirname);
204             projectData.setProperty(STYLESHEET_DIR, dirname);
205             refreshAvailableStylesheets();
206             tf_stylesheetDir.grabFocus();
207          }
208       }
209    }
210 
211 
212    /***
213     * Description of the Method
214     *
215     * @param e Description of the Parameter
216     */
217    public void valueChanged(ListSelectionEvent e) {
218       if (e.getSource() == list_xslStylesheets) {
219          int selIndex = list_xslStylesheets.getSelectedIndex();
220 
221          if (selIndex >= 0) {
222             File f = (File) list_xslStylesheets.getModel()
223                                                .getElementAt(selIndex);
224 
225             panel_editor.setFile(f);
226 
227             b_start.setEnabled(true);
228          } else {
229             b_start.setEnabled(false);
230          }
231       } else if (e.getSource() == list_results) {
232          int selIndex = list_results.getSelectedIndex();
233 
234          if (selIndex >= 0) {
235             File f = (File) list_results.getModel()
236                                         .getElementAt(selIndex);
237 
238             panel_editor.setFile(f);
239 
240             b_openInBrowser.setEnabled(true);
241          } else {
242             b_openInBrowser.setEnabled(false);
243          }
244       }
245    }
246 
247 
248    /***
249     * This method is called from within the constructor to initialize the form.
250     * WARNING: Do NOT modify this code. The content of this method is always
251     * regenerated by the FormEditor.
252     */
253    private void initComponents() {
254       b_browse = new javax.swing.JButton();
255 
256       jLabel1 = new javax.swing.JLabel();
257 
258       tf_stylesheetDir = new javax.swing.JTextField();
259       tf_stylesheetDir.setText(projectData.getProperty(STYLESHEET_DIR));
260 
261       jLabel2 = new javax.swing.JLabel();
262 
263       tf_xsltEncoding = new javax.swing.JTextField();
264       tf_xsltEncoding.setText(projectData.getProperty(XSLT_ENCODING));
265 
266       setLayout(new GridBagLayout());
267 
268       JPanel panel_top = new JPanel();
269 
270       panel_top.setLayout(new GridLayout(1, 2));
271 
272       JPanel panel_top_left = new JPanel();
273 
274       // xsl stylesheet stuff
275       panel_top_left.setLayout(new BorderLayout());
276 
277       panel_top_left.add(BorderLayout.NORTH, new JLabel("XSL stylesheets"));
278 
279       list_xslStylesheets = new JList();
280 
281       list_xslStylesheets.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
282 
283       list_xslStylesheets.addListSelectionListener(this);
284 
285       panel_top_left.add(BorderLayout.CENTER,
286                          new JScrollPane(list_xslStylesheets));
287 
288       JPanel panel_buttons1 = new JPanel();
289 
290       panel_buttons1.add(b_start = new JButton("start transformation!"));
291 
292       b_start.addActionListener(this);
293 
294       b_start.setToolTipText("generate JSPs by applying selected XSL file to the dbforms config XML file.");
295 
296       panel_buttons1.add(b_refresh = new JButton("refresh list"));
297 
298       b_refresh.addActionListener(this);
299 
300       b_refresh.setToolTipText("refresh XSL stylesheet repository in "
301                                + parent.getDbFormsHome().getAbsolutePath()
302                                + parent.getFileSeparator() + "xsl-stylesheets");
303 
304       panel_top_left.add(BorderLayout.SOUTH, panel_buttons1);
305 
306       JPanel panel_top_right = new JPanel();
307 
308       // xhtml result stuff
309       panel_top_right.setLayout(new BorderLayout());
310 
311       panel_top_right.add(BorderLayout.NORTH,
312                           new JLabel("(Generated) JSP files in WebApp-Root"));
313 
314       list_results = new JList();
315 
316       list_results.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
317 
318       list_results.addListSelectionListener(this);
319 
320       panel_top_right.add(BorderLayout.CENTER, new JScrollPane(list_results));
321 
322       JPanel panel_buttons2 = new JPanel();
323 
324       b_openInBrowser = new JButton("Open in browser");
325 
326       b_openInBrowser.setToolTipText("Test a selected result file. Web server  must be running!");
327 
328       b_openInBrowser.addActionListener(this);
329 
330       b_refreshJSPs = new JButton("Refresh JSP list");
331 
332       b_refreshJSPs.addActionListener(this);
333 
334       b_refreshJSPs.setToolTipText("refresh listing of JSP files in your Web application");
335 
336       panel_buttons2.add(b_openInBrowser);
337 
338       panel_buttons2.add(b_refreshJSPs);
339 
340       panel_top_right.add(BorderLayout.SOUTH, panel_buttons2);
341 
342       panel_top.add(panel_top_left);
343 
344       panel_top.add(panel_top_right);
345 
346       cb_useJsCalendar = new JCheckBox("Use JavaScript Calendar in generated pages for editing date fields");
347 
348       jPanel2 = new javax.swing.JPanel();
349 
350       jPanel2.setLayout(new java.awt.GridBagLayout());
351 
352       java.awt.GridBagConstraints gridBagConstraints2;
353 
354       jLabel1.setText("Stylesheet Directory:   ");
355 
356       gridBagConstraints2 = new java.awt.GridBagConstraints();
357 
358       jPanel2.add(jLabel1, gridBagConstraints2);
359 
360       tf_stylesheetDir.setMinimumSize(new java.awt.Dimension(50, 24));
361 
362       gridBagConstraints2 = new java.awt.GridBagConstraints();
363 
364       gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
365 
366       gridBagConstraints2.weightx = 1.0;
367 
368       jPanel2.add(tf_stylesheetDir, gridBagConstraints2);
369 
370       b_browse.setText("browse...");
371 
372       gridBagConstraints2           = new java.awt.GridBagConstraints();
373       gridBagConstraints2.gridwidth = GridBagConstraints.REMAINDER;
374 
375       jPanel2.add(b_browse, gridBagConstraints2);
376 
377       jLabel2.setText("XSLT Encoding:   ");
378       jLabel2.setHorizontalTextPosition(SwingConstants.RIGHT);
379 
380       java.awt.GridBagConstraints gridBagConstraints3 = new java.awt.GridBagConstraints();
381       gridBagConstraints3.fill = java.awt.GridBagConstraints.HORIZONTAL;
382       jPanel2.add(jLabel2, gridBagConstraints3);
383 
384       gridBagConstraints3         = new java.awt.GridBagConstraints();
385       gridBagConstraints3.fill    = java.awt.GridBagConstraints.HORIZONTAL;
386       gridBagConstraints3.weightx = 1.0;
387       tf_xsltEncoding.setMinimumSize(new java.awt.Dimension(50, 24));
388 
389       //tf_encoding.setPreferredSize(new java.awt.Dimension(50, 24));
390       jPanel2.add(tf_xsltEncoding, gridBagConstraints3);
391 
392       cb_useJsCalendar.addItemListener(new java.awt.event.ItemListener() {
393             public void itemStateChanged(java.awt.event.ItemEvent e) {
394                projectData.setProperty(USE_JAVASCRIPT_CALENDAR,
395                                        cb_useJsCalendar.isSelected()
396                                        ? TRUESTRING
397                                        : FALSESTRING);
398             }
399          });
400 
401       GridBagConstraints gridBagConstraints1 = new java.awt.GridBagConstraints();
402 
403       gridBagConstraints1.gridx = 0;
404 
405       gridBagConstraints1.gridy   = 0;
406       gridBagConstraints1.weightx = 1.0;
407       gridBagConstraints1.fill    = java.awt.GridBagConstraints.HORIZONTAL;
408 
409       gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
410 
411       add(jPanel2, gridBagConstraints1);
412 
413       //       add(tf_stylesheetDir, gridBagConstraints1);
414       gridBagConstraints1.gridx = 0;
415 
416       gridBagConstraints1.gridy = 1;
417 
418       gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
419 
420       add(cb_useJsCalendar, gridBagConstraints1);
421 
422       gridBagConstraints1.gridy = 2;
423 
424       gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
425 
426       gridBagConstraints1.weightx = 0.5;
427 
428       gridBagConstraints1.weighty = 0.5;
429 
430       add(panel_top, gridBagConstraints1);
431 
432       //      add(BorderLayout.NORTH, panel_top);
433       panel_editor = new EditorPanel();
434 
435       gridBagConstraints1.gridy = 3;
436 
437       add(panel_editor, gridBagConstraints1);
438 
439       //      add(BorderLayout.CENTER, panel_editor);
440       // set logical state of dialog
441       list_xslStylesheets.clearSelection();
442 
443       b_start.setEnabled(false);
444 
445       list_results.clearSelection();
446 
447       b_openInBrowser.setEnabled(false);
448    }
449 
450 
451    /***
452     * Description of the Method
453     */
454    private void initComponents2() {
455       b_browse.addActionListener(this);
456 
457       // first listener => for global property / Project data
458       addAFocusListener(tf_stylesheetDir, STYLESHEET_DIR);
459 
460       b_browse.setToolTipText("default: DBFORMS_HOME"
461                               + parent.getFileSeparator() + "xsl-stylesheets"
462                               + parent.getFileSeparator()
463                               + "\nDefault location for Stylesheets");
464 
465       // listener => for global property / Project data
466       addAFocusListener(tf_xsltEncoding, XSLT_ENCODING);
467    }
468 
469 
470    /***
471     * Description of the Method
472     */
473    private void performXSLTransformation() {
474       System.out.println(projectData.toString());
475 
476       boolean useJsCalendar = TRUESTRING.equalsIgnoreCase(projectData
477                                                           .getProperty(USE_JAVASCRIPT_CALENDAR));
478 
479       String  webAppRoot = projectData.getProperty("webAppRoot");
480 
481       System.out.println("webAppRoot=" + webAppRoot + "!");
482 
483       if ("".equals(webAppRoot)) {
484          JOptionPane.showMessageDialog(this,
485                                        "Please provide web application root",
486                                        "missing data", JOptionPane.ERROR_MESSAGE);
487 
488          return;
489       }
490 
491       //String sourcePath = webAppRoot + parent.getFileSeparator() + "WEB-INF" + parent.getFileSeparator() + projectData.getProperty("configFile");
492       String sourcePath = projectData.getProperty("configFile");
493 
494       File   sourceFile = new File(sourcePath);
495 
496       System.out.println("sourcePath=" + sourcePath);
497 
498       if (!sourceFile.exists() || !sourceFile.canRead()) {
499          JOptionPane.showMessageDialog(this,
500                                        "Please provide correct XML config file, "
501                                        + sourcePath + " is wrong",
502                                        "wrong data", JOptionPane.ERROR_MESSAGE);
503 
504          return;
505       }
506 
507       File transformFile = (File) list_xslStylesheets.getModel()
508                                                      .getElementAt(list_xslStylesheets
509                                                                    .getSelectedIndex());
510 
511       if (!transformFile.exists() || !transformFile.canRead()) {
512          JOptionPane.showMessageDialog(this,
513                                        "Please provide correct XSL stylesheet",
514                                        "wrong data", JOptionPane.ERROR_MESSAGE);
515 
516          return;
517       }
518 
519       String destPath = FileNameTool.normalize(parent.getDbFormsHome().getAbsolutePath())
520                         + "temp" + parent.getFileSeparator()
521                         + "temp_result.xhtml";
522 
523       File   destFile = new File(destPath);
524 
525       try {
526          String xsltEncoding = projectData.getProperty(XSLT_ENCODING);
527 
528          // removve trailing spaces if not null
529          if (xsltEncoding != null) {
530             xsltEncoding = xsltEncoding.trim();
531          }
532 
533          XSLTransformer.transform(sourceFile, transformFile, destFile,
534                                   useJsCalendar, xsltEncoding);
535 
536          FileSplitter fs = new FileSplitter(destFile, new File(webAppRoot));
537 
538          fs.splitFile();
539 
540          this.refreshJSPs();
541 
542          JOptionPane.showMessageDialog(this,
543                                        "XSL Transformation done.\nCheck new JSPs in your WebApp root",
544                                        "XSL result", JOptionPane.PLAIN_MESSAGE);
545       } catch (Exception e) {
546          JOptionPane.showMessageDialog(this,
547                                        "Error during transform:" + e.toString(),
548                                        "XSL transform error",
549                                        JOptionPane.ERROR_MESSAGE);
550       }
551    }
552 
553 
554    /***
555     * Description of the Method
556     */
557    private void refreshAvailableStylesheets() {
558       //String xslDirStr = FileNameTool.normalize(parent.getDbFormsHome().getAbsolutePath()) + "xsl-stylesheets";
559       String xslDirStr = FileNameTool.normalize(parent.getProjectData().getProperty(STYLESHEET_DIR));
560       File   xslDir = new File(xslDirStr);
561 
562       if (xslDir.isDirectory() && xslDir.canRead()) {
563          try {
564             this.availableStylesheets = FileUtility.getFilesInDirectory(xslDir,
565                                                                         null);
566 
567             list_xslStylesheets.setListData(this.availableStylesheets);
568          } catch (Exception ioe) {
569             ioe.printStackTrace();
570 
571             showExceptionDialog(ioe);
572          }
573       }
574    }
575 
576 
577    /***
578     * Description of the Method
579     */
580    private void refreshJSPs() {
581       String jspDirStr = projectData.getProperty("webAppRoot");
582 
583       File   jspDir = new File(jspDirStr);
584 
585       if (jspDir.isDirectory() && jspDir.canRead()) {
586          try {
587             String[] postFixList = {
588                                       ".jsp"
589                                    };
590 
591             this.JSPs = FileUtility.getFilesInDirectory(jspDir, postFixList);
592 
593             list_results.setListData(this.JSPs);
594          } catch (Exception ioe) {
595             ioe.printStackTrace();
596 
597             showExceptionDialog(ioe);
598          }
599       }
600    }
601 }