View Javadoc
1 package com.bonevich.erj.ui; 2 3 import com.bonevich.erj.model.*; 4 import com.bonevich.erj.ui.editor.*; 5 import com.bonevich.util.dependency.*; 6 7 import org.tigris.gef.ui.PopupGenerator; 8 9 import javax.swing.tree.*; 10 import java.awt.event.MouseEvent; 11 12 import java.util.*; 13 14 /*** 15 * 16 * @author Jeffrey Bonevich <bonevich@telocity.com> 17 * @version $Id: ErjTreeNode.html,v 1.1 2009/03/07 17:55:33 jbonevic Exp $ 18 */ 19 public class ErjTreeNode extends DefaultMutableTreeNode 20 implements PopupGenerator 21 { 22 ////////////////////////////////////////////////////////// 23 // Attributes 24 private DefaultTreeModel _model; 25 26 ////////////////////////////////////////////////////////// 27 // Dependency sentries 28 private Dependent _dep_userObject = new Dependent( 29 new UpdateAction() 30 { 31 public void onUpdate() 32 { 33 //System.err.println("(node) onUpdate called!"); 34 removeAllChildren(); 35 36 // call to getChildren makes us a dependent on the info model object 37 Iterator children = getChildren().iterator(); 38 int index = 0; 39 while (children.hasNext()) 40 { 41 ErjTreeNode newNode = new ErjTreeNode(children.next(), _model); 42 insert(newNode, index++); 43 } 44 int[] indexes = new int[index]; 45 for (int i = 0; i < index; i++) 46 { 47 indexes[i] = i; 48 } 49 if (_model != null) 50 { 51 _model.nodesWereInserted(ErjTreeNode.this, indexes); 52 } 53 } 54 } 55 ); 56 57 /*** Creates new ErjTreeNode */ 58 public ErjTreeNode(Object modelElement) 59 { 60 super(modelElement); 61 } 62 public ErjTreeNode(Object modelElement, DefaultTreeModel model) 63 { 64 this(modelElement); 65 _model = model; 66 } 67 68 public void setModel(DefaultTreeModel model) 69 { 70 _model = model; 71 } 72 73 public boolean isSchema() 74 { 75 return getUserObject() instanceof Schema; 76 } 77 78 public boolean isRelation() 79 { 80 return getUserObject() instanceof Relation; 81 } 82 83 public boolean isDomain() 84 { 85 return getUserObject() instanceof Domain; 86 } 87 88 public boolean isAttribute() 89 { 90 return getUserObject() instanceof Attribute; 91 } 92 93 public boolean isForeignKey() 94 { 95 return getUserObject() instanceof ForeignKey; 96 } 97 98 public boolean getAllowsChildren() 99 { 100 boolean rez = isRoot() || isSchema() || isRelation(); 101 return rez; 102 } 103 104 public boolean isLeaf() 105 { 106 return !getAllowsChildren(); 107 } 108 109 public void checkDependents() 110 { 111 // calling checkDependents will cause this node to recursively 112 // update its dependents (update itself and then ask children to 113 // do the same) 114 _dep_userObject.onGet(); 115 if (!isLeaf()) 116 { 117 Enumeration chn = children(); 118 while (chn.hasMoreElements()) 119 { 120 ErjTreeNode child = (ErjTreeNode) chn.nextElement(); 121 child.checkDependents(); 122 } 123 } 124 } 125 126 public void add(MutableTreeNode newChild) 127 { 128 super.add(newChild); 129 System.err.println("Added a new child = " + ((DefaultMutableTreeNode)newChild).getUserObject().getClass().getName() + " to this node = " + getUserObject().getClass().getName()); 130 } 131 132 //FIXME: Cache result and add Dependent - dump cache onUpdate 133 private List getChildren() 134 { 135 Object parent = getUserObject(); 136 List chiblings = new ArrayList(); 137 if (parent instanceof Schema) 138 { 139 Schema s = (Schema)parent; 140 // the following calls for dynamic attributes of the schema 141 // will register this dependent, establishing the two way link 142 Iterator relItr = s.getRelationIterator(); 143 Iterator fkItr = s.getKeyIterator(); 144 Iterator domItr = s.getDomainIterator(); 145 while(relItr.hasNext()) 146 { 147 chiblings.add(relItr.next()); 148 } 149 while(fkItr.hasNext()) 150 { 151 chiblings.add(fkItr.next()); 152 } 153 while(domItr.hasNext()) 154 { 155 chiblings.add(domItr.next()); 156 } 157 } 158 else if (parent instanceof Relation) 159 { 160 Iterator itr = ((Relation) parent).getAttributeIterator(); 161 while(itr.hasNext()) 162 { 163 chiblings.add(itr.next()); 164 } 165 } 166 //System.err.println("chn = " + chiblings); 167 return chiblings; 168 } 169 170 ////////////////////////////////////////////////////////// 171 // PopupGenerator implementation 172 public Vector getPopUpActions(MouseEvent e) 173 { 174 Vector actions = new Vector(); 175 if (isSchema()) 176 { 177 Schema s = (Schema)getUserObject(); 178 actions.addElement(new CmdEditSchema(s, SchemaEditor.MODE_EDIT_SCHEMA)); 179 actions.addElement(new CmdCreateDomain(s)); 180 } 181 else if (isRelation()) 182 { 183 Relation r = (Relation)getUserObject(); 184 actions.addElement(new CmdEditRelation(r, RelationEditor.MODE_EDIT_RELATION)); 185 actions.addElement(new CmdDeleteRelation(r)); 186 } 187 else if (isForeignKey()) 188 { 189 ForeignKey fk = (ForeignKey)getUserObject(); 190 actions.addElement(new CmdEditForeignKey(fk)); 191 actions.addElement(new CmdDeleteForeignKey(fk)); 192 } 193 else if (isDomain()) 194 { 195 Domain d = (Domain)getUserObject(); 196 actions.addElement(new CmdEditDomain(d)); 197 actions.addElement(new CmdDeleteDomain(d)); 198 } 199 else if (isAttribute()) 200 { 201 Attribute a = (Attribute) getUserObject(); 202 actions.addElement(new CmdEditAttribute(a)); 203 Iterator itr = a.getKeyIterator(); 204 while (itr.hasNext()) 205 { 206 KeyConstraint key = (KeyConstraint) itr.next(); 207 if (!(key instanceof ForeignKey)) 208 { 209 actions.addElement(new CmdEditKey(key, "Edit Key " + key.getIdentifier())); 210 } 211 } 212 actions.addElement(new CmdDeleteAttribute(a)); 213 } 214 215 return actions; 216 } 217 218 } /*** end class ErjTreeNode */

This page was automatically generated by Maven