File: KnownHostTable.java

package info (click to toggle)
airport-utils 2-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,020 kB
  • ctags: 5,068
  • sloc: java: 30,844; xml: 571; sh: 553; makefile: 38
file content (317 lines) | stat: -rw-r--r-- 7,571 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * Wireless Host Monitor
 *
 * Copyright (C) 2003, Jonathan Sevy <jsevy@mcs.drexel.edu>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

package airporthostmon; 
 
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

/**
*	Handles display and updating of port mapping information.
*/

public class KnownHostTable extends JPanel
{
	private Vector displayVector;
	private JTable table;
	private AbstractTableModel tableModel;
	private JScrollPane scrollPane;
	
	
	
	/**
	*	Table model which maintains list of name - mac address pairs.
	*/
	private class KnownHostTableModel extends AbstractTableModel
	{
		public int getColumnCount() 
		{ 
			return 2; 
		}
		
		public int getRowCount() 
		{ 
			return displayVector.size() + 1;
		}
		
		public boolean isCellEditable(int row, int col) 
		{ 
			// allow first empty row to be editted
			if (row <= displayVector.size())
			    return true;
			else
			    return false;
		}
		
		public String getColumnName(int col) 
		{ 
			switch (col)
			{
				case 0:
					return "Name";
					
				case 1:
					return "MAC address";
					
				default:
					return "";
			}
			
		}
		
		public Object getValueAt(int row, int col)
		{
			
			if (row < displayVector.size())
			{
				String[] hostInfo = (String[])displayVector.elementAt(row);
				
				switch (col)
    			{
    				case 0:
    					return hostInfo[0];     // name
    					
    				case 1:
    					return hostInfo[1];     // MAC address
    					
    				default:
    					return "";
    			}
		    }
			else
				return "";
		}
		
		public void setValueAt(Object newValue, int row, int col) 
		{
			// do nothing if row is bigger than vector size
			if (row > displayVector.size())
			    return;
			
			if (newValue instanceof String)
			{
				if (row < displayVector.size())
				{
					String[] hostInfo = (String[])displayVector.elementAt(row);
					hostInfo[col] = (String)newValue;
				}
				else
				{
					String[] hostInfo = {"",""};
					hostInfo[col] = (String)newValue;
					displayVector.insertElementAt(hostInfo, displayVector.size());
				}
			}

		}
		
	}
	
	
	
	
	
	
	/**
	*	Create new empty table
	*/
	
	public KnownHostTable()
	{
		
		displayVector = new Vector();
		table = new JTable();
		tableModel = new KnownHostTableModel();
		table.setModel(tableModel);
        table.setPreferredScrollableViewportSize(new Dimension(300,300));
		scrollPane = new JScrollPane(table);
		setUpDisplay();
	}
	
	
	
	/**
	*	Create new table based on data in config file.
	*/
	
	public KnownHostTable(TreeMap knownHostTreeMap)
	{
		displayVector = new Vector();
		Collection knownHostCollection = knownHostTreeMap.values();
		Iterator iterator = knownHostCollection.iterator();
		
		while (iterator.hasNext())
		{
		    KnownHostInfo knownHostInfo = (KnownHostInfo)iterator.next();
		    String[] hostInfo = {knownHostInfo.name, knownHostInfo.macAddress.toString()};
		    displayVector.add(hostInfo);
		}
		table = new JTable();
		tableModel = new KnownHostTableModel();
		table.setModel(tableModel);
        table.setPreferredScrollableViewportSize(new Dimension(300,300));
		scrollPane = new JScrollPane(table);
		setUpDisplay();
	}
	
	
	
	
	private void setUpDisplay()
	{
		
		GridBagLayout  theLayout = new GridBagLayout();
		this.setLayout(theLayout);
		
		GridBagConstraints c = new GridBagConstraints();
		
		c.gridwidth = 1;
		c.gridheight = 1;
		c.fill = GridBagConstraints.BOTH;
		c.ipadx = 0;
		c.ipady = 0;
		Insets theMargin = new Insets(2,2,2,2);
		c.insets = theMargin;
		c.anchor = GridBagConstraints.CENTER;
		c.weightx = .5;
		c.weighty = .5;
		
		c.gridx = 1;
		c.gridy = 1;
		theLayout.setConstraints(scrollPane, c);
		this.add(scrollPane);
	}
	
	
	
	public synchronized void setInfo(Vector knownHostVector)
	{
		displayVector = new Vector();
		for (int i = 0; i < knownHostVector.size(); i++)
		{
		    KnownHostInfo knownHostInfo = (KnownHostInfo)knownHostVector.elementAt(i);
		    String[] hostInfo = {knownHostInfo.name, knownHostInfo.macAddress.toString()};
		    displayVector.add(hostInfo);
		}
		
		tableModel.fireTableDataChanged();
	}
	
	
	
	public synchronized void addHost(KnownHostInfo knownHostInfo)
	{
		String newMacAddressString = knownHostInfo.macAddress.toString().trim();
		String newName = knownHostInfo.name;
		
		// see if MAC address already in list
		for (int i = 0; i < displayVector.size(); i++)
    	{
    	    String[] hostInfo = (String[])displayVector.elementAt(i);
    	    String name = hostInfo[0];
    	    String macAddressString = hostInfo[1].trim();
    	    
    	    // if MAC address equals that we're adding, return
    	    if (macAddressString.equals(newMacAddressString))
    	        return;
    	        
    	}
    	
    	// didn't find it; add new info to table
    	String[] newHostInfo = {newName, newMacAddressString};
		displayVector.add(newHostInfo);
		
	    tableModel.fireTableDataChanged();
	}
	
	
	
	
	public synchronized TreeMap getKnownHostTreeMap()
	    throws ValueFormatException
	{
	    // stop any in-progress editing operations
	    // first, record changes if cell currently being edited
		TableCellEditor editor = table.getCellEditor(); 
		if(editor != null)
			editor.stopCellEditing();
		
		// try to create a Vector of KnownHostInfo objects; this could throw
		// a ValueFormatException if there's an invalid MAC address
		TreeMap knownHostTreeMap = new TreeMap();
		int i = 0;
		try
		{
    		for (i = 0; i < displayVector.size(); i++)
    		{
    		    String[] hostInfo = (String[])displayVector.elementAt(i);
    		    String name = hostInfo[0];
    		    String macAddressString = hostInfo[1].trim();
    		    
    		    // if MAC address string is empty after trimming, just continue
    		    if (macAddressString.equals(""))
    		        continue;
    		        
    		    MacAddress macAddress = new MacAddress(macAddressString);
    		    
    		    // throw exception if duplicate entry
    		    if (knownHostTreeMap.containsKey(macAddress))
    		        throw new ValueFormatException("Duplicate entry for MAC address " + hostInfo[1]);
    		        
    		    knownHostTreeMap.put(macAddress, new KnownHostInfo(name, macAddress)); 
    		}
		}
		catch(ValueFormatException e)
		{
		    // set the editing to the mac address in the row that caused the problem
		    //table.editCellAt(i,1);
			
			// rethrow the exception so can report it
			throw e;
		}
			
	    return knownHostTreeMap;
	}
		
	
	
	
	public void setColumnPreferredWidths(JTable table) 
	{
		TableModel tableModel = table.getModel();
		TableColumnModel tableColumnModel = table.getColumnModel();
		
		for (int i = 0; i < tableModel.getColumnCount(); i++)
		{
		    int stringSize = tableModel.getColumnName(i).length();
		    tableColumnModel.getColumn(i).setPreferredWidth(stringSize*20);
		}
	}
		
	


}