Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Sapphire » null context in PossibleValuesService
null context in PossibleValuesService [message #1718341] Tue, 22 December 2015 16:06 Go to next message
Amaury TETREL is currently offline Amaury TETRELFriend
Messages: 9
Registered: December 2015
Junior Member
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 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin KomissarchikFriend
Messages: 1077
Registered: July 2009
Senior Member
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.
Re: null context in PossibleValuesService [message #1718381 is a reply to message #1718344] Wed, 23 December 2015 07:57 Go to previous messageGo to next message
Amaury TETREL is currently offline Amaury TETRELFriend
Messages: 9
Registered: December 2015
Junior Member
Thank you for your answer Konstantin.

I tried to attach my listener using attach(Listener l) method, but looks like a ResourceChangedListener can't be attached with this method.
How can I attach such a listener ?
Re: null context in PossibleValuesService [message #1718394 is a reply to message #1718381] Wed, 23 December 2015 10:33 Go to previous message
Amaury TETREL is currently offline Amaury TETRELFriend
Messages: 9
Registered: December 2015
Junior Member
I finally found what was wrong in my code.

Thank you for your help =)
Previous Topic:Get the current Element as Java Object
Next Topic:ActionHandlerFilter to all sections
Goto Forum:
  


Current Time: Sat Sep 22 20:55:02 GMT 2018

Powered by FUDForum. Page generated in 0.01769 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top