1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
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.apache.commons.jelly.swing;
17  
18  import javax.swing.table.AbstractTableModel;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  /***
24   * A sample table model
25   *
26   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
27   * @version $Revision: 1.8 $
28   */
29  public class MyTableModel extends AbstractTableModel {
30  
31      /*** The Log to which logging calls will be made. */
32      private static final Log log = LogFactory.getLog(MyTableModel.class);
33  
34      public MyTableModel() {
35      }
36  
37  
38      final String[] columnNames = {
39          "First Name",
40          "Last Name",
41          "Sport",
42          "# of Years",
43          "Vegetarian"
44      };
45      final Object[][] data = {
46          {"Mary", "Campione",
47           "Snowboarding", new Integer(5), new Boolean(false)},
48          {"Alison", "Huml",
49           "Rowing", new Integer(3), new Boolean(true)},
50          {"Kathy", "Walrath",
51           "Chasing toddlers", new Integer(2), new Boolean(false)},
52          {"Mark", "Andrews",
53           "Speed reading", new Integer(20), new Boolean(true)},
54          {"Angela", "Lih",
55           "Teaching high school", new Integer(4), new Boolean(false)}
56          };
57  
58      public int getColumnCount() {
59          return columnNames.length;
60      }
61  
62      public int getRowCount() {
63          return data.length;
64      }
65  
66      public String getColumnName(int col) {
67          return columnNames[col];
68      }
69  
70      public Object getValueAt(int row, int col) {
71          return data[row][col];
72      }
73  
74      /*
75       * JTable uses this method to determine the default renderer/
76       * editor for each cell.  If we didn't implement this method,
77       * then the last column would contain text ("true"/"false"),
78       * rather than a check box.
79       */
80      public Class getColumnClass(int c) {
81          return getValueAt(0, c).getClass();
82      }
83  
84      /*
85       * Don't need to implement this method unless your table's
86       * editable.
87       */
88      public boolean isCellEditable(int row, int col) {
89          //Note that the data/cell address is constant,
90          //no matter where the cell appears onscreen.
91          if (col < 2) {
92              return false;
93          } else {
94              return true;
95          }
96      }
97  
98      /*
99       * Don't need to implement this method unless your table's
100      * data can change.
101      */
102     public void setValueAt(Object value, int row, int col) {
103         if (log.isDebugEnabled()) {
104             log.debug("Setting value at " + row + "," + col
105                                + " to " + value
106                                + " (an instance of "
107                                + value.getClass() + ")");
108         }
109 
110         if (data[0][col] instanceof Integer
111                 && !(value instanceof Integer)) {
112             //With JFC/Swing 1.1 and JDK 1.2, we need to create
113             //an Integer from the value; otherwise, the column
114             //switches to contain Strings.  Starting with v 1.3,
115             //the table automatically converts value to an Integer,
116             //so you only need the code in the 'else' part of this
117             //'if' block.
118             //XXX: See TableEditDemo.java for a better solution!!!
119             try {
120                 data[row][col] = new Integer(value.toString());
121                 fireTableCellUpdated(row, col);
122             } catch (NumberFormatException e) {
123                 log.error( "The \"" + getColumnName(col)
124                     + "\" column accepts only integer values.");
125             }
126         } else {
127             data[row][col] = value;
128             fireTableCellUpdated(row, col);
129         }
130     }
131 
132 
133 }