KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > ModeStructureSnapshot


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
30
31
32 /**
33  * Snapshot of split structure in model.
34  *
35  * @author Peter Zavadsky
36  */

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     /** Creates a new instance of ModesModelSnapshot. */
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     /** @param name name of mode */
67     public ModeSnapshot findModeSnapshot(String JavaDoc 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 JavaDoc 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     /** Superclass for snapshot of model element.
117      * There are three types, split, mode, and editor (represents editor area) type. */

118     public static abstract class ElementSnapshot {
119         // PENDING revise
120
/** Corresponding object in model (SplitNode or ModeNode or ModeImpl for separate mode). */
121         private final ModelElement originator;
122         
123         private /*final*/ SplitSnapshot parent;
124
125         
126         public ElementSnapshot(ModelElement originator, SplitSnapshot parent) {
127             this.originator = originator;
128             setParent(parent);
129         }
130
131         /** Gets originator object. Used only in model. */
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 JavaDoc("Parent can be set only once," // NOI18N
141
+ " this.parent=" + this.parent + ", parent=" + parent); // NOI18N
142
}
143         }
144         
145         public SplitSnapshot getParent() {
146             return parent;
147         }
148         
149         public boolean originatorEquals(ElementSnapshot o) {
150             return getClass().equals(o.getClass()) // To prevent mismatch between split and mode snapshot.
151
// Split has now originator corresponding to first child.
152
&& ((ElementSnapshot)o).originator == originator;
153         }
154
155         public abstract double getResizeWeight();
156         
157         /** Indicates whether component represented by this element is visible or not. */
158         public abstract boolean isVisibleInSplit();
159
160         /** Indicates whether there is at least one visible descendant (split relevant). */
161         public abstract boolean hasVisibleDescendant();
162         
163         public String JavaDoc toString() {
164             return "Snapshot[originatorHash=" + (originator != null ? Integer.toHexString(originator.hashCode()) : "null" ) + "]"; // NOI18N
165
}
166     }
167     
168     /** */
169     public static class SplitSnapshot extends ElementSnapshot {
170         private final int orientation;
171         private final List JavaDoc<ElementSnapshot> childSnapshots = new ArrayList<ElementSnapshot>();
172         private final Map<ElementSnapshot, Double JavaDoc> childSnapshot2splitWeight = new HashMap<ElementSnapshot, Double JavaDoc>();
173         private final double resizeWeight;
174         
175         public SplitSnapshot(ModelElement originator, SplitSnapshot parent, int orientation,
176                 List JavaDoc<ElementSnapshot> childSnapshots,
177                 Map<ElementSnapshot, Double JavaDoc> childSnapshot2splitWeight, double resizeWeight) {
178             super(originator, parent); // XXX PENDING originator corresponds to the first child model element.
179

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 JavaDoc getVisibleChildSnapshots() {
191             List JavaDoc<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 JavaDoc<ElementSnapshot> getChildSnapshots() {
203             return new ArrayList<ElementSnapshot>(childSnapshots);
204         }
205         
206         public double getChildSnapshotSplitWeight(ElementSnapshot childSnapshot) {
207             Double JavaDoc d = (Double JavaDoc)childSnapshot2splitWeight.get(childSnapshot);
208             return d == null ? -1 : d.doubleValue();
209         }
210         
211         public double getResizeWeight() {
212             return resizeWeight;
213         }
214         
215         /** Indicates whether component represented by this node is visible or not. */
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                     // At leas two are needed so the split is showing.
223
if(count >= 2) {
224                         return true;
225                     }
226                 }
227             }
228             
229             return false;
230         }
231
232         /** Indicates whether there is at least one visible descendant. */
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 JavaDoc toString() {
245             return super.toString() + "[orientation=" // NOI18N
246
+ (orientation == Constants.HORIZONTAL ? "horizontal" : "vertical") // NOI18N
247
+ "]"; // NOI18N
248
}
249        
250     }
251
252     /** */
253     public static class ModeSnapshot extends ElementSnapshot {
254         private final ModeImpl mode;
255         
256         private final String JavaDoc 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             // XXX Even if originators are same, they differ if their states are different.
287
// Difference -> split vs. separate representations.
288
ModeSnapshot me = (ModeSnapshot)o;
289             return getState() == me.getState();
290         }
291         
292         public ModeImpl getMode() {
293             return mode;
294         }
295         
296         public String JavaDoc 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 JavaDoc toString() {
363             return super.toString() + "[name=" + mode.getName() + ", permanent=" + mode.isPermanent() // NOI18N
364
+ ", constraints=" + Arrays.asList(mode.getConstraints()) + "]"; // NOI18N
365
}
366
367     }
368     
369     public static class SlidingModeSnapshot extends ModeSnapshot {
370
371         private final String JavaDoc side;
372         private final Map<TopComponent,Integer JavaDoc> slideInSizes;
373         
374         public SlidingModeSnapshot(ModeImpl mode, String JavaDoc side, Map<TopComponent,Integer JavaDoc> slideInSizes) {
375             super(null, null, mode, 0D);
376             
377             this.side = side;
378             this.slideInSizes = slideInSizes;
379         }
380         
381         public String JavaDoc getSide () {
382             return side;
383         }
384         
385         public Map<TopComponent,Integer JavaDoc> getSlideInSizes() {
386             return slideInSizes;
387         }
388     } // end of SlidingModeSnapshot
389

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         /** Indicates whether component represented by this node is visible or not. */
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         /** Indicates whether there is at least one visible descendant. */
422         public boolean hasVisibleDescendant() {
423             return isVisibleInSplit();
424         }
425         
426         public String JavaDoc toString() {
427             return super.toString() + "\n" + editorAreaSnapshot; // NOI18N
428
}
429     }
430
431     
432     public String JavaDoc toString() {
433         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
434         sb.append("\nModesSnapshot hashCode=" + Integer.toHexString(hashCode())); // NOI18N
435
sb.append("\nSplit modes:\n"); // NOI18N
436
sb.append(dumpSnapshot(splitRootSnapshot, 0));
437         sb.append("\nSeparate Modes:"); // NOI18N
438
sb.append(dumpSet(separateModeSnapshots));
439         return sb.toString();
440     }
441     
442     private static String JavaDoc dumpSnapshot(ElementSnapshot snapshot, int indent) {
443         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
444         String JavaDoc indentString = createIndentString(indent);
445         
446         if(snapshot instanceof SplitSnapshot) {
447             SplitSnapshot splitSnapshot = (SplitSnapshot)snapshot;
448             sb.append(indentString + "split="+splitSnapshot); // NOI18N
449
indent++;
450             for(Iterator it = splitSnapshot.getChildSnapshots().iterator(); it.hasNext(); ) {
451                 ElementSnapshot child = (ElementSnapshot)it.next();
452                 sb.append("\n" + dumpSnapshot(child, indent)); // NOI18N
453
}
454         } else if(snapshot instanceof ModeSnapshot) {
455             sb.append(indentString + "mode=" + snapshot); // NOI18N
456
} else if(snapshot instanceof EditorSnapshot) {
457             sb.append(indentString + "editor=" + snapshot); // NOI18N
458
sb.append(dumpSnapshot(((EditorSnapshot)snapshot).getEditorAreaSnapshot(), ++indent));
459         }
460         
461         return sb.toString();
462     }
463     
464     private static String JavaDoc createIndentString(int indent) {
465         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(indent);
466         for(int i = 0; i < indent; i++) {
467             sb.append(' ');
468         }
469         
470         return sb.toString();
471     }
472     
473     private static String JavaDoc dumpSet(Set separateModes) {
474         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
475         
476         for(java.util.Iterator JavaDoc it = separateModes.iterator(); it.hasNext(); ) {
477             sb.append("\nmode=" + it.next());
478         }
479         
480         return sb.toString();
481     }
482     
483 }
484
485
Popular Tags