View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.log4j.chainsaw.zeroconf;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import javax.jmdns.ServiceEvent;
24  import javax.jmdns.ServiceInfo;
25  import javax.jmdns.ServiceListener;
26  import javax.swing.table.AbstractTableModel;
27  
28  public class ZeroConfDeviceModel extends AbstractTableModel implements ServiceListener {
29  
30      private List deviceList = new ArrayList();
31      private ZeroConfPreferenceModel zeroConfPreferenceModel;
32      private transient ZeroConfPlugin plugin;
33      
34      public ZeroConfDeviceModel() {
35      }
36      
37      public int getRowCount() {
38          return deviceList.size();
39      }
40  
41      public int getColumnCount() {
42          return 4;
43      }
44  
45      public ServiceInfo getServiceInfoAtRow(int row) {
46          return (ServiceInfo) deviceList.get(row);
47      }
48      public Object getValueAt(int rowIndex, int columnIndex) {
49          ServiceInfo info = (ServiceInfo) deviceList.get(rowIndex);
50          if(info == null) {
51              return "";
52          }
53          switch(columnIndex) {
54          case 0:
55                  return getAutoConnectHandle(info);
56          case 1:
57                  return info.getAddress().getHostName() + ":" + info.getPort();
58          case 2:
59                  return zeroConfPreferenceModel.getAutoConnectDevices().contains(getAutoConnectHandle(info))?Boolean.TRUE:Boolean.FALSE;
60          case 3:
61                  return plugin.isConnectedTo(info)?"Connected":"Not Connected";
62  //                return plugin.isConnectedTo(info)?new ImageIcon(ChainsawIcons.ANIM_NET_CONNECT):new ImageIcon();
63              default:
64                      return "";
65          }
66      }
67  
68      private String getAutoConnectHandle(ServiceInfo info) {
69          return info.getName();
70      }
71  
72      public void serviceAdded(ServiceEvent event) {
73      }
74  
75      public void serviceRemoved(ServiceEvent event) {
76          for (Iterator iter = deviceList.iterator(); iter.hasNext();) {
77              ServiceInfo info = (ServiceInfo) iter.next();
78              if(info.getName().equals(event.getName())) {
79                  iter.remove();
80              }
81          }
82          fireTableDataChanged();
83      }
84  
85      public void serviceResolved(ServiceEvent event) {
86          deviceList.add(event.getInfo());
87          fireTableDataChanged();
88      }
89  
90      public void setZeroConfPreferenceModel(
91              ZeroConfPreferenceModel zeroConfPreferenceModel) {
92          this.zeroConfPreferenceModel = zeroConfPreferenceModel;
93      }
94  
95      public String getColumnName(int column) {
96          switch(column) {
97          case 0:
98                  return "ZeroConf name";
99          case 1:
100                 return "Address:Port";
101         case 2:
102                 return "Auto-connect";
103         case 3:
104                 return "Connection Status";
105             default:
106                     return "";
107         }
108     }
109 
110     public boolean isCellEditable(int rowIndex, int columnIndex) {
111         return columnIndex == 2;
112     }
113 
114     public Class getColumnClass(int columnIndex) {
115         switch(columnIndex) {
116         case 2:
117                return Boolean.class;
118         default:
119             return super.getColumnClass(columnIndex);
120         }
121     }
122 
123     public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
124         if(columnIndex!=2 || !(aValue instanceof Boolean))  {
125             return;
126         }
127         boolean autoConnect = ((Boolean)aValue).booleanValue();
128         Object device = this.deviceList.get(rowIndex);
129         String autoConnectHandle = getAutoConnectHandle((ServiceInfo) device);
130         if(autoConnect) {
131             zeroConfPreferenceModel.getAutoConnectDevices().add(autoConnectHandle);
132         }else {
133             zeroConfPreferenceModel.getAutoConnectDevices().remove(autoConnectHandle);
134         }
135         fireTableDataChanged();
136     }
137     
138     void setZeroConfPluginParent(ZeroConfPlugin parent) {
139         this.plugin = parent;
140     }
141 
142 
143 }