Package com.nexirius.framework.datamodel

Source Code of com.nexirius.framework.datamodel.StructModel

//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.framework.datamodel;

import com.nexirius.util.CopyPairs;

/**
* A StructModel represents a container which consists of an arbitrary number of children which
* are of diffenrent types and which have field names assigned. The type of the structure is
* basically coded in the order, types and name of the children. Two structures which are equal in
* the order, types and name of the children can be assigned with no structure changes.
*
* @author Marcel Baumann
*/
public class StructModel extends DataModelContainer {

    public static String CAPTION_SEPARATOR = "\t";

    /**
     * Creates a new structure with no children.
     */
    public StructModel() {
    }

    /**
     * Creates a new structure whithout children
     *
     * @param fieldName The inital filed name
     */
    public StructModel(String fieldName) {
        super();
        setFieldName(fieldName);
    }

    /**
     * Creates a new structure which is initialised with a list of children
     *
     * @param v This list is directly used as value (not copied)
     */
    public StructModel(DataModelVector v) {
        super(v);
    }

    /*
     * calls clear() on all members
     */
    public synchronized void clear() {
        for (DataModel i = getChildren().firstItem(); i != null; i = getChildren().nextItem()) {
            i.clear();
        }
    }

    /*
     * calls reset() on all members
     */
    public synchronized void reset() {
        for (DataModel i = getChildren().firstItem(); i != null; i = getChildren().nextItem()) {
            i.reset();
        }
    }

    /**
     * Creates an exact copy of this.
     *
     * @param instance  null (eventually used by subclasses which define the duplicate method)
     * @param copyPairs A list which avoids creating two copies of the same instance (can be null)
     */
    public DataModel duplicate(DataModel instance, CopyPairs copyPairs) {
        if (copyPairs == null) {
            copyPairs = new CopyPairs();
        }
       
        if (instance == null) {
            try {
                instance = (DataModel) getClass().getConstructor(null).newInstance(null);
            } catch (Exception ex) {
                System.err.println("Need public default constructor (with no arguments) in " + getClass().getName());
                ex.printStackTrace();
            }
        }
       
        DataModelEnumeration en = getEnumeration();

        while (en.hasMore()) {
            DataModel origChild = en.next();

            try {
                DataModel newChild = instance.getChild(origChild.getFieldName());

                newChild.assignModel(origChild, copyPairs);
                copyPairs.put(origChild, newChild);
            } catch (Exception e) {
                DataModel newChild = origChild.duplicate(null, copyPairs);

                newChild.assignModel(origChild, copyPairs);
                copyPairs.put(origChild, newChild);

                if (getClass() == StructModel.class) {
                    ((StructModel)instance).append(newChild);
                }
            }
        }


        /*
        try {
            instance.dropData(dragData());
            // not needed any more: instance.setStatus(getStatus());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        */

        instance.setStatus(getStatus());

        return instance;
    }

    /**
     * only used by duplicate
     * @param orig
     * @param copyPairs
     */
    protected void assignModel(DataModel orig, CopyPairs copyPairs) {
        DataModelEnumeration en = getEnumeration();

        while (en.hasMore()) {
            DataModel child = en.next();

            try {
                DataModel origChild = orig.getChild(child.getFieldName());

                child.assignModel(origChild, copyPairs);
                copyPairs.put(origChild, child);
            } catch (Exception e) {
                e.printStackTrace()//TODO
            }
        }

        setStatus(orig.getStatus());
    }

    /**
     * should return true if the associated editor enables child editing
     */
    public boolean isChildEditor() {
        return true;
    }

    public String getCaption() {
        return getFieldName();

//        StringBuffer sb = new StringBuffer();
//        DataModelEnumeration en = getEnumeration();
//
//        while (en.hasMore()) {
//            DataModel model = en.next();
//
//            sb.append(model.getCaption());
//
//            if (en.hasMore()) {
//                sb.append(CAPTION_SEPARATOR);
//            }
//        }
//
//        return sb.toString();
    }
}
TOP

Related Classes of com.nexirius.framework.datamodel.StructModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.