View Javadoc

1   /***
2    * 
3    */
4   package org.telscenter.pas.steps;
5   
6   import info.clearthought.layout.TableLayout;
7   import info.clearthought.layout.TableLayoutConstants;
8   
9   import java.awt.BorderLayout;
10  import java.awt.Dimension;
11  import java.awt.FlowLayout;
12  import java.awt.event.ActionEvent;
13  import java.awt.event.ActionListener;
14  import java.net.MalformedURLException;
15  import java.net.URL;
16  import java.util.ArrayList;
17  import java.util.Collections;
18  import java.util.List;
19  
20  import javax.swing.AbstractAction;
21  import javax.swing.Action;
22  import javax.swing.BorderFactory;
23  import javax.swing.ImageIcon;
24  import javax.swing.JButton;
25  import javax.swing.JDialog;
26  import javax.swing.JPanel;
27  import javax.swing.JProgressBar;
28  import javax.swing.JSplitPane;
29  import javax.swing.JTextField;
30  import javax.swing.JToolBar;
31  
32  import org.telscenter.pas.common.ui.CommonUI;
33  import org.telscenter.pas.ui.icons.PasCommonIconProvider;
34  import org.telscenter.pas.ui.util.PasColors;
35  
36  /***
37   * @author aperritano
38   * 
39   */
40  public class BrowseWebUI extends JPanel {
41  
42  	protected JSplitPane splitPane;
43  
44  	protected int dividerLocation = 0;
45  
46  	protected ImageIcon homeActive = new ImageIcon(PasCommonIconProvider
47  			.getImage("homeActive.png"));
48  	protected ImageIcon stopActive = new ImageIcon(PasCommonIconProvider
49  			.getImage("stopActive.png"));
50  	protected ImageIcon nextActive = new ImageIcon(PasCommonIconProvider
51  			.getImage("nextActive.png"));
52  	protected ImageIcon previousActive = new ImageIcon(PasCommonIconProvider
53  			.getImage("previousActive.png"));
54  	protected ImageIcon refreshActive = new ImageIcon(PasCommonIconProvider
55  			.getImage("refreshActive.png"));
56  
57  	protected ImageIcon homeRollover = new ImageIcon(PasCommonIconProvider
58  			.getImage("homeRollover.png"));
59  	protected ImageIcon stopRollover = new ImageIcon(PasCommonIconProvider
60  			.getImage("stopRollover.png"));
61  	protected ImageIcon nextRollover = new ImageIcon(PasCommonIconProvider
62  			.getImage("nextRollover.png"));
63  	protected ImageIcon previousRollover = new ImageIcon(PasCommonIconProvider
64  			.getImage("previousRollover.png"));
65  	protected ImageIcon refreshRollover = new ImageIcon(PasCommonIconProvider
66  			.getImage("refreshRollover.png"));
67  
68  	private BrowseWeb browseWeb;
69  
70  	protected JPanel browserToolbarPanel;
71  
72  	protected JToolBar browserToolbar;
73  
74  	private JProgressBar progressBar;
75  
76  	private JTextField addressTextField;
77  
78  	private JButton saveLinkButton;
79  
80  	private int vSpace;
81  
82  	private int hSpace;
83  
84  	private JButton goButton;
85  
86  	private List<URL> urlHistory = new ArrayList<URL>();
87  	/***
88  	 * 
89  	 */
90  	public BrowseWebUI() {
91  		// TODO Auto-generated constructor stub
92  	}
93  
94  	/***
95  	 * @param bookmark
96  	 */
97  	public BrowseWebUI(BrowseWeb browseWeb) {
98  		this.browseWeb = browseWeb;
99  		init();
100 	}
101 
102 	/*
103 	 * Initializes the panel
104 	 */
105 	public void init() {
106 
107 		splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new JPanel(),
108 				browseWeb.getWebBrowserComponent());
109 		splitPane.setOneTouchExpandable(true);
110 		splitPane.setBorder(BorderFactory.createEmptyBorder());
111 		splitPane.setDividerLocation(dividerLocation);
112 		splitPane.setDividerSize(0);
113 
114 		browserToolbarPanel = new JPanel(new BorderLayout(0, 0));
115 		browserToolbarPanel.add(this.createBrowserToolbarPanel(),
116 				BorderLayout.WEST);
117 	
118 		browserToolbarPanel.setBackground(PasColors.browseWebBackgroundColor);
119 
120 		progressBar = new JProgressBar();
121 		progressBar.setStringPainted(true);
122 
123 		// browserToolbarPanel.add(progressBar,BorderLayout.SOUTH);
124 
125 		this.setLayout(new BorderLayout(0, 0));
126 		// this.setBackground(PasColors.browseWebBackgroundColor);
127 		this.add(browserToolbarPanel, BorderLayout.NORTH);
128 		this.add(splitPane, BorderLayout.CENTER);
129 	}
130 	
131 	
132 	/*
133 	 * sets the toolbar to on or off
134 	 */
135 	public void setBrowserToolbarPanelVisible(boolean isVisible) {
136 		browserToolbarPanel.setVisible(isVisible);
137 	}
138 
139 	/*
140 	 * This may not actually be used when using the mozswing browser
141 	 */
142 	protected JPanel createAddressBar() {
143 		
144 		///JPanel addressPanel = new JPanel(new BorderLayout(0,0));
145 		
146 		addressTextField = new JTextField();
147 		
148 		if( browseWeb.getUrl() != null)
149 			addressTextField.setText(browseWeb.getUrl().toString());
150 		
151 		//addressPanel.add(addressTextField,BorderLayout.CENTER);
152 		
153 		goButton = new JButton();
154 		goButton.setAction(new SaveLinkAction());
155 		goButton.setText("Go");
156 		goButton.setToolTipText("Go to this address");
157 		goButton.addActionListener(new ActionListener() {
158 
159 			public void actionPerformed(ActionEvent e) {
160 				try {
161 					browseWeb.setUrl(new URL(addressTextField.getText()));
162 					browseWeb.getComponent().validate();
163 				} catch (MalformedURLException e1) {
164 					//todo pop and error if bad url
165 					e1.printStackTrace();
166 				}
167 			}});
168 		//CommonUI.makeButtonTransparent(goButton);
169 		//addressPanel.add(goButton,BorderLayout.EAST);
170 		
171 		JPanel addressPanel = new JPanel();
172 
173 		final double pref = TableLayoutConstants.PREFERRED;
174 		final double fill = TableLayoutConstants.FILL;
175 		vSpace = 6;
176 		hSpace = 6;
177 		final double size[][] = {
178 				{ fill, hSpace, pref,hSpace
179 				},
180 
181 				{ vSpace,pref}
182 
183 		};
184 		addressPanel.setLayout(new TableLayout(size));
185 		addressPanel.add(addressTextField,"0,1");
186 		addressPanel.add(goButton,"2,1");
187 		addressPanel.setOpaque(false);
188 		return addressPanel;
189 	}
190 	
191 	/*
192 	 * This may not actually be used when using the mozswing browser
193 	 */
194 	protected JPanel createSaveLinkButtonPanel() {
195 		
196 		JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
197 		
198 		saveLinkButton = new JButton();
199 		//saveLinkButton.setAction(new SaveLinkAction());
200 		saveLinkButton.setText("Save Link");
201 		saveLinkButton.setToolTipText("Save this Link");
202 		panel.setBackground(PasColors.browseWebBackgroundColor);
203 		panel.setBorder(BorderFactory.createEmptyBorder(vSpace, hSpace, 0, hSpace));
204 		panel.add(saveLinkButton);
205 		panel.setOpaque(false);
206 		return panel;
207 	}
208 	
209 	class SaveLinkAction extends AbstractAction {
210 		public void actionPerformed(ActionEvent e) {
211 			URL address = browseWeb.getBrowser().getUrl();
212 		}
213 		
214 	}
215 	public Action getPreviewAction(boolean hasAddressBar) {
216 		return null;
217 		//return new PopupPreviewAction(hasAddressBar);
218 	}
219 	
220 	public List<URL> getUrlHistory() {
221 		return Collections.unmodifiableList(urlHistory);
222 	}
223 	
224 	public void addHistoryUrl(URL url) {
225 		urlHistory.add(url);
226 	}
227 	
228 	
229 	
230 	/*
231 	 * This gets called but the contents are not actually used in the browser.
232 	 * It seems as if the mozswing browser overrides all of this.
233 	 */
234 	protected JPanel createBrowserToolbarPanel() {
235 		browserToolbar = new JToolBar();
236 		browserToolbar.setFloatable(false);
237 		browserToolbar.setRollover(true);
238 		browserToolbar.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
239 
240 		JButton previousButton = new JButton();
241 		previousButton.setAction(previousAction);
242 		previousButton.setIcon(previousActive);
243 		previousButton.setRolloverIcon(previousRollover);
244 
245 		previousButton.setToolTipText("Back");
246 		CommonUI.makeButtonTransparent(previousButton);
247 		previousButton.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
248 
249 		JButton nextButton = new JButton();
250 		nextButton.setAction(nextAction);
251 		nextButton.setIcon(nextActive);
252 		nextButton.setRolloverIcon(nextRollover);
253 
254 		nextButton.setToolTipText("Forward");
255 		CommonUI.makeButtonTransparent(nextButton);
256 		nextButton.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
257 
258 		JButton refreshButton = new JButton();
259 		refreshButton.setAction(refreshAction);
260 		refreshButton.setIcon(refreshActive);
261 		refreshButton.setRolloverIcon(refreshRollover);
262 		refreshButton.setToolTipText("Refresh");
263 		CommonUI.makeButtonTransparent(refreshButton);
264 		refreshButton.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
265 
266 		JButton stopButton = new JButton();
267 
268 		stopButton.setAction(stopAction);
269 		stopButton.setIcon(stopActive);
270 		stopButton.setRolloverIcon(stopRollover);
271 		stopButton.setToolTipText("Stop Page Load");
272 		CommonUI.makeButtonTransparent(stopButton);
273 		stopButton.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
274 
275 		JButton homeButton = new JButton();
276 
277 		homeButton.setAction(homeAction);
278 		homeButton.setIcon(homeActive);
279 		homeButton.setRolloverIcon(homeRollover);
280 		homeButton.setToolTipText("Home Page");
281 		CommonUI.makeButtonTransparent(homeButton);
282 		homeButton.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
283 
284 		browserToolbar.add(previousButton);
285 		browserToolbar.add(nextButton);
286 		browserToolbar.add(refreshButton);
287 		browserToolbar.add(stopButton);
288 		browserToolbar.add(homeButton);
289 		browserToolbar.setOpaque(false);
290 
291 		JPanel tPanel = new JPanel(new BorderLayout(0, 0));
292 		tPanel.add(browserToolbar, BorderLayout.WEST);
293 		tPanel.setOpaque(false);
294 
295 		return tPanel;
296 
297 	}
298 
299 	/***
300 	 * @param hasLink
301 	 */
302 	public void useSaveLinkButton(boolean hasLink) {
303 		if( hasLink )
304 			browserToolbarPanel.add(this.createSaveLinkButtonPanel(),
305 					BorderLayout.EAST);
306 		
307 	}
308 
309 	/***
310 	 * @param hasAddressBar
311 	 */
312 	public void useAddressBar(boolean hasAddressBar) {
313 		if( hasAddressBar )
314 		browserToolbarPanel.add(this.createAddressBar(),
315 				BorderLayout.CENTER);
316 		
317 	}
318 
319 	public void addButtonBrowserToolbar(JButton button) {
320 		browserToolbar.add(button);
321 	}
322 
323 	/***
324 	 * @return the browserToolbarPanel
325 	 */
326 	public JPanel getBrowserToolbarPanel() {
327 		return browserToolbarPanel;
328 	}
329 
330 	/***
331 	 * @param browserToolbarPanel
332 	 *            the browserToolbarPanel to set
333 	 */
334 	public void setBrowserToolbarPanel(JPanel browserToolbarPanel) {
335 		this.browserToolbarPanel = browserToolbarPanel;
336 	}
337 
338 	protected Action nextAction = new AbstractAction(null) {
339 		public void actionPerformed(ActionEvent e) {
340 			browseWeb.getBrowser().forward();
341 
342 		}
343 	};
344 
345 	protected Action previousAction = new AbstractAction(null) {
346 		public void actionPerformed(ActionEvent e) {
347 			browseWeb.getBrowser().back();
348 		}
349 	};
350 
351 	protected Action homeAction = new AbstractAction(null) {
352 		public void actionPerformed(ActionEvent e) {
353 			browseWeb.setUrl(browseWeb.getUrl());
354 		}
355 	};
356 
357 	protected Action stopAction = new AbstractAction(null) {
358 		public void actionPerformed(ActionEvent e) {
359 			browseWeb.getBrowser().stop();
360 		}
361 	};
362 
363 	protected Action refreshAction = new AbstractAction(null) {
364 		public void actionPerformed(ActionEvent e) {
365 			browseWeb.getBrowser().refresh();
366 		}
367 	};
368 
369 	/***
370 	 * @return the progressBar
371 	 */
372 	public JProgressBar getProgressBar() {
373 		return progressBar;
374 	}
375 
376 	/***
377 	 * @param progressBar
378 	 *            the progressBar to set
379 	 */
380 	public void setProgressBar(JProgressBar progressBar) {
381 		this.progressBar = progressBar;
382 	}
383 
384 }