1 19 20 package org.netbeans.modules.form.palette; 21 22 import javax.swing.*; 23 import javax.swing.border.*; 24 import javax.swing.event.*; 25 import java.awt.Component ; 26 import java.awt.event.*; 27 import java.beans.*; 28 import java.util.*; 29 import java.io.File ; 30 import java.net.URL ; 31 32 import org.openide.WizardDescriptor; 33 import org.openide.ErrorManager; 34 import org.openide.filesystems.*; 35 import org.netbeans.api.project.libraries.*; 36 37 44 45 class ChooseLibraryWizardPanel implements WizardDescriptor.Panel { 46 47 private LibrarySelector librarySelector; 48 49 51 private EventListenerList listenerList; 52 53 56 public java.awt.Component getComponent() { 57 if (librarySelector == null) { 58 librarySelector = new LibrarySelector(); 59 60 librarySelector.setName( 62 PaletteUtils.getBundleString("CTL_SelectLibrary_Caption")); librarySelector.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (0)); 65 66 librarySelector.list.addListSelectionListener( 67 new ListSelectionListener() { 68 public void valueChanged(ListSelectionEvent e) { 69 fireStateChanged(); 70 } 71 }); 72 73 } 79 80 return librarySelector; 81 } 82 83 public org.openide.util.HelpCtx getHelp() { 84 return new org.openide.util.HelpCtx("beans.adding"); } 87 88 public boolean isValid() { 89 return librarySelector != null 90 && librarySelector.getSelectedLibraries() != null; 91 } 92 93 public void readSettings(Object settings) { 94 } 96 97 public void storeSettings(Object settings) { 98 if (librarySelector != null) { Library[] libraries = librarySelector.getSelectedLibraries(); 100 101 104 Map fileMap = new HashMap(); Map libraryMap = new HashMap(); 107 for (int i=0; i < libraries.length; i++) { 108 List content = libraries[i].getContent("classpath"); for (Iterator it=content.iterator(); it.hasNext(); ) { 111 URL rootURL = (URL ) it.next(); 112 if ("jar".equals(rootURL.getProtocol())) { String path = rootURL.getPath(); 114 int index = path.lastIndexOf('!'); 115 if (index != -1) { 116 try { 117 rootURL = new URL (path.substring(0, index)); 118 } catch (java.net.MalformedURLException mex) { 119 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, mex); 120 continue; 121 } 122 } 123 } 124 File rootFile = FileUtil.toFile(URLMapper.findFileObject(rootURL)); 125 String rootPath = rootFile.getAbsolutePath(); 126 fileMap.put(rootPath, rootFile); 127 libraryMap.put(rootPath, libraries[i].getName()); 128 } 129 } 130 131 File [] libFiles = new File [fileMap.size()]; 132 fileMap.values().toArray(libFiles); 133 134 AddToPaletteWizard wizard = (AddToPaletteWizard) settings; 135 wizard.setJARFiles(libFiles); 136 wizard.libraryNameMap = libraryMap; 137 } 138 } 139 140 public void addChangeListener(ChangeListener listener) { 141 if (listenerList == null) 142 listenerList = new EventListenerList(); 143 listenerList.add(ChangeListener.class, listener); 144 } 145 146 public void removeChangeListener(ChangeListener listener) { 147 if (listenerList != null) 148 listenerList.remove(ChangeListener.class, listener); 149 } 150 151 153 void fireStateChanged() { 154 if (listenerList == null) 155 return; 156 157 ChangeEvent e = null; 158 Object [] listeners = listenerList.getListenerList(); 159 for (int i=listeners.length-2; i >= 0; i-=2) { 160 if (listeners[i] == ChangeListener.class) { 161 if (e == null) 162 e = new ChangeEvent(this); 163 ((ChangeListener)listeners[i+1]).stateChanged(e); 164 } 165 } 166 } 167 168 170 static class LibrarySelector extends JPanel { 171 172 JList list; 173 List libList; 174 175 LibrarySelector() { 176 list = new JList(); 177 list.setCellRenderer(new LibraryRenderer()); 178 list.setLayoutOrientation(JList.VERTICAL_WRAP); 179 list.setVisibleRowCount(0); 180 updateLibraryList(); 181 182 setLayout(new java.awt.GridBagLayout ()); 183 java.awt.GridBagConstraints gridBagConstraints; 184 185 JLabel label1 = new JLabel(); 186 org.openide.awt.Mnemonics.setLocalizedText( 187 label1, PaletteUtils.getBundleString("CTL_Libraries")); label1.setLabelFor(list); 189 gridBagConstraints = new java.awt.GridBagConstraints (); 190 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 191 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 192 add(label1, gridBagConstraints); 193 194 204 JScrollPane scrollpane1 = new javax.swing.JScrollPane (); 205 scrollpane1.setViewportView(list); 206 gridBagConstraints = new java.awt.GridBagConstraints (); 207 gridBagConstraints.gridx = 0; 208 gridBagConstraints.gridy = 1; 209 gridBagConstraints.gridwidth = 3; 210 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 211 gridBagConstraints.weightx = 1.0; 212 gridBagConstraints.weighty = 1.0; 213 add(scrollpane1, gridBagConstraints); 214 215 } 226 227 Library[] getSelectedLibraries() { 228 Object [] selected = list.getSelectedValues(); 229 Library[] libraries = new Library[selected.length]; 230 for (int i=0; i < selected.length; i++) 231 libraries[i] = (Library) selected[i]; 232 return libraries; 233 } 234 235 void updateLibraryList() { 236 Library[] libraries = LibraryManager.getDefault().getLibraries(); 237 libList = new ArrayList(libraries.length); 238 for (int i=0; i < libraries.length; i++) 239 if (libraries[i].getType().equals("j2se")) libList.add(libraries[i]); 241 242 list.setModel(new AbstractListModel() { 243 public int getSize() { return libList.size(); } 244 public Object getElementAt(int i) { return libList.get(i); } 245 }); 246 } 247 248 public void addNotify() { 249 super.addNotify(); 250 list.requestFocus(); 251 } 252 253 public java.awt.Dimension getPreferredSize() { 254 return new java.awt.Dimension (400, 300); 255 } 256 } 257 258 private static class LibraryRenderer extends JLabel 259 implements ListCellRenderer 260 { 261 private static final Border hasFocusBorder = 262 new LineBorder(UIManager.getColor("List.focusCellHighlight")); private static final Border noFocusBorder = BorderFactory.createEmptyBorder(1, 1, 1, 1); 264 265 public LibraryRenderer() { 266 setOpaque(true); 267 setBorder(noFocusBorder); 268 } 269 270 public Component getListCellRendererComponent(JList list, 271 Object value, 272 int index, 273 boolean isSelected, 274 boolean cellHasFocus) 275 { 276 Library lib = (Library) value; 277 setText(lib.getDisplayName()); 278 279 if (isSelected){ 280 setBackground(UIManager.getColor("List.selectionBackground")); setForeground(UIManager.getColor("List.selectionForeground")); } 283 else { 284 setBackground(list.getBackground()); 285 setForeground(list.getForeground()); 286 } 287 setBorder(cellHasFocus ? hasFocusBorder : noFocusBorder); 288 return this; 289 } 290 } 291 } 292 | Popular Tags |