[ 
https://issues.apache.org/jira/browse/WICKET-3776?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13046005#comment-13046005
 ] 

Russell Morrisey edited comment on WICKET-3776 at 6/8/11 3:24 PM:
------------------------------------------------------------------

Ok, let me give another example, then. Compare these two snippets of code:

private void setPermissions(Componet c) {
 c.setEnableState(new MyPermissionsPredicate());
}

DropDownChoice ddc = new DropDownChoice(...);
setPermissions(ddc);

TextField textField = new TextField(...);
setPermissions(textField);

HiddenField hiddenField = new HiddenField(...);
setPermissions(hiddenField);

ComponentFactory factory = getSomeComponentFactory();
Component c = factory.get();
setPermissions(c);

By contrast, here is the current style:

private void setPermissions(Componet c) {
 c.setEnabled(new MyPermissionsPredicate().evaluate(c));
}

DropDownChoice ddc = new DropDownChoice(...) {
 protected void onConfigure() {
  setPermissions(this);
 }
};

TextField textField = new TextField(...)  {
 protected void onConfigure() {
  setPermissions(this);
 }
};

HiddenField hiddenField = new HiddenField(...)  {
 protected void onConfigure() {
  setPermissions(this);
 }
}

ComponentFactory factory = getSomeComponentFactory(new IPermissionsHandler() {
  protected void onConfigure() {
   setPermissions(this);
  }
});
Component c = factory.get();
setPermissions(c);

In the current style, I am generating more subclasses, and it feels like I am 
repeating myself each time. It complicates the type hierarchy, and it 
complicates the design of my factory by forcing me to introduce something at 
the point of the component's construction, which feeds into the factory 
implementation. Controlling enabled and visibility state is something which 
cuts across a variety of different types of components; it often isn't specific 
to a particular type of component on the page.




      was (Author: rmorrisey):
    Ok, let me give another example, then. Compare these two snippets of code:

private void setPermissions(Componet c) {
 c.setEnableState(new MyPermissionsPredicate());
}

DropDownChoice ddc = new DropDownChoice(...);
setPermissions(ddc);

TextField textField = new TextField(...);
setPermissions(textField);

HiddenField hiddenField = new HiddenField(...);
setPermissions(textField);

ComponentFactory factory = getSomeComponentFactory();
Component c = factory.get();
setPermissions(c);

By contrast, here is the current style:

private void setPermissions(Componet c) {
 c.setEnabled(new MyPermissionsPredicate().evaluate(c));
}

DropDownChoice ddc = new DropDownChoice(...) {
 protected void onConfigure() {
  setPermissions(this);
 }
};

TextField textField = new TextField(...)  {
 protected void onConfigure() {
  setPermissions(this);
 }
};

HiddenField hiddenField = new HiddenField(...)  {
 protected void onConfigure() {
  setPermissions(this);
 }
}

ComponentFactory factory = getSomeComponentFactory(new IPermissionsHandler() {
  protected void onConfigure() {
   setPermissions(this);
  }
});
Component c = factory.get();
setPermissions(c);

In the current style, I am generating more subclasses, and it feels like I am 
repeating myself each time. It complicates the type hierarchy, and it 
complicates the design of my factory by forcing me to introduce something at 
the point of the component's construction, which feeds into the factory 
implementation. Controlling enabled and visibility state is something which 
cuts across a variety of different types of components; it often isn't specific 
to a particular type of component on the page.



  
> Implement default onConfigure() using optional Predicate
> --------------------------------------------------------
>
>                 Key: WICKET-3776
>                 URL: https://issues.apache.org/jira/browse/WICKET-3776
>             Project: Wicket
>          Issue Type: Improvement
>          Components: wicket-core
>    Affects Versions: 1.4.17
>            Reporter: Russell Morrisey
>            Assignee: Igor Vaynberg
>              Labels: ConfigurationPredicate, Predicate, component, 
> onConfigure, setEnabled, setVisible
>
> I would really like to see methods on Component to configure visible and 
> enabled state using a predicate pattern. Ex:
> interface ConfigurationPredicate<T extends Component> extends IDetachable {
>  boolean evaluate(T component);
> }
> ...
> DropDownChoice ddc = createDDC(); //my convenience method which sets standard 
> properties, creates a label for the component, and other stuff
> ddc.setVisibility(new ConfigurationPredicate<DropDownChoice>() {
>   public boolean evaluate(DropDownChoice component) {
>    myOtherComponent.configure();
>    return (myOtherComponent.isVisibleInHierarchy());
>   }
>   public void detach() {
>    myOtherComponent.detach();
>   }
> });
> Please consider creating an optional field on Component which can hold a 
> visibility predicate and an enabled state predicate. You can see the 
> advantage clearly in the code above: I don't have to inline my convenience 
> method. I would like composition to be used, instead of inheritance, so I 
> don't have to subclass DropDownChoice just to control the visibility.
> Thanks, guys!

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to