1 19 20 package org.netbeans.modules.apisupport.project.ui.platform; 21 22 import java.awt.Component ; 23 import java.awt.Image ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.util.Arrays ; 27 import java.util.Comparator ; 28 import javax.swing.AbstractListModel ; 29 import javax.swing.ComboBoxModel ; 30 import javax.swing.DefaultListCellRenderer ; 31 import javax.swing.Icon ; 32 import javax.swing.ImageIcon ; 33 import javax.swing.JComboBox ; 34 import javax.swing.JLabel ; 35 import javax.swing.JList ; 36 import javax.swing.ListCellRenderer ; 37 import javax.swing.plaf.UIResource ; 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 47 public final class LibrariesModel extends AbstractListModel implements ComboBoxModel , PropertyChangeListener { 48 private Library[] cache; 49 private Object selectedLibrary = null; 50 51 public static JComboBox getComboBox() { 52 JComboBox librariesComboBox = new JComboBox (new LibrariesModel()); 53 librariesComboBox.setRenderer(new LibraryRenderer()); 54 return librariesComboBox; 55 } 56 57 58 private LibrariesModel() { 59 LibraryManager manager = LibraryManager.getDefault(); 60 manager.addPropertyChangeListener((PropertyChangeListener )WeakListeners.create(PropertyChangeListener .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 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 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 () { 110 public int compare(Object o1, Object o2) { 111 assert (o1 instanceof Library) && (o2 instanceof Library); 112 String name1 = ((Library)o1).getDisplayName(); 113 String name2 = ((Library)o2).getDisplayName(); 114 return name1.compareToIgnoreCase(name2); 115 } 116 }); 117 return libs; 118 } 119 120 public void setSelectedItem(Object 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 getSelectedItem() { 129 return selectedLibrary; 130 } 131 132 private static final class LibraryRenderer extends JLabel implements ListCellRenderer , UIResource { 133 134 private static final String LIBRARY_ICON = "org/netbeans/modules/apisupport/project/ui/resources/libraries.gif"; private Icon cachedIcon; 136 137 public LibraryRenderer () { 138 setOpaque(true); 139 } 140 141 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 142 setName("ComboBox.listRenderer"); 145 String 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 public String getName() { 168 String name = super.getName(); 169 return name == null ? "ComboBox.renderer" : name; } 171 172 private synchronized Icon createIcon () { 173 if (this.cachedIcon == null) { 174 Image img = Utilities.loadImage(LIBRARY_ICON); 175 this.cachedIcon = new ImageIcon (img); 176 } 177 return this.cachedIcon; 178 } 179 } 180 } 181 | Popular Tags |