1 56 57 package org.objectstyle.cayenne.dataview.dvmodeler; 58 59 import javax.swing.tree.*; 60 import javax.swing.event.*; 61 import java.util.*; 62 63 68 69 public class DataMapTreeModel implements TreeModel{ 70 private List dataMaps; 71 private String root = "root"; 72 73 private Vector treeModelListeners; 74 75 public DataMapTreeModel() { 76 } 77 78 public void setDataMaps(List dataMaps){ 79 this.dataMaps = dataMaps; 80 fireTreeStructureChanged(new TreeModelEvent(this, new TreePath(root))); 81 } 82 83 84 87 public Object getChild(Object parent, int index) { 88 if (root.equals(parent)) { 89 return dataMaps.get(index); 90 } else if (parent instanceof DataMap){ 91 DataMap p = (DataMap)parent; 92 return p.getObjEntity(index); 93 } else if (parent instanceof ObjEntity){ 94 ObjEntity p = (ObjEntity)parent; 95 return p.getObjEntityView(index); 96 } else if (parent instanceof ObjEntityView){ 97 ObjEntityView p = (ObjEntityView)parent; 98 return p.getObjEntityViewField(index); 99 } else 100 return null; 101 } 102 105 public int getChildCount(Object parent) { 106 if (root.equals(parent)) { 107 return dataMaps.size(); 108 } else if (parent instanceof DataMap){ 109 DataMap p = (DataMap)parent; 110 return p.getObjEntityCount(); 111 } else if (parent instanceof ObjEntity){ 112 ObjEntity p = (ObjEntity)parent; 113 return p.getObjEntityViewCount(); 114 } else if (parent instanceof ObjEntityView){ 115 ObjEntityView p = (ObjEntityView)parent; 116 return p.getObjEntityViewFieldCount(); 117 } else 118 return 0; 119 } 120 121 124 public int getIndexOfChild(Object parent, Object child) { 125 if (root.equals(parent)) { 126 return dataMaps.indexOf(child); 127 } else if (parent instanceof DataMap){ 128 DataMap p = (DataMap)parent; 129 return p.getIndexOfObjEntity((ObjEntity)child); 130 } else if (parent instanceof ObjEntity){ 131 ObjEntity p = (ObjEntity)parent; 132 return p.getIndexOfObjEntityView((ObjEntityView)child); 133 } else if (parent instanceof ObjEntityView){ 134 ObjEntityView p = (ObjEntityView)parent; 135 return p.getIndexOfObjEntityViewField((ObjEntityViewField)child); 136 } else 137 return 0; 138 } 139 140 143 public Object getRoot() { 144 return root; 145 } 146 147 150 public boolean isLeaf(Object node) { 151 if (root.equals(node)) { 152 return dataMaps == null || dataMaps.isEmpty(); 153 } else if (node instanceof DataMap){ 154 DataMap p = (DataMap)node; 155 return p.getObjEntityCount() == 0; 156 } else if (node instanceof ObjEntity){ 157 ObjEntity p = (ObjEntity)node; 158 return p.getObjEntityViewCount() == 0; 159 } else if (node instanceof ObjEntityView){ 160 ObjEntityView p = (ObjEntityView)node; 161 return p.getObjEntityViewFieldCount() == 0; 162 } else if (node instanceof ObjEntityViewField){ 163 return true; 164 } else 165 return true; 166 } 167 168 169 170 174 public void valueForPathChanged(TreePath path, Object newValue) { 175 System.out.println("*** valueForPathChanged : " 176 + path + " --> " + newValue); 177 } 178 179 public synchronized void removeTreeModelListener(TreeModelListener l) { 180 if (treeModelListeners != null && treeModelListeners.contains(l)) { 181 Vector v = (Vector) treeModelListeners.clone(); 182 v.removeElement(l); 183 treeModelListeners = v; 184 } 185 } 186 public synchronized void addTreeModelListener(TreeModelListener l) { 187 Vector v = treeModelListeners == null ? new Vector(2) : (Vector) treeModelListeners.clone(); 188 if (!v.contains(l)) { 189 v.addElement(l); 190 treeModelListeners = v; 191 } 192 } 193 protected void fireTreeNodesChanged(TreeModelEvent e) { 194 if (treeModelListeners != null) { 195 Vector listeners = treeModelListeners; 196 int count = listeners.size(); 197 for (int i = 0; i < count; i++) { 198 ((TreeModelListener) listeners.elementAt(i)).treeNodesChanged(e); 199 } 200 } 201 } 202 protected void fireTreeNodesInserted(TreeModelEvent e) { 203 if (treeModelListeners != null) { 204 Vector listeners = treeModelListeners; 205 int count = listeners.size(); 206 for (int i = 0; i < count; i++) { 207 ((TreeModelListener) listeners.elementAt(i)).treeNodesInserted(e); 208 } 209 } 210 } 211 protected void fireTreeNodesRemoved(TreeModelEvent e) { 212 if (treeModelListeners != null) { 213 Vector listeners = treeModelListeners; 214 int count = listeners.size(); 215 for (int i = 0; i < count; i++) { 216 ((TreeModelListener) listeners.elementAt(i)).treeNodesRemoved(e); 217 } 218 } 219 } 220 protected void fireTreeStructureChanged(TreeModelEvent e) { 221 if (treeModelListeners != null) { 222 Vector listeners = treeModelListeners; 223 int count = listeners.size(); 224 for (int i = 0; i < count; i++) { 225 ((TreeModelListener) listeners.elementAt(i)).treeStructureChanged(e); 226 } 227 } 228 } 229 230 public void replaceObjEntityView(ObjEntity oldObjEntity, 231 int oldIndex, 232 ObjEntity newObjEntity, 233 ObjEntityView objEntityView) { 234 235 DataMap oldDataMap = oldObjEntity.getDataMap(); 236 fireTreeNodesRemoved(new TreeModelEvent( 237 this, 238 new Object [] {root, oldDataMap, oldObjEntity}, 239 new int[] {oldIndex}, 240 new Object [] {objEntityView})); 241 242 DataMap newDataMap = newObjEntity.getDataMap(); 243 244 int newIndex = newObjEntity.getIndexOfObjEntityView(objEntityView); 245 fireTreeNodesInserted(new TreeModelEvent( 246 this, 247 new Object [] {root, newDataMap, newObjEntity}, 248 new int[] {newIndex}, 249 new Object [] {objEntityView})); 250 251 } 252 253 public TreePath objEntityViewAdded(ObjEntityView view) { 254 ObjEntity entity = view.getObjEntity(); 255 if (entity == null) 256 return null; 257 DataMap dataMap = entity.getDataMap(); 258 int index = entity.getIndexOfObjEntityView(view); 259 fireTreeNodesInserted(new TreeModelEvent( 260 this, 261 new Object [] {root, dataMap, entity}, 262 new int[] {index}, 263 new Object [] {view})); 264 return new TreePath(new Object [] {root, dataMap, entity, view}); 265 } 266 267 public void objEntityViewRemoved(ObjEntity entity, ObjEntityView view, int index) { 268 DataMap dataMap = entity.getDataMap(); 269 fireTreeNodesRemoved(new TreeModelEvent( 270 this, 271 new Object [] {root, dataMap, entity}, 272 new int[] {index}, 273 new Object [] {view})); 274 } 275 276 public void objEntityViewsRemoved(Map removingViews) { 277 HashMap views = (HashMap)removingViews; 278 for(Iterator j = views.keySet().iterator(); j.hasNext();){ 279 ObjEntityView view = (ObjEntityView)j.next(); 280 ObjEntity entity = view.getObjEntity(); 281 Integer index = (Integer )views.get(view); 282 DataMap dataMap = entity.getDataMap(); 283 284 fireTreeNodesRemoved(new TreeModelEvent( 285 this, 286 new Object [] {root, dataMap, entity}, 287 new int[] {index.intValue()}, 288 new Object [] {view})); 289 } 290 } 291 292 293 public TreePath objEntityViewChanged(ObjEntityView view) { 294 ObjEntity entity = view.getObjEntity(); 295 if (entity == null) 296 return null; 297 DataMap dataMap = entity.getDataMap(); 298 int index = entity.getIndexOfObjEntityView(view); 299 fireTreeNodesChanged(new TreeModelEvent( 300 this, 301 new Object [] {root, dataMap, entity}, 302 new int[] {index}, 303 new Object [] {view})); 304 return new TreePath(new Object [] {root, dataMap, entity, view}); 305 } 306 307 public TreePath fieldAdded(ObjEntityViewField field) { 308 ObjEntityView owner = field.getObjEntityView(); 309 ObjEntity entity = owner.getObjEntity(); 310 if (entity == null) 311 return null; 312 DataMap dataMap = entity.getDataMap(); 313 int index = owner.getIndexOfObjEntityViewField(field); 314 fireTreeNodesInserted(new TreeModelEvent( 315 this, 316 new Object [] {root, dataMap, entity, owner}, 317 new int[] {index}, 318 new Object [] {field})); 319 return new TreePath(new Object [] {root, dataMap, entity, owner, field}); 320 } 321 322 public void fieldRemoved(ObjEntityViewField field, int index) { 323 ObjEntityView owner = field.getObjEntityView(); 324 ObjEntity entity = owner.getObjEntity(); 325 326 DataMap dataMap = entity.getDataMap(); 327 fireTreeNodesRemoved(new TreeModelEvent( 328 this, 329 new Object [] {root, dataMap, entity, owner}, 330 new int[] {index}, 331 new Object [] {field})); 332 333 } 334 335 public TreePath fieldChanged(ObjEntityViewField field) { 336 ObjEntityView owner = field.getObjEntityView(); 337 ObjEntity entity = owner.getObjEntity(); 338 if (entity == null) 339 return null; 340 DataMap dataMap = entity.getDataMap(); 341 int index = owner.getIndexOfObjEntityViewField(field); 342 fireTreeNodesChanged(new TreeModelEvent( 343 this, 344 new Object [] {root, dataMap, entity, owner}, 345 new int[] {index}, 346 new Object [] {field})); 347 return new TreePath(new Object [] {root, dataMap, entity, owner, field}); 348 } 349 } 350 | Popular Tags |