Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 
 package org.apache.cayenne.modeler.util;
 
 import java.util.List;
 
 
Superclass of CayenneModeler table model classes.

Author(s):
Andrei Adamchik
 
 public abstract class CayenneTableModel extends AbstractTableModel {
 
     protected ProjectController mediator;
     protected Object eventSource;
     protected List objectList;

    
Constructor for CayenneTableModel.
 
     public CayenneTableModel(ProjectController mediatorObject eventSource,
             java.util.List objectList) {
         super();
         this. = eventSource;
         this. = mediator;
         this. = objectList;
 
         orderList();
     }
 
     public void setValueAt(Object newValint rowint col) {
         if (!Util.nullSafeEquals(newValgetValueAt(rowcol))) {
             setUpdatedValueAt(newValrowcol);
         }
     }

    
Sets a new value after it is already checked by the superclass and it is determined that the value has changed.
 
     public abstract void setUpdatedValueAt(Object newValint rowint col);

    
Orders internal object list. Key returned by getOrderingKey is used for comparison.
 
     protected void orderList() {
         String key = getOrderingKey();
         if (key != null) {
             Collections.sort(new PropertyComparator(
                     getOrderingKey(),
                     getElementsClass()));
         }
     }

    
Returns Java class of the internal list elements.
 
     public abstract Class getElementsClass();

    
Returns the key by which to order elements in the object list. Default value is "name".
 
     public String getOrderingKey() {
         return "name";
     }

    
Returns the number of objects on the list.
    public int getRowCount() {
        return .size();
    }

    
Returns an object used as an event source.
    public Object getEventSource() {
        return ;
    }

    
Returns EventController object.
    public ProjectController getMediator() {
        return ;
    }

    
Returns internal object list.
    public java.util.List getObjectList() {
        return ;
    }
    public void addRow(Object row) {
        .add(row);
        fireTableDataChanged();
    }
    public void removeRow(Object row) {
        .remove(row);
        fireTableDataChanged();
    }

    
Moves a row up, jumping down if row is already at the top.
    public int moveRowUp(Object row) {
        int len = .size();
        if (len < 2) {
            return -1;
        }
        int ind = .indexOf(row);
        if (ind <= 0) {
            return -1;
        }
        int neighborIndex = ind - 1;
        if (neighborIndex < 0) {
            neighborIndex = len - 1;
        }
        swapRows(indneighborIndex);
        return neighborIndex;
    }

    
Moves a row down, jumping up if row is already at the bottom.
    public int moveRowDown(Object row) {
        int len = .size();
        if (len < 2) {
            return -1;
        }
        int ind = .indexOf(row);
        // not valid if it is not found or it is at the end of the list
        if (ind < 0 || (ind + 1) >= len) {
            return -1;
        }
        int neighborIndex = ind + 1;
        swapRows(indneighborIndex);
        return neighborIndex;
    }
    protected void swapRows(int iint j) {
        Collections.swap(ij);
        fireTableDataChanged();
    }
    class PropertyComparator implements Comparator {
        Method getter;
        PropertyComparator(String propertyNameClass beanClass) {
            try {
                 = findGetter(beanClasspropertyName);
            }
            catch (IntrospectionException e) {
                throw new CayenneRuntimeException("Introspection error"e);
            }
        }
        Method findGetter(Class beanClassString propertyName)
                throws IntrospectionException {
            BeanInfo info = Introspector.getBeanInfo(beanClass);
            PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
            for (int i = 0; i < descriptors.lengthi++) {
                if (propertyName.equals(descriptors[i].getName())) {
                    return descriptors[i].getReadMethod();
                }
            }
            throw new IntrospectionException("No getter found for " + propertyName);
        }
        public int compare(Object o1Object o2) {
            if ((o1 == null && o2 == null) || o1 == o2) {
                return 0;
            }
            else if (o1 == null && o2 != null) {
                return -1;
            }
            else if (o1 != null && o2 == null) {
                return 1;
            }
            try {
                Comparable p1 = (Comparable.invoke(o1null);
                Comparable p2 = (Comparable.invoke(o2null);
                return (p1 == null) ? -1 : p1.compareTo(p2);
            }
            catch (Exception ex) {
                throw new CayenneRuntimeException("Error reading property."ex);
            }
        }
    }
New to GrepCode? Check out our FAQ X