| How add restriction to add a new component [message #901832] |
Tue, 14 August 2012 13:37  |
Arthur Fücher 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 14:02   |
Konstantin Scheglov Messages: 547 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 #901846 is a reply to message #901843] |
Tue, 14 August 2012 14:36   |
Konstantin Scheglov Messages: 547 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 14: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 14:39   |
Konstantin Scheglov Messages: 547 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:35Konstatin,
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 #901854 is a reply to message #901851] |
Tue, 14 August 2012 15:00   |
Konstantin Scheglov Messages: 547 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 #901862 is a reply to message #901859] |
Tue, 14 August 2012 15:22   |
Konstantin Scheglov Messages: 547 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 15: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 16:50   |
Arthur Fücher 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 16:51] Report message to a moderator
|
|
|
|
|
|
|
|
| Re: How add restriction to add a new component [message #902032 is a reply to message #902008] |
Wed, 15 August 2012 11:06   |
Konstantin Scheglov Messages: 547 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.
|
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.08490 seconds