KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > platform > 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.apisupport.project.ui.platform;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Image JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Comparator JavaDoc;
28 import javax.swing.AbstractListModel JavaDoc;
29 import javax.swing.ComboBoxModel JavaDoc;
30 import javax.swing.DefaultListCellRenderer JavaDoc;
31 import javax.swing.Icon JavaDoc;
32 import javax.swing.ImageIcon JavaDoc;
33 import javax.swing.JComboBox JavaDoc;
34 import javax.swing.JLabel JavaDoc;
35 import javax.swing.JList JavaDoc;
36 import javax.swing.ListCellRenderer JavaDoc;
37 import javax.swing.plaf.UIResource JavaDoc;
38 import org.netbeans.api.project.libraries.Library;
39 import org.netbeans.api.project.libraries.LibraryManager;
40 import org.openide.util.Utilities;
41 import org.openide.util.WeakListeners;
42
43 /**
44  *
45  * @author Radek Matous
46  */

47 public final class LibrariesModel extends AbstractListModel JavaDoc implements ComboBoxModel JavaDoc, PropertyChangeListener JavaDoc {
48     private Library[] cache;
49     private Object JavaDoc selectedLibrary = null;
50     
51     public static JComboBox JavaDoc getComboBox() {
52         JComboBox JavaDoc librariesComboBox = new JComboBox JavaDoc(new LibrariesModel());
53         librariesComboBox.setRenderer(new LibraryRenderer());
54         return librariesComboBox;
55     }
56     
57     /** Creates a new instance of LibrariesModels */
58     private LibrariesModel() {
59         LibraryManager manager = LibraryManager.getDefault();
60         manager.addPropertyChangeListener((PropertyChangeListener JavaDoc)WeakListeners.create(PropertyChangeListener JavaDoc.class,
61                 this, manager));
62         Library[] libraries = getLibraries();
63         if (libraries != null && libraries.length > 0) {
64             selectedLibrary = libraries[0];
65         }
66         
67     }
68     
69     public synchronized int getSize() {
70         if (this.cache == null) {
71             this.cache = this.createLibraries();
72         }
73         return this.cache.length;
74     }
75     
76     public synchronized Object JavaDoc getElementAt(int index) {
77         if (this.cache == null) {
78             this.cache = this.createLibraries();
79         }
80         if (index >= 0 && index < this.cache.length) {
81             return this.cache[index];
82         } else {
83             return null;
84         }
85     }
86     
87     public synchronized void propertyChange(PropertyChangeEvent JavaDoc evt) {
88         int oldSize = this.cache == null ? 0 : this.cache.length;
89         this.cache = createLibraries();
90         int newSize = this.cache.length;
91         
92         this.fireContentsChanged(this, 0, Math.min(oldSize-1,newSize-1));
93         if (oldSize > newSize) {
94             this.fireIntervalRemoved(this,newSize,oldSize-1);
95         } else if (oldSize < newSize) {
96             this.fireIntervalAdded(this,oldSize,newSize-1);
97         }
98     }
99     
100     public synchronized Library[] getLibraries() {
101         if (this.cache == null) {
102             this.cache = this.createLibraries();
103         }
104         return this.cache;
105     }
106     
107     private Library[] createLibraries() {
108         Library[] libs = LibraryManager.getDefault().getLibraries();
109         Arrays.sort(libs, new Comparator JavaDoc() {
110             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
111                 assert (o1 instanceof Library) && (o2 instanceof Library);
112                 String JavaDoc name1 = ((Library)o1).getDisplayName();
113                 String JavaDoc name2 = ((Library)o2).getDisplayName();
114                 return name1.compareToIgnoreCase(name2);
115             }
116         });
117         return libs;
118     }
119         
120     public void setSelectedItem(Object JavaDoc libarry) {
121         assert libarry == null || libarry instanceof Library;
122         if (selectedLibrary != libarry) {
123             selectedLibrary = libarry;
124             fireContentsChanged(this, -1, -1);
125         }
126     }
127     
128     public Object JavaDoc getSelectedItem() {
129         return selectedLibrary;
130     }
131     
132     private static final class LibraryRenderer extends JLabel JavaDoc implements ListCellRenderer JavaDoc, UIResource JavaDoc {
133         
134         private static final String JavaDoc LIBRARY_ICON = "org/netbeans/modules/apisupport/project/ui/resources/libraries.gif"; //NOI18N
135
private Icon JavaDoc cachedIcon;
136         
137         public LibraryRenderer () {
138             setOpaque(true);
139         }
140         
141         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
142             // #93658: GTK needs name to render cell renderer "natively"
143
setName("ComboBox.listRenderer"); // NOI18N
144

145             String JavaDoc displayName = null;
146             
147             if (value instanceof Library) {
148                 Library lib = ((Library)value);
149                 displayName = lib.getDisplayName();
150             }
151             setText(displayName);
152             setIcon(createIcon());
153             
154             if ( isSelected ) {
155                 setBackground(list.getSelectionBackground());
156                 setForeground(list.getSelectionForeground());
157             }
158             else {
159                 setBackground(list.getBackground());
160                 setForeground(list.getForeground());
161             }
162             
163             return this;
164         }
165         
166         // #93658: GTK needs name to render cell renderer "natively"
167
public String JavaDoc getName() {
168             String JavaDoc name = super.getName();
169             return name == null ? "ComboBox.renderer" : name; // NOI18N
170
}
171         
172         private synchronized Icon JavaDoc createIcon () {
173             if (this.cachedIcon == null) {
174                 Image JavaDoc img = Utilities.loadImage(LIBRARY_ICON);
175                 this.cachedIcon = new ImageIcon JavaDoc (img);
176             }
177             return this.cachedIcon;
178         }
179     }
180 }
181
Popular Tags