![]()
Step 2 - Property Change Events
Every time the StormBase or one of the pilots needs to communicate with the application, it fires events to all event listeners. The java.beans package provides the API for these events:
import ice.util.PropertyConstants; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener;The browser needs to implement the PropertyChangeListener interface and register with StormBase as a listener. The class definition therefore now looks like this:
class BrowserFrame extends Frame implements PropertyChangeListener {Creating the Status Line
Create a status line in the browser frame by means of an AWT Label.
At the same time, make use of the viewport argument in the constructor by saving its ID in a property. The properties are now:
private Label statusLabel; private Panel contentPanel; private StormBase stormBase; private String viewportId;Create the following constructor:
BrowserFrame(StormBase stormBase, Viewport viewport) { this.stormBase = stormBase; viewportId = viewport.getId(); contentPanel = new Panel(); stormBase.addPropertyChangeListener(this, viewportId); Font font = new Font("SansSerif", Font.PLAIN, 11); Panel controlPanel = new Panel(); controlPanel.setLayout(new BorderLayout()); statusLabel = new Label("", Label.LEFT); statusLabel.setFont(font); statusLabel.setBackground(Color.lightGray); Panel statusPanel = new Panel(); GridBagLayout gridBagLayout = new GridBagLayout(); GridBagConstraints gridBagConstraints = new GridBagConstraints(); gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; statusPanel.setLayout(gridBagLayout); gridBagLayout.setConstraints(statusLabel, gridBagConstraints); statusPanel.add(statusLabel); controlPanel.add(statusPanel, BorderLayout.SOUTH); add(contentPanel, BorderLayout.CENTER); add(controlPanel, BorderLayout.SOUTH); setSize(600, 600); }The viewportId property is set to the ID of the Viewport.
Using this string, a listener is registered to StormBase with the addPropertyChangeListener method. This ensures all events fired for this Viewport are received.
Some new panels with the status are added.
Receiving Events
The following is the implementation of the PropertyChangeListener interface:
public void propertyChange(PropertyChangeEvent event) { Viewport viewport = (Viewport)event.getSource(); String propertyName = event.getPropertyName(); String newValue; if (event.getNewValue() instanceof String) { newValue = (String)event.getNewValue(); } else { newValue = ""; } if (propertyName.equals(PropertyConstants.STATUS_LINE)) { statusLabel.setText(newValue); } else if (propertyName.equals(PropertyConstants.OUTSTANDING_IMAGES)) { if (!newValue.equals("0")) { statusLabel.setText("loading images: " + newValue + " left"); } else { statusLabel.setText("loading images: done"); } } else if (propertyName.equals(PropertyConstants.CONTENT_LOADING)) { if (newValue.equals(PropertyConstants.ERROR)) { ContentLoader contentLoader = (ContentLoader)event.getOldValue(); if (contentLoader != null) { statusLabel.setText(viewport.getName() + ": " + contentLoader.getException()); } else { statusLabel.setText(viewport.getName() + ": loading error"); } } } }The code gets the source of the event, which is always a Viewport. The property name is always a String, whereas the new value is always an Object.
The event is checked to see if it should affect the status line. For more information on events, see Working with Property Change Events. The events used in this code are:
- statusLine
The application displays a message in the status line.- outstandingImages
This property is for HTML documents. It indicates how many images remain to be loaded by the image loader threads.- contentLoading
This property has several possible values, including error. If your code receives this event, it should provide some useful information. An ice.storm.ContentLoader should be given as the old value. This should contain an Exception object, which you can print to the status line. If there is no ContentLoader, you can indicate there is an error.At the completion of this step, you have a browser with a status line at the bottom of the window that displays the loading progress.
|
Copyright 2005. ICEsoft Technologies, Inc. http://www.icesoft.com |