FileDocCategorySizeDatePackage
SimpleCanvasManager.javaAPI DocphoneME MR2 API (J2ME)12918Wed May 02 18:00:34 BST 2007com.sun.perseus.model

SimpleCanvasManager

public class SimpleCanvasManager extends Object implements com.sun.perseus.util.RunnableQueue.RunnableHandler, UpdateListener

The SimpleCanvasManager class is responsible for keeping the rendering of a ModelNode tree on a RenderGraphics current.

Specifically, the SimpleCanvasManager listens to update events in a ModelNode tree and triggers repaint into the RenderGraphics when necessary.

The SimpleCanvasManager does not handle documents that have not been loaded and does nothing on loadComplete calls.

version
$Id: SimpleCanvasManager.java,v 1.15 2006/06/29 10:47:34 ln156897 Exp $

Fields Summary
public final Object
lock
The object used as a lock to synchronize access to the paint surface.
protected boolean
canvasConsumed
This flag should be used by the consumer of the offscreen buffer to let the SimpleCanvasManager know when the updated offscreen has been consumed.
protected com.sun.perseus.j2d.RenderGraphics
rg
The RenderGraphics which this SimpleCanvasManager keeps up to date with its associated model changes.
protected DocumentNode
documentNode
Model which this SimpleCanvasManager renders to the associated RenderGraphics
protected CanvasUpdateListener
canvasUpdateListener
Listens to canvas updates
protected boolean
needRepaint
Controls whether a repaint is needed or not
protected com.sun.perseus.j2d.RGB
clearPaint
The color used to clear the canvas.
protected DirtyAreaManager
dirtyAreaManager
Dirty area manager. May be null.
protected boolean
isOff
When off, no updates are made to the rendering surface.
Constructors Summary
public SimpleCanvasManager(com.sun.perseus.j2d.RenderGraphics rg, DocumentNode documentNode, CanvasUpdateListener canvasUpdateListener)

param
rg the RenderGraphics which this instance will keep up to date with the model changes.
param
documentNode the DocumentNode, root of the tree that this SimpleCanvasManager will draw and keep current on the RenderGraphics
param
canvasUpdateListener the CanvasUpdateListener which listens to completed updates on the associated RenderGraphics
throws
IllegalArgumentException if rg, documentNode or listener is null.


                                                                                                               
     
              
              
               

        if (rg == null 
            || documentNode == null 
            || canvasUpdateListener == null) {
            throw new IllegalArgumentException(
                    "RenderGraphics : " + rg 
                    + " DocumentNode : " + documentNode 
                    + " CanvasUpdateListener : " + canvasUpdateListener);
        }

        this.rg = rg;
        this.documentNode = documentNode;
        this.canvasUpdateListener = canvasUpdateListener;
        this.documentNode.setUpdateListener(this);
        this.dirtyAreaManager = new DirtyAreaManager(documentNode);
    
Methods Summary
public voidconsume()
Should be called by the SimpleCanvasManager user to notify the SimpleCanvasManager when an update to the canvas has been consumed.

        synchronized (lock) {
            canvasConsumed = true;
            lock.notifyAll();
        }
    
protected voidfullPaint()
Utility method used to do a full repaint. This method should be called from the update thread only.

        synchronized (lock) {
            if (DirtyAreaManager.ON) {
                dirtyAreaManager.refresh(documentNode, rg, clearPaint);
            } else {
                rg.setRenderingTile(null);
                rg.setFill(clearPaint);
                rg.setTransform(null);
                rg.setFillOpacity(1);
                rg.fillRect(0, 0, documentNode.getWidth(), 
                            documentNode.getHeight(), 0, 0);
                documentNode.paint(rg);
            }
            canvasConsumed = false;
        }

        canvasUpdateListener.updateComplete(this);
    
public com.sun.perseus.j2d.RenderGraphicsgetRenderGraphics()

return
the RenderGraphics to the canvas managed by this SimpleCanvasManager.

        return rg;
    
public booleanisOff()

return
true if the SimpleCanvasManager is currently bypassing canvas updates.

        return isOff;
    
public voidloadBegun(ModelNode node)
Invoked when the input node has started loading

param
node the ModelNode for which loading has started.

    
public voidloadComplete(ModelNode node)
Invoked when the input node has finished loading.

param
node the node for which loading is complete.

        // Do nothing.
    
