View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2006 Christian W. Hargraves (engrean@hotmail.com)
4   
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  package net.sf.jameleon.ui;
20  
21  import org.jdesktop.jdic.browser.*;
22  
23  import javax.swing.*;
24  import java.net.URL;
25  import java.awt.*;
26  import java.awt.event.ActionListener;
27  import java.awt.event.ActionEvent;
28  import java.io.File;
29  
30  import net.sf.jameleon.util.JameleonUtility;
31  import net.sf.jameleon.util.Configurator;
32  import net.sf.jameleon.util.JameleonDefaultValues;
33  
34  public class BasicHtmlBrowser extends JFrame{
35  
36      private IWebBrowser browser;
37      private boolean browserNotSupported;
38      protected JButton backButton = new JButton("Back");
39      protected JButton forwardButton = new JButton("Forward");
40  
41      public BasicHtmlBrowser(String title){
42          super(title);
43  	    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
44          init();
45      }
46  
47      public BasicHtmlBrowser(String title, URL url){
48          this(title);
49          goToUrl(url);
50      }
51  
52      public void goToUrl(URL url){
53          if (browser != null){
54              browser.setURL(url);
55              setVisible(true);
56          }else{
57              String errMsg = "<html>Please set your default browser to IE (if under windows) or install Mozilla (if not " +
58                      "under Windows) to enable this feature.</html>";
59              if (browserNotSupported){
60                  errMsg = "<html><body>The browser functionality is not supported on your operating system.</body></html>";
61              }
62              JOptionPane.showMessageDialog(
63                              this,
64                              errMsg,
65                              "Error Starting Default Browser",
66                              JOptionPane.ERROR_MESSAGE);
67          }
68      }
69  
70      protected void init(){
71          backButton.addActionListener(new ActionListener(){
72              public void actionPerformed(ActionEvent e) {
73                  if (browser.isBackEnabled()){
74                      browser.back();
75                  }
76              }
77          });
78  
79          forwardButton.addActionListener(new ActionListener(){
80              public void actionPerformed(ActionEvent e) {
81                  if (browser.isForwardEnabled()){
82                      browser.forward();
83                  }
84              }
85          });
86  
87  
88          JPanel northP = new JPanel(new SpringLayout());
89          northP.add(backButton);
90          northP.add(forwardButton);
91  
92          SpringUtilities.makeCompactGrid(northP,
93                                          1, 2,   //rows, cols
94                                          6, 6,   //initX, initY
95                                          6, 6);  //xPad, yPad
96  
97  
98          try{
99              BrowserEngineManager bem = BrowserEngineManager.instance();
100             IBrowserEngine be = bem.getActiveEngine();
101             if (be != null){
102                 browser = be.getWebBrowser();
103                 browser.addWebBrowserListener(new BrowserListener());
104                 getContentPane().add(northP, BorderLayout.NORTH);
105                 getContentPane().add(browser.asComponent(), BorderLayout.CENTER);
106             }
107             pack();
108             setSize(850,600);
109         }catch(UnsatisfiedLinkError err){
110             browserNotSupported = true;
111         }
112     }
113 
114     private class BrowserListener implements WebBrowserListener{
115         public void documentCompleted(WebBrowserEvent event) {
116         }
117         public void downloadCompleted(WebBrowserEvent event) {
118         }
119         public void downloadError(WebBrowserEvent event) {
120         }
121         public void downloadProgress(WebBrowserEvent event) {
122         }
123         public void downloadStarted(WebBrowserEvent event) {
124         }
125         public void statusTextChange(WebBrowserEvent event) {
126         }
127         public void titleChange(WebBrowserEvent event) {
128             setTitle(event.getData());
129         }
130         public void initializationCompleted(WebBrowserEvent event) {
131         }
132         public void windowClose(WebBrowserEvent event) {
133         }
134     }
135 }