null context in PossibleValuesService [message #1718341] |
Tue, 22 December 2015 16:06  |
Eclipse User |
|
|
|
Hello,
I created a custom annotation, "ReferentielPossibleValues"
@XmlBinding(path = "@code")
@Label(standard = "Code")
@Required
// @Service(impl = ReferentielPossibleValueService.class, params =
// @Service.Param(name = "reference", value = "application"))
@ReferentielPossibleValues(ref = "application")
ValueProperty PROP_CODE = new ValueProperty(TYPE, "Code");
String getCode();
void setCode(String value);
On Sapphire extenxion I put the folowing code :
<service>
<implementation>fr.cats.mos.editor.service.ReferentielPossibleValueService
</implementation>
<condition>fr.cats.mos.editor.service.ReferentielPossibleValueService$Condition
</condition>
<id>referentiel-possible-values</id>
<context>Sapphire.Property.MetaModel</context>
</service>
I would like to update the possible values for this "Code" element using a IResourceChangeListener.
My custom class ReferentielPossibleValueService extends PossibleValuesService and IResourceChangeListener.
However, eachj time I'm trying to use this listener to update the available values, I'm getting a NullPointerException on the context() element. I think I forgot to initialize something but I don't really know what.
Here is the entire class code :
public class ReferentielPossibleValueService extends PossibleValuesService
implements IResourceChangeListener {
private static DocumentBuilder DOCUMENT_BUILDER;
private Set<String> getPossibleValues(String reference)
throws SAXException, IOException {
// getting values from XML file
return values
}
@Override
public void resourceChanged(IResourceChangeEvent iResourceChangeEvent) {
if (iResourceChangeEvent.getType() == IResourceChangeEvent.POST_CHANGE) {
try {
initPossibleValuesService();
Set<String> values = getPossibleValues("application");
System.out.println(values.toString());
compute(values);
} catch (SAXException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
protected void initPossibleValuesService() {
super.initPossibleValuesService();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
try {
DOCUMENT_BUILDER = documentBuilderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
Sapphire.service(LoggingService.class).log(e);
throw new IllegalStateException(e);
}
}
public static final class Condition extends ServiceCondition {
@Override
public boolean applicable(final ServiceContext context) {
final Property property = context.find(Property.class);
if (property instanceof Value || property instanceof ElementList) {
final ReferentielPossibleValues possibleValuesAnnotation = property
.definition().getAnnotation(
ReferentielPossibleValues.class);
return (possibleValuesAnnotation != null && possibleValuesAnnotation
.ref().length() > 0);
}
return false;
}
}
@Override
protected void compute(Set<String> values) {
final ReferentielPossibleValues a = context(PropertyDef.class)
.getAnnotation(ReferentielPossibleValues.class);
values.clear();
try {
values.addAll(this.getPossibleValues(a.ref()));
} catch (SAXException | IOException e) {
Sapphire.service(LoggingService.class).log(e);
throw new IllegalStateException(e);
}
}
}
I hope someone here will be able to answer my question.
Thank you, and merry christmas ! =)
Amaury
|
|
|
Re: null context in PossibleValuesService [message #1718344 is a reply to message #1718341] |
Tue, 22 December 2015 17:42   |
Eclipse User |
|
|
|
You didn't include the code that shows how you are attaching your resource change listener, but I suspect that you are using a service instance that hasn't been created and activated by the framework.
The issue is that you are registering it as a MetaModel service, which means it has no access to model instance, but in your service condition you are looking up Property.class (an instance of a property) from the context. You need to change it to lookup PropertyDef.class instead.
The best place to attach external listeners, such as resource change listeners, is during service init. Then don't forget to detach the listener during service dispose.
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05067 seconds