KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > editor > app > gui > TestEditorFrame


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.test.editor.app.gui;
20
21 import java.awt.Component JavaDoc;
22 import java.awt.event.ComponentAdapter JavaDoc;
23 import java.awt.event.ComponentEvent JavaDoc;
24 import java.awt.event.WindowAdapter JavaDoc;
25 import javax.swing.*;
26 import java.util.Vector JavaDoc;
27 import java.io.File JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import javax.swing.filechooser.FileFilter JavaDoc;
30 import javax.swing.tree.DefaultTreeModel JavaDoc;
31 import org.netbeans.test.editor.app.Main;
32
33 import org.netbeans.test.editor.app.core.*;
34 import org.netbeans.test.editor.app.core.Test;
35 import org.netbeans.test.editor.app.gui.QuestionDialog;
36 import org.netbeans.test.editor.app.gui.tree.TestNodeDelegate;
37 import org.netbeans.test.editor.app.tests.GenerateTests;
38 import org.netbeans.test.editor.app.util.Scheduler;
39
40 /**
41  *
42  *
43  *
44  * @author ehucka
45  *
46  * @version
47  *
48  */

49
50 public class TestEditorFrame extends javax.swing.JFrame JavaDoc {
51     
52     private static String JavaDoc TITLE="Editor Test Application";
53     
54     private TestNodeDelegate root;
55     
56     private Test test;
57     
58     private TreeDialog tree=null;
59     
60     File JavaDoc currentDirectory;
61     
62     String JavaDoc oldpath,oldpackage;
63     
64     /** Creates new form TestEditorFrame */
65     
66     public TestEditorFrame() {
67         initComponents();
68         pack();
69         this.setSize(800,500);
70         this.setLocation(300,100);
71         oldpath="";
72         oldpackage="";
73         // currentDirectory=new File(System.getProperty("user.dir"));
74
currentDirectory=new File JavaDoc("/nbcvs/nball/editor/test/qa-functional/src/org/netbeans/test/editor/app");
75     }
76     
77     public EventLoggingEditorPane getEditor() {
78         return (EventLoggingEditorPane)editor;
79     }
80     
81     public void setTest(Test test) {
82         this.test=test;
83         root = (TestNodeDelegate)test.getNodeDelegate();
84         if (tree != null) {
85             tree.setRootContext(root);
86         }
87     }
88     
89     public void newRootWindow() {
90         if (tree == null) {
91             tree=new TreeDialog(this);
92             tree.setRootContext(root);
93             addComponentListener(new ComponentAdapter JavaDoc() {
94                 public void componentMoved(ComponentEvent JavaDoc e) {
95                     if (tree != null) {
96                         tree.setLocation(getX()-tree.getWidth(), getY());
97                     }
98                 }
99                 
100                 public void componentResized(ComponentEvent JavaDoc e) {
101                     if (tree != null) {
102                         tree.setSize(tree.getWidth(),getHeight());
103                         tree.setLocation(getX()-tree.getWidth(), getY());
104                     }
105                 }
106             });
107             viewExplorerM.setEnabled(false);
108             tree.addWindowListener(new WindowAdapter JavaDoc() {
109                 public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
110                     viewExplorerM.setEnabled(true);
111                     tree=null;
112                 }
113             });
114             tree.show();
115         }
116     }
117     
118     public Test getTest() {
119         return test;
120     }
121     
122     public TestNodeDelegate getRootNode() {
123         return root;
124     }
125     
126     /** This method is called from within the constructor to
127      *
128      * initialize the form.
129      *
130      * WARNING: Do NOT modify this code. The content of this method is
131      *
132      * always regenerated by the FormEditor.
133      *
134      */

