/*
 * Copyright 2016 the original author or authors.
 *
 * 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 com.github.alexfalappa.nbspringboot.navigator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 * A super simple table model for the navigator UI.
 *
 * @author Michael J. Simons, 2016-09-16
 */
public class MappedElementsModel extends AbstractTableModel {

    private static final long serialVersionUID = 5870247061989235811L;

    private final List<MappedElement> data = Collections.synchronizedList(new ArrayList<MappedElement>());

    @Override
    public String getColumnName(int column) {
        String rv;
        switch (column) {
            case 0:
                rv = Bundle.resourceUrl();
                break;
            case 1:
                rv = Bundle.requestMethod();
                break;
            case 2:
                rv = Bundle.handlerMethod();
                break;
            default:
                rv = null;
        }
        return rv;
    }

    @Override
    public int getRowCount() {
        return this.data.size();
    }

    @Override
    public int getColumnCount() {
        return 3;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        final MappedElement mappedElement = this.data.get(rowIndex);
        Object rv;
        switch (columnIndex) {
            case 0:
                rv = mappedElement.getResourceUrl();
                break;
            case 1:
                rv = mappedElement.getRequestMethod();
                break;
            case 2:
                rv = mappedElement.getHandlerMethod();
                break;
            default:
                rv = null;
        }

        return rv;
    }

    void refresh(final List<MappedElement> newData) {        
        this.data.clear();
        this.data.addAll(newData);
        Collections.sort(this.data, new Comparator<MappedElement>() {
            @Override
            public int compare(MappedElement o1, MappedElement o2) {
                int rv = o1.getResourceUrl().compareTo(o2.getResourceUrl());
                if (rv == 0) {
                    if (o1.getRequestMethod()== null) {
                        rv = -1;
                    } else if (o2.getRequestMethod() == null) {
                        rv = 1;
                    } else {
                        rv = o1.getRequestMethod().compareTo(o2.getRequestMethod());
                    }

                }
                return rv;
            }
        });
        this.fireTableDataChanged();
    }
}

@@ -771,24 +771,59 @@
 .util.List;%0A
+import javax.swing.SwingUtilities;%0A
 import javax
@@ -2437,24 +2437,25 @@
 wData) %7B
+%0A
         
 %0A       
@@ -2446,30 +2446,122 @@
         
-%0A        this.
+final Runnable r = new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                
 data.cle
@@ -2574,21 +2574,24 @@
         
+  
-this.
+      
 data.add
@@ -2604,16 +2604,24 @@
 wData);%0A
+        
         
@@ -2637,21 +2637,16 @@
 ns.sort(
-this.
 data, ne
@@ -2681,32 +2681,40 @@
 ) %7B%0A            
+        
 @Override%0A      
@@ -2699,32 +2699,40 @@
       @Override%0A
+        
             publ
@@ -2800,16 +2800,24 @@
         
+        
 int rv =
@@ -2881,24 +2881,32 @@
             
+        
 if (rv == 0)
@@ -2924,24 +2924,32 @@
             
+    
+    
 if (o1.getRe
@@ -2961,16 +2961,17 @@
 Method()
+ 
 == null)
@@ -2993,24 +2993,32 @@
             
+      
+  
 rv = -1;%0A   
@@ -3014,16 +3014,24 @@
 v = -1;%0A
+        
         
@@ -3114,16 +3114,32 @@
     
-rv = 1;%0A
+        rv = 1;%0A        
     
@@ -3183,24 +3183,32 @@
             
+    
+    
 rv = o1.getR
@@ -3268,35 +3268,50 @@
                 
-%7D%0A%0A
+        %7D%0A        
                 
@@ -3320,32 +3320,40 @@
                 
+        
 return rv;%0A     
@@ -3363,30 +3363,53 @@
     
-%7D%0A        %7D);%0A
+        %7D%0A                %7D);%0A       
         
 this
@@ -3404,21 +3404,17 @@
         
-this.
+ 
 fireTabl
@@ -3425,20 +3425,84 @@
 aChanged();%0A
+            %7D%0A        %7D;%0A        SwingUtilities.invokeLater(r);%0A
     %7D%0A%7D%0A

Update UI async