KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > gumby > view > binding > ListBinding


1 package org.sapia.gumby.view.binding;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Vector JavaDoc;
7
8 import javax.swing.JList JavaDoc;
9
10 import org.apache.commons.jxpath.JXPathContext;
11 import org.sapia.gumby.view.View;
12
13 /**
14  * @author Yanick Duchesne
15  *
16  * <dl>
17  * <dt><b>Copyright: </b>
18  * <dd>Copyright &#169; 2002-2005 <a HREF="http://www.sapia-oss.org">Sapia Open
19  * Source Software </a>. All Rights Reserved.</dd>
20  * </dt>
21  * <dt><b>License: </b>
22  * <dd>Read the license.txt file of the jar or visit the <a
23  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the Sapia
24  * OSS web site</dd>
25  * </dt>
26  * </dl>
27  */

28 public class ListBinding extends AbstractJXPathBinding {
29
30   private Collection JavaDoc _options;
31
32   public void onBound(View owner, Object JavaDoc model) {
33     JList JavaDoc list = (JList JavaDoc) acquireWidget(owner);
34     initList(list, model);
35   }
36
37   public void onChanged(View owner, Object JavaDoc model) {
38     onBound(owner, model);
39   }
40
41   public void onUpdated(View owner, Object JavaDoc model) {
42     onBound(owner, model);
43   }
44
45   public void updateModel(View owner, Object JavaDoc model) {
46     JXPathContext ctx = JXPathContext.newContext(model);
47     ctx.setLenient(false);
48     JList JavaDoc list = (JList JavaDoc) acquireWidget(owner);
49     Collection JavaDoc newValues = new ArrayList JavaDoc();
50     if(list.getSelectedIndex() >= 0) {
51       Object JavaDoc[] selected = list.getSelectedValues();
52       for(int i = 0; i < selected.length; i++) {
53         newValues.add(selected[i]);
54       }
55       try {
56         _expr.setValue(ctx, newValues);
57       } catch(RuntimeException JavaDoc e) {
58         Collection JavaDoc currentValues = (Collection JavaDoc) _expr.getValue(ctx);
59         if(currentValues != null) {
60           currentValues.clear();
61           currentValues.addAll(newValues);
62         }
63       }
64     }
65   }
66
67   public void setOptions(Collection JavaDoc options) {
68     _options = options;
69   }
70
71   public void addOption(Object JavaDoc option) {
72     if(_options == null) {
73       _options = new ArrayList JavaDoc();
74     }
75     _options.add(option);
76   }
77
78   private void initList(JList JavaDoc comp, Object JavaDoc model) {
79     JXPathContext ctx = JXPathContext.newContext(model);
80     ctx.setLenient(true);
81     Collection JavaDoc values = (Collection JavaDoc) _expr.getValue(ctx);
82     Vector JavaDoc data = new Vector JavaDoc();
83     if(values != null) {
84       if(_options != null) {
85         Iterator JavaDoc valuesItr = values.iterator();
86         while(valuesItr.hasNext()) {
87           Object JavaDoc value = valuesItr.next();
88           Iterator JavaDoc options = _options.iterator();
89           int count = 0;
90           while(options.hasNext()) {
91             Object JavaDoc current = options.next();
92             data.add(current);
93             if(current.equals(value)) {
94               comp.setSelectedIndex(count);
95             }
96             count++;
97           }
98         }
99       }
100     } else if(_options != null) {
101       Iterator JavaDoc options = _options.iterator();
102       while(options.hasNext()) {
103         data.add(options.next());
104       }
105     }
106     comp.setListData(data);
107   }
108 }
109
Popular Tags