Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » WindowBuilder » How add restriction to add a new component
How add restriction to add a new component [message #901832] Tue, 14 August 2012 17:37 Go to next message
Arthur Fücher is currently offline Arthur FücherFriend
Messages: 59
Registered: August 2012
Member
Hi,
I made a new component extending a SashForm, just to start to work with toolkit and window builder. My component just make a new SashForm, but it always do a HORIZONTAL style in the constructor. The component is working!
Now, I want to do some validations when the user try to add a new component in my "SashForm".
How I do that? When the user is trying to add a new component, in the icon of the mouse stay a green or red icon, how can I intercept this?

[]'s
Re: How add restriction to add a new component [message #901838 is a reply to message #901832] Tue, 14 August 2012 18:02 Go to previous messageGo to next message
Konstantin Scheglov is currently offline Konstantin ScheglovFriend
Messages: 555
Registered: July 2009
Senior Member
SashForm is done as "flow container".
		<!-- flow container -->
		<parameter name="flowContainer">true</parameter>
		<parameter name="flowContainer.horizontal">isHorizontal()</parameter>
		<parameter name="flowContainer.component">org.eclipse.swt.widgets.Control</parameter>


So, your component description file (*.wbp-component.xml) could provide "component-validator" to check component better than just "component" (which checks just type).

There are examples of this in org.eclipse.wb.rcp plugin.


Konstantin Scheglov,
Google, Inc.
Re: How add restriction to add a new component [message #901839 is a reply to message #901838] Tue, 14 August 2012 18:14 Go to previous messageGo to next message
Arthur Fücher is currently offline Arthur FücherFriend
Messages: 59
Registered: August 2012
Member
Hi Konstantin,

I do this implementation just to test:

<parameter name="flowContainer.component-validator">false</parameter>

If I understand what you say, it will always return false and I could't add a new component to my "SashForm".
But doesn't work... I don't understand what you say, or I implemented wrong?

<!-- PARAMETERS -->
<parameters>
<parameter name="layout.has">false</parameter>
<!-- border -->
<parameter name="shouldDrawBorder">true</parameter>
<!-- flow container -->
<parameter name="flowContainer">true</parameter>
<parameter name="flowContainer.horizontal">isHorizontal()</parameter>
<parameter name="flowContainer.component">org.eclipse.swt.widgets.Control</parameter>
<parameter name="flowContainer.component-validator">false</parameter>
</parameters>

Thanks!
Re: How add restriction to add a new component [message #901841 is a reply to message #901839] Tue, 14 August 2012 18:21 Go to previous messageGo to next message
Konstantin Scheglov is currently offline Konstantin ScheglovFriend
Messages: 555
Registered: July 2009
Senior Member
Where do you place this text?
It should be in same package as component, in the file with the same name, but with extension *.wbp-component.xml instead of Java.


Konstantin Scheglov,
Google, Inc.
Re: How add restriction to add a new component [message #901843 is a reply to message #901841] Tue, 14 August 2012 18:26 Go to previous messageGo to next message
Arthur Fücher is currently offline Arthur FücherFriend
Messages: 59
Registered: August 2012
Member
Sorry...I make a mistake, and it's working!!

Thank you..

just one more question, there is a way to I do this restriction in the object that I'm adding?

For example, a item menu just can be added in a menu..is the same parameter?

And, there is any place that have a complete documentation about all the parameters that could I use in de *.wbp-component.xml?
Re: How add restriction to add a new component [message #901845 is a reply to message #901843] Tue, 14 August 2012 18:35 Go to previous messageGo to next message
Arthur Fücher is currently offline Arthur FücherFriend
Messages: 59
Registered: August 2012
Member
Konstatin,

How can I call a method of my class to validate this?
<parameter name="flowContainer.component-validator">canAdd()</parameter>

I do that, but this generate this error in console:

[Error: unable to access property (null parent): canAdd]
Re: How add restriction to add a new component [message #901846 is a reply to message #901843] Tue, 14 August 2012 18:36 Go to previous messageGo to next message
Konstantin Scheglov is currently offline Konstantin ScheglovFriend
Messages: 555
Registered: July 2009
Senior Member
These are advanced topics, they are not covered by any documentation.
There is some internal documentation (also may be not 100% complete, but most things are covered) for *.wbp-component.xml files
http://dev.eclipse.org/svnroot/tools/org.eclipse.windowbuilder/trunk/org.eclipse.wb.core/development_doc/
You need Designer_2.0_descriptions.pdf

See
org.eclipse.wb.core.gef.policy.validator.LayoutRequestValidators.mixWithMandatory(ILayoutRequestValidator)
org.eclipse.wb.core.gef.policy.validator.CompatibleLayoutRequestValidator.areCompatible(EditPart, Object)
org.eclipse.wb.core.gef.policy.validator.CompatibleLayoutRequestValidator.childAgreeToBeDroppedOnParent(Object, Object)

i.e. "GEF.requestValidator.child" is *.wbp-component.xml
For example
		<!-- GEF validators -->
		<parameter name="GEF.requestValidator.child"><![CDATA[
			def existChildrenByType(type) {
			  foreach(child : parent.getChildren()){
			    if(isComponentType(child,type)){
			      return true;
			    }
			  }
			  return false;
			}
			existChildrenByType("org.eclipse.jface.viewers.Viewer")
			]]></parameter>
	</parameters>


Konstantin Scheglov,
Google, Inc.

[Updated on: Tue, 14 August 2012 18:37]

Report message to a moderator

Re: How add restriction to add a new component [message #901848 is a reply to message #901845] Tue, 14 August 2012 18:39 Go to previous messageGo to next message
Konstantin Scheglov is currently offline Konstantin ScheglovFriend
Messages: 555
Registered: July 2009
Senior Member
You have "parent" and "child" variables in these scripts.
Probably you need "child.canAdd()" in this case.


Arthur Fücher wrote on Tue, 14 August 2012 14:35
Konstatin,

How can I call a method of my class to validate this?
<parameter name="flowContainer.component-validator">canAdd()</parameter>

I do that, but this generate this error in console:

[Error: unable to access property (null parent): canAdd]



Konstantin Scheglov,
Google, Inc.
Re: How add restriction to add a new component [message #901851 is a reply to message #901848] Tue, 14 August 2012 18:48 Go to previous messageGo to next message
Arthur Fücher is currently offline Arthur FücherFriend
Messages: 59
Registered: August 2012
Member
I used child and return this in the console:
[Error: could not access: child; in class: null]

in my class I do this:
public boolean canAdd(){
return false;
}


This method I need to create in the class or in the Info class?
Re: How add restriction to add a new component [message #901854 is a reply to message #901851] Tue, 14 August 2012 19:00 Go to previous messageGo to next message
Konstantin Scheglov is currently offline Konstantin ScheglovFriend
Messages: 555
Registered: July 2009
Senior Member
Ah... Yes, this method should be in the Info class.
And at the time of creation infoInstance.getObject() will return null.
So, you can not call method of the class.
But if you create it and you provided *.wbp-component.xml for it, you could set parameters for creation to check if this creation is valid to be added to the container.


Konstantin Scheglov,
Google, Inc.
Re: How add restriction to add a new component [message #901859 is a reply to message #901854] Tue, 14 August 2012 19:12 Go to previous messageGo to next message
Arthur Fücher is currently offline Arthur FücherFriend
Messages: 59
Registered: August 2012
Member
Ok,

To explain what I'm doing: I have 2 classes extends SashForm: HorizontalBox and VertivalBox. To start, I want to allow the user just add a Horizontal in a Vertical, and a Vertical in a Horizontal...
For this I do this implementation:
<parameter name="flowContainer.component-validator"><![CDATA[!isComponentType('com.test.fw.wb.mvc.core.HorizontalBox')]]>

But I want to do more...I want to allow the user just add 2 children for each "Box", to do this I need to access a method...right? Or can I do it otherwise?
Re: How add restriction to add a new component [message #901862 is a reply to message #901859] Tue, 14 August 2012 19:22 Go to previous messageGo to next message
Konstantin Scheglov is currently offline Konstantin ScheglovFriend
Messages: 555
Registered: July 2009
Senior Member
If you want to limit number of children, this is actually "component-validator", not "GEF.requestValidator.child", because this is parent imposed limitation, not children.
And parent is actually always accessible, both Info and its toolkit Object.
Here is part of Nebula CompositeTable.


<parameter name="flowContainer.component-validator"><![CDATA[ReflectionUtils.getFullyQualifiedName(component.description.componentClass,false) == 'org.eclipse.swt.widgets.Composite' && container.getChildren().size() < 2]]></parameter>

So, something like "container.getChildren().size() < 2" could work.
Only trick is if we move same component inside, we should not include moving component into these "2" components.
But again, if this is creation, then "component.object === null", i.e. we don't have object yet.

Note that for flow containers "container" and "component" variables are available.
Not "parent" and "child".

Also, "ReflectionUtils.getFullyQualifiedName" is not the best way to check type.


Konstantin Scheglov,
Google, Inc.

[Updated on: Tue, 14 August 2012 19:23]

Report message to a moderator

Re: How add restriction to add a new component [message #901870 is a reply to message #901862] Tue, 14 August 2012 20:50 Go to previous messageGo to next message
Arthur Fücher is currently offline Arthur FücherFriend
Messages: 59
Registered: August 2012
Member
I do what you say, and it's is working, but when I'm adding the second component into the Box the component-validator allows because I have only one. But, the validation is called one more time in:
FlowContainerLayoutEditPolicy [line: 58] - decorateChild(EditPart)
(m_container.validateComponent(child.getModel()))

This time, it's not more valid because I have 2 childrens, and they don't install my SelectionEditPolicy.

The parameter in *.wbp-component.xml is like this:
<parameter name="flowContainer.component-validator"><![CDATA[
!isComponentType('com.test.fw.wb.mvc.core.HorizontalBox') && container.getChildren().size() < 2]]>
</parameter>

I'm doing something wrong? Or missing something?

[Updated on: Tue, 14 August 2012 20:51]

Report message to a moderator

Re: How add restriction to add a new component [message #901874 is a reply to message #901870] Tue, 14 August 2012 21:00 Go to previous messageGo to next message
Konstantin Scheglov is currently offline Konstantin ScheglovFriend
Messages: 555
Registered: July 2009
Senior Member
Ah... You could check in "component-validator" if component is already child of container.
This will solve two problems at once - move component inside of container and decorateChild().


Konstantin Scheglov,
Google, Inc.
Re: How add restriction to add a new component [message #901878 is a reply to message #901874] Tue, 14 August 2012 21:14 Go to previous messageGo to next message
Arthur Fücher is currently offline Arthur FücherFriend
Messages: 59
Registered: August 2012
Member
I try to do this, using component.parent() == container, but it cause this error:
unable to resolve method: org.eclipse.wb.internal.swt.model.widgets.ButtonInfo.parent() [arglength=0]

It's wrong this method? Or it's the problem that the object in the first time we don't have the object?

*I'm trying to add a button!
Re: How add restriction to add a new component [message #901879 is a reply to message #901878] Tue, 14 August 2012 21:15 Go to previous messageGo to next message
Konstantin Scheglov is currently offline Konstantin ScheglovFriend
Messages: 555
Registered: July 2009
Senior Member
Use property access "component.parent" or method call "component.getParent()", but not combination of them Smile

BTW, "component" and "container" are Info instances, not toolkit objects.
So, they exist always.


Konstantin Scheglov,
Google, Inc.

[Updated on: Tue, 14 August 2012 21:16]

Report message to a moderator

Re: How add restriction to add a new component [message #901976 is a reply to message #901879] Wed, 15 August 2012 11:38 Go to previous messageGo to next message
Arthur Fücher is currently offline Arthur FücherFriend
Messages: 59
Registered: August 2012
Member
Thanks Konstatin for help me!

Now it's working, and I will continue in my project...

[]'s
Re: How add restriction to add a new component [message #902008 is a reply to message #901976] Wed, 15 August 2012 13:21 Go to previous messageGo to next message
Arthur Fücher is currently offline Arthur FücherFriend
Messages: 59
Registered: August 2012
Member
Another question...

When I need to do a restriction in "SashForm" I've used flowContainer.component-validator because as you say 'SashForm is done as "flow container"'.. but if I need to do a restriction in a class that extend for example "Shell", how can I know what I need to use in the place of flow container?
Re: How add restriction to add a new component [message #902032 is a reply to message #902008] Wed, 15 August 2012 15:06 Go to previous messageGo to next message
Konstantin Scheglov is currently offline Konstantin ScheglovFriend
Messages: 555
Registered: July 2009
Senior Member
Each container has LayoutEditPolicy, which decides which components to accept, how to show feedback and what command to execute to add/move component.
"Flow container" means just special kind of LayoutEditPolicy which is description-driven, uses information specified in *.wbp-component.xml files, so users can easily configure such often cases.
Some LayoutEditPolicy implementations are very specific, for example for GridLayout or BorderLayout.
So, if you have specific Layout manager, you may be will need to create plugin with LayoutEditPolicy.

But may be "GEF.requestValidator.parent" parameter can be used for your task.


Konstantin Scheglov,
Google, Inc.
Re: How add restriction to add a new component [message #902053 is a reply to message #902032] Wed, 15 August 2012 16:30 Go to previous messageGo to next message
Arthur Fücher is currently offline Arthur FücherFriend
Messages: 59
Registered: August 2012
Member
Hmm...ok!

If I have a Shell with a FillLayout, the restriction need to stay on...?FillLayout or Shell?

I think that is FillLayout, but I can't extend it to do create the "MyFillLayout.wbp-component.xml"...it's right?
Re: How add restriction to add a new component [message #902057 is a reply to message #902053] Wed, 15 August 2012 16:55 Go to previous messageGo to next message
Konstantin Scheglov is currently offline Konstantin ScheglovFriend
Messages: 555
Registered: July 2009
Senior Member
You can not restrict anything if you are not involved.
I.e. you will need your Shell subclass, FillLayout subclass or component subclass.
Not all listed classes can be subclassed, restrictions apply, etc Smile


Konstantin Scheglov,
Google, Inc.
Re: How add restriction to add a new component [message #902058 is a reply to message #902057] Wed, 15 August 2012 16:58 Go to previous messageGo to next message
Arthur Fücher is currently offline Arthur FücherFriend
Messages: 59
Registered: August 2012
Member
Ok, but FillLayout is a final class and I can't extend it... So, can I create "MyFillLayout" based on the FillLayout.java ?
Re: How add restriction to add a new component [message #902061 is a reply to message #902058] Wed, 15 August 2012 17:17 Go to previous messageGo to next message
Konstantin Scheglov is currently offline Konstantin ScheglovFriend
Messages: 555
Registered: July 2009
Senior Member
So, you can not.

Konstantin Scheglov,
Google, Inc.
Re: How add restriction to add a new component [message #902064 is a reply to message #902061] Wed, 15 August 2012 17:35 Go to previous messageGo to next message
Arthur Fücher is currently offline Arthur FücherFriend
Messages: 59
Registered: August 2012
Member
Sad Ok, so I need implement the restriction in the child? Can I do this?
Re: How add restriction to add a new component [message #902065 is a reply to message #902064] Wed, 15 August 2012 17:38 Go to previous messageGo to next message
Konstantin Scheglov is currently offline Konstantin ScheglovFriend
Messages: 555
Registered: July 2009
Senior Member
See "GEF.requestValidator.child" discussed above.

Konstantin Scheglov,
Google, Inc.
Re: How add restriction to add a new component [message #902079 is a reply to message #902065] Wed, 15 August 2012 18:35 Go to previous message
Arthur Fücher is currently offline Arthur FücherFriend
Messages: 59
Registered: August 2012
Member
Thank for your support! It's working as I wish...

[]'s
Previous Topic:Wrong positioning of the components
Next Topic:SashForm error after resizing objects after move
Goto Forum:
  


Current Time: Thu Mar 28 15:51:14 GMT 2024

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

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

Back to the top