1 19 20 21 package org.netbeans.core.windows.view; 22 23 24 import org.netbeans.core.windows.ModeImpl; 25 import org.netbeans.core.windows.ModeStructureSnapshot; 26 import org.netbeans.core.windows.ModeStructureSnapshot.ElementSnapshot; 27 import org.netbeans.core.windows.model.ModelElement; 28 import org.openide.windows.TopComponent; 29 30 import java.awt.*; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.Set ; 34 35 36 42 final class ModeStructureAccessorImpl implements ModeStructureAccessor { 43 44 private final ElementAccessor splitRootAccessor; 45 46 private final Set <ModeAccessor> separateModeAccessors; 47 48 private final Set <SlidingAccessor> slidingModeAccessors; 49 50 51 public ModeStructureAccessorImpl(ElementAccessor splitRootAccessor, 52 Set <ModeAccessor> separateModeAccessors, 53 Set <SlidingAccessor> slidingModeAccessors) { 54 this.splitRootAccessor = splitRootAccessor; 55 this.separateModeAccessors = separateModeAccessors; 56 this.slidingModeAccessors = slidingModeAccessors; 57 } 58 59 public ElementAccessor getSplitRootAccessor() { 60 return splitRootAccessor; 61 } 62 63 public ModeAccessor[] getSeparateModeAccessors() { 64 return separateModeAccessors.toArray(new ModeAccessor[0]); 65 } 66 67 public SlidingAccessor[] getSlidingModeAccessors() { 68 return slidingModeAccessors.toArray(new SlidingAccessor[0]); 69 } 70 71 72 public ModeAccessor findModeAccessor(String name) { 73 ModeAccessor ma = findModeAccessorOfName(splitRootAccessor, name); 74 if(ma != null) { 75 return ma; 76 } 77 78 for(Iterator it = separateModeAccessors.iterator(); it.hasNext(); ) { 79 ma = (ModeAccessor)it.next(); 80 if(name.equals(ma.getName())) { 81 return ma; 82 } 83 } 84 85 for(Iterator it = slidingModeAccessors.iterator(); it.hasNext(); ) { 86 ma = (ModeAccessor)it.next(); 87 if(name.equals(ma.getName())) { 88 return ma; 89 } 90 } 91 92 return null; 93 } 94 95 private static ModeAccessor findModeAccessorOfName(ElementAccessor accessor, String name) { 96 if(accessor instanceof ModeAccessor) { 97 ModeAccessor ma = (ModeAccessor)accessor; 98 if(name.equals(ma.getName())) { 99 return ma; 100 } 101 } else if(accessor instanceof SplitAccessor) { 102 SplitAccessor split = (SplitAccessor)accessor; 103 ElementAccessor[] children = split.getChildren(); 104 for( int i=0; i<children.length; i++ ) { 105 ModeAccessor ma = findModeAccessorOfName(children[i], name); 106 if(ma != null) { 107 return ma; 108 } 109 } 110 } else if(accessor instanceof EditorAccessor) { 111 EditorAccessor editorAccessor = (EditorAccessor)accessor; 112 ModeAccessor ma = findModeAccessorOfName(editorAccessor.getEditorAreaAccessor(), name); 113 if(ma != null) { 114 return ma; 115 } 116 } 117 118 return null; 119 } 120 121 122 124 static abstract class ElementAccessorImpl implements ElementAccessor { 125 127 private final ModelElement originator; 128 129 private final ModeStructureSnapshot.ElementSnapshot snapshot; 130 131 132 public ElementAccessorImpl(ModelElement originator, ModeStructureSnapshot.ElementSnapshot snapshot) { 133 this.originator = originator; 134 this.snapshot = snapshot; 135 } 136 137 138 public final ModelElement getOriginator() { 139 return originator; 140 } 141 142 public final ModeStructureSnapshot.ElementSnapshot getSnapshot() { 143 return snapshot; 144 } 145 146 public boolean originatorEquals(ElementAccessor o) { 147 if(o instanceof ElementAccessorImpl) { 148 return getClass().equals(o.getClass()) && ((ElementAccessorImpl)o).originator == originator; 151 } 152 return false; 153 } 154 155 public String toString() { 156 return super.toString() + "[originatorHash=" + (originator != null ? Integer.toHexString(originator.hashCode()) : "null") + "]"; } 158 } 159 160 161 static final class SplitAccessorImpl extends ElementAccessorImpl implements SplitAccessor { 162 private final int orientation; 163 private final double[] splitPositions; private final ElementAccessor[] children; 165 private final double resizeWeight; 166 167 public SplitAccessorImpl(ModelElement originator, ElementSnapshot snapshot, 168 int orientation, double[] splitPositions, 169 ElementAccessor[] children, double resizeWeight) { 170 super(originator, snapshot); 172 this.orientation = orientation; 173 this.splitPositions = splitPositions; 174 this.children = children; 175 this.resizeWeight = resizeWeight; 176 } 177 178 public int getOrientation() { 179 return orientation; 180 } 181 182 public double[] getSplitWeights() { 183 return splitPositions; 184 } 185 186 public ElementAccessor[] getChildren() { 187 return children; 188 } 189 190 public double getResizeWeight() { 191 return resizeWeight; 192 } 193 194 public String toString() { 195 StringBuffer buffer = new StringBuffer (); 196 buffer.append( super.toString() ); 197 buffer.append( "[orientation=" + orientation ); buffer.append( ", splitPosition=" ); for( int i=0; i<splitPositions.length; i++ ) { 200 buffer.append( splitPositions[i] ); 201 if( i < splitPositions.length-1 ) 202 buffer.append( " : " ); } 204 buffer.append( "]" ); return buffer.toString(); 206 } 207 } 208 209 210 static class ModeAccessorImpl extends ElementAccessorImpl implements ModeAccessor { 211 212 public ModeAccessorImpl(ModelElement originator, ModeStructureSnapshot.ModeSnapshot snapshot) { 213 super(originator, snapshot); 214 } 215 216 private ModeStructureSnapshot.ModeSnapshot getModeSnapShot() { 217 return (ModeStructureSnapshot.ModeSnapshot)getSnapshot(); 218 } 219 220 221 public boolean originatorEquals(ElementAccessor o) { 222 if(!super.originatorEquals(o)) { 223 return false; 224 } 225 226 ModeAccessor me = (ModeAccessor)o; 229 return getState() == me.getState(); 230 } 231 232 public ModeImpl getMode() { 233 return getModeSnapShot().getMode(); 234 } 235 236 public String getName() { 237 return getModeSnapShot().getName(); 238 } 239 240 public int getState() { 241 return getModeSnapShot().getState(); 242 } 243 244 public int getKind() { 245 return getModeSnapShot().getKind(); 246 } 247 248 public Rectangle getBounds() { 249 return getModeSnapShot().getBounds(); 250 } 251 252 public int getFrameState() { 253 return getModeSnapShot().getFrameState(); 254 } 255 256 public TopComponent getSelectedTopComponent() { 257 return getModeSnapShot().getSelectedTopComponent(); 258 } 259 260 public TopComponent[] getOpenedTopComponents() { 261 return getModeSnapShot().getOpenedTopComponents(); 262 } 263 264 public double getResizeWeight() { 265 return getModeSnapShot().getResizeWeight(); 266 } 267 268 public String toString() { 269 return super.toString() + "[name=" + getName() + " ]"; } 271 272 } 273 274 275 static final class SlidingAccessorImpl extends ModeAccessorImpl implements SlidingAccessor { 276 277 private final String side; 278 private final Map <TopComponent,Integer > slideInSizes; 279 280 public SlidingAccessorImpl(ModelElement originator, 281 ModeStructureSnapshot.ModeSnapshot snapshot, 282 String side, Map <TopComponent,Integer > slideInSizes) { 283 super(originator, snapshot); 284 285 this.side = side; 286 this.slideInSizes = slideInSizes; 287 } 288 289 public String getSide() { 290 return side; 291 } 292 293 public Map <TopComponent,Integer > getSlideInSizes() { 294 return slideInSizes; 295 } 296 297 public boolean originatorEquals(ElementAccessor o) { 298 if(!super.originatorEquals(o)) { 299 return false; 300 } 301 302 SlidingAccessor me = (SlidingAccessor)o; 304 return getSide() == me.getSide(); 305 } 306 307 } 309 310 311 static final class EditorAccessorImpl extends ElementAccessorImpl implements EditorAccessor { 312 private final ElementAccessor editorAreaAccessor; 313 private final double resizeWeight; 314 315 public EditorAccessorImpl(ModelElement originator, ElementSnapshot snapshot, 316 ElementAccessor editorAreaAccessor, double resizeWeight) { 317 super(originator, snapshot); 318 319 this.editorAreaAccessor = editorAreaAccessor; 320 this.resizeWeight = resizeWeight; 321 } 322 323 public double getResizeWeight() { 324 return resizeWeight; 325 } 326 327 public ElementAccessor getEditorAreaAccessor() { 328 return editorAreaAccessor; 329 } 330 331 public String toString() { 332 return super.toString() + "\n" + editorAreaAccessor; } 334 } 335 336 337 public String toString() { 338 StringBuffer sb = new StringBuffer (); 339 sb.append("\nModesAccessorImpl hashCode=" + hashCode()); sb.append("\nSplit modes:\n"); sb.append(dumpAccessor(splitRootAccessor, 0)); 342 sb.append("\nSeparate Modes:"); sb.append(dumpSet(separateModeAccessors)); 344 return sb.toString(); 345 } 346 347 private static String dumpAccessor(ElementAccessor accessor, int indent) { 348 StringBuffer sb = new StringBuffer (); 349 String indentString = createIndentString(indent); 350 351 if(accessor instanceof SplitAccessor) { 352 SplitAccessor splitAccessor = (SplitAccessor)accessor; 353 sb.append(indentString + "split="+splitAccessor); indent++; 355 ElementAccessor[] children = splitAccessor.getChildren(); 356 for( int i=0; i<children.length; i++ ) { 357 sb.append("\n" + dumpAccessor(children[i], indent)); } 359 } else if(accessor instanceof ModeAccessor) { 360 sb.append(indentString + "mode=" + accessor); } else if(accessor instanceof EditorAccessor) { 362 sb.append(indentString + "editor=" + accessor); sb.append(dumpAccessor(((EditorAccessor)accessor).getEditorAreaAccessor(), ++indent)); 364 } 365 366 return sb.toString(); 367 } 368 369 private static String createIndentString(int indent) { 370 StringBuffer sb = new StringBuffer (indent); 371 for(int i = 0; i < indent; i++) { 372 sb.append(' '); 373 } 374 375 return sb.toString(); 376 } 377 378 private static String dumpSet(Set separateModes) { 379 StringBuffer sb = new StringBuffer (); 380 381 for(java.util.Iterator it = separateModes.iterator(); it.hasNext(); ) { 382 sb.append("\nmode=" + it.next()); 383 } 384 385 return sb.toString(); 386 } 387 388 389 } 390 391 | Popular Tags |