View Javadoc

1   /*
2      Copyright 2002-2006 Martin van den Bemt
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  package org.xulux.guilayer.swing.models;
17  
18  import java.util.List;
19  
20  import javax.swing.event.TableModelListener;
21  import javax.swing.table.TableModel;
22  
23  import org.xulux.api.dataprovider.IConverter;
24  import org.xulux.api.dataprovider.IDataProvider;
25  import org.xulux.api.dataprovider.IField;
26  import org.xulux.api.dataprovider.IMapping;
27  import org.xulux.api.gui.IWidget;
28  import org.xulux.core.XuluxContext;
29  import org.xulux.dataprovider.Dictionary;
30  import org.xulux.gui.XuluxListener;
31  import org.xulux.guilayer.swing.widgets.Table;
32  import org.xulux.utils.BooleanUtils;
33  
34  /**
35   * The nyx tablemodel contains all magic for tables.
36   * @todo Assumes lists right now.. Should support more probably..
37   *
38   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
39   * @version $Id: NyxTableModel.java,v 1.1 2005/12/18 12:58:21 mvdb Exp $
40   */
41  public class NyxTableModel extends XuluxListener implements TableModel {
42  
43      /**
44       * the table
45       */
46      protected Table table;
47      /**
48       * the tablemodel
49       */
50      protected TableModel model;
51  
52      /**
53       *
54       */
55      public NyxTableModel() {
56          super();
57      }
58  
59      /**
60       * @param table the table
61       */
62      public NyxTableModel(Table table) {
63          setTable(table);
64      }
65  
66      /**
67       * @param model the model
68       * @param table the table
69       */
70      public NyxTableModel(TableModel model, Table table) {
71          setTable(table);
72          setModel(model);
73      }
74  
75      /**
76       *
77       * @param model the model
78       */
79      public void setModel(TableModel model) {
80          this.model = model;
81      }
82  
83      /**
84       * @return if the model has a model already
85       */
86      public boolean hasModel() {
87          return this.model != null;
88      }
89  
90      /**
91       * Set the table to be used in this tablemodel
92       * @param table the table
93       */
94      public void setTable(Table table) {
95          this.table = table;
96      }
97  
98      /**
99       * @see javax.swing.table.TableModel#getRowCount()
100      */
101     public int getRowCount() {
102         if (hasModel()) {
103             return model.getRowCount();
104         }
105         int rowCount = 0;
106         if (table != null) {
107             if (table.getContent() != null) {
108                 rowCount = ((List) table.getContent()).size();
109             }
110         }
111 //        System.err.println("rowcount : " + rowCount);
112         return rowCount;
113     }
114 
115     /**
116      * Not used,we use a columnmodel.
117      *
118      * @see javax.swing.table.TableModel#getColumnCount()
119      */
120     public int getColumnCount() {
121         if (hasModel()) {
122             return model.getColumnCount();
123         }
124         return 0;
125     }
126 
127     /**
128      * Not used, we use a columnmodel for that...
129      * @see javax.swing.table.TableModel#getColumnName(int)
130      */
131     public String getColumnName(int columnIndex) {
132         if (hasModel()) {
133             return model.getColumnName(columnIndex);
134         }
135         return String.valueOf(columnIndex);
136     }
137 
138     /**
139      * @see javax.swing.table.TableModel#getColumnClass(int)
140      */
141     public Class getColumnClass(int columnIndex) {
142         if (hasModel()) {
143             return model.getColumnClass(columnIndex);
144         }
145         return String.class;
146     }
147 
148     /**
149      * @see javax.swing.table.TableModel#isCellEditable(int, int)
150      */
151     public boolean isCellEditable(int rowIndex, int columnIndex) {
152         if (hasModel()) {
153             return model.isCellEditable(rowIndex, columnIndex);
154         }
155         String editable = ((IWidget) table.getChildWidgets().get(columnIndex)).getProperty("editable");
156         return BooleanUtils.toBoolean(editable);
157     }
158 
159     /**
160      * Returns the row object if the columnIndex is -1.
161      *
162      * @see javax.swing.table.TableModel#getValueAt(int, int)
163      */
164     public Object getValueAt(int rowIndex, int columnIndex) {
165         if (hasModel()) {
166             return model.getValueAt(rowIndex, columnIndex);
167         }
168         if (columnIndex == -1) {
169             return ((List) table.getContent()).get(rowIndex);
170         }
171         IWidget w = (IWidget) table.getChildWidgets().get(columnIndex);
172         if (w.getProvider() != null && w.getField() != null) {
173             IDataProvider provider = XuluxContext.getDictionary().getProvider(w.getProvider());
174             Object rowValue = ((List) table.getContent()).get(rowIndex);
175             return provider.getValue(rowValue, w.getField(), rowValue);
176         } else if (w.getField() != null) {
177             IMapping map = null;
178             String alias = w.getProperty("alias");
179             Object obj = ((List) table.getContent()).get(rowIndex);
180             if (alias != null) {
181                 map = XuluxContext.getDictionary().getDefaultProvider().getMapping(alias);
182             }
183             if (map == null) {
184                 map = XuluxContext.getDictionary().getDefaultProvider().getMapping(obj);
185             }
186             IField field = map.getField(w.getField());
187             if (field != null) {
188                 Object value = field.getValue(obj);
189                 if (value == null) {
190                    return "";
191                 }
192                 IConverter converter = Dictionary.getConverter(value);
193                 if (converter != null) {
194                     value = converter.getGuiValue(value);
195                 }
196                 return value;
197             }
198         }
199         return "";
200     }
201 
202     /**
203      * Sets the value of a bean..
204      *
205      * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
206      */
207     public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
208         if (hasModel()) {
209             model.setValueAt(aValue, rowIndex, columnIndex);
210             return;
211         }
212         IWidget w = (IWidget) table.getChildWidgets().get(columnIndex);
213         if (w.getField() != null) {
214             IMapping map = XuluxContext.getDictionary().getDefaultProvider().getMapping(((List) table.getContent()).get(rowIndex));
215             IField field = map.getField(w.getField());
216             if (field != null) {
217                 field.setValue(((List) table.getContent()).get(rowIndex), aValue);
218             }
219         }
220         // process rules, etc..
221         this.widget = w;
222         // fire post rule if any are present..
223         completed();
224     }
225 
226     /**
227      * @see javax.swing.table.TableModel#addTableModelListener(javax.swing.event.TableModelListener)
228      */
229     public void addTableModelListener(TableModelListener l) {
230         if (hasModel()) {
231             model.addTableModelListener(l);
232             return;
233         }
234     }
235 
236     /**
237      * @see javax.swing.table.TableModel#removeTableModelListener(javax.swing.event.TableModelListener)
238      */
239     public void removeTableModelListener(TableModelListener l) {
240         if (hasModel()) {
241             model.removeTableModelListener(l);
242             return;
243         }
244     }
245 
246     /**
247      * Destroy the tablemodel..
248      *
249      */
250     public void destroy() {
251         this.table = null;
252         this.model = null;
253     }
254 
255     /**
256      * refreshes the table, since there is new content
257      */
258     public void refresh() {
259         
260     }
261 
262 }