135     
136     private void initComponents() {//GEN-BEGIN:initComponents
137
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
138
139         jPanel2 = new javax.swing.JPanel JavaDoc();
140         jScrollPane3 = new javax.swing.JScrollPane JavaDoc();
141         editor = new EventLoggingEditorPane();
142         jPanel4 = new javax.swing.JPanel JavaDoc();
143         historyScrollPane = new javax.swing.JScrollPane JavaDoc();
144         history = new javax.swing.JTextArea JavaDoc();
145         jMenuBar1 = new javax.swing.JMenuBar JavaDoc();
146         jMenu1 = new javax.swing.JMenu JavaDoc();
147         jMenuItem1 = new javax.swing.JMenuItem JavaDoc();
148         jMenuItem2 = new javax.swing.JMenuItem JavaDoc();
149         jMenuItem3 = new javax.swing.JMenuItem JavaDoc();
150         jMenuItem4 = new javax.swing.JMenuItem JavaDoc();
151         jSeparator1 = new javax.swing.JSeparator JavaDoc();
152         jMenuItem5 = new javax.swing.JMenuItem JavaDoc();
153         jMenu3 = new javax.swing.JMenu JavaDoc();
154         jMenuItem6 = new javax.swing.JMenuItem JavaDoc();
155         jSeparator2 = new javax.swing.JSeparator JavaDoc();
156         jMenuItem7 = new javax.swing.JMenuItem JavaDoc();
157         jMenu2 = new javax.swing.JMenu JavaDoc();
158         viewExplorerM = new javax.swing.JMenuItem JavaDoc();
159
160         getContentPane().setLayout(new java.awt.GridBagLayout JavaDoc());
161
162         setTitle("Editor Test Application");
163         addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
164             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
165                 TestEditorFrame.this.exitForm(evt);
166             }
167         });
168
169         jPanel2.setLayout(new java.awt.BorderLayout JavaDoc());
170
171         jPanel2.setBorder(new javax.swing.border.TitledBorder JavaDoc("Editor"));
172         jPanel2.setMinimumSize(new java.awt.Dimension JavaDoc(300, 200));
173         jPanel2.setPreferredSize(new java.awt.Dimension JavaDoc(300, 200));
174         jScrollPane3.setAutoscrolls(true);
175         editor.setMinimumSize(new java.awt.Dimension JavaDoc(200, 100));
176         editor.setPreferredSize(new java.awt.Dimension JavaDoc(6, 23));
177         jScrollPane3.setViewportView(editor);
178
179         jPanel2.add(jScrollPane3, java.awt.BorderLayout.CENTER);
180
181         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
182         gridBagConstraints.gridx = 1;
183         gridBagConstraints.gridy = 0;
184         gridBagConstraints.gridwidth = 3;
185         gridBagConstraints.gridheight = 3;
186         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
187         gridBagConstraints.weightx = 1.0;
188         gridBagConstraints.weighty = 1.0;
189         getContentPane().add(jPanel2, gridBagConstraints);
190
191         jPanel4.setLayout(new java.awt.BorderLayout JavaDoc());
192
193         jPanel4.setBorder(new javax.swing.border.TitledBorder JavaDoc("History"));
194         jPanel4.setMinimumSize(new java.awt.Dimension JavaDoc(500, 75));
195         jPanel4.setPreferredSize(new java.awt.Dimension JavaDoc(500, 75));
196         historyScrollPane.setViewportView(history);
197
198         jPanel4.add(historyScrollPane, java.awt.BorderLayout.CENTER);
199
200         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
201         gridBagConstraints.gridx = 1;
202         gridBagConstraints.gridy = 3;
203         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
204         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
205         gridBagConstraints.weightx = 1.0;
206         gridBagConstraints.weighty = 1.0;
207         getContentPane().add(jPanel4, gridBagConstraints);
208
209         jMenu1.setText("Test");
210         jMenuItem1.setText("New");
211         jMenuItem1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
212             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
213                 TestEditorFrame.this.newTest(evt);
214             }
215         });
216
217         jMenu1.add(jMenuItem1);
218
219         jMenuItem2.setText("Open");
220         jMenuItem2.addActionListener(new java.awt.event.ActionListener JavaDoc() {
221             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
222                 TestEditorFrame.this.openTest(evt);
223             }
224         });
225
226         jMenu1.add(jMenuItem2);
227
228         jMenuItem3.setText("Save");
229         jMenuItem3.addActionListener(new java.awt.event.ActionListener JavaDoc() {
230             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
231                 TestEditorFrame.this.saveTest(evt);
232             }
233         });
234
235         jMenu1.add(jMenuItem3);
236
237         jMenuItem4.setText("Save as ...");
238         jMenuItem4.addActionListener(new java.awt.event.ActionListener JavaDoc() {
239             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
240                 TestEditorFrame.this.saveAsTest(evt);
241             }
242         });
243
244         jMenu1.add(jMenuItem4);
245
246         jMenu1.add(jSeparator1);
247
248         jMenuItem5.setText("Exit");
249         jMenuItem5.addActionListener(new java.awt.event.ActionListener JavaDoc() {
250             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
251                 TestEditorFrame.this.exitTest(evt);
252             }
253         });
254
255         jMenu1.add(jMenuItem5);
256
257         jMenuBar1.add(jMenu1);
258
259         jMenu3.setText("Tools");
260         jMenuItem6.setText("Generate Test");
261         jMenuItem6.addActionListener(new java.awt.event.ActionListener JavaDoc() {
262             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
263                 TestEditorFrame.this.generateTest(evt);
264             }
265         });
266
267         jMenu3.add(jMenuItem6);
268
269         jMenu3.add(jSeparator2);
270
271         jMenuItem7.setText("Actions");
272         jMenuItem7.addActionListener(new java.awt.event.ActionListener JavaDoc() {
273             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
274                 TestEditorFrame.this.testThreads(evt);
275             }
276         });
277
278         jMenu3.add(jMenuItem7);
279
280         jMenuBar1.add(jMenu3);
281
282         jMenu2.setText("View");
283         viewExplorerM.setText("Explorer");
284         viewExplorerM.addActionListener(new java.awt.event.ActionListener JavaDoc() {
285             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
286                 TestEditorFrame.this.viewExplorer(evt);
287             }
288         });
289
290         jMenu2.add(viewExplorerM);
291
292         jMenuBar1.add(jMenu2);
293
294         setJMenuBar(jMenuBar1);
295
296     }//GEN-END:initComponents
297

