Package com.nexirius.ulc.ulcviewer

Source Code of com.nexirius.ulc.ulcviewer.ULCObjectList

//{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.ulc.ulcviewer;

import com.nexirius.framework.datamodel.LimitIntModel;
import com.nexirius.util.EventMulticaster;
import com.nexirius.util.SortedVector;
import com.nexirius.util.assertion.Assert;
import com.nexirius.util.resource.ClientResource;
import com.ulcjava.base.application.IComboBoxModel;
import com.ulcjava.base.application.event.IListDataListener;
import com.ulcjava.base.application.event.ListDataEvent;

import java.util.Enumeration;


/**
* A class which is used to represent a com.ulcjava.base.application.IComboBoxModel based on a
* list and a current index
*/
public class ULCObjectList implements IComboBoxModel {

    public static final String _NEXIRIUS_VERSION = "3.0";
    SortedVector vector;
    LimitIntModel model = null;
    Object listenerList = null;
    ClientResource clientResource = null;

    /**
     * Creates an instance with a predefined list and a current index.
     *
     * @param v     The list which is displayed in a ComboBox
     * @param model The model which represents the current value of the combo box.
     */
    public ULCObjectList(SortedVector v, LimitIntModel model) {
        vector = v;
        this.model = model;
    }

    /**
     * Creates an object list which is based on a limit int model.
     *
     * @param model A LimitIntModel which preferably has an associated valueArray
     */
    public ULCObjectList(LimitIntModel model) {
        this(null, model);

        if (model.getValueArray() != null) {
            setArray(model.getValueArray().getArray());
        }
    }

    /**
     * Set a new Object array which represents the values which are displayed in the
     * combo box model.
     *
     * @param array Any array of objects which is used within a combo box
     */
    public void setArray(Object array[]) {
        if (array == null) {
            return;
        }

        setVector(new SortedVector(array));
    }

    /**
     * Activates the automatic translation of the text
     */
    public void activateTranslation(ClientResource cr) {
        clientResource = cr;
    }

    private String translate(String s) {
        return (clientResource == null ? s : clientResource.getLabel(s));
    }

    /**
     * Set a new list of Objects which represents the values which are displayed in the
     * combo box model.
     *
     * @param v Any list of Objects which is used within a combo box
     */
    public void setVector(SortedVector v) {
        Assert.pre(v != null && v.size() > 0, "ObjectList needs a list with at least one element");

        vector = v;
        this.model.setLimits(0, getSize() - 1);
        fireListDataChanged();
    }

    /**
     * Returns the currently selected element
     */
    public Object getSelectedItem() {
        model.validate();

        Object ret = vector.elementAt(model.getInt());

        if (ret instanceof String) {
            ret = translate((String) ret);
        }

        return ret;
    }

    /**
     * Sets the currently selected status to a specific object.
     */
    public void setSelectedItem(Object o) {
        if (clientResource == null) {
            model.setInt(vector.indexOf(o));
        } else {
            Enumeration e = vector.elements();
            int i = 0;

            while (e.hasMoreElements()) {
                Object element = e.nextElement();

                if (element instanceof String) {

                    if (translate((String) element).equals(o)) {
                        model.setInt(i);

                        break;
                    }
                }
                ++i;
            }
        }
    }

    /**
     * Returns the string representation of selected status (method toString())
     */
    public String getText() {
        return translate(getSelectedItem().toString());
    }

    /**
     * Try to set the index to the first element in the list which starts with the given text.
     *
     * @param text This string is used in object.toString().startsWith(text) where object is any
     *             one of the vector elements.
     * @return true If an element from the list starts with text
     */
    public boolean setStartText(String text) {
        int i = 0;

        for (Object o = vector.firstObject(); o != null; o = vector.nextObject()) {

            if (translate(o.toString()).startsWith(text)) {
                //model.startEdit();
                model.setInt(i);
                //model.finishEdit();

                return true;
            }
            ++i;
        }

        return false;
    }

    public int getIndexOf(String text) {
        int i = 0;

        for (Object o = vector.firstObject(); o != null; o = vector.nextObject()) {

            if (translate(o.toString()).equals(text)) {

                return i;
            }
            ++i;
        }

        return -1;
    }

    /**
     * Return true if at least one element is in the list.
     */
    public boolean hasList() {
        return vector != null && vector.size() > 0;
    }

    /**
     * Returns the value at the specified index.
     */
    public Object getElementAt(int index) {
        Object value = vector.elementAt(index);

        if (value instanceof String) {
            value = translate((String) value);
        }

        return value;
    }

    /**
     * Returns the length of the list.
     */
    public int getSize() {
        return vector.size();
    }

    public void addListDataListener(IListDataListener l) {
        this.listenerList = EventMulticaster.addEventListener(this.listenerList, l);
    }

    /**
     * Remove a ListDataListener from the list that was notified each time a change to the data model occured.
     */
    public synchronized void removeListDataListener(IListDataListener l) {
        this.listenerList = EventMulticaster.removeEventListener(this.listenerList, l);
    }

    /**
     * Fire a ListDataEvent.CONTENTS_CHANGED event to all the registered listeners
     */
    public void fireListDataChanged() {
        ListDataEvent e = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, getSize() - 1);
        fireListDataEvent(e);
    }

    private void fireListDataEvent(ListDataEvent e) {
        Object listeners[] = EventMulticaster.getArray(listenerList);
        boolean needGarbageCollect = false;

        if (listeners != null) {
            for (int i = 0; i < listeners.length; ++i) {

                if (listeners[i] == null) {
                    needGarbageCollect = true;
                } else {
                    ((IListDataListener) listeners[i]).contentsChanged(e);
                }
            }
        }

        if (needGarbageCollect) {
            listenerList = EventMulticaster.garbageCollect(listenerList);
        }
    }
}
TOP

Related Classes of com.nexirius.ulc.ulcviewer.ULCObjectList

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.