001    package nl.cwi.sen1.visbase.factbrowser.data;
002    
003    import java.util.Iterator;
004    import java.util.Observable;
005    import java.util.Observer;
006    
007    import javax.swing.tree.DefaultMutableTreeNode;
008    
009    import nl.cwi.sen1.relationstores.types.RType;
010    
011    import org.apache.commons.logging.Log;
012    import org.apache.commons.logging.LogFactory;
013    
014    /**
015     * This class is the in memory representation of a Fact coupled to a specific
016     * FactType. This FactType is like <str, str> or similar. The class extends the
017     * DefaultMutableTreeNode so we can directly add new Visualisations which can
018     * represent the Fact.
019     *
020     * @author Renze de Vries
021     * @date 14-02-2007
022     *
023     */
024    public class RStoreFact extends DefaultMutableTreeNode implements Observer {
025        private static final long serialVersionUID = 4010260633083415595L;
026        private static final Log log = LogFactory.getLog(RStoreFact.class);
027        private String factName;
028        private RType rType;
029    
030        private int factId;
031    
032        private RStoreFactType factType;
033    
034        /**
035         * This is the default constructor only present for failsafe situations.
036         * Normally the overloaded constructor: FactNode(String factName, int
037         * factId, RStoreFactType factType) should be used.
038         *
039         * @author Renze de Vries
040         * @date 14-02-2007
041         */
042        public RStoreFact() {
043            factName = "";
044            factId = 0;
045            factType = new RStoreFactType("");
046    
047            //By default all FactNodes have no available visualisations
048            DefaultMutableTreeNode noVisNode = new DefaultMutableTreeNode(
049                    "no visualisations");
050            this.add(noVisNode);
051        }
052    
053        /**
054         * The constructor with all the parameters to create a new Fact
055         *
056         * @param factName
057         *            The Name of the Fact
058         * @param factId
059         *            The unique identifier of the Fact
060         * @param factType
061         *            The Type the fact is composed of
062         *
063         * @author Renze de Vries
064         * @date 14-02-2007
065         */
066        public RStoreFact(String factName, int factId, RStoreFactType factType) {
067            this.factName = factName;
068            this.factId = factId;
069            this.factType = factType;
070    
071            factType.addObserver(this);
072            
073            update(factType, null);
074    
075            //By default all FactNodes have no available visualisations
076            //DefaultMutableTreeNode noVisNode = new DefaultMutableTreeNode(
077            //        "no visualisations");
078            //this.add(noVisNode);
079        }
080    
081        /**
082         * toString method so in the tree it will be visible what the name of the
083         * fact is and which FactType it is.
084         *
085         * @author Renze de Vries
086         * @date 14-02-2007
087         */
088        public String toString() {
089            return factName + "(" + factType.getFactType() + ")";
090        }
091    
092        /**
093         * getFactName method so we can determine the name without the FactType
094         *
095         * @author Renze de Vries
096         * @date 20-02-2007
097         */
098        public String getFactName() {
099            return this.factName;
100        }
101    
102        /**
103         * get the FactType this fact is composed of.
104         *
105         * @return The factType
106         *
107         * @author Renze de Vries
108         * @date 14-02-2007
109         */
110        public RStoreFactType getFactType() {
111            return this.factType;
112        }
113    
114        /**
115         * This method is there to contain the RType.
116         *
117         * @return The RType itself
118         *
119         * @author Renze de Vries
120         * @date 23-02-2007
121         */
122        public RType getRType() {
123            return rType;
124        }
125    
126        /**
127         * This method sets the RType of the FactNode
128         *
129         * @param type This is the actual RType
130         *
131         * @author Renze de Vries
132         * @date 23-02-2007
133         */
134        public void setRType(RType type) {
135            rType = type;
136        }
137    
138        /**
139         * get the FactId so we can identify it's uniqueness
140         *
141         * @return The factId
142         *
143         * @author Renze de Vries
144         * @date 14-02-2007
145         */
146        public int getFactId() {
147            return this.factId;
148        }
149    
150        /**
151         * Whenever a new visualisation plugin is added for the specific FactType
152         * this method is called as an update caller. This method will update the in
153         * memory tree so it will display the current available visualisations
154         *
155         * @author Renze de Vries
156         * @date 14-02-2007
157         */
158        public void update(Observable arg0, Object arg1) {
159            log.debug("New visualisation plugin for this fact: "
160                    + factName);
161    
162            //Cast the update from the model to a RStoreFactType
163            RStoreFactType factType = (RStoreFactType) arg0;
164    
165            //Remove all the children so we can rebuild the tree
166            this.removeAllChildren();
167    
168            Iterator<VisualisationPlugin> iterator = factType.getVisualisationPlugins().iterator();
169    
170            //While there are visualisations continue
171            while (iterator.hasNext()) {
172                VisualisationPlugin visPlugin = iterator.next();
173                // Display the visualisationPlugin in the visible Tree
174                DefaultMutableTreeNode visTreeNode = new DefaultMutableTreeNode(
175                        visPlugin);
176    
177                log.debug("Adding the visualisation to FactNode");
178    
179                //add the visualisatoinPlugin to the Tree
180                this.add(visTreeNode);
181            }
182        }
183    
184            public String getType() {
185                    return getRType().toTerm().toString();
186            }
187    }