Package com.bbn.openmap.tools.icon

Source Code of com.bbn.openmap.tools.icon.IconPartList

// **********************************************************************
//
// <copyright>
//
//  BBN Technologies
//  10 Moulton Street
//  Cambridge, MA 02138
//  (617) 873-8000
//
//  Copyright (C) BBNT Solutions LLC. All rights reserved.
//
// </copyright>
// **********************************************************************
//
// $Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/tools/icon/IconPartList.java,v $
// $RCSfile: IconPartList.java,v $
// $Revision: 1.3.2.1 $
// $Date: 2004/10/14 18:27:43 $
// $Author: dietrick $
//
// **********************************************************************

package com.bbn.openmap.tools.icon;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.GeneralPath;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import com.bbn.openmap.omGraphics.DrawingAttributes;

/**
* An IconPartList is a group of IconParts that can be rendered
* together. If you ask an IconPartList for it's geometry, it will
* combine all its parts into one geometry, and use its
* DrawingAttributes to render that combined shape. The IconPartList
* is itself an IconPart, so the recursive possibilities are endless.
*/
public class IconPartList implements IconPart {

    protected List parts;
    protected DrawingAttributes renderingAttributes = null;
    protected Shape clip = null;

    public IconPartList() {}

    protected List getList() {
        if (parts == null) {
            parts = new LinkedList();
        }
        return parts;
    }

    public Iterator iterator() {
        return parts.iterator();
    }

    /**
     * First in drawn on bottom. Last in on top.
     */
    public void add(IconPart part) {
        getList().add(part);
    }

    public boolean remove(IconPart part) {
        return getList().remove(part);
    }

    public void clear() {
        getList().clear();
    }

    public void render(Graphics g, int width, int height) {
        render(g, width, height, null);
    }

    /**
     * @param appDA drawing attributes to use under certain
     *        conditions. Certain IconParts on this list may use these
     *        drawing attributes if they want/should. May be null.
     */
    public void render(Graphics g, int width, int height,
                       DrawingAttributes appDA) {

        // Handle clip area in Graphics, first
        Shape clip = getClip();
        if (clip != null) {
            g.setClip(clip);
        }

        DrawingAttributes da = getRenderingAttributes();
        DrawingAttributes tmpDA = null;

        Iterator it = iterator();
        while (it.hasNext()) {
            IconPart part = (IconPart) it.next();

            if (da != null) {
                tmpDA = part.getRenderingAttributes();
                part.setRenderingAttributes(da);
            }

            Graphics2D g2 = (Graphics2D) g.create();
            part.render(g2, width, height);
            g2.dispose();

            if (da != null) {
                part.setRenderingAttributes(tmpDA);
                tmpDA = null;
            }
        }
    }

    public void setClip(Shape clipArea) {
        clip = clipArea;
    }

    public Shape getClip() {
        return clip;
    }

    public void setGeometry(Shape shape) {
        // dump the list, create a generic IconPart to hold the shape.
        List list = getList();
        list.clear();
        list.add(new BasicIconPart(shape));
    }

    /**
     * If you ask a IconPartList for its geometry, it will combine all
     * its parts to make one Shape object. All the rendering
     * attributes from the individual parts will be ignored. The
     * contributions will be kept geometrically separate
     * (disconnected) and their clipping areas will be ignored.
     */
    public Shape getGeometry() {
        GeneralPath geometry = null;
        Iterator it = iterator();
        while (it.hasNext()) {
            IconPart part = (IconPart) it.next();

            Shape shp = part.getGeometry();

            if (shp == null) {
                continue;
            }

            if (geometry == null) {
                geometry = new GeneralPath(shp);
            } else {
                geometry.append(shp, false);
            }
        }
        return geometry;
    }

    public void setRenderingAttributes(DrawingAttributes da) {
        renderingAttributes = da;
    }

    public DrawingAttributes getRenderingAttributes() {
        return renderingAttributes;
    }

    public Object clone() {
        IconPartList clone = new IconPartList();
        Iterator it = iterator();
        while (it.hasNext()) {
            IconPart ip = (IconPart) it.next();
            clone.add((IconPart) ip.clone());
        }

        clone.setRenderingAttributes(getRenderingAttributes());
        clone.setClip(getClip());
        return clone;
    }

}
TOP

Related Classes of com.bbn.openmap.tools.icon.IconPartList

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.