1 19 20 21 package org.netbeans.core.windows; 22 23 24 import org.netbeans.core.windows.model.ModelElement; 25 import org.openide.windows.TopComponent; 26 27 import java.awt.*; 28 import java.util.*; 29 import java.util.List ; 30 31 32 37 public class ModeStructureSnapshot { 38 39 private final ElementSnapshot splitRootSnapshot; 40 41 private final Set<ModeSnapshot> separateModeSnapshots; 42 43 private final Set<SlidingModeSnapshot> slidingModeSnapshots; 44 45 46 public ModeStructureSnapshot(ElementSnapshot splitRootSnapshot, 47 Set<ModeSnapshot> separateModeSnapshots, 48 Set<SlidingModeSnapshot> slidingModeSnapshots) { 49 this.splitRootSnapshot = splitRootSnapshot; 50 this.separateModeSnapshots = separateModeSnapshots; 51 this.slidingModeSnapshots = slidingModeSnapshots; 52 } 53 54 public ElementSnapshot getSplitRootSnapshot() { 55 return splitRootSnapshot; 56 } 57 58 public ModeSnapshot[] getSeparateModeSnapshots() { 59 return separateModeSnapshots.toArray(new ModeSnapshot[0]); 60 } 61 62 public SlidingModeSnapshot[] getSlidingModeSnapshots() { 63 return slidingModeSnapshots.toArray(new SlidingModeSnapshot[0]); 64 } 65 66 67 public ModeSnapshot findModeSnapshot(String name) { 68 ModeSnapshot ma = findModeSnapshotOfName(splitRootSnapshot, name); 69 if(ma != null) { 70 return ma; 71 } 72 73 for(Iterator it = separateModeSnapshots.iterator(); it.hasNext(); ) { 74 ma = (ModeSnapshot)it.next(); 75 if(name.equals(ma.getName())) { 76 return ma; 77 } 78 } 79 80 for(Iterator it = slidingModeSnapshots.iterator(); it.hasNext(); ) { 81 ma = (SlidingModeSnapshot)it.next(); 82 if(name.equals(ma.getName())) { 83 return ma; 84 } 85 } 86 87 return null; 88 } 89 90 private static ModeSnapshot findModeSnapshotOfName(ElementSnapshot snapshot, String name) { 91 if(snapshot instanceof ModeSnapshot) { 92 ModeSnapshot ma = (ModeSnapshot)snapshot; 93 if(name.equals(ma.getName())) { 94 return ma; 95 } 96 } else if(snapshot instanceof SplitSnapshot) { 97 SplitSnapshot split = (SplitSnapshot)snapshot; 98 for(Iterator it = split.getChildSnapshots().iterator(); it.hasNext(); ) { 99 ElementSnapshot child = (ElementSnapshot)it.next(); 100 ModeSnapshot ma = findModeSnapshotOfName(child, name); 101 if(ma != null) { 102 return ma; 103 } 104 } 105 } else if(snapshot instanceof EditorSnapshot) { 106 EditorSnapshot editorSnapshot = (EditorSnapshot)snapshot; 107 ModeSnapshot ma = findModeSnapshotOfName(editorSnapshot.getEditorAreaSnapshot(), name); 108 if(ma != null) { 109 return ma; 110 } 111 } 112 113 return null; 114 } 115 116 118 public static abstract class ElementSnapshot { 119 121 private final ModelElement originator; 122 123 private SplitSnapshot parent; 124 125 126 public ElementSnapshot(ModelElement originator, SplitSnapshot parent) { 127 this.originator = originator; 128 setParent(parent); 129 } 130 131 132 public ModelElement getOriginator() { 133 return originator; 134 } 135 136 public void setParent(SplitSnapshot parent) { 137 if(this.parent == null) { 138 this.parent = parent; 139 } else { 140 throw new IllegalStateException ("Parent can be set only once," + " this.parent=" + this.parent + ", parent=" + parent); } 143 } 144 145 public SplitSnapshot getParent() { 146 return parent; 147 } 148 149 public boolean originatorEquals(ElementSnapshot o) { 150 return getClass().equals(o.getClass()) && ((ElementSnapshot)o).originator == originator; 153 } 154 155 public abstract double getResizeWeight(); 156 157 158 public abstract boolean isVisibleInSplit(); 159 160 161 public abstract boolean hasVisibleDescendant(); 162 163 public String toString() { 164 return "Snapshot[originatorHash=" + (originator != null ? Integer.toHexString(originator.hashCode()) : "null" ) + "]"; } 166 } 167 168 169 public static class SplitSnapshot extends ElementSnapshot { 170 private final int orientation; 171 private final List <ElementSnapshot> childSnapshots = new ArrayList<ElementSnapshot>(); 172 private final Map<ElementSnapshot, Double > childSnapshot2splitWeight = new HashMap<ElementSnapshot, Double >(); 173 private final double resizeWeight; 174 175 public SplitSnapshot(ModelElement originator, SplitSnapshot parent, int orientation, 176 List <ElementSnapshot> childSnapshots, 177 Map<ElementSnapshot, Double > childSnapshot2splitWeight, double resizeWeight) { 178 super(originator, parent); 180 this.orientation = orientation; 181 this.childSnapshots.addAll(childSnapshots); 182 this.childSnapshot2splitWeight.putAll(childSnapshot2splitWeight); 183 this.resizeWeight = resizeWeight; 184 } 185 186 public int getOrientation() { 187 return orientation; 188 } 189 190 public List getVisibleChildSnapshots() { 191 List <ElementSnapshot> l = getChildSnapshots(); 192 for(Iterator<ElementSnapshot> it = l.iterator(); it.hasNext(); ) { 193 ElementSnapshot child = it.next(); 194 if(!child.hasVisibleDescendant()) { 195 it.remove(); 196 } 197 } 198 199 return l; 200 } 201 202 public List <ElementSnapshot> getChildSnapshots() { 203 return new ArrayList<ElementSnapshot>(childSnapshots); 204 } 205 206 public double getChildSnapshotSplitWeight(ElementSnapshot childSnapshot) { 207 Double d = (Double )childSnapshot2splitWeight.get(childSnapshot); 208 return d == null ? -1 : d.doubleValue(); 209 } 210 211 public double getResizeWeight() { 212 return resizeWeight; 213 } 214 215 216 public boolean isVisibleInSplit() { 217 int count = 0; 218 for(Iterator it = getChildSnapshots().iterator(); it.hasNext(); ) { 219 ElementSnapshot child = (ElementSnapshot)it.next(); 220 if(child.hasVisibleDescendant()) { 221 count++; 222 if(count >= 2) { 224 return true; 225 } 226 } 227 } 228 229 return false; 230 } 231 232 233 public boolean hasVisibleDescendant() { 234 for(Iterator it = getChildSnapshots().iterator(); it.hasNext(); ) { 235 ElementSnapshot child = (ElementSnapshot)it.next(); 236 if(child.hasVisibleDescendant()) { 237 return true; 238 } 239 } 240 241 return false; 242 } 243 244 public String toString() { 245 return super.toString() + "[orientation=" + (orientation == Constants.HORIZONTAL ? "horizontal" : "vertical") + "]"; } 249 250 } 251 252 253 public static class ModeSnapshot extends ElementSnapshot { 254 private final ModeImpl mode; 255 256 private final String name; 257 private final int state; 258 private final int kind; 259 private final Rectangle bounds; 260 private final int frameState; 261 private final TopComponent selectedTopComponent; 262 private final TopComponent[] openedTopComponents; 263 private final double resizeWeight; 264 265 public ModeSnapshot(ModelElement originator, SplitSnapshot parent, ModeImpl mode, double resizeWeight) { 266 super(originator, parent); 267 268 this.mode = mode; 269 270 this.name = mode.getName(); 271 this.state = mode.getState(); 272 this.kind = mode.getKind(); 273 this.bounds = mode.getBounds(); 274 this.frameState = mode.getFrameState(); 275 this.selectedTopComponent = mode.getSelectedTopComponent(); 276 this.openedTopComponents = mode.getOpenedTopComponents().toArray(new TopComponent[0]); 277 this.resizeWeight = resizeWeight; 278 } 279 280 281 public boolean originatorEquals(ElementSnapshot o) { 282 if(!super.originatorEquals(o)) { 283 return false; 284 } 285 286 ModeSnapshot me = (ModeSnapshot)o; 289 return getState() == me.getState(); 290 } 291 292 public ModeImpl getMode() { 293 return mode; 294 } 295 296 public String getName() { 297 return name; 298 } 299 300 public int getState() { 301 return state; 302 } 303 304 public int getKind() { 305 return kind; 306 } 307 308 public Rectangle getBounds() { 309 return bounds; 310 } 311 312 public int getFrameState() { 313 return frameState; 314 } 315 316 public TopComponent getSelectedTopComponent() { 317 return selectedTopComponent; 318 } 319 320 public TopComponent[] getOpenedTopComponents() { 321 return openedTopComponents; 322 } 323 324 public double getResizeWeight() { 325 return resizeWeight; 326 } 327 328 public boolean isVisibleInSplit() { 329 if(getOpenedTopComponents().length == 0) { 330 return false; 331 } 332 333 if(getState() == Constants.MODE_STATE_SEPARATED) { 334 return false; 335 } 336 337 if(mode.getKind() == Constants.MODE_KIND_EDITOR ) { 338 WindowManagerImpl wm = WindowManagerImpl.getInstance(); 339 if( null != wm.getEditorMaximizedMode() && wm.getEditorMaximizedMode() != mode ) 340 return false; 341 } 342 343 return true; 344 } 345 346 public boolean isVisibleSeparate() { 347 if(getOpenedTopComponents().length == 0) { 348 return false; 349 } 350 351 if(getState() == Constants.MODE_STATE_JOINED) { 352 return false; 353 } 354 355 return true; 356 } 357 358 public boolean hasVisibleDescendant() { 359 return isVisibleInSplit(); 360 } 361 362 public String toString() { 363 return super.toString() + "[name=" + mode.getName() + ", permanent=" + mode.isPermanent() + ", constraints=" + Arrays.asList(mode.getConstraints()) + "]"; } 366 367 } 368 369 public static class SlidingModeSnapshot extends ModeSnapshot { 370 371 private final String side; 372 private final Map<TopComponent,Integer > slideInSizes; 373 374 public SlidingModeSnapshot(ModeImpl mode, String side, Map<TopComponent,Integer > slideInSizes) { 375 super(null, null, mode, 0D); 376 377 this.side = side; 378 this.slideInSizes = slideInSizes; 379 } 380 381 public String getSide () { 382 return side; 383 } 384 385 public Map<TopComponent,Integer > getSlideInSizes() { 386 return slideInSizes; 387 } 388 } 390 391 392 public static class EditorSnapshot extends ElementSnapshot { 393 private final ModeStructureSnapshot.ElementSnapshot editorAreaSnapshot; 394 private final double resizeWeight; 395 396 public EditorSnapshot(ModelElement originator, SplitSnapshot parent, 397 ElementSnapshot editorAreaSnapshot, double resizeWeight) { 398 super(originator, parent); 399 400 this.editorAreaSnapshot = editorAreaSnapshot; 401 this.resizeWeight = resizeWeight; 402 } 403 404 public double getResizeWeight() { 405 return resizeWeight; 406 } 407 408 public ElementSnapshot getEditorAreaSnapshot() { 409 return editorAreaSnapshot; 410 } 411 412 413 public boolean isVisibleInSplit() { 414 if(Constants.SWITCH_HIDE_EMPTY_DOCUMENT_AREA) { 415 return editorAreaSnapshot.isVisibleInSplit(); 416 } else { 417 return true; 418 } 419 } 420 421 422 public boolean hasVisibleDescendant() { 423 return isVisibleInSplit(); 424 } 425 426 public String toString() { 427 return super.toString() + "\n" + editorAreaSnapshot; } 429 } 430 431 432 public String toString() { 433 StringBuffer sb = new StringBuffer (); 434 sb.append("\nModesSnapshot hashCode=" + Integer.toHexString(hashCode())); sb.append("\nSplit modes:\n"); sb.append(dumpSnapshot(splitRootSnapshot, 0)); 437 sb.append("\nSeparate Modes:"); sb.append(dumpSet(separateModeSnapshots)); 439 return sb.toString(); 440 } 441 442 private static String dumpSnapshot(ElementSnapshot snapshot, int indent) { 443 StringBuffer sb = new StringBuffer (); 444 String indentString = createIndentString(indent); 445 446 if(snapshot instanceof SplitSnapshot) { 447 SplitSnapshot splitSnapshot = (SplitSnapshot)snapshot; 448 sb.append(indentString + "split="+splitSnapshot); indent++; 450 for(Iterator it = splitSnapshot.getChildSnapshots().iterator(); it.hasNext(); ) { 451 ElementSnapshot child = (ElementSnapshot)it.next(); 452 sb.append("\n" + dumpSnapshot(child, indent)); } 454 } else if(snapshot instanceof ModeSnapshot) { 455 sb.append(indentString + "mode=" + snapshot); } else if(snapshot instanceof EditorSnapshot) { 457 sb.append(indentString + "editor=" + snapshot); sb.append(dumpSnapshot(((EditorSnapshot)snapshot).getEditorAreaSnapshot(), ++indent)); 459 } 460 461 return sb.toString(); 462 } 463 464 private static String createIndentString(int indent) { 465 StringBuffer sb = new StringBuffer (indent); 466 for(int i = 0; i < indent; i++) { 467 sb.append(' '); 468 } 469 470 return sb.toString(); 471 } 472 473 private static String dumpSet(Set separateModes) { 474 StringBuffer sb = new StringBuffer (); 475 476 for(java.util.Iterator it = separateModes.iterator(); it.hasNext(); ) { 477 sb.append("\nmode=" + it.next()); 478 } 479 480 return sb.toString(); 481 } 482 483 } 484 485 | Popular Tags |