1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
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
94 private ProjectData projectData;
95 private AbstractPropertyPanel tab_configFilePanel;
96
97
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
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
132 this.setTitle(titleCore + "[New Project]");
133 this.setSize(750, 500);
134 initComponents();
135
136
137
138
139
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
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
180 metalMenuItem.setSelected(true);
181 setLookAndFeel(metalLF);
182
183 jMenuBar1.add(jMenu2);
184
185
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
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
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
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
290 else {
291 ProjectData pd = null;
292 String propsFileName = (String) argsMap.get(ARG_PROPERTY_FILE_NAME);
293
294
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) {
430 System.exit(0);
431 }
432
433
434
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() {
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
538 tab_webAppPanel = new WebAppPanel(this);
539
540
541 jTabbedPane1.add("(1) Web Application properties", tab_webAppPanel);
542
543 tab_dbPanel = new DbPanel(this);
544
545
546 jTabbedPane1.add("(2) Database properties", tab_dbPanel);
547
548 tab_configFilePanel = new ConfigFilePanel(this);
549
550
551 jTabbedPane1.add("(3) XML config", tab_configFilePanel);
552
553 tab_xslTranformPanel = new XSLTransformPanel(this);
554
555
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
565
566
567
568
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
660
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 }