1 package com.sri.emo.dbobj.model_tree; 2 3 import com.jcorporate.expresso.core.db.DBException; 4 import com.sri.emo.dbobj.IViewable; 5 import org.apache.log4j.Logger; 6 7 import java.io.Serializable; 8 9 /*** 10 * Default Model node used for nesting ModelVisitable data. 11 * 12 * @author Michael Rimov 13 */ 14 public class DefaultModelNode extends AbstractModelNode { 15 16 /*** 17 * The actual node data. Technically the DefaultModelNode is just a placeholder 18 * and the nodeData is the real concern -- the Model Tree only conforms 19 * the data to a tree-like structure. 20 */ 21 private final ModelVisitable nodeData; 22 23 /*** 24 * Event sink. 25 */ 26 private static final Logger log = Logger.getLogger(DefaultModelNode.class); 27 28 /*** 29 * Fill status variable. 30 */ 31 private ModelFillStatus fillStatus = ModelFillStatus.COMPLETE; 32 33 /*** 34 * Tag information for this node. 35 */ 36 private Serializable tagInfo; 37 38 /*** 39 * Completion Status (truncated|recursive|complete|undefined). 40 */ 41 private NodeCompletionStatus completionStatus = NodeCompletionStatus.UNDEFINED_STATUS; 42 43 /*** 44 * Constructor used for a root node. 45 * 46 * @param data The data to be attached. 47 */ 48 public DefaultModelNode(final ModelVisitable data) { 49 super(); 50 nodeData = data; 51 extractIViewableData(data); 52 } 53 54 /*** 55 * Extracts the iviewable data to make the link to view the data. 56 * 57 * @param data ModelVisitable the current data to extract the data from. 58 * @throws IllegalStateException if we cannot extract the data 59 * most likely due to database exception. 60 */ 61 private void extractIViewableData(final ModelVisitable data) throws IllegalStateException { 62 if (data instanceof IViewable) { 63 IViewable castedData = (IViewable) data; 64 try { 65 this.setLink(castedData.getViewTrans()); 66 this.setLabel(getLink().getLabel()); 67 } catch (DBException ex) { 68 log.error("Error getting viewable link from data: " + data.toString(), ex); 69 throw new IllegalStateException( 70 "Error getting viewable link from data: " + data.toString() + " " + ex.getMessage()); 71 } 72 } 73 } 74 75 /*** 76 * Constructor to indicate a nested node. 77 * 78 * @param nodeParent ModelCompositeNode nodeParent.addChild() is automatically 79 * called by the base class implementation. 80 * @param data the data to be attached to the node. 81 */ 82 public DefaultModelNode(final ModelVisitable data, final MutableModelNode nodeParent) { 83 super(nodeParent); 84 nodeData = data; 85 extractIViewableData(data); 86 } 87 88 /*** 89 * Retrieve the viewable interface of what we are dealing with. 90 * 91 * @return IViewable 92 */ 93 public ModelVisitable getVisitable() { 94 return nodeData; 95 } 96 97 /*** 98 * {@inheritDoc} 99 * 100 * @return ModelFillStatus 101 */ 102 public ModelFillStatus getModelFillStatus() { 103 return fillStatus; 104 } 105 106 /*** 107 * Set the model status in terms of 'filled parts'. A set on a particular 108 * node propagates calls up the tree. 109 * 110 * @param newStatus ModelStatus 111 */ 112 public void setModelFilledStatus(ModelFillStatus newStatus) { 113 fillStatus = newStatus; 114 if (this.getParent() != ModelNode.NO_PARENT) { 115 DefaultModelNode parent = (DefaultModelNode) this.getParent(); 116 ModelFillStatus oldStatus = parent.getModelFillStatus(); 117 ModelFillStatus newParentStatus = oldStatus.intersect(fillStatus); 118 parent.setModelFilledStatus(newParentStatus); 119 } 120 } 121 122 /*** 123 * {@inheritDoc} 124 * 125 * @return Serializable 126 */ 127 public synchronized Serializable getAdditionalInfo() { 128 return tagInfo; 129 } 130 131 /*** 132 * {@inheritDoc} 133 * 134 * @param newInfo the new application-specific information. 135 */ 136 public synchronized void setAdditionalInfo(Serializable newInfo) { 137 tagInfo = newInfo; 138 } 139 140 /*** 141 * {@inheritDoc} 142 * 143 * @return NodeCompletionStatus 144 */ 145 public NodeCompletionStatus getNodeCompletionStatus() { 146 return completionStatus; 147 } 148 149 /*** 150 * Sets the node completion status. They can either be Undefined, Complete, 151 * or Recursive -- pointing to another model node in the tree. 152 * 153 * @param newStatus NodeCompletionStatus 154 * @see NodeCompletionStatus 155 */ 156 public void setNodeCompletionStatus(NodeCompletionStatus newStatus) { 157 completionStatus = newStatus; 158 } 159 160 }