1 6 7 package org.netbeans.modules.piagetproject.gridsplit; 8 9 import java.awt.Component ; 10 import java.awt.Dimension ; 11 import java.util.ArrayList ; 12 import java.util.Collection ; 13 14 19 public class GridSplitModel { 20 21 private GridSplitCell root; 22 private ArrayList listeners = new ArrayList (); 23 24 25 public GridSplitModel( Component rootComp, Dimension preferredSize, double resizeWeight ) { 26 if( null != root ) { 27 throw new IllegalArgumentException ( "The hierarchy root is already set." ); 28 } 29 30 root = new GridSplitCell( rootComp, resizeWeight, preferredSize ); 31 } 32 33 36 public GridSplitCell getRootCell() { 37 return root; 38 } 39 40 43 public void addToRoot( Component compToAdd, int side ) { 44 addToRoot( compToAdd, side, 0.5 ); 45 } 46 47 51 public void addToRoot( Component compToAdd, int side, double initialSize ) { 52 addToRoot( compToAdd, side, initialSize, 0.0 ); 53 } 54 55 60 public void addToRoot( Component compToAdd, int side, double initialSize, double resizeWeight ) { 61 GridSplitCell newCell = root.addToSide( compToAdd, side, initialSize, resizeWeight ); 62 63 fireCellAdded( newCell ); 64 } 65 66 69 public void addToSide( Component neighbor, Component compToAdd, int side ) { 70 addToSide( neighbor, compToAdd, side, 0.5 ); 71 } 72 73 77 public void addToSide( Component neighbor, Component compToAdd, int side, double initialSize ) { 78 addToSide( neighbor, compToAdd, side, initialSize, 0.0 ); 79 } 80 81 86 public void addToSide( Component neighbor, Component compToAdd, int side, double initialSize, double resizeWeight ) { 87 GridSplitCell cell = findCellFor( neighbor ); 88 if( null == cell ) { 89 throw new IllegalArgumentException ( "Component not in hierarchy: " + neighbor ); 90 } 91 92 GridSplitCell newCell = cell.addToSide( compToAdd, side, initialSize, resizeWeight ); 93 94 fireCellAdded( newCell ); 95 } 96 97 101 public void remove( Component c ) { 102 GridSplitCell cell = findCellFor( c ); 103 if( null == cell ) { 104 throw new IllegalArgumentException ( "Component not in hierarchy: " + c ); 105 } 106 107 if( cell == root ) { 108 throw new IllegalArgumentException ( "Cannot remove the root cell" ); 110 } 111 112 cell.getParent().remove( cell ); 113 114 fireCellRemoved( cell ); 115 } 116 117 122 public void setHidden( Component c, boolean hidden ) { 123 GridSplitCell cell = findCellFor( c ); 124 if( null == cell ) { 125 throw new IllegalArgumentException ( "Component not in hierarchy: " + c ); 126 } 127 128 if( cell.isHidden() == hidden ) 129 return; 130 131 GridSplitCell oldValue = new GridSplitCell( cell ); 132 133 cell.setHidden( hidden ); 134 135 fireCellModified( oldValue, cell ); 136 } 137 138 141 public boolean isHidden( Component c ) { 142 GridSplitCell cell = findCellFor( c ); 143 if( null == cell ) { 144 throw new IllegalArgumentException ( "Component not in hierarchy: " + c ); 145 } 146 147 return cell.isHidden(); 148 } 149 150 public void addChangeListener( GridSplitModelListener listener ) { 151 synchronized( listeners ) { 152 listeners.add( listener ); 153 } 154 } 155 156 public void removeChangeListener( GridSplitModelListener listener ) { 157 synchronized( listeners ) { 158 listeners.remove( listener ); 159 } 160 } 161 162 void fireCellAdded( GridSplitCell cell ) { 163 GridSplitModelListener[] arr; 164 synchronized( listeners ) { 165 arr = new GridSplitModelListener[listeners.size()]; 166 listeners.toArray( arr ); 167 } 168 for( int i=0; i<arr.length; i++ ) { 169 arr[i].cellAdded( cell ); 170 } 171 } 172 173 void fireCellRemoved( GridSplitCell cell ) { 174 GridSplitModelListener[] arr; 175 synchronized( listeners ) { 176 arr = new GridSplitModelListener[listeners.size()]; 177 listeners.toArray( arr ); 178 } 179 for( int i=0; i<arr.length; i++ ) { 180 arr[i].cellRemoved( cell ); 181 } 182 } 183 184 void fireCellModified( GridSplitCell oldCell, GridSplitCell newCell ) { 185 GridSplitModelListener[] arr; 186 synchronized( listeners ) { 187 arr = new GridSplitModelListener[listeners.size()]; 188 listeners.toArray( arr ); 189 } 190 for( int i=0; i<arr.length; i++ ) { 191 arr[i].cellModified( oldCell, newCell ); 192 } 193 } 194 195 GridSplitCell findCellFor( Component c ) { 196 if( null == root ) 197 return null; 198 return findCell( root, c ); 199 } 200 201 204 Collection getComponents() { 205 ArrayList res = new ArrayList (); 206 collectComponents( getRootCell(), res ); 207 return res; 208 } 209 210 private void collectComponents( GridSplitCell cell, Collection components ) { 211 if( cell.isSplit() ) { 212 for( int i=0; i<cell.count(); i++ ) { 213 collectComponents( cell.cellAt( i ), components ); 214 } 215 } else { 216 components.add( cell.getComponent() ); 217 } 218 } 219 220 private static GridSplitCell findCell( GridSplitCell cell, Component c ) { 221 if( cell.isSplit() ) { 222 for( int i=0; i<cell.count(); i++ ) { 223 GridSplitCell child = cell.cellAt( i ); 224 GridSplitCell res = findCell( child, c ); 225 if( null != res ) 226 return res; 227 } 228 return null; 229 } else { 230 if( c == cell.getComponent() ) 231 return cell; 232 } 233 return null; 234 } 235 } 236 | Popular Tags |