Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jorphan.gui » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *   http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    *
   17    */
   18   
   19   package org.apache.jorphan.gui;
   20   
   21   import java.util.ArrayList;
   22   import java.util.Arrays;
   23   import java.util.Iterator;
   24   import java.util.List;
   25   
   26   import javax.swing.event.TableModelEvent;
   27   import javax.swing.table.DefaultTableModel;
   28   
   29   import org.apache.jorphan.logging.LoggingManager;
   30   import org.apache.jorphan.reflect.Functor;
   31   import org.apache.log.Logger;
   32   
   33   /**
   34    * The ObjectTableModel is a TableModel whose rows are objects;
   35    * columns are defined as Functors on the object.
   36    */
   37   public class ObjectTableModel extends DefaultTableModel {
   38       private static final Logger log = LoggingManager.getLoggerForClass();
   39   
   40       private static final long serialVersionUID = 233L;
   41   
   42       private transient ArrayList objects = new ArrayList();
   43   
   44       private transient List headers = new ArrayList();
   45   
   46       private transient ArrayList classes = new ArrayList();
   47   
   48       private transient ArrayList readFunctors = new ArrayList();
   49   
   50       private transient ArrayList writeFunctors = new ArrayList();
   51   
   52       private transient Class objectClass = null; // if provided
   53   
   54       /**
   55        * The ObjectTableModel is a TableModel whose rows are objects;
   56        * columns are defined as Functors on the object.
   57        *
   58        * @param headers - Column names
   59        * @param _objClass - Object class that will be used
   60        * @param readFunctors - used to get the values
   61        * @param writeFunctors - used to set the values
   62        * @param editorClasses - class for each column
   63        */
   64       public ObjectTableModel(String[] headers, Class _objClass, Functor[] readFunctors, Functor[] writeFunctors, Class[] editorClasses) {
   65           this(headers, readFunctors, writeFunctors, editorClasses);
   66           this.objectClass=_objClass;
   67       }
   68   
   69       /**
   70        * The ObjectTableModel is a TableModel whose rows are objects;
   71        * columns are defined as Functors on the object.
   72        *
   73        * @param headers - Column names
   74        * @param readFunctors - used to get the values
   75        * @param writeFunctors - used to set the values
   76        * @param editorClasses - class for each column
   77        */
   78       public ObjectTableModel(String[] headers, Functor[] readFunctors, Functor[] writeFunctors, Class[] editorClasses) {
   79           this.headers.addAll(Arrays.asList(headers));
   80           this.classes.addAll(Arrays.asList(editorClasses));
   81           this.readFunctors = new ArrayList(Arrays.asList(readFunctors));
   82           this.writeFunctors = new ArrayList(Arrays.asList(writeFunctors));
   83   
   84           int numHeaders = headers.length;
   85   
   86           int numClasses = classes.size();
   87           if (numClasses != numHeaders){
   88               log.warn("Header count="+numHeaders+" but classes count="+numClasses);
   89           }
   90   
   91           // Functor count = 0 is handled specially
   92           int numWrite = writeFunctors.length;
   93           if (numWrite > 0 && numWrite != numHeaders){
   94               log.warn("Header count="+numHeaders+" but writeFunctor count="+numWrite);
   95           }
   96   
   97           int numRead = readFunctors.length;
   98           if (numRead > 0 && numRead != numHeaders){
   99               log.warn("Header count="+numHeaders+" but readFunctor count="+numRead);
  100           }
  101       }
  102   
  103       private Object readResolve() {
  104           objects = new ArrayList();
  105           headers = new ArrayList();
  106           classes = new ArrayList();
  107           readFunctors = new ArrayList();
  108           writeFunctors = new ArrayList();
  109           return this;
  110       }
  111   
  112       public Iterator iterator() {
  113           return objects.iterator();
  114       }
  115   
  116       public void clearData() {
  117           int size = getRowCount();
  118           objects.clear();
  119           super.fireTableRowsDeleted(0, size);
  120       }
  121   
  122       public void addRow(Object value) {
  123           log.debug("Adding row value: " + value);
  124           if (objectClass != null) {
  125               final Class valueClass = value.getClass();
  126               if (!objectClass.isAssignableFrom(valueClass)){
  127                   throw new IllegalArgumentException("Trying to add class: "+valueClass.getName()
  128                           +"; expecting class: "+objectClass.getName());
  129               }
  130           }
  131           objects.add(value);
  132           super.fireTableRowsInserted(objects.size() - 1, objects.size());
  133       }
  134   
  135       public void insertRow(Object value, int index) {
  136           objects.add(index, value);
  137           super.fireTableRowsInserted(index, index + 1);
  138       }
  139   
  140       /**
  141        * @see javax.swing.table.TableModel#getColumnCount()
  142        */
  143       public int getColumnCount() {
  144           return headers.size();
  145       }
  146   
  147       /**
  148        * @see javax.swing.table.TableModel#getColumnName(int)
  149        */
  150       public String getColumnName(int col) {
  151           return (String) headers.get(col);
  152       }
  153   
  154       /**
  155        * @see javax.swing.table.TableModel#getRowCount()
  156        */
  157       public int getRowCount() {
  158           if (objects == null) {
  159               return 0;
  160           }
  161           return objects.size();
  162       }
  163   
  164       /**
  165        * @see javax.swing.table.TableModel#getValueAt(int, int)
  166        */
  167       public Object getValueAt(int row, int col) {
  168           log.debug("Getting row value");
  169           Object value = objects.get(row);
  170           if(headers.size() == 1 && col >= readFunctors.size()) {
  171               return value;
  172           }
  173           Functor getMethod = (Functor) readFunctors.get(col);
  174           if (getMethod != null && value != null) {
  175               return getMethod.invoke(value);
  176           }
  177           return null;
  178       }
  179   
  180       /**
  181        * @see javax.swing.table.TableModel#isCellEditable(int, int)
  182        */
  183       public boolean isCellEditable(int arg0, int arg1) {
  184           return true;
  185       }
  186   
  187       /**
  188        * @see javax.swing.table.DefaultTableModel#moveRow(int, int, int)
  189        */
  190       public void moveRow(int start, int end, int to) {
  191           List subList = objects.subList(start, end);
  192           for (int x = end - 1; x >= start; x--) {
  193               objects.remove(x);
  194           }
  195           objects.addAll(to, subList);
  196           super.fireTableChanged(new TableModelEvent(this));
  197       }
  198   
  199       /**
  200        * @see javax.swing.table.DefaultTableModel#removeRow(int)
  201        */
  202       public void removeRow(int row) {
  203           objects.remove(row);
  204           super.fireTableRowsDeleted(row, row);
  205       }
  206   
  207       /**
  208        * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
  209        */
  210       public void setValueAt(Object cellValue, int row, int col) {
  211           if (row < objects.size()) {
  212               Object value = objects.get(row);
  213               if (col < writeFunctors.size()) {
  214                   Functor setMethod = (Functor) writeFunctors.get(col);
  215                   if (setMethod != null) {
  216                       setMethod.invoke(value, new Object[] { cellValue });
  217                       super.fireTableDataChanged();
  218                   }
  219               }
  220               else if(headers.size() == 1)
  221               {
  222                   objects.set(row,cellValue);
  223               }
  224           }
  225       }
  226   
  227       /**
  228        * @see javax.swing.table.TableModel#getColumnClass(int)
  229        */
  230       public Class getColumnClass(int arg0) {
  231           return (Class) classes.get(arg0);
  232       }
  233   
  234       /**
  235        * Check all registered functors.
  236        * <p>
  237        * <b>** only for use in unit test code **</b>
  238        * </p>
  239        *
  240        * @param _value - an instance of the table model row data item
  241        * (if null, use the class passed to the constructor).
  242        *
  243        * @param caller - class of caller.
  244        *
  245        * @return false if at least one Functor cannot be found.
  246        */
  247       public boolean checkFunctors(Object _value, Class caller){
  248           Object value;
  249           if (_value == null && objectClass != null) {
  250               try {
  251                   value = objectClass.newInstance();
  252               } catch (InstantiationException e) {
  253                   log.error("Cannot create instance of class "+objectClass.getName(),e);
  254                   return false;
  255               } catch (IllegalAccessException e) {
  256                   log.error("Cannot create instance of class "+objectClass.getName(),e);
  257                   return false;
  258               }
  259           } else {
  260               value = _value;
  261           }
  262           boolean status = true;
  263           for(int i=0;i<getColumnCount();i++){
  264               Functor setMethod = (Functor) writeFunctors.get(i);
  265               if (setMethod != null) {
  266                   if (!setMethod.checkMethod(value,getColumnClass(i))){
  267                       status=false;
  268                       log.warn(caller.getName()+" is attempting to use nonexistent "+setMethod.toString());
  269                   }
  270               }
  271               Functor getMethod = (Functor) readFunctors.get(i);
  272               if (getMethod != null) {
  273                   if (!getMethod.checkMethod(value)){
  274                       status=false;
  275                       log.warn(caller.getName()+" is attempting to use nonexistent "+getMethod.toString());
  276                   }
  277               }
  278   
  279           }
  280           return status;
  281       }
  282   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jorphan.gui » [javadoc | source]