KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > model > ModesSubModel


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 package org.netbeans.core.windows.model;
21
22 import java.util.Collections JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import org.netbeans.core.windows.Constants;
27 import org.netbeans.core.windows.ModeImpl;
28 import org.netbeans.core.windows.ModeStructureSnapshot;
29 import org.netbeans.core.windows.SplitConstraint;
30
31 import java.util.HashSet JavaDoc;
32 import java.util.Set JavaDoc;
33 import org.netbeans.core.windows.WindowManagerImpl;
34 import org.openide.windows.TopComponent;
35
36
37 /**
38  * Sub-model which keeps modes data strucute, i.e. as split and also separate
39  * ones. Note, it keeps editor ones in another field of split model, for better manipulation.
40  * See UI spec about editor area and editor/view component types.
41  * Note: this instances aren't thread safe, thus they has to be properly synchronized.
42  * The client has to synchronize it.
43  * (see in DefaultModel, where it is only place supposed to be used.)
44  *
45  * @author Peter Zavadsky
46  */

47 final class ModesSubModel {
48
49     /** Associated parent model. */
50     private final Model parentModel;
51     
52     /** Set of modes. */
53     private final Set JavaDoc<ModeImpl> modes = new HashSet JavaDoc<ModeImpl>(10);
54
55     /** Represents split model of modes, also contains special editor area. */
56     private final EditorSplitSubModel editorSplitSubModel;
57    
58     /** Sliding modes model, <ModeImpl, String> mapping
59      of mode and side of presence */

60     private final HashMap JavaDoc<ModeImpl, String JavaDoc> slidingModes2Sides = new HashMap JavaDoc<ModeImpl, String JavaDoc>(5);
61     private final HashMap JavaDoc<String JavaDoc, ModeImpl> slidingSides2Modes = new HashMap JavaDoc<String JavaDoc, ModeImpl>(5);
62
63     /** Active mode. */
64     private ModeImpl activeMode;
65     /** Maximized mode. */
66     private ModeImpl editorMaximizedMode;
67     private ModeImpl viewMaximizedMode;
68     
69     // (sliding side + TopComponent ID) -> size in pixels (width or height
70
//depending on the sliding side)
71
private final Map JavaDoc<String JavaDoc,Integer JavaDoc> slideInSizes = new HashMap JavaDoc<String JavaDoc,Integer JavaDoc>(15);
72      
73     
74     /** Creates a new instance of ModesModel */
75     public ModesSubModel(Model parentModel) {
76         this.parentModel = parentModel;
77
78         this.editorSplitSubModel = new EditorSplitSubModel(parentModel, new SplitSubModel(parentModel));
79     }
80
81     
82     public void setEditorAreaConstraints(SplitConstraint[] editorAreaConstraints) {
83         editorSplitSubModel.setEditorNodeConstraints(editorAreaConstraints);
84     }
85     
86     public SplitConstraint[] getModelElementConstraints(ModelElement element) {
87         return editorSplitSubModel.getModelElementConstraints(element);
88     }
89     
90     public SplitConstraint[] getEditorAreaConstraints() {
91         return editorSplitSubModel.getEditorNodeConstraints();
92     }
93     
94     public SplitConstraint[] getModeConstraints(ModeImpl mode) {
95         if(mode.getKind() == Constants.MODE_KIND_EDITOR) {
96             return editorSplitSubModel.getEditorArea().getModeConstraints(mode);
97         } else {
98             return editorSplitSubModel.getModeConstraints(mode);
99         }
100     }
101     
102     /**
103      * Find the side (LEFT/RIGHT/BOTTOM) where the TopComponent from the given
104      * mode should slide to.
105      *
106      * @param mode Mode
107      * @return The slide side for TopComponents from the given mode.
108      */

109     public String JavaDoc getSlideSideForMode( ModeImpl mode ) {
110         return editorSplitSubModel.getSlideSideForMode( mode );
111     }
112     
113     public String JavaDoc getSlidingModeConstraints(ModeImpl mode) {
114         return slidingModes2Sides.get(mode);
115     }
116     
117     public ModeImpl getSlidingMode(String JavaDoc side) {
118         return slidingSides2Modes.get(side);
119     }
120     
121     public Set JavaDoc<ModeImpl> getSlidingModes() {
122         return Collections.unmodifiableSet(slidingModes2Sides.keySet());
123     }
124
125     public boolean addMode(ModeImpl mode, SplitConstraint[] constraints) {
126         if(modes.contains(mode)) {
127             return false;
128         }
129
130         boolean result;
131         if(mode.getKind() == Constants.MODE_KIND_EDITOR &&
132            mode.getState() == Constants.MODE_STATE_JOINED) {
133             result = editorSplitSubModel.getEditorArea().addMode(mode, constraints);
134         } else {
135             result = editorSplitSubModel.addMode(mode, constraints);
136         }
137
138         if(result) {
139             modes.add(mode);
140         }
141         return result;
142     }
143     
144     // XXX
145
public boolean addModeToSide(ModeImpl mode, ModeImpl attachMode, String JavaDoc side) {
146         if(modes.contains(mode)) {
147             return false;
148         }
149
150         boolean result;
151         // XXX PENDING
152
if(mode.getKind() == Constants.MODE_KIND_EDITOR) {
153             result = editorSplitSubModel.getEditorArea().addModeToSide(mode, attachMode, side);
154         } else {
155             result = editorSplitSubModel.addModeToSide(mode, attachMode, side);
156         }
157
158         if(result) {
159             modes.add(mode);
160         }
161         return result;
162     }
163     
164     // XXX
165
public boolean addModeAround(ModeImpl mode, String JavaDoc side) {
166         if(modes.contains(mode)) {
167             return false;
168         }
169
170         boolean result;
171         // XXX PENDING
172
if(mode.getKind() == Constants.MODE_KIND_EDITOR) {
173             result = false;
174         } else {
175             result = editorSplitSubModel.addModeAround(mode, side);
176         }
177
178         if(result) {
179             modes.add(mode);
180         }
181         return result;
182     }
183     
184     // XXX
185
public boolean addModeAroundEditor(ModeImpl mode, String JavaDoc side) {
186         if(modes.contains(mode)) {
187             return false;
188         }
189
190         boolean result;
191         // XXX PENDING
192
if(mode.getKind() == Constants.MODE_KIND_EDITOR) {
193             result = editorSplitSubModel.getEditorArea().addModeToSideRoot(mode, side);
194         } else {
195             result = editorSplitSubModel.addModeAroundEditor(mode, side);
196         }
197
198         if(result) {
199             modes.add(mode);
200         }
201         return result;
202     }
203     
204     public boolean addModeSliding(ModeImpl mode, String JavaDoc side, Map JavaDoc<String JavaDoc,Integer JavaDoc> slideInSizes) {
205         if(modes.contains(mode) || (mode.getKind() != Constants.MODE_KIND_SLIDING)) {
206             return false;
207         }
208
209         slidingModes2Sides.put(mode, side);
210         slidingSides2Modes.put(side, mode);
211         
212         modes.add(mode);
213         
214         if( null != slideInSizes ) {
215             for( Iterator JavaDoc<String JavaDoc> i=slideInSizes.keySet().iterator(); i.hasNext(); ) {
216                 String JavaDoc tcId = i.next();
217                 this.slideInSizes.put( side+tcId, slideInSizes.get( tcId ) );
218             }
219         }
220         
221         return true;
222     }
223
224     public Map JavaDoc<String JavaDoc, Integer JavaDoc> getSlideInSizes(String JavaDoc side) {
225         Map JavaDoc<String JavaDoc,Integer JavaDoc> res = new HashMap JavaDoc<String JavaDoc,Integer JavaDoc>( 5 );
226         for( Iterator JavaDoc<String JavaDoc> i=slideInSizes.keySet().iterator(); i.hasNext(); ) {
227             String JavaDoc key = i.next();
228             if( key.startsWith( side ) ) {
229                 String JavaDoc tcId = key.substring( side.length() );
230                 Integer JavaDoc size = slideInSizes.get( key );
231                 res.put( tcId, size );
232             }
233         }
234         return res;
235     }
236     
237     public Map JavaDoc<TopComponent, Integer JavaDoc> getSlideInSizes(ModeImpl mode) {
238         WindowManagerImpl wm = WindowManagerImpl.getInstance();
239         TopComponent[] tcs = mode.getTopComponents();
240         Map JavaDoc<TopComponent,Integer JavaDoc> res = new HashMap JavaDoc<TopComponent,Integer JavaDoc>( tcs.length );
241         for( TopComponent tc : tcs ) {
242             String JavaDoc tcId = wm.findTopComponentID( tc );
243             Integer JavaDoc size = slideInSizes.get( mode.getSide() + tcId );
244             if( null != size ) {
245                 res.put( tc, size );
246             }
247         }
248         return res;
249     }
250     
251     public void setSlideInSize(String JavaDoc side, TopComponent tc, int size) {
252         if( null != tc && null != side ) {
253             String JavaDoc tcId = WindowManagerImpl.getInstance().findTopComponentID(tc);
254             slideInSizes.put( side+tcId, new Integer JavaDoc(size) );
255         }
256     }
257     
258     public boolean removeMode(ModeImpl mode) {
259         int kind = mode.getKind();
260         if (kind == Constants.MODE_KIND_SLIDING) {
261             return true;
262             // don't remove the sliding modes, to make dnd easier..
263
// slidingSides2Modes.remove(side);
264
// return slidingModes2Sides.remove(mode) != null;
265
}
266         modes.remove(mode);
267         if(mode.getKind() == Constants.MODE_KIND_EDITOR) {
268             return editorSplitSubModel.getEditorArea().removeMode(mode);
269         } else {
270             return editorSplitSubModel.removeMode(mode);
271         }
272     }
273     
274     /** Sets active mode. */
275     public boolean setActiveMode(ModeImpl activeMode) {
276         if(activeMode == null || modes.contains(activeMode)) {
277             this.activeMode = activeMode;
278             return true;
279         }
280         
281         return false;
282     }
283     
284     /** Gets acitve mode. */
285     public ModeImpl getActiveMode() {
286         return this.activeMode;
287     }
288     
289     /** Sets maximized mode for editor components. */
290     public boolean setEditorMaximizedMode(ModeImpl maximizedMode) {
291         if(maximizedMode == null || modes.contains(maximizedMode)) {
292             this.editorMaximizedMode = maximizedMode;
293             return true;
294         }
295         
296         return false;
297     }
298     
299     /** Gets maximized mode for editor components. */
300     public ModeImpl getEditorMaximizedMode() {
301         return this.editorMaximizedMode;
302     }
303     
304     /** Sets maximized mode for non-editor components. */
305     public boolean setViewMaximizedMode(ModeImpl maximizedMode) {
306         if(maximizedMode == null || modes.contains(maximizedMode)) {
307             this.viewMaximizedMode = maximizedMode;
308             return true;
309         }
310         
311         return false;
312     }
313     
314     /** Gets maximized mode for non-editor components. */
315     public ModeImpl getViewMaximizedMode() {
316         return this.viewMaximizedMode;
317     }
318
319     public Set JavaDoc<ModeImpl> getModes() {
320         return new HashSet JavaDoc<ModeImpl>(modes);
321     }
322     
323     public void setSplitWeights( ModelElement[] snapshots, double[] splitWeights ) {
324         editorSplitSubModel.setSplitWeights( snapshots, splitWeights );
325     }
326     
327     public String JavaDoc toString() {
328         return getClass().getName() + "@" + Integer.toHexString(hashCode()) // NOI18N
329
+ "\n" + editorSplitSubModel; // NOI18N
330
}
331
332     /////////////////////////////////////////////
333
// used when creating snapshot of this model.
334
public ModeStructureSnapshot.ElementSnapshot createSplitSnapshot() {
335         return editorSplitSubModel.createSplitSnapshot();
336     }
337
338     /** Set of mode element snapshots. */
339     public Set JavaDoc<ModeStructureSnapshot.ModeSnapshot> createSeparateModeSnapshots() {
340         Set JavaDoc<ModeStructureSnapshot.ModeSnapshot> s =
341                 new HashSet JavaDoc<ModeStructureSnapshot.ModeSnapshot>();
342         
343         s.addAll(editorSplitSubModel.createSeparateSnapshots());
344         
345         return s;
346     }
347     
348     public Set JavaDoc<ModeStructureSnapshot.SlidingModeSnapshot> createSlidingModeSnapshots() {
349         Set JavaDoc<ModeStructureSnapshot.SlidingModeSnapshot> result =
350                 new HashSet JavaDoc<ModeStructureSnapshot.SlidingModeSnapshot>();
351         for (Map.Entry JavaDoc<ModeImpl, String JavaDoc> curEntry: slidingModes2Sides.entrySet()) {
352             result.add(new ModeStructureSnapshot.SlidingModeSnapshot(
353                     curEntry.getKey(), curEntry.getValue(),
354                     getSlideInSizes(curEntry.getKey())));
355         }
356         
357         return result;
358     }
359     
360     ////////////////////////////////////////////
361

362     
363     //////////////////////////////
364
// Controller updates >>
365

366     public ModeImpl getModeForOriginator(ModelElement originator) {
367         ModeImpl mode = editorSplitSubModel.getModeForOriginator(originator);
368         
369         if(modes.contains(mode)) {
370             return mode;
371         } else {
372             return null;
373         }
374     }
375     
376     // Controller updates <<
377
///////////////////////////////
378

379 }
380
381
Popular Tags