1 19 20 package org.netbeans.modules.apisupport.project.ui.customizer; 21 22 import java.awt.Component ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.text.Collator ; 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.Comparator ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.SortedSet ; 32 import java.util.TreeSet ; 33 import javax.swing.ComboBoxModel ; 34 import javax.swing.DefaultListCellRenderer ; 35 import javax.swing.JLabel ; 36 import javax.swing.JList ; 37 import javax.swing.ListCellRenderer ; 38 import javax.swing.event.ListDataEvent ; 39 import javax.swing.event.ListDataListener ; 40 import javax.swing.plaf.UIResource ; 41 import org.netbeans.api.java.platform.JavaPlatform; 42 import org.netbeans.api.java.platform.JavaPlatformManager; 43 import org.openide.util.WeakListeners; 44 45 49 public class JavaPlatformComponentFactory { 50 51 private JavaPlatformComponentFactory() {} 52 53 public static ComboBoxModel javaPlatformListModel() { 54 return new Model (); 55 } 56 57 public static ListCellRenderer javaPlatformListCellRenderer() { 58 return new Renderer (); 59 } 60 61 private static final class Model implements ComboBoxModel , PropertyChangeListener , Comparator <JavaPlatform> { 62 63 private static final Collator COLL = Collator.getInstance(); 64 private static final JavaPlatformManager mgr = JavaPlatformManager.getDefault(); 65 private final SortedSet <JavaPlatform> platforms = new TreeSet (this); 66 private final List <ListDataListener > listeners = new ArrayList (); 67 private JavaPlatform selected; 68 69 public Model() { 70 refresh(); 71 mgr.addPropertyChangeListener(WeakListeners.propertyChange(this, mgr)); 72 } 73 74 private void refresh() { 75 platforms.clear(); 76 platforms.addAll(Arrays.asList(mgr.getInstalledPlatforms())); 77 } 78 79 public int getSize() { 80 return platforms.size(); 81 } 82 83 public Object getElementAt(int index) { 84 return (JavaPlatform) new ArrayList (platforms).get(index); 85 } 86 87 public void addListDataListener(ListDataListener l) { 88 listeners.add(l); 89 } 90 91 public void removeListDataListener(ListDataListener l) { 92 listeners.remove(l); 93 } 94 95 public void setSelectedItem(Object sel) { 96 if (sel != selected) { 97 selected = (JavaPlatform) sel; 98 fireChange(); 99 } 100 } 101 102 public Object getSelectedItem() { 103 return selected; 104 } 105 106 private void fireChange() { 107 ListDataEvent ev = new ListDataEvent (this, ListDataEvent.CONTENTS_CHANGED, 0, 0); 108 Iterator it = new ArrayList (listeners).iterator(); 109 while (it.hasNext()) { 110 ((ListDataListener ) it.next()).contentsChanged(ev); 111 } 112 } 113 114 public void propertyChange(PropertyChangeEvent evt) { 115 refresh(); 116 fireChange(); 117 } 118 119 public int compare(JavaPlatform p1, JavaPlatform p2) { 120 int res = COLL.compare(p1.getDisplayName(), p2.getDisplayName()); 121 if (res != 0) { 122 return res; 123 } else { 124 String id1 = ModuleProperties.getPlatformID(p1); 125 String id2 = ModuleProperties.getPlatformID(p2); 126 if (id1 != null && id2 != null) { 127 return id1.compareTo(id2); 128 } else { 129 return System.identityHashCode(p1) - System.identityHashCode(p2); 130 } 131 } 132 } 133 134 } 135 136 private static final class Renderer extends JLabel implements ListCellRenderer , UIResource { 137 138 public Renderer() { 139 setOpaque(true); 140 } 141 142 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 143 setName("ComboBox.listRenderer"); 146 String name = value != null ? ((JavaPlatform) value).getDisplayName() : null; 147 setText(name); 148 149 if ( isSelected ) { 150 setBackground(list.getSelectionBackground()); 151 setForeground(list.getSelectionForeground()); 152 } 153 else { 154 setBackground(list.getBackground()); 155 setForeground(list.getForeground()); 156 } 157 158 return this; 159 } 160 161 public String getName() { 163 String name = super.getName(); 164 return name == null ? "ComboBox.renderer" : name; } 166 167 } 168 169 } 170 | Popular Tags |