1 19 20 package org.netbeans.modules.form.palette; 21 22 import java.awt.Component ; 23 import java.awt.Dimension ; 24 import javax.swing.*; 25 import javax.swing.border.*; 26 import java.io.File ; 27 import java.util.*; 28 import javax.swing.event.*; 29 import java.awt.event.*; 30 31 import org.openide.WizardDescriptor; 32 33 import org.netbeans.modules.form.project.ClassSource; 34 35 42 43 class ChooseBeansWizardPanel implements WizardDescriptor.Panel { 44 45 private File [] currentFiles; private Map libraryNameMap; 48 private java.util.List markedBeans; private java.util.List allBeans; private String sourceType; 51 52 private BeanSelector beanSelector; 53 private JLabel noBeansLabel; 54 55 private EventListenerList listenerList; 56 57 60 public Component getComponent() { 61 if ((markedBeans == null) && ((allBeans == null) || (allBeans.size() == 0))) { 62 String messageKey; 64 if (ClassSource.JAR_SOURCE.equals(sourceType)) 65 messageKey = "MSG_NoBeanInJAR"; else if (ClassSource.LIBRARY_SOURCE.equals(sourceType)) 67 messageKey = "MSG_NoBeanInLibrary"; else if (ClassSource.PROJECT_SOURCE.equals(sourceType)) 69 messageKey = "MSG_NoBeanInProject"; else 71 throw new IllegalArgumentException (); 72 noBeansLabel = new JLabel(PaletteUtils.getBundleString(messageKey)); 73 noBeansLabel.setPreferredSize(new Dimension (400, 300)); 74 noBeansLabel.setVerticalAlignment(SwingConstants.TOP); 75 noBeansLabel.setName(PaletteUtils.getBundleString("CTL_NoBeans_Caption")); noBeansLabel.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (1)); 78 return noBeansLabel; 79 } else { 80 if (beanSelector == null) { beanSelector = new BeanSelector(); 82 83 beanSelector.setName(PaletteUtils.getBundleString("CTL_SelectBeans_Caption")); beanSelector.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (1)); 87 if (markedBeans != null || allBeans != null) 88 beanSelector.setBeans(markedBeans, allBeans); 89 90 Listener listener = new Listener(); 91 beanSelector.list.addListSelectionListener(listener); 92 beanSelector.radio1.addActionListener(listener); 93 beanSelector.radio2.addActionListener(listener); 94 } 95 } 96 97 return beanSelector; 98 } 99 100 public org.openide.util.HelpCtx getHelp() { 101 return new org.openide.util.HelpCtx("beans.adding"); } 104 105 public boolean isValid() { 106 return beanSelector != null && beanSelector.getSelectedBeans().size() > 0; 107 } 108 109 public void readSettings(Object settings) { 110 AddToPaletteWizard wizard = (AddToPaletteWizard) settings; 111 sourceType = wizard.getSourceType(); 112 File [] jarFiles = wizard.getJARFiles(); 113 114 if (currentFiles != null && currentFiles.length == jarFiles.length) 115 for (int i=0; i < jarFiles.length; i++) 116 if (jarFiles[i].equals(currentFiles[i])) { 117 if (i+1 == jarFiles.length) 118 return; } 120 else break; 121 122 currentFiles = jarFiles; 123 libraryNameMap = wizard.libraryNameMap; 124 125 allBeans = null; markedBeans = BeanInstaller.findJavaBeansInJar(jarFiles); 127 if (markedBeans != null) { 128 if (libraryNameMap != null) remapLibraryNames(markedBeans, libraryNameMap); 130 Collections.sort(markedBeans); 131 } 132 else { 133 allBeans = BeanInstaller.findJavaBeans(jarFiles); 134 if (libraryNameMap != null) remapLibraryNames(allBeans, libraryNameMap); 136 Collections.sort(allBeans); 137 } 138 139 if (beanSelector != null) 140 beanSelector.setBeans(markedBeans, allBeans); 141 } 142 143 public void storeSettings(Object settings) { 144 if (beanSelector != null) { 145 java.util.List itemList = beanSelector.getSelectedBeans(); 146 BeanInstaller.ItemInfo[] itemArray = 147 new BeanInstaller.ItemInfo[itemList.size()]; 148 itemList.toArray(itemArray); 149 ((AddToPaletteWizard)settings).setSelectedBeans(itemArray); 150 } 151 } 152 153 public void addChangeListener(ChangeListener listener) { 154 if (listenerList == null) 155 listenerList = new EventListenerList(); 156 listenerList.add(ChangeListener.class, listener); 157 } 158 159 public void removeChangeListener(ChangeListener listener) { 160 if (listenerList != null) 161 listenerList.remove(ChangeListener.class, listener); 162 } 163 164 166 void fireStateChanged() { 167 if (listenerList == null) 168 return; 169 170 ChangeEvent e = null; 171 Object [] listeners = listenerList.getListenerList(); 172 for (int i=listeners.length-2; i >= 0; i-=2) { 173 if (listeners[i] == ChangeListener.class) { 174 if (e == null) 175 e = new ChangeEvent(this); 176 ((ChangeListener)listeners[i+1]).stateChanged(e); 177 } 178 } 179 } 180 181 private static void remapLibraryNames(List beans, Map map) { 182 for (int i=0, n=beans.size(); i < n; i++) { 183 BeanInstaller.ItemInfo ii = (BeanInstaller.ItemInfo) beans.get(i); 184 ii.source = (String ) map.get(ii.source); 185 } 186 } 187 188 190 static class BeanSelector extends JPanel { 191 192 JList list; 193 JRadioButton radio1, radio2; 194 195 BeanSelector() { 196 setLayout(new java.awt.GridBagLayout ()); 197 java.awt.GridBagConstraints gridBagConstraints; 198 199 JLabel label1 = new JLabel(); 200 org.openide.awt.Mnemonics.setLocalizedText( 201 label1, PaletteUtils.getBundleString("CTL_SelectBeans")); label1.setLabelFor(list); 203 gridBagConstraints = new java.awt.GridBagConstraints (); 204 gridBagConstraints.gridwidth = 3; 205 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 206 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 207 add(label1, gridBagConstraints); 208 209 list = new JList(); 210 list.setLayoutOrientation(JList.VERTICAL_WRAP); 211 list.setVisibleRowCount(0); 212 list.setCellRenderer(new ItemInfoRenderer()); 213 list.getAccessibleContext().setAccessibleDescription( 214 PaletteUtils.getBundleString("ACSD_CTL_SelectBeans")); 216 JScrollPane scrollPane = new JScrollPane(); 217 scrollPane.setViewportView(list); 218 gridBagConstraints = new java.awt.GridBagConstraints (); 219 gridBagConstraints.gridx = 0; 220 gridBagConstraints.gridy = 1; 221 gridBagConstraints.gridwidth = 3; 222 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 223 gridBagConstraints.weightx = 1.0; 224 gridBagConstraints.weighty = 1.0; 225 add(scrollPane, gridBagConstraints); 226 227 radio1 = new JRadioButton(); 228 radio1.setActionCommand("SHOW MARKED"); org.openide.awt.Mnemonics.setLocalizedText( 230 radio1, PaletteUtils.getBundleString("CTL_ShowMarked")); radio1.setToolTipText(PaletteUtils.getBundleString("HINT_ShowMarked")); gridBagConstraints = new java.awt.GridBagConstraints (); 233 gridBagConstraints.gridx = 0; 234 gridBagConstraints.gridy = 2; 235 gridBagConstraints.insets = new java.awt.Insets (6, 0, 0, 0); 236 add(radio1, gridBagConstraints); 237 239 radio2 = new JRadioButton(); 240 radio2.setActionCommand("SHOW ALL"); org.openide.awt.Mnemonics.setLocalizedText( 242 radio2, PaletteUtils.getBundleString("CTL_ShowAllClasses")); radio2.setToolTipText(PaletteUtils.getBundleString("HINT_ShowAllClasses")); gridBagConstraints = new java.awt.GridBagConstraints (); 245 gridBagConstraints.gridx = 1; 246 gridBagConstraints.gridy = 2; 247 gridBagConstraints.insets = new java.awt.Insets (6, 0, 0, 0); 248 add(radio2, gridBagConstraints); 249 251 ButtonGroup buttonGroup = new ButtonGroup(); 252 buttonGroup.add(radio1); 253 buttonGroup.add(radio2); 254 255 getAccessibleContext().setAccessibleDescription( 256 PaletteUtils.getBundleString("ACSD_SelectBeansDialog")); } 258 259 void setBeans(List markedBeans, List allBeans) { 260 if (markedBeans == null) { 261 radio1.setEnabled(false); 262 } 263 else { 264 radio1.setEnabled(true); 265 radio1.setSelected(true); 266 setDisplayedBeans(markedBeans); 267 } 268 if (allBeans != null && markedBeans == null) { 269 radio2.setSelected(true); 270 setDisplayedBeans(allBeans); 271 } 272 } 273 274 275 void setDisplayedBeans(final List beans) { 276 list.setModel(new AbstractListModel() { 277 public int getSize() { return beans.size(); } 278 public Object getElementAt(int i) { return beans.get(i); } 279 }); 280 } 281 282 283 List getSelectedBeans() { 284 Object [] sel = list.getSelectedValues(); 285 List al = new ArrayList(sel.length); 286 for (int i = 0; i < sel.length; i++) 287 al.add(sel[i]); 288 return al; 289 } 290 291 public Dimension getPreferredSize() { 292 return new Dimension (400, 300); 293 } 294 } 295 296 298 private static class ItemInfoRenderer extends JLabel 299 implements ListCellRenderer 300 { 301 private static final Border hasFocusBorder = 302 new LineBorder(UIManager.getColor("List.focusCellHighlight")); private static final Border noFocusBorder = BorderFactory.createEmptyBorder(1, 1, 1, 1); 304 305 public ItemInfoRenderer() { 306 setOpaque(true); 307 setBorder(noFocusBorder); 308 } 309 310 public Component getListCellRendererComponent(JList list, 311 Object value, 312 int index, 313 boolean isSelected, 314 boolean cellHasFocus) 315 { 316 String name = ((BeanInstaller.ItemInfo)value).classname; 317 setToolTipText(name); 319 int i = name.lastIndexOf('.'); 320 if (i >= 0) 321 name = name.substring(i+1); 322 323 setText(name); 325 if (isSelected){ 326 setBackground(UIManager.getColor("List.selectionBackground")); setForeground(UIManager.getColor("List.selectionForeground")); } 329 else { 330 setBackground(list.getBackground()); 331 setForeground(list.getForeground()); 332 } 333 setBorder(cellHasFocus ? hasFocusBorder : noFocusBorder); 334 return this; 335 } 336 } 337 338 340 class Listener implements ListSelectionListener, ActionListener { 341 342 public void valueChanged(ListSelectionEvent e) { 343 fireStateChanged(); 344 } 345 346 public void actionPerformed(ActionEvent ev) { 347 if ("SHOW MARKED".equals(ev.getActionCommand())) { beanSelector.setDisplayedBeans(markedBeans); 349 } 350 else if ("SHOW ALL".equals(ev.getActionCommand())) { if (allBeans == null) { allBeans = BeanInstaller.findJavaBeans(currentFiles); 354 if (libraryNameMap != null) { remapLibraryNames(allBeans, libraryNameMap); 356 libraryNameMap = null; 357 } 358 Collections.sort(allBeans); 359 } 360 beanSelector.setDisplayedBeans(allBeans); 361 } 362 } 363 } 364 } 365 | Popular Tags |