KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > multiview > MultiViewModel


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.multiview;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.TreeMap JavaDoc;
32 import javax.swing.ButtonGroup JavaDoc;
33 import javax.swing.ButtonModel JavaDoc;
34 import org.netbeans.core.api.multiview.MultiViewPerspective;
35 import org.netbeans.core.spi.multiview.MultiViewDescription;
36 import org.netbeans.core.spi.multiview.MultiViewElement;
37 import org.netbeans.core.spi.multiview.MultiViewElementCallback;
38
39 import org.openide.windows.TopComponent;
40
41 /**
42  * Model handling maintainance of descriptions, creation of elements and selection
43  * of active items.
44  * @author Milos Kleint
45  */

46
47 class MultiViewModel {
48     
49     private MultiViewDescription currentEditor;
50     private Map JavaDoc nestedElements; //key=description, value null or multiviewelement
51
private Map JavaDoc nestedCallbacks; //key=element, value null or the MultiViewElementCallback that it's used by this element.
52
private Map JavaDoc nestedPerspectives; //key=description, value perspective
53
// private Map nestedPerspectiveComponents; //key=description, value mull or perspectiveComponent
54
private MultiViewDescription[] descriptions;
55     private ButtonGroup JavaDoc group;
56     private Collection JavaDoc shownElements;
57     private ArrayList JavaDoc listeners;
58     private ActionRequestObserverFactory observerFactory;
59     
60     MultiViewModel(MultiViewDescription[] descs, MultiViewDescription defaultDescr,
61                    MultiViewModel.ActionRequestObserverFactory factory) {
62         this(descs, defaultDescr, factory, Collections.EMPTY_MAP);
63     }
64     
65     /**
66      * constructor used at deserialization...
67      */

68     MultiViewModel(MultiViewDescription[] descs, MultiViewDescription defaultDescr,
69                    MultiViewModel.ActionRequestObserverFactory factory, Map JavaDoc existingElements) {
70         observerFactory = factory;
71         nestedElements = new HashMap JavaDoc();
72 // nestedPerspectiveComponents = new HashMap();
73
nestedPerspectives = new HashMap JavaDoc();
74         nestedCallbacks = new HashMap JavaDoc();
75         shownElements = new HashSet JavaDoc(descs.length + 3);
76         descriptions = descs;
77         this.group = group;
78         for (int i = 0; i < descriptions.length; i++) {
79             MultiViewElement element = (MultiViewElement)existingElements.get(descriptions[i]);
80             nestedElements.put(descriptions[i], element);
81             nestedPerspectives.put(descriptions[i], Accessor.DEFAULT.createPerspective(descriptions[i]));
82             if (element != null) {
83                 // set the observer..
84
MultiViewElementCallback call = factory.createElementCallback(descriptions[i]);
85                 nestedCallbacks.put(element, call);
86                 element.setMultiViewCallback(call);
87 // nestedPerspectiveComponents.put(descriptions[i], Accessor.DEFAULT.createPersComponent(element));
88
}
89         }
90         currentEditor = (defaultDescr == null || !nestedElements.containsKey(defaultDescr) ? descriptions[0] : defaultDescr);
91         group = new BtnGroup();
92     }
93     
94     
95     void setActiveDescription(MultiViewDescription description) {
96         if (currentEditor == description) return;
97         MultiViewDescription old = currentEditor;
98         currentEditor = description;
99         fireSelectionChanged(old, description);
100     }
101     
102     MultiViewDescription getActiveDescription() {
103         return currentEditor;
104     }
105     
106     MultiViewElement getActiveElement() {
107         return getActiveElement(true);
108     }
109     
110     MultiViewElement getActiveElement(boolean createIfNotCreatedYet) {
111         return getElementForDescription(currentEditor, createIfNotCreatedYet);
112     }
113     
114     /**
115      * returns all elements that were so far created/instantiated.
116      */

117     synchronized Collection JavaDoc getCreatedElements() {
118        Collection JavaDoc col = new ArrayList JavaDoc(nestedElements.size());
119        Iterator JavaDoc it = nestedElements.entrySet().iterator();
120        while (it.hasNext()) {
121            Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
122            if (entry.getValue() != null) {
123                col.add(entry.getValue());
124            }
125        }
126        return col;
127     }
128     
129     /**
130      * keeps track of already shown elements, so that componentOpened() is not called multiple Times on a single element.
131      *
132      */

133     boolean wasShownBefore(MultiViewElement element) {
134         return shownElements.contains(element);
135     }
136     /**
137      * mars the compoment as shown before, meaning componentOpened() was called on it.
138      */

139     void markAsShown(MultiViewElement element) {
140         shownElements.add(element);
141     }
142
143     /**
144      * mars the compoment as currently hidden, meaning componentClosed() was called on it.
145      */

146     
147     void markAsHidden(MultiViewElement element) {
148         shownElements.remove(element);
149     }
150     
151     /**
152      * returns a list of current MultiViewDescriptions.
153      */

154     MultiViewDescription[] getDescriptions() {
155         return descriptions;
156     }
157     
158     MultiViewPerspective[] getPerspectives() {
159         MultiViewPerspective[] toReturn = new MultiViewPerspective[descriptions.length];
160         for (int i = 0; i < descriptions.length; i++) {
161             toReturn[i] = (MultiViewPerspective)nestedPerspectives.get(descriptions[i]);
162         }
163         return toReturn;
164     }
165     
166     MultiViewPerspective getSelectedPerspective() {
167         return (MultiViewPerspective)nestedPerspectives.get(getActiveDescription());
168     }
169     
170 // MultiViewPerspectiveComponent getMVComponentForDescription(MultiViewDescription desc) {
171
// return (MultiViewPerspectiveComponent)nestedPerspectiveComponents.get(desc);
172
// }
173

174     /**
175      * The button group where the togglebuttons for the descriptions are put into.
176      */

177     ButtonGroup JavaDoc getButtonGroup() {
178         return group;
179     }
180     
181     MultiViewElement getElementForDescription(MultiViewDescription description) {
182         return getElementForDescription(description, true);
183     }
184
185     /**
186      * used primarily at deserialization time.
187      */

188      synchronized MultiViewElement getElementForDescription(MultiViewDescription description, boolean create) {
189         MultiViewElement element = (MultiViewElement)nestedElements.get(description);
190         if (element == null && create) {
191             element = description.createElement();
192             MultiViewElementCallback call = observerFactory.createElementCallback(description);
193             nestedCallbacks.put(element, call);
194             element.setMultiViewCallback(call);
195             nestedElements.put(description, element);
196  // nestedPerspectiveComponents.put(description, Accessor.DEFAULT.createPersComponent(element));
197
}
198         return element;
199     }
200      
201      synchronized MultiViewElementCallback getCallbackForElement(MultiViewElement elem) {
202          return (MultiViewElementCallback)nestedCallbacks.get(elem);
203      }
204     
205     
206     void addElementSelectionListener(ElementSelectionListener listener) {
207         if (listeners == null) {
208             listeners = new ArrayList JavaDoc();
209         }
210         synchronized (listeners) {
211             listeners.add(listener);
212         }
213     }
214     
215     void removeElementSelectionListener(ElementSelectionListener listener) {
216         if (listeners == null) {
217             listeners = new ArrayList JavaDoc();
218         }
219         synchronized (listeners) {
220             listeners.remove(listener);
221         }
222     }
223     
224     private void fireSelectionChanged(MultiViewDescription oldOne, MultiViewDescription newOne) {
225         if (listeners != null) {
226             synchronized (listeners) {
227                 Iterator JavaDoc it = listeners.iterator();
228                 while (it.hasNext()) {
229                     ElementSelectionListener list = (ElementSelectionListener)it.next();
230                     list.selectionChanged(oldOne, newOne);
231                 }
232             }
233         }
234     }
235     
236     void fireActivateCurrent() {
237         if (listeners != null) {
238             synchronized (listeners) {
239                 Iterator JavaDoc it = listeners.iterator();
240                 while (it.hasNext()) {
241                     ElementSelectionListener list = (ElementSelectionListener)it.next();
242                     list.selectionActivatedByButton();
243                 }
244             }
245         }
246     }
247
248     public String JavaDoc toString() {
249         return "current=" + currentEditor; // NOI18N
250
}
251
252     /**
253      * listener for changes in model's selected element.
254      */

255     static interface ElementSelectionListener {
256         
257         public void selectionChanged(MultiViewDescription oldOne, MultiViewDescription newOne);
258         
259         public void selectionActivatedByButton();
260     }
261     
262     /**
263      * Interface for creating the observers that are passed to newly created elements.
264      */

265     static interface ActionRequestObserverFactory {
266         MultiViewElementCallback createElementCallback(MultiViewDescription desc);
267     }
268     
269     /**
270      * handles selection of active element among the tgglebuttons.. more straightforward then adding listeners.
271      */

272     
273     private class BtnGroup extends ButtonGroup JavaDoc {
274         
275         public void setSelected(ButtonModel JavaDoc m, boolean b) {
276             super.setSelected(m, b);
277             if (getSelection() instanceof TabsComponent.TabsButtonModel) {
278                 TabsComponent.TabsButtonModel mod = (TabsComponent.TabsButtonModel)m;
279                 MultiViewDescription desc = mod.getButtonsDescription();
280                 setActiveDescription(desc);
281             }
282         }
283         
284     }
285     
286     
287 }
Popular Tags