001    /* EVolve - an Extensible Software Visualization Framework
002     * Copyright (C) 2001-2002 Qin Wang
003     *
004     * This library is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU Library General Public
006     * License as published by the Free Software Foundation; either
007     * version 2 of the License, or (at your option) any later version.
008     *
009     * This library is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012     * Library General Public License for more details.
013     *
014     * You should have received a copy of the GNU Library General Public
015     * License along with this library; if not, write to the
016     * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017     * Boston, MA 02111-1307, USA.
018     */
019    
020    /*
021     * EVolve is distributed at http://www.sable.mcgill.ca/EVolve/
022     */
023    
024    package EVolve.data;
025    
026    import java.util.HashMap;
027    import java.util.HashSet;
028    import java.util.Iterator;
029    
030    import EVolve.Scene;
031    import EVolve.util.HelperFuncs;
032    
033    public class ReferenceLink implements Cloneable{
034        private String name; // name of the link
035        private int sourceType; // type of the source element
036        private int sourceIndex; // field index of the source element
037        private int targetType; // type of the target entity
038        private HashMap target; // id of the targets
039        private String description; // description of the link
040        private HashSet properties;
041    
042        /**
043         * Creates a reference link.
044         *
045         * @param  name  name of the link
046         * @param  sourceType  type of the source element
047         * @param  sourceIndex  field index of the source element
048         * @param  targetType  type of the target entity
049         * @param  description  description of the link
050         * @param  properties properties of the link
051         */
052        public ReferenceLink(String name, int sourceType, int sourceIndex, int targetType, String description, String[] properties) {
053            this.name = name;
054            this.sourceType = sourceType;
055            this.sourceIndex = sourceIndex;
056            this.targetType = targetType;
057            this.description = description;
058            this.properties = new HashSet();
059            for (int i=0; i<properties.length; i++)
060                this.properties.add(properties[i]);
061            if (properties.length == 0)
062                this.properties.add("reference");
063    
064            target = Scene.getDataManager().getEntity()[targetType];
065            /*target = new int[Scene.getDataManager().getEntity()[targetType].length];
066            for (int i = 0; i < target.length; i++) {
067                target[i] = i;
068            }*/
069        }
070    
071        /**
072         * Creates a reference link by combining two reference links.
073         *
074         * @param  from  where the link starts
075         * @param  to  where the link ends
076         */
077        public ReferenceLink(ReferenceLink from, ReferenceLink to) {
078            this.name = to.name;
079            this.sourceType = from.sourceType;
080            this.sourceIndex = from.sourceIndex;
081            this.targetType = to.targetType;
082            this.description = from.description + "  >>>  " + to.description;
083    
084            /*target = new int[from.target.length];
085            for (int i = 0; i < target.length; i++) {
086                target[i] = to.target[Scene.getDataManager().getEntity()[from.targetType][from.target[i]].getField()[to.sourceIndex]];
087            }*/
088            target = new HashMap();
089            Iterator it = from.target.keySet().iterator();
090            while (it.hasNext()) {
091                Object key = it.next();
092                Object fromKey = new Long(((Entity)from.target.get(key)).getField()[to.sourceIndex]);
093                Entity entity = (Entity)Scene.getDataManager().getEntity()[from.targetType].get(fromKey);
094                target.put(key,entity);
095            }
096            this.properties = HelperFuncs.cloneHashSet(from.properties);
097        }
098    
099        /**
100         * Gets the name of the link.
101         *
102         * @return  name of the link
103         */
104        public String getName() {
105            return name;
106        }
107    
108        /**
109         * Gets the type of the source element.
110         *
111         * @return  type of the source element
112         */
113        public int getSourceType() {
114            return sourceType;
115        }
116    
117        /**
118         * Gets the field index of the source element.
119         *
120         * @return  field index of the source element
121         */
122        public int getSourceIndex() {
123            return sourceIndex;
124        }
125    
126        /**
127         * Gets the type of the target entity.
128         *
129         * @return  type of the target entity
130         */
131        public int getTargetType() {
132            return targetType;
133        }
134    
135        /**
136         * Gets the target.
137         *
138         * @param  source reference
139         * @return  target
140         */
141        public long getTarget(long source) {
142            return ((Entity)target.get(new Long(source))).getId();
143        }
144    
145        /**
146         * Gets the description of the link.
147         *
148         * @return  description of the link
149         */
150        public String getDescription() {
151            return description;
152        }
153    
154        public boolean hasProperty(String property) {
155            return properties.contains(property);
156        }
157    
158        public Object clone() {
159            ReferenceLink o = null;
160            try {
161                o = (ReferenceLink)super.clone();
162            } catch (CloneNotSupportedException e) {
163                e.printStackTrace();
164                return null;
165            }
166            o.name = name;
167            o.target = HelperFuncs.cloneHashMap(target) ;
168            return o;
169        }
170    }