KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dataview > DOListModel


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.dataview;
21
22 import javax.swing.AbstractListModel JavaDoc;
23
24 import org.apache.cayenne.DataObject;
25
26 /**
27  * A Swing ListModel wrapping {@link DataObjectList}.
28  *
29  * @since 1.1
30  */

31 public class DOListModel extends AbstractListModel JavaDoc implements DataObjectChangeListener,
32         FieldValueChangeListener {
33
34     protected ObjEntityViewField viewField;
35     protected DataObjectList dataObjects = new DataObjectList(1);
36
37     public DOListModel() {
38     }
39
40     public void setViewField(ObjEntityViewField field) {
41         if (this.viewField != null) {
42             this.viewField.getOwner().getOwner().removeFieldValueChangeListener(this);
43         }
44         this.viewField = field;
45         viewField.getOwner().getOwner().addFieldValueChangeListener(this);
46         fireContentsChanged(this, 0, getSize());
47     }
48
49     public void setDataObjects(DataObjectList dataObjects) {
50         this.dataObjects.removeDataObjectChangeListener(this);
51         this.dataObjects = dataObjects;
52         this.dataObjects.addDataObjectChangeListener(this);
53         fireContentsChanged(this, 0, getSize());
54     }
55
56     public int getSize() {
57         return dataObjects.size();
58     }
59
60     public DataObject getDataObject(int index) {
61         return (DataObject) dataObjects.get(index);
62     }
63
64     public Object JavaDoc getElementAt(int index) {
65         if (viewField == null)
66             return getDataObject(index);
67         return viewField.getValue(getDataObject(index));
68     }
69
70     public void dataChanged(DataObjectChangeEvent event) {
71         if (event.isMultiObjectChange()) {
72             fireContentsChanged(this, 0, getSize());
73             return;
74         }
75         int affectedRow = event.getAffectedDataObjectIndex();
76         switch (event.getId()) {
77             case DataObjectChangeEvent.DATAOBJECT_ADDED:
78                 fireIntervalAdded(this, affectedRow, affectedRow);
79                 break;
80             case DataObjectChangeEvent.DATAOBJECT_REMOVED:
81                 fireIntervalRemoved(this, affectedRow, affectedRow);
82                 break;
83             case DataObjectChangeEvent.DATAOBJECT_CHANGED:
84                 fireContentsChanged(this, affectedRow, affectedRow);
85                 break;
86             default:
87                 fireContentsChanged(this, 0, getSize());
88         }
89     }
90
91     public ObjEntityViewField getViewField() {
92         return viewField;
93     }
94
95     public DataObjectList getDataObjects() {
96         return dataObjects;
97     }
98
99     public void fieldValueChanged(FieldValueChangeEvent event) {
100         if (viewField != null && viewField.isSameObjAttribute(event.getField())) {
101             int index = dataObjects.indexOf(event.getModifiedObject());
102             if (index >= 0)
103                 fireContentsChanged(this, index, index);
104         }
105     }
106 }
107
Popular Tags