1 19 20 package org.netbeans.modules.form.layoutdesign; 21 22 import java.awt.Rectangle ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyChangeSupport ; 25 import java.util.*; 26 27 41 42 public final class LayoutComponent implements LayoutConstants { 43 44 private String componentId; 46 47 private LayoutComponent parentComponent; 49 50 private LayoutInterval[] layoutIntervals; 53 54 private boolean[] resizability; 56 57 private LayoutInterval[] layoutRoots; 61 62 private java.util.List subComponents; 64 65 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport (this); 66 67 private int horizontalLinkId = NOT_EXPLICITLY_DEFINED; 69 70 private int verticalLinkId = NOT_EXPLICITLY_DEFINED; 72 73 76 public LayoutComponent(String id, boolean isContainer) { 77 if (id == null) 78 throw new NullPointerException (); 79 componentId = id; 80 layoutIntervals = new LayoutInterval[DIM_COUNT]; 81 for (int i=0; i < DIM_COUNT; i++) { 82 layoutIntervals[i] = new LayoutInterval(SINGLE); 83 layoutIntervals[i].setComponent(this); 84 layoutIntervals[i].setSizes(USE_PREFERRED_SIZE, 85 NOT_EXPLICITLY_DEFINED, 86 USE_PREFERRED_SIZE); 87 } 88 if (isContainer) { 89 layoutRoots = new LayoutInterval[DIM_COUNT]; 90 for (int i=0; i < DIM_COUNT; i++) { 91 layoutRoots[i] = new LayoutInterval(PARALLEL); 92 } 96 } 97 } 98 99 public LayoutComponent(String id, boolean isContainer, int initialWidth, int initialHeight) { 100 this(id, isContainer); 101 if (isContainer) { 102 for (int i=0; i < DIM_COUNT; i++) { 103 LayoutInterval gap = new LayoutInterval(SINGLE); 104 gap.setSizes(0, i==HORIZONTAL ? initialWidth : initialHeight, Short.MAX_VALUE); 105 layoutRoots[i].add(gap, 0); 106 } 107 } 108 else { 109 layoutIntervals[HORIZONTAL].setPreferredSize(initialWidth); 110 layoutIntervals[VERTICAL].setPreferredSize(initialHeight); 111 } 112 } 113 114 void setId(String id) { 115 componentId = id; 116 } 117 118 void setLayoutInterval(LayoutInterval interval, int dimension) { 119 layoutIntervals[dimension] = interval; 120 } 121 122 void setResizability(boolean[] resizability) { 123 this.resizability = resizability; 124 } 125 126 boolean[] getResizability() { 127 return resizability; 128 } 129 130 133 public String getId() { 134 return componentId; 135 } 136 137 public LayoutComponent getParent() { 138 return parentComponent; 139 } 140 141 public boolean isParentOf(LayoutComponent comp) { 142 do { 143 comp = comp.getParent(); 144 if (comp == this) 145 return true; 146 } 147 while (comp != null); 148 return false; 149 } 150 151 public LayoutInterval getLayoutInterval(int dimension) { 152 return layoutIntervals[dimension]; 153 } 154 155 public boolean isLayoutContainer() { 156 return layoutRoots != null; 157 } 158 159 public LayoutInterval getLayoutRoot(int dimension) { 160 return layoutRoots[dimension]; 161 } 162 163 LayoutInterval[] getLayoutRoots() { 164 return layoutRoots; 165 } 166 167 169 public Iterator getSubcomponents() { 170 return subComponents != null && subComponents.size() > 0 ? 171 subComponents.iterator() : Collections.EMPTY_LIST.iterator(); 172 } 173 174 int getSubComponentCount() { 175 return (subComponents == null) ? 0 : subComponents.size(); 176 } 177 178 LayoutComponent getSubComponent(int index) { 179 return (LayoutComponent)subComponents.get(index); 180 } 181 182 int indexOf(LayoutComponent comp) { 183 return subComponents != null ? subComponents.indexOf(comp) : -1; 184 } 185 186 190 int add(LayoutComponent comp, int index) { 191 assert isLayoutContainer(); 192 193 if (subComponents == null) { 194 subComponents = new LinkedList(); 195 } 196 if (index < 0) { 197 index = subComponents.size(); 198 } 199 subComponents.add(index, comp); 200 comp.parentComponent = this; 201 202 return index; 203 } 204 205 int remove(LayoutComponent comp) { 206 int index; 207 if (subComponents != null) { 208 index = subComponents.indexOf(comp); 209 if (index >= 0) { 210 subComponents.remove(index); 211 comp.parentComponent = null; 212 } 213 } 214 else index = -1; 215 return index; 216 } 217 218 void setLayoutContainer(boolean isContainer, LayoutInterval[] roots) { 219 if (isContainer != isLayoutContainer()) { 220 if (isContainer) { 221 if (roots == null) { 222 layoutRoots = new LayoutInterval[DIM_COUNT]; 223 for (int i=0; i < DIM_COUNT; i++) { 224 layoutRoots[i] = new LayoutInterval(PARALLEL); 225 } 226 } else { 227 layoutRoots = roots; 228 } 229 } 230 else { 231 layoutRoots = null; 232 subComponents = null; 233 } 234 } 235 } 236 237 239 static LayoutComponent getCommonParent(LayoutComponent comp1, LayoutComponent comp2) { 240 Iterator parents1 = parentsOfComponent(comp1).iterator(); 242 Iterator parents2 = parentsOfComponent(comp2).iterator(); 243 LayoutComponent parent1 = (LayoutComponent)parents1.next(); 244 LayoutComponent parent2 = (LayoutComponent)parents2.next(); 245 246 LayoutComponent parent = null; 248 while (parent1 == parent2) { 249 parent = parent1; 250 if (parents1.hasNext()) { 251 parent1 = (LayoutComponent)parents1.next(); 252 } else { 253 break; 254 } 255 if (parents2.hasNext()) { 256 parent2 = (LayoutComponent)parents2.next(); 257 } else { 258 break; 259 } 260 } 261 return parent; 262 } 263 264 private static List parentsOfComponent(LayoutComponent comp) { 265 List parents = new LinkedList(); 266 while (comp != null) { 267 parents.add(0, comp); 268 comp = comp.getParent(); 269 } 270 return parents; 271 } 272 273 277 void setCurrentBounds(Rectangle bounds, int baseline) { 278 LayoutRegion space = layoutIntervals[0].getCurrentSpace();; 279 space.set(bounds, baseline > 0 ? bounds.y + baseline : LayoutRegion.UNKNOWN); 280 for (int i=1; i < layoutIntervals.length; i++) { 281 layoutIntervals[i].setCurrentSpace(space); 282 } 283 } 284 285 void setCurrentInterior(Rectangle bounds) { 286 LayoutRegion space = null; 287 for (int i=0; i < layoutRoots.length; i++) { 288 if (space == null) { 289 space = layoutRoots[i].getCurrentSpace(); 290 space.set(bounds, LayoutRegion.UNKNOWN); 291 } 292 else { 293 layoutRoots[i].setCurrentSpace(space); 294 } 295 } 296 } 297 298 301 public boolean isLinkSized(int dimension) { 302 if (dimension == HORIZONTAL) { 303 return NOT_EXPLICITLY_DEFINED != horizontalLinkId; 304 } 305 return NOT_EXPLICITLY_DEFINED != verticalLinkId; 306 } 307 308 311 public int getLinkSizeId(int dimension) { 312 if (dimension == HORIZONTAL) { 313 return horizontalLinkId; 314 } 315 return verticalLinkId; 316 } 317 318 321 public void setLinkSizeId(int id, int dimension) { 322 if (dimension == HORIZONTAL) { 323 horizontalLinkId = id; 324 } else { 325 verticalLinkId = id; 326 } 327 328 } 329 330 public void addPropertyChangeListener(PropertyChangeListener listener) { 332 propertyChangeSupport.addPropertyChangeListener(listener); 333 } 334 335 public void removePropertyChangeListener(PropertyChangeListener listener) { 336 propertyChangeSupport.removePropertyChangeListener(listener); 337 } 338 339 void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 340 propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue); 341 } 342 343 } 344 | Popular Tags |