1 19 20 package org.netbeans.modules.webclient; 21 22 import java.awt.*; 23 import java.awt.event.*; 24 import java.beans.PropertyChangeListener ; 25 import java.beans.PropertyChangeEvent ; 26 28 import org.openide.windows.TopComponent; 29 30 import org.mozilla.webclient.*; 31 32 import org.openide.util.NbBundle; 33 import org.openide.util.RequestProcessor; 34 35 40 class WebclientBrowserComponent extends Frame { 41 42 private static final int DEFAULT_WIDTH = 400; 43 private static final int DEFAULT_HEIGHT = 350; 44 45 private static final boolean debug = true; 46 47 private WebclientBrowserImpl browserImpl; 48 49 private BrowserControlCanvas browser; 50 51 private Button bPrev; 52 53 private Button bNext; 54 55 private Button bHome; 56 57 private Button bReload; 58 59 private Button bStop; 60 61 private TextField tfLocation; 62 63 private BrowserListener bListener; 64 65 66 public WebclientBrowserComponent (WebclientBrowserImpl browserImpl) { 67 super (); 68 if (debug) System.out.println ("Creating MozillaBrowserComponent"); setLayout (new BorderLayout ()); 70 this.browserImpl = browserImpl; 71 72 try { 73 browser = (BrowserControlCanvas) 74 browserImpl.getBrowserControl ().queryInterface(BrowserControl.BROWSER_CONTROL_CANVAS_NAME); 75 add (browser, "Center"); browser.setVisible (true); 77 78 bListener = new BrowserListener (); 79 addWindowListener(bListener); 80 81 initToolbar (); 82 setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 83 } 84 catch (ClassNotFoundException ex) { 85 ex.printStackTrace (); 86 } 87 catch (NullPointerException ex) { 88 ex.printStackTrace (); 89 } 90 } 91 92 private void initToolbar () { 93 Panel toolbar = new Panel (); 94 toolbar.add (bPrev = new Button (NbBundle.getMessage(WebclientBrowserComponent.class,"LBL_Prev"))); 95 toolbar.add (bNext = new Button (NbBundle.getMessage(WebclientBrowserComponent.class,"LBL_Next"))); 96 toolbar.add (bStop = new Button (NbBundle.getMessage(WebclientBrowserComponent.class,"LBL_Stop"))); 97 toolbar.add (bReload = new Button (NbBundle.getMessage(WebclientBrowserComponent.class,"LBL_Reload"))); 98 toolbar.add (bHome = new Button (NbBundle.getMessage(WebclientBrowserComponent.class,"LBL_Home"))); 99 toolbar.add (tfLocation = new TextField (30)); 100 add (toolbar, "North"); bPrev.addActionListener(bListener); 102 bNext.addActionListener(bListener); 103 bStop.addActionListener(bListener); 104 bReload.addActionListener(bListener); 105 bHome.addActionListener(bListener); 106 tfLocation.addActionListener(bListener); 107 browserImpl.addPropertyChangeListener(bListener); 108 refreshToolbar (); 109 } 110 111 112 void refreshToolbar () { 113 bPrev.setEnabled(browserImpl.isBackward()); 114 bNext.setEnabled(browserImpl.isForward()); 115 java.net.URL url = browserImpl.getURL(); 116 if (url != null) { 117 if (debug) System.out.println("setText to "+url); tfLocation.setText(url.toString()); 119 } 120 } 121 122 125 public void addNotify() { 126 super.addNotify (); 127 browserImpl.initialize (); 128 } 129 130 133 public java.awt.Dimension getPreferredSize () { 134 java.awt.Dimension superPref = super.getPreferredSize (); 135 return new java.awt.Dimension ( 136 Math.max (DEFAULT_WIDTH, superPref.width), 137 Math.max (DEFAULT_HEIGHT, superPref.height) 138 ); 139 } 140 141 144 private void delete () { 145 browser.setVisible (false); 146 browser = null; 147 } 148 149 private class BrowserListener 150 implements ActionListener, WindowListener, PropertyChangeListener , Runnable { 151 public void actionPerformed (ActionEvent e) { 152 if (e.getSource () == bPrev) { 153 browserImpl.backward(); 154 } 155 else if (e.getSource () == bNext) { 156 browserImpl.forward(); 157 } 158 else if (e.getSource () == bHome) { 159 try { 160 java.net.URL homePage = new java.net.URL (org.openide.awt.HtmlBrowser.getHomePage()); 161 browserImpl.setURL(homePage); 162 } 163 catch (java.net.MalformedURLException ex) { 164 browserImpl.setStatusMessage(NbBundle.getMessage(WebclientBrowserComponent.class,"MSG_Cannot_get_home_page")); 165 } 166 } 167 else if (e.getSource () == bStop) { 168 browserImpl.stopLoading(); 169 } 170 else if (e.getSource () == bReload) { 171 browserImpl.reloadDocument (); 172 } 173 else if (e.getSource () == tfLocation) { 174 try { 175 browserImpl.setURL (new java.net.URL (tfLocation.getText())); 176 } 177 catch (java.net.MalformedURLException ex) { 178 tfLocation.setText (""); 179 } 180 } 181 } 182 183 public void propertyChange (PropertyChangeEvent evt) { 184 String property = evt.getPropertyName (); 185 if (property == null) { 186 RequestProcessor.postRequest (this); 187 return; 188 } 189 190 if (property.equals (browserImpl.PROP_URL) || 191 property.equals (browserImpl.PROP_TITLE) || 192 property.equals (browserImpl.PROP_BACKWARD) || 193 property.equals (browserImpl.PROP_FORWARD)) 194 RequestProcessor.postRequest (this); 195 } 196 197 198 private void close () { 199 browserImpl.destroy (); 203 } 204 205 public void windowClosing(WindowEvent e) { 206 setVisible (false); 207 } 208 public void windowClosed(WindowEvent e) {} 209 public void windowActivated(java.awt.event.WindowEvent windowEvent) {} 210 public void windowDeactivated(java.awt.event.WindowEvent windowEvent) {} 211 public void windowDeiconified(java.awt.event.WindowEvent windowEvent) {} 212 public void windowIconified(java.awt.event.WindowEvent windowEvent) {} 213 public void windowOpened(java.awt.event.WindowEvent windowEvent) {} 214 215 public void run() { 216 refreshToolbar(); 217 } 218 219 } 220 } | Popular Tags |