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 javax.jmdns.ServiceEvent;
20  import javax.jmdns.ServiceInfo;
21  import javax.jmdns.ServiceListener;
22  import javax.swing.table.AbstractTableModel;
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  public class ZeroConfDeviceModel extends AbstractTableModel implements ServiceListener {
28  
29      private List<ServiceInfo> deviceList = new ArrayList<>();
30      private ZeroConfPreferenceModel zeroConfPreferenceModel;
31      private transient ZeroConfPlugin plugin;
32  
33      public ZeroConfDeviceModel() {
34      }
35  
36      public int getRowCount() {
37          return deviceList.size();
38      }
39  
40      public int getColumnCount() {
41          return 4;
42      }
43  
44      public ServiceInfo getServiceInfoAtRow(int row) {
45          return deviceList.get(row);
46      }
47  
48      public Object getValueAt(int rowIndex, int columnIndex) {
49          ServiceInfo info = 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<ServiceInfo> iter = deviceList.iterator(); iter.hasNext(); ) {
77              ServiceInfo info = 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;
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 }