298     private void testThreads(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_testThreads
299
// Add your handling code here:
300
try {
301             java.io.PrintWriter JavaDoc pw=new java.io.PrintWriter JavaDoc(new java.io.FileWriter JavaDoc("/tmp/actions.lst"));
302             TestLogAction tl=new TestLogAction(1);
303             String JavaDoc[] keymap=tl.getKeyMaps();
304             for (int i=0;i < keymap.length;i++) {
305                 pw.println(" <TestLogAction Name=\""+keymap[i]+"\" Command=\"\" />");
306             }
307             pw.close();
308             
309         } catch (Exception JavaDoc ex) {
310             ex.printStackTrace();
311         }
312     }//GEN-LAST:event_testThreads
313

314     private void generateTest(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_generateTest
315
// Add your handling code here:
316
if (Main.getFileName() == null) {
317             System.err.println("You have not specify xml file of the Test.");
318             return;
319         }
320         TestGenerateDialog dlg = new TestGenerateDialog(this,currentDirectory.getAbsolutePath());
321         if (oldpath.length() > 0) {
322             dlg.setPath(oldpath);
323         }
324         if (oldpackage.length() > 0) {
325             dlg.setPackageName(oldpackage);
326         }
327         dlg.show();
328         if (dlg.getState()) {
329             new Thread JavaDoc() {
330                 String JavaDoc path,pack;
331                 
332                 public void start(String JavaDoc pth,String JavaDoc pck) {
333                     path=pth;
334                     pack=pck;
335                     oldpath=path;
336                     oldpackage=pack;
337                     start();
338                 }
339                 
340                 public void run() {
341                     try {
342                         GenerateTests.generateTest(test, Main.getFileName(), path, pack);
343                     } catch (Exception JavaDoc ex) {
344                         ex.printStackTrace();
345                     }
346                 }
347             }.start(dlg.getPath(),dlg.getPackageName());
348         }
349     }//GEN-LAST:event_generateTest
350

351     private void viewExplorer(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_viewExplorer
352
// Add your handling code here:
353
newRootWindow();
354     }//GEN-LAST:event_viewExplorer
355

356   private void exitTest(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_exitTest
357
// Add your handling code here:
358
exitForm(null);
359   }//GEN-LAST:event_exitTest
360

361   private void saveAsTest(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_saveAsTest
362
// Add your handling code here:
363
String JavaDoc dlgFile;
364       
365       if ((dlgFile = fileDlg(false)) != null) {
366           new Thread JavaDoc() {
367               private String JavaDoc file;
368               
369               public void start(String JavaDoc afile) {
370                   file = afile;
371                   start();
372               }
373               
374               public void run() {
375                   Main.saveAsTest(file);
376               }
377           }.start(dlgFile);
378       }
379   }//GEN-LAST:event_saveAsTest
380

381   private void saveTest(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_saveTest
382
// Add your handling code here:
383
if (Main.isNoname()) {
384           saveAsTest(null);
385       }
386       
387       new Thread JavaDoc() {
388           public void run() {
389               Main.saveTest();
390           }
391       }.start();
392   }//GEN-LAST:event_saveTest
393

394   private void openTest(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_openTest
395
// Add your handling code here:
396
String JavaDoc dlgFile;
397       
398       if ((dlgFile = fileDlg(true)) != null) {
399           new Thread JavaDoc() {
400               
401               private String JavaDoc file;
402               
403               public void start(String JavaDoc afile) {
404                   file = afile;
405                   start();
406               }
407               public void run() {
408                   Main.openTest(file);
409               }
410           }.start(dlgFile);
411       }
412   }//GEN-LAST:event_openTest
413

