I've added generics.. Still not working though, it's as if the components
get added twice (and the fragment never gets added or something like that)




Index:
branches/1.5/visural-wicket/src/com/visural/wicket/component/viewmode/ViewOrEdit.java
===================================================================
---
branches/1.5/visural-wicket/src/com/visural/wicket/component/viewmode/ViewOrEdit.java
(revision
266)
+++
branches/1.5/visural-wicket/src/com/visural/wicket/component/viewmode/ViewOrEdit.java
(working
copy)
@@ -14,11 +14,8 @@
  *  limitations under the License.
  *  under the License.
  */
-package com.visural.wicket.component.viewmode;
+package com.netdesign.ccadmin.components.vieworedit;

-import com.visural.wicket.security.IPrivilege;
-import com.visural.wicket.security.ISecureEnableInstance;
-import com.visural.wicket.security.ISecureRenderInstance;
 import org.apache.wicket.Component;
 import org.apache.wicket.markup.html.basic.Label;
 import org.apache.wicket.markup.html.form.AbstractChoice;
@@ -35,19 +32,19 @@
 import org.apache.wicket.model.Model;

 /**
- * A wrapper for a FormComponent which replaces the component with a Label
- * when the wrapped control is in "view mode".
- *
+ * A wrapper for a FormComponent which replaces the component with a Label
when
+ * the wrapped control is in "view mode".
+ *
  * Apply to a containing DOM element, e.g. `<span></span>` or `<div></div>`
  *
  * The component you provide to the constructor must be given as ID
matching
  * `ViewOrEdit.COMP_ID`.
- *
+ *
  * The idea is to allow the rendering of web-like modern applications where
  * non-editable elements are represented in text form, using the same
wicket
  * page / form implementation.
- *
- * What is "view mode" for a given component is determined by the method
+ *
+ * What is "view mode" for a given component is determined by the method
  * `isViewMode()`. The default implementation of this method uses the
component
  * and its form's `isEnabled()` state to determine view or edit.
  *
@@ -59,194 +56,194 @@
  * apply to form elements wrapped in a `ViewOrEdit` container. Just name
your
  * `ViewOrEdit` element as per the property you wish to apply to the child
  * control.
- *
+ *
  * @version $Id$
  * @author Richard Nichols
  */
-public class ViewOrEdit extends FormComponentPanel implements
ISecureRenderInstance, ISecureEnableInstance {
-    private static final long serialVersionUID = 1L;
-    /**
-     * This is the id of the component to be contained within the
ViewOrEdit container.
-     * Any component provided to the ViewOrEdit component should be
identified as ViewOrEdit.COMP_ID
-     */
-    public final static String COMP_ID = "component";
+public class ViewOrEdit<T> extends FormComponentPanel<T> {
+ private static final long serialVersionUID = 1L;
+ /**
+ * This is the id of the component to be contained within the ViewOrEdit
+ * container. Any component provided to the ViewOrEdit component should be
+ * identified as ViewOrEdit.COMP_ID
+ */
+ public final static String COMP_ID = "component";

-    private final FormComponent component;
-    private Label label;
+ private final FormComponent<T> component;
+ private Label label;

-    public ViewOrEdit(String id, FormComponent component) {
-        this(id, component, null, null);
-    }
+ public ViewOrEdit(String id, FormComponent<T> component) {
+ this(id, component, null, null);
+ }

-    public ViewOrEdit(String id, FormComponent component, IModel
labelModel) {
-        this(id, component, labelModel, null);
-    }
+ public ViewOrEdit(String id, FormComponent<T> component,
+ IModel<T> labelModel) {
+ this(id, component, labelModel, null);
+ }

-    public ViewOrEdit(String id, FormComponent component,
ComponentModelToLabel componentModelToLabel) {
-        this(id, component, null, componentModelToLabel);
-    }
+ public ViewOrEdit(String id, FormComponent<T> component,
+ ComponentModelToLabel<T> componentModelToLabel) {
+ this(id, component, null, componentModelToLabel);
+ }

-    protected ViewOrEdit(final String id, final FormComponent component,
IModel labelModel, final ComponentModelToLabel componentModelToLabel) {
-        super(id);
-        this.component = component;
-
-        if (labelModel == null && componentModelToLabel != null) {
-            labelModel = new AbstractReadOnlyModel() {
+ protected ViewOrEdit(final String id, final FormComponent<T> component,
+ IModel labelModel,
+ final ComponentModelToLabel<T> componentModelToLabel) {
+ super(id);
+ this.component = component;

-                private final ComponentModelToLabel converter =
componentModelToLabel;
+ if (labelModel == null && componentModelToLabel != null) {
+ labelModel = new AbstractReadOnlyModel<String>() {

-                @Override
-                public Object getObject() {
-                    return
converter.convertToLabel(component.getModelObject());
-                }
-            };
-        }
+ private final ComponentModelToLabel<T> converter = componentModelToLabel;

-        if (component == null || !component.getId().equals(COMP_ID)) {
-            throw new IllegalArgumentException("The component provided to
LabelOnViewOnly(...) must have the id \"" + COMP_ID + "\"");
-        }
+ @Override
+ public String getObject() {
+ return converter.convertToLabel(component.getModelObject());
+ }
+ };
+ }

-        Fragment f = resolveComponentFragment(labelModel);
-        if (f == null) {
-            throw new UnsupportedOperationException("No view mode fragment
for component of type " + component.getClass().getName());
-        }
-        add(f);
-    }
+ if (component == null || !component.getId().equals(COMP_ID)) {
+ throw new IllegalArgumentException(
+ "The component provided to LabelOnViewOnly(...) must have the id \""
+ + COMP_ID + "\"");
+ }

-    /**
-     * Provide support for using CompountPropertyModel or similar on form
-     * and having that property model chain to the form component being
wrapped
-     * in a ViewOrEdit.
-     *
-     * @return
-     */
-    @Override
-    protected IModel<?> initModel() {
-        final IModel parentModel = super.initModel();
-        if (parentModel != null && parentModel instanceof IWrapModel) {
-            // we want to set this model to wrap the form component
-            return new WrappedParentModel((IWrapModel)parentModel);
-        }
-        // we do this in case there is no parent model to avoid
-        // "Attempt to set model object on null model of component" errors
-        return new Model();
-    }
+ Fragment f = resolveComponentFragment(labelModel);
+ // f.setVisible(false);
+ if (f == null) {
+ throw new UnsupportedOperationException(
+ "No view mode fragment for component of type "
+ + component.getClass().getName());
+ }
+ add(f);
+ }

-    @Override
-    protected void convertInput() {
-        // this forces a call to initModel()
-        getDefaultModel();
-    }
+ /**
+ * Provide support for using CompountPropertyModel or similar on form and
+ * having that property model chain to the form component being wrapped in
a
+ * ViewOrEdit.
+ *
+ * @return
+ */
+ @Override
+ protected IModel<?> initModel() {
+ final IModel parentModel = super.initModel();
+ if (parentModel != null && parentModel instanceof IWrapModel) {
+ // we want to set this model to wrap the form component
+ return new WrappedParentModel((IWrapModel<T>) parentModel);
+ }
+ // we do this in case there is no parent model to avoid
+ // "Attempt to set model object on null model of component" errors
+ return new Model();
+ }

-    /**
-     * Determine whether the component is in view mode or not.
-     *
-     * By default, the implementation will assume view mode when this
component
-     * is not enabled or the form component is not enabled or the form
itself
-     * is not enabled, however this behaviour may be overriden as req'd.
-     *
-     * @return
-     */
-    public boolean isViewMode() {
-        return !this.isEnabled() || !component.isEnabled() ||
!component.getForm().isEnabled();
-    }
+ @Override
+ protected void convertInput() {
+ // this forces a call to initModel()
+ getDefaultModel();
+ }

-    @Override
-    protected void onBeforeRender() {
-        // this forces a call to initModel()
-        getDefaultModel();
-        // now proceed as normal
-        super.onBeforeRender();
-        boolean isView = this.isViewMode();
-        label.setVisible(isView);
-        component.setVisible(!isView);
-    }
+ /**
+ * Determine whether the component is in view mode or not.
+ *
+ * By default, the implementation will assume view mode when this component
+ * is not enabled or the form component is not enabled or the form itself
is
+ * not enabled, however this behaviour may be overriden as req'd.
+ *
+ * @return
+ */
+ public boolean isViewMode() {
+ return !this.isEnabled() || !component.isEnabled()
+ || !component.getForm().isEnabled();
+ }

-    private Fragment resolveComponentFragment(IModel labelModel) {
-        if (labelModel == null) {
-            // TODO: rather than doing this, maybe lookup converter?
-            labelModel = new IModel() {
+ @Override
+ protected void onBeforeRender() {
+ // this forces a call to initModel()
+ getDefaultModel();
+ // now proceed as normal
+ super.onBeforeRender();

-                public Object getObject() {
-                    return (component.getModelObject() == null ? null :
component.getModelObject().toString());
-                }
+ boolean isView = this.isViewMode();
+ label.setVisible(isView);
+ component.setVisible(!isView);
+ }

-                public void setObject(Object arg0) {
-                }
+ private Fragment resolveComponentFragment(IModel<String> labelModel) {
+ if (labelModel == null) {
+ // TODO: rather than doing this, maybe lookup converter?
+ labelModel = new AbstractReadOnlyModel<String>() {
+ public String getObject() {
+ return (component.getModelObject() == null ? null
+ : component.getModelObject().toString());
+ }

-                public void detach() {
-                }
-            };
-        }
-        label = new Label("viewLabel", labelModel);
-        label.setEscapeModelStrings(isEscapeLabelModelStrings());
-        if (TextField.class.isAssignableFrom(component.getClass())) {
-            Fragment f = new Fragment("controlPair", "textfield", this);
-            f.add(label);
-            f.add(component);
-            return f;
-        }
-        if (CheckBox.class.isAssignableFrom(component.getClass())) {
-            Fragment f = new Fragment("controlPair", "checkbox", this);
-            f.add(label);
-            f.add(component);
-            return f;
-        }
-        if (TextArea.class.isAssignableFrom(component.getClass())) {
-            Fragment f = new Fragment("controlPair", "textarea", this);
-            f.add(label);
-            f.add(component);
-            return f;
-        }
-        if (AbstractChoice.class.isAssignableFrom(component.getClass())) {
-            Fragment f = new Fragment("controlPair", "choice", this);
-            f.add(label);
-            f.add(component);
-            return f;
-        }
-        return null;
-    }
+ };
+ }

-    public FormComponent getComponent() {
-        return component;
-    }
+ label = new Label("viewLabel", labelModel);
+ label.setEscapeModelStrings(isEscapeLabelModelStrings());
+ if (TextField.class.isAssignableFrom(component.getClass())) {
+ Fragment f = new Fragment("controlPair", "textfield", this);
+ f.add(label);
+ f.add(component);
+ return f;
+ }
+ if (CheckBox.class.isAssignableFrom(component.getClass())) {
+ Fragment f = new Fragment("controlPair", "checkbox", this);
+ f.add(label);
+ f.add(component);
+ return f;
+ }
+ if (TextArea.class.isAssignableFrom(component.getClass())) {
+ Fragment f = new Fragment("controlPair", "textarea", this);
+ f.add(label);
+ f.add(component);
+ return f;
+ }
+ if (AbstractChoice.class.isAssignableFrom(component.getClass())) {
+ Fragment f = new Fragment("controlPair", "choice", this);
+ f.add(label);
+ f.add(component);
+ return f;
+ }
+ return null;
+ }

-    public boolean isEscapeLabelModelStrings() {
-        return false;
-    }
+ public FormComponent<T> getComponent() {
+ return component;
+ }

-    class WrappedParentModel implements IComponentInheritedModel {
-        private final IWrapModel realParent;
+ public boolean isEscapeLabelModelStrings() {
+ return false;
+ }

-        public WrappedParentModel(IWrapModel realParent) {
-            this.realParent = realParent;
-        }
+ class WrappedParentModel implements IComponentInheritedModel {
+ private final IWrapModel realParent;

-        public IWrapModel wrapOnInheritance(Component arg0) {
-            if (arg0 == ViewOrEdit.this.component) {
-                return realParent;
-            } else {
-                return null;
-            }
-        }
+ public WrappedParentModel(IWrapModel realParent) {
+ this.realParent = realParent;
+ }

-        public Object getObject() {
-            return null;
-        }
+ public IWrapModel wrapOnInheritance(Component arg0) {
+ if (arg0 == ViewOrEdit.this.component) {
+ return realParent;
+ } else {
+ return null;
+ }
+ }

-        public void setObject(Object arg0) {
-        }
+ public Object getObject() {
+ return null;
+ }

-        public void detach() {
-        }
-    }
+ public void setObject(Object arg0) {
+ }

-    public IPrivilege getRenderPrivilege() {
-        return IPrivilege.NULL;
-    }
+ public void detach() {
+ }
+ }

-    public IPrivilege getEnablePrivilege() {
-        return IPrivilege.NULL;
-    }
-
-}
+}
\ No newline at end of file


2011/3/10 nino martinez wael <[email protected]>

> Ok I've given it a run.. And are having problems :(
>
> It still does'nt seem like ViewOrEdit honors the fact that it is either
> ViewOrEdit.
> So it either looks like:
> http://img826.imageshack.us/i/greenshot20110310111030.png/
> Or
> http://img689.imageshack.us/i/greenshot20110310111207.png/
>
> If I try to run the examples I get and error:
> http://img833.imageshack.us/i/greenshot20110310111126.png/
>
>
> Also I had to add the servlet api inorder to run the ant build, as well as
> putting the jar files besides the other wicket dependencies.
>
> Index: branches/1.5/visural-wicket-examples/lib/nblibraries.properties
> ===================================================================
> --- branches/1.5/visural-wicket-examples/lib/nblibraries.properties (revision
> 266)
> +++ branches/1.5/visural-wicket-examples/lib/nblibraries.properties (working
> copy)
> @@ -20,6 +20,7 @@
>  libs.visural-wicket.src=\
>      ${base}/visural-wicket/visural-wicket-0.5-sources.jar
>  libs.Wicket.classpath=\
> + ${base}/Wicket/servlet-api-2.5-20081211.jar;\
>      ${base}/Wicket/slf4j-api-1.4.2.jar;\
>      ${base}/Wicket/slf4j-jdk14-1.4.2.jar;\
>      ${base}/Wicket/wicket-core-1.5-rc2.jar;\
>
>
>
>
>
> 2011/3/9 nino martinez wael <[email protected]>
>
> Thanks. I'll give feedback if needed
>> On Mar 9, 2011 4:22 AM, "Richard Nichols" <[email protected]> wrote:
>> > I've committed what appears to be a mostly working visural-wicket with
>> 1.5
>> > (rc2) compatibility.
>> >
>> >
>> http://code.google.com/p/visural-wicket/source/browse/#svn%2Fbranches%2F1.5
>> >
>> > If there are any issues found, please report them at
>> > http://code.google.com/p/visural-wicket/
>> >
>> > cheers,
>> > Richard.
>> >
>> > On Wed, Mar 9, 2011 at 4:30 AM, nino martinez wael <
>> > [email protected]> wrote:
>> >
>> >> ok great.. I'll probably take at look at it monday then..
>> >>
>> >> 2011/3/8 Richard Nichols <[email protected]>
>> >>
>> >> > Hi Nino,
>> >> >
>> >> > The version in the public respository 1.5 branch is still a direct
>> copy
>> >> of
>> >> > 1.4 - sorry I haven't commited the WIP 1.5 changes yet.
>> >> >
>> >> > I'll aim to get the WIP committed in this week.
>> >> >
>> >> > cheers,
>> >> > Richard.
>> >> >
>> >> > On Mon, Mar 7, 2011 at 6:47 PM, nino martinez wael <
>> >> > [email protected]> wrote:
>> >> >
>> >> > > Strange it compiles well, but does not work as wanted.. Any idea on
>> >> which
>> >> > > parts are causing the problems? On my dropdowns the dropdowns are
>> >> empty,
>> >> > > and in my textfields both the text and the span are added if in
>> view
>> >> > mode,
>> >> > > and if not 2 textfields are added.
>> >> > >
>> >> > > 2011/3/5 Richard Nichols <[email protected]>
>> >> > >
>> >> > > > I've created a 1.5 branch and it's in the works. 1.5 broke more
>> stuff
>> >> > > > than expected :)
>> >> > > >
>> >> > > > Hopefully I can have a beta 1.5 branch functional within a few
>> weeks
>> >> > > >
>> >> > > > Sent from my iPhone
>> >> > > >
>> >> > > > On 05/03/2011, at 6:15 AM, nino martinez wael
>> >> > > > <[email protected]> wrote:
>> >> > > >
>> >> > > > > So does has anybody run it on wicket 1.5, or is there something
>> in
>> >> > the
>> >> > > > works
>> >> > > > > for it?
>> >> > > > >
>> >> > > > > regards Nino
>> >> > > >
>> >> > > >
>> ---------------------------------------------------------------------
>> >> > > > To unsubscribe, e-mail: [email protected]
>> >> > > > For additional commands, e-mail: [email protected]
>> >> > > >
>> >> > > >
>> >> > >
>> >> >
>> >> >
>> >> >
>> >> > --
>> >> > Richard Nichols
>> >> > http://www.richardnichols.net/ :: http://onmydoorstep.com.au/
>> >> >
>> >>
>> >
>> >
>> >
>> > --
>> > Richard Nichols
>> > http://www.richardnichols.net/ :: http://onmydoorstep.com.au/
>>
>
>

Reply via email to