Index: SortableRendererDev.java
===================================================================
--- SortableRendererDev.java	(revision 43340)
+++ SortableRendererDev.java	(revision 44360)
@@ -1,6 +1,25 @@
 /* Glazed Lists                                                 (c) 2003-2006 */
 /* http://publicobject.com/glazedlists/                      publicobject.com,*/
 /*                                                     O'Dell Engineering Ltd.*/
+
+
+//The contents of this file are subject to the Mozilla Public License
+//Version 1.1 (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.mozilla.org/MPL/
+//
+//Software distributed under the License is distributed on an "AS IS"
+//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+//License for the specific language governing rights and limitations
+//under the License.
+//
+//The Original Code is Glazed Lists SVN trunk revision 2321
+//The Initial Developer of the Original Code is James Lemieux.
+
+//Portions created by Biomatters employees are Copyright Biomatters Ltd 2011. All Rights Reserved.
+//Contributor(s): Dr Firas Swidan, Simon Buxton
+
+
 package com.biomatters.plugins.annotationTableViewer.glazedLists.modifications;
 
 import javax.swing.*;
@@ -24,4 +43,4 @@
      *      <code>null</code> if there is not sorting
      */
     public void setSortIcon(Icon sortIcon);
-}
\ No newline at end of file
+}
Index: TableComparatorChooserDev.java
===================================================================
--- TableComparatorChooserDev.java	(revision 43340)
+++ TableComparatorChooserDev.java	(revision 44360)
@@ -1,6 +1,25 @@
 /* Glazed Lists                                                 (c) 2003-2006 */
 /* http://publicobject.com/glazedlists/                      publicobject.com,*/
 /*                                                     O'Dell Engineering Ltd.*/
+
+
+//The contents of this file are subject to the Mozilla Public License
+//Version 1.1 (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.mozilla.org/MPL/
+//
+//Software distributed under the License is distributed on an "AS IS"
+//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+//License for the specific language governing rights and limitations
+//under the License.
+//
+//The Original Code is Glazed Lists SVN trunk revision 2321
+//The Initial Developer of the Original Code is Jesse Wilson.
+//
+//Portions created by Biomatters employees are Copyright Biomatters Ltd 2011. All Rights Reserved.
+//Contributor(s): Dr Firas Swidan, Simon Buxton
+
+
 package com.biomatters.plugins.annotationTableViewer.glazedLists.modifications;
 
 import ca.odell.glazedlists.EventList;
@@ -9,6 +28,9 @@
 import ca.odell.glazedlists.gui.TableFormat;
 import ca.odell.glazedlists.impl.SortIconFactory;
 import ca.odell.glazedlists.impl.gui.SortingStrategy;
+import com.biomatters.components.HideableColumnsTableColumnModel;
+import com.biomatters.geneious.common.logs.Logs;
+import com.biomatters.plugins.annotationTableViewer.AnnotationTablePreferences;
 
 import javax.swing.*;
 import javax.swing.event.TableModelEvent;
@@ -16,6 +38,7 @@
 import javax.swing.plaf.UIResource;
 import javax.swing.table.DefaultTableCellRenderer;
 import javax.swing.table.TableCellRenderer;
+import javax.swing.table.TableColumn;
 import javax.swing.table.TableColumnModel;
 import java.awt.*;
 import java.awt.event.ActionEvent;
@@ -82,6 +105,7 @@
 
     /** when somebody clicks on the header, update the sorting state */
     private final HeaderClickHandler headerClickHandler;
+    private AnnotationTablePreferences annotationTablePreferences;
 
     /**
      * Creates and installs a TableComparatorChooser.
@@ -115,6 +139,65 @@
         headerClickHandler = new HeaderClickHandler(table, (SortingStrategy)strategy);
     }
 
+    private int oldColumn = -1;
+    private int oldCount = -1;
+
+    private int newColumn = -1;
+    private int newCount = -1;
+
+    private String oldColumnName = null;
+    private String newColumnName = null;
+
+    public void setClickedColumn(int column){
+        newColumn = column;
+        setNewColumnName();
+    }
+
+    private void setNewColumnName() {
+        Logs.info("new column value: " + newColumn);
+        HideableColumnsTableColumnModel columnModel = (HideableColumnsTableColumnModel) table.getColumnModel();
+        assert columnModel != null;
+        TableColumn columnByModelIndex = columnModel.getColumnByModelIndex(newColumn);
+        assert columnByModelIndex != null;
+        Object headerValue = columnByModelIndex.getHeaderValue();
+        assert headerValue != null;
+        newColumnName = headerValue.toString();
+    }
+
+    public void setClickCount(int count){
+        newCount = count;
+    }
+
+    public void setClickedColumnAndCount(int column, int count){
+        if(count == 2){
+            newColumn = -1;
+            newCount = -1;
+            newColumnName = null;
+            return;
+        }
+        this.newColumn = column;
+        setNewColumnName(); //save column name before doing the sorting. Otherwise weird stuff can happen
+        if(newColumn == oldColumn){
+            newCount = count + oldCount;
+        }
+        else{
+            newCount = count;
+        }
+    }
+
+    public void applySort(){
+        oldColumn = newColumn;
+        oldColumnName = newColumnName;
+        oldCount = newCount;
+        //for restoring order
+
+        boolean sortColumnReversed = oldCount % 2 == 0;
+        annotationTablePreferences.setSortColumn(oldColumnName, sortColumnReversed);
+        //repainting for changes to take effect
+        table.getTableHeader().revalidate();
+        table.getTableHeader().repaint();
+    }
+
     /**
      * A method to wrap the default renderer of the JTableHeader if it does not
      * appear to be wrapped already. This is particularly useful when the UI
@@ -286,8 +369,13 @@
      * Gets the sorting style currently applied to the specified column.
      */
     @Override
-    protected final int getSortingStyle(int column) {
-        return super.getSortingStyle(table.convertColumnIndexToModel(column));
+    protected final int getSortingStyle(int noneModelColumn) {
+        int column = table.convertColumnIndexToModel(noneModelColumn);
+            //check SortingState for these values
+        if(column != oldColumn) return 0; // unsorted
+        return oldCount % 2 == 0 ?  1 : 2; // 1 ascending, 2 descending
+//        return super.getSortingStyle(table.convertColumnIndexToModel(column));
+        
     }
 
     /**
@@ -360,6 +448,10 @@
         table = null;
     }
 
+    public void setAnnotationTablePreferences(AnnotationTablePreferences annotationTablePreferences) {
+        this.annotationTablePreferences = annotationTablePreferences;
+    }
+
     /**
      * Nested Listener class handles changes in the UI delegate for the table's
      * header. It responds by rewrapping the default renderer for the table
@@ -549,6 +641,7 @@
                 final boolean shift = e.isShiftDown();
                 final boolean control = e.isControlDown() || e.isMetaDown();
                 delegate.columnClicked(sortingState, column, clicks, shift, control);
+                setClickedColumnAndCount(column, clicks);
             }
         }
 
@@ -566,4 +659,4 @@
             table.getTableHeader().removeMouseListener(this);
         }
     }
-}
\ No newline at end of file
+}
Index: MutableTableModelEventDev.java
===================================================================
--- MutableTableModelEventDev.java	(revision 43340)
+++ MutableTableModelEventDev.java	(revision 44360)
@@ -1,6 +1,25 @@
 /* Glazed Lists                                                 (c) 2003-2006 */
 /* http://publicobject.com/glazedlists/                      publicobject.com,*/
 /*                                                     O'Dell Engineering Ltd.*/
+
+
+//The contents of this file are subject to the Mozilla Public License
+//Version 1.1 (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.mozilla.org/MPL/
+//
+//Software distributed under the License is distributed on an "AS IS"
+//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+//License for the specific language governing rights and limitations
+//under the License.
+//
+//The Original Code is Glazed Lists SVN trunk revision 2321
+//The Initial Developer of the Original Code is Jesse Wilson.
+//
+//Portions created by Biomatters employees are Copyright Biomatters Ltd 2011. All Rights Reserved.
+//Contributor(s): Dr Firas Swidan, Simon Buxton
+
+
 package com.biomatters.plugins.annotationTableViewer.glazedLists.modifications;
 
 // the core Glazed Lists packages
@@ -72,4 +91,4 @@
         else if(listChangeType == ListEvent.UPDATE) type = UPDATE;
         column = ALL_COLUMNS;
     }
