    001    /*****************************************************************************
    002     * Copyright (C) PicoContainer Organization. All rights reserved.            *
    003     * ------------------------------------------------------------------------- *
    004     * The software in this package is published under the terms of the BSD      *
    005     * style license a copy of which has been included with this distribution in *
    006     * the LICENSE.txt file.                                                     *
    007     *                                                                           *
    008     * Original code by                                                          *
    009     *****************************************************************************/
    010    package org.picocontainer.testmodel;
    011
    012    import java.beans.PropertyEditorSupport;
    013
    014    /**
    015     *
    016     * @author greg
    017     */
    018    public class CoupleBeanEditor extends PropertyEditorSupport {
    019        private static final String PREFIX_A = "a's name:";
    020        private static final String PREFIX_B = "b's name:";
    021        private static final String SEPARATOR = ";";
    022
    023        public CoupleBeanEditor() {
    024            super();
    025        }
    026
    027        public void setAsText(String s) throws IllegalArgumentException {
    028            int startA = s.indexOf(PREFIX_A);
    029            int stopA = s.indexOf(SEPARATOR, startA+PREFIX_A.length());
    030            int startB = s.indexOf(PREFIX_B, stopA + SEPARATOR.length());
    031            int stopB = s.indexOf(SEPARATOR, startB+ PREFIX_B.length());
    032            if (startA < 0 || stopA < 0 || startB < 0 || stopB < 0) {
    033                throw new IllegalArgumentException("Can't parse " + s + " into a CoupleBean");
    034            }
    035            String nameA = s.substring(startA + PREFIX_A.length(), stopA);
    036            String nameB = s.substring(startB + PREFIX_B.length(), stopB);
    037
    038            PersonBean a = new PersonBean();
    039            a.setName(nameA);
    040            PersonBean b = new PersonBean();
    041            b.setName(nameB);
    042            setValue(new CoupleBean(a, b));
    043        }
    044    }