001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui.model;
023:
024: import java.io.Serializable;
025: import java.util.ArrayList;
026: import java.util.Collection;
027: import java.util.List;
028:
029: import org.beryl.gui.GUIException;
030: import org.beryl.gui.View;
031: import org.beryl.gui.widgets.Item;
032:
033: /**
034: * List-based data model
035: */
036:
037: public class ListDataModel extends AbstractDataModel implements
038: Serializable {
039: private List list = null;
040:
041: public ListDataModel() {
042: list = new ArrayList();
043: }
044:
045: public ListDataModel(List list) {
046: this .list = list;
047: }
048:
049: public ListDataModel(Collection collection) {
050: list = new ArrayList(collection);
051: }
052:
053: public void setValue(int index, Object newValue)
054: throws GUIException {
055: setValue(null, index, newValue);
056: }
057:
058: public void setValue(View view, int index, Object newValue)
059: throws GUIException {
060: ListChangeEvent event = new ListChangeEvent(view, this ,
061: ListChangeEvent.INTERVAL_CHANGED, index, index);
062: list.set(index, newValue);
063: fireModelEvent(event);
064: }
065:
066: public void removeIndex(View view, int index) throws GUIException {
067: ListChangeEvent event = new ListChangeEvent(view, this ,
068: ListChangeEvent.INTERVAL_REMOVED, index, index);
069: list.remove(index);
070: fireModelEvent(event);
071: }
072:
073: public void removeValue(View view, Object value)
074: throws GUIException {
075: int index = list.indexOf(value);
076: if (index == -1)
077: throw new GUIException("Value could not be found");
078: removeIndex(view, index);
079: }
080:
081: public void insertValue(View view, int index, Object value)
082: throws GUIException {
083: if (value instanceof Item) {
084: ((Item) value).setOwningDataModel(this );
085: }
086:
087: ListChangeEvent event = new ListChangeEvent(view, this ,
088: ListChangeEvent.INTERVAL_ADDED, index, index);
089: list.add(index, value);
090: fireModelEvent(event);
091: }
092:
093: public void addValue(View view, Object value) throws GUIException {
094: insertValue(view, list.size(), value);
095: }
096:
097: public void addValue(Object value) throws GUIException {
098: insertValue(null, list.size(), value);
099: }
100:
101: public void clear(View view) throws GUIException {
102: ListChangeEvent event = new ListChangeEvent(view, this ,
103: ListChangeEvent.INTERVAL_REMOVED, 0, list.size() - 1);
104: list.clear();
105: fireModelEvent(event);
106: }
107:
108: public int indexOf(Object value) {
109: return list.indexOf(value);
110: }
111:
112: public Object getValue(int index) {
113: return list.get(index);
114: }
115:
116: public int getSize() {
117: return list.size();
118: }
119: }
|