List of usage examples for java.beans PropertyDescriptor getDisplayName
public String getDisplayName()
From source file:corner.util.BeanUtils.java
/** * ?get????//from ww w . j av a 2 s.co m * @param bean ? */ public static List getPropertyMethodNames(Object bean) { Class ObjectClass = EntityService.getEntityClass(bean); List<String> propertys = new ArrayList<String>(); PropertyDescriptor[] pds = org.apache.commons.beanutils.PropertyUtils.getPropertyDescriptors(ObjectClass); for (PropertyDescriptor pd : pds) { propertys.add(pd.getDisplayName()); } return propertys; }
From source file:com.sparkplatform.api.core.PropertyAsserter.java
/** * See {@link #assertBasicGetterSetterBehavior(Object,String)} method. Big difference here is that we try to * automatically introspect the target object, finding read/write properties, and automatically testing the getter * and setter. Note specifically that read-only properties are ignored, as there is no way for us to know how to set * the value (since there isn't a public setter). * <p/>//from w ww .ja v a 2 s. c o m * Any property names contained in the blacklist will be skipped. * <p/> * * @param target the object on which to invoke the getter and setter * @param propertyNames the list of property names that should not be tested */ public static void assertBasicGetterSetterBehaviorWithBlacklist(Object target, String... propertyNames) { List<String> blacklist = Arrays.asList(propertyNames); try { BeanInfo beanInfo = Introspector.getBeanInfo(target.getClass()); PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor descriptor : descriptors) { if (descriptor.getWriteMethod() == null) { continue; } if (!blacklist.contains(descriptor.getDisplayName())) { assertBasicGetterSetterBehavior(target, descriptor.getDisplayName()); } } } catch (IntrospectionException e) { fail("Failed while introspecting target " + target.getClass()); } }
From source file:edu.harvard.med.screensaver.model.AbstractEntityInstanceTest.java
/** * Subclasses should call this method to build their TestSuite, as it will * include tests for the test methods declared in this class, as well as tests * for each entity property found in the specified AbstractEntity class. * //from w w w. j a va 2 s . c o m * @param entityTestClass * @param entityClass * @return */ public static TestSuite buildTestSuite(Class<? extends AbstractEntityInstanceTest> entityTestClass, Class<? extends AbstractEntity> entityClass) { TestSuite testSuite = new TestSuite(entityTestClass); BeanInfo beanInfo; try { beanInfo = Introspector.getBeanInfo(entityClass); // add all the property-specific tests for this entity class for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) { if (propertyDescriptor.getName().equals("class")) { log.debug("not creating test for \"class\" property " + propertyDescriptor.getDisplayName()); } else if (ModelIntrospectionUtil.isTransientProperty(propertyDescriptor)) { log.debug("not creating test for transient (non-persistent) property " + propertyDescriptor.getDisplayName()); } else /*if (ModelIntrospectionUtil.isToManyEntityRelationship(propertyDescriptor))*/ { propertyDescriptor = new GenericTypeAwarePropertyDescriptor(entityClass, propertyDescriptor); testSuite.addTest(new EntityPropertyTest(entityClass, propertyDescriptor)); } } } catch (IntrospectionException e) { e.printStackTrace(); fail(e.getMessage()); } return testSuite; }
From source file:net.sf.ij_plugins.util.DialogUtil.java
/** * Utility to automatically create ImageJ's GenericDialog for editing bean properties. It uses BeanInfo to extract * display names for each field. If a fields type is not supported irs name will be displayed with a tag * "[Unsupported type: class_name]".//from ww w . j a va 2s .co m * * @param bean * @return <code>true</code> if user closed bean dialog using OK button, <code>false</code> otherwise. */ static public boolean showGenericDialog(final Object bean, final String title) { final BeanInfo beanInfo; try { beanInfo = Introspector.getBeanInfo(bean.getClass()); } catch (IntrospectionException e) { throw new IJPluginsRuntimeException("Error extracting bean info.", e); } final GenericDialog genericDialog = new GenericDialog(title); // Create generic dialog fields for each bean's property final PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); try { for (int i = 0; i < propertyDescriptors.length; i++) { final PropertyDescriptor pd = propertyDescriptors[i]; final Class type = pd.getPropertyType(); if (type.equals(Class.class) && "class".equals(pd.getName())) { continue; } final Object o = PropertyUtils.getSimpleProperty(bean, pd.getName()); if (type.equals(Boolean.TYPE)) { boolean value = ((Boolean) o).booleanValue(); genericDialog.addCheckbox(pd.getDisplayName(), value); } else if (type.equals(Integer.TYPE)) { int value = ((Integer) o).intValue(); genericDialog.addNumericField(pd.getDisplayName(), value, 0); } else if (type.equals(Float.TYPE)) { double value = ((Float) o).doubleValue(); genericDialog.addNumericField(pd.getDisplayName(), value, 6, 10, ""); } else if (type.equals(Double.TYPE)) { double value = ((Double) o).doubleValue(); genericDialog.addNumericField(pd.getDisplayName(), value, 6, 10, ""); } else { genericDialog.addMessage(pd.getDisplayName() + "[Unsupported type: " + type + "]"); } } } catch (IllegalAccessException e) { throw new IJPluginsRuntimeException(e); } catch (InvocationTargetException e) { throw new IJPluginsRuntimeException(e); } catch (NoSuchMethodException e) { throw new IJPluginsRuntimeException(e); } // final Panel helpPanel = new Panel(); // helpPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0)); // final Button helpButton = new Button("Help"); //// helpButton.addActionListener(this); // helpPanel.add(helpButton); // genericDialog.addPanel(helpPanel, GridBagConstraints.EAST, new Insets(15,0,0,0)); // Show modal dialog genericDialog.showDialog(); if (genericDialog.wasCanceled()) { return false; } // Read fields from generic dialog into bean's properties. try { for (int i = 0; i < propertyDescriptors.length; i++) { final PropertyDescriptor pd = propertyDescriptors[i]; final Class type = pd.getPropertyType(); final Object propertyValue; if (type.equals(Boolean.TYPE)) { boolean value = genericDialog.getNextBoolean(); propertyValue = Boolean.valueOf(value); } else if (type.equals(Integer.TYPE)) { int value = (int) Math.round(genericDialog.getNextNumber()); propertyValue = new Integer(value); } else if (type.equals(Float.TYPE)) { double value = genericDialog.getNextNumber(); propertyValue = new Float(value); } else if (type.equals(Double.TYPE)) { double value = genericDialog.getNextNumber(); propertyValue = new Double(value); } else { continue; } PropertyUtils.setProperty(bean, pd.getName(), propertyValue); } } catch (IllegalAccessException e) { throw new IJPluginsRuntimeException(e); } catch (InvocationTargetException e) { throw new IJPluginsRuntimeException(e); } catch (NoSuchMethodException e) { throw new IJPluginsRuntimeException(e); } return true; }
From source file:dhbw.ka.mwi.businesshorizon2.ui.process.period.input.AbstractInputPresenter.java
/** * Sorgt dafuer, dass aenderungen in das Periodenobjekt geschrieben werden. * ueberprueft die Benutzereingabe auf ihre Konvertierbarkeit in eine * Doublevariable und gibt im Fehlerfall eine Fehlermeldung an den User * zurueck./* w w w . j a va 2 s .c om*/ * * @param newContent * Inhalt des Textfeldes das in das Periodenobjekt geschrieben * werden soll * @param textFieldColumn * Spalte des GridLayouts wo das Textfeld liegt * @param textFieldRow * Reihe des GridLayouts wo das Textfeld liegt * @param destination * Name der Property in welche newContent geschrieben werden soll */ public void validateChange(String newContent, int textFieldColumn, int textFieldRow, String destination) { destination = shownProperties[Arrays.asList(germanNamesProperties).indexOf(destination)]; logger.debug("" + newContent); try { df.parse(newContent).doubleValue(); df.parse(newContent).doubleValue(); } catch (Exception e) { getView().setWrong(textFieldColumn, textFieldRow, true); return; } getView().setWrong(textFieldColumn, textFieldRow, false); for (PropertyDescriptor pd : BeanUtils.getPropertyDescriptors(period.getClass())) { if (Arrays.asList(shownProperties).contains(destination)) { if (pd.getDisplayName().equals(destination)) { try { pd.getWriteMethod(); period.toString(); pd.getWriteMethod().invoke(period, new Object[] { df.parse(newContent).doubleValue() }); logger.debug("Content should be written: " + (double) pd.getReadMethod().invoke(period)); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | ParseException e) { e.printStackTrace(); } } } } }
From source file:dhbw.ka.mwi.businesshorizon2.ui.process.period.input.AbstractInputPresenter.java
@Override public boolean isValid() { if (period != null) { try {/*from ww w . j a v a 2 s. c om*/ ArrayList<String> wrongFields = new ArrayList<String>(); boolean anyFalse = false; wrongFields.add(period.getYear() + ""); for (PropertyDescriptor pdr : Introspector.getBeanInfo(period.getClass(), Object.class) .getPropertyDescriptors()) { if (Arrays.asList(shownProperties) .contains(pdr.getDisplayName().substring(0, pdr.getDisplayName().length() - 3))) { if (!(boolean) pdr.getReadMethod().invoke(period)) { wrongFields.add(pdr.getDisplayName().substring(0, pdr.getDisplayName().length() - 3)); anyFalse = true; } } } if (anyFalse) { eventBus.fireEvent(new WrongFieldsEvent(wrongFields)); return false; } } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IntrospectionException e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; } else return false; }
From source file:dhbw.ka.mwi.businesshorizon2.ui.process.period.input.AbstractInputPresenter.java
public void processEvent(ShowInputViewEventInterface event) { logger.debug("ShowDirektViewEvent erhalten"); period = event.getPeriod();/* ww w . ja va2s . c om*/ getView().initForm(); getView().addHeader(period.getYear()); try { for (PropertyDescriptor pd : Introspector.getBeanInfo(period.getClass(), Object.class) .getPropertyDescriptors()) { logger.debug("Processing: " + pd.getName()); if (Arrays.asList(shownProperties).contains(pd.getDisplayName())) { try { String germanName; germanName = germanNamesProperties[Arrays.asList(shownProperties) .indexOf(pd.getDisplayName())]; boolean skipInitialContent = true; for (PropertyDescriptor pdr : Introspector.getBeanInfo(period.getClass(), Object.class) .getPropertyDescriptors()) { if ((pd.getDisplayName() + "Set").equals(pdr.getDisplayName())) { skipInitialContent = !(boolean) pdr.getReadMethod().invoke(period); logger.debug("method found and skipInitialContent set to " + skipInitialContent); } } if (skipInitialContent) { getView().addInputField(germanName); logger.debug("initialContent skipped"); } else { getView().addInputField(germanName, (double) pd.getReadMethod().invoke(period)); logger.debug("initialContent written"); } } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); } } } } catch (IntrospectionException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:net.sf.taverna.t2.workbench.ui.servicepanel.actions.AddServiceProviderAction.java
protected JPanel buildEditor(Configuration configuration) { List<String> uiBuilderConfig = new ArrayList<>(); int lastPreferred = 0; for (PropertyDescriptor property : getProperties(configuration)) { if (property.isHidden() || property.isExpert()) // TODO: Add support for expert properties continue; String propertySpec = property.getName() + ":name=" + property.getDisplayName(); if (property.isPreferred()) // Add it to the front uiBuilderConfig.add(lastPreferred++, propertySpec); else// w w w . j a v a 2 s . com uiBuilderConfig.add(propertySpec); } return UIBuilder.buildEditor(configuration, uiBuilderConfig.toArray(new String[0])); }
From source file:org.walkmod.conf.entities.impl.ConfigurationImpl.java
private List<PropertyDefinition> getProperties(Object o) { List<PropertyDefinition> result = new LinkedList<PropertyDefinition>(); PropertyDescriptor[] properties = BeanUtils.getPropertyDescriptors(o.getClass()); if (properties != null) { for (PropertyDescriptor pd : properties) { if (pd.getWriteMethod() != null) { String name = pd.getDisplayName(); Class<?> clazz = pd.getPropertyType(); String type = clazz.getSimpleName(); String value = ""; if (String.class.isAssignableFrom(clazz) || Number.class.isAssignableFrom(clazz) || clazz.isPrimitive()) { if (pd.getReadMethod() != null) { try { value = pd.getReadMethod().invoke(o).toString(); } catch (Exception e) { }//from w w w .j a v a2 s. com } else { Field[] fields = o.getClass().getDeclaredFields(); boolean found = false; for (int i = 0; i < fields.length && !found; i++) { if (fields[i].getName().equals(name)) { found = true; fields[i].setAccessible(true); try { value = fields[i].get(o).toString(); } catch (Exception e) { } } } } } PropertyDefinition item = new PropertyDefinitionImpl(type, name, value); result.add(item); } } } return result; }
From source file:com.twinsoft.convertigo.beans.core.MySimpleBeanInfo.java
protected PropertyDescriptor getPropertyDescriptor(String name) throws IntrospectionException { checkAdditionalProperties();/*from w ww . java 2s .c o m*/ for (int i = 0; i < properties.length; i++) { PropertyDescriptor property = properties[i]; if (name.equals(property.getName())) { PropertyDescriptor clone = new PropertyDescriptor(name, property.getReadMethod(), property.getWriteMethod()); clone.setDisplayName(property.getDisplayName()); clone.setShortDescription(property.getShortDescription()); clone.setPropertyEditorClass(property.getPropertyEditorClass()); clone.setBound(property.isBound()); clone.setConstrained(property.isConstrained()); clone.setExpert(property.isExpert()); clone.setHidden(property.isHidden()); clone.setPreferred(property.isPreferred()); for (String attributeName : Collections.list(property.attributeNames())) { clone.setValue(attributeName, property.getValue(attributeName)); } return properties[i] = clone; } } return null; }