KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > libraries > ui > LibrariesModel


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.modules.project.libraries.ui;
21
22
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.*;
26
27
28 import org.netbeans.spi.project.libraries.LibraryImplementation;
29 import org.netbeans.spi.project.libraries.LibraryProvider;
30 import org.netbeans.modules.project.libraries.WritableLibraryProvider;
31 import org.openide.util.Lookup;
32 import org.openide.util.LookupListener;
33 import org.openide.util.LookupEvent;
34 import org.openide.ErrorManager;
35
36 /**
37  *
38  * @author tom
39  */

40 class LibrariesModel extends javax.swing.AbstractListModel JavaDoc implements PropertyChangeListener JavaDoc, LookupListener {
41
42     private List<LibraryImplementation> actualLibraries;
43     private List<LibraryImplementation> addedLibraries;
44     private List<LibraryImplementation> removedLibraries;
45     private List<ProxyLibraryImplementation> changedLibraries;
46     private Collection<? extends LibraryProvider> currentStorages;
47     private Map<LibraryImplementation,LibraryProvider> storageByLib;
48     private Lookup.Result<LibraryProvider> lresult;
49     private WritableLibraryProvider writableProvider;
50
51     /** Creates a new instance of LibrariesModel */
52     public LibrariesModel () {
53         this.addedLibraries = new ArrayList<LibraryImplementation>();
54         this.removedLibraries = new ArrayList<LibraryImplementation>();
55         this.changedLibraries = new ArrayList<ProxyLibraryImplementation>();
56         this.currentStorages = Collections.emptySet();
57         this.storageByLib = new HashMap<LibraryImplementation,LibraryProvider>();
58         this.getLibraries ();
59     }
60     
61     public Object JavaDoc getElementAt(int index) {
62         if (index < 0 || index >= this.actualLibraries.size())
63             return null;
64         return actualLibraries.get(index);
65     }
66     
67     public int getSize() {
68         return this.actualLibraries.size();
69     }
70
71     public void addLibrary (LibraryImplementation impl) {
72         this.addedLibraries.add (impl);
73         int index=0;
74         Comparator<LibraryImplementation> c = new LibrariesComparator();
75         for (; index < this.actualLibraries.size(); index++) {
76             LibraryImplementation tmp = this.actualLibraries.get(index);
77             if (c.compare(impl,tmp)<0)
78                 break;
79         }
80         this.actualLibraries.add (index, impl);
81         this.fireIntervalAdded (this,index,index);
82     }
83
84     public void removeLibrary (LibraryImplementation impl) {
85         if (this.addedLibraries.contains(impl)) {
86             this.addedLibraries.remove(impl);
87         }
88         else {
89             this.removedLibraries.add (((ProxyLibraryImplementation)impl).getOriginal());
90         }
91         int index = this.actualLibraries.indexOf(impl);
92         this.actualLibraries.remove(index);
93         this.fireIntervalRemoved(this,index,index);
94     }
95
96     public void modifyLibrary(ProxyLibraryImplementation impl) {
97         if (!this.addedLibraries.contains (impl) && !this.changedLibraries.contains(impl)) {
98             this.changedLibraries.add(impl);
99         }
100         int index = this.actualLibraries.indexOf (impl);
101         this.fireContentsChanged(this,index,index);
102     }
103
104     public boolean isLibraryEditable (LibraryImplementation impl) {
105         if (this.addedLibraries.contains(impl))
106             return true;
107         LibraryProvider provider = storageByLib.get
108                 (((ProxyLibraryImplementation)impl).getOriginal());
109         //Todo: Currently just one WritableLibraryProvider
110
//Todo: if changed, must be rewritten to handle it
111
return (provider == this.writableProvider);
112     }
113
114     public void apply () throws IOException JavaDoc {
115         //Todo: Currently just one WritableLibraryProvider
116
//Todo: if changed, must be rewritten to handle it
117
for (LibraryImplementation impl : removedLibraries) {
118             LibraryProvider storage = storageByLib.get(impl);
119             if (storage == this.writableProvider) {
120                 this.writableProvider.removeLibrary (impl);
121             }
122             else {
123                 ErrorManager.getDefault().log ("Can not find storage for library: "+impl.getName()); //NOI18N
124
}
125         }
126         if (this.writableProvider != null) {
127             for (LibraryImplementation impl : addedLibraries) {
128                 writableProvider.addLibrary(impl);
129             }
130         }
131         else {
132             ErrorManager.getDefault().log("Cannot add libraries, no WritableLibraryProvider."); //NOI18N
133
}
134         for (ProxyLibraryImplementation proxy : changedLibraries) {
135             LibraryProvider storage = storageByLib.get(proxy.getOriginal());
136             if (storage == this.writableProvider) {
137                 this.writableProvider.updateLibrary (proxy.getOriginal(), proxy);
138             }
139             else {
140                 ErrorManager.getDefault().log ("Can not find storage for library: "+proxy.getOriginal().getName()); //NOI18N
141
}
142         }
143         cleanUp ();
144     }
145
146     public void cancel() {
147         cleanUp ();
148     }
149
150     public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
151         this.storagesChanged();
152     }
153
154     public void resultChanged(LookupEvent ev) {
155         this.storagesChanged();
156     }
157
158     public void storagesChanged () {
159         int oldSize;
160         synchronized (this) {
161             oldSize = this.actualLibraries == null ? 0 : this.actualLibraries.size();
162             getLibraries();
163         }
164         this.fireContentsChanged(this, 0, Math.max(oldSize,this.actualLibraries.size()));
165     }
166
167     private LibraryImplementation findModified (LibraryImplementation impl) {
168         for (Iterator it = changedLibraries.iterator(); it.hasNext();) {
169             ProxyLibraryImplementation proxy = (ProxyLibraryImplementation) it.next();
170             if (proxy.getOriginal().equals (impl)) {
171                 return proxy;
172             }
173         }
174         return null;
175     }
176
177     private synchronized void cleanUp () {
178         this.addedLibraries.clear();
179         this.removedLibraries.clear();
180         this.changedLibraries.clear();
181         for (LibraryProvider p : currentStorages) {
182             p.removePropertyChangeListener (this);
183         }
184         this.currentStorages = Collections.emptySet();
185     }
186
187     private synchronized void getLibraries () {
188         List<LibraryImplementation> libraries = new ArrayList<LibraryImplementation>();
189         if (this.lresult == null) {
190             //First time
191
this.lresult = Lookup.getDefault().lookupResult(LibraryProvider.class);
192             this.lresult.addLookupListener (this);
193         }
194         Collection<? extends LibraryProvider> instances = this.lresult.allInstances();
195         Collection<LibraryProvider> toAdd = new HashSet<LibraryProvider>(instances);
196         toAdd.removeAll(this.currentStorages);
197         Collection<LibraryProvider> toRemove = new HashSet<LibraryProvider>(this.currentStorages);
198         toRemove.removeAll (instances);
199         this.currentStorages = instances;
200         this.storageByLib.clear();
201         for (LibraryProvider storage : instances) {
202             //TODO: in case of more WritableLibraryProvider must be changed
203
if (this.writableProvider == null && storage instanceof WritableLibraryProvider) {
204                 this.writableProvider = (WritableLibraryProvider) storage;
205             }
206             for (LibraryImplementation lib : storage.getLibraries()) {
207                 LibraryImplementation proxy = null;
208                 if (removedLibraries.contains(lib)) {
209                     this.storageByLib.put (lib,storage);
210                 }
211                 else if ((proxy = findModified (lib))!=null) {
212                     libraries.add (proxy);
213                     this.storageByLib.put (lib,storage);
214                 }
215                 else {
216                     libraries.add(new ProxyLibraryImplementation(lib,this));
217                     this.storageByLib.put (lib,storage);
218                 }
219             }
220         }
221         libraries.addAll (this.addedLibraries);
222         Collections.sort(libraries, new LibrariesComparator());
223
224         for (LibraryProvider p : toRemove) {
225             p.removePropertyChangeListener(this);
226         }
227         for (LibraryProvider p : toAdd) {
228             p.addPropertyChangeListener(this);
229         }
230         this.actualLibraries = libraries;
231     }
232
233     private static class LibrariesComparator implements Comparator<LibraryImplementation> {
234         public int compare(LibraryImplementation lib1, LibraryImplementation lib2) {
235             String JavaDoc name1 = LibrariesCustomizer.getLocalizedString(lib1.getLocalizingBundle(), lib1.getName());
236             String JavaDoc name2 = LibrariesCustomizer.getLocalizedString(lib2.getLocalizingBundle(), lib2.getName());
237             return name1.compareToIgnoreCase(name2);
238         }
239     }
240
241 }
242
Popular Tags