View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/devgui/DevGui.java,v 1.14 2005/11/30 20:31:17 hkollmann Exp $
3    * $Revision: 1.14 $
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   * DevGui.java
26   *
27   * Created on 26. April 2001, 14:50
28   */
29  package org.dbforms.devgui;
30  
31  import org.dbforms.util.Util;
32  
33  import java.awt.BorderLayout;
34  import java.awt.event.ActionEvent;
35  import java.awt.event.ActionListener;
36  import java.awt.event.InputEvent;
37  import java.awt.event.KeyEvent;
38  
39  import java.io.BufferedWriter;
40  import java.io.File;
41  import java.io.FileWriter;
42  import java.io.IOException;
43  
44  import java.util.HashMap;
45  import java.util.Map;
46  
47  import javax.swing.ButtonGroup;
48  import javax.swing.JFileChooser;
49  import javax.swing.JMenu;
50  import javax.swing.JOptionPane;
51  import javax.swing.KeyStroke;
52  import javax.swing.SwingConstants;
53  import javax.swing.SwingUtilities;
54  import javax.swing.UIManager;
55  
56  
57  
58  /***
59   * -- DevGui -- This SWING application provides support for automatic
60   * generation of JSP views. It does so by retrieving database-metadata as XML
61   * file from a given datasource and by applying the XML file to XSL
62   * stylesheets. HINTS FOR MAKING THIS APP MORE CONVENIENT ARE VERY WELCOME!
63   * Check manual or contact the author for more info.
64   *
65   * @author Joachim Peer
66   * @version
67   */
68  public class DevGui extends javax.swing.JFrame implements ActionListener {
69     private static final String metalLF                = "javax.swing.plaf.metal.MetalLookAndFeel";
70     private static final String motifLF                = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
71     private static final String windowsLF              = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
72     private static final String ARG_PROPERTY_FILE_NAME = "propertyfilename";
73     private static final String ARG_CREATE_CONFIG      = "createconfigfile";
74     private static final String ARG_OUTPUT_FILE_NAME   = "outputfilename";
75     private File                dbFormsHome;
76     private javax.swing.JFrame  helpFrame;
77     private javax.swing.JMenu   jMenu1;
78  
79     // Gui Variables declaration
80     private javax.swing.JMenuBar    jMenuBar1;
81     private javax.swing.JMenuItem   exitMenuItem;
82     private javax.swing.JMenuItem   helpMenuItem;
83     private javax.swing.JMenuItem   metalMenuItem;
84     private javax.swing.JMenuItem   motifMenuItem;
85     private javax.swing.JMenuItem   newMenuItem;
86     private javax.swing.JMenuItem   openMenuItem;
87     private javax.swing.JMenuItem   saveAsMenuItem;
88     private javax.swing.JMenuItem   saveMenuItem;
89     private javax.swing.JMenuItem   windowsMenuItem;
90     private javax.swing.JSeparator  jSeparator1;
91     private javax.swing.JTabbedPane jTabbedPane1;
92  
93     // other variables
94     private ProjectData   projectData;
95     private AbstractPropertyPanel tab_configFilePanel;
96  
97     // windows (tabs)
98     private AbstractPropertyPanel tab_dbPanel;
99     private AbstractPropertyPanel tab_webAppPanel;
100    private AbstractPropertyPanel tab_xslTranformPanel;
101    private String        dbFormsHomeStr;
102    private String        fileSeparator;
103    private final String  titleCore = "DbForms Developers' GUI - ";
104 
105    /***
106     * Creates new form DevGui
107     *
108     * @param projectData DOCUMENT ME!
109     */
110    public DevGui(ProjectData projectData) {
111       this.projectData = projectData;
112 
113       boolean projectDataLoaded = false;
114       dbFormsHomeStr = System.getProperty("DBFORMS_HOME");
115 
116       if (dbFormsHomeStr != null) {
117          dbFormsHome = new File(dbFormsHomeStr);
118          System.out.println("dbFormsHome=" + dbFormsHome.getAbsolutePath());
119 
120          if (dbFormsHome.isDirectory() && dbFormsHome.canRead()) {
121             // CREATE DATA MODEL + SOME HELPER INFOS
122             if (projectData == null) {
123                this.projectData = new ProjectData();
124             } else {
125                projectDataLoaded = true;
126             }
127 
128             this.fileSeparator = System.getProperties()
129                                        .getProperty("file.separator");
130 
131             // CREATE GUI
132             this.setTitle(titleCore + "[New Project]");
133             this.setSize(750, 500);
134             initComponents();
135 
136             /* changes dikr 2002-03-04: set tab placement to top and
137              *  shorten tab titles a bit....
138              * (this is done here because initComponents seems to be
139              * automatically generated by (netbeans?) Gui builder)
140              */
141             jTabbedPane1.setTabPlacement(SwingConstants.TOP);
142             jTabbedPane1.setTitleAt(0, "(1) Web Application");
143             jTabbedPane1.setToolTipTextAt(0,
144                                           "specify web application location and URI");
145             jTabbedPane1.setTitleAt(1, "(2) Database");
146             jTabbedPane1.setToolTipTextAt(1,
147                                           "set database properties like JDBC driver, username");
148             jTabbedPane1.setTitleAt(2, "(3) XML Config");
149             jTabbedPane1.setToolTipTextAt(2,
150                                           "automatically generate dbforms config file");
151             jTabbedPane1.setTitleAt(3, "(4) XSL Transformation");
152             jTabbedPane1.setToolTipTextAt(3,
153                                           "use XSL to generate JSPs from dbforms config file");
154 
155             /* create Menuitem for Look and Feel:
156               */
157             ButtonGroup lfButtonGroup = new ButtonGroup();
158             JMenu       jMenu2 = new javax.swing.JMenu();
159             jMenu2.setText("Look&Feel");
160 
161             metalMenuItem = new javax.swing.JRadioButtonMenuItem("Java Metal L&F");
162             metalMenuItem.setActionCommand("Java Metal L&F");
163             metalMenuItem.addActionListener(this);
164             lfButtonGroup.add(metalMenuItem);
165             jMenu2.add(metalMenuItem);
166 
167             motifMenuItem = new javax.swing.JRadioButtonMenuItem("Motif L&F");
168             motifMenuItem.setActionCommand("Motiv L&F");
169             motifMenuItem.addActionListener(this);
170             lfButtonGroup.add(motifMenuItem);
171             jMenu2.add(motifMenuItem);
172 
173             windowsMenuItem = new javax.swing.JRadioButtonMenuItem("Windows L&F");
174             windowsMenuItem.setActionCommand("Windows L&F");
175             windowsMenuItem.addActionListener(this);
176             lfButtonGroup.add(windowsMenuItem);
177             jMenu2.add(windowsMenuItem);
178 
179             // set Default L&F:
180             metalMenuItem.setSelected(true);
181             setLookAndFeel(metalLF);
182 
183             jMenuBar1.add(jMenu2);
184 
185             /* create Menuitem for Help:
186             */
187             JMenu jMenu3 = new javax.swing.JMenu();
188             jMenu3.setText("Help");
189             helpMenuItem = new javax.swing.JMenuItem();
190             helpMenuItem.setActionCommand("Show Helptext");
191             helpMenuItem.setText("Show Helptext");
192             helpMenuItem.addActionListener(this);
193             jMenu3.add(helpMenuItem);
194             jMenuBar1.add(jMenu3);
195 
196             helpFrame = new HelpFrame();
197 
198             //        pack ();
199             doLayout();
200 
201             if (projectDataLoaded) {
202                updateGUI();
203                this.setTitle(titleCore + "["
204                              + projectData.getFile().getAbsolutePath() + "]");
205             }
206          } else {
207             quickQuit("DBFORMS_HOME is set to \"" + dbFormsHomeStr
208                       + "\", which is not a valid accessible directory.", 1);
209          }
210       } else {
211          quickQuit("Pleases set the environment variable DBFORMS_HOME to the root of your unzipped DbForms-Distribution! Check manual for more info.",
212                    1);
213       }
214    }
215 
216    /***
217     * DOCUMENT ME!
218     *
219     * @return DOCUMENT ME!
220     */
221    public File getDbFormsHome() {
222       return dbFormsHome;
223    }
224 
225 
226    /***
227     * DOCUMENT ME!
228     *
229     * @return DOCUMENT ME!
230     */
231    public String getFileSeparator() {
232       return fileSeparator;
233    }
234 
235 
236    /***
237     * DOCUMENT ME!
238     *
239     * @return DOCUMENT ME!
240     */
241    public ProjectData getProjectData() {
242       return projectData;
243    }
244 
245 
246    //GEN-END:initComponents
247 
248    /***
249     * DOCUMENT ME!
250     *
251     * @param e DOCUMENT ME!
252     */
253    public void actionPerformed(ActionEvent e) {
254       if (e.getSource() == newMenuItem) {
255          newProject();
256       } else if (e.getSource() == openMenuItem) {
257          openProject();
258       } else if (e.getSource() == saveMenuItem) {
259          saveProject();
260       } else if (e.getSource() == saveAsMenuItem) {
261          saveAsProject();
262       } else if (e.getSource() == exitMenuItem) {
263          exitProject();
264       } else if (e.getSource() == helpMenuItem) {
265          helpFrame.show();
266       } else if (e.getSource() == metalMenuItem) {
267          setLookAndFeel(metalLF);
268       } else if (e.getSource() == motifMenuItem) {
269          setLookAndFeel(motifLF);
270       } else if (e.getSource() == windowsMenuItem) {
271          setLookAndFeel(windowsLF);
272       }
273    }
274 
275 
276    /***
277     * DOCUMENT ME!
278     *
279     * @param args the command line arguments
280     */
281    public static void main(String[] args) {
282       Map argsMap = parseCommandlineParameters(args);
283 
284       // create dbforms-config.xml ad exit
285       if (argsMap.get(ARG_CREATE_CONFIG) != null) {
286          createConfigFile((String) argsMap.get(ARG_PROPERTY_FILE_NAME),
287                           (String) argsMap.get(ARG_OUTPUT_FILE_NAME));
288       }
289       // start gui
290       else {
291          ProjectData pd            = null;
292          String      propsFileName = (String) argsMap.get(ARG_PROPERTY_FILE_NAME);
293 
294          // try to load property file
295          if (!Util.isNull(propsFileName)) {
296             File projFile = new File(propsFileName);
297 
298             try {
299                pd = ProjectData.loadFromDisc(projFile);
300             } catch (IOException e) {
301                e.printStackTrace();
302                System.exit(1);
303             }
304          }
305 
306          new DevGui(pd).show();
307       }
308    }
309 
310 
311    /***
312     * DOCUMENT ME!
313     *
314     * @param propsFileName DOCUMENT ME!
315     * @param outputFileName DOCUMENT ME!
316     */
317    protected static void createConfigFile(String propsFileName,
318                                           String outputFileName) {
319       try {
320          File        projFile = new File(propsFileName);
321          ProjectData pd = ProjectData.loadFromDisc(projFile);
322 
323          if (Util.isNull(outputFileName)) {
324             outputFileName = pd.getProperty("configFile");
325          }
326 
327          if (Util.isNull(outputFileName)) {
328             throw new Exception("Property configFile in propery file not set");
329          }
330 
331          String         result = XMLConfigGenerator.createXMLOutput(pd, false);
332          BufferedWriter bw = new BufferedWriter(new FileWriter(outputFileName));
333          bw.write(result);
334          bw.close();
335       } catch (Exception ex) {
336          ex.printStackTrace();
337       }
338    }
339 
340 
341    /***
342     * DOCUMENT ME!
343     *
344     * @param args DOCUMENT ME!
345     *
346     * @return DOCUMENT ME!
347     */
348    protected static Map parseCommandlineParameters(String[] args) {
349       if (args == null) {
350          return null;
351       }
352 
353       Map argsMap = new HashMap();
354 
355       for (int i = 0; i < args.length; i++) {
356          if (args[i].equalsIgnoreCase("-propertyfile")) {
357             if (argsMap.get(ARG_PROPERTY_FILE_NAME) == null) {
358                argsMap.put(ARG_PROPERTY_FILE_NAME, args[++i]);
359             } else {
360                System.err.println("Not allowed to specify \"createconfigfile\" and \"-propertyfile\" at the same time");
361                System.err.println("Programm stopped");
362                System.err.println();
363                usage();
364                System.exit(1);
365             }
366          } else if (args[i].equalsIgnoreCase("createconfigfile")) {
367             argsMap.put(ARG_CREATE_CONFIG, Boolean.valueOf(true));
368 
369             if (argsMap.get(ARG_PROPERTY_FILE_NAME) == null) {
370                if (i < args.length) {
371                   argsMap.put(ARG_PROPERTY_FILE_NAME, args[++i]);
372                } else {
373                   System.err.println("<propertyfilename> must be specified");
374                   System.err.println("Programm stopped");
375                   System.err.println();
376                   usage();
377                   System.exit(1);
378                }
379 
380                if (i < (args.length - 1)) {
381                   argsMap.put(ARG_OUTPUT_FILE_NAME, args[++i]);
382                }
383             } else {
384                System.err.println("Not allowed to specify \"createconfigfile\" and \"-propertyfile\" at the same time");
385                System.err.println("Programm stopped");
386                System.err.println();
387                usage();
388                System.exit(1);
389             }
390          } else {
391             System.err.println("Undefined parameter \"" + args[i] + "\"");
392             System.err.println("Programm stopped");
393             System.err.println();
394             usage();
395             System.exit(1);
396          }
397       }
398 
399       return argsMap;
400    }
401 
402 
403    static void usage() {
404       System.out.println("Usage: ");
405       System.out.println("Gui mode: \n"
406                          + "  java -DDBFORMS_HOME=/path/to/dbfhome org.dbforms.devgui.DevGui ");
407       System.out.println("Gui mode with automatic loaded configfile: \n"
408                          + "  java org.dbforms.devgui.DevGui -propertyfile <propertyfilename>");
409       System.out.println("Command line mode: \n"
410                          + "  java org.dbforms.devgui.DevGui createconfigfile <propertyfilename> [ <outputfilename> ]");
411    }
412 
413 
414    private void setLookAndFeel(String lf) {
415       try {
416          UIManager.setLookAndFeel(lf);
417          SwingUtilities.updateComponentTreeUI(this);
418       } catch (Exception ex) {
419          System.err.println(ex.getMessage());
420       }
421    }
422 
423 
424    /***
425     * Exit the Application
426     *
427     * @param evt DOCUMENT ME!
428     */
429    private void exitForm(java.awt.event.WindowEvent evt) { //GEN-FIRST:event_exitForm
430       System.exit(0);
431    }
432 
433 
434    // dkr 2002-03-04:  add gui dialog if user tries to exit while having unsaved data
435    private void exitProject() {
436       if (!projectData.isUnsaved()) {
437          System.exit(0);
438       } else {
439          int answer = JOptionPane.showOptionDialog(this,
440                                                    "Your project data has not been not saved.",
441                                                    "Ooops",
442                                                    JOptionPane.YES_NO_CANCEL_OPTION,
443                                                    JOptionPane.WARNING_MESSAGE,
444                                                    null,
445                                                    new String[] {
446                                                       "Save and exit",
447                                                       "Exit without saving",
448                                                       "Cancel"
449                                                    }, "Save and exit");
450 
451          switch (answer) {
452             case 0:
453                saveProject();
454 
455             case 1:
456                System.exit(0);
457 
458             case 2:
459                return;
460          }
461       }
462    }
463 
464 
465    /***
466     * This method is called from within the constructor to initialize the form.
467     * WARNING: Do NOT modify this code. The content of this method is always
468     * regenerated by the FormEditor.
469     */
470    private void initComponents() { //GEN-BEGIN:initComponents
471       jMenuBar1      = new javax.swing.JMenuBar();
472       jMenu1         = new javax.swing.JMenu();
473       newMenuItem    = new javax.swing.JMenuItem();
474       openMenuItem   = new javax.swing.JMenuItem();
475       saveMenuItem   = new javax.swing.JMenuItem();
476       saveAsMenuItem = new javax.swing.JMenuItem();
477       jSeparator1    = new javax.swing.JSeparator();
478       exitMenuItem   = new javax.swing.JMenuItem();
479       jTabbedPane1   = new javax.swing.JTabbedPane();
480 
481       jMenu1.setActionCommand("Project");
482       jMenu1.setText("Project");
483 
484       newMenuItem.setActionCommand("New");
485       newMenuItem.setText("New");
486       newMenuItem.addActionListener(this);
487       newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
488                                                         InputEvent.CTRL_MASK,
489                                                         false));
490       jMenu1.add(newMenuItem);
491 
492       openMenuItem.setActionCommand("Open");
493       openMenuItem.setText("Open");
494       openMenuItem.addActionListener(this);
495       openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
496                                                          InputEvent.CTRL_MASK,
497                                                          false));
498       jMenu1.add(openMenuItem);
499 
500       saveMenuItem.setActionCommand("Save");
501       saveMenuItem.setText("Save");
502       saveMenuItem.addActionListener(this);
503       saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
504                                                          InputEvent.CTRL_MASK,
505                                                          false));
506       jMenu1.add(saveMenuItem);
507 
508       saveAsMenuItem.setActionCommand("Save As...");
509       saveAsMenuItem.setText("Save As...");
510       saveAsMenuItem.addActionListener(this);
511       jMenu1.add(saveAsMenuItem);
512 
513       jMenu1.add(jSeparator1);
514 
515       exitMenuItem.setActionCommand("Exit");
516       exitMenuItem.setText("Exit");
517       exitMenuItem.addActionListener(this);
518       jMenu1.add(exitMenuItem);
519 
520       jMenuBar1.add(jMenu1);
521 
522       addWindowListener(new java.awt.event.WindowAdapter() {
523             public void windowClosing(java.awt.event.WindowEvent evt) {
524                exitForm(evt);
525             }
526          });
527 
528       setJMenuBar(jMenuBar1);
529 
530       getContentPane()
531          .setLayout(new BorderLayout());
532 
533       getContentPane()
534          .add(java.awt.BorderLayout.CENTER, jTabbedPane1);
535       jTabbedPane1.setTabPlacement(SwingConstants.LEFT);
536 
537       // install panels
538       tab_webAppPanel = new WebAppPanel(this);
539 
540       //jTabbedPane1.addTab("(1) Web Application properties", null, tab_webAppPanel, "provide information about your web application");
541       jTabbedPane1.add("(1) Web Application properties", tab_webAppPanel);
542 
543       tab_dbPanel = new DbPanel(this);
544 
545       //jTabbedPane1.addTab("(2) Database properties", null, tab_dbPanel, "provide valid data for creating a database connection");
546       jTabbedPane1.add("(2) Database properties", tab_dbPanel);
547 
548       tab_configFilePanel = new ConfigFilePanel(this);
549 
550       //jTabbedPane1.addTab("(3) XML config", null, tab_configFilePanel, "automatically generate the DbForms Config file" );
551       jTabbedPane1.add("(3) XML config", tab_configFilePanel);
552 
553       tab_xslTranformPanel = new XSLTransformPanel(this);
554 
555       //jTabbedPane1.addTab("(4) XSL transformation", null, tab_xslTranformPanel, "automatically generate JSP views based on table data stored in DbForms Config file");
556       jTabbedPane1.add("(4) XSL transformation", tab_xslTranformPanel);
557 
558       tab_webAppPanel.doLayout();
559       tab_dbPanel.doLayout();
560       tab_configFilePanel.doLayout();
561       tab_xslTranformPanel.doLayout();
562 
563       /*
564                       jTabbedPane1.setSelectedIndex(0);
565                       jTabbedPane1.setSelectedIndex(1);
566                       jTabbedPane1.setSelectedIndex(2);
567                       jTabbedPane1.setSelectedIndex(3);
568                       jTabbedPane1.setSelectedIndex(0);
569 
570       */
571    }
572 
573 
574    private void newProject() {
575       this.projectData = new ProjectData();
576       updateGUI();
577       this.setTitle(titleCore + "[New Project]");
578 
579       System.out.println("new Project initialized");
580    }
581 
582 
583    private void openProject() {
584       JFileChooser dlg_fileChooser = new JFileChooser();
585       dlg_fileChooser.setDialogTitle("Open Project...");
586       dlg_fileChooser.setVisible(true);
587 
588       int returnVal = dlg_fileChooser.showOpenDialog(this);
589 
590       if (returnVal == JFileChooser.APPROVE_OPTION) {
591          File f = dlg_fileChooser.getSelectedFile();
592 
593          try {
594             if (f.isFile()) {
595                this.projectData = ProjectData.loadFromDisc(f);
596                updateGUI();
597                this.setTitle(titleCore + "["
598                              + projectData.getFile().getAbsolutePath() + "]");
599             }
600          } catch (IOException ioe) {
601             ioe.printStackTrace();
602             showExceptionDialog(ioe);
603          }
604       }
605    }
606 
607 
608    private void quickQuit(String message,
609                           int    returnValue) {
610       System.out.println(message);
611       System.out.println("\nPlease drop a line of feedback to j.peer@gmx.net, thanks!\n");
612       System.exit(returnValue);
613    }
614 
615 
616    private void saveAsProject() {
617       JFileChooser dlg_fileChooser = new JFileChooser();
618       dlg_fileChooser.setDialogTitle("Save Project As...");
619       dlg_fileChooser.setVisible(true);
620 
621       int returnVal = dlg_fileChooser.showSaveDialog(this);
622 
623       if (returnVal == JFileChooser.APPROVE_OPTION) {
624          try {
625             File f = dlg_fileChooser.getSelectedFile();
626             this.projectData.storeToDisc(f);
627             this.setTitle(titleCore + "["
628                           + projectData.getFile().getAbsolutePath() + "]");
629          } catch (IOException ioe) {
630             ioe.printStackTrace();
631             showExceptionDialog(ioe);
632          }
633       }
634    }
635 
636 
637    private void saveProject() {
638       if (projectData.getFile() != null) {
639          try {
640             this.projectData.storeToDisc(projectData.getFile());
641          } catch (IOException ioe) {
642             ioe.printStackTrace();
643             showExceptionDialog(ioe);
644          }
645       } else {
646          saveAsProject();
647       }
648    }
649 
650 
651    private void showExceptionDialog(Exception e) {
652       JOptionPane.showMessageDialog(this,
653                                     "An exception occurred:\n\n" + e.toString()
654                                     + "\n", "Exception",
655                                     JOptionPane.ERROR_MESSAGE);
656    }
657 
658 
659    //GEN-LAST:event_exitForm
660    //======================= BUSINESS METHODS
661    private void updateGUI() {
662       this.tab_dbPanel.setNewProjectData(projectData);
663       this.tab_webAppPanel.setNewProjectData(projectData);
664       this.tab_configFilePanel.setNewProjectData(projectData);
665       this.tab_xslTranformPanel.setNewProjectData(projectData);
666    }
667 }