1 19 20 package org.netbeans.modules.web.project.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.ArrayList ; 36 import java.util.Arrays ; 37 import java.util.Collection ; 38 import java.util.Comparator ; 39 import java.util.Iterator ; 40 import java.util.List ; 41 42 43 import org.openide.util.Utilities; 44 45 49 public class LibrariesChooser extends javax.swing.JPanel implements HelpCtx.Provider { 50 51 52 public LibrariesChooser (final JButton addLibraryOption, String j2eePlatform) { 53 initComponents(); 54 jList1.setPrototypeCellValue("0123456789012345678901234"); jList1.setModel(new LibrariesListModel(j2eePlatform)); 56 jList1.setCellRenderer(new LibraryRenderer()); 57 jList1.addListSelectionListener( new ListSelectionListener () { 58 public void valueChanged(ListSelectionEvent e) { 59 if (e.getValueIsAdjusting()) { 60 return; 61 } 62 addLibraryOption.setEnabled (jList1.getSelectedIndices().length != 0); 63 } 64 }); 65 } 66 67 public HelpCtx getHelpCtx() { 68 return new HelpCtx( LibrariesChooser.class ); 69 } 70 71 public Library[] getSelectedLibraries () { 72 Object [] selected = this.jList1.getSelectedValues(); 73 Library[] libraries = new Library[selected.length]; 74 System.arraycopy(selected,0,libraries,0,selected.length); 75 return libraries; 76 } 77 78 83 private void initComponents() { 85 java.awt.GridBagConstraints gridBagConstraints; 86 87 jLabel1 = new javax.swing.JLabel (); 88 jScrollPane1 = new javax.swing.JScrollPane (); 89 jList1 = new javax.swing.JList (); 90 edit = new javax.swing.JButton (); 91 92 setLayout(new java.awt.GridBagLayout ()); 93 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 } 139 private void editLibraries(java.awt.event.ActionEvent evt) { LibrariesListModel model = (LibrariesListModel) jList1.getModel (); 141 Collection oldLibraries = Arrays.asList(model.getLibraries()); 142 LibrariesCustomizer.showCustomizer((Library)this.jList1.getSelectedValue()); 143 List currentLibraries = Arrays.asList(model.getLibraries()); 144 Collection newLibraries = new ArrayList (currentLibraries); 145 146 newLibraries.removeAll(oldLibraries); 147 int indexes[] = new int [newLibraries.size()]; 148 149 Iterator it = newLibraries.iterator(); 150 for (int i=0; it.hasNext();i++) { 151 Library lib = (Library) it.next (); 152 indexes[i] = currentLibraries.indexOf (lib); 153 } 154 this.jList1.setSelectedIndices (indexes); 155 } 157 158 private javax.swing.JButton edit; 160 private javax.swing.JLabel jLabel1; 161 private javax.swing.JList jList1; 162 private javax.swing.JScrollPane jScrollPane1; 163 165 166 167 private static final class LibrariesListModel extends AbstractListModel implements PropertyChangeListener { 168 169 private Library[] cache; 170 171 private String j2eePlatform; 172 private int numberOfLibs; 173 private static ArrayList filter13 = new ArrayList (); 174 175 static { 176 filter13.add(LibraryManager.getDefault().getLibrary("jstl11")); 177 filter13.add(LibraryManager.getDefault().getLibrary("jaxrpc16")); 178 filter13.add(LibraryManager.getDefault().getLibrary("toplink")); 179 filter13.add(LibraryManager.getDefault().getLibrary("Spring")); 180 filter13.add(LibraryManager.getDefault().getLibrary("jaxws20")); 181 filter13.add(LibraryManager.getDefault().getLibrary("jaxb20")); 182 filter13.add(LibraryManager.getDefault().getLibrary("struts")); 183 filter13.add(LibraryManager.getDefault().getLibrary("jsf")); 184 } 185 186 public LibrariesListModel (String j2eePlatform) { 187 this.j2eePlatform = j2eePlatform; 188 LibraryManager manager = LibraryManager.getDefault(); 189 manager.addPropertyChangeListener((PropertyChangeListener )WeakListeners.create(PropertyChangeListener .class, 190 this, manager)); 191 } 192 193 public synchronized int getSize() { 194 if (this.cache == null) { 195 this.cache = this.createLibraries(); 196 } 197 return this.cache.length; 198 } 199 200 public synchronized Object getElementAt(int index) { 201 if (this.cache == null) { 202 this.cache = this.createLibraries(); 203 } 204 if (index >= 0 && index < this.cache.length) { 205 return this.cache[index]; 206 } 207 else { 208 return null; 209 } 210 } 211 212 public synchronized void propertyChange(PropertyChangeEvent evt) { 213 int oldSize = this.cache == null ? 0 : numberOfLibs; 214 this.cache = createLibraries(); 215 int newSize = numberOfLibs; 216 this.fireContentsChanged(this, 0, Math.min(oldSize-1,newSize-1)); 217 if (oldSize > newSize) { 218 this.fireIntervalRemoved(this,newSize,oldSize-1); 219 } 220 else if (oldSize < newSize) { 221 this.fireIntervalAdded(this,oldSize,newSize-1); 222 } 223 } 224 225 public synchronized Library[] getLibraries () { 226 if (this.cache == null) { 227 this.cache = this.createLibraries(); 228 } 229 return this.cache; 230 } 231 232 private Library[] createLibraries () { 233 Library[] libs = LibraryManager.getDefault().getLibraries(); 234 235 numberOfLibs = libs.length; 236 ArrayList asList = new ArrayList (); 237 for (int i = 0; i < libs.length; i++) { 238 if (j2eePlatform.equals("1.3") && filter13.contains(libs[i])) 239 continue; 240 asList.add(libs [i]); 241 } 242 libs = (Library[]) asList.toArray(new Library [asList.size()]); 243 244 Arrays.sort(libs, new Comparator () { 245 public int compare (Object o1, Object o2) { 246 assert (o1 instanceof Library) && (o2 instanceof Library); 247 String name1 = ((Library)o1).getDisplayName(); 248 String name2 = ((Library)o2).getDisplayName(); 249 return name1.compareToIgnoreCase(name2); 250 } 251 }); 252 return libs; 253 } 254 } 255 256 257 private final class LibraryRenderer extends DefaultListCellRenderer { 258 259 private static final String LIBRARY_ICON = "org/netbeans/modules/web/project/ui/resources/libraries.gif"; private Icon cachedIcon; 261 262 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 263 String displayName = null; 264 if (value instanceof Library) { 265 Library lib = ((Library)value); 266 displayName = lib.getDisplayName(); 267 } 268 super.getListCellRendererComponent(list, displayName, index, isSelected, cellHasFocus); 269 270 setIcon(createIcon()); 271 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 |