View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program 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
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.plugin.calpahtml;
21  
22  import java.awt.event.ActionEvent;
23  import java.net.MalformedURLException;
24  import java.net.URL;
25  
26  import javax.swing.AbstractAction;
27  import javax.swing.Action;
28  import javax.swing.JRootPane;
29  
30  import org.xnap.XNap;
31  import org.xnap.gui.ActionProvider;
32  import org.xnap.gui.StatusBar;
33  import org.xnap.gui.util.IconHelper;
34  
35  import calpa.html.CalCons;
36  import calpa.html.CalHTMLPane;
37  import calpa.html.CalHTMLPreferences;
38  import calpa.html.DefaultCalHTMLObserver;
39  
40  
41  public class CalpaHtmlPanel extends JRootPane implements ActionProvider {
42  
43      // --- Data Field(s) ---
44  
45      private Action backAction = new BackAction();
46      private Action forwardAction = new ForwardAction();
47      private Action reloadAction = new ReloadAction();
48  	public Action stopAction = new StopAction();
49  
50  	private CalHTMLPane htmlPane;
51  
52      // --- Constructor(s) ---
53  
54      public CalpaHtmlPanel() 
55      {
56  
57  		CalHTMLPreferences pref = new CalHTMLPreferences();
58  		pref.setShowTestNavBar(true);
59  
60  		htmlPane= new CalHTMLPane(pref, new CalHTMLObserver(this), null);
61  		URL url = null;
62  	  
63  		try {
64  			url = new URL("http://xnap.sf.net/about.html");
65  		} 
66  		catch (MalformedURLException e) { 
67  		}
68  				
69  		htmlPane.setVisible(true);
70  		getContentPane().add(htmlPane);
71  		htmlPane.showHTMLDocument(url);
72      }
73  
74      // --- Method(s) ---
75  
76  
77      public Action[] getActions()
78      {
79  		return new Action[] { backAction, forwardAction, reloadAction, 
80  							  stopAction };
81      }
82  
83  
84      // --- Inner Class(es) ---
85  
86      /***
87       * Goes back in the browsing history
88       */
89      private class BackAction extends AbstractAction 
90  	{
91          public BackAction() 
92  		{
93              putValue(Action.NAME, XNap.tr("Back"));
94              putValue(Action.SHORT_DESCRIPTION,
95  					 XNap.tr("Go back"));
96  			putValue(IconHelper.XNAP_ICON, "1leftarrow.png");
97          }
98  
99          public void actionPerformed(ActionEvent event) 
100 		{
101 			htmlPane.goBack();
102 		}
103     } 
104 
105     /***
106      * Goes forward in the browsing history
107      */
108     private class ForwardAction extends AbstractAction 
109 	{
110         public ForwardAction() 
111 		{
112             putValue(Action.NAME, XNap.tr("Forward"));
113             putValue(Action.SHORT_DESCRIPTION,
114 					 XNap.tr("Go forward"));
115 			putValue(IconHelper.XNAP_ICON, "1rightarrow.png");
116         }
117 
118         public void actionPerformed(ActionEvent event) 
119 		{
120 			htmlPane.goForward();
121 		}
122     } 
123 
124     /***
125      * Reloads the current document
126      */
127     private class ReloadAction extends AbstractAction 
128 	{
129         public ReloadAction() 
130 		{
131             putValue(Action.NAME, XNap.tr("Reload"));
132             putValue(Action.SHORT_DESCRIPTION,
133 					 XNap.tr("Reload current document"));
134 			putValue(IconHelper.XNAP_ICON, "reload.png");
135         }
136 
137         public void actionPerformed(ActionEvent event) 
138 		{
139 			htmlPane.reloadDocument();
140 		}
141     } 
142     /***
143      * Stops loading the current document
144      */
145     private class StopAction extends AbstractAction 
146 	{
147         public StopAction() 
148 		{
149             putValue(Action.NAME, XNap.tr("Stop"));
150             putValue(Action.SHORT_DESCRIPTION,
151 					 XNap.tr("Stop loading the document"));
152 			putValue(IconHelper.XNAP_ICON, "stop.png");
153         }
154 
155         public void actionPerformed(ActionEvent event) 
156 		{
157 			htmlPane.stopAll();
158 		}
159     } 
160 
161 
162   private static class CalHTMLObserver extends DefaultCalHTMLObserver {
163 	  
164 	  private CalpaHtmlPanel parent;
165 
166       public CalHTMLObserver(CalpaHtmlPanel parent) 
167 	  {
168 		  super();
169 		  this.parent = parent;
170       }
171 	  
172       public void linkFocusedUpdate(CalHTMLPane p, URL url) 
173 	  {
174 		  StatusBar.setText((url == null) ? "" : url.toExternalForm());
175       }
176 
177 	  public void statusUpdate(CalHTMLPane p, int status, java.net.URL url,
178 							   int value, java.lang.String message) 
179 	  {
180 		  if (status == CalCons.DOC_LOADED || status == CalCons.PARSE_FAILED 
181 			  || status == CalCons.PARSE_FAILED_POST_CONNECT)
182 			  {
183 				  parent.stopAction.setEnabled(false);
184 			  }
185 		  if (status == CalCons.PRE_CONNECT || 
186 			  status == CalCons.WAITING_FOR_IMAGES)
187 			  {
188 				  parent.stopAction.setEnabled(true);
189 			  }
190 	  }
191 	  public void historyUpdate(CalHTMLPane pane,
192                           int position)
193 	  {
194 		  
195 		  if (position == CalCons.AT_HISTORY_MIDDLE) 
196 			  { 
197 				  parent.forwardAction.setEnabled(true);
198 				  parent.backAction.setEnabled(true);
199 			  } 
200 		  else 
201 			  { 
202 				  if ((position & CalCons.AT_HISTORY_TOP) > 0) { 
203 					  parent.forwardAction.setEnabled(false);
204 				  } 
205 				  else { 
206 					  parent.forwardAction.setEnabled(true);				  
207 				  } 
208 				  if ((position & CalCons.AT_HISTORY_BOTTOM) > 0) { 
209 					  parent.backAction.setEnabled(false);
210 				  } 
211 				  else { 
212 					  parent.backAction.setEnabled(true);
213 				  } 
214 			  } 
215 	  }
216   }
217 }