1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.form;
import java.beans.*;
import java.util.*;
import java.awt.*;
/** The MethodPicker is a form which allows user to pick one of methods
* with specified required return type.
*
* @author Ian Formanek
* @version 1.00, Aug 29, 1998
*/
public class MethodPicker extends javax.swing.JPanel {
static final long serialVersionUID =7355140527892160804L;
/** Initializes the Form */
public MethodPicker(FormModel formModel, RADComponent componentToSelect, Class requiredType) {
this.formModel = formModel;
this.requiredType = requiredType;
initComponents();
java.util.List componentsList = formModel.getMetaComponents();
Collections.sort(componentsList, new ParametersPicker.ComponentComparator());
components = new RADComponent[componentsList.size()];
componentsList.toArray(components);
int selIndex = -1;
for (Iterator it = componentsList.iterator(); it.hasNext(); ) {
RADComponent radComp = (RADComponent) it.next();
if (componentToSelect != null && componentToSelect == radComp)
selIndex = componentsCombo.getItemCount();
if (radComp == formModel.getTopRADComponent())
componentsCombo.addItem(
FormUtils.getBundleString("CTL_FormTopContainerName")); // NOI18N
else
componentsCombo.addItem(radComp.getName());
}
if (selIndex >= 0)
componentsCombo.setSelectedIndex(selIndex);
updateMethodList();
componentLabel.setText(FormUtils.getBundleString("CTL_CW_Component")); // NOI18N
listLabel.setText(FormUtils.getBundleString("CTL_CW_MethodList")); // NOI18N
componentLabel.setDisplayedMnemonic(
FormUtils.getBundleString("CTL_CW_Component_Mnemonic").charAt(0)); // NOI18N
listLabel.setDisplayedMnemonic(
FormUtils.getBundleString("CTL_CW_MethodList_Mnemonic").charAt(0)); // NOI18N
componentsCombo.getAccessibleContext().setAccessibleDescription(
FormUtils.getBundleString("ACSD_CTL_CW_Component")); // NOI18N
methodList.getAccessibleContext().setAccessibleDescription(
FormUtils.getBundleString("ACSD_CTL_CW_MethodList")); // NOI18N
getAccessibleContext().setAccessibleDescription(
FormUtils.getBundleString("ACSD_MethodPicker")); // NOI18N
// HelpCtx.setHelpIDString(this, "gui.connecting.code"); // NOI18N
}
public boolean isPickerValid() {
return pickerValid;
}
private void setPickerValid(boolean v) {
boolean old = pickerValid;
pickerValid = v;
firePropertyChange("pickerValid", old, pickerValid); // NOI18N
}
RADComponent getSelectedComponent() {
return selectedComponent;
}
void setSelectedComponent(RADComponent selectedComponent) {
if (selectedComponent != null)
componentsCombo.setSelectedItem(selectedComponent.getName());
}
MethodDescriptor getSelectedMethod() {
if ((selectedComponent == null) ||(methodList.getSelectedIndex() == -1))
return null;
return descriptors [methodList.getSelectedIndex()];
}
void setSelectedMethod(MethodDescriptor selectedMethod) {
if (selectedMethod == null) {
methodList.setSelectedIndex(-1);
} else {
methodList.setSelectedValue(FormUtils.getMethodName(selectedMethod), true);
}
}
// ----------------------------------------------------------------------------
// private methods
private void addComponentsRecursively(ComponentContainer cont, Vector vect) {
RADComponent[] children = cont.getSubBeans();
for (int i = 0; i < children.length; i++) {
vect.addElement(children[i]);
if (children[i] instanceof ComponentContainer)
addComponentsRecursively((ComponentContainer)children[i], vect);
}
}
private void updateMethodList() {
RADComponent sel = getSelectedComponent();
if (sel == null) {
methodList.setListData(new Object [0]);
methodList.revalidate();
methodList.repaint();
} else {
MethodDescriptor[] descs = sel.getBeanInfo().getMethodDescriptors();
ArrayList filtered = new ArrayList();
for (int i = 0; i < descs.length; i ++) {
if (requiredType.isAssignableFrom(descs[i].getMethod().getReturnType()) &&
(descs[i].getMethod().getParameterTypes().length == 0)) // [FUTURE: - currently we allow only methods without params]
{
filtered.add(descs[i]);
}
}
// sort the methods by name
Collections.sort(filtered, new Comparator() {
public int compare(Object o1, Object o2) {
return((MethodDescriptor)o1).getName().compareTo(((MethodDescriptor)o2).getName());
}
}
);
descriptors = new MethodDescriptor[filtered.size()];
filtered.toArray(descriptors);
String[] items = new String [descriptors.length];
for (int i = 0; i < descriptors.length; i++)
items[i] = FormUtils.getMethodName(descriptors[i]);
methodList.setListData(items);
methodList.revalidate();
methodList.repaint();
}
}
private void updateState() {
if ((getSelectedComponent() == null) || (getSelectedMethod() == null)) {
setPickerValid(false);
} else {
setPickerValid(getSelectedMethod().getMethod().getParameterTypes().length == 0);
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the FormEditor.
*/
private void initComponents() {//GEN-BEGIN:initComponents
java.awt.GridBagConstraints gridBagConstraints;
componentLabel = new javax.swing.JLabel();
componentsCombo = new javax.swing.JComboBox();
listLabel = new javax.swing.JLabel();
propertiesScrollPane = new javax.swing.JScrollPane();
methodList = new javax.swing.JList();
setLayout(new java.awt.GridBagLayout());
setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(12, 12, 0, 11)));
componentLabel.setLabelFor(componentsCombo);
componentLabel.setText(FormUtils.getBundleString("CTL_Component"));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 6);
add(componentLabel, gridBagConstraints);
componentsCombo.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
componentsComboItemStateChanged(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
add(componentsCombo, gridBagConstraints);
listLabel.setLabelFor(methodList);
listLabel.setText(FormUtils.getBundleString("CTL_Component"));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0);
add(listLabel, gridBagConstraints);
methodList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
methodList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
methodListValueChanged(evt);
}
});
propertiesScrollPane.setViewportView(methodList);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
add(propertiesScrollPane, gridBagConstraints);
}//GEN-END:initComponents
private void methodListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_methodListValueChanged
if (methodList.getSelectedIndex() == -1)
selectedMethod = null;
else
selectedMethod = descriptors[methodList.getSelectedIndex()];
updateState();
}//GEN-LAST:event_methodListValueChanged
private void componentsComboItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_componentsComboItemStateChanged
if (componentsCombo.getSelectedIndex() == -1)
selectedComponent = null;
else
selectedComponent = components[componentsCombo.getSelectedIndex()];
updateMethodList();
}//GEN-LAST:event_componentsComboItemStateChanged
/** Closes the dialog */
private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:closeDialog
}//GEN-LAST:closeDialog
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel listLabel;
private javax.swing.JLabel componentLabel;
private javax.swing.JList methodList;
private javax.swing.JComboBox componentsCombo;
private javax.swing.JScrollPane propertiesScrollPane;
// End of variables declaration//GEN-END:variables
private FormModel formModel;
private boolean pickerValid = false;
private RADComponent[] components;
private Class requiredType;
private MethodDescriptor[] descriptors;
private RADComponent selectedComponent;
private MethodDescriptor selectedMethod;
}
|