You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Peter Beshai <pb...@hotmail.com> on 2006/11/07 19:08:16 UTC

Autocompleter behaviour with "multi-staged" page

I am running into some strange behaviour with the Autocompleter component. 
Say I have a page with two stages -- i.e., I submit a form on the first page 
and it reloads the same page but shows a different part -- and I have an 
autocompleter on the second stage, it will not work unless I persist the 
'stage' flag.

I currently have the stage flag stored in a hidden field on the forms in 
both the first and second stages. While this has worked fine for every other 
part of my form, the autocompleter doesn't seem to like it.

This may seem vague, so hopefully the source shown below will clear things 
up. Does anyone have any ideas on how I can fix this _without_ having to 
persist the stage flag? Thanks!


------------------------------------
TestAutocompleter.html:
------------------------------------
<html jwcid="shell">
<body jwcid="@Body">

<div jwcid="wrapper">

<div jwcid="@If" condition="ognl:stage==1">
<form jwcid="@Form">
  <input type="hidden" jwcid="@Hidden" value="ognl:stage" />
  <select jwcid="categoriesAutocompleter"></select>
  <input type="submit" jwcid="stage1Submit" class="submitButton" />
</form>
</div>

<div jwcid="@If" condition="ognl:stage==2">
<form jwcid="@Form">
  <input type="hidden" jwcid="@Hidden" value="ognl:stage" />
  <select jwcid="categoriesAutocompleter2"></select>
  <input type="submit" jwcid="stage2Submit" class="submitButton" />
</form>
</div>

</div>
</body>
</html>


--------------------------------------
TestAutocompleter.java::
--------------------------------------
package com.novelis.fileupload.page;

import java.util.HashMap;
import java.util.Map;

import org.apache.hivemind.util.Defense;
import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.InitialValue;
import org.apache.tapestry.dojo.form.Autocompleter;
import org.apache.tapestry.dojo.form.IAutocompleteModel;
import org.apache.tapestry.form.Submit;
import org.apache.tapestry.html.BasePage;
import org.apache.tapestry.html.Shell;


public abstract class TestAutocompleter extends BasePage {

    @Component(bindings = {
            "title='Autocompleter Test'",
            "doctype='html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" 
\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\"'",
            })
    public abstract Shell getShell();

    @Component(bindings = {"action=listener:nextStage","value='Next 
Stage'"})
    public abstract Submit getStage1Submit();

    @Component(bindings = {"action=listener:resetStage","value='Reset'"})
    public abstract Submit getStage2Submit();


    @Component(bindings = {"model=categoriesModel", "value=newCategory"})
    public abstract Autocompleter getCategoriesAutocompleter2();

    @Component(bindings = {"model=categoriesModel", "value=newCategory"})
    public abstract Autocompleter getCategoriesAutocompleter();

    /** Values */
    public abstract String getNewCategory();

    // Uncommenting the line below will make it work.
    // @Persist
    @InitialValue("1")
    public abstract int getStage();
    public abstract void setStage(int stage);

    /** Listeners */
    public void nextStage() {
        System.out.println("next stage: " + (getStage()+1));
        setStage(getStage()+1);
    }

    public void resetStage() {
        System.out.println("resetting");
        setStage(1);
    }

    public IAutocompleteModel getCategoriesModel() {
        Map<String, String> testMap = new HashMap<String, String>();
        testMap.put("category1", "The First Category");
        testMap.put("category2", "The Second Category");
        testMap.put("category3", "The Third Category");
        testMap.put("category4", "The Fourth Category");

        return new CategoryAutocompleteModel(testMap);

    }

    // Basic AutocompleteModel that works with a String, String map.
    // The first String is the key(and value) and the second is the label.
    private class CategoryAutocompleteModel implements IAutocompleteModel {
        private Map<String, String> _values;

        public CategoryAutocompleteModel(Map<String, String> values)
        {
            Defense.notNull(values, "Value list can't be null.");
            _values = values;

        }

        public Map filterValues(String match)
        {
            Map<Object, String> ret = new HashMap<Object, String>();

            if (match == null)
                return ret;

            String filter = match.trim().toLowerCase();

            for (Map.Entry<String, String> entry: _values.entrySet()) {

                String label = getLabelFor(entry.getKey());

                int length = filter.length();
                if(label.length() >= length && 
label.toLowerCase().substring(0,length).equals(filter))
                    ret.put(getPrimaryKey(entry.getKey()), label);
            }

            return ret;
        }


        public String getLabelFor(Object value) {
            return _values.get(value);
        }

        // value and primary key are the same thing
        public Object getPrimaryKey(Object value) {
            return value;
        }

        // value and primary key are the same thing
        public Object getValue(Object primaryKey) {
            return primaryKey;
        }
    }
}



--
Peter Beshai - Using Tapestry 4.1.1

Pure Mathematics Student
University of Waterloo





--
Peter Beshai - Using Tapestry 4.1.1

Pure Mathematics Student
University of Waterloo

_________________________________________________________________
Achetez ce que vous voulez, quand vous voulez sur Sympatico / MSN Magasiner 
http://magasiner.sympatico.msn.ca/content/shp/?ctId=101,ptnrid=176,ptnrdata=081805


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


RE: Autocompleter behaviour with "multi-staged" page

Posted by Peter Beshai <pb...@hotmail.com>.
Whoops, typo int he HTML file. should read <div id="wrapper"> not <div 
jwcid="wrapper">

Nonetheless, I have decided to use the @Persist as my solution. 
@Persist("session") was undesirable, but @Persist("flash") will do the trick 
for now. Although I still don't understand why I need it. :-(



--
Peter Beshai - Using Tapestry 4.11

Pure Mathematics Student
University of Waterloo





>From: "Peter Beshai" <pb...@hotmail.com>
>Reply-To: "Tapestry users" <us...@tapestry.apache.org>
>To: users@tapestry.apache.org
>Subject: Autocompleter behaviour with "multi-staged" page
>Date: Tue, 07 Nov 2006 13:08:16 -0500
>
>I am running into some strange behaviour with the Autocompleter component. 
>Say I have a page with two stages -- i.e., I submit a form on the first 
>page and it reloads the same page but shows a different part -- and I have 
>an autocompleter on the second stage, it will not work unless I persist the 
>'stage' flag.
>
>I currently have the stage flag stored in a hidden field on the forms in 
>both the first and second stages. While this has worked fine for every 
>other part of my form, the autocompleter doesn't seem to like it.
>
>This may seem vague, so hopefully the source shown below will clear things 
>up. Does anyone have any ideas on how I can fix this _without_ having to 
>persist the stage flag? Thanks!
>
>
>------------------------------------
>TestAutocompleter.html:
>------------------------------------
><html jwcid="shell">
><body jwcid="@Body">
>
><div jwcid="wrapper">
>
><div jwcid="@If" condition="ognl:stage==1">
><form jwcid="@Form">
>  <input type="hidden" jwcid="@Hidden" value="ognl:stage" />
>  <select jwcid="categoriesAutocompleter"></select>
>  <input type="submit" jwcid="stage1Submit" class="submitButton" />
></form>
></div>
>
><div jwcid="@If" condition="ognl:stage==2">
><form jwcid="@Form">
>  <input type="hidden" jwcid="@Hidden" value="ognl:stage" />
>  <select jwcid="categoriesAutocompleter2"></select>
>  <input type="submit" jwcid="stage2Submit" class="submitButton" />
></form>
></div>
>
></div>
></body>
></html>
>
>
>--------------------------------------
>TestAutocompleter.java::
>--------------------------------------
>package com.novelis.fileupload.page;
>
>import java.util.HashMap;
>import java.util.Map;
>
>import org.apache.hivemind.util.Defense;
>import org.apache.tapestry.annotations.Component;
>import org.apache.tapestry.annotations.InitialValue;
>import org.apache.tapestry.dojo.form.Autocompleter;
>import org.apache.tapestry.dojo.form.IAutocompleteModel;
>import org.apache.tapestry.form.Submit;
>import org.apache.tapestry.html.BasePage;
>import org.apache.tapestry.html.Shell;
>
>
>public abstract class TestAutocompleter extends BasePage {
>
>    @Component(bindings = {
>            "title='Autocompleter Test'",
>            "doctype='html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" 
>\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\"'",
>            })
>    public abstract Shell getShell();
>
>    @Component(bindings = {"action=listener:nextStage","value='Next 
>Stage'"})
>    public abstract Submit getStage1Submit();
>
>    @Component(bindings = {"action=listener:resetStage","value='Reset'"})
>    public abstract Submit getStage2Submit();
>
>
>    @Component(bindings = {"model=categoriesModel", "value=newCategory"})
>    public abstract Autocompleter getCategoriesAutocompleter2();
>
>    @Component(bindings = {"model=categoriesModel", "value=newCategory"})
>    public abstract Autocompleter getCategoriesAutocompleter();
>
>    /** Values */
>    public abstract String getNewCategory();
>
>    // Uncommenting the line below will make it work.
>    // @Persist
>    @InitialValue("1")
>    public abstract int getStage();
>    public abstract void setStage(int stage);
>
>    /** Listeners */
>    public void nextStage() {
>        System.out.println("next stage: " + (getStage()+1));
>        setStage(getStage()+1);
>    }
>
>    public void resetStage() {
>        System.out.println("resetting");
>        setStage(1);
>    }
>
>    public IAutocompleteModel getCategoriesModel() {
>        Map<String, String> testMap = new HashMap<String, String>();
>        testMap.put("category1", "The First Category");
>        testMap.put("category2", "The Second Category");
>        testMap.put("category3", "The Third Category");
>        testMap.put("category4", "The Fourth Category");
>
>        return new CategoryAutocompleteModel(testMap);
>
>    }
>
>    // Basic AutocompleteModel that works with a String, String map.
>    // The first String is the key(and value) and the second is the label.
>    private class CategoryAutocompleteModel implements IAutocompleteModel {
>        private Map<String, String> _values;
>
>        public CategoryAutocompleteModel(Map<String, String> values)
>        {
>            Defense.notNull(values, "Value list can't be null.");
>            _values = values;
>
>        }
>
>        public Map filterValues(String match)
>        {
>            Map<Object, String> ret = new HashMap<Object, String>();
>
>            if (match == null)
>                return ret;
>
>            String filter = match.trim().toLowerCase();
>
>            for (Map.Entry<String, String> entry: _values.entrySet()) {
>
>                String label = getLabelFor(entry.getKey());
>
>                int length = filter.length();
>                if(label.length() >= length && 
>label.toLowerCase().substring(0,length).equals(filter))
>                    ret.put(getPrimaryKey(entry.getKey()), label);
>            }
>
>            return ret;
>        }
>
>
>        public String getLabelFor(Object value) {
>            return _values.get(value);
>        }
>
>        // value and primary key are the same thing
>        public Object getPrimaryKey(Object value) {
>            return value;
>        }
>
>        // value and primary key are the same thing
>        public Object getValue(Object primaryKey) {
>            return primaryKey;
>        }
>    }
>}
>
>
>
>--
>Peter Beshai - Using Tapestry 4.1.1
>
>Pure Mathematics Student
>University of Waterloo
>
>
>
>
>
>--
>Peter Beshai - Using Tapestry 4.1.1
>
>Pure Mathematics Student
>University of Waterloo
>
>_________________________________________________________________
>Achetez ce que vous voulez, quand vous voulez sur Sympatico / MSN Magasiner 
>http://magasiner.sympatico.msn.ca/content/shp/?ctId=101,ptnrid=176,ptnrdata=081805
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>For additional commands, e-mail: users-help@tapestry.apache.org
>

_________________________________________________________________
Voyez vos amis en faisant un appel vidèo dans Windows Live Messenger 
http://imagine-msn.com/messenger/launch80/default.aspx?locale=fr-ca


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org