-}
\ No newline at end of file
+}
Index: SortedList1_6OnThread.java
===================================================================
--- SortedList1_6OnThread.java	(revision 43340)
+++ SortedList1_6OnThread.java	(revision 44360)
@@ -1,6 +1,25 @@
 /* Glazed Lists                                                 (c) 2003-2006 */
 /* http://publicobject.com/glazedlists/                      publicobject.com,*/
 /*                                                     O'Dell Engineering Ltd.*/
+
+
+//The contents of this file are subject to the Mozilla Public License
+//Version 1.1 (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.mozilla.org/MPL/
+//
+//Software distributed under the License is distributed on an "AS IS"
+//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+//License for the specific language governing rights and limitations
+//under the License.
+//
+//The Original Code is Glazed Lists version 1.6
+//The Initial Developer of the Original Code is Jesse Wilson.
+//
+//Portions created by Biomatters employees are Copyright Biomatters Ltd 2011. All Rights Reserved.
+//Contributor(s): Dr Firas Swidan, Simon Buxton
+
+
 package com.biomatters.plugins.annotationTableViewer.glazedLists.modifications;
 
 // the core Glazed Lists packages
@@ -9,13 +28,17 @@
 import ca.odell.glazedlists.GlazedLists;
 import ca.odell.glazedlists.TransformedList;
 import ca.odell.glazedlists.event.ListEvent;
+import ca.odell.glazedlists.event.ListEventListener;
 import ca.odell.glazedlists.impl.GlazedListsImpl;
 import ca.odell.glazedlists.impl.adt.IndexedTree;
 import ca.odell.glazedlists.impl.adt.IndexedTreeIterator;
 import ca.odell.glazedlists.impl.adt.IndexedTreeNode;
+import ca.odell.glazedlists.impl.swing.SwingThreadProxyEventList;
+import com.biomatters.geneious.common.logs.Logs;
 import com.biomatters.plugins.annotationTableViewer.AnnotationTableJPanel;
+import com.biomatters.plugins.annotationTableViewer.glazedLists.AnnotationRow;
+import com.biomatters.plugins.annotationTableViewer.glazedLists.TableOperationLock;
 
-import javax.swing.*;
 import java.util.*;
 
 /**
@@ -72,6 +95,8 @@
      * for the current element to move if its value changes.
      */
     public static final int AVOID_MOVING_ELEMENTS = 1;
+    private static final String RESORTING = "Restoring sort";
+    private static final String SORTING = "Sorting";
 
     /** a map from the unsorted index to the sorted index */
     private IndexedTree<IndexedTreeNode> unsorted = null;
@@ -84,21 +109,23 @@
     /** one of {@link #STRICT_SORT_ORDER} or {@link #AVOID_MOVING_ELEMENTS}. */
     private int mode = STRICT_SORT_ORDER;
 
-    private JTable table;
     private boolean comparatorCalledFromConstructor = false;
     private Thread addingThread;
     private boolean noLongerViewed = false;
     private AnnotationTableJPanel annotationTableJPanel;
+    private TableComparatorChooserDev<E> tableComparatorChooserDev;
+    private FilterList1_6OnThread<AnnotationRow> filterList;
 
-
+    private TableOperationLock tableOperationLock;
+    
     /**
      * Creates a {@link SortedList1_6OnThread} that sorts the specified {@link EventList}.
      * Because this constructor takes no {@link Comparator} argument, all
      * elements in the specified {@link EventList} must implement {@link Comparable}
      * or a {@link ClassCastException} will be thrown.
      */
