1 19 20 package org.netbeans.modules.j2ee.earproject.ui.customizer; 21 22 import java.awt.Component ; 23 import java.awt.Image ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.Collection ; 29 import java.util.Comparator ; 30 import java.util.List ; 31 import javax.swing.AbstractListModel ; 32 import javax.swing.DefaultListCellRenderer ; 33 import javax.swing.Icon ; 34 import javax.swing.ImageIcon ; 35 import javax.swing.JList ; 36 import javax.swing.event.ListSelectionListener ; 37 import org.netbeans.api.project.libraries.LibrariesCustomizer; 38 import org.netbeans.api.project.libraries.Library; 39 import org.netbeans.api.project.libraries.LibraryManager; 40 import org.openide.util.HelpCtx; 41 import org.openide.util.NbBundle; 42 import org.openide.util.Utilities; 43 import org.openide.util.WeakListeners; 44 45 48 public class LibrariesChooser extends javax.swing.JPanel implements HelpCtx.Provider { 49 50 private final Collection incompatibleLibs; 51 52 53 public LibrariesChooser(Collection alreadySelectedLibs, String j2eePlatform) { 54 initComponents(); 55 jList1.setPrototypeCellValue("0123456789012345678901234"); jList1.setModel(new LibrariesListModel()); 57 incompatibleLibs = java.util.Collections.EMPTY_LIST; 59 jList1.setCellRenderer(new LibraryRenderer(alreadySelectedLibs, incompatibleLibs, j2eePlatform)); 61 } 62 63 public Library[] getSelectedLibraries () { 64 Object [] selected = this.jList1.getSelectedValues(); 65 Collection <Library> libs = new ArrayList <Library>(); 66 for (int i = 0; i < selected.length; i++) { 67 Library lib = (Library) selected[i]; 68 if(!incompatibleLibs.contains(lib)) { libs.add(lib); 70 } 71 } 72 return libs.toArray(new Library[libs.size()]); 73 } 74 75 public HelpCtx getHelpCtx() { 76 return new HelpCtx(LibrariesChooser.class); 77 } 78 79 public void addListSelectionListener(ListSelectionListener listener) { 80 jList1.addListSelectionListener(listener); 81 } 82 83 public boolean isValidSelection() { 84 Object [] selected = this.jList1.getSelectedValues(); 85 if(selected.length == 0) { 86 return false; 87 } 88 for (int i = 0; i < selected.length; i++) { 89 if(incompatibleLibs.contains(selected[i])) { 90 return false; 91 } 92 } 93 return true; 94 } 95 96 101 private void initComponents() { 103 java.awt.GridBagConstraints gridBagConstraints; 104 105 jLabel1 = new javax.swing.JLabel (); 106 jScrollPane1 = new javax.swing.JScrollPane (); 107 jList1 = new javax.swing.JList (); 108 edit = new javax.swing.JButton (); 109 110 setLayout(new java.awt.GridBagLayout ()); 111 112 setPreferredSize(new java.awt.Dimension (350, 250)); 113 getAccessibleContext().setAccessibleDescription(null); jLabel1.setLabelFor(jList1); 115 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, NbBundle.getMessage(LibrariesChooser.class, "CTL_Libraries")); gridBagConstraints = new java.awt.GridBagConstraints (); 117 gridBagConstraints.gridx = 0; 118 gridBagConstraints.gridy = 0; 119 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 120 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 121 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 122 gridBagConstraints.weightx = 1.0; 123 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 12); 124 add(jLabel1, gridBagConstraints); 125 126 jScrollPane1.setViewportView(jList1); 127 jList1.getAccessibleContext().setAccessibleDescription(null); 129 gridBagConstraints = new java.awt.GridBagConstraints (); 130 gridBagConstraints.gridx = 0; 131 gridBagConstraints.gridy = 1; 132 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 133 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 134 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 135 gridBagConstraints.weightx = 1.0; 136 gridBagConstraints.weighty = 1.0; 137 gridBagConstraints.insets = new java.awt.Insets (6, 12, 12, 12); 138 add(jScrollPane1, gridBagConstraints); 139 140 org.openide.awt.Mnemonics.setLocalizedText(edit, NbBundle.getMessage(LibrariesChooser.class, "CTL_ManageLibraries")); edit.addActionListener(new java.awt.event.ActionListener () { 142 public void actionPerformed(java.awt.event.ActionEvent evt) { 143 editLibraries(evt); 144 } 145 }); 146 147 gridBagConstraints = new java.awt.GridBagConstraints (); 148 gridBagConstraints.gridx = 0; 149 gridBagConstraints.gridy = 2; 150 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 151 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 12); 152 add(edit, gridBagConstraints); 153 edit.getAccessibleContext().setAccessibleDescription(null); 155 } 157 private void editLibraries(java.awt.event.ActionEvent evt) { LibrariesListModel model = (LibrariesListModel) jList1.getModel (); 159 Collection <Library> oldLibraries = Arrays.asList(model.getLibraries()); 160 LibrariesCustomizer.showCustomizer((Library)this.jList1.getSelectedValue()); 161 List <Library> currentLibraries = Arrays.asList(model.getLibraries()); 162 Collection <Library> newLibraries = new ArrayList <Library>(currentLibraries); 163 164 newLibraries.removeAll(oldLibraries); 165 int indexes[] = new int[newLibraries.size()]; 166 167 int i = 0; 168 for (Library lib : newLibraries) { 169 indexes[i++] = currentLibraries.indexOf(lib); 170 } 171 this.jList1.setSelectedIndices (indexes); 172 } 174 175 private javax.swing.JButton edit; 177 private javax.swing.JLabel jLabel1; 178 private javax.swing.JList jList1; 179 private javax.swing.JScrollPane jScrollPane1; 180 182 183 184 private static final class LibrariesListModel extends AbstractListModel implements PropertyChangeListener { 185 186 private Library[] cache; 187 188 private int numberOfLibs; 189 190 192 197 public LibrariesListModel() { 198 LibraryManager manager = LibraryManager.getDefault(); 199 manager.addPropertyChangeListener((PropertyChangeListener )WeakListeners.create(PropertyChangeListener .class, 200 this, manager)); 201 } 202 203 public synchronized int getSize() { 204 if (this.cache == null) { 205 this.cache = this.createLibraries(); 206 } 207 return this.cache.length; 208 } 209 210 public synchronized Object getElementAt(int index) { 211 if (this.cache == null) { 212 this.cache = this.createLibraries(); 213 } 214 if (index >= 0 && index < this.cache.length) { 215 return this.cache[index]; 216 } 217 else { 218 return null; 219 } 220 } 221 222 public synchronized void propertyChange(PropertyChangeEvent evt) { 223 int oldSize = this.cache == null ? 0 : numberOfLibs; 224 this.cache = createLibraries(); 225 int newSize = numberOfLibs; 226 this.fireContentsChanged(this, 0, Math.min(oldSize-1,newSize-1)); 227 if (oldSize > newSize) { 228 this.fireIntervalRemoved(this,newSize,oldSize-1); 229 } 230 else if (oldSize < newSize) { 231 this.fireIntervalAdded(this,oldSize,newSize-1); 232 } 233 } 234 235 public synchronized Library[] getLibraries () { 236 if (this.cache == null) { 237 this.cache = this.createLibraries(); 238 } 239 return this.cache; 240 } 241 242 private Library[] createLibraries () { 243 Library[] libs = LibraryManager.getDefault().getLibraries(); 244 numberOfLibs = libs.length; 245 Arrays.sort(libs, new Comparator <Library> () { 246 public int compare (Library l1, Library l2) { 247 String name1 = l1.getDisplayName(); 248 String name2 = l2.getDisplayName(); 249 return name1.compareToIgnoreCase(name2); 250 } 251 }); 252 return libs; 253 } 254 } 255 256 257 private static final class LibraryRenderer extends DefaultListCellRenderer { 258 259 private static final String LIBRARY_ICON = "org/netbeans/modules/j2ee/earproject/ui/resources/libraries.gif"; private Icon cachedIcon; 261 private final Collection alreadySelectedLibs; 262 private final Collection incompatibleLibs; 263 private final String j2eePlatform; 264 265 public LibraryRenderer(Collection alreadySelectedLibs, Collection incompatibleLibs, String j2eePlatform) { 266 this.alreadySelectedLibs = alreadySelectedLibs; 267 this.j2eePlatform = j2eePlatform; 268 this.incompatibleLibs = incompatibleLibs; 269 } 270 271 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 272 String displayName = null; 273 if (value instanceof Library) { 274 displayName = ((Library)value).getDisplayName(); 275 } 276 super.getListCellRendererComponent(list, displayName, index, isSelected, cellHasFocus); 277 final String toolTipText; 278 if (value instanceof Library) { 279 final String libraryString = VisualClasspathSupport.getLibraryString((Library) value); 280 if (alreadySelectedLibs.contains(value)) { 281 toolTipText = NbBundle.getMessage(LibrariesChooser.class, "LBL_LibraryAlreadyInProject_ToolTip") + 282 " !!! (" + libraryString + ")"; 283 } else if (incompatibleLibs.contains(value)) { 284 toolTipText = NbBundle.getMessage(LibrariesChooser.class, "LBL_IncompatibleLibrary_ToolTip") 285 + " (" + j2eePlatform + ") !!! (" + libraryString + ")"; 286 setEnabled(false); 287 } else { 288 toolTipText = libraryString; 289 } 290 } else { 291 toolTipText = null; 292 } 293 setToolTipText(toolTipText); 294 setIcon(createIcon()); 295 return this; 296 } 297 298 private synchronized Icon createIcon () { 299 if (this.cachedIcon == null) { 300 Image img = Utilities.loadImage(LIBRARY_ICON); 301 this.cachedIcon = new ImageIcon (img); 302 } 303 return this.cachedIcon; 304 } 305 306 } 307 308 } 309 | Popular Tags |