Ultrashock Tutorials > Flash 8 > XML Portfolio Viewer  
 
by Jim Armstrong, 2112 FX::Singularity
Download Source Files 
 
XML Portfolio Viewer
 
 Introduction and Code Requirements 
 Section 01: Layout XML  
 Section 02: Portfolio XML  
 Section 03: Architecture  
 Section 04: Preloading  
 Section 05: Error Handling  
 Section 06: pair & triple classes 
 Section 07: Observer-Observable Utility 

 Section 08: Style Manager
 Section 09: Layout Items  
 Section 10: Custom Layout Classes  
 Section 11: Glyph Class 
 Section 12: Multi-dimensional Thumbnailer  
 Section 13: Model 
 Section 14: View 
 Section 15: Controller 
 Section 16: Suggestions for Improvement 

Style Manager

The Style Manager used in the portfolio viewer is a straightforward implementation of manager classes you may have seen in online tutorials and books. A container class is used to store style properties and register listeners for style information. The style manager class is responsible for maintaining the entire collection of named styles and broadcasting style information to listeners. Listeners are required to implement a prescribed interface in order to receive style information.

A change in style properties affects the style holder class and a small amount of code in the Model parser and listener object. The style manager architecture and implementation remains unchanged.

StyleLayout Class

The StyleLayout class contains properties representing style information for the layout. The current implementation uses TextFields to represent layout items such as Label or TextArea components. Most of the style information involves simple TextField and TextFormat properties.

Style properties have public access as they can be set to practically anything and the application does not fail. The worst result is the user not achieving the desired application appearance. These properties are error checked during the parsing phase. Public access is simpler and preserves memory as there is no need to provide getter & setter functions that do nothing more than retrieve and set a variable. That's the definition of public access :)

As the application is modified to use more sophisticated visual display tools, the StyleLayout class a convenient place to store common style and skin properties.

The StyleLayout class contains methods to add listeners, remove listeners, clear the listener list, and an update() method, which is used by the Style Manager class.

The code for the update() method is interesting.

public function update():Void
{
  for( var i=__myListeners.length-1; i>=0; i-- )
    __myListeners[i].updateStyle(this);
};

Components are listeners for style information, so each component (LayoutItem or its subclass) must implement an updateStyle() method to respond to broadcasts of style information.

LayoutInterface

This interface is repeated in the next section, but it should be evident that a LayoutItem is required to implement at least a notify() method in order to be an Observer and an updateStyle() method to respond to style broadcasts. As it happens, a LayoutItem is required to implement three methods, defined in the LayoutInterface,

interface LayoutInterface
{
  // respond to style updates
  public function updateStyle(_s:StyleLayout):Void
  // an observer must have an accessible name property
  public function getName():String;
  // observer responds to notification from observed subject
  public function notify( _info:Object ):Void;
}

Implementing this interface ensures that a LayoutItem or its subclass can respond to notification from an ObservableObject and an update from a StyleLayout instance. This is why there is no Observer interface; LayoutInterface is a superset of Observer.

StyleMgr Class

The StyleMgr class contains an array of StyleLayout instances, indexed by style name. As an individual style is parsed in the layout.xml file, the style name is used to add a new reference to this array. The style name is used to lookup the appropriate StyleLayout instance to register components for style updates. An update() method is provided to update all style information to all registered listeners at once.

To see how this works, examine the layout.xml file. Most layouts use two styles -- 'std' and 'darkblue.' When the 'std' style is parsed, a new StyleLayout instance is created. Style properties are assigned to that StyleLayout instance. A reference to the StyleLayout instance is stored in the __styles array.

The process is repeated when parsing the 'darkblue' style. After parsing all styles, the __styles array contains two values, __styles['std'] and __styles['darkblue']. Each value contains a reference to a StyleLayout instance, corresponding to that named style.

In most layouts, the INSTRUCT and STATUS components register as listeners for the 'std' style. When the <style> tag is parsed for each of those components, the style name and a reference to the component are passed to the StyleMgr addComponent() method. The StyleMgr looks up the 'std' style in the __styles array, returning a reference to a StyleLayout instance. The StyleLayout addListener() method is called to register the component as a listener for the style. For most layouts, INSTRUCT and STATUS are registered as listeners for the 'std' style. The SYNOPSIS component is registered as a listener for the 'darkblue' style.

Since each component implements the LayoutInterface, the component class contains an updateStyle() method. When the StyleMgr class update() method is called, it loops through each named style in the collection, calling its update() method, which in turn calls the updateStyle() method for each registered listener.

For the 'std' style, the INSTRUCT and STATUS components receive references to the StyleLayout class containing their style information. The information is retrieved and applied in the LayoutItem updateStyle() method.

Changing style information is a matter of adding and deleting properties in the StyleLayout class and then using the new information in the LayoutItem class. We have the flexibility to modify those properties and apply any style to any component in XML.

 
©2006 Ultrashock.com - All rights reserved