KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > binding > ListBinding


1 /*
2  * $Id: ListBinding.java,v 1.3 2005/02/28 17:40:18 kleopatra Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.binding;
9
10 import java.beans.PropertyChangeEvent JavaDoc;
11 import java.beans.PropertyChangeListener JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.List JavaDoc;
14
15 import javax.swing.AbstractListModel JavaDoc;
16 import javax.swing.DefaultListModel JavaDoc;
17 import javax.swing.JComponent JavaDoc;
18 import javax.swing.JList JavaDoc;
19 import javax.swing.ListModel JavaDoc;
20
21 import org.jdesktop.swing.data.DataConstants;
22 import org.jdesktop.swing.data.DataModel;
23 import org.jdesktop.swing.data.MetaData;
24 import org.jdesktop.swing.data.SelectionModel;
25
26 public class ListBinding extends AbstractBinding {
27     
28     private JList JavaDoc list;
29
30     public ListBinding(JList JavaDoc list, DataModel model, String JavaDoc fieldName) {
31         super(list, model, fieldName, AbstractBinding.AUTO_VALIDATE_NONE);
32     }
33
34
35     public boolean isModified() {
36         return false;
37     }
38
39     public boolean isValid() {
40         return true;
41     }
42
43     public JComponent JavaDoc getComponent() {
44         return list;
45     }
46
47     protected void setComponent(JComponent JavaDoc component) {
48         list = (JList JavaDoc)component;
49         configureSelection();
50     }
51
52     private void configureSelection() {
53         Object JavaDoc selectionMeta = metaData.getCustomProperty(DataConstants.SELECTION_MODEL);
54         if (selectionMeta instanceof SelectionModel) {
55             new ListSelectionBinding((SelectionModel) selectionMeta, list.getSelectionModel());
56         }
57         
58     }
59
60     
61     protected void installMetaDataListener() {
62         PropertyChangeListener JavaDoc l = new PropertyChangeListener JavaDoc() {
63
64             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
65                 if (DataConstants.SELECTION_MODEL.equals(evt.getPropertyName())) {
66                     configureSelection();
67                 }
68                 
69             }
70             
71         };
72         metaData.addPropertyChangeListener(l);
73     }
74
75     
76     protected Object JavaDoc getComponentValue(){
77     ListModel JavaDoc model = list.getModel();
78     Class JavaDoc klazz = metaData.getElementClass();
79
80     if (klazz.equals(List JavaDoc.class)) {
81         List JavaDoc lvalue = new ArrayList JavaDoc();
82         for (int i = 0, size = model.getSize(); i < size; i++) {
83         lvalue.add(model.getElementAt(i));
84         }
85         return lvalue;
86     }
87     else if (klazz.isArray()) {
88
89         // XXX we lose the array type, use a generic Object[]
90
int size = model.getSize();
91         Object JavaDoc[] values = new Object JavaDoc[size];
92         for (int i = 0; i < size; i++) {
93         values[i] = model.getElementAt(i);
94         }
95         return values;
96     }
97     return null;
98     }
99
100     protected void setComponentValue(Object JavaDoc value) {
101     Class JavaDoc klazz = metaData.getElementClass();
102     if (klazz.equals(List JavaDoc.class)) {
103         List JavaDoc lvalue = (List JavaDoc)value;
104         if (lvalue != null) {
105         list.setListData(lvalue.toArray());
106         }
107     }
108     else if (klazz.isArray()) {
109         Object JavaDoc[] arrayValue = (Object JavaDoc[])value;
110
111         if (arrayValue != null) {
112         list.setListData(arrayValue);
113         } else {
114         // Empty the list.
115
list.setModel(new DefaultListModel JavaDoc());
116         }
117     }
118     }
119 }
120
121
Popular Tags