2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.apisupport.project.ui.platform;
44
import java.awt.Component;
45
import java.awt.Image;
46
import java.beans.PropertyChangeEvent;
47
import java.beans.PropertyChangeListener;
48
import java.util.Arrays;
49
import java.util.Comparator;
50
import javax.swing.AbstractListModel;
51
import javax.swing.ComboBoxModel;
52
import javax.swing.Icon;
53
import javax.swing.ImageIcon;
54
import javax.swing.JComboBox;
55
import javax.swing.JLabel;
56
import javax.swing.JList;
57
import javax.swing.ListCellRenderer;
58
import javax.swing.plaf.UIResource;
59
import org.netbeans.api.project.libraries.Library;
60
import org.netbeans.api.project.libraries.LibraryManager;
61
import org.openide.util.Utilities;
62
import org.openide.util.WeakListeners;
66
* @author Radek Matous
68
public final class LibrariesModel extends AbstractListModel implements ComboBoxModel, PropertyChangeListener {
69
private Library[] cache;
70
private Object selectedLibrary = null;
72
public static JComboBox getComboBox() {
73
JComboBox librariesComboBox = new JComboBox(new LibrariesModel());
74
librariesComboBox.setRenderer(new LibraryRenderer());
75
return librariesComboBox;
78
/** Creates a new instance of LibrariesModels */
79
private LibrariesModel() {
80
LibraryManager manager = LibraryManager.getDefault();
81
manager.addPropertyChangeListener(WeakListeners.create(PropertyChangeListener.class, this, manager));
82
Library[] libraries = getLibraries();
83
if (libraries != null && libraries.length > 0) {
84
selectedLibrary = libraries[0];
89
public synchronized int getSize() {
90
if (this.cache == null) {
91
this.cache = this.createLibraries();
93
return this.cache.length;
96
public synchronized Object getElementAt(int index) {
97
if (this.cache == null) {
98
this.cache = this.createLibraries();
100
if (index >= 0 && index < this.cache.length) {
101
return this.cache[index];
107
public synchronized void propertyChange(PropertyChangeEvent evt) {
108
int oldSize = this.cache == null ? 0 : this.cache.length;
109
this.cache = createLibraries();
110
int newSize = this.cache.length;
112
this.fireContentsChanged(this, 0, Math.min(oldSize-1,newSize-1));
113
if (oldSize > newSize) {
114
this.fireIntervalRemoved(this,newSize,oldSize-1);
115
} else if (oldSize < newSize) {
116
this.fireIntervalAdded(this,oldSize,newSize-1);
120
public synchronized Library[] getLibraries() {
121
if (this.cache == null) {
122
this.cache = this.createLibraries();
127
private Library[] createLibraries() {
128
Library[] libs = LibraryManager.getDefault().getLibraries();
129
Arrays.sort(libs, new Comparator<Library>() {
130
public int compare(Library l1, Library l2) {
131
return l1.getDisplayName().compareToIgnoreCase(l2.getDisplayName());
137
public void setSelectedItem(Object libarry) {
138
assert libarry == null || libarry instanceof Library;
139
if (selectedLibrary != libarry) {
140
selectedLibrary = libarry;
141
fireContentsChanged(this, -1, -1);
145
public Object getSelectedItem() {
146
return selectedLibrary;
149
private static final class LibraryRenderer extends JLabel implements ListCellRenderer, UIResource {
151
private static final String LIBRARY_ICON = "org/netbeans/modules/apisupport/project/ui/resources/libraries.gif"; //NOI18N
152
private Icon cachedIcon;
154
public LibraryRenderer () {
158
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
159
// #93658: GTK needs name to render cell renderer "natively"
160
setName("ComboBox.listRenderer"); // NOI18N
162
String displayName = null;
164
if (value instanceof Library) {
165
Library lib = ((Library)value);
166
displayName = lib.getDisplayName();
168
setText(displayName);
169
setIcon(createIcon());
172
setBackground(list.getSelectionBackground());
173
setForeground(list.getSelectionForeground());
176
setBackground(list.getBackground());
177
setForeground(list.getForeground());
183
// #93658: GTK needs name to render cell renderer "natively"
184
public String getName() {
185
String name = super.getName();
186
return name == null ? "ComboBox.renderer" : name; // NOI18N
189
private synchronized Icon createIcon () {
190
if (this.cachedIcon == null) {
191
Image img = Utilities.loadImage(LIBRARY_ICON);
192
this.cachedIcon = new ImageIcon (img);
194
return this.cachedIcon;