1 19 20 21 package org.netbeans.core.windows.view; 22 23 24 import org.netbeans.core.windows.Debug; 25 import org.netbeans.core.windows.ModeStructureSnapshot; 26 import org.netbeans.core.windows.ModeStructureSnapshot.ElementSnapshot; 27 import org.netbeans.core.windows.WindowSystemSnapshot; 28 29 import java.util.*; 30 import org.netbeans.core.windows.model.ModelElement; 31 32 33 34 43 final class ViewHelper { 44 45 46 private static final boolean DEBUG = Debug.isLoggable(ViewHelper.class); 47 48 49 50 private ViewHelper() { 51 } 52 53 54 public static WindowSystemAccessor createWindowSystemAccessor( 55 WindowSystemSnapshot wss 56 ) { 57 if(wss == null) { 59 return null; 60 } 61 62 WindowSystemAccessorImpl wsa = new WindowSystemAccessorImpl(); 63 64 ModeStructureAccessorImpl msa = createModeStructureAccessor(wss.getModeStructureSnapshot()); 65 wsa.setModeStructureAccessor(msa); 66 67 ModeStructureSnapshot.ModeSnapshot activeSnapshot = wss.getActiveModeSnapshot(); 68 wsa.setActiveModeAccessor(activeSnapshot == null ? null : msa.findModeAccessor(activeSnapshot.getName())); 69 70 ModeStructureSnapshot.ModeSnapshot maximizedSnapshot = wss.getMaximizedModeSnapshot(); 71 wsa.setMaximizedModeAccessor(maximizedSnapshot == null ? null : msa.findModeAccessor(maximizedSnapshot.getName())); 72 73 wsa.setMainWindowBoundsJoined(wss.getMainWindowBoundsJoined()); 74 wsa.setMainWindowBoundsSeparated(wss.getMainWindowBoundsSeparated()); 75 wsa.setEditorAreaBounds(wss.getEditorAreaBounds()); 76 wsa.setEditorAreaState(wss.getEditorAreaState()); 77 wsa.setEditorAreaFrameState(wss.getEditorAreaFrameState()); 78 wsa.setMainWindowFrameStateJoined(wss.getMainWindowFrameStateJoined()); 79 wsa.setMainWindowFrameStateSeparated(wss.getMainWindowFrameStateSeparated()); 80 wsa.setToolbarConfigurationName(wss.getToolbarConfigurationName()); 81 return wsa; 82 } 83 84 private static ModeStructureAccessorImpl createModeStructureAccessor(ModeStructureSnapshot mss) { 85 ElementAccessor splitRoot = createVisibleAccessor(mss.getSplitRootSnapshot()); 86 Set<ModeAccessor> separateModes = createSeparateModeAccessors(mss.getSeparateModeSnapshots()); 87 Set<SlidingAccessor> slidingModes = createSlidingModeAccessors(mss.getSlidingModeSnapshots()); 88 89 ModeStructureAccessorImpl msa = new ModeStructureAccessorImpl(splitRoot, separateModes, slidingModes); 90 return msa; 91 } 92 93 private static Set<ModeAccessor> createSeparateModeAccessors(ModeStructureSnapshot.ModeSnapshot[] separateModeSnapshots) { 94 Set<ModeAccessor> s = new HashSet<ModeAccessor>(); 95 for(int i = 0; i < separateModeSnapshots.length; i++) { 96 ModeStructureSnapshot.ModeSnapshot snapshot = separateModeSnapshots[i]; 97 if(snapshot.isVisibleSeparate()) { 98 s.add(new ModeStructureAccessorImpl.ModeAccessorImpl( 99 snapshot.getOriginator(), 100 snapshot)); 101 } 102 } 103 104 return s; 105 } 106 107 private static Set<SlidingAccessor> createSlidingModeAccessors(ModeStructureSnapshot.SlidingModeSnapshot[] slidingModeSnapshots) { 108 Set<SlidingAccessor> s = new HashSet<SlidingAccessor>(); 109 ModeStructureSnapshot.SlidingModeSnapshot snapshot; 110 for(int i = 0; i < slidingModeSnapshots.length; i++) { 111 snapshot = slidingModeSnapshots[i]; 112 s.add(new ModeStructureAccessorImpl.SlidingAccessorImpl( 113 snapshot.getOriginator(), 114 snapshot, 115 snapshot.getSide(), 116 snapshot.getSlideInSizes() 117 )); 118 } 119 120 return s; 121 } 122 123 124 private static ElementAccessor createVisibleAccessor(ModeStructureSnapshot.ElementSnapshot snapshot) { 125 if(snapshot == null) { 126 return null; 127 } 128 129 if(snapshot instanceof ModeStructureSnapshot.EditorSnapshot) { ModeStructureSnapshot.EditorSnapshot editorSnapshot = (ModeStructureSnapshot.EditorSnapshot)snapshot; 131 return new ModeStructureAccessorImpl.EditorAccessorImpl( 132 editorSnapshot.getOriginator(), 133 editorSnapshot, 134 createVisibleAccessor(editorSnapshot.getEditorAreaSnapshot()), 135 editorSnapshot.getResizeWeight()); 136 } 137 138 if(snapshot.isVisibleInSplit()) { 139 if(snapshot instanceof ModeStructureSnapshot.SplitSnapshot) { 140 ModeStructureSnapshot.SplitSnapshot splitSnapshot = (ModeStructureSnapshot.SplitSnapshot)snapshot; 141 return createSplitAccessor(splitSnapshot); 142 } else if(snapshot instanceof ModeStructureSnapshot.ModeSnapshot) { 143 ModeStructureSnapshot.ModeSnapshot modeSnapshot = (ModeStructureSnapshot.ModeSnapshot)snapshot; 144 return new ModeStructureAccessorImpl.ModeAccessorImpl( 145 modeSnapshot.getOriginator(), 146 modeSnapshot); 147 } 148 } else { 149 if(snapshot instanceof ModeStructureSnapshot.SplitSnapshot) { 150 ModeStructureSnapshot.SplitSnapshot splitSnapshot = (ModeStructureSnapshot.SplitSnapshot)snapshot; 152 for(Iterator it = splitSnapshot.getChildSnapshots().iterator(); it.hasNext(); ) { 153 ModeStructureSnapshot.ElementSnapshot child = (ModeStructureSnapshot.ElementSnapshot)it.next(); 154 if(child.hasVisibleDescendant()) { 155 return createVisibleAccessor(child); 156 } 157 } 158 } 159 } 160 161 return null; 162 } 163 164 private static ElementAccessor createSplitAccessor(ModeStructureSnapshot.SplitSnapshot splitSnapshot) { 165 List visibleChildren = splitSnapshot.getVisibleChildSnapshots(); 166 167 ArrayList<ElementAccessor> children = new ArrayList<ElementAccessor>( visibleChildren.size() ); 168 ArrayList<Double > weights = new ArrayList<Double >( visibleChildren.size() ); 169 170 int index = 0; 171 for( Iterator i=visibleChildren.iterator(); i.hasNext(); index++ ) { 172 ModeStructureSnapshot.ElementSnapshot child = (ModeStructureSnapshot.ElementSnapshot)i.next(); 173 ElementAccessor childAccessor = createVisibleAccessor( child ); 174 double weight = splitSnapshot.getChildSnapshotSplitWeight( child ); 175 if( childAccessor instanceof SplitAccessor 177 && ((SplitAccessor)childAccessor).getOrientation() == splitSnapshot.getOrientation() ) { 178 SplitAccessor subSplit = (SplitAccessor)childAccessor; 182 ElementAccessor[] childrenToMerge = subSplit.getChildren(); 183 double[] weightsToMerge = subSplit.getSplitWeights(); 184 for( int j=0; j<childrenToMerge.length; j++ ) { 185 children.add( childrenToMerge[j] ); 186 weights.add( Double.valueOf( weightsToMerge[j] * weight ) ); 187 } 188 } else { 189 children.add( childAccessor ); 190 weights.add( Double.valueOf( weight ) ); 191 } 192 } 193 194 ElementAccessor[] childrenAccessors = new ElementAccessor[children.size()]; 195 double[] splitWeights = new double[children.size()]; 196 for( int i=0; i<children.size(); i++ ) { 197 ElementAccessor ea = (ElementAccessor)children.get( i ); 198 Double weight = (Double )weights.get( i ); 199 childrenAccessors[i] = ea; 200 splitWeights[i] = weight.doubleValue(); 201 } 202 203 return new ModeStructureAccessorImpl.SplitAccessorImpl( 204 splitSnapshot.getOriginator(), 205 splitSnapshot, 206 splitSnapshot.getOrientation(), 207 splitWeights, 208 childrenAccessors, 209 splitSnapshot.getResizeWeight()); 210 } 211 212 222 public static void setSplitWeights(SplitAccessor splitAccessor, 223 ElementAccessor[] children, double[] splitWeights, ControllerHandler controllerHandler) { 224 225 ModeStructureSnapshot.SplitSnapshot splitSnapshot = (ModeStructureSnapshot.SplitSnapshot)splitAccessor.getSnapshot(); 226 227 if(splitSnapshot == null) { 228 return; 229 } 230 231 ModelElement[] elements = new ModelElement[ children.length ]; 232 for( int i=0; i<children.length; i++ ) { 233 ModeStructureSnapshot.ElementSnapshot snapshot = findVisibleSplitSnapshot( children[i].getSnapshot() ); 239 elements[ i ] = snapshot.getOriginator(); 240 splitWeights[ i ] = correctNestedSplitWeight( snapshot.getParent(), splitWeights[i] ); 243 } 244 245 controllerHandler.userChangedSplit( elements, splitWeights ); 246 } 247 248 263 private static double correctNestedSplitWeight( ModeStructureSnapshot.SplitSnapshot split, double splitWeight ) { 264 int nestedSplitOrientation = split.getOrientation(); 265 ModeStructureSnapshot.SplitSnapshot parentSplit = split.getParent(); 266 while( null != parentSplit && !parentSplit.isVisibleInSplit() ) { split = parentSplit; 268 parentSplit = parentSplit.getParent(); 269 } 270 if( null != parentSplit && parentSplit.getOrientation() == nestedSplitOrientation ) { 271 double parentSplitWeight = parentSplit.getChildSnapshotSplitWeight( split ); 272 if( parentSplit.getVisibleChildSnapshots().size() > 1 && parentSplitWeight > 0.0 ) 273 splitWeight /= parentSplitWeight; 274 275 return correctNestedSplitWeight( parentSplit, splitWeight ); 276 } 277 return splitWeight; 278 } 279 280 284 private static ModeStructureSnapshot.ElementSnapshot findVisibleSplitSnapshot( ModeStructureSnapshot.ElementSnapshot snapshot ) { 285 ModeStructureSnapshot.SplitSnapshot parent = snapshot.getParent(); 286 if( null != parent ) { 287 List visibleChildren = parent.getVisibleChildSnapshots(); 288 if( visibleChildren.size() == 1 ) { 289 return findVisibleSplitSnapshot( parent ); 290 } 291 } 292 return snapshot; 293 } 294 295 private static void debugLog(String message) { 296 Debug.log(ViewHelper.class, message); 297 } 298 299 } 300 301 | Popular Tags |