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