package org.yamcs.studio.ui.clients;

import java.util.ArrayList;
import java.util.logging.Logger;

import org.yamcs.protobuf.YamcsManagement.ClientInfo;

public class ClientsTableModel {
    private static final Logger log = Logger.getLogger(ClientsTableModel.class.getName());
    private ArrayList<ClientInfo> clients = new ArrayList<ClientInfo>();
    private ClientsTableViewer clientsTableViewer;

    public ClientsTableModel(ClientsTableViewer clientsTableViewer)
    {
        this.clientsTableViewer = clientsTableViewer;
    }

    public void updateClient(ClientInfo uci) {
        try {
            boolean found = false;
            for (int i = 0; i < clients.size(); ++i) {
                ClientInfo ci = clients.get(i);
                if (ci.getId() == uci.getId()) {
                    clients.set(i, uci);
                    clientsTableViewer.replace(uci, i);
                    found = true;
                    break;
                }
            }
            if (!found) {
                clients.add(uci);
                clientsTableViewer.add(uci);
            }
        } catch (Exception e)
        {
            log.severe(e.toString());
        }

    }

    public void removeClient(ClientInfo rci) {
        try {
            boolean found = false;
            for (int i = 0; i < clients.size(); ++i) {
                ClientInfo ci = clients.get(i);
                if (ci.getId() == rci.getId()) {
                    clients.remove(ci);
                    clientsTableViewer.remove(ci);
                    found = true;
                    break;
                }
            }
            if (!found) {
                log.warning("client not found in list: " + rci);
            }
        } catch (Exception e)
        {
            log.severe(e.toString());
        }

    }
}

@@ -80,22 +80,12 @@
 til.
-logging.Logger
+List
 ;%0A%0Ai
@@ -175,98 +175,8 @@
 l %7B%0A
-    private static final Logger log = Logger.getLogger(ClientsTableModel.class.getName());
 %0A   
@@ -184,21 +184,16 @@
 private 
-Array
 List%3CCli
@@ -225,26 +225,16 @@
 rayList%3C
-ClientInfo
 %3E();%0A   
@@ -349,20 +349,16 @@
 eViewer)
-%0A   
  %7B%0A     
@@ -460,34 +460,16 @@
  uci) %7B%0A
-        try %7B%0A    
         
@@ -491,36 +491,32 @@
  false;%0A        
-   
- 
 for (int i = 0; 
@@ -534,36 +534,32 @@
 .size(); ++i) %7B%0A
-    
             Clie
@@ -590,36 +590,32 @@
 i);%0A            
-    
 if (ci.getId() =
@@ -639,36 +639,32 @@
                 
- 
-   
 clients.set(i, u
@@ -664,28 +664,24 @@
 et(i, uci);%0A
-    
             
@@ -728,36 +728,32 @@
                 
-   
- 
 found = true;%0A  
@@ -742,36 +742,32 @@
   found = true;%0A
-    
                 
@@ -777,36 +777,32 @@
 ak;%0A            
-    
 %7D%0A            %7D%0A
@@ -791,34 +791,26 @@
   %7D%0A        
- 
-   %7D%0A    
+%7D%0A
         if (
@@ -823,36 +823,32 @@
 ) %7B%0A            
-   
- 
 clients.add(uci)
@@ -841,36 +841,32 @@
 ients.add(uci);%0A
-    
             clie
@@ -902,277 +902,78 @@
     
-    %7D%0A        %7D catch (Exception e)%0A        %7B%0A            log.severe(e.toString());%0A        %7D%0A%0A    %7D%0A%0A    public void removeClient(ClientInfo rci) %7B%0A        try %7B%0A            boolean found = false;%0A            for (int i = 0; i %3C clients.size(); ++i) %7B%0A                
+%7D%0A%0A    %7D%0A%0A    public void removeClient(ClientInfo rci) %7B%0A        for (
 Clie
@@ -978,33 +978,33 @@
 ientInfo ci 
-=
+:
  clients
 .get(i);%0A   
@@ -987,37 +987,28 @@
 ci : clients
-.get(i);%0A    
+) %7B%0A
             
@@ -1048,36 +1048,32 @@
                 
-    
 clients.remove(c
@@ -1068,36 +1068,32 @@
 nts.remove(ci);%0A
-    
                 
@@ -1127,46 +1127,8 @@
 i);%0A
-                    found = true;%0A    
     
@@ -1138,36 +1138,32 @@
          break;%0A
-    
             %7D%0A  
@@ -1172,207 +1172,9 @@
     
-    %7D%0A            if (!found) %7B%0A                log.warning(%22client not found in list: %22 + rci);%0A            %7D%0A        %7D catch (Exception e)%0A        %7B%0A            log.severe(e.toString());%0A        %7D%0A
+%7D
 %0A   

Remove exception silencers