//{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.simpletype.SimpleType_array;
import javax.swing.*;
/**
* A class that represents an array of simple type information which can be displayed as a list.
*
* @author Marcel Baumann
*/
public class SimpleArrayModel extends SimpleModel {
MyListModel listModel = null;
public SimpleArrayModel() {
super(new String[0]);
}
/**
* Creates an instance and initializes it with a value
*
* @param value Any array of Objects
*/
public SimpleArrayModel(Object value[]) {
super(new SimpleType_array(value));
}
/**
* Creates an instance and initializes it with a value and field name
*
* @param value Any array of Objects
* @param fieldName The initial field name
*/
public SimpleArrayModel(Object value[], String fieldName) {
super(new SimpleType_array(value), fieldName);
}
/**
* Returns the current value as array of Object
*/
public Object[] getArray() {
return ((SimpleType_array) getSimpleType()).getArray();
}
/**
* Set the new value as array of Object
*
* @param array The new value
*/
public void setArray(Object array[]) {
setValue(new SimpleType_array(array));
}
/**
* Get the current length of the associated array
*/
public int getLength() {
return ((SimpleType_array) getSimpleType()).getLength();
}
/*
* remove the contents
*/
public void clear() {
// ignored by default
}
/**
* Return the array member element at a given index.
*
* @param index An index which between 0 and getLength()-1 (otherwise a RuntimeException is raised)
* @return The element
*/
public Object getItem(int index) {
return ((SimpleType_array)getSimpleType()).getItem(index);
}
public Object setItem(int index, Object newValue) {
Object oldValue = getItem(index);
((SimpleType_array)getSimpleType()).setItem(index, newValue);
boolean valueChanged = false;
if (oldValue == null) {
valueChanged = newValue != null;
} else {
valueChanged = !oldValue.equals(newValue);
}
if (valueChanged) {
fireValueChange(this, new DataModelEvent(this, DataModelEvent.VALUE_CHANGE, DataModelEvent.SUBTYPE_DEFAULT, index, null));
}
return oldValue;
}
/**
* Set the new value as text. The text is basically a list of string literals with double quotes (").
* The component types of the array are supposed to have a constructor which takes a string as argument
* to initialize the component.
*
* @param text The text which is parsed to produce a new simple array value
* @throws Exception If the text is not parsed correctly
*/
public void setText(String text)
throws Exception {
setValue(new SimpleType_array(text));
}
/**
* Creates an exact copy of the array. The array is shared among the original and the copy!
*/
public DataModel duplicate(DataModel instance, CopyPairs copyPairs) {
if (instance == null) {
instance = new SimpleArrayModel(getArray());
}
if (copyPairs == null) {
copyPairs = new CopyPairs();
}
return super.duplicate(instance, copyPairs);
}
/**
* Returns a Swing list model which represents the values of the array.
*/
public ListModel getListModel() {
if (listModel == null) {
listModel = new MyListModel();
addSoftDataModelListener(listModel);
}
return listModel;
}
class MyListModel extends AbstractListModel implements DataModelListener {
public Object getElementAt(int index) {
return ((SimpleType_array) getSimpleType()).getItem(index);
}
public int getSize() {
return getLength();
}
public void dataModelChangeValue(DataModelEvent event) {
int index = event.getIndex();
if (index >= 0) {
fireContentsChanged(this, index, index);
}
fireContentsChanged(this, 0, getSize());
}
public void dataModelChangeStructure(DataModelEvent event) {
}
public void dataModelGrabFocus(DataModelEvent event) {
}
}
}