Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to add attributes to an abstract component
How to add attributes to an abstract component [message #757582] Sat, 19 November 2011 21:18 Go to next message
Jens Missing name is currently offline Jens Missing nameFriend
Messages: 27
Registered: November 2011
Junior Member
hi,

I would like to add an "element property" to my abstract component.
Because every component needs the element properties. How can I accomplish this?


Component:
	 Textfield | TextArea | Label 
;



Label:
	'label' name=ID '{' text=STRING properties=ElementProperties?'}'
;

Textfield:
	'textfield' name=ID '{' text=STRING properties=ElementProperties?'}'
;
TextArea:
	'textarea' name=ID '{' 'sizeX' sizeX = INT & 'sizeY' sizeY = INT properties=ElementProperties?'}'
;



I don't want to do this for each component:

	«IF (b.properties.colspan != null)»
	    		«b.name»Constraint.gridwidth = «b.properties.colspan»;
	    	«ENDIF»
	    	
	    	«IF (b.properties.rowspan != null)»
	    		«b.name»Constraint.gridheight = «b.properties.rowspan»;
	    	«ENDIF»
	    	
	    	«IF (b.properties.alignment != null)»
	    		«b.name»Constraint.fill = «b.name»Constraint«b.properties.alignment»;
	    	«ENDIF»



regards,

jens
Re: How to add attributes to an abstract component [message #757586 is a reply to message #757582] Sat, 19 November 2011 21:53 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i do not get your problem. your component eclass gets a name and a properties
so this happens automatically. at least with the grammar you shared.

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to add attributes to an abstract component [message #757587 is a reply to message #757586] Sat, 19 November 2011 22:05 Go to previous messageGo to next message
Jens Missing name is currently offline Jens Missing nameFriend
Messages: 27
Registered: November 2011
Junior Member
hi Christian,

sorry I didn't get that: "your component eclass gets a name and a properties
so this happens automatically"
I wanted to do something like this. Could you give me an example please.

  def setProperties(Component c) '''
	«IF (c.properties.colspan != null)»
    		«c.name»Constraint.gridwidth = «c.properties.colspan»;
    	«ENDIF»
    	
    	«IF (c.properties.rowspan != null)»
    		«c.name»Constraint.gridheight = «c.properties.rowspan»;
    	«ENDIF»
    	
    	«IF (c.properties.alignment != null)»
    		«c.name»Constraint.fill = «c.name»Constraint«c.properties.alignment»;
    	«ENDIF»
    '''




so that I don't have to do it for all my components:..


    def dispatch compile (Button b, Panel p) '''
    	JButton «b.name» = new JButton("«b.text»");
    	«IF (b.actionmessage!=null)»
    	«b.name».addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent e) {
    		JOptionPane.showMessageDialog(null,"«b.actionmessage»");    	
    	}});
		«ENDIF»
		«IF (b.properties != null)»
    		GridBagConstraints «b.name»Constraint = new GridBagConstraints();
	    	«b.name»Constraint.gridx = «b.properties.position.gridX»;
	    	«b.name»Constraint.gridy = «b.properties.position.gridY»;
	    	«p.name».add(«b.name», «b.name»Constraint);
	    	
	    	«IF (b.properties.colspan != null)»
	    		«b.name»Constraint.gridwidth = «b.properties.colspan»;
	    	«ENDIF»
	    	
	    	«IF (b.properties.rowspan != null)»
	    		«b.name»Constraint.gridheight = «b.properties.rowspan»;
	    	«ENDIF»
	    	
	    	«IF (b.properties.alignment != null)»
	    		«b.name»Constraint.fill = «b.name»Constraint«b.properties.alignment»;
	    	«ENDIF»
	    	
	    	
    	«ELSE»
    		«p.name».add(«b.name»);
    	«ENDIF»
		
	    «p.name».add(«b.name»);
    '''
    
    def setProperties(Component c) '''
    	«c.eClass.» ?????????
    '''




regards,
jens

[Updated on: Sat, 19 November 2011 22:09]

Report message to a moderator

Re: How to add attributes to an abstract component [message #757588 is a reply to message #757587] Sat, 19 November 2011 22:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

as i said: this works with the grammar you shared!!!!!
here is the metamodel generated

  <eClassifiers xsi:type="ecore:EClass" name="Component">
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="properties" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
  </eClassifiers>



~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to add attributes to an abstract component [message #757589 is a reply to message #757588] Sat, 19 November 2011 22:22 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
see http://www.eclipse.org/Xtext/documentation/2_1_0/020-grammar-language.php#metamodelInference_4 too

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to add attributes to an abstract component [message #757591 is a reply to message #757588] Sat, 19 November 2011 23:01 Go to previous messageGo to next message
Jens Missing name is currently offline Jens Missing nameFriend
Messages: 27
Registered: November 2011
Junior Member
hi Christian,

I am sorry. I missed "properties=ElementProperties?"
in one of my components. Now it works. Thanks.



But I have still one question:

A construct like this is not possible?

(The advantage of this would be, that I don't have to write
"properties=ElementProperties?" in each of my components.)


Component:
	 (Textfield | TextArea)
properties=ElementProperties?
;

Textfield:
	'textfield' name=ID '{' text=STRING'}'
;
TextArea:
	'textarea' name=ID '{' 'sizeX' sizeX = INT & 'sizeY' sizeY = INT '}'
;




regards,

jens.

Re: How to add attributes to an abstract component [message #757592 is a reply to message #757591] Sat, 19 November 2011 23:10 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
it actually is possible

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:How to decide which generator to use
Next Topic:[Xtext 2.1.1][SOLVED] create custom loop scope
Goto Forum:
  


Current Time: Thu Apr 18 02:16:33 GMT 2024

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

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

Back to the top