001: /*
002: * XGroupHierarchy.java
003: *
004: * Created on 01 February 2007, 10:54
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package com.xoetrope.svg;
011:
012: import com.kitfox.svg.Group;
013: import com.kitfox.svg.SVGElement;
014: import com.kitfox.svg.SVGElementException;
015: import com.kitfox.svg.SVGRoot;
016: import com.kitfox.svg.animation.AnimationElement;
017: import com.xoetrope.carousel.build.BuildProperties;
018: import com.xoetrope.svg.XSvgImageMap;
019: import java.util.ArrayList;
020: import java.util.Vector;
021: import javax.swing.tree.DefaultMutableTreeNode;
022:
023: /**
024: *
025: *
026: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
027: * the GNU Public License (GPL), please see license.txt for more details. If
028: * you make commercial use of this software you must purchase a commercial
029: * license from Xoetrope.</p>
030: * <p> $Revision: 1.2 $</p>
031: */
032: public class XGroupHierarchy {
033: protected XSvgImageMap imageMap;
034: protected DefaultMutableTreeNode rootNode;
035: protected SVGRoot root;
036: private int groupId = 1;
037:
038: /**
039: * Creates a new instance of GroupHierarchy
040: * This class can be used to extract the group hierarchy from an svg image.
041: */
042: public XGroupHierarchy(XSvgImageMap imageMap) {
043: this .imageMap = imageMap;
044: root = imageMap.getSvgDiagram().getRoot();
045: rootNode = new DefaultMutableTreeNode(root.getId());
046: extractGroups();
047: }
048:
049: /**
050: * Private method used by this class to recursively extract the group hierarchy from the svg.
051: */
052: public void extractGroups() {
053: Vector vector = new Vector();
054: root.getChildren(vector);
055:
056: for (int i = 0; i < vector.size(); i++) {
057: SVGElement e = (SVGElement) vector.get(i);
058: if (e instanceof Group) {
059: groupInfo((Group) e, rootNode);
060: }
061: }
062: }
063:
064: /**
065: * Method called recursively to create the tree.
066: * @param group the svg group that this class will contain.
067: * @param parent the parent <CODE>DefaultMutableTreeNode</CODE> of this classes node.
068: */
069: private void groupInfo(Group group, DefaultMutableTreeNode parent) {
070: DefaultMutableTreeNode current = null;
071: try {
072: if (group.hasAttribute("id", AnimationElement.AT_XML)) {
073: current = new DefaultMutableTreeNode(group
074: .getPresAbsolute("id").getStringValue());
075: } else {
076: current = new DefaultMutableTreeNode("Group " + groupId);
077: groupId += 1;
078: }
079: } catch (Exception ex) {
080: if (BuildProperties.DEBUG)
081: ex.printStackTrace();
082: }
083:
084: parent.add(current);
085:
086: Vector vector = new Vector();
087: group.getChildren(vector);
088: for (int i = 0; i < vector.size(); i++) {
089: SVGElement e = (SVGElement) vector.get(i);
090: if (e instanceof Group) {
091: groupInfo((Group) e, current);
092: }
093: }
094: }
095:
096: /**
097: * Returns the extracted group hierarchy to the user.
098: * @return the root <CODE>DefaultMutableTreeNode</CODE> of the hierarchy.
099: */
100: public DefaultMutableTreeNode getGroupHierarchy() {
101: return rootNode;
102: }
103: }
|