Package com.nexirius.framework.datamodel

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

//{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;
import com.nexirius.util.TextToken;
import com.nexirius.util.XString;
import com.nexirius.util.simpletype.SimpleType;

import java.io.OutputStream;
import java.io.PushbackInputStream;

/**
* A class that represents a simple information (number or string) which can be displayed in a text field.
* All subtypes of this class are based on SimpleType classes @see com.nexirius.util.simpletype
*
* @author Marcel Baumann
*/
public abstract class SimpleModel extends DataModel {
    public static final String EMPTY_STRING = "";

    /**
     * Create a new instance with an initial value
     *
     * @param value The initial value
     */
    public SimpleModel(Object value) {
        super(value);
    }

    /**
     * Create a new instance with an initial value and field name
     *
     * @param value     The initial value
     * @param fieldName The initial field name
     */
    public SimpleModel(Object value, String fieldName) {
        super(value, fieldName);
    }

    /*
     * remove the contents
     */
    public void clear() {
        if (isTransient()) {

            return;
        }

        try {
            setText(EMPTY_STRING);
        } catch (Exception ex) {
        }
    }

    public boolean filled() {
        return !isEmpty();
    }

    /**
     * Read persistent information from input stream
     *
     * @param in The input stream to read from
     * @throws Exception If the input stream is not readable or has corrupted data
     */
    public void readDataFrom(PushbackInputStream in)
            throws Exception {
        super.readDataFrom(in);

        TextToken token = TextToken.nextToken(in);

        if (token.isString()) {
            String value = token.getString();
            SimpleType v = getSimpleType().duplicate();

            v.set(value);

            assignValue(v);
        } else {
            throw new Exception("Expecting String literal (field value) but have:" + token);
        }
    }

    /**
     * Writes persistent information to output stream
     *
     * @param out The stream where to write to
     * @throws Exception If the output stream is not writable
     */
    public void writeDataTo(OutputStream out)
            throws Exception {
        super.writeDataTo(out);

        TextToken value = new TextToken(getSimpleType().toString());

        value.writeTo(out);
    }

    /**
     * Create a copy instance of the value.
     *
     * @param copyPairs May be null (not needed here)
     */
    public Object cloneValue(CopyPairs copyPairs) {
        return getSimpleType().duplicate();
    }

    /**
     * Returns the current value as SimpleType instance
     */
    public SimpleType getSimpleType() {
        return (SimpleType) getValue();
    }

    /**
     * Return the text representation which is displayed on GUI
     */
    public String getText() {
        if (isExceptional()) {
            return getStatus().getExceptionalStringId();
        }

        return getSimpleType().toString();
    }

    /**
     * This function is used when the user types in a new value in the GUI.
     *
     * @param text The new value
     * @throws Exception If the value cannot be parsed successfully. Value is not accepted. (Results in red text display on the GUI)
     */
    public abstract void setText(String text) throws Exception;

    /**
     * @return the number of columns to be displayed
     */
    public int getColumns() {
        return 2;
    }

    /**
     * @return the label at the specified column index
     */
    public String getLabel(int index) {
        switch (index) {
            case 0:
                return getFieldName();
            case 1:
                return getText();
        }

        return null;
    }

    public String getCaption() {
        return getFieldName();
    }

    /**
     * @return the column specific background color id
     */
    public String getBackgroundColorId(int index) {
        return null;
    }

    /**
     * Returns a string representation of the current value
     */
    public String toString() {
        return getText();
    }

    /**
     * Returns true if the values are equal and the ModelStatus is equal
     */
    public boolean equals(Object other) {
        if (other != null && other instanceof SimpleModel) {
            boolean ret = getSimpleType().equals(((SimpleModel) other).getSimpleType());


            if (ret) {
                ret = getStatus().equals(((SimpleModel) other).getStatus());
            }

            return ret;
        }

        return false;
    }

    /**
     * @return if o1>o2 -> -1 if o1<o2 -> 1 if o1==o2 -> 0
     */
    public int textCompare(Object o1, Object o2) {
        return XString.compare(((SimpleModel) o1).getText(), ((SimpleModel) o2).getText());
    }
}
TOP

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

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.