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 DataViewTreeModel implements TreeModel{ 70 private List dataViews; 71 private String root = "root"; 72 73 private Vector treeModelListeners; 74 75 public DataViewTreeModel() { 76 } 77 78 public void setDataViews(List dataViews){ 79 this.dataViews = dataViews; 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 dataViews.get(index); 90 } else if (parent instanceof DataView){ 91 DataView p = (DataView)parent; 92 return p.getObjEntityView(index); 93 } else if (parent instanceof ObjEntityView){ 94 ObjEntityView p = (ObjEntityView)parent; 95 return p.getObjEntityViewField(index); 96 } else 97 return null; 98 } 99 102 public int getChildCount(Object parent) { 103 if (root.equals(parent)) { 104 return dataViews.size(); 105 } else if (parent instanceof DataView){ 106 DataView p = (DataView)parent; 107 return p.getObjEntityViewCount(); 108 } else if (parent instanceof ObjEntityView){ 109 ObjEntityView p = (ObjEntityView)parent; 110 return p.getObjEntityViewFieldCount(); 111 } else if (parent instanceof ObjEntityViewField){ 112 return 0; 113 } else 114 return -1; 115 } 116 117 120 public int getIndexOfChild(Object parent, Object child) { 121 if (root.equals(parent)) { 122 return dataViews.indexOf(child); 123 } else if (parent instanceof DataView){ 124 DataView p = (DataView)parent; 125 return p.getIndexOfObjEntityView((ObjEntityView)child); 126 } else if (parent instanceof ObjEntityView){ 127 ObjEntityView p = (ObjEntityView)parent; 128 return p.getIndexOfObjEntityViewField((ObjEntityViewField)child); 129 } else 130 return -1; 131 } 132 133 136 public Object getRoot() { 137 return root; 138 } 139 140 143 public boolean isLeaf(Object node) { 144 if (root.equals(node)) { 145 return dataViews == null || dataViews.isEmpty(); 146 } else if (node instanceof DataView){ 147 DataView p = (DataView)node; 148 return p.getObjEntityViewCount() == 0; 149 } else if (node instanceof ObjEntityView){ 150 ObjEntityView p = (ObjEntityView)node; 151 return p.getObjEntityViewFieldCount() == 0; 152 } else if (node instanceof ObjEntityViewField){ 153 return true; 154 } else 155 return true; 156 } 157 158 public TreePath dataViewAdded(DataView dataView) { 159 int index = dataViews.indexOf(dataView); 160 if (index < 0) return null; 161 fireTreeNodesInserted(new TreeModelEvent( 162 this, 163 new TreePath(root), 164 new int[] {index}, 165 new Object [] {dataView})); 166 return new TreePath(new Object [] {root, dataView}); 167 } 168 169 public void dataViewRemoved(DataView dataView, int index) { 170 171 fireTreeNodesRemoved(new TreeModelEvent( 172 this, 173 new TreePath(root), 174 new int[] {index}, 175 new Object [] {dataView})); 176 } 177 178 public TreePath dataViewChanged(DataView dataView) { 179 int index = dataViews.indexOf(dataView); 180 if (index < 0) return null; 181 fireTreeNodesChanged(new TreeModelEvent( 182 this, 183 new TreePath(root), 184 new int[] {index}, 185 new Object [] {dataView})); 186 return new TreePath(new Object [] {root, dataView}); 187 } 188 189 public TreePath objEntityViewAdded(ObjEntityView view) { 190 DataView owner = view.getDataView(); 191 int index = owner.getIndexOfObjEntityView(view); 192 fireTreeNodesInserted(new TreeModelEvent( 193 this, 194 new Object [] {root, owner}, 195 new int[] {index}, 196 new Object [] {view})); 197 return new TreePath(new Object [] {root, owner, view}); 198 } 199 200 public void objEntityViewRemoved(ObjEntityView view, int index) { 201 DataView owner = view.getDataView(); 202 fireTreeNodesRemoved(new TreeModelEvent( 203 this, 204 new Object [] {root, owner}, 205 new int[] {index}, 206 new Object [] {view})); 207 } 208 209 public TreePath objEntityViewChanged(ObjEntityView view) { 210 DataView owner = view.getDataView(); 211 int index = owner.getIndexOfObjEntityView(view); 212 fireTreeNodesChanged(new TreeModelEvent( 213 this, 214 new Object [] {root, owner}, 215 new int[] {index}, 216 new Object [] {view})); 217 return new TreePath(new Object [] {root, owner, view}); 218 } 219 220 public TreePath fieldAdded(ObjEntityViewField field) { 221 ObjEntityView owner = field.getObjEntityView(); 222 DataView dataView = owner.getDataView(); 223 int index = owner.getIndexOfObjEntityViewField(field); 224 fireTreeNodesInserted(new TreeModelEvent( 225 this, 226 new Object [] {root, dataView, owner}, 227 new int[] {index}, 228 new Object [] {field})); 229 return new TreePath(new Object [] {root, dataView, owner, field}); 230 } 231 232 public void fieldRemoved(ObjEntityViewField field, int index) { 233 ObjEntityView owner = field.getObjEntityView(); 234 DataView dataView = owner.getDataView(); 235 fireTreeNodesRemoved(new TreeModelEvent( 236 this, 237 new Object [] {root, dataView, owner}, 238 new int[] {index}, 239 new Object [] {field})); 240 } 241 242 public TreePath fieldChanged(ObjEntityViewField field) { 243 ObjEntityView owner = field.getObjEntityView(); 244 DataView dataView = owner.getDataView(); 245 int index = owner.getIndexOfObjEntityViewField(field); 246 fireTreeNodesChanged(new TreeModelEvent( 247 this, 248 new Object [] {root, dataView, owner}, 249 new int[] {index}, 250 new Object [] {field})); 251 return new TreePath(new Object [] {root, dataView, owner, field}); 252 } 253 254 258 public void valueForPathChanged(TreePath path, Object newValue) { 259 System.out.println("*** valueForPathChanged : " 260 + path + " --> " + newValue); 261 } 262 263 public synchronized void removeTreeModelListener(TreeModelListener l) { 264 if (treeModelListeners != null && treeModelListeners.contains(l)) { 265 Vector v = (Vector) treeModelListeners.clone(); 266 v.removeElement(l); 267 treeModelListeners = v; 268 } 269 } 270 public synchronized void addTreeModelListener(TreeModelListener l) { 271 Vector v = treeModelListeners == null ? new Vector(2) : (Vector) treeModelListeners.clone(); 272 if (!v.contains(l)) { 273 v.addElement(l); 274 treeModelListeners = v; 275 } 276 } 277 protected void fireTreeNodesChanged(TreeModelEvent e) { 278 if (treeModelListeners != null) { 279 Vector listeners = treeModelListeners; 280 int count = listeners.size(); 281 for (int i = 0; i < count; i++) { 282 ((TreeModelListener) listeners.elementAt(i)).treeNodesChanged(e); 283 } 284 } 285 } 286 protected void fireTreeNodesInserted(TreeModelEvent e) { 287 if (treeModelListeners != null) { 288 Vector listeners = treeModelListeners; 289 int count = listeners.size(); 290 for (int i = 0; i < count; i++) { 291 ((TreeModelListener) listeners.elementAt(i)).treeNodesInserted(e); 292 } 293 } 294 } 295 protected void fireTreeNodesRemoved(TreeModelEvent e) { 296 if (treeModelListeners != null) { 297 Vector listeners = treeModelListeners; 298 int count = listeners.size(); 299 for (int i = 0; i < count; i++) { 300 ((TreeModelListener) listeners.elementAt(i)).treeNodesRemoved(e); 301 } 302 } 303 } 304 protected void fireTreeStructureChanged(TreeModelEvent e) { 305 if (treeModelListeners != null) { 306 Vector listeners = treeModelListeners; 307 int count = listeners.size(); 308 for (int i = 0; i < count; i++) { 309 ((TreeModelListener) listeners.elementAt(i)).treeStructureChanged(e); 310 } 311 } 312 } 313 } 314 | Popular Tags |