414   private void newTest(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_newTest
415
// Add your handling code here:
416
if (!Main.newTest()) {
417           if (quest("Test was modified. Save it?")) {
418               saveTest(null);
419           }
420       }
421       Main.newTest();
422   }//GEN-LAST:event_newTest
423
/** Exit the Application */
424     private void exitForm(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_exitForm
425
if (Main.isChanged()) {
426             if (quest("Test is modified. Save it?")) {
427                 saveTest(null);
428             }
429         }
430         
431         if (tree != null) {
432             tree.close();
433         }
434         Main.finish(); //I think, it's useless now.
435
System.exit(0);
436     }//GEN-LAST:event_exitForm
437

438     public void killFrame() {
439         setVisible(false);
440     }
441     
442     public boolean quest(String JavaDoc question) {
443         QuestionDialog dlg=new QuestionDialog(this,question);
444         dlg.show();
445         return dlg.getAnswer();
446     }
447     
448     public String JavaDoc fileDlg(boolean open) {
449         
450         JFileChooser fch;
451         File JavaDoc file = null;
452         System.err.println(currentDirectory);
453         fch=new JFileChooser(currentDirectory);
454         fch.setFileFilter(new FileFilter JavaDoc() {
455             public boolean accept(File JavaDoc f) {
456                 String JavaDoc s=f.getName().substring(f.getName().lastIndexOf('.')+1).toLowerCase();
457                 if (f.isDirectory() || s.compareTo("xml") == 0) {
458                     return true;
459                 }
460                 return false;
461             }
462             
463             public String JavaDoc getDescription() {
464                 return "XML Test files";
465             }
466         });
467         if (open) {
468             fch.setDialogTitle("Open Test ...");
469             if (fch.showOpenDialog(fch) == JFileChooser.APPROVE_OPTION)
470                 if (!(file = fch.getSelectedFile()).isFile())
471                     return null;
472         } else {
473             fch.setDialogTitle("Save Test As ...");
474             if (fch.showSaveDialog(fch) == JFileChooser.APPROVE_OPTION) {
475                 file = fch.getSelectedFile();
476             }
477         }
478         if (file == null)
479             return null;
480         else
481             currentDirectory=file.getParentFile();
482         return file.getAbsolutePath();
483     }
484     
485     public void appendHistory(String JavaDoc text) {
486         history.append(text);
487         history.repaint();
488         JScrollBar bar=historyScrollPane.getVerticalScrollBar();
489         if (bar != null) {
490             bar.setValue(bar.getMaximum());
491         }
492     }
493 /*
494     private int horizontalScrollBarPolicy() {
495         return ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS/;
496     }
497  
498     private int verticalScrollBarPolicy() {
499         return ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
500     }
501  */

502     /** Getter for property tree.
503      * @return Value of property tree.
504      *
505      */

506     public TreeDialog getTree() {
507         return tree;
508     }
509     
510     public void setTitleFileName(String JavaDoc name) {
511         setTitle(TITLE+" ["+name+"]");
512     }
513     
514     // Variables declaration - do not modify//GEN-BEGIN:variables
515
private javax.swing.JSeparator JavaDoc jSeparator2;
516     private javax.swing.JPanel JavaDoc jPanel4;
517     private javax.swing.JMenuItem JavaDoc jMenuItem2;
518     private javax.swing.JMenu JavaDoc jMenu2;
519     private javax.swing.JMenuItem JavaDoc jMenuItem5;
520     private javax.swing.JMenuItem JavaDoc jMenuItem3;
521     private javax.swing.JMenuItem JavaDoc viewExplorerM;
522     private javax.swing.JPanel JavaDoc jPanel2;
523     private javax.swing.JMenuItem JavaDoc jMenuItem4;
524     private javax.swing.JMenuItem JavaDoc jMenuItem1;
525     private javax.swing.JMenu JavaDoc jMenu3;
526     private javax.swing.JMenuItem JavaDoc jMenuItem6;
527     private javax.swing.JSeparator JavaDoc jSeparator1;
528     private javax.swing.JEditorPane JavaDoc editor;
529     private javax.swing.JMenu JavaDoc jMenu1;
530     private javax.swing.JTextArea JavaDoc history;
531     private javax.swing.JScrollPane JavaDoc historyScrollPane;
532     private javax.swing.JScrollPane JavaDoc jScrollPane3;
533     private javax.swing.JMenuItem JavaDoc jMenuItem7;
534     private javax.swing.JMenuBar JavaDoc jMenuBar1;
535     // End of variables declaration//GEN-END:variables
536

537 }
538
539
Popular Tags