KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > view > ModeStructureAccessorImpl


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.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 JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Set JavaDoc;
34
35
36 /**
37  * Used to pass information of modes model state to view in nice fashion.
38  * Need to figure out to which package this class belongs, temporary here.
39  *
40  * @author Peter Zavadsky
41  */

42 final class ModeStructureAccessorImpl implements ModeStructureAccessor {
43     
44     private final ElementAccessor splitRootAccessor;
45     
46     private final Set JavaDoc<ModeAccessor> separateModeAccessors;
47     
48     private final Set JavaDoc<SlidingAccessor> slidingModeAccessors;
49     
50     /** Creates a new instance of ModesModelAccessorImpl. */
51     public ModeStructureAccessorImpl(ElementAccessor splitRootAccessor,
52             Set JavaDoc<ModeAccessor> separateModeAccessors,
53             Set JavaDoc<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     /** @param name name of mode */
72     public ModeAccessor findModeAccessor(String JavaDoc name) {
73         ModeAccessor ma = findModeAccessorOfName(splitRootAccessor, name);
74         if(ma != null) {
75             return ma;
76         }
77         
78         for(Iterator JavaDoc 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 JavaDoc 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 JavaDoc 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     /** Superclass for accessor of model element.
123      * There are three types, split, mode, and editor (represents editor area) type. */

124     static abstract class ElementAccessorImpl implements ElementAccessor {
125         // PENDING revise
126
/** Corresponding object in model (SplitNode or ModeNode for separate mode). */
127         private final ModelElement originator;
128         /** Corresponding snapshot. */
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         /** Gets originator object. Used only in model. */
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()) // To prevent mismatch between split and mode accessor.
149
// Split has now originator corresponding to first child.
150
&& ((ElementAccessorImpl)o).originator == originator;
151             }
152             return false;
153         }
154         
155         public String JavaDoc toString() {
156             return super.toString() + "[originatorHash=" + (originator != null ? Integer.toHexString(originator.hashCode()) : "null") + "]"; // NOI18N
157
}
158     }
159     
160     /** */
161     static final class SplitAccessorImpl extends ElementAccessorImpl implements SplitAccessor {
162         private final int orientation;
163         private final double[] splitPositions; // relative
164
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); // It correspond to the first child model element.
171

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 JavaDoc toString() {
195             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
196             buffer.append( super.toString() );
197             buffer.append( "[orientation=" + orientation );// NOI18N
198
buffer.append( ", splitPosition=" ); // NOI18N
199
for( int i=0; i<splitPositions.length; i++ ) {
200                 buffer.append( splitPositions[i] );
201                 if( i < splitPositions.length-1 )
202                     buffer.append( " : " ); // NOI18N
203
}
204             buffer.append( "]" ); // NOI18N
205
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             // XXX Even if originators are same, they differ if their states are different.
227
// Difference -> split vs. separate representations.
228
ModeAccessor me = (ModeAccessor)o;
229             return getState() == me.getState();
230         }
231         
232         public ModeImpl getMode() {
233             return getModeSnapShot().getMode();
234         }
235         
236         public String JavaDoc 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 JavaDoc toString() {
269             return super.toString() + "[name=" + getName() + " ]"; // NOI18N
270
}
271
272     }
273
274     /** Data accessor for sliding view */
275     static final class SlidingAccessorImpl extends ModeAccessorImpl implements SlidingAccessor {
276
277         private final String JavaDoc side;
278         private final Map JavaDoc<TopComponent,Integer JavaDoc> slideInSizes;
279         
280         public SlidingAccessorImpl(ModelElement originator,
281                 ModeStructureSnapshot.ModeSnapshot snapshot,
282                 String JavaDoc side, Map JavaDoc<TopComponent,Integer JavaDoc> slideInSizes) {
283             super(originator, snapshot);
284
285             this.side = side;
286             this.slideInSizes = slideInSizes;
287         }
288     
289         public String JavaDoc getSide() {
290             return side;
291         }
292         
293         public Map JavaDoc<TopComponent,Integer JavaDoc> getSlideInSizes() {
294             return slideInSizes;
295         }
296         
297         public boolean originatorEquals(ElementAccessor o) {
298             if(!super.originatorEquals(o)) {
299                 return false;
300             }
301             
302             // XXX Even if originators are same, they differ if their side are different.
303
SlidingAccessor me = (SlidingAccessor)o;
304             return getSide() == me.getSide();
305         }
306         
307     } // end of SlidingAccessorImpl
308

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 JavaDoc toString() {
332             return super.toString() + "\n" + editorAreaAccessor; // NOI18N
333
}
334     }
335
336     
337     public String JavaDoc toString() {
338         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
339         sb.append("\nModesAccessorImpl hashCode=" + hashCode()); // NOI18N
340
sb.append("\nSplit modes:\n"); // NOI18N
341
sb.append(dumpAccessor(splitRootAccessor, 0));
342         sb.append("\nSeparate Modes:"); // NOI18N
343
sb.append(dumpSet(separateModeAccessors));
344         return sb.toString();
345     }
346     
347     private static String JavaDoc dumpAccessor(ElementAccessor accessor, int indent) {
348         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
349         String JavaDoc indentString = createIndentString(indent);
350         
351         if(accessor instanceof SplitAccessor) {
352             SplitAccessor splitAccessor = (SplitAccessor)accessor;
353             sb.append(indentString + "split="+splitAccessor); // NOI18N
354
indent++;
355             ElementAccessor[] children = splitAccessor.getChildren();
356             for( int i=0; i<children.length; i++ ) {
357                 sb.append("\n" + dumpAccessor(children[i], indent)); // NOI18N
358
}
359         } else if(accessor instanceof ModeAccessor) {
360             sb.append(indentString + "mode=" + accessor); // NOI18N
361
} else if(accessor instanceof EditorAccessor) {
362             sb.append(indentString + "editor=" + accessor); // NOI18N
363
sb.append(dumpAccessor(((EditorAccessor)accessor).getEditorAreaAccessor(), ++indent));
364         }
365         
366         return sb.toString();
367     }
368     
369     private static String JavaDoc createIndentString(int indent) {
370         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(indent);
371         for(int i = 0; i < indent; i++) {
372             sb.append(' ');
373         }
374         
375         return sb.toString();
376     }
377     
378     private static String JavaDoc dumpSet(Set JavaDoc separateModes) {
379         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
380         
381         for(java.util.Iterator JavaDoc 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