-    public SortedList1_6OnThread(EventList<E> source, JTable table, AnnotationTableJPanel annotationTableJPanel) {
-        this(source, (Comparator<E>)GlazedLists.comparableComparator(), table, annotationTableJPanel);
+    public SortedList1_6OnThread(EventList<E> source, AnnotationTableJPanel annotationTableJPanel, TableOperationLock tableOperationLock) {
+        this(source, (Comparator<E>)GlazedLists.comparableComparator(), annotationTableJPanel, tableOperationLock);
     }
 
 
@@ -111,13 +138,15 @@
      * specified {@link Comparator} is <code>null</code>, then this {@link List}
      * will be unsorted.
      */
-    public SortedList1_6OnThread(EventList<E> source, Comparator<? super E> comparator, JTable table, AnnotationTableJPanel annotationTableJPanel) {
+    public SortedList1_6OnThread(EventList<E> source, Comparator<? super E> comparator, AnnotationTableJPanel annotationTableJPanel, TableOperationLock tableOperationLock) {
         super(source);
-        this.table = table;
+
         this.annotationTableJPanel = annotationTableJPanel;
         comparatorCalledFromConstructor = true;
-        setComparator(comparator);
+        this.tableOperationLock = tableOperationLock;
 
+        setComparatorOnThread(comparator); // do not run it from swing thread (as setComparator() runs asynchronously on its own thread), to avoid using too much memory.
+
         source.addListEventListener(this);
     }
 
@@ -149,6 +178,15 @@
         return this.mode;
     }
 
+    public void addListEventListener(ListEventListener<E> listChangeListener) {
+        if(listChangeListener instanceof SwingThreadProxyEventList){
+            Logs.info("tried to add a swingthreadproxyeventlist as a listener");
+        }
+        else{
+            super.addListEventListener(listChangeListener);
+        }
+    }
+
     /**
      * Inserts all of the elements in the specified collection into this
      * list at the specified position (optional operation).  Shifts the
@@ -184,10 +222,8 @@
         if(index < 0 || index > size()) throw new IndexOutOfBoundsException("Cannot add at " + index + " on list of size " + size());
         if(values.size() == 0) return false;
 
-        final int initializeSize = this.size();
 
-
-        addingThread = new Thread(){
+        addingThread = new Thread(".sortingListAddAllThread"){
             public void run(){
                 addAllOnThread(index, values);
             }
@@ -210,14 +246,17 @@
 
     private void addAllOnThread(int index, Collection<? extends E> values) {
         updates.beginEvent(true);
-        annotationTableJPanel.preparePanelForLoadingStart();
+
         annotationTableJPanel.setProgressForLoading(values.size());
 
         int progressIndex = 0;
         for (Iterator<? extends E> iter = values.iterator(); iter.hasNext();) {
 
             if(isNoLongerViewed()) return;
-            annotationTableJPanel.setProgressValue(progressIndex);
+
+
+            annotationTableJPanel.setPotentiallyProgressValue(progressIndex);
+
             this.add(index, iter.next());
 
             // advance the insertion location if its within the size of the list
@@ -226,6 +265,7 @@
 
             ++progressIndex;
         }
+        annotationTableJPanel.setProgressValue(progressIndex - 1);
         updates.commitEvent();
         annotationTableJPanel.preparePanelForLoadingEnd();
 
@@ -459,6 +499,8 @@
         return comparator;
     }
 
+    private Thread sortingThread;
+
     /**
      * Set the {@link Comparator} in use in this {@link ca.odell.glazedlists.EventList}. This will
      * sort the {@link ca.odell.glazedlists.EventList} into a new order.
@@ -476,114 +518,154 @@
      *      this {@link SortedList1_6OnThread} in unsorted order.
      */
     public void setComparator(final Comparator<? super E> comparator) {
-    	Thread thread = new Thread("ComparatorSetThread"){
-			@Override
-			public void run(){
-		    	setComparatorOnThread(comparator);
-			}
-    	};
-    	thread.start();
 
-//    	thread.run();
+        sortingThread = new Thread("ComparatorSetThread"){
+            @Override
+            public void run(){
+                setComparatorOnThread(comparator);
+            }
+        };
+        sortingThread.start();
+    }
 
+    private boolean restoringSort = false;
+
+    public void setRestoringSort(boolean restoringSort){
+        this.restoringSort = restoringSort;
     }
 
+    public void setFilterList(FilterList1_6OnThread<AnnotationRow> filterList){
+        this.filterList = filterList;
+    }
+
 	private void setComparatorOnThread(Comparator<? super E> comparator) {
 
-        if(!comparatorCalledFromConstructor){
-            annotationTableJPanel.preparePanelForSortingStart();
-        }
+        tableOperationLock.waitForLock();
+        try {
+            long start = Logs.start();
+            if(!comparatorCalledFromConstructor){
+                annotationTableJPanel.preparePanelForSortingStart(restoringSort ? RESORTING : SORTING);
+            }
 
-        //save old comparator
-        Comparator<? super E> oldComparator = this.comparator;
+            //save old comparator
+            Comparator<? super E> oldComparator = this.comparator;
 
-		// save this comparator
-        this.comparator = comparator;
-        // keep the old trees to construct the reordering
-        IndexedTree previousSorted = sorted;
-        // create the sorted list with a simple comparator
-        Comparator treeComparator = null;
-        if(comparator != null) treeComparator = new IndexedTreeNodeComparator(comparator);
-        else treeComparator = new IndexedTreeNodeRawOrderComparator();
-        sorted = new IndexedTree<IndexedTreeNode>(treeComparator);
+            // save this comparator
+            this.comparator = comparator;
+            // keep the old trees to construct the reordering
+            IndexedTree<IndexedTreeNode> previousSorted = sorted;
+            // create the sorted list with a simple comparator
+            Comparator treeComparator = comparator == null? new IndexedTreeNodeRawOrderComparator(): new IndexedTreeNodeComparator(comparator);
+            sorted = new IndexedTree<IndexedTreeNode>(treeComparator);
 
+            // create a list which knows the offsets of the indexes to initialize this list
+            if(previousSorted == null && unsorted == null) {
+                unsorted = new IndexedTree<IndexedTreeNode>();
+                // add all elements in the source list, in order
+                int totalIterationCount = source.size() * 3; // * 3 because we are going to update from table model as well, both deletions and additions
+                if(totalIterationCount > 0) annotationTableJPanel.setProgressForSorting(totalIterationCount, restoringSort ? RESORTING : SORTING);
+                for(int i = 0, n = source.size(); i < n; i++) {
 
+                    annotationTableJPanel.setPotentiallyProgressValue(i);
 
-        // create a list which knows the offsets of the indexes to initialize this list
-        if(previousSorted == null && unsorted == null) {
-            unsorted = new IndexedTree<IndexedTreeNode>();
-            // add all elements in the source list, in order
-            int totalIterationCount = source.size();
-            if(totalIterationCount > 0) annotationTableJPanel.setProgressForSorting(totalIterationCount);
-//            if(source.isEmpty()) compositeProgressListener.setComplete();
-            for(int i = 0, n = source.size(); i < n; i++) {
-//                compositeProgressListener.beginSubtask();
-                annotationTableJPanel.setProgressValue(i);
-                if(annotationTableJPanel.isSortingCanceled() || isNoLongerViewed()) return;
-                IndexedTreeNode unsortedNode = unsorted.addByNode(i, IndexedTreeNode.EMPTY_NODE);
+                    if(annotationTableJPanel.isSortingCanceled() || isNoLongerViewed()){
+                        return;
+                    }
+                    IndexedTreeNode unsortedNode = unsorted.addByNode(i, IndexedTreeNode.EMPTY_NODE);
+                    insertByUnsortedNode(unsortedNode);
+                }
+                // this is the first sort so we're done
+                finishSettingComparator();
+                return;
+            }
+
+            // if the lists are empty, we're done
+            if(source.size() == 0){
+                return;
+            }
+
+            int totalIterationCount = unsorted.size() + size() + filterList.size() * 2; // *2 because we are going to update from table model as well, bothe deletions and insertions
+            annotationTableJPanel.setProgressForSorting(totalIterationCount, restoringSort ? RESORTING : SORTING);
+            int iterationCount = 0;
+            // rebuild the sorted tree to reflect the new Comparator
+            for(IndexedTreeIterator i = unsorted.iterator(0); i.hasNext(); ) {
+
+                annotationTableJPanel.setPotentiallyProgressValue(iterationCount);
+
+                ++iterationCount;
+                if(annotationTableJPanel.isSortingCanceled() || isNoLongerViewed()){
+                    restoreComparator(oldComparator, previousSorted);
+                    return;
+                }
+                IndexedTreeNode unsortedNode = i.next();
                 insertByUnsortedNode(unsortedNode);
             }
-            // this is the first sort so we're done
-            finishSettingComparator();
-            return;
-        }
 
-        // if the lists are empty, we're done
-        if(source.size() == 0) return;
+            // construct the reorder map
+            int[] reorderMap = new int[size()];
+            int oldSortedIndex = 0;
+            for(IndexedTreeIterator i = previousSorted.iterator(0); i.hasNext(); oldSortedIndex++) {
 
-        int totalIterationCount = unsorted.size() + size();
-        annotationTableJPanel.setProgressForSorting(totalIterationCount);
-        int iterationCount = 0;
-        // rebuild the sorted tree to reflect the new Comparator
-        for(IndexedTreeIterator i = unsorted.iterator(0); i.hasNext(); ) {
-            annotationTableJPanel.setProgressValue(iterationCount);
-            ++iterationCount;
-            if(annotationTableJPanel.isSortingCanceled() || isNoLongerViewed()){
-                restoreComparator(oldComparator, previousSorted);
-                return;
+                annotationTableJPanel.setPotentiallyProgressValue(iterationCount);
+
+                ++iterationCount;
+                if(annotationTableJPanel.isSortingCanceled() || isNoLongerViewed()){
+                    restoreComparator(oldComparator, previousSorted);
+                    return;
+                }
+                IndexedTreeNode oldSortedNode = i.next();
+                IndexedTreeNode unsortedNode = (IndexedTreeNode)oldSortedNode.getValue();
+                IndexedTreeNode newSortedNode = (IndexedTreeNode)unsortedNode.getValue();
+                int newSortedIndex = newSortedNode.getIndex();
+                reorderMap[newSortedIndex] = oldSortedIndex;
             }
-            IndexedTreeNode unsortedNode = i.next();
-            insertByUnsortedNode(unsortedNode);
-        }
 
-        // construct the reorder map
-        int[] reorderMap = new int[size()];
-        int oldSortedIndex = 0;
-        for(IndexedTreeIterator i = previousSorted.iterator(0); i.hasNext(); oldSortedIndex++) {
-            annotationTableJPanel.setProgressValue(iterationCount);
-            ++iterationCount;
-            if(annotationTableJPanel.isSortingCanceled() || isNoLongerViewed()){
-                restoreComparator(oldComparator, previousSorted);
-                return;
+            annotationTableJPanel.noCanceling();
+
+            //not to flicker the progress bar immediately
+            try {
+                Thread.sleep(300);
+            } catch (InterruptedException ignored) {
             }
-            IndexedTreeNode oldSortedNode = i.next();
-            IndexedTreeNode unsortedNode = (IndexedTreeNode)oldSortedNode.getValue();
-            IndexedTreeNode newSortedNode = (IndexedTreeNode)unsortedNode.getValue();
-            int newSortedIndex = newSortedNode.getIndex();
-            reorderMap[newSortedIndex] = oldSortedIndex;
+
+            Logs.duration(start, 500, "to sort the annotation table");
+
+            start = Logs.start();
+
+            // notification about the big change
+            updates.beginEvent();
+            updates.reorder(reorderMap);
+            updates.commitEvent();
+
+            smoothSortFinish();
+
+            Logs.duration(start, 500, "to fire sorting event");
+        } finally {
+            tableOperationLock.unlock();
         }
+	}
 
-        //todo: no cancelling from here. How to indicate that?
-        annotationTableJPanel.noCanceling();
-        // notification about the big change
-        updates.beginEvent();
-        updates.reorder(reorderMap);
-        updates.commitEvent();
 
+
+    private void smoothSortFinish() {
+        if(tableComparatorChooserDev != null)  tableComparatorChooserDev.applySort();
         redisplayTable();
-	}
+    }
 
-    private void restoreComparator(Comparator<? super E> oldComparator, IndexedTree previousSorted) {
+
+    private void restoreComparator(Comparator<? super E> oldComparator, IndexedTree<IndexedTreeNode> previousSorted) {
         this.comparator = oldComparator;
         sorted = previousSorted;
         redisplayTable();
     }
 
     private void redisplayTable() {
+        finishSettingComparator();
         if(!comparatorCalledFromConstructor){
+
             annotationTableJPanel.preparePanelForTableViewing();
         }
-        finishSettingComparator();
+
     }
 
     private void finishSettingComparator() {
@@ -709,7 +791,19 @@
         return indexOf(object) != -1;
     }
 
+    public void setTableComparatorChooserDev(TableComparatorChooserDev<E> tableComparatorChooser) {
+        this.tableComparatorChooserDev = tableComparatorChooser;
+    }
 
+    public void joinSortingThread() {
+        if(sortingThread == null) return;
+        try {
+            sortingThread.join();
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
     /**
      * A comparator that takes an indexed node, and compares the value
      * of an object in a list that has the index of that node.
@@ -820,4 +914,4 @@
             treeIterator = sorted.iterator(indexToRemove);
         }
     }
-}
\ No newline at end of file
+}
Index: AbstractTableComparatorChooser1_6.java
===================================================================
--- AbstractTableComparatorChooser1_6.java	(revision 43340)
+++ AbstractTableComparatorChooser1_6.java	(revision 44360)
@@ -1,6 +1,25 @@
 /* Glazed Lists                                                 (c) 2003-2006 */
 /* http://publicobject.com/glazedlists/                      publicobject.com,*/
 /*                                                     O'Dell Engineering Ltd.*/
+
+
+//The contents of this file are subject to the Mozilla Public License
+//Version 1.1 (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.mozilla.org/MPL/
+//
+//Software distributed under the License is distributed on an "AS IS"
+//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+//License for the specific language governing rights and limitations
+//under the License.
+//
+//The Original Code is Glazed Lists version 1.6
+//The Initial Developer of the Original Code is Kevin Maltby.
+//
+//Portions created by Biomatters employees are Copyright Biomatters Ltd 2011. All Rights Reserved.
+//Contributor(s): Dr Firas Swidan, Simon Buxton
+
+
 package com.biomatters.plugins.annotationTableViewer.glazedLists.modifications;
 
 // Primary GlazedList package
@@ -25,7 +44,6 @@
  */
 public abstract class AbstractTableComparatorChooser1_6<E> extends AbstractTableComparatorChooser<E>{
 
-  
     /** the sorted list to choose the comparators for */
     protected SortedList1_6OnThread<E> sortedList1_6OnThread;
 
@@ -37,6 +55,7 @@
         super(new SortedList<E>(new BasicEventList<E>()), tableFormat2);//no empty constructor.
     	this.sortedList1_6OnThread = sortedList2;
 
+
     }
 
     /**
@@ -61,5 +80,4 @@
         super.dispose();
     }
 
-
-}
\ No newline at end of file
+}
Index: FilterList1_6OnThread.java
===================================================================
--- FilterList1_6OnThread.java	(revision 43340)
+++ FilterList1_6OnThread.java	(revision 44360)
@@ -1,6 +1,25 @@
 /* Glazed Lists                                                 (c) 2003-2006 */
 /* http://publicobject.com/glazedlists/                      publicobject.com,*/
 /*                                                     O'Dell Engineering Ltd.*/
+
+
+//The contents of this file are subject to the Mozilla Public License
+//Version 1.1 (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.mozilla.org/MPL/
+//
+//Software distributed under the License is distributed on an "AS IS"
+//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+//License for the specific language governing rights and limitations
+//under the License.
+//
+//The Original Code is Glazed Lists version 1.6
+//The Initial Developers of the Original Code are Jesse Wilson, James Lemieux.
+//
+//Portions created by Biomatters employees are Copyright Biomatters Ltd 2011. All Rights Reserved.
+//Contributor(s): Dr Firas Swidan, Simon Buxton
+
+
 package com.biomatters.plugins.annotationTableViewer.glazedLists.modifications;
 
 // the core Glazed Lists packages
@@ -8,12 +27,25 @@
 import ca.odell.glazedlists.EventList;
 import ca.odell.glazedlists.TransformedList;
 import ca.odell.glazedlists.event.ListEvent;
+import ca.odell.glazedlists.event.ListEventAssembler;
+import ca.odell.glazedlists.event.ListEventListener;
 import ca.odell.glazedlists.impl.adt.Barcode;
 import ca.odell.glazedlists.impl.adt.BarcodeIterator;
+import ca.odell.glazedlists.impl.swing.SwingThreadProxyEventList;
 import ca.odell.glazedlists.matchers.Matcher;
 import ca.odell.glazedlists.matchers.MatcherEditor;
 import ca.odell.glazedlists.matchers.Matchers;
+import com.biomatters.geneious.common.logs.Logs;
+import com.biomatters.geneious.publicapi.utilities.ThreadUtilities;
+import com.biomatters.plugins.annotationTableViewer.AnnotationTableJPanel;
+import com.biomatters.plugins.annotationTableViewer.glazedLists.FilterOperation;
+import com.biomatters.plugins.annotationTableViewer.glazedLists.FilteringOperationState;
+import com.biomatters.plugins.annotationTableViewer.glazedLists.FilteringThread;
+import com.biomatters.plugins.annotationTableViewer.glazedLists.TableOperationLock;
 
+import javax.swing.*;
+import java.util.List;
+
 // volatile implementation support
 
 /**
@@ -63,15 +95,26 @@
     /** listener handles changes to the matcher */
     private final MatcherEditor.Listener listener = new PrivateMatcherEditorListener();
 
+    private AnnotationTableJPanel annotationTableJPanel;
 
+    private final FilteringOperationState filteringOperationState = new FilteringOperationState();
 
+    private FilteringThread filteringThread;
+
+    JComponent filteringBusyIcon;
+    private TableOperationLock tableOperationLock;
+
     /**
      * Creates a {@link FilterList1_6OnThread} that includes a subset of the specified
      * source {@link EventList}.
      */
-    public FilterList1_6OnThread(EventList<E> source) {
+    public FilterList1_6OnThread(EventList<E> source, AnnotationTableJPanel annotationTableJPanel, TableOperationLock tableOperationLock) {
         super(source);
 
+        this.annotationTableJPanel = annotationTableJPanel;
+
+        this.tableOperationLock = tableOperationLock;
+
         // build a list of what is filtered and what's not
         flagList.addBlack(0, source.size());
 
@@ -79,53 +122,35 @@
         source.addListEventListener(this);
     }
 
-    /**
-     * Convenience constructor for creating a {@link FilterList1_6OnThread} and setting its
-     * {@link Matcher}.
-     */
-    public FilterList1_6OnThread(EventList<E> source, Matcher<? super E> matcher) {
-        this(source);
 
-        // if no matcher was given, we have no further initialization work
-        if (matcher == null) return;
-
-        currentMatcher = matcher;
-        changed();
-    }
-
     /**
      * Convenience constructor for creating a {@link FilterList1_6OnThread} and setting its
      * {@link MatcherEditor}.
      */
-    public FilterList1_6OnThread(EventList<E> source, MatcherEditor<? super E> matcherEditor) {
-        this(source);
-
+    public FilterList1_6OnThread(EventList<E> source, MatcherEditor<? super E> matcherEditor, AnnotationTableJPanel annotationTableJPanel, TableOperationLock tableOperationLock) {
+        this(source, annotationTableJPanel, tableOperationLock);
         // if no matcherEditor was given, we have no further initialization work
         if (matcherEditor == null) return;
 
         currentEditor = matcherEditor;
         currentEditor.addMatcherEditorListener(listener);
         currentMatcher = currentEditor.getMatcher();
-        changed();
+        //the following call takes for ever (compared to other running times)
+        // and is not needed for empty source.
+        if(!source.isEmpty()) changed();
     }
 
-    /**
-     * Set the {@link Matcher} which specifies which elements shall be filtered.
-     *
-     * <p>This will remove the current {@link Matcher} or {@link MatcherEditor}
-     * and refilter the entire list.
-     */
-    public void setMatcher(Matcher<? super E> matcher) {
-        // cancel the previous editor
-        if(currentEditor != null) {
-            currentEditor.removeMatcherEditorListener(listener);
-            currentEditor = null;
+    public void setFilteringIcon(JComponent filteringBusyIcon){
+        this.filteringBusyIcon = filteringBusyIcon;
+    }
+
+    public void addListEventListener(ListEventListener<E> listChangeListener) {
+        if(listChangeListener instanceof SwingThreadProxyEventList){
+            Logs.info("tried to add a swingthreadproxyeventlist as a listener");
         }
-
-        if (matcher != null)
-            changeMatcherWithLocks(currentEditor, matcher, MatcherEditor.Event.CHANGED);
-        else
-            changeMatcherWithLocks(currentEditor, null, MatcherEditor.Event.MATCH_ALL);
+        else{
+            super.addListEventListener(listChangeListener);
+        }
     }
 
     /**
@@ -151,14 +176,16 @@
         }
     }
 
-    
+    public boolean isThereAnOngoingOperation(){
+        return filteringThread.isThereAnOngoingOperation();
+    }
 
-
+    //we need to synchronize filtering and sorting
     /** {@inheritDoc} */
-    public final void listChanged(ListEvent<E> listChanges) {
+    public synchronized final void listChanged(ListEvent<E> listChanges) {
         // all of these changes to this list happen "atomically"
         updates.beginEvent();
-
+//
         // handle reordering events
         if(listChanges.isReordering()) {
             int[] sourceReorderMap = listChanges.getReorderMap();
@@ -248,20 +275,100 @@
         updates.commitEvent();
     }
 
+    public FilterOperation getFilteringOperation() {
+        return filteringThread.getFilteringOperation();
+    }
+
+//    public void joinFilteringThread(){
+//        synchronized (filteringOperationState){
+//            try {
+//                // todo confirm with firas that this is ok
+//                Thread filterThread = filteringOperationState.getFilterThread();
+//                if (filterThread != null) filterThread.join();
+//            } catch (InterruptedException e) {
+//                throw new RuntimeException(e);
+//            }
+//        }
+//    }
+
+
+    public FilteringOperationState getFilteringOperationState(){
+        return filteringOperationState;
+    }
+
+    public void hideTablePanel(){
+        annotationTableJPanel.hideTablePanel();
+        ThreadUtilities.invokeNowOrLater(new Runnable() {
+            public void run() {
+                filteringBusyIcon.setVisible(true);
+            }
+        });
+    }
+
+    public void showTablePanel(){
+        annotationTableJPanel.showTablePanel();
+        ThreadUtilities.invokeNowOrLater(new Runnable() {
+            public void run() {
+                filteringBusyIcon.setVisible(false);
+            }
+        });
+    }
+
     /**
      * This method acquires the write lock for the FilterList and then selects
      * an appropriate delegate method to perform the correct work for each of
      * the possible <code>changeType</code>s.
      */
-    private void changeMatcherWithLocks(MatcherEditor<? super E> matcherEditor, Matcher<? super E> matcher, int changeType) {
-        getReadWriteLock().writeLock().lock();
-        try {
-            changeMatcher(matcherEditor, matcher, changeType);
-        } finally {
-            getReadWriteLock().writeLock().unlock();
+     // we need to synchronize filtering and sorting see changedList
+    private synchronized void changeMatcherWithLocks(final MatcherEditor<? super E> matcherEditor, final Matcher<? super E> matcher, final int changeType) {
+
+        if(filteringThread == null){ // these take a lot of memory, so do them on demand
+            filteringThread = new FilteringThread(this, (SortedList1_6OnThread)source, tableOperationLock);
+            filteringThread.start();
         }
+        filteringThread.setFilteringOperation(new FilterOperation(matcherEditor, matcher, changeType));
+        
+//        boolean waitForPreviousFilter = false;
+//
+//        Thread filterThread = new Thread(){
+//            public void run(){
+//                changeMatcher(matcherEditor, matcher, changeType);
+//            }
+//        };
+//
+//        synchronized(filteringOperationState){
+//            //there is a running filtering step
+//            if(filteringOperationState.getFilterThread() != null){
+//                //we need to wait for it to  finish
+//                waitForPreviousFilter = true;
+//                //if we can cancel it, cancel it
+//                if(filteringOperationState.isCanCancelPreviousFiltering()){
+//                    filteringOperationState.setCancelPreviousFiltering(true);
+//                }
+//            }
+//            //set that a filtering step is going on
+//            else{
+//                filteringOperationState.reset(filterThread);
+//            }
+//        }
+//
+//        //we need  to wait for previous filtering operation and to reset the state. There are other solutions to this
+//        // Starting with the easiest one.
+//        if(waitForPreviousFilter){
+//            joinFilteringThread();
+//        }
+//
+//        synchronized(filteringOperationState){
+//            filteringOperationState.reset(filterThread);
+//        }
+//        filterThread.start();
+
     }
 
+    public void changeMatcher(FilterOperation filterOperation){
+        changeMatcher(filterOperation.getMatcherEditor(), filterOperation.getMatcher(), filterOperation.getChangeType());
+    }
+
     /**
      * This method selects an appropriate delegate method to perform the
      * correct work for each of the possible <code>changeType</code>s. This
@@ -269,9 +376,16 @@
      * during initialization of FilterList.
      */
     private void changeMatcher(MatcherEditor<? super E> matcherEditor, Matcher<? super E> matcher, int changeType) {
+
+//        Logs.info("glazed list delegate thing: " + System.getProperty("GlazedLists.ListEventAssemblerDelegate"));
+
+        long start = Logs.start();
+
         // ensure the MatcherEvent is from OUR MatcherEditor
         if (currentEditor != matcherEditor) throw new IllegalStateException();
 
+
+
         switch (changeType) {
             case MatcherEditor.Event.CONSTRAINED: currentMatcher = matcher; this.constrained(); break;
             case MatcherEditor.Event.RELAXED: currentMatcher = matcher; this.relaxed(); break;
@@ -279,6 +393,8 @@
             case MatcherEditor.Event.MATCH_ALL: currentMatcher = Matchers.trueMatcher(); this.matchAll(); break;
             case MatcherEditor.Event.MATCH_NONE: currentMatcher = Matchers.falseMatcher(); this.matchNone(); break;
         }
+
+        Logs.duration(start, 500, "to change matching filter of annotation table");
     }
 
     /**
@@ -297,10 +413,17 @@
         flagList.clear();
         flagList.addWhite(0, source.size());
 
+        if (filteringOperationState.isCancelPreviousFiltering()) return;
         // commit the changes and notify listeners
         updates.commitEvent();
+        smoothEndingOfMatchChange();
     }
 
+
+    private void smoothEndingOfMatchChange() {
+        filteringOperationState.reset();
+    }
+
     /**
      * Handles a clearing of the filter. That is, the filter list will act as
      * a passthrough and not discriminate any of the elements of the wrapped
@@ -320,16 +443,39 @@
         // are conceptually black. Otherwise we would need to change flags
         // to black as we go so that flag offsets are correct
         for(BarcodeIterator i = flagList.iterator(); i.hasNextWhite();) {
+            if(filteringOperationState.isCancelPreviousFiltering()){
+                filterOperationCanceled();
+                return;
+            }
             i.nextWhite();
             updates.addInsert(i.getIndex());
         }
         flagList.clear();
         flagList.addBlack(0, source.size());
 
+        if (filteringOperationState.isCancelPreviousFiltering()) return;
         // commit the changes and notify listeners
         updates.commitEvent();
+        smoothEndingOfMatchChange();
     }
 
+    private void filterOperationCanceled() {
+        /*
+         * need to flush this guy, as there is no way to cancel a beginEvent(). However, we need to copy its listeners, among which is
+         * the table model.
+         */
+
+        ListEventAssembler<E> tmpUpdates = new ListEventAssembler<E>(this, publisher);
+        List<ListEventListener<E>> listEventListeners = updates.getListEventListeners();
+        for(ListEventListener<E> listener : listEventListeners){
+            tmpUpdates.addListEventListener(listener);
+        }
+        updates = tmpUpdates;
+
+        //reset operation state
+        filteringOperationState.reset();
+    }
+
     /**
      * Handles a relaxing or widening of the filter. This may change the
      * contents of this {@link EventList} as filtered elements are unfiltered
@@ -341,14 +487,19 @@
 
         // for all filtered items, see what the change is
         for(BarcodeIterator i = flagList.iterator(); i.hasNextWhite();) {
+            if(filteringOperationState.isCancelPreviousFiltering()){
+                filterOperationCanceled();
+                return;
+            }
             i.nextWhite();
             if(currentMatcher.matches(source.get(i.getIndex()))) {
                 updates.addInsert(i.setBlack());
             }
         }
-
+        if (filteringOperationState.isCancelPreviousFiltering()) return;
         // commit the changes and notify listeners
         updates.commitEvent();
+        smoothEndingOfMatchChange();
     }
 
     /**
@@ -362,6 +513,10 @@
 
         // for all unfiltered items, see what the change is
         for(BarcodeIterator i = flagList.iterator(); i.hasNextBlack();) {
+            if(filteringOperationState.isCancelPreviousFiltering()){
+                filterOperationCanceled();
+                return;
+            }
             i.nextBlack();
             if(!currentMatcher.matches(source.get(i.getIndex()))) {
                 int blackIndex = i.getBlackIndex();
@@ -370,8 +525,10 @@
             }
         }
 
+        if (filteringOperationState.isCancelPreviousFiltering()) return;
         // commit the changes and notify listeners
         updates.commitEvent();
+        smoothEndingOfMatchChange();
     }
 
     /**
@@ -379,6 +536,7 @@
      * of this {@link EventList} as elements are filtered and unfiltered.
      */
     private void changed() {
+
         // all of these changes to this list happen "atomically"
         updates.beginEvent();
 
@@ -386,6 +544,11 @@
         for(BarcodeIterator i = flagList.iterator();i.hasNext();) {
             i.next();
 
+            if(filteringOperationState.isCancelPreviousFiltering()){
+                filterOperationCanceled();
+                return;
+            }
+
             // determine if this value was already filtered out or not
             int filteredIndex = i.getBlackIndex();
             boolean wasIncluded = filteredIndex != -1;
@@ -397,14 +560,28 @@
                 i.setWhite();
                 updates.addDelete(filteredIndex);
 
-            // this element is being added as a result of the change
+                // this element is being added as a result of the change
             } else if(!wasIncluded && include) {
                 updates.addInsert(i.setBlack());
             }
         }
 
+        if (filteringOperationState.isCancelPreviousFiltering()){
+            filterOperationCanceled();
+            return;
+        }
+
+
+        //sleep a bit not to cause a flicker
+        try {
+            Thread.sleep(100);
+        } catch (InterruptedException ignored) {
+        }
+
         // commit the changes and notify listeners
         updates.commitEvent();
+        smoothEndingOfMatchChange();
+
     }
 
 
@@ -443,4 +620,4 @@
     protected boolean isWritable() {
         return true;
     }
-}
\ No newline at end of file
+}
Index: AdvancedTableModelDev.java
===================================================================
--- AdvancedTableModelDev.java	(revision 43340)
+++ AdvancedTableModelDev.java	(revision 44360)
@@ -1,12 +1,27 @@
 /* Glazed Lists                                                 (c) 2003-2006 */
 /* http://publicobject.com/glazedlists/                      publicobject.com,*/
 /*                                                     O'Dell Engineering Ltd.*/
+
+
+//The contents of this file are subject to the Mozilla Public License
+//Version 1.1 (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.mozilla.org/MPL/
+//
+//Software distributed under the License is distributed on an "AS IS"
+//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+//License for the specific language governing rights and limitations
+//under the License.
+//
+//The Original Code is Glazed Lists SVN trunk revision 2321
+//The Initial Developer of the Original Code is Holger Brands.
+//
+//Portions created by Biomatters employees are Copyright Biomatters Ltd 2011. All Rights Reserved.
+//Contributor(s): Dr Firas Swidan, Simon Buxton
+
+
 package com.biomatters.plugins.annotationTableViewer.glazedLists.modifications;
 
-/**
- * I got this file from a development version in the first half of July. It is a dev version after 1.8
- * 
- */
 
 import ca.odell.glazedlists.EventList;
 import ca.odell.glazedlists.gui.TableFormat;
Index: DefaultEventTableModelDev.java
===================================================================
--- DefaultEventTableModelDev.java	(revision 43340)
+++ DefaultEventTableModelDev.java	(revision 44360)
@@ -3,23 +3,36 @@
 /*                                                     O'Dell Engineering Ltd.*/
 package com.biomatters.plugins.annotationTableViewer.glazedLists.modifications;
 
-/**
- * I got this file from a development version in the first half of July. It is a dev version after 1.8
- * 
- */
+//The contents of this file are subject to the Mozilla Public License
+//Version 1.1 (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.mozilla.org/MPL/
+//
+//Software distributed under the License is distributed on an "AS IS"
+//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+//License for the specific language governing rights and limitations
+//under the License.
+//
+//The Original Code is Glazed Lists SVN trunk revision 2321
+//The Initial Developer of the Original Code is Jesse Wilson.
+//
+//Portions created by Biomatters employees are Copyright Biomatters Ltd 2011. All Rights Reserved.
+//Contributor(s): Dr Firas Swidan, Simon Buxton
 
+
 import ca.odell.glazedlists.EventList;
 import ca.odell.glazedlists.event.ListEvent;
 import ca.odell.glazedlists.event.ListEventListener;
 import ca.odell.glazedlists.gui.AdvancedTableFormat;
 import ca.odell.glazedlists.gui.TableFormat;
 import ca.odell.glazedlists.gui.WritableTableFormat;
+import com.biomatters.geneious.common.logs.Logs;
+import com.biomatters.geneious.publicapi.utilities.ThreadUtilities;
+import com.biomatters.plugins.annotationTableViewer.AnnotationTableJPanel;
 
 import javax.swing.*;
 import javax.swing.table.AbstractTableModel;
 import javax.swing.table.TableModel;
-import java.awt.*;
-import java.lang.reflect.InvocationTargetException;
 
 /**
  * A {@link TableModel} that holds an {@link EventList}. Each element of the list
@@ -55,6 +68,7 @@
 
     /** reusable TableModelEvent for broadcasting changes */
     private final MutableTableModelEventDev tableModelEvent = new MutableTableModelEventDev(this);
+    private AnnotationTableJPanel annotationTableJPanel;
 
     /**
      * Creates a new table model that extracts column data from the given
@@ -64,8 +78,9 @@
      * @param tableFormat the object responsible for extracting column data
      *      from the row objects
      */
-    public DefaultEventTableModelDev(EventList<E> source, TableFormat<? super E> tableFormat) {
+    public DefaultEventTableModelDev(EventList<E> source, TableFormat<? super E> tableFormat, AnnotationTableJPanel annotationTableJPanel) {
         this(source, false, tableFormat);
+        this.annotationTableJPanel = annotationTableJPanel;
     }
 
     /**
@@ -98,21 +113,13 @@
     public void setTableFormat(TableFormat<? super E> tableFormat) {
         this.tableFormat = tableFormat;
         tableModelEvent.setStructureChanged();
-        fireTableChanged(tableModelEvent);
-        if (EventQueue.isDispatchThread()){
-            fireTableChanged(tableModelEvent);
-        }
-        else{
-            try {
-                SwingUtilities.invokeAndWait(new Runnable() {
-                    public void run() {
-                        fireTableChanged(tableModelEvent);
-                    }
-                });
-            } catch (InterruptedException e) {
-            } catch (InvocationTargetException e) {
+        //as much as I can tell, we never really get here...
+        ThreadUtilities.invokeNowOrWait(new Runnable() {
+            public void run() {
+//                tableComparatorChooserDev.applySort();
+                fireTableChanged(tableModelEvent);
             }
-        }
+        });
     }
 
     /**
@@ -148,26 +155,36 @@
 //        if (!EventQueue.isDispatchThread())
 //            throw new IllegalStateException("Events to " + this.getClass().getSimpleName() + " must arrive on the EDT - consider adding GlazedListsSwing.swingThreadProxyList(source) somewhere in your list pipeline");
 
+        int i = 0;
+        if(listChanges.isReordering()) i = listChanges.getReorderMap().length * 2;
         // for all changes, one block at a time
         while (listChanges.nextBlock()) {
+            // this seems to be getting called for every item changed, eg a resorting of 1000 annotations is 1000 removals and 1000 adds...
             // get the current change info
+
+            //update progress. This is called twice. Once for deletions, and once for additions.
+            annotationTableJPanel.setPotentiallyProgressValue(i);
+            ++i;
+
             int startIndex = listChanges.getBlockStartIndex();
             int endIndex = listChanges.getBlockEndIndex();
             int changeType = listChanges.getType();
             // create a table model event for this block
+
             tableModelEvent.setValues(startIndex, endIndex, changeType);
-            if (EventQueue.isDispatchThread()){
-                fireTableChanged(tableModelEvent);
-            }
-            else{
-                //invokeandwait here causes a deadlock
-                SwingUtilities.invokeLater(new Runnable() {
-                    public void run() {
-                        fireTableChanged(tableModelEvent);
-                    }
-                });
-            }
+//            ThreadUtilities.invokeNowOrLater(new Runnable() {
+//                public void run() {
+//                    tableComparatorChooserDev.applySort();
+
+            // consulting with Matt, some automated scanners of jcomponents in geneious might
+            // have interaction with the table and we do not want that to happen while applying these changes
+            // however, the table is actually removed anyhow before the operation is applied. For that see AnnotationTableJPanel.prepareForOperationStart
+//                    fireTableChanged(tableModelEvent);
+//                }
+//            });
         }
+
+        if(listChanges.isReordering()) annotationTableJPanel.setProgressComplete();
     }
 
     /**
@@ -223,12 +240,40 @@
      * Retrieves the value at the specified location of the table.
      */
     public Object getValueAt(int row, int column) {
-        source.getReadWriteLock().readLock().lock();
-        try {
-            return tableFormat.getColumnValue(source.get(row), column);
-        } finally {
-            source.getReadWriteLock().readLock().unlock();
+
+        FilterList1_6OnThread filterList = (FilterList1_6OnThread)source;
+        //we do not want an arrival of a new filtering operation to interfere with the following call
+        synchronized (filterList.getFilteringOperation()){
+            //we could end up here because of asynchronous timing between EDT (table drawing itself) and user having initiated a new
+            // filtering operation on the filtering thread.
+            //see also showTablePanel in AnnotationTableJPanel
+            //we should stop the drawing of the table when we end up here, but we cannot. Following
+            // calls might happen after filtering is done, and they will end up with the
+            // exception below.
+            if(filterList.isThereAnOngoingOperation()){
+                Logs.info("SPECIAL CASE");
+                return null;
+            }
+
+            source.getReadWriteLock().readLock().lock();
+            //noinspection ProhibitedExceptionCaught
+            try {
+                return tableFormat.getColumnValue(source.get(row), column);
+            }
+            //sometimes we even end up here as explained above
+            catch(IndexOutOfBoundsException e){
+                Logs.info("exception: " + e.getMessage());
+                return null;
+            }
+            catch (NullPointerException e){
+                Logs.info("exception: " + e.getMessage());
+                return null;
+            }
+            finally {
+                source.getReadWriteLock().readLock().unlock();
+            }
         }
+
     }
 
     /**
@@ -294,6 +339,7 @@
      */
     public void dispose() {
         source.removeListEventListener(this);
+        // hmm
 //        if (disposeSource) source.dispose();
         // this encourages exceptions to be thrown if this model is incorrectly accessed again
         source = null;
Index: EventSelectionModel1_6.java
===================================================================
--- EventSelectionModel1_6.java	(revision 43821)
+++ EventSelectionModel1_6.java	(revision 45001)
@@ -1,8 +1,25 @@
 /* Glazed Lists                                                 (c) 2003-2006 */
 /* http://publicobject.com/glazedlists/                      publicobject.com,*/
 /*                                                     O'Dell Engineering Ltd.*/
-package ca.odell.glazedlists.swing;

+//The contents of this file are subject to the Mozilla Public License
+//Version 1.1 (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.mozilla.org/MPL/
+//
+//Software distributed under the License is distributed on an "AS IS"
+//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+//License for the specific language governing rights and limitations
+//under the License.
+//
+//The Original Code is Glazed Lists version 1.6
+//The Initial Developers of the Original Code are Jesse Wilson.
+//
+//Portions created by Biomatters employees are Copyright Biomatters Ltd 2011. All Rights Reserved.
+//Contributor(s): Dr Firas Swidan, Simon Buxton
+
+package com.biomatters.plugins.annotationTableViewer.glazedLists.modifications;
+
 // the core Glazed Lists packages

 import ca.odell.glazedlists.EventList;
@@ -20,12 +37,12 @@
 // for lists of listeners

 /**
- * An {@link EventSelectionModel} is a class that performs two simulaneous
+ * An {@link ca.odell.glazedlists.swing.EventSelectionModel1_6} is a class that performs two simulaneous
  * services. It is a {@link ListSelectionModel} to provide selection tracking for a
  * {@link JTable}. It is also a {@link EventList} that contains the table's selection.
  *
  * <p>As elements are selected or deselected, the {@link EventList} aspect of this
- * {@link EventSelectionModel} changes. Changes to that {@link List} will change the
+ * {@link ca.odell.glazedlists.swing.EventSelectionModel1_6} changes. Changes to that {@link List} will change the
  * source {@link EventList}. To modify only the selection, use the
  * {@link ListSelectionModel}'s methods.
  *
@@ -49,7 +66,7 @@
  *
  * @author <a href="mailto:jesse@odel.on.ca">Jesse Wilson</a>
  */
-public final class EventSelectionModel<E> implements ListSelectionModel {
+public final class EventSelectionModel1_6<E> implements ListSelectionModel {

     /** the event lists that provide an event list view of the selection */
     private ListSelection<E> listSelection;
@@ -57,6 +74,8 @@
     /** the proxy moves events to the Swing Event Dispatch thread */
     private TransformedList<E, E> swingThreadSource;

+    private EventList<E> source;
+
     /** whether the user can modify the selection */
     private boolean enabled = true;

@@ -71,7 +90,7 @@
     /**
      * Creates a new selection model that also presents a list of the selection.
      *
-     * The {@link EventSelectionModel} listens to this {@link EventList} in order
+     * The {@link ca.odell.glazedlists.swing.EventSelectionModel1_6} listens to this {@link EventList} in order
      * to adjust selection when the {@link EventList} is modified. For example,
      * when an element is added to the {@link EventList}, this may offset the
      * selection of the following elements.
@@ -80,15 +99,15 @@
      *      be the same {@link EventList} passed to the constructor of your
      *      {@link EventTableModel} or {@link EventListModel}.
      */
-    public EventSelectionModel(EventList<E> source) {
+    public EventSelectionModel1_6(EventList<E> source) {
         // lock the source list for reading since we want to prevent writes
         // from occurring until we fully initialize this EventSelectionModel
         source.getReadWriteLock().readLock().lock();
         try {
-            swingThreadSource = GlazedListsSwing.swingThreadProxyList(source);
-
+//            swingThreadSource = GlazedListsSwing.swingThreadProxyList(source);
+             this.source = source;
             // build a list for reading the selection
-            this.listSelection = new ListSelection<E>(swingThreadSource);
+            this.listSelection = new ListSelection<E>(source);
             this.listSelection.addSelectionListener(new SwingSelectionListener());
         } finally {
             source.getReadWriteLock().readLock().unlock();
@@ -123,7 +142,7 @@
     /**
      * Gets the selection model that provides selection management for a table.
      *
-     * @deprecated As of 2004/11/26, the {@link EventSelectionModel} implements
+     * @deprecated As of 2004/11/26, the {@link ca.odell.glazedlists.swing.EventSelectionModel1_6} implements
      *      {@link ListSelectionModel} directly.
      */
     public ListSelectionModel getListSelectionModel() {
@@ -219,7 +238,7 @@
      */
     public void removeSelectionInterval(int index0, int index1) {
         if(!enabled) return;
-        if(index0 == 0 && index1 == 0 && swingThreadSource.isEmpty()) return; // hack for Java 5 compatibility
+        if(index0 == 0 && index1 == 0 && source.isEmpty()) return; // hack for Java 5 compatibility
         listSelection.deselect(index0, index1);
     }

@@ -360,18 +379,18 @@
     }

     /**
-     * Releases the resources consumed by this {@link EventSelectionModel} so that it
+     * Releases the resources consumed by this {@link ca.odell.glazedlists.swing.EventSelectionModel1_6} so that it
      * may eventually be garbage collected.
      *
-     * <p>An {@link EventSelectionModel} will be garbage collected without a call to
+     * <p>An {@link ca.odell.glazedlists.swing.EventSelectionModel1_6} will be garbage collected without a call to
      * {@link #dispose()}, but not before its source {@link EventList} is garbage
-     * collected. By calling {@link #dispose()}, you allow the {@link EventSelectionModel}
+     * collected. By calling {@link #dispose()}, you allow the {@link ca.odell.glazedlists.swing.EventSelectionModel1_6}
      * to be garbage collected before its source {@link EventList}. This is
-     * necessary for situations where an {@link EventSelectionModel} is short-lived but
+     * necessary for situations where an {@link ca.odell.glazedlists.swing.EventSelectionModel1_6} is short-lived but
      * its source {@link EventList} is long-lived.
      *
      * <p><strong><font color="#FF0000">Warning:</font></strong> It is an error
-     * to call any method on a {@link EventSelectionModel} after it has been disposed.
+     * to call any method on a {@link ca.odell.glazedlists.swing.EventSelectionModel1_6} after it has been disposed.
      */
     public void dispose() {
         listSelection.dispose();
