/*
 * jets3t : Java Extra-Tasty S3 Toolkit (for Amazon S3 online storage service)
 * This is a java.net project, see https://jets3t.dev.java.net/
 * 
 * Copyright 2008 James Murty
 * 
 * Licensed 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.jets3t.apps.cockpit.gui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;

import javax.swing.table.DefaultTableModel;

import org.jets3t.service.model.S3Object;

/**
 * A table model to store {@link S3Object}s.
 * 
 * @author James Murty
 */
public class ObjectTableModel extends DefaultTableModel {
    private static final long serialVersionUID = 8570725021470237261L;
    
    private ArrayList objectList = new ArrayList();
    
    public ObjectTableModel() {
        super(new String[] {"Object Key","Size","Last Modified"}, 0);
    }
    
    public int addObject(S3Object object) {
        int insertRow = 
            Collections.binarySearch(objectList, object, new Comparator() {
                public int compare(Object o1, Object o2) {
                    return ((S3Object)o1).getKey().compareToIgnoreCase(((S3Object)o2).getKey());
                }
            });
        if (insertRow >= 0) {
            // We already have an item with this key, replace it.
            objectList.remove(insertRow);
            this.removeRow(insertRow);                
        } else {
            insertRow = (-insertRow) - 1;                
        }
        // New object to insert.
        objectList.add(insertRow, object);
        this.insertRow(insertRow, new Object[] {object.getKey(), 
            new Long(object.getContentLength()), object.getLastModifiedDate()});
        
        return insertRow;
    }
    
    public void addObjects(S3Object[] objects) {
        for (int i = 0; i < objects.length; i++) {
            addObject(objects[i]);
        }
    }
    
    public void removeObject(S3Object object) {
        int index = objectList.indexOf(object);
        this.removeRow(index);
        objectList.remove(object);
    }
    
    public void removeAllObjects() {
        int rowCount = this.getRowCount();
        for (int i = 0; i < rowCount; i++) {
            this.removeRow(0);
        }
        objectList.clear();
    }
    
    public S3Object getObject(int row) {
        synchronized (objectList) {
            return (S3Object) objectList.get(row);
        }
    }
    
    public S3Object[] getObjects() {
        synchronized (objectList) {
            return (S3Object[]) objectList.toArray(new S3Object[objectList.size()]);
        }            
    }
    
    public boolean isCellEditable(int row, int column) {
        return false;
    }
    
    public Class getColumnClass(int columnIndex) {
        if (columnIndex == 0) {
            return String.class;
        } else if (columnIndex == 1) {
            return Long.class;
        } else {
            return Date.class;
        }
    }
    
}
@@ -2486,32 +2486,62 @@
 ndexOf(object);%0A
+        if (index %3E= 0) %7B%0A    
         this.rem
@@ -2555,32 +2555,36 @@
 index);%0A        
+   
+ 
 objectList.remov
@@ -2590,24 +2590,34 @@
 ve(object);%0A
+        %7D%0A
     %7D%0A    %0A 

Fix to avoid odd occasional errors when removing objects from the table.