KickJava   Java API By Example, From Geeks To Geeks.

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


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.model;
22
23
24 import org.netbeans.core.windows.WindowManagerImpl;
25 import org.openide.windows.TopComponent;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import org.netbeans.core.windows.Constants;
31
32 /**
33  * Model which stored TopComponents in one mode. It manages opened, closed
34  * and selected TopComponent.
35  * This sub model is not thread safe. It is supposed to be just part of DefaultModeModel
36  * which is responsible for the synch.
37  *
38  * @author Peter Zavadsky
39  */

40 final class TopComponentSubModel {
41
42     /** List of opened TopComponents. */
43     private final List JavaDoc<TopComponent> openedTopComponents = new ArrayList JavaDoc<TopComponent>(10);
44     /** List of all TopComponent IDs (both opened and closed). */
45     private final List JavaDoc<String JavaDoc> tcIDs = new ArrayList JavaDoc<String JavaDoc>(10);
46     /** kind of mode model this sub model is part of */
47     private final int kind;
48     /** Selected TopComponent ID. Has to be present in openedTopComponenets. */
49     private String JavaDoc selectedTopComponentID;
50     /** ID of top component that was the selected one before switching to/from maximized mode */
51     private String JavaDoc previousSelectedTopComponentID;
52
53     public TopComponentSubModel(int kind) {
54         this.kind = kind;
55     }
56     
57     public List JavaDoc<TopComponent> getTopComponents() {
58         List JavaDoc<TopComponent> l = new ArrayList JavaDoc<TopComponent>(openedTopComponents);
59         
60         List JavaDoc<String JavaDoc> ids = new ArrayList JavaDoc<String JavaDoc>(tcIDs);
61         List JavaDoc<TopComponent> ll = new ArrayList JavaDoc<TopComponent>(ids.size());
62         for(Iterator JavaDoc<String JavaDoc> it = ids.iterator(); it.hasNext(); ) {
63             String JavaDoc tcID = it.next();
64             TopComponent tc = getTopComponent(tcID);
65             if(tc != null) {
66                 ll.add(tc);
67             } else {
68                 // XXX TopComponent was garbaged, remove its ID.
69
it.remove();
70             }
71         }
72         ll.removeAll(openedTopComponents);
73         l.addAll(ll);
74         
75         return l;
76     }
77     
78     public List JavaDoc<TopComponent> getOpenedTopComponents() {
79         return new ArrayList JavaDoc<TopComponent>(openedTopComponents);
80     }
81
82     public boolean addOpenedTopComponent(TopComponent tc) {
83         if(openedTopComponents.contains(tc)) {
84             return false;
85         }
86
87         String JavaDoc tcID = getID(tc);
88         int index = tcIDs.indexOf(tcID);
89         
90         int position = openedTopComponents.size();
91         if( index >= 0 ) {
92             for( TopComponent otc : openedTopComponents ) {
93                 String JavaDoc otcID = getID(otc);
94                 int openedIndex = tcIDs.indexOf( otcID );
95                 if( openedIndex >= index ) {
96                     position = openedTopComponents.indexOf( otc );
97                     break;
98                 }
99             }
100         }
101         // additional double check if we got the same instance of topcomponent
102
//#39914 + #43401 - no need to remove this one without fixing the inconsistency, it will fail later on TabbedAdapter.
103
TopComponent persTC = getTopComponent(tcID);
104         if (persTC != tc) {
105             String JavaDoc message = "Model in inconsistent state, generated TC ID=" + tcID + " for " + tc.getClass() + ":" + tc.hashCode() + " but" +
106             " that ID is reserved for TC=" + persTC.getClass() + ":" + persTC.hashCode();
107             assert false : message;
108         }
109         //-- end of check..
110
openedTopComponents.add(position, tc);
111         if(!tcIDs.contains(tcID)) {
112             tcIDs.add(tcID);
113         }
114         
115         if(selectedTopComponentID == null && !isNullSelectionAllowed()) {
116             selectedTopComponentID = tcID;
117         }
118         
119         // XXX - should be deleted after TopComponent.isSliding is introduced
120
if (kind == Constants.MODE_KIND_SLIDING) {
121             setSlidingProperty(tc);
122         } else {
123             clearSlidingProperty(tc);
124         }
125         
126         return true;
127     }
128     
129     public boolean insertOpenedTopComponent(TopComponent tc, int index) {
130         if(index >= 0
131         && !openedTopComponents.isEmpty()
132         && openedTopComponents.size() > index
133         && openedTopComponents.get(index) == tc) {
134             return false;
135         }
136         
137         // Remove from previous index
138
openedTopComponents.remove(tc);
139         
140         int position = index;
141         if(position < 0) {
142             position = 0;
143         } else if(position > openedTopComponents.size()) {
144             position = openedTopComponents.size();
145         }
146
147         String JavaDoc tcID = getID(tc);
148         tcIDs.remove(tcID);
149         openedTopComponents.add(position, tc);
150         if(position == 0) {
151             tcIDs.add(0, tcID);
152         } else {
153             TopComponent previous = (TopComponent)openedTopComponents.get(position - 1);
154             int previousIndex = tcIDs.indexOf(getID(previous));
155             tcIDs.add(previousIndex + 1, tcID);
156         }
157         
158         if(selectedTopComponentID == null && !isNullSelectionAllowed()) {
159             selectedTopComponentID = getID(tc);
160         }
161         
162         // XXX - should be deleted after TopComponent.isSliding is introduced
163
if (kind == Constants.MODE_KIND_SLIDING) {
164             setSlidingProperty(tc);
165         } else {
166             clearSlidingProperty(tc);
167         }
168         
169         return true;
170     }
171     
172     public boolean addClosedTopComponent(TopComponent tc) {
173         int index = openedTopComponents.indexOf(tc);
174         String JavaDoc tcID = getID(tc);
175         if (!tcIDs.contains(tcID)) {
176             tcIDs.add(tcID);
177         }
178         if(index != -1) {
179             openedTopComponents.remove(tc);
180             if (selectedTopComponentID != null && selectedTopComponentID.equals(getID(tc))) {
181                 adjustSelectedTopComponent(index);
182             }
183         }
184         
185         
186         // XXX - should be deleted after TopComponent.isSliding is introduced
187
if (kind == Constants.MODE_KIND_SLIDING) {
188             setSlidingProperty(tc);
189         } else {
190             clearSlidingProperty(tc);
191         }
192         
193         return true;
194     }
195     
196     public boolean addUnloadedTopComponent(String JavaDoc tcID) {
197         if(!tcIDs.contains(tcID)) {
198             tcIDs.add(tcID);
199         }
200         
201         return true;
202     }
203     
204     public boolean removeTopComponent(TopComponent tc) {
205         boolean res;
206         String JavaDoc tcID = getID(tc);
207         if(openedTopComponents.contains(tc)) {
208             if(selectedTopComponentID != null && selectedTopComponentID.equals(tcID)) {
209                 int index = openedTopComponents.indexOf(getTopComponent(selectedTopComponentID));
210                 openedTopComponents.remove(tc);
211                 adjustSelectedTopComponent(index);
212             } else {
213                 openedTopComponents.remove(tc);
214             }
215             tcIDs.remove(tcID);
216             
217             res = true;
218         } else if(tcIDs.contains(tcID)) {
219             tcIDs.remove(tcID);
220             res = true;
221         } else {
222             res = false;
223         }
224
225         // XXX - should be deleted after TopComponent.isSliding is introduced
226
clearSlidingProperty(tc);
227         
228         return res;
229     }
230
231     public boolean containsTopComponent(TopComponent tc) {
232         return openedTopComponents.contains(tc) || tcIDs.contains(getID(tc));
233     }
234     
235     public boolean isEmpty() {
236         return tcIDs.isEmpty();
237     }
238     
239     private void adjustSelectedTopComponent(int index) {
240         if(openedTopComponents.isEmpty() || isNullSelectionAllowed()) {
241             selectedTopComponentID = null;
242             return;
243         }
244         
245         if(index > openedTopComponents.size() - 1) {
246             index = openedTopComponents.size() - 1;
247         }
248         
249         selectedTopComponentID = getID((TopComponent)openedTopComponents.get(index));
250     }
251
252     /** @return true for sliding kind of model, false otherwise. It means that
253      * null selection is valid only in sliding kind of model.
254      */

255     private boolean isNullSelectionAllowed() {
256         return kind == Constants.MODE_KIND_SLIDING;
257     }
258
259     /** Sets selected component. Note that for sliding kind null selection
260      * is allowed
261      */

262     public void setSelectedTopComponent(TopComponent tc) {
263         if(tc != null && !openedTopComponents.contains(tc)) {
264             return;
265         }
266         
267         if (tc == null && isNullSelectionAllowed()) {
268             selectedTopComponentID = null;
269         } else {
270             selectedTopComponentID = getID(tc);
271         }
272     }
273     
274     public void setPreviousSelectedTopComponent(TopComponent tc) {
275         if(tc != null )
276             previousSelectedTopComponentID = getID(tc);
277         else
278             previousSelectedTopComponentID = null;
279     }
280     
281     public void setUnloadedSelectedTopComponent(String JavaDoc tcID) {
282         if(tcID != null && !getOpenedTopComponentsIDs().contains(tcID)) {
283             return;
284         }
285         
286         selectedTopComponentID = tcID;
287     }
288     
289     public void setUnloadedPreviousSelectedTopComponent(String JavaDoc tcID) {
290         previousSelectedTopComponentID = tcID;
291     }
292     
293     public List JavaDoc<String JavaDoc> getOpenedTopComponentsIDs() {
294         List JavaDoc<String JavaDoc> l = new ArrayList JavaDoc<String JavaDoc>(openedTopComponents.size());
295         for(Iterator JavaDoc<TopComponent> it = openedTopComponents.iterator(); it.hasNext(); ) {
296             l.add(getID(it.next()));
297         }
298         return l;
299     }
300     
301     // XXX
302
public List JavaDoc<String JavaDoc> getClosedTopComponentsIDs() {
303         List JavaDoc<String JavaDoc> closedIDs = new ArrayList JavaDoc<String JavaDoc>(tcIDs);
304         closedIDs.removeAll(getOpenedTopComponentsIDs());
305         return closedIDs;
306     }
307
308     // XXX
309
public List JavaDoc<String JavaDoc> getTopComponentsIDs() {
310         return new ArrayList JavaDoc<String JavaDoc>(tcIDs);
311     }
312     
313     // XXX
314
public void removeClosedTopComponentID(String JavaDoc tcID) {
315         tcIDs.remove(tcID);
316     }
317     
318     public TopComponent getSelectedTopComponent() {
319         return getTopComponent(selectedTopComponentID);
320     }
321     
322     public TopComponent getPreviousSelectedTopComponent() {
323         if( null != previousSelectedTopComponentID )
324             return getTopComponent(previousSelectedTopComponentID);
325         return null;
326     }
327
328     private static TopComponent getTopComponent(String JavaDoc tcID) {
329         return WindowManagerImpl.getInstance().getTopComponentForID(tcID);
330     }
331     
332     private static String JavaDoc getID(TopComponent tc) {
333         return WindowManagerImpl.getInstance().findTopComponentID(tc);
334     }
335
336     
337     // XXX - should be deleted after TopComponent.isSliding is introduced
338
private static final String JavaDoc IS_SLIDING = "isSliding";
339     
340     // XXX - should be deleted after TopComponent.isSliding is introduced
341
private void setSlidingProperty(TopComponent tc) {
342         tc.putClientProperty(IS_SLIDING, Boolean.TRUE);
343     }
344
345     // XXX - should be deleted after TopComponent.isSliding is introduced
346
private void clearSlidingProperty(TopComponent tc) {
347         tc.putClientProperty(IS_SLIDING, null);
348     }
349     
350 }
351
Popular Tags