1 19 20 package org.netbeans.swing.outline; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Enumeration ; 25 import java.util.List ; 26 import javax.swing.event.TableModelEvent ; 27 import javax.swing.event.TableModelListener ; 28 import javax.swing.event.TreeExpansionEvent ; 29 import javax.swing.event.TreeExpansionListener ; 30 import javax.swing.event.TreeModelEvent ; 31 import javax.swing.event.TreeModelListener ; 32 import javax.swing.event.TreeWillExpandListener ; 33 import javax.swing.table.TableModel ; 34 import javax.swing.tree.AbstractLayoutCache ; 35 import javax.swing.tree.ExpandVetoException ; 36 import javax.swing.tree.FixedHeightLayoutCache ; 37 import javax.swing.tree.TreeModel ; 38 import javax.swing.tree.TreePath ; 39 import javax.swing.tree.VariableHeightLayoutCache ; 40 41 91 public class DefaultOutlineModel implements OutlineModel { 92 private TreeModel treeModel; 93 private TableModel tableModel; 94 private AbstractLayoutCache layout; 95 private TreePathSupport treePathSupport; 96 private EventBroadcaster broadcaster; 97 private static final int NODES_CHANGED = 0; 100 private static final int NODES_INSERTED = 1; 101 private static final int NODES_REMOVED = 2; 102 private static final int STRUCTURE_CHANGED = 3; 103 104 108 109 private String nodeColumnName; 110 111 private static final String [] types = new String [] { 113 "nodesChanged", "nodesInserted", "nodesRemoved", "structureChanged" 114 }; 115 116 122 public static OutlineModel createOutlineModel(TreeModel treeModel, RowModel rowModel) { 123 return createOutlineModel (treeModel, rowModel, false); 124 } 125 126 128 public static OutlineModel createOutlineModel(TreeModel treeModel, RowModel rowModel, boolean isLargeModel) { 129 TableModel tableModel = new ProxyTableModel(rowModel); 130 return new DefaultOutlineModel (treeModel, tableModel, isLargeModel); 131 } 132 133 137 protected DefaultOutlineModel(TreeModel treeModel, TableModel tableModel, boolean largeModel) { 138 this.treeModel = treeModel; 139 this.tableModel = tableModel; 140 141 layout = largeModel ? (AbstractLayoutCache ) new FixedHeightLayoutCache () 142 : (AbstractLayoutCache ) new VariableHeightLayoutCache (); 143 144 broadcaster = new EventBroadcaster (this); 145 146 layout.setRootVisible(true); 147 layout.setModel(this); 148 treePathSupport = new TreePathSupport(this, layout); 149 treePathSupport.addTreeExpansionListener(broadcaster); 150 treePathSupport.addTreeWillExpandListener(broadcaster); 151 treeModel.addTreeModelListener(broadcaster); 152 tableModel.addTableModelListener(broadcaster); 153 if (tableModel instanceof ProxyTableModel) { 154 ((ProxyTableModel) tableModel).setOutlineModel(this); 155 } 156 } 157 158 public final TreePathSupport getTreePathSupport() { 159 return treePathSupport; 160 } 161 162 public final AbstractLayoutCache getLayout() { 163 return layout; 164 } 165 166 public boolean areMoreEventsPending() { 167 return broadcaster.areMoreEventsPending(); 168 } 169 170 171 TreeModel getTreeModel() { 172 return treeModel; 173 } 174 175 176 TableModel getTableModel() { 177 return tableModel; 178 } 179 180 public final Object getChild(Object parent, int index) { 181 return treeModel.getChild (parent, index); 182 } 183 184 public final int getChildCount(Object parent) { 185 return treeModel.getChildCount (parent); 186 } 187 188 190 public final Class getColumnClass(int columnIndex) { 191 if (columnIndex == 0) { 192 return Object .class; 193 } else { 194 return tableModel.getColumnClass(columnIndex-1); 195 } 196 } 197 198 public final int getColumnCount() { 199 return tableModel.getColumnCount()+1; 200 } 201 204 public void setNodeColumnName(String inName) { 205 nodeColumnName = inName; 206 } 207 public String getColumnName(int columnIndex) { 208 212 if (columnIndex == 0) { 213 return null == nodeColumnName ? "Nodes": nodeColumnName; } else { 215 return tableModel.getColumnName(columnIndex-1); 216 } 217 } 218 219 public final int getIndexOfChild(Object parent, Object child) { 220 return treeModel.getIndexOfChild(parent, child); 221 } 222 223 public final Object getRoot() { 224 return treeModel.getRoot(); 225 } 226 227 public final int getRowCount() { 228 return layout.getRowCount(); 229 } 230 231 public final Object getValueAt(int rowIndex, int columnIndex) { 232 Object result; 233 if (columnIndex == 0) { TreePath path = getLayout().getPathForRow(rowIndex); 235 if (path != null) { 236 result = path.getLastPathComponent(); 237 } else { 238 result = null; 239 } 240 } else { 241 result = (tableModel.getValueAt(rowIndex, columnIndex -1)); 242 } 243 return result; 244 } 245 246 public boolean isCellEditable(int rowIndex, int columnIndex) { 247 if (columnIndex == 0) { 248 return false; } else { 250 return tableModel.isCellEditable(rowIndex, columnIndex-1); 251 } 252 } 253 254 public final boolean isLeaf(Object node) { 255 return treeModel.isLeaf(node); 256 } 257 258 259 public final synchronized void addTableModelListener(TableModelListener l) { 260 broadcaster.addTableModelListener (l); 261 } 262 263 264 public final synchronized void addTreeModelListener(TreeModelListener l) { 265 broadcaster.addTreeModelListener (l); 266 } 267 268 269 public final synchronized void removeTableModelListener(TableModelListener l) { 270 broadcaster.removeTableModelListener(l); 271 } 272 273 274 public final synchronized void removeTreeModelListener(TreeModelListener l) { 275 broadcaster.removeTreeModelListener(l); 276 } 277 278 279 public final void setValueAt(Object aValue, int rowIndex, int columnIndex) { 280 if (columnIndex != 0) { 281 tableModel.setValueAt (aValue, rowIndex, columnIndex-1); 282 } else { 283 } 285 } 286 287 public final void valueForPathChanged(javax.swing.tree.TreePath path, Object newValue) { 288 treeModel.valueForPathChanged(path, newValue); 291 } 292 293 public boolean isLargeModel() { 294 return layout instanceof FixedHeightLayoutCache ; 295 } 296 297 public NodeRowModel getRowNodeModel() { 298 return (ProxyTableModel)tableModel; 299 } 300 301 302 } 303 | Popular Tags |