public voidloadStarting(DocumentNode documentNode, java.io.InputStream is)
Invoked when the document starts loading

param
documentNode the DocumentNode for which loading is starting
param
is the InputStream from which SVG content is loaded.

    
public voidloadingFailed(DocumentNode documentNode, java.lang.Exception error)
Invoked when a document error happened before finishing loading.

param
documentNode the DocumentNode for which loading has failed.
param
error the exception which describes the reason why loading failed.

        // Do nothing.
    
public voidmodifiedNode(ModelNode node)
Invoked when a node modification completed.

param
node the node which was just modified.

        if (!needRepaint 
            &&
            (node.hasNodeRendering() 
             || 
             node.hasDescendants())) {
            needRepaint = true;
        }
    
public voidmodifyingNode(ModelNode node)
Invoked when a node is about to be modified. This will be used in the future to track dirty areas.

param
node the node which is about to be modified

        if ((node.hasNodeRendering() || node.hasDescendants())
            && (node.canRenderState == 0)) {
            needRepaint = true;
        }
    
public voidmodifyingNodeRendering(ModelNode node)
Invoked when a node's rendering is about to be modified

param
node the node which is about to be modified

        // Note that this is redundant with the the check done in 
        // DirtyAreaManager. However, this is needed because DirtyAreaManager
        // is sometimes used stand-alone (e.g. from ScalableGraphics).
        // Having the call in the if statement makes the check redundant only 
        // when evaluating to true.
        if (DirtyAreaManager.ON) {
            dirtyAreaManager.modifyingNodeRendering(node);
        }
    
public voidnodeInserted(ModelNode node)
Invoked when a node has been inserted into the tree

param
node the newly inserted node

        if (DirtyAreaManager.ON) {
            dirtyAreaManager.nodeInserted(node);
        }
        needRepaint = true;
    
public voidrunnableInvoked(com.sun.perseus.util.RunnableQueue rq, java.lang.Runnable r)
Called when the given Runnable has just been invoked and has returned.

param
r the Runnable which just got executed
param
rq the RunnableQueue which executed the input Runnable

        if (!isOff) {
            updateCanvas();
        }
    
public voidsetClearPaint(com.sun.perseus.j2d.RGB clearPaint)
Sets the paint used to clear the canvas.

param
clearPaint the new paint.

        if (clearPaint == null) {
            throw new NullPointerException();
        }

        this.clearPaint = clearPaint;
        needRepaint = true;
    
public voidsetRenderGraphics(com.sun.perseus.j2d.RenderGraphics rg)

param
rg the RenderGraphics which this instance should now update. Setting the RenderGraphics causes a full repaint to be scheduled.
throws
IllegalArgumentException if rg is null.

        if (rg == null) {
            throw new IllegalArgumentException();
        }

        synchronized (lock) {
            this.rg = rg;
            
            // Repaint the documentNode into the new graphics
            needRepaint = true;

            // The new buffer has not been painted and has not been
            // consumed.
            canvasConsumed = false;
        }
    
public voidtextInserted(ModelNode node)
Invoked when a string has been appended, during a load phase. This is only used when parsing a document and is used in support of progressive download, like the other loadXXX methods.

param
node the ModelNode on which text has been inserted.

    
public voidturnOff()
Turns off any rendering updates.

        isOff = true;
    
public voidturnOn()
Turns rendering updates on.

        isOff = false;
    
public voidupdateCanvas()
Utility method used to update the canvas appropriately depending on what is needed. During the loading phase, while we do progressive rendering, the canvas will only redraw nodes in the progressiveNodes list, unless a repaint has been requested. Important Note: this method should only be called from the update thread, i.e., the thread that also manages the model node tree.

        if (needRepaint) {
            if (canvasConsumed) {
                fullPaint();
                needRepaint = false;
            } else {
                // There is a request to update the canvas
                // (likely after a Runnable was invoked),
                // but the last update was not consumed.
                // If there is a Runnable in the RunnableQueue,
                // we just skip this rendering update. Otherwise,
                // schedule a fake Runnable to force a later repaint.
                if (documentNode.getUpdateQueue().getSize() == 0) {
                    documentNode.getUpdateQueue()
                        .preemptLater(new Runnable() {
                                public void run() {
                                }
                            }, this);
                }
            }
        }