1 19 20 package org.netbeans.modules.java.j2seproject.ui.customizer; 21 22 import javax.swing.event.ListSelectionEvent ; 23 import javax.swing.event.ListSelectionListener ; 24 import org.netbeans.api.project.libraries.Library; 25 import org.netbeans.api.project.libraries.LibraryManager; 26 import org.netbeans.api.project.libraries.LibrariesCustomizer; 27 import org.openide.util.HelpCtx; 28 import org.openide.util.WeakListeners; 29 30 import javax.swing.*; 31 import java.awt.*; 32 import java.beans.PropertyChangeListener ; 33 import java.beans.PropertyChangeEvent ; 34 35 import java.util.*; 36 import java.util.List ; 37 38 39 import org.openide.util.Utilities; 40 import org.openide.util.NbBundle; 41 45 public class LibrariesChooser extends javax.swing.JPanel implements HelpCtx.Provider { 46 47 private Set containedLibraries; 48 49 50 public LibrariesChooser (final JButton addLibraryOption, Set containedLibraries) { 51 this.containedLibraries = containedLibraries; 52 initComponents(); 53 jList1.setPrototypeCellValue("0123456789012345678901234"); jList1.setModel(new LibrariesListModel()); 55 jList1.setCellRenderer(new LibraryRenderer()); 56 jList1.addListSelectionListener( new ListSelectionListener () { 57 public void valueChanged(ListSelectionEvent e) { 58 if (e.getValueIsAdjusting()) { 59 return; 60 } 61 addLibraryOption.setEnabled (jList1.getSelectedIndices().length != 0); 62 } 63 }); 64 } 65 66 public HelpCtx getHelpCtx() { 67 return new HelpCtx( LibrariesChooser.class ); 68 } 69 70 public Library[] getSelectedLibraries () { 71 Object [] selected = this.jList1.getSelectedValues(); 72 Library[] libraries = new Library[selected.length]; 73 System.arraycopy(selected,0,libraries,0,selected.length); 74 return libraries; 75 } 76 77 82 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 84 85 jLabel1 = new javax.swing.JLabel (); 86 jScrollPane1 = new javax.swing.JScrollPane (); 87 jList1 = new javax.swing.JList (); 88 edit = new javax.swing.JButton (); 89 90 setLayout(new java.awt.GridBagLayout ()); 91 92 setPreferredSize(new java.awt.Dimension (350, 250)); 93 getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(LibrariesChooser.class).getString("AD_LibrariesChooser")); 94 jLabel1.setDisplayedMnemonic(org.openide.util.NbBundle.getBundle(LibrariesChooser.class).getString("MNE_InstalledLibraries").charAt(0)); 95 jLabel1.setLabelFor(jList1); 96 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getBundle(LibrariesChooser.class).getString("CTL_InstalledLibraries")); 97 gridBagConstraints = new java.awt.GridBagConstraints (); 98 gridBagConstraints.gridx = 0; 99 gridBagConstraints.gridy = 0; 100 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 101 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 102 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 103 gridBagConstraints.weightx = 1.0; 104 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 12); 105 add(jLabel1, gridBagConstraints); 106 107 jScrollPane1.setViewportView(jList1); 108 jList1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(LibrariesChooser.class).getString("AD_jScrollPaneLibraries")); 109 110 gridBagConstraints = new java.awt.GridBagConstraints (); 111 gridBagConstraints.gridx = 0; 112 gridBagConstraints.gridy = 1; 113 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 114 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 115 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 116 gridBagConstraints.weightx = 1.0; 117 gridBagConstraints.weighty = 1.0; 118 gridBagConstraints.insets = new java.awt.Insets (6, 12, 12, 12); 119 add(jScrollPane1, gridBagConstraints); 120 121 org.openide.awt.Mnemonics.setLocalizedText(edit, org.openide.util.NbBundle.getBundle(LibrariesChooser.class).getString("CTL_EditLibraries")); 122 edit.addActionListener(new java.awt.event.ActionListener () { 123 public void actionPerformed(java.awt.event.ActionEvent evt) { 124 editLibraries(evt); 125 } 126 }); 127 128 gridBagConstraints = new java.awt.GridBagConstraints (); 129 gridBagConstraints.gridx = 0; 130 gridBagConstraints.gridy = 2; 131 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 132 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 12); 133 add(edit, gridBagConstraints); 134 edit.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(LibrariesChooser.class).getString("AD_jButtonManageLibraries")); 135 136 } 138 private void editLibraries(java.awt.event.ActionEvent evt) { LibrariesListModel model = (LibrariesListModel) jList1.getModel (); 140 Collection oldLibraries = Arrays.asList(model.getLibraries()); 141 LibrariesCustomizer.showCustomizer((Library)this.jList1.getSelectedValue()); 142 List currentLibraries = Arrays.asList(model.getLibraries()); 143 Collection newLibraries = new ArrayList (currentLibraries); 144 145 newLibraries.removeAll(oldLibraries); 146 int indexes[] = new int [newLibraries.size()]; 147 148 Iterator it = newLibraries.iterator(); 149 for (int i=0; it.hasNext();i++) { 150 Library lib = (Library) it.next (); 151 indexes[i] = currentLibraries.indexOf (lib); 152 } 153 this.jList1.setSelectedIndices (indexes); 154 } 156 157 private javax.swing.JButton edit; 159 private javax.swing.JLabel jLabel1; 160 private javax.swing.JList jList1; 161 private javax.swing.JScrollPane jScrollPane1; 162 164 165 166 private static final class LibrariesListModel extends AbstractListModel implements PropertyChangeListener { 167 168 private Library[] cache; 169 170 public LibrariesListModel () { 171 LibraryManager manager = LibraryManager.getDefault(); 172 manager.addPropertyChangeListener((PropertyChangeListener )WeakListeners.create(PropertyChangeListener .class, 173 this, manager)); 174 } 175 176 public synchronized int getSize() { 177 if (this.cache == null) { 178 this.cache = this.createLibraries(); 179 } 180 return this.cache.length; 181 } 182 183 public synchronized Object getElementAt(int index) { 184 if (this.cache == null) { 185 this.cache = this.createLibraries(); 186 } 187 if (index >= 0 && index < this.cache.length) { 188 return this.cache[index]; 189 } 190 else { 191 return null; 192 } 193 } 194 195 public synchronized void propertyChange(PropertyChangeEvent evt) { 196 int oldSize = this.cache == null ? 0 : this.cache.length; 197 this.cache = createLibraries(); 198 int newSize = this.cache.length; 199 this.fireContentsChanged(this, 0, Math.min(oldSize-1,newSize-1)); 200 if (oldSize > newSize) { 201 this.fireIntervalRemoved(this,newSize,oldSize-1); 202 } 203 else if (oldSize < newSize) { 204 this.fireIntervalAdded(this,oldSize,newSize-1); 205 } 206 } 207 208 public synchronized Library[] getLibraries () { 209 if (this.cache == null) { 210 this.cache = this.createLibraries(); 211 } 212 return this.cache; 213 } 214 215 private Library[] createLibraries () { 216 Library[] libs = LibraryManager.getDefault().getLibraries(); 217 Arrays.sort(libs, new Comparator () { 218 public int compare (Object o1, Object o2) { 219 assert (o1 instanceof Library) && (o2 instanceof Library); 220 String name1 = ((Library)o1).getDisplayName(); 221 String name2 = ((Library)o2).getDisplayName(); 222 return name1.compareToIgnoreCase(name2); 223 } 224 }); 225 return libs; 226 } 227 } 228 229 230 private final class LibraryRenderer extends DefaultListCellRenderer { 231 232 private static final String LIBRARY_ICON = "org/netbeans/modules/java/j2seproject/ui/resources/libraries.gif"; private Icon cachedIcon; 234 235 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 236 String displayName = null; 237 String toolTip = null; 238 Color color = null; 239 if (value instanceof Library) { 240 Library lib = ((Library)value); 241 displayName = lib.getDisplayName(); 242 } 248 super.getListCellRendererComponent(list, displayName, index, isSelected, cellHasFocus); 249 254 setIcon(createIcon()); 255 return this; 260 } 261 262 private synchronized Icon createIcon () { 263 if (this.cachedIcon == null) { 264 Image img = Utilities.loadImage(LIBRARY_ICON); 265 this.cachedIcon = new ImageIcon (img); 266 } 267 return this.cachedIcon; 268 } 269 270 } 271 272 } 273 | Popular Tags |