How to set a Java bean property as an expert property? - java

The Java Beans Introspection API includes in the PropertyDescriptor class the method isExpert. Bean-based GUI editors, like WindowsMaker, use this to hide or show "exotic" properties.
What causes a certain property of a Java bean to be considered "expert"? How does the Swing library, for example, sets certain properties as "expert properties"? How can I programmatically do the same for Java beans that I write?

This apparently is done in the JDK using a non-standard compiler extension.
If you have a look at the Swing source code, some Javadoc comments include a #beaninfo tag, containing such information:
/**
* ... Some comment ...
* #beaninfo
* bound: true
* expert: true
*/
public void setSomething(SomeType value) {
// ...
}
Here is an example, in the JTable class source code.
I also found this article, talking about the #beaninfo tag.

You can create a BeanInfo class for your bean. This allows you to customize the property descriptor. There is a section covering this in the JavaBean tutorial.

Related

Need to understand broadleaf commerce custom annotation implementation

I have been trying go through code of Broadleaf Commerce. There were multiple Custom Annotations used however I was not able to locate there Processor. Can anyone help me here. To take example #AdminPresentation it a custom annotation in package org.broadleafcommerce.common.presentation;
However how this is processed throughout the app, I was not able to locate. What I have understood till now we can use Reflection or AOP for its processing. But There was nothing for this.
Please help.
Source code - https://github.com/BroadleafCommerce/BroadleafCommerce
For a short answer, org.broadleafcommerce.openadmin.server.dao.provider.metadata.BasicFieldMetadataProvider#addMetadata is one place that processes those annotations.
On a broader level, the controllers in the openadmin will use the AdminEntityService to get ClassMetaData (all data about how a class should be displayed to an admin user). The #AdminPresentation annotation is one source of this data. The method AdminEntityServiceImpl#getClassMetadata is the main gateway for getting the ClassMetaData.
#getClassMetadata calls #inspect and eventually gets to PersistenceManager#inspect. This method uses the DynamicEntityDao to eventually get to Metadata#getFieldMetadataForTargetClass. That method gets each field of a class via reflection, and then each of those Fields is processed through the available FieldMetadataProviders. The FieldMetadataProviders turn a java.lang.reflect.Field into org.broadleafcommerce.openadmin.dto.FieldMetadata.
Any provided FieldMetadataProvider can contribute field metadata. This metadata is used in the FormBuilderService to construct the admin page.
Class References:
AdminEntityService - org.broadleafcommerce.openadmin.server.service.AdminEntityServiceImpl
PersistenceManager - org.broadleafcommerce.openadmin.server.service.persistence.PersistenceManagerImpl#inspect
DynamicEntityDao - org.broadleafcommerce.openadmin.server.dao.DynamicEntityDaoImpl#getPropertiesForEntityClass
Metadata - org.broadleafcommerce.openadmin.server.dao.Metadata#getFieldMetadataForTargetClass
FieldMetadataProvider - org.broadleafcommerce.openadmin.server.dao.provider.metadata.FieldMetadataProvider, org.broadleafcommerce.openadmin.server.dao.DynamicEntityDaoImpl#fieldMetadataProviders
FormBuilderService - org.broadleafcommerce.openadmin.web.service.FormBuilderServiceImpl
Have a look to :
https://www.baeldung.com/java-custom-annotation
You will get explanations about : "default" in the Custom Annotations.
Florent COUDERT.

Enterprise Architect - MDA converst Map to List

I am using EA for creation of PIM. When I generate java code directly I am getting correct data type where I want - Map where I set qualifiers on association properties which as I understand meas that it is going to be a map. And that works as expected. Hovewer when I do the MDA transformation and generete code - properties are conveted to List (which is bad) bug setters and getters method keep using Map as in the following example:
public class Check {
private List< Comp> comps;
private List< Gratuity> gratuities;
public Check(){
}
public Map<String, Comp> getcomps(){
return comps;
}
public Map<String, Gratuity> getgratuities(){
return gratuities;
}
I am using default transformation package for Java. I tried to add following line to Java transformation for connector is source section
%connectorType%
%PI="\n "%
{
%TRANSFORM_CURRENT()%
%TRANSFORM_REFERENCE("Connector",connectorGUID)%
Source
{
%TRANSFORM_REFERENCE("Class",connectorSourceElemGUID)%
access=%qt%%connectorSourceAccess == "Public" ? "Private" : value%%qt%
qualifier=%connectorSourceQualifier%
%TRANSFORM_CURRENT("Source","access")%
}
Target
{
%TRANSFORM_REFERENCE("Class",connectorDestElemGUID)%
access=%qt%%connectorDestAccess == "Public" ? "Private" : value%%qt%
%TRANSFORM_CURRENT("Target","access")%
%PI="\n"%
}
}
but that doesn't seem to help
This is an incomplete answer, but it's too long to go in a comment.
I'm not convinced that the connector source qualifier determines which collection class (Map, List) is used. There are three things involved here: the MDA transform template, the code generation template and the collection class options.
Check Tools -- Options -- Source Code Engineering -- Java. There you'll find settings for Default Collection Class and Additional Collection Classes (these are used for attributes), and (by clicking the Collection Classes button) collection class settings for associations. Check these.
Also, check the Linked Attribute Declaration template for Java code generation. It seems to me that this does not check the qualifier, but it does check %linkAttCollectionClass%.
I got a reply form Enterprise Architect support which says it is bug - original message:
I am sorry it does not work because there's an issue with regard to transformation of the Connector 'qualifier'.
The transformation template '%TRANSFORM_CURRENT()%' (and your new added 'qualifier="tr: String') is all correct, but the issue makes it fail to transform that qualifier value across.
We are going to resolve this issue in a future release of EA. Unfortunately I cannot provide a timeframe for release.
Issue ID: 13106266

REQ: Retrieving properties in my java app. collected by "PropertyPlaceholderConfigurer" -- properties are stored in a database as key/value

PUsing Spring 3.2.0.release and JDK 1.6. I've a standalone Java program (NOT running inside tomcat etal) and I'm loading properties from a database.
I've used this excellent article as a base and it works perfectly. Using the PropertiesPrinter bean (defined there) as a base and adding getters I can do stuff like getFileLocation(), getPetDogsName() but then I need to have/create setter/getters for every property.
What I would like to have is a Spring Bean or normal Java class called DatabaseProperties with a method like getProperty("filelocation"); which I can use in my application (main)and so I can retrieve/get the value of the property filelocation which is somewhere inside the information collected by PropertyPlaceholderConfigurer.
I've done a lot of digging but can't seem to find the information I need or at least I'm not able to combine the gathered info into a working program as I'm not fluent with Spring....
Any hint/pointers/urls/code is higly appreciated. It's probably relative easy but it is still out of reach for me atm.
One solution for reading values set by the PropertyPlaceholderConfigurer, is to use the #Value annotation rather than a method for setting class member variables:
class MyClass {
#Value("${file.location}")
private String fileLocation;
...
}

How to cleanly deal with structured selections in eclipse?

I'm writing an eclipse plugin, where I want to contribute an action to the JDT package explorer. In order for the action to be executed, there must be two different files selected. Therefore I retrieve the active selection of the JDT package explorer in the command handler for that action. That is where my problem is.
Currently the code that extracts the selected files from IStructuredSelection involves multiple local variables with multiple if statements and multiple returns in about 30 lines of code. And of course this code looks at least a bit ugly.
What concept(s) and or patterns should I use to make this code more cleaner?
The eclipse platform recommends to use the adapter pattern. So I thought of creating a pojo like this:
public class FooCommandArgs {
private IFile xmlFile;
private IFile csvFile;
//getters and setters here ...
}
and creating the necessary adapters for it from IStructuredSelection. However doing this would just move the ugly code to some other places.
So as the field names suggest, each item I want to extract from the IStructuredSelection has to fulfill some conditions. My idea is to use bean validation api for this. The pojo would then look like this:
public class FooCommandArgs {
#NotNull
#FileExtension("xml")
#Content(type=ContentType.XML, value="http://my.schema.location/schema.xsd")
private IFile xmlFile;
#NotNull
#FileExtension("csv")
private IFile csvFile;
//getters and setters here ...
}
The Validator interface of the bean validation api provides the method <T> Set<ConstraintViolation<T>> validateValue(Class<T> beanType, String propertyName, Object value, Class<?>... groups) which I could use for that. I just would have to introspect the java bean properties and then invoke that method for every combination of an IStructuredSelection item and pojo property. If the result is that every item could be assigned to a bean property with no constraint violations than I can just go on with handling the actual command. Ambiguities might as well be handled.
EDIT:
I have implemented this suggestion and it works out quite nice. Using this technique, it is also very easy to explain to a user programmatically, why a certain command isn't enabled or can not be executed.
I don't want to forget to mention JCommander at this point, which is the inspiration for this idea.

How to generate locale properties file from my Messages interface?

I am implementing an internationalized GWT application at the moment. I read the related documentation and have created the necessary interface that extends com.google.gwt.i18n.client.Messages. I'd like to use MD5 key generation. The methods are annotated like this:
#DefaultMessage("Demo")
#Description("A menu item's label")
#Meaning("Nothing special so far")
String bucketDemo();
How could I automatically generate the translatable .properties files based on the interface? I see that i18nCreator is for generating the interface and the properties file the same time.
OMG, I missed the point that this file is automatically generated by the GWT Compiler.. :-)

Categories

Resources