org.sakaiproject.metaobj.shared.mgt.IdListCustomEditor.java Source code

Java tutorial

Introduction

Here is the source code for org.sakaiproject.metaobj.shared.mgt.IdListCustomEditor.java

Source

/**********************************************************************************
 * $URL: https://source.sakaiproject.org/svn/metaobj/trunk/metaobj-util/tool-lib/src/java/org/sakaiproject/metaobj/shared/mgt/IdListCustomEditor.java $
 * $Id: IdListCustomEditor.java 105079 2012-02-24 23:08:11Z ottenhoff@longsight.com $
 ***********************************************************************************
 *
 * Copyright (c) 2004, 2005, 2006, 2007, 2008 The Sakai Foundation
 *
 * Licensed under the Educational Community 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.opensource.org/licenses/ECL-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.sakaiproject.metaobj.shared.mgt;

import java.beans.PropertyEditorSupport;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.sakaiproject.metaobj.shared.model.Id;
import org.sakaiproject.metaobj.utils.mvc.intf.TypedPropertyEditor;

public class IdListCustomEditor extends PropertyEditorSupport implements TypedPropertyEditor {
    protected final transient Log logger = LogFactory.getLog(getClass());

    private IdManager idManager = null;

    /**
     * Parse the Date from the given text, using the specified DateFormat.
     */
    public void setAsText(String text) throws IllegalArgumentException {
        if (text == null || text.length() == 0) {
            setValue(new ArrayList());
        } else {
            String[] items = text.split(",");
            List ids = new ArrayList();
            for (int i = 0; i < items.length; i++) {
                ids.add(getIdManager().getId(items[i]));
            }
            setValue(ids);
        }
    }

    /**
     * Format the Date as String, using the specified DateFormat.
     */
    public String getAsText() {
        if (!(getValue() instanceof List)) {
            return null;
        }

        List ids = (List) getValue();

        StringBuilder sb = new StringBuilder();

        for (Iterator i = ids.iterator(); i.hasNext();) {
            Id id = (Id) i.next();
            sb.append(id.getValue());
            if (i.hasNext()) {
                sb.append(',');
            }
        }

        return sb.toString();
    }

    public IdManager getIdManager() {
        return idManager;
    }

    public void setIdManager(IdManager idManager) {
        this.idManager = idManager;
    }

    public Class getType() {
        return List.class;
    }

}