1 19 20 package org.netbeans.modules.j2ee.ejbjarproject.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() { 84 java.awt.GridBagConstraints gridBagConstraints; 85 86 jLabel1 = new javax.swing.JLabel (); 87 jScrollPane1 = new javax.swing.JScrollPane (); 88 jList1 = new javax.swing.JList (); 89 edit = new javax.swing.JButton (); 90 91 setLayout(new java.awt.GridBagLayout ()); 92 93 setPreferredSize(new java.awt.Dimension (350, 250)); 94 getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(LibrariesChooser.class).getString("AD_LibrariesChooser")); 95 jLabel1.setDisplayedMnemonic(org.openide.util.NbBundle.getBundle(LibrariesChooser.class).getString("MNE_InstalledLibraries").charAt(0)); 96 jLabel1.setLabelFor(jList1); 97 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getBundle(LibrariesChooser.class).getString("CTL_InstalledLibraries")); 98 gridBagConstraints = new java.awt.GridBagConstraints (); 99 gridBagConstraints.gridx = 0; 100 gridBagConstraints.gridy = 0; 101 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 102 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 103 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 104 gridBagConstraints.weightx = 1.0; 105 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 12); 106 add(jLabel1, gridBagConstraints); 107 108 jScrollPane1.setViewportView(jList1); 109 jList1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(LibrariesChooser.class).getString("AD_jScrollPaneLibraries")); 110 111 gridBagConstraints = new java.awt.GridBagConstraints (); 112 gridBagConstraints.gridx = 0; 113 gridBagConstraints.gridy = 1; 114 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 115 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 116 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 117 gridBagConstraints.weightx = 1.0; 118 gridBagConstraints.weighty = 1.0; 119 gridBagConstraints.insets = new java.awt.Insets (6, 12, 12, 12); 120 add(jScrollPane1, gridBagConstraints); 121 122 org.openide.awt.Mnemonics.setLocalizedText(edit, org.openide.util.NbBundle.getBundle(LibrariesChooser.class).getString("CTL_EditLibraries")); 123 edit.addActionListener(new java.awt.event.ActionListener () { 124 public void actionPerformed(java.awt.event.ActionEvent evt) { 125 editLibraries(evt); 126 } 127 }); 128 129 gridBagConstraints = new java.awt.GridBagConstraints (); 130 gridBagConstraints.gridx = 0; 131 gridBagConstraints.gridy = 2; 132 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 133 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 12); 134 add(edit, gridBagConstraints); 135 edit.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(LibrariesChooser.class).getString("AD_jButtonManageLibraries")); 136 137 } 138 140 private void editLibraries(java.awt.event.ActionEvent evt) { LibrariesListModel model = (LibrariesListModel) jList1.getModel (); 142 Collection oldLibraries = Arrays.asList(model.getLibraries()); 143 LibrariesCustomizer.showCustomizer((Library)this.jList1.getSelectedValue()); 144 List currentLibraries = Arrays.asList(model.getLibraries()); 145 Collection newLibraries = new ArrayList (currentLibraries); 146 147 newLibraries.removeAll(oldLibraries); 148 int indexes[] = new int [newLibraries.size()]; 149 150 Iterator it = newLibraries.iterator(); 151 for (int i=0; it.hasNext();i++) { 152 Library lib = (Library) it.next (); 153 indexes[i] = currentLibraries.indexOf (lib); 154 } 155 this.jList1.setSelectedIndices (indexes); 156 } 158 159 private javax.swing.JButton edit; 161 private javax.swing.JLabel jLabel1; 162 private javax.swing.JList jList1; 163 private javax.swing.JScrollPane jScrollPane1; 164 166 167 168 private static final class LibrariesListModel extends AbstractListModel implements PropertyChangeListener { 169 170 private Library[] cache; 171 172 public LibrariesListModel () { 173 LibraryManager manager = LibraryManager.getDefault(); 174 manager.addPropertyChangeListener((PropertyChangeListener )WeakListeners.create(PropertyChangeListener .class, 175 this, manager)); 176 } 177 178 public synchronized int getSize() { 179 if (this.cache == null) { 180 this.cache = this.createLibraries(); 181 } 182 return this.cache.length; 183 } 184 185 public synchronized Object getElementAt(int index) { 186 if (this.cache == null) { 187 this.cache = this.createLibraries(); 188 } 189 if (index >= 0 && index < this.cache.length) { 190 return this.cache[index]; 191 } 192 else { 193 return null; 194 } 195 } 196 197 public synchronized void propertyChange(PropertyChangeEvent evt) { 198 int oldSize = this.cache == null ? 0 : this.cache.length; 199 this.cache = createLibraries(); 200 int newSize = this.cache.length; 201 this.fireContentsChanged(this, 0, Math.min(oldSize-1,newSize-1)); 202 if (oldSize > newSize) { 203 this.fireIntervalRemoved(this,newSize,oldSize-1); 204 } 205 else if (oldSize < newSize) { 206 this.fireIntervalAdded(this,oldSize,newSize-1); 207 } 208 } 209 210 public synchronized Library[] getLibraries () { 211 if (this.cache == null) { 212 this.cache = this.createLibraries(); 213 } 214 return this.cache; 215 } 216 217 private Library[] createLibraries () { 218 Library[] libs = LibraryManager.getDefault().getLibraries(); 219 Arrays.sort(libs, new Comparator () { 220 public int compare (Object o1, Object o2) { 221 assert (o1 instanceof Library) && (o2 instanceof Library); 222 String name1 = ((Library)o1).getDisplayName(); 223 String name2 = ((Library)o2).getDisplayName(); 224 return name1.compareToIgnoreCase(name2); 225 } 226 }); 227 return libs; 228 } 229 } 230 231 232 private final class LibraryRenderer extends DefaultListCellRenderer { 233 234 private static final String LIBRARY_ICON = "org/netbeans/modules/java/j2seproject/ui/resources/libraries.gif"; private Icon cachedIcon; 236 237 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 238 String displayName = null; 239 String toolTip = null; 240 Color color = null; 241 if (value instanceof Library) { 242 Library lib = ((Library)value); 243 displayName = lib.getDisplayName(); 244 } 250 super.getListCellRendererComponent(list, displayName, index, isSelected, cellHasFocus); 251 256 setIcon(createIcon()); 257 return this; 262 } 263 264 private synchronized Icon createIcon () { 265 if (this.cachedIcon == null) { 266 Image img = Utilities.loadImage(LIBRARY_ICON); 267 this.cachedIcon = new ImageIcon (img); 268 } 269 return this.cachedIcon; 270 } 271 272 } 273 274 } 275 | Popular Tags |