You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by bu...@apache.org on 2005/08/05 09:23:33 UTC

DO NOT REPLY [Bug 36039] New: - Implement a more javascript feel for the ScriptableList

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=36039>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=36039

           Summary: Implement a more javascript feel for the ScriptableList
           Product: Struts
           Version: Nightly Build
          Platform: All
        OS/Version: other
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Struts Flow
        AssignedTo: dev@struts.apache.org
        ReportedBy: ronald_villaver@yahoo.com


This is to give the ScriptableList wrapper the more javascript feel in reference
to manipulating data.

currently this wont work... because of IndexOutOfBounds exception when the
runtime tries to access a list with an index not yet there.

var a = new ArrayList();
a[5] = "1234";


Here's the suggested ScriptableList with modifications.
*
 * Copyright 1999-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.struts.flow.sugar;

import org.mozilla.javascript.*;

import java.util.*;
import java.io.Serializable;

/**
 * Wrap a java.util.List for JavaScript.
 */
public class ScriptableList extends JavaObjectWrapper implements Scriptable,
Wrapper, Serializable {

    private List list;

    public ScriptableList() {
        this.list = list;
    }

    public ScriptableList(List list) {
        this.list = list;
        this.javaObject = javaObject;
    }
    
    public ScriptableList(Scriptable scope, Object javaObject, Class staticType,
Map funcs) {
        super(scope, javaObject, staticType, funcs);
        if (javaObject instanceof List) {
            this.list = (List)javaObject;
        } else {
            throw new IllegalArgumentException("Passed object "+javaObject+" is
not an instance of List");
        }
    }

    public String getClassName() {
        return staticType.toString();
    }

    public boolean has(int index, Scriptable start) {
        //return (list.get(index) != null); <-- this throws an
ArrayIndexOutOfBounds type error. You can just simply try catch this. This is
caused I think because the parser would try to retrieve the value first(reading
the statement left to right) then assign the value to that index... or something
like that. 
		try{
			return (list.get(index) != null);
		}catch(Exception e){
	        return false;
		}
    }

    public Object get(int index, Scriptable start) {
        return list.get(index);
    }

    public void put(int index, Scriptable start, Object value) {
        //list.add(index, value);
		// Another suggestion to have the full feel of javascript array, is the
ability to set a variable at a higher index than the current length of the array.
		int max = index+1;
		if(max>list.size()){
			for(int i=list.size();i<index;i++){
				list.add(i,null);
			}
			list.add(index,value);
		}else{
			list.set(index,value);
		}
    }

    public void delete(int index) {
        list.remove(index);
    }

    public Object[] getIds() {
        
        //TODO: optimize this :)
        Integer[] ids = new Integer[list.size()];
        for (int x=0; x<ids.length; x++) {
            ids[x] = new Integer(x);
        }
        return ids;
    }

    public Object unwrap() {
        return this.list;
    }

}

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org