generictestbeancustomizer.java
来自「测试工具」· Java 代码 · 共 614 行 · 第 1/2 页
JAVA
614 行
}
/*
* (non-Javadoc)
*
* @see org.apache.jmeter.gui.JMeterGUIComponent#configure(org.apache.jmeter.testelement.TestElement)
*/
public void setObject(Object map) {
propertyMap = (Map) map;
if (propertyMap.size() == 0) {
// Uninitialized -- set it to the defaults:
for (int i = 0; i < editors.length; i++) {
Object value = descriptors[i].getValue(DEFAULT);
String name = descriptors[i].getName();
if (value != null) {
propertyMap.put(name, value);
log.debug("Set " + name + "= " + value);
}
firePropertyChange(name, null, value);
}
}
// Now set the editors to the element's values:
for (int i = 0; i < editors.length; i++) {
if (editors[i] == null)
continue;
try {
setEditorValue(i, propertyMap.get(descriptors[i].getName()));
} catch (IllegalArgumentException e) {
// I guess this can happen as a result of a bad
// file read? In this case, it would be better to replace the
// incorrect value with anything valid, e.g. the default value
// for the property.
// But for the time being, I just prefer to be aware of any
// problems occuring here, most likely programming errors,
// so I'll bail out.
// (MS Note) Can't bail out - newly create elements have blank
// values and must get the defaults.
// Also, when loading previous versions of jmeter test scripts,
// some values
// may not be right, and should get default values - MS
// TODO: review this and possibly change to:
setEditorValue(i, descriptors[i].getValue(DEFAULT));
}
}
}
// /**
// * Find the index of the property of the given name.
// *
// * @param name
// * the name of the property
// * @return the index of that property in the descriptors array, or -1 if
// * there's no property of this name.
// */
// private int descriptorIndex(String name) // NOTUSED
// {
// for (int i = 0; i < descriptors.length; i++) {
// if (descriptors[i].getName().equals(name)) {
// return i;
// }
// }
// return -1;
// }
/**
* Initialize the GUI.
*/
private void init() {
setLayout(new GridBagLayout());
GridBagConstraints cl = new GridBagConstraints(); // for labels
cl.gridx = 0;
cl.anchor = GridBagConstraints.EAST;
cl.insets = new Insets(0, 1, 0, 1);
GridBagConstraints ce = new GridBagConstraints(); // for editors
ce.fill = GridBagConstraints.BOTH;
ce.gridx = 1;
ce.weightx = 1.0;
ce.insets = new Insets(0, 1, 0, 1);
GridBagConstraints cp = new GridBagConstraints(); // for panels
cp.fill = GridBagConstraints.BOTH;
cp.gridx = 1;
cp.gridy = GridBagConstraints.RELATIVE;
cp.gridwidth = 2;
cp.weightx = 1.0;
JPanel currentPanel = this;
String currentGroup = DEFAULT_GROUP;
int y = 0;
for (int i = 0; i < editors.length; i++) {
if (editors[i] == null)
continue;
if (log.isDebugEnabled()) {
log.debug("Laying property " + descriptors[i].getName());
}
String g = group(descriptors[i]);
if (!currentGroup.equals(g)) {
if (currentPanel != this) {
add(currentPanel, cp);
}
currentGroup = g;
currentPanel = new JPanel(new GridBagLayout());
currentPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
groupDisplayName(g)));
cp.weighty = 0.0;
y = 0;
}
Component customEditor = editors[i].getCustomEditor();
boolean multiLineEditor = false;
if (customEditor.getPreferredSize().height > 50 || customEditor instanceof JScrollPane) {
// TODO: the above works in the current situation, but it's
// just a hack. How to get each editor to report whether it
// wants to grow bigger? Whether the property label should
// be at the left or at the top of the editor? ...?
multiLineEditor = true;
}
JLabel label = createLabel(descriptors[i]);
label.setLabelFor(customEditor);
cl.gridy = y;
cl.gridwidth = multiLineEditor ? 2 : 1;
cl.anchor = multiLineEditor ? GridBagConstraints.CENTER : GridBagConstraints.EAST;
currentPanel.add(label, cl);
ce.gridx = multiLineEditor ? 0 : 1;
ce.gridy = multiLineEditor ? ++y : y;
ce.gridwidth = multiLineEditor ? 2 : 1;
ce.weighty = multiLineEditor ? 1.0 : 0.0;
cp.weighty += ce.weighty;
currentPanel.add(customEditor, ce);
y++;
}
if (currentPanel != this) {
add(currentPanel, cp);
}
// Add a 0-sized invisible component that will take all the vertical
// space that nobody wants:
cp.weighty = 0.0001;
add(Box.createHorizontalStrut(0), cp);
}
private JLabel createLabel(PropertyDescriptor desc) {
String text = desc.getDisplayName();
if (!"".equals(text)) {
text = propertyFieldLabelMessage.format(new Object[] { desc.getDisplayName() });
}
// if the displayName is the empty string, leave it like that.
JLabel label = new JLabel(text);
label.setHorizontalAlignment(JLabel.TRAILING);
text = propertyToolTipMessage.format(new Object[] { desc.getName(), desc.getShortDescription() });
label.setToolTipText(text);
return label;
}
/**
* Obtain a property descriptor's group.
*
* @param descriptor
* @return the group String.
*/
private String group(PropertyDescriptor d) {
String group = (String) d.getValue(GROUP);
if (group == null)
group = DEFAULT_GROUP;
return group;
}
/**
* Obtain a group's display name
*/
private String groupDisplayName(String group) {
try {
ResourceBundle b = (ResourceBundle) beanInfo.getBeanDescriptor().getValue(RESOURCE_BUNDLE);
if (b == null)
return group;
else
return b.getString(group + ".displayName");
} catch (MissingResourceException e) {
return group;
}
}
/**
* Comparator used to sort properties for presentation in the GUI.
*/
private class PropertyComparator implements Comparator, Serializable {
public int compare(Object o1, Object o2) {
return compare((PropertyDescriptor) o1, (PropertyDescriptor) o2);
}
private int compare(PropertyDescriptor d1, PropertyDescriptor d2) {
int result;
String g1 = group(d1), g2 = group(d2);
Integer go1 = groupOrder(g1), go2 = groupOrder(g2);
result = go1.compareTo(go2);
if (result != 0)
return result;
result = g1.compareTo(g2);
if (result != 0)
return result;
Integer po1 = propertyOrder(d1), po2 = propertyOrder(d2);
result = po1.compareTo(po2);
if (result != 0)
return result;
return d1.getName().compareTo(d2.getName());
}
/**
* Obtain a group's order.
*
* @param group
* group name
* @return the group's order (zero by default)
*/
private Integer groupOrder(String group) {
Integer order = (Integer) beanInfo.getBeanDescriptor().getValue(ORDER(group));
if (order == null)
order = new Integer(0);
return order;
}
/**
* Obtain a property's order.
*
* @param d
* @return the property's order attribute (zero by default)
*/
private Integer propertyOrder(PropertyDescriptor d) {
Integer order = (Integer) d.getValue(ORDER);
if (order == null)
order = new Integer(0);
return order;
}
}
/**
* Save values from the GUI fields into the property map
*/
void saveGuiFields() {
for (int i = 0; i < editors.length; i++) {
PropertyEditor propertyEditor=editors[i]; // might be null (e.g. in testing)
if (propertyEditor != null) {
Object value = propertyEditor.getValue();
String name = descriptors[i].getName();
if (value == null) {
propertyMap.remove(name);
if (log.isDebugEnabled()) {
log.debug("Unset " + name);
}
} else {
propertyMap.put(name, value);
if (log.isDebugEnabled()) {
log.debug("Set " + name + "= " + value);
}
}
}
}
}
void clearGuiFields() {
for (int i = 0; i < editors.length; i++) {
PropertyEditor propertyEditor=editors[i]; // might be null (e.g. in testing)
if (propertyEditor != null) {
try {
if (propertyEditor instanceof WrapperEditor){
WrapperEditor we = (WrapperEditor) propertyEditor;
String tags[]=we.getTags();
if (tags != null && tags.length > 0) {
we.setAsText(tags[0]);
} else {
we.setValue("");
}
} else if (propertyEditor instanceof ComboStringEditor) {
ComboStringEditor cse = (ComboStringEditor) propertyEditor;
cse.setAsText(cse.getInitialEditValue());
} else {
propertyEditor.setAsText("");
}
} catch (IllegalArgumentException ex){
log.error("Failed to set field "+descriptors[i].getName(),ex);
}
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?