KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import org.netbeans.core.windows.WindowManagerImpl;
24 import org.openide.windows.TopComponent;
25
26 import java.util.Collection JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Set JavaDoc;
30
31
32 /**
33  *
34  * @author Peter Zavadsky
35  */

36 final class DefaultTopComponentGroupModel implements TopComponentGroupModel {
37
38     /** Programatic name of group. */
39     private final String JavaDoc name;
40
41     /** The opening state of this group */
42     private boolean opened;
43
44     /** All TopComponent IDs belonging to this group. */
45     private final Set JavaDoc<String JavaDoc> topComponents = new HashSet JavaDoc<String JavaDoc>(3);
46     // XXX Helper
47
/** All TopComponent IDs which were opened by this group (at the moment
48      * when group was opening). When group is closed this set should be emtpy. */

49     private final Set JavaDoc<String JavaDoc> openedTopComponents = new HashSet JavaDoc<String JavaDoc>(3);
50     
51     /** All TopComponent IDs which were already opened before this group was
52      * opened (at the moment when group was opening). When group is closed this
53      * set should be emtpy. */

54     private final Set JavaDoc<String JavaDoc> openedBeforeTopComponents = new HashSet JavaDoc<String JavaDoc>(3);
55     
56     /** TopComponent IDs with opening flag. */
57     private final Set JavaDoc<String JavaDoc> openingTopComponents = new HashSet JavaDoc<String JavaDoc>(3);
58     /** TopComponent IDs with closing flag. */
59     private final Set JavaDoc<String JavaDoc> closingTopComponents = new HashSet JavaDoc<String JavaDoc>(3);
60     
61     private final Object JavaDoc LOCK_OPENED = new Object JavaDoc();
62     
63     private final Object JavaDoc LOCK_TOPCOMPONENTS = new Object JavaDoc();
64
65     
66     public DefaultTopComponentGroupModel(String JavaDoc name, boolean opened) {
67         this.name = name;
68         this.opened = opened;
69     }
70     
71     
72     public String JavaDoc getName() {
73         return name;
74     }
75     
76     public void open(
77             Collection JavaDoc<TopComponent> openedTopComponents,
78             Collection JavaDoc<TopComponent> openedBeforeTopComponents) {
79         synchronized(LOCK_OPENED) {
80             this.opened = true;
81             this.openedTopComponents.clear();
82             for(TopComponent tc: openedTopComponents) {
83                 String JavaDoc tcID = getID(tc);
84                 if(tcID != null) {
85                     this.openedTopComponents.add(tcID);
86                 }
87             }
88             this.openedBeforeTopComponents.clear();
89             for(TopComponent tc: openedBeforeTopComponents) {
90                 String JavaDoc tcID = getID(tc);
91                 if(tcID != null) {
92                     this.openedBeforeTopComponents.add(tcID);
93                 }
94             }
95         }
96     }
97     
98     public void close() {
99         synchronized(LOCK_OPENED) {
100             this.opened = false;
101             this.openedTopComponents.clear();
102             this.openedBeforeTopComponents.clear();
103         }
104     }
105     
106     public boolean isOpened() {
107         synchronized(LOCK_OPENED) {
108             return this.opened;
109         }
110     }
111     
112     public Set JavaDoc<TopComponent> getTopComponents() {
113         Set JavaDoc<String JavaDoc> s;
114         synchronized(LOCK_TOPCOMPONENTS) {
115             s = new HashSet JavaDoc<String JavaDoc>(topComponents);
116         }
117         
118         Set JavaDoc<TopComponent> result = new HashSet JavaDoc<TopComponent>(s.size());
119         for(String JavaDoc tcId: s) {
120             TopComponent tc = getTopComponent(tcId);
121             if(tc != null) {
122                 result.add(tc);
123             }
124         }
125         
126         return result;
127     }
128     
129     public Set JavaDoc<TopComponent> getOpenedTopComponents() {
130         Set JavaDoc<String JavaDoc> s;
131         synchronized(LOCK_OPENED) {
132             s = new HashSet JavaDoc<String JavaDoc>(openedTopComponents);
133         }
134         
135         Set JavaDoc<TopComponent> result = new HashSet JavaDoc<TopComponent>(s.size());
136         for(String JavaDoc tcId: s) {
137             TopComponent tc = getTopComponent(tcId);
138             if(tc != null) {
139                 result.add(tc);
140             }
141         }
142         
143         return result;
144     }
145     
146     public Set JavaDoc<TopComponent> getOpenedBeforeTopComponents() {
147         Set JavaDoc<String JavaDoc> s;
148         synchronized(LOCK_OPENED) {
149             s = new HashSet JavaDoc<String JavaDoc>(openedBeforeTopComponents);
150         }
151         
152         Set JavaDoc<TopComponent> result = new HashSet JavaDoc<TopComponent>(s.size());
153         for(String JavaDoc tcId: s) {
154             TopComponent tc = getTopComponent(tcId);
155             if(tc != null) {
156                 result.add(tc);
157             }
158         }
159         
160         return result;
161     }
162     
163     public Set JavaDoc<TopComponent> getOpeningTopComponents() {
164         Set JavaDoc<String JavaDoc> s;
165         synchronized(LOCK_TOPCOMPONENTS) {
166             s = new HashSet JavaDoc<String JavaDoc>(openingTopComponents);
167         }
168         
169         Set JavaDoc<TopComponent> result = new HashSet JavaDoc<TopComponent>(s.size());
170         for(String JavaDoc tcId: s) {
171             TopComponent tc = getTopComponent(tcId);
172             if(tc != null) {
173                 result.add(tc);
174             }
175         }
176         
177         return result;
178     }
179     
180     public Set JavaDoc<TopComponent> getClosingTopComponents() {
181         Set JavaDoc<String JavaDoc> s;
182         synchronized(LOCK_TOPCOMPONENTS) {
183             s = new HashSet JavaDoc<String JavaDoc>(closingTopComponents);
184         }
185         
186         Set JavaDoc<TopComponent> result = new HashSet JavaDoc<TopComponent>(s.size());
187         for(String JavaDoc tcId: s) {
188             TopComponent tc = getTopComponent(tcId);
189             if(tc != null) {
190                 result.add(tc);
191             }
192         }
193         
194         return result;
195     }
196
197     public boolean addUnloadedTopComponent(String JavaDoc tcID) {
198         synchronized(LOCK_TOPCOMPONENTS) {
199             return topComponents.add(tcID);
200         }
201     }
202     
203     public boolean removeUnloadedTopComponent(String JavaDoc tcID) {
204         synchronized(LOCK_TOPCOMPONENTS) {
205             if(openingTopComponents.contains(tcID)) {
206                 openingTopComponents.remove(tcID);
207             }
208             if(closingTopComponents.contains(tcID)) {
209                 closingTopComponents.remove(tcID);
210             }
211             return topComponents.remove(tcID);
212         }
213     }
214     
215     public boolean addOpeningTopComponent(TopComponent tc) {
216         return addUnloadedOpeningTopComponent(getID(tc));
217     }
218     
219     public boolean addUnloadedOpeningTopComponent(String JavaDoc tcID) {
220         synchronized(LOCK_TOPCOMPONENTS) {
221             if(!topComponents.contains(tcID)) {
222                 topComponents.add(tcID);
223             }
224             return openingTopComponents.add(tcID);
225         }
226     }
227     
228     public boolean removeOpeningTopComponent(TopComponent tc) {
229         return removeUnloadedOpeningTopComponent(getID(tc));
230     }
231     
232     public boolean removeUnloadedOpeningTopComponent(String JavaDoc tcID) {
233         synchronized(LOCK_TOPCOMPONENTS) {
234             return openingTopComponents.remove(tcID);
235         }
236     }
237     
238     public boolean addUnloadedClosingTopComponent(String JavaDoc tcID) {
239         synchronized(LOCK_TOPCOMPONENTS) {
240             if(!topComponents.contains(tcID)) {
241                 topComponents.add(tcID);
242             }
243             return closingTopComponents.add(tcID);
244         }
245     }
246     
247     public boolean removeUnloadedClosingTopComponent(String JavaDoc tcID) {
248         synchronized(LOCK_TOPCOMPONENTS) {
249             return closingTopComponents.remove(tcID);
250         }
251     }
252     
253     // XXX
254
public boolean addUnloadedOpenedTopComponent(String JavaDoc tcID) {
255         synchronized(LOCK_OPENED) {
256             if(!this.opened) {
257                 return false;
258             }
259             this.openedTopComponents.add(tcID);
260         }
261         return true;
262     }
263
264     private static TopComponent getTopComponent(String JavaDoc tcID) {
265         return WindowManagerImpl.getInstance().getTopComponentForID(tcID);
266     }
267     
268     private static String JavaDoc getID(TopComponent tc) {
269         return WindowManagerImpl.getInstance().findTopComponentID(tc);
270     }
271
272     
273     // XXX>>
274
public Set JavaDoc<String JavaDoc> getTopComponentsIDs() {
275         synchronized(LOCK_TOPCOMPONENTS) {
276             return new HashSet JavaDoc<String JavaDoc>(topComponents);
277         }
278     }
279     
280     public Set JavaDoc<String JavaDoc> getOpeningSetIDs() {
281         synchronized(LOCK_TOPCOMPONENTS) {
282             return new HashSet JavaDoc<String JavaDoc>(openingTopComponents);
283         }
284     }
285     public Set JavaDoc<String JavaDoc> getClosingSetIDs() {
286         synchronized(LOCK_TOPCOMPONENTS) {
287             return new HashSet JavaDoc<String JavaDoc>(closingTopComponents);
288         }
289     }
290     public Set JavaDoc<String JavaDoc> getOpenedTopComponentsIDs() {
291         synchronized(LOCK_TOPCOMPONENTS) {
292             return new HashSet JavaDoc<String JavaDoc>(openedTopComponents);
293         }
294     }
295     // XXX<<
296

297 }
298
299
Popular Tags