[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.modeling.gmf] Re: integer arrays

Martin,

Comments below.

Martin wrote:
Hi,

I need to use one and two dimensional integer arrays in my application and I would appreciate it if anyone could advise me of the best way to do this.
Sounds more like an EMF question than a GMF one.

What I am trying to to is define arrays to minimise any custom programming that I have to do in order to edit these parameters and to serialise them to XML.
You've looked at how EByteArray is supported?

I have done some experiments, explained below, but none of them give the ideal result so I would appreciate ideas about how I could improve this.


My first attempt is to define an array like this:

/**
* @model containment="true"
*/
int[] getOneDArray();

This generates an entry in the model.ecore file:
IntArray [int[]]
I assume this entry means that I have to write the code for this type myself.
Yes, you'd have to implement the two methods in the XyzFactoryImpl for doing createIntArrayFromString and convertIntArrayToString. A space separated list ought to work nicely.

When I generate the code it has the following error: cannot cast from int[] to InternalEObject this seems to be because I set containment="true" so I tried it without:
Yes, containment is only for EReferences and you really need an EDataType since, as suggested, an int array cannot be an EObject.

/**
* @model
*/
int[] getOneDArray();
This generates an entry in the model.ecore file:
<.> IntArray [int[]]
Yes an EDataType is what you need.

The code generated OK, but when I used the application it just treated this like a single string rather than an integer array.
Yes, that's how it will be serialized.
Since editing the parameter and serialising it to XML needs the values in string form I thought it would be easier to just treat it as an array of strings and then convert to array when I need to use it in my application. So I tried this:

/**
* @model
*/
EList<String> getStringArray();
Why not just have a multi-valued Integer feature then?

This works as I want, of course I will have to write code to validate user input and also do the Integer.parseInt when I need to use. The thing that concerns me more is that the UML diagram will not correctly show that this is is an integer array.
Because it's not. :-P
Also this seems to give a mismatch between the model required for the application and the diagram.

An even more difficult question is how to work with two dimensional arrays. This won't work:

/**
* @model
*/
int[][] getTwoDArray();
Why won't it work? Of course no matter what you do, it will be serialized and treated as a single value.

and this won't work either:

/**
* @model
*/
EList<EList<String>> getTwoDArray();
Here you'd need at least an EDataType to represent the inner EList<String> and those would be treated as single values.

The only way that I can to do this is to intersperse a complete new class called, say, 'row' then I can have a single dimensional array of rows, and each row contains a a single dimensional array of elements. Bus again this seems to give a mismatch between the model required for the application and the diagram.
The other thing that I would like to do is show these arrays directly in the diagram. Is it possible to put a jface TableViewer directly in the rectangular box in the diagram?
It seems to me you should try to get EDataType for int[] and int[][] working since that matches what you actually need in the Java API.

Martin