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 PropertyPicker extends javax.swing.JPanel { 34 35 public static final int CANCEL = 0; 36 public static final int OK = 1; 37 38 static final long serialVersionUID =5689122601606238081L; 39 40 public PropertyPicker(FormModel formModel, RADComponent componentToSelect, Class requiredType) { 41 this.formModel = formModel; 42 this.requiredType = requiredType; 43 initComponents(); 44 45 java.util.List componentsList = formModel.getMetaComponents(); 46 Collections.sort(componentsList, new ParametersPicker.ComponentComparator()); 47 components = new RADComponent[componentsList.size()]; 48 componentsList.toArray(components); 49 50 int selIndex = -1; 51 for (Iterator it = componentsList.iterator(); it.hasNext(); ) { 52 RADComponent radComp = (RADComponent) it.next(); 53 if (componentToSelect != null && componentToSelect == radComp) 54 selIndex = componentsCombo.getItemCount(); 55 if (radComp == formModel.getTopRADComponent()) 56 componentsCombo.addItem( 57 FormUtils.getBundleString("CTL_FormTopContainerName")); else 59 componentsCombo.addItem(radComp.getName()); 60 } 61 if (selIndex >= 0) 62 componentsCombo.setSelectedIndex(selIndex); 63 64 propertyList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); 65 updatePropertyList(); 66 67 componentLabel.setDisplayedMnemonic( 69 FormUtils.getBundleString("CTL_CW_Component_Mnemonic").charAt(0)); listLabel.setDisplayedMnemonic( 71 FormUtils.getBundleString("CTL_CW_PropertyList_Mnemonic").charAt(0)); 73 componentsCombo.getAccessibleContext().setAccessibleDescription( 74 FormUtils.getBundleString("ACSD_CTL_CW_Component")); propertyList.getAccessibleContext().setAccessibleDescription( 76 FormUtils.getBundleString("ACSD_CTL_CW_PropertyList")); getAccessibleContext().setAccessibleDescription( 78 FormUtils.getBundleString("ACSD_PropertyPicker")); 80 } 82 83 public boolean isPickerValid() { 84 return pickerValid; 85 } 86 87 private void setPickerValid(boolean v) { 88 boolean old = pickerValid; 89 pickerValid = v; 90 firePropertyChange("pickerValid", old, pickerValid); } 92 93 RADComponent getSelectedComponent() { 94 return selectedComponent; 95 } 96 97 void setSelectedComponent(RADComponent selectedComponent) { 98 if (selectedComponent != null) 99 componentsCombo.setSelectedItem(selectedComponent.getName()); 100 } 101 102 PropertyDescriptor getSelectedProperty() { 103 if ((selectedComponent == null) ||(propertyList.getSelectedIndex() == -1)) 104 return null; 105 return descriptors [propertyList.getSelectedIndex()]; 106 } 107 108 void setSelectedProperty(PropertyDescriptor selectedProperty) { 109 if (selectedProperty == null) { 110 propertyList.setSelectedIndex(-1); 111 } else { 112 propertyList.setSelectedValue(selectedProperty.getName(), true); 113 } 114 } 115 118 private void updatePropertyList() { 119 RADComponent sel = getSelectedComponent(); 120 if (sel == null) { 121 propertyList.setListData(new Object [0]); 122 propertyList.revalidate(); 123 propertyList.repaint(); 124 } else { 125 PropertyDescriptor[] descs = sel.getBeanInfo().getPropertyDescriptors(); 126 ArrayList filtered = new ArrayList(); 127 for (int i = 0; i < descs.length; i ++) { 128 if ((descs[i].getReadMethod() != null) && (descs[i].getPropertyType() != null) && requiredType.isAssignableFrom(descs[i].getPropertyType())) { 131 filtered.add(descs[i]); 132 } 133 } 134 135 Collections.sort(filtered, new Comparator() { 137 public int compare(Object o1, Object o2) { 138 return((PropertyDescriptor)o1).getName().compareTo(((PropertyDescriptor)o2).getName()); 139 } 140 } 141 ); 142 143 descriptors = new PropertyDescriptor[filtered.size()]; 144 filtered.toArray(descriptors); 145 146 String [] items = new String [descriptors.length]; 147 for (int i = 0; i < descriptors.length; i++) 148 items[i] = descriptors[i].getName(); 149 propertyList.setListData(items); 150 propertyList.revalidate(); 151 propertyList.repaint(); 152 } 153 } 154 155 private void updateState() { 156 setPickerValid((getSelectedComponent() != null) &&(getSelectedProperty() != null)); 157 } 158 159 164 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 166 167 componentsCombo = new javax.swing.JComboBox (); 168 propertiesScrollPane = new javax.swing.JScrollPane (); 169 propertyList = new javax.swing.JList (); 170 componentLabel = new javax.swing.JLabel (); 171 listLabel = new javax.swing.JLabel (); 172 173 setLayout(new java.awt.GridBagLayout ()); 174 175 setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (12, 12, 0, 11))); 176 componentsCombo.addItemListener(new java.awt.event.ItemListener () { 177 public void itemStateChanged(java.awt.event.ItemEvent evt) { 178 componentsComboItemStateChanged(evt); 179 } 180 }); 181 182 gridBagConstraints = new java.awt.GridBagConstraints (); 183 gridBagConstraints.gridx = 1; 184 gridBagConstraints.gridy = 0; 185 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 186 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 187 gridBagConstraints.weightx = 1.0; 188 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 189 add(componentsCombo, gridBagConstraints); 190 191 propertyList.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 192 public void valueChanged(javax.swing.event.ListSelectionEvent evt) { 193 propertyListValueChanged(evt); 194 } 195 }); 196 197 propertiesScrollPane.setViewportView(propertyList); 198 199 gridBagConstraints = new java.awt.GridBagConstraints (); 200 gridBagConstraints.gridx = 0; 201 gridBagConstraints.gridy = 2; 202 gridBagConstraints.gridwidth = 2; 203 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 204 gridBagConstraints.weightx = 1.0; 205 gridBagConstraints.weighty = 1.0; 206 add(propertiesScrollPane, gridBagConstraints); 207 208 componentLabel.setLabelFor(componentsCombo); 209 componentLabel.setText(FormUtils.getBundleString("CTL_Component")); 210 gridBagConstraints = new java.awt.GridBagConstraints (); 211 gridBagConstraints.gridx = 0; 212 gridBagConstraints.gridy = 0; 213 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 214 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 6); 215 add(componentLabel, gridBagConstraints); 216 217 listLabel.setLabelFor(propertyList); 218 listLabel.setText(FormUtils.getBundleString("CTL_CW_PropertyList")); 219 gridBagConstraints = new java.awt.GridBagConstraints (); 220 gridBagConstraints.gridx = 0; 221 gridBagConstraints.gridy = 1; 222 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 223 gridBagConstraints.insets = new java.awt.Insets (0, 0, 2, 0); 224 add(listLabel, gridBagConstraints); 225 226 } 228 229 private void propertyListValueChanged(javax.swing.event.ListSelectionEvent evt) { if (propertyList.getSelectedIndex() == -1) 231 selectedProperty = null; 232 else 233 selectedProperty = descriptors[propertyList.getSelectedIndex()]; 234 updateState(); 235 } 237 private void componentsComboItemStateChanged(java.awt.event.ItemEvent evt) { if (componentsCombo.getSelectedIndex() == -1) 239 selectedComponent = null; 240 else 241 selectedComponent = components[componentsCombo.getSelectedIndex()]; 242 updatePropertyList(); 243 } 245 246 private void closeDialog(java.awt.event.WindowEvent evt) { } 249 private javax.swing.JList propertyList; 251 private javax.swing.JLabel listLabel; 252 private javax.swing.JLabel componentLabel; 253 private javax.swing.JComboBox componentsCombo; 254 private javax.swing.JScrollPane propertiesScrollPane; 255 257 258 private FormModel formModel; 259 private boolean pickerValid = false; 260 261 private RADComponent[] components; 262 private Class requiredType; 263 private PropertyDescriptor[] descriptors; 264 private RADComponent selectedComponent; 265 private PropertyDescriptor selectedProperty; 266 267 } 268 | Popular Tags |