KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > catalog > CatalogMounterModel


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 package org.netbeans.modules.xml.catalog;
20
21 import java.beans.*;
22 import java.net.*;
23 import java.util.*;
24
25 import javax.swing.*;
26 import javax.swing.event.*;
27
28 import org.openide.util.Utilities;
29
30 import org.netbeans.modules.xml.catalog.spi.*;
31
32 /**
33  * Data holder driving CatalogMounterPanel.
34  * Selected getCatalog() points to a catalog that have been choosen and customized
35  * by a user.
36  *
37  * @author Petr Kuzel
38  * @version
39  */

40 final class CatalogMounterModel extends Object JavaDoc {
41
42     private Object JavaDoc catalog = null; // selected & customized catalog instance
43

44     private ComboBoxModel cxModel = null; // model containig CatalogMounterModel.Entries
45

46     private List changeListeners = new ArrayList(2);
47         
48     
49     /** Creates new CatalogMounterModel */
50     public CatalogMounterModel(Iterator providers) {
51         
52         Vector providersList = new Vector();
53         while (providers.hasNext()) {
54             providersList.add(new Entry((Class JavaDoc)providers.next()));
55         }
56         
57         cxModel = new DefaultComboBoxModel(providersList);
58         cxModel.addListDataListener(new Lis());
59         initCatalog();
60     }
61                     
62     /**
63      * Currently selected & customized catalog instance.
64      * (may return null if no provider available)
65      */

66     public Object JavaDoc getCatalog() {
67         return catalog;
68     }
69
70     /**
71      * Customizer class of current catalog.
72      * @return Customizer instance it needs to be initialized by
73      * setObject(getCatalog()); (may return null if no provider available)
74      */

75     public Customizer getCatalogCustomizer() {
76         if (catalog == null) return null;
77         return org.netbeans.modules.xml.catalog.lib.Util.getProviderCustomizer(catalog.getClass());
78     }
79  
80     /**
81      * Form visualizing this model have to use this method for
82      * obtaining model for Comboboxes or lists.
83      */

84     public ComboBoxModel getCatalogComboBoxModel() {
85         return cxModel;
86     }
87
88     /**
89      * Anyone listen on our state, it fires in the add order.
90      */

91     public void addChangeListener(ChangeListener l) {
92         changeListeners.add(l);
93     }
94
95     
96     public void removeChangeListener(ChangeListener l) {
97         changeListeners.remove(l);
98     }
99     
100     // ~~~~~~~~~~~~~~~~~~~~~~ IMPL ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
101

102     private Entry getSelectedEntry() {
103         return (Entry) cxModel.getSelectedItem();
104     }
105
106     /** Set selected calatog instance to new uncustomized catalog. */
107     private void initCatalog() {
108
109         Entry entry = getSelectedEntry();
110         if (entry == null) {
111             catalog = null;
112         } else {
113             catalog = org.netbeans.modules.xml.catalog.lib.Util.createProvider(entry.src);
114         }
115         
116         fireStateChanged();
117     }
118
119     private void fireStateChanged() {
120
121         for (Iterator it = changeListeners.iterator(); it.hasNext();) {
122             ChangeListener next = (ChangeListener) it.next();
123             next.stateChanged(new ChangeEvent(this));
124         }
125     }
126     
127     /**
128      * Wrapper class for ComboModel members redefinig toSting() method.
129      */

130     private class Entry {
131         
132         String JavaDoc name = null;
133         Class JavaDoc src;
134         
135         public Entry(Class JavaDoc src) {
136             this.src = src;
137             try {
138                 name = Utilities.getBeanInfo(src).getBeanDescriptor().getDisplayName();
139             } catch (IntrospectionException ex) {
140                 name = src.toString();
141             }
142         }
143         
144         public String JavaDoc toString() {
145             return name;
146         }
147     }
148
149     
150     /**
151      * Listen on combo model and update selected catalog instance.
152      * Implementation calls CatalogMounterModel initCatalog() on selection
153      * change.
154      */

155     private class Lis implements ListDataListener {
156         
157         public void contentsChanged(ListDataEvent e) {
158             initCatalog();
159         }
160                 
161         public void intervalAdded(ListDataEvent e) {
162             initCatalog();
163         }
164                 
165         public void intervalRemoved(ListDataEvent e) {
166             initCatalog();
167         }
168     }
169 }
170
Popular Tags