1 19 20 21 package org.netbeans.modules.form; 22 23 import java.beans.*; 24 import java.util.*; 25 import java.awt.*; 26 27 33 public class MethodPicker extends javax.swing.JPanel { 34 35 static final long serialVersionUID =7355140527892160804L; 36 37 public MethodPicker(FormModel formModel, RADComponent componentToSelect, Class requiredType) { 38 this.formModel = formModel; 39 this.requiredType = requiredType; 40 initComponents(); 41 42 java.util.List componentsList = formModel.getMetaComponents(); 43 Collections.sort(componentsList, new ParametersPicker.ComponentComparator()); 44 components = new RADComponent[componentsList.size()]; 45 componentsList.toArray(components); 46 47 int selIndex = -1; 48 for (Iterator it = componentsList.iterator(); it.hasNext(); ) { 49 RADComponent radComp = (RADComponent) it.next(); 50 if (componentToSelect != null && componentToSelect == radComp) 51 selIndex = componentsCombo.getItemCount(); 52 if (radComp == formModel.getTopRADComponent()) 53 componentsCombo.addItem( 54 FormUtils.getBundleString("CTL_FormTopContainerName")); else 56 componentsCombo.addItem(radComp.getName()); 57 } 58 if (selIndex >= 0) 59 componentsCombo.setSelectedIndex(selIndex); 60 61 updateMethodList(); 62 63 componentLabel.setText(FormUtils.getBundleString("CTL_CW_Component")); listLabel.setText(FormUtils.getBundleString("CTL_CW_MethodList")); 66 componentLabel.setDisplayedMnemonic( 67 FormUtils.getBundleString("CTL_CW_Component_Mnemonic").charAt(0)); listLabel.setDisplayedMnemonic( 69 FormUtils.getBundleString("CTL_CW_MethodList_Mnemonic").charAt(0)); 71 componentsCombo.getAccessibleContext().setAccessibleDescription( 72 FormUtils.getBundleString("ACSD_CTL_CW_Component")); methodList.getAccessibleContext().setAccessibleDescription( 74 FormUtils.getBundleString("ACSD_CTL_CW_MethodList")); getAccessibleContext().setAccessibleDescription( 76 FormUtils.getBundleString("ACSD_MethodPicker")); } 79 80 public boolean isPickerValid() { 81 return pickerValid; 82 } 83 84 private void setPickerValid(boolean v) { 85 boolean old = pickerValid; 86 pickerValid = v; 87 firePropertyChange("pickerValid", old, pickerValid); } 89 90 RADComponent getSelectedComponent() { 91 return selectedComponent; 92 } 93 94 void setSelectedComponent(RADComponent selectedComponent) { 95 if (selectedComponent != null) 96 componentsCombo.setSelectedItem(selectedComponent.getName()); 97 } 98 99 MethodDescriptor getSelectedMethod() { 100 if ((selectedComponent == null) ||(methodList.getSelectedIndex() == -1)) 101 return null; 102 return descriptors [methodList.getSelectedIndex()]; 103 } 104 105 void setSelectedMethod(MethodDescriptor selectedMethod) { 106 if (selectedMethod == null) { 107 methodList.setSelectedIndex(-1); 108 } else { 109 methodList.setSelectedValue(FormUtils.getMethodName(selectedMethod), true); 110 } 111 } 112 113 116 private void addComponentsRecursively(ComponentContainer cont, Vector vect) { 117 RADComponent[] children = cont.getSubBeans(); 118 for (int i = 0; i < children.length; i++) { 119 vect.addElement(children[i]); 120 if (children[i] instanceof ComponentContainer) 121 addComponentsRecursively((ComponentContainer)children[i], vect); 122 } 123 } 124 125 private void updateMethodList() { 126 RADComponent sel = getSelectedComponent(); 127 if (sel == null) { 128 methodList.setListData(new Object [0]); 129 methodList.revalidate(); 130 methodList.repaint(); 131 } else { 132 MethodDescriptor[] descs = sel.getBeanInfo().getMethodDescriptors(); 133 ArrayList filtered = new ArrayList(); 134 for (int i = 0; i < descs.length; i ++) { 135 if (requiredType.isAssignableFrom(descs[i].getMethod().getReturnType()) && 136 (descs[i].getMethod().getParameterTypes().length == 0)) { 138 filtered.add(descs[i]); 139 } 140 } 141 Collections.sort(filtered, new Comparator() { 143 public int compare(Object o1, Object o2) { 144 return((MethodDescriptor)o1).getName().compareTo(((MethodDescriptor)o2).getName()); 145 } 146 } 147 ); 148 149 descriptors = new MethodDescriptor[filtered.size()]; 150 filtered.toArray(descriptors); 151 152 String [] items = new String [descriptors.length]; 153 for (int i = 0; i < descriptors.length; i++) 154 items[i] = FormUtils.getMethodName(descriptors[i]); 155 methodList.setListData(items); 156 methodList.revalidate(); 157 methodList.repaint(); 158 } 159 } 160 161 private void updateState() { 162 if ((getSelectedComponent() == null) || (getSelectedMethod() == null)) { 163 setPickerValid(false); 164 } else { 165 setPickerValid(getSelectedMethod().getMethod().getParameterTypes().length == 0); 166 } 167 } 168 169 174 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 176 177 componentLabel = new javax.swing.JLabel (); 178 componentsCombo = new javax.swing.JComboBox (); 179 listLabel = new javax.swing.JLabel (); 180 propertiesScrollPane = new javax.swing.JScrollPane (); 181 methodList = new javax.swing.JList (); 182 183 setLayout(new java.awt.GridBagLayout ()); 184 185 setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (12, 12, 0, 11))); 186 componentLabel.setLabelFor(componentsCombo); 187 componentLabel.setText(FormUtils.getBundleString("CTL_Component")); 188 gridBagConstraints = new java.awt.GridBagConstraints (); 189 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 190 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 6); 191 add(componentLabel, gridBagConstraints); 192 193 componentsCombo.addItemListener(new java.awt.event.ItemListener () { 194 public void itemStateChanged(java.awt.event.ItemEvent evt) { 195 componentsComboItemStateChanged(evt); 196 } 197 }); 198 199 gridBagConstraints = new java.awt.GridBagConstraints (); 200 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 201 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 202 add(componentsCombo, gridBagConstraints); 203 204 listLabel.setLabelFor(methodList); 205 listLabel.setText(FormUtils.getBundleString("CTL_Component")); 206 gridBagConstraints = new java.awt.GridBagConstraints (); 207 gridBagConstraints.gridx = 0; 208 gridBagConstraints.gridy = 1; 209 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 210 gridBagConstraints.insets = new java.awt.Insets (0, 0, 2, 0); 211 add(listLabel, gridBagConstraints); 212 213 methodList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); 214 methodList.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 215 public void valueChanged(javax.swing.event.ListSelectionEvent evt) { 216 methodListValueChanged(evt); 217 } 218 }); 219 220 propertiesScrollPane.setViewportView(methodList); 221 222 gridBagConstraints = new java.awt.GridBagConstraints (); 223 gridBagConstraints.gridx = 0; 224 gridBagConstraints.gridy = 2; 225 gridBagConstraints.gridwidth = 2; 226 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 227 gridBagConstraints.weightx = 1.0; 228 gridBagConstraints.weighty = 1.0; 229 add(propertiesScrollPane, gridBagConstraints); 230 231 } 233 private void methodListValueChanged(javax.swing.event.ListSelectionEvent evt) { if (methodList.getSelectedIndex() == -1) 235 selectedMethod = null; 236 else 237 selectedMethod = descriptors[methodList.getSelectedIndex()]; 238 updateState(); 239 } 241 private void componentsComboItemStateChanged(java.awt.event.ItemEvent evt) { if (componentsCombo.getSelectedIndex() == -1) 243 selectedComponent = null; 244 else 245 selectedComponent = components[componentsCombo.getSelectedIndex()]; 246 updateMethodList(); 247 } 249 250 private void closeDialog(java.awt.event.WindowEvent evt) { } 253 private javax.swing.JLabel listLabel; 255 private javax.swing.JLabel componentLabel; 256 private javax.swing.JList methodList; 257 private javax.swing.JComboBox componentsCombo; 258 private javax.swing.JScrollPane propertiesScrollPane; 259 261 262 private FormModel formModel; 263 private boolean pickerValid = false; 264 265 private RADComponent[] components; 266 private Class requiredType; 267 private MethodDescriptor[] descriptors; 268 private RADComponent selectedComponent; 269 private MethodDescriptor selectedMethod; 270 271 } 272 | Popular Tags |