Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[sapphire-dev] New topic in forum Sapphire, called null context in PossibleValuesService, by Amaury TETREL

Title: Eclipse Community Forums
Subject: null context in PossibleValuesService Author: Amaury TETREL Date: Tue, 22 December 2015 16:06
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
[ Reply ][ Quote ][ View Topic/Message ][ Unsubscribe from this forum ]

Back to the top