How to create a generic list with Eclipse EMF? - eclipse

I want to create a class with Eclipse EMF that contains a List with String objects. I see that Ecore has an EList but I can't change the generic type of the list.
Any idea how to do this?

If you want to generate code that gives you an EList<String>, then add a new EAttribute to an EClass, give it the EType EString, and set its "Upper Bound" property to '-1'.
If you want to create such a list programmatically, you could use the BasicEList for example (org.eclipse.emf.common.util.BasicEList<E>):
EList<String> stringList = new BasicEList<String>();
If you want to see your other options, open the type hierarchy on: org.eclipse.emf.common.util.AbstractEList<E>

Not sure if your question was answered, and what you actually want to do.
If you want to generate Java code from an .ecore file, then I provide here an example using the Eclipse Juno's Sample Ecore Model Editor of EMF (right click on the .ecore file).
Maybe it's not directly what you want, but this might be helpful for someone else.
Suppose you want a method like this in your generated Java class MyClass:
<T extends String> EList<T> getListOfType(Class<T> T)
In your Sample Ecore Model Editor you want to achieve How your method looks in the Ecore Editor by
add to MyClass a "New Child" of EOperation, name it getListOfType
add to getListOfType a "New Child" of ETypeParameter, name it T
add to T a "New Child" of EGeneric Bound Type, you would see a "T extends ?" instead of "T"
click the arrow to "T extends ?", click on "?", in "Property" window choose within the drop down menu of EClassifier an EString, now you would see "T extends EString"
add to getListOfType a "New Child" of EGeneric Return Type
click on the newly create "?" of return type, choose within a drop down menu of EClassifier an EEList
open the arrow of EEList, in the Property window choose within a drop down menu of EType Parameter a "T extends EString"
add to getListOfType a "New Child" of "EParameter"
in the property window of the newly created parameter "null", choose Name as "clazz", EType as "EJavaClass"
in the property window of the new "?" (two level below the node "clazz: EJavaClass"), choose EType Parameter as "T extends EString", now "clazz: EJavaClass" becomes "clazz: EJavaClass"
Now youre .ecore file is ready to be used to generate a java class.

Related

How to define a function for use in an ODM decision table w/o changing the XOM?

I'm using ODM 8.5 (the JRules successor). In my Java domain, I have a three-character String field that represents a number, "000" to "999". I'd like to have a decision table that represents logic like:
if field is between "000" and "012" then set the result to "tiny"
if field is between "013" and "060" then set the result to "less tiny"
...
IBM's documentation on defining columns states - "A condition statement is an incomplete BAL predicate expression...". Is there anything in the BAL that does the kind of String comparison I want to do? If not, is it possible to call a function defined in the IRL from the BAL? If so, how? I'm also open to other suggestions on how to handle this simple problem in ODM (without changing the existing Java XOM). Right now, it looks to me that I can't use an ODM decision table, although the underlying logic seems well-suited to a decision table.
This answer is heavily based on Justin Phillips's nice answer to this question, updated for ODM 8.5. Please plus up his answer.
The main idea is to create a function in the Business Object Model (BOM) that can be called from your rules. To add a BOM function:
Right click the bom folder in the Eclipse rules project.
Select New -> BOM Entry from the menu.
Select the Create an empty BOM entry option and then click Finish.
Double click the new BOM entry to bring up the BOM editor view, and
then click New Class.
Enter the class name and then click Finish.
Double click the new BOM class from the list, then under
the Members section, click the New button.
In the New Member
dialog, select the Method option, enter a Name (isBetween),
return Type for the method (boolean), and add three String parameters (testee - the value being tested, min and max). Click the Finish button.
Double click the new method under the Members section, and select the Static and Final options.
Click the Create link under the "Member Verbalization" section and fill in the Template text box with {0} is between {1,min} to {2,max}
Under the BOM to XOM Mapping section, enter your Java code.
11. Go back to the class level BOM editor and set the Execution name to the value void in the "BOM to XOM Mapping" section. This indicates that the BOM class is not linked to a Java class (XOM).
The verbalization for the newly created member should now be accessible when filling out the Test in the Condition Column for the decision table.

How can I get a list of already define contextIds for the extension point org.eclipse.ui.bindings?

I'd like to bind a new keyboard shortcut to an existing context in my plugin.xml. How can I get a list of all known contextIds?
Specifically, I'm looking for a context ID that will bind the shortcut to the Project Explorer.
If you create a key binding in the plugin.xml editor there is a Browse button on the contextId line that will show you a list of know context ids.

How to control the visibility of objectContribution in popupMenus?

Hi guys,I had create a popupmenu with code above and this menu would be only showed if the folder's name matched "AAA".
Here is the problem:if there is a folder which is also named "AAA" after another folder,this menu would be showed,too.Actually we just wanna our popupmenu be showed in specific main folders,not their sub-folders.So how can i do this?
Define an additional "visibility" tag for that contribution and then use a PropertyTester inside the visibility tag to check the name in more detail.

How to get content assist for "for loop" anywhere in Eclipse?

In Eclipse, after a line like this:
List list = new ArrayList();
Typing "for" just beneath, and followed by "ctrl-space" (by default), will bring several options that can help completing this "for loop":
But if the variable "list" is declared far from here (e.g. as a class field) which may not be directly inferred from this context, or there are many Lists declared,then the assistance doesn't work well:
## split line ---
In some cases, Eclipse can assist, but just don't work for member variable. E.g. manually type "another" and ENTER after the ":" didn't persuade Eclipse to guess about it....
(P.S. workable case:
Auto guessed
Entered wanted name, and ENTER, works great
)
Does anyone have any tip to make this assistance work under such scenarios?
I followed Ashutosh Jindal's tip and I managed to configure the template that works (tested with Kepler release). Here it is:
for (${iterable_type:elemType(iterable)} ${iterable_element:newName(iterable_type)} : ${iterable:var(java.lang.Iterable)}) {
${cursor}
}
The main point was to change localVar to var in the template definition (the Eclipse docs clearly explain this).
How to use it:
Print the template name (foreach for default template) and hit Enter. The template will be used with default collection selected by Eclipse (the latest collection declared)
Hit TAB twice to jump to collection element. You will get drop down list with all iterable collections that apply.
Use UP/DOWN arrows to select desired collection, hit Enter. Eclipse will adjust element type and name (very cool).
Click for the screenshot
This works almost as good as Intellij templates. The drawbacks are:
template does not include arrays (as opposed to default foreach template). For arrays you have to define another template.
I have not tried this myself, but take a look at the code template definition. For example, the foreach code template is defined in Preferences -> Java -> Editor -> Templates as follows :
The definition is as follows :
for (${iterable_type} ${iterable_element} : ${iterable}) {
${cursor}
}
Notice the variables being used such as iterable_type.
Now take a look at this Eclipse help page.
There is a variable there called ${id:localVar(type[,type]*)} which is described as follows :
Evaluates to a local variable or parameter visible in the current scope that is a subtype of any of the given type. If no type is specified, any non-primitive local variable matches.
${array} is a shortcut for ${array:localVar(java.lang.Object[])}, but also matches arrays of primitive types.
${collection} is a shortcut for ${collection:localVar(java.util.Collection)}.
${iterable} is a shortcut for ${iterable:localVar(java.lang.Iterable)}, but also matches arrays.
A screenshot of the same :
I believe that if you wanted to increase the scope from which the foreach template infers it's variables, you may have to edit the template definition with the appropriate variable.
Let me know if this helps. Unfortunately, I have not delved into editing the code templates before so shall not be able to give a concrete example.
What I usually do to solve the content assist with for loop is the following:
create a local variable by typing collection variable that is declared far above and a semicolon:
list;
press Ctrl+2 L
Eclipse generates a new local variable declaration which looks like this:
List list2 = list;
type my foreach and autocomplete with Ctrl+space getting the following:
List list2 = list;
for (Object object : list2) {
}
place cursor on list2 in the for loop declaration and press Alt+Shift+I which stands for inline variable.
this results in what you want to achieve. The effort is minimal after some practicing:
for (Object object : list) {
}
Years later, if you enter the name of the field/variable you can press Quick Fix (⌘+1) and in the quick fix menu you can choose different types of for-loops for the collection or array.

transfer netbeans made component to lwuit's "Resource Editor"

I tried to use "table" Component in a Form with "Resource Editor" in lwuit, but I could not add/edit the rows items. so I end up with this question:
Is it possible to create/edit the table component in the generated "StateMachine" class in Netbeans and see the result in "Resource Editor"?
And if it's not possible, how can I hack the .res file in order to make the "table" as it should be?
There is support for live embedding in the resource editor but its deprecated and obtuse.
I suggest you just add a label where you want the custom component to be, give it a unique name and override the method createComponentInstance which is invoked internally with the name of the label (name it something unique).
You can then just return the instance of your custom made component and it will appear where the label is in the GUI builder.