下面列出了怎么用java.beans.FeatureDescriptor的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Tests that a valid FeatureDescriptors are returned.
*/
@Test
public void testGetFeatureDescriptors02() {
ResourceBundleELResolver resolver = new ResourceBundleELResolver();
ELContext context = new ELContextImpl();
ResourceBundle resourceBundle = new TesterResourceBundle(
new Object[][] { { "key", "value" } });
@SuppressWarnings("unchecked")
Iterator<FeatureDescriptor> result = resolver.getFeatureDescriptors(
context, resourceBundle);
while (result.hasNext()) {
FeatureDescriptor featureDescriptor = result.next();
Assert.assertEquals("key", featureDescriptor.getDisplayName());
Assert.assertEquals("key", featureDescriptor.getName());
Assert.assertEquals("", featureDescriptor.getShortDescription());
Assert.assertFalse(featureDescriptor.isExpert());
Assert.assertFalse(featureDescriptor.isHidden());
Assert.assertTrue(featureDescriptor.isPreferred());
Assert.assertEquals(String.class,
featureDescriptor.getValue(ELResolver.TYPE));
Assert.assertEquals(Boolean.TRUE, featureDescriptor
.getValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME));
}
}
@Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
if (base == null) {
return null;
}
try {
BeanInfo info = Introspector.getBeanInfo(base.getClass());
PropertyDescriptor[] pds = info.getPropertyDescriptors();
for (int i = 0; i < pds.length; i++) {
pds[i].setValue(RESOLVABLE_AT_DESIGN_TIME, Boolean.TRUE);
pds[i].setValue(TYPE, pds[i].getPropertyType());
}
return Arrays.asList((FeatureDescriptor[]) pds).iterator();
} catch (IntrospectionException e) {
//
}
return null;
}
/**
* Tests that a valid FeatureDescriptors are returned.
*/
@Test
public void testGetFeatureDescriptors02() {
MapELResolver mapELResolver = new MapELResolver();
ELContext context = new StandardELContext(
ELManager.getExpressionFactory());
Map<String, String> map = new HashMap<>();
map.put("key", "value");
Iterator<FeatureDescriptor> result = mapELResolver
.getFeatureDescriptors(context, map);
while (result.hasNext()) {
FeatureDescriptor featureDescriptor = result.next();
Assert.assertEquals("key", featureDescriptor.getDisplayName());
Assert.assertEquals("key", featureDescriptor.getName());
Assert.assertEquals("", featureDescriptor.getShortDescription());
Assert.assertFalse(featureDescriptor.isExpert());
Assert.assertFalse(featureDescriptor.isHidden());
Assert.assertTrue(featureDescriptor.isPreferred());
Assert.assertEquals("key".getClass(),
featureDescriptor.getValue(ELResolver.TYPE));
Assert.assertEquals(Boolean.TRUE, featureDescriptor
.getValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME));
}
}
/**
* Tests that a valid FeatureDescriptors are returned.
*/
@Test
public void testGetFeatureDescriptors02() {
ResourceBundleELResolver resolver = new ResourceBundleELResolver();
ELContext context = new StandardELContext(
ELManager.getExpressionFactory());
ResourceBundle resourceBundle = new TesterResourceBundle(
new Object[][] { { "key", "value" } });
Iterator<FeatureDescriptor> result = resolver.getFeatureDescriptors(
context, resourceBundle);
while (result.hasNext()) {
FeatureDescriptor featureDescriptor = result.next();
Assert.assertEquals("key", featureDescriptor.getDisplayName());
Assert.assertEquals("key", featureDescriptor.getName());
Assert.assertEquals("", featureDescriptor.getShortDescription());
Assert.assertFalse(featureDescriptor.isExpert());
Assert.assertFalse(featureDescriptor.isHidden());
Assert.assertTrue(featureDescriptor.isPreferred());
Assert.assertEquals(String.class,
featureDescriptor.getValue(ELResolver.TYPE));
Assert.assertEquals(Boolean.TRUE, featureDescriptor
.getValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME));
}
}
/**
* Tests that a valid FeatureDescriptors are returned.
*/
@Test
public void testGetFeatureDescriptors02() {
ResourceBundleELResolver resolver = new ResourceBundleELResolver();
ELContext context = new ELContextImpl();
ResourceBundle resourceBundle = new TesterResourceBundle(
new Object[][] { { "key", "value" } });
@SuppressWarnings("unchecked")
Iterator<FeatureDescriptor> result = resolver.getFeatureDescriptors(
context, resourceBundle);
while (result.hasNext()) {
FeatureDescriptor featureDescriptor = result.next();
Assert.assertEquals("key", featureDescriptor.getDisplayName());
Assert.assertEquals("key", featureDescriptor.getName());
Assert.assertEquals("", featureDescriptor.getShortDescription());
Assert.assertFalse(featureDescriptor.isExpert());
Assert.assertFalse(featureDescriptor.isHidden());
Assert.assertTrue(featureDescriptor.isPreferred());
Assert.assertEquals(String.class,
featureDescriptor.getValue(ELResolver.TYPE));
Assert.assertEquals(Boolean.TRUE, featureDescriptor
.getValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME));
}
}
@Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
if (base == null) {
return null;
}
try {
BeanInfo info = Introspector.getBeanInfo(base.getClass());
PropertyDescriptor[] pds = info.getPropertyDescriptors();
for (int i = 0; i < pds.length; i++) {
pds[i].setValue(RESOLVABLE_AT_DESIGN_TIME, Boolean.TRUE);
pds[i].setValue(TYPE, pds[i].getPropertyType());
}
return Arrays.asList((FeatureDescriptor[]) pds).iterator();
} catch (IntrospectionException e) {
//
}
return null;
}
/**
* Tests that a valid FeatureDescriptors are returned.
*/
@Test
public void testGetFeatureDescriptors02() {
BeanELResolver resolver = new BeanELResolver();
ELContext context = new ELContextImpl();
Iterator<FeatureDescriptor> result = resolver.getFeatureDescriptors(context, new Bean());
while (result.hasNext()) {
PropertyDescriptor featureDescriptor = (PropertyDescriptor) result.next();
Assert.assertEquals(featureDescriptor.getPropertyType(),
featureDescriptor.getValue(ELResolver.TYPE));
Assert.assertEquals(Boolean.TRUE,
featureDescriptor.getValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME));
}
}
@Override
public void actionPerformed(ActionEvent ae) {
int i = getSelectedRow();
if (i != -1) {
FeatureDescriptor fd = getPropertySetModel().getFeatureDescriptor(i);
if (fd instanceof Property) {
java.beans.PropertyEditor ped = PropUtils.getPropertyEditor((Property) fd);
System.err.println(ped.getClass().getName());
} else {
System.err.println("PropertySets - no editor"); //NOI18N
}
} else {
System.err.println("No selection"); //NOI18N
}
}
/**
* Tests that a valid FeatureDescriptors are not returned if base is not
* Map.
*/
@Test
public void testGetFeatureDescriptors01() {
BeanELResolver resolver = new BeanELResolver();
ELContext context = new StandardELContext(ELManager.getExpressionFactory());
Iterator<FeatureDescriptor> result = resolver.getFeatureDescriptors(context, null);
Assert.assertNull(result);
}
@Override
protected JPopupMenu createPopupMenu() {
FeatureDescriptor fd = getSelection();
if (fd != null) {
if (fd instanceof RuleEditorNode.DeclarationProperty) {
//property
//
//actions:
//remove
//hide
//????
//custom popop for the whole panel
JPopupMenu pm = new JPopupMenu();
if(!addPropertyMode) {
pm.add(new GoToSourceAction(RuleEditorPanel.this, (RuleEditorNode.DeclarationProperty) fd));
pm.addSeparator();
pm.add(new RemovePropertyAction(RuleEditorPanel.this, (RuleEditorNode.DeclarationProperty) fd));
}
return pm;
} else if (fd instanceof RuleEditorNode.PropertyCategoryPropertySet) {
//property category
//TODO possibly add "add property" action which would
//preselect the css category in the "add property dialog".
}
}
//no context popup - create the generic popup
return genericPopupMenu;
}
/**
* Tests that a valid FeatureDescriptors are not returned if base is not
* Map.
*/
@Test
public void testGetFeatureDescriptors01() {
MapELResolver mapELResolver = new MapELResolver();
ELContext context = new StandardELContext(
ELManager.getExpressionFactory());
Iterator<FeatureDescriptor> result = mapELResolver
.getFeatureDescriptors(context, new Object());
Assert.assertNull(result);
}
/**
* If the base object is not null, returns an Iterator containing the set of JavaBeans
* properties available on the given object. Otherwise, returns null. The Iterator returned must
* contain zero or more instances of java.beans.FeatureDescriptor. Each info object contains
* information about a property in the bean, as obtained by calling the
* BeanInfo.getPropertyDescriptors method. The FeatureDescriptor is initialized using the same
* fields as are present in the PropertyDescriptor, with the additional required named
* attributes "type" and "resolvableAtDesignTime" set as follows:
* <ul>
* <li>{@link ELResolver#TYPE} - The runtime type of the property, from
* PropertyDescriptor.getPropertyType().</li>
* <li>{@link ELResolver#RESOLVABLE_AT_DESIGN_TIME} - true.</li>
* </ul>
*
* @param context
* The context of this evaluation.
* @param base
* The bean to analyze.
* @return An Iterator containing zero or more FeatureDescriptor objects, each representing a
* property on this bean, or null if the base object is null.
*/
@Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
if (isResolvable(base)) {
JsonNode node = (JsonNode) base;
final Iterator<String> keys = node.fieldNames();
return new Iterator<FeatureDescriptor>() {
public boolean hasNext() {
return keys.hasNext();
}
public FeatureDescriptor next() {
Object key = keys.next();
FeatureDescriptor feature = new FeatureDescriptor();
feature.setDisplayName(key == null ? "null" : key.toString());
feature.setName(feature.getDisplayName());
feature.setShortDescription("");
feature.setExpert(true);
feature.setHidden(false);
feature.setPreferred(true);
feature.setValue(TYPE, key == null ? "null" : key.getClass());
feature.setValue(RESOLVABLE_AT_DESIGN_TIME, true);
return feature;
}
public void remove() {
throw new UnsupportedOperationException("cannot remove");
}
};
}
return null;
}
/**
* If the base object is a map, returns an Iterator containing the set of keys available in the
* Map. Otherwise, returns null. The Iterator returned must contain zero or more instances of
* java.beans.FeatureDescriptor. Each info object contains information about a key in the Map,
* and is initialized as follows:
* <ul>
* <li>displayName - The return value of calling the toString method on this key, or "null" if
* the key is null</li>
* <li>name - Same as displayName property</li>
* <li>shortDescription - Empty string</li>
* <li>expert - false</li>
* <li>hidden - false</li>
* <li>preferred - true</li>
* </ul>
* In addition, the following named attributes must be set in the returned FeatureDescriptors:
* <ul>
* <li>{@link ELResolver#TYPE} - The return value of calling the getClass() method on this key,
* or null if the key is null.</li>
* <li>{@link ELResolver#RESOLVABLE_AT_DESIGN_TIME} - true</li>
* </ul>
*
* @param context
* The context of this evaluation.
* @param base
* The map to analyze. Only bases of type Map are handled by this resolver.
* @return An Iterator containing zero or more (possibly infinitely more) FeatureDescriptor
* objects, each representing a key in this map, or null if the base object is not a
* map.
*/
@Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
if (isResolvable(base)) {
Map<?, ?> map = (Map<?, ?>) base;
final Iterator<?> keys = map.keySet().iterator();
return new Iterator<FeatureDescriptor>() {
public boolean hasNext() {
return keys.hasNext();
}
public FeatureDescriptor next() {
Object key = keys.next();
FeatureDescriptor feature = new FeatureDescriptor();
feature.setDisplayName(key == null ? "null" : key.toString());
feature.setName(feature.getDisplayName());
feature.setShortDescription("");
feature.setExpert(true);
feature.setHidden(false);
feature.setPreferred(true);
feature.setValue(TYPE, key == null ? "null" : key.getClass());
feature.setValue(RESOLVABLE_AT_DESIGN_TIME, true);
return feature;
}
public void remove() {
throw new UnsupportedOperationException("cannot remove");
}
};
}
return null;
}
/** Used by EditablePropertyDisplayer to provide access to the real
* feature descriptor. Some property editors will cast the result of
* env.getFeatureDescriptor() as Property or PropertyDescriptor, so we
* need to return the original */
FeatureDescriptor getFeatureDescriptor() {
if (mdl instanceof ExPropertyModel) {
return ((ExPropertyModel) mdl).getFeatureDescriptor();
} else {
return this;
}
}
/**
* If the base object is not null, returns an Iterator containing the set of JavaBeans
* properties available on the given object. Otherwise, returns null. The Iterator returned must
* contain zero or more instances of java.beans.FeatureDescriptor. Each info object contains
* information about a property in the bean, as obtained by calling the
* BeanInfo.getPropertyDescriptors method. The FeatureDescriptor is initialized using the same
* fields as are present in the PropertyDescriptor, with the additional required named
* attributes "type" and "resolvableAtDesignTime" set as follows:
* <ul>
* <li>{@link ELResolver#TYPE} - The runtime type of the property, from
* PropertyDescriptor.getPropertyType().</li>
* <li>{@link ELResolver#RESOLVABLE_AT_DESIGN_TIME} - true.</li>
* </ul>
*
* @param context
* The context of this evaluation.
* @param base
* The bean to analyze.
* @return An Iterator containing zero or more FeatureDescriptor objects, each representing a
* property on this bean, or null if the base object is null.
*/
@Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
if (isResolvable(base)) {
final PropertyDescriptor[] properties;
try {
properties = Introspector.getBeanInfo(base.getClass()).getPropertyDescriptors();
} catch (IntrospectionException e) {
return Collections.<FeatureDescriptor> emptyList().iterator();
}
return new Iterator<FeatureDescriptor>() {
int next = 0;
public boolean hasNext() {
return properties != null && next < properties.length;
}
public FeatureDescriptor next() {
PropertyDescriptor property = properties[next++];
FeatureDescriptor feature = new FeatureDescriptor();
feature.setDisplayName(property.getDisplayName());
feature.setName(property.getName());
feature.setShortDescription(property.getShortDescription());
feature.setExpert(property.isExpert());
feature.setHidden(property.isHidden());
feature.setPreferred(property.isPreferred());
feature.setValue(TYPE, property.getPropertyType());
feature.setValue(RESOLVABLE_AT_DESIGN_TIME, true);
return feature;
}
public void remove() {
throw new UnsupportedOperationException("cannot remove");
}
};
}
return null;
}
/**
* Tests that a valid FeatureDescriptors are not returned if base is not
* Map.
*/
@Test
public void testGetFeatureDescriptors01() {
BeanELResolver resolver = new BeanELResolver();
ELContext context = new ELContextImpl();
Iterator<FeatureDescriptor> result = resolver.getFeatureDescriptors(context, null);
Assert.assertNull(result);
}
/**
* If the base object is a ResourceBundle, returns an Iterator containing the set of keys
* available in the ResourceBundle. Otherwise, returns null. The Iterator returned must contain
* zero or more instances of java.beans.FeatureDescriptor. Each info object contains information
* about a key in the ResourceBundle, and is initialized as follows:
* <ul>
* <li>displayName - The String key name</li>
* <li>name - Same as displayName property</li>
* <li>shortDescription - Empty string</li>
* <li>expert - false</li>
* <li>hidden - false</li>
* <li>preferred - true</li>
* </ul>
* In addition, the following named attributes must be set in the returned FeatureDescriptors:
* <ul>
* <li>{@link ELResolver#TYPE} - String.class.</li>
* <li>{@link ELResolver#RESOLVABLE_AT_DESIGN_TIME} - true</li>
* </ul>
*
* @param context
* The context of this evaluation.
* @param base
* The bundle to analyze. Only bases of type ResourceBundle are handled by this
* resolver.
* @return An Iterator containing zero or more (possibly infinitely more) FeatureDescriptor
* objects, each representing a key in this bundle, or null if the base object is not a
* ResourceBundle.
*/
@Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
if (isResolvable(base)) {
final Enumeration<String> keys = ((ResourceBundle) base).getKeys();
return new Iterator<FeatureDescriptor>() {
public boolean hasNext() {
return keys.hasMoreElements();
}
public FeatureDescriptor next() {
FeatureDescriptor feature = new FeatureDescriptor();
feature.setDisplayName(keys.nextElement());
feature.setName(feature.getDisplayName());
feature.setShortDescription("");
feature.setExpert(true);
feature.setHidden(false);
feature.setPreferred(true);
feature.setValue(TYPE, String.class);
feature.setValue(RESOLVABLE_AT_DESIGN_TIME, true);
return feature;
}
public void remove() {
throw new UnsupportedOperationException("Cannot remove");
}
};
}
return null;
}
public static String[] getNullPropertyNames(Object source) {
final BeanWrapper wrappedSource = new BeanWrapperImpl(source);
return Stream.of(wrappedSource.getPropertyDescriptors())
.map(FeatureDescriptor::getName)
.filter(propertyName -> wrappedSource.getPropertyValue(propertyName) == null)
.toArray(String[]::new);
}
@Override
public void attachEnv(PropertyEnv env) {
//System.out.println("ValuePropertyEditor.attachEnv("+env+"), feature descriptor = "+env.getFeatureDescriptor());
env.setState(PropertyEnv.STATE_NEEDS_VALIDATION);
env.addVetoableChangeListener(validate);
if (delegatePropertyEditor instanceof ExPropertyEditor) {
//System.out.println(" attaches to "+delegatePropertyEditor);
if (delegateValue instanceof String) {
ShortenedStrings.StringInfo shortenedInfo = ShortenedStrings.getShortenedInfo((String) delegateValue);
if (shortenedInfo != null) {
// The value is too large, do not allow editing!
FeatureDescriptor desc = env.getFeatureDescriptor();
if (desc instanceof Node.Property){
Node.Property prop = (Node.Property)desc;
// Need to make it uneditable
try {
Method forceNotEditableMethod = prop.getClass().getDeclaredMethod("forceNotEditable");
forceNotEditableMethod.setAccessible(true);
forceNotEditableMethod.invoke(prop);
} catch (Exception ex){}
//editable = prop.canWrite();
}
}
}
((ExPropertyEditor) delegatePropertyEditor).attachEnv(env);
this.env = env;
}
}
private TableCellEditor getCustomEditor( int row ) {
FeatureDescriptor fd = getPropertySetModel().getFeatureDescriptor(row);
if (fd instanceof PropertySet)
return null;
Object res = fd.getValue( "custom.cell.editor"); //NOI18N
if( res instanceof TableCellEditor ) {
prepareCustomEditor( res );
return ( TableCellEditor ) res;
}
return null;
}
@Override
public void actionPerformed(ActionEvent ae) {
FeatureDescriptor fd = _getSelection();
if (fd instanceof PropertySet) {
int row = SheetTable.this.getSelectedRow();
boolean b = getPropertySetModel().isExpanded(fd);
if (b) {
toggleExpanded(row);
}
}
}
/**
* If the base object is not null, returns an Iterator containing the set of JavaBeans properties available on the given object. Otherwise, returns null. The Iterator returned must contain zero or
* more instances of java.beans.FeatureDescriptor. Each info object contains information about a property in the bean, as obtained by calling the BeanInfo.getPropertyDescriptors method. The
* FeatureDescriptor is initialized using the same fields as are present in the PropertyDescriptor, with the additional required named attributes "type" and "resolvableAtDesignTime" set as
* follows:
* <ul>
* <li>{@link ELResolver#TYPE} - The runtime type of the property, from PropertyDescriptor.getPropertyType().</li>
* <li>{@link ELResolver#RESOLVABLE_AT_DESIGN_TIME} - true.</li>
* </ul>
*
* @param context
* The context of this evaluation.
* @param base
* The bean to analyze.
* @return An Iterator containing zero or more FeatureDescriptor objects, each representing a property on this bean, or null if the base object is null.
*/
@Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
if (isResolvable(base)) {
JsonNode node = (JsonNode) base;
final Iterator<String> keys = node.fieldNames();
return new Iterator<FeatureDescriptor>() {
@Override
public boolean hasNext() {
return keys.hasNext();
}
@Override
public FeatureDescriptor next() {
Object key = keys.next();
FeatureDescriptor feature = new FeatureDescriptor();
feature.setDisplayName(key == null ? "null" : key.toString());
feature.setName(feature.getDisplayName());
feature.setShortDescription("");
feature.setExpert(true);
feature.setHidden(false);
feature.setPreferred(true);
feature.setValue(TYPE, key == null ? "null" : key.getClass());
feature.setValue(RESOLVABLE_AT_DESIGN_TIME, true);
return feature;
}
@Override
public void remove() {
throw new UnsupportedOperationException("cannot remove");
}
};
}
return null;
}
public void valueChanged(PropertyEditor editor) {
failed = false;
try {
// System.err.println("ValueChanged - new value " + editor.getValue());
if (getInplaceEditor() != null) {
setEnteredValue(getProperty().getValue());
} else {
//Handle case where our parent PropertyPanel is no longer showing, but
//the custom editor we invoked still is. Issue 38004
PropertyModel mdl = (modelRef != null) ? modelRef.get() : null;
if (mdl != null) {
FeatureDescriptor fd = null;
if (mdl instanceof ExPropertyModel) {
fd = ((ExPropertyModel) mdl).getFeatureDescriptor();
}
String title = null;
if (fd != null) {
title = fd.getDisplayName();
}
failed = PropUtils.updateProp(mdl, editor, title); //XXX
}
}
} catch (Exception e) {
throw (IllegalStateException) new IllegalStateException("Problem setting entered value from custom editor").initCause(e);
}
}
@Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context,
Object base) {
return null;
}
@Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context,
Object base) {
return null;
}
@Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
return null;
}
@Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
return null;
}
@Override
public FeatureDescriptor getDescriptor() {
return new FeatureDescriptor();
}
@Override
public FeatureDescriptor getDescriptor() {
return new FeatureDescriptor();
}
@Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
return null;
}