File: AirportLoginStringTable.java

package info (click to toggle)
airport-utils 2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 4,048 kB
  • sloc: java: 30,844; xml: 571; sh: 563; makefile: 10
file content (459 lines) | stat: -rw-r--r-- 10,984 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 * AirportBaseStationConfigurator
 *
 * Copyright (C) 2000, 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 airport;


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import byteblock.*;

/**
*	Handles display and updating of entries used in login string.
*/

public class AirportLoginStringTable extends AirportInfoPanel
{
	private AirportInfo airportInfo;
	private Vector tableContentsVector;
	private JTable table;
	private JScrollPane tableScrollPane;
	private AbstractTableModel tableModel;
	private JComboBox elementTypeComboBox;
	
	private final int MAX_NUM_LOGIN_STRING_ENTRIES = 20;
		
	
	/**
	*	Table model which maintains list of login string elements.
	*/
	
	private class LoginStringTableModel extends AbstractTableModel
	{
		public int getColumnCount() 
		{ 
			return 3; 
		}
		
		public int getRowCount() 
		{ 
			return MAX_NUM_LOGIN_STRING_ENTRIES;
		}
		
		public boolean isCellEditable(int row, int col) 
		{ 
			if (row <= tableContentsVector.size())
			{
				if (col < 2)
					return true;
				else 	// carriage return column; editable only if send or receive (not delay)
				{
					
					if (row == tableContentsVector.size())
						return false;
					else 
					{
						Object[] currentRow = (Object[])tableContentsVector.elementAt(row);
						if ( ((String)currentRow[0]).equals("Send string") || ((String)currentRow[0]).equals("Expect string") )
							return true;
						else
							return false;
					}
				}
			}
			else
				return false;
		}
		
		public String getColumnName(int col) 
		{ 
			switch (col)
			{
				case 0:
					return "Element type";
				case 1:
					return "Value";
				default:
					return "Carriage return";
			} 
		}
		
		
		/*
         * JTable uses this method to determine the default renderer/
         * editor for each cell.
         */
         
        public Class getColumnClass(int column) 
        {
            String stringDummy = "";
            Boolean booleanDummy = new Boolean(true);
            
            switch (column)
            {
            	case 0:
            		return stringDummy.getClass();
            	
            	case 1:
            		return stringDummy.getClass();
            	
            	default:
            		return booleanDummy.getClass();
            }
            
        }
        
		
		public Object getValueAt(int row, int col)
		{
			//System.out.println("Get value: " + row + ", " + col);
			
			if (row < tableContentsVector.size())
			{
				return ((Object[])tableContentsVector.elementAt(row))[col];
			}
			else
			{
				if (col < 2)
					return "";
				else
					return new Boolean(false);
			}
		}
		
		public void setValueAt(Object newValue, int row, int col) 
		{
			if (row < tableContentsVector.size())
			{
				Object[] addressEntry = (Object[])tableContentsVector.elementAt(row);
				addressEntry[col] = newValue;
				
				// if change type to "delay", erase cr entry
				if ( (col == 0) && (((String)newValue).equals("Delay (seconds)")) )
				{
					addressEntry[2] = new Boolean(false);
					fireTableCellUpdated(row, 2);
				}
			}
			else
			{
				Object[] addressEntry = {"", "", new Boolean(false)};
				addressEntry[col] = newValue;
				tableContentsVector.insertElementAt(addressEntry, tableContentsVector.size());
			}
			
			//System.out.println("New value: " + newValue);
			
			fireTableCellUpdated(row, col);
		
		}
		
	}
	
	
	
	
	
	
	
	/**
	*	Create new table based on data in airportInfo.
	*/
	
	public AirportLoginStringTable(AirportInfo airportInfo)
	{
		this.airportInfo = airportInfo;
		
		tableContentsVector = new Vector();
		
		elementTypeComboBox = new JComboBox();
		elementTypeComboBox.addItem("");
		elementTypeComboBox.addItem("Expect string");
		elementTypeComboBox.addItem("Send string");
		elementTypeComboBox.addItem("Delay (seconds)");
		
		
		setUpDisplay();
	}
	
	
	
	
	private void setUpDisplay()
	{
		refreshDisplay();
	}
	
	
	
	
	
	
	
	
	
	/**
	*	Write the modem string information into the appropriate location
	*	in the byte block referenced by the AirportInfo object supplied in the constructor.
	*	@throws ValueFormatException Thrown if an entry is malformed. Also sets 
	*	editting focus to the offending cell.
	*/
	
	public void writeValue()
		throws ValueFormatException
	{
		
		// first, record changes if cell currently being edited
		TableCellEditor editor = table.getCellEditor(); 
		if(editor != null)
			editor.stopCellEditing();
		
		
		
		
		// erase current block of info
		AirportInfoRecord loginStringBlockRecord = airportInfo.get("Login information string");
		loginStringBlockRecord.clearWindow();
		
		byte[] loginBytes = new byte[AirportInfo.MAX_NUM_LOGIN_CHARS];
		
		int currentPosition = 0;
		
		for (int i = 0; i < tableContentsVector.size(); i++)
		{
			
			String elementTypeString = (String)((Object[])tableContentsVector.elementAt(i))[0];
			String elementValueString = (String)((Object[])tableContentsVector.elementAt(i))[1];
			boolean carriageReturnFlag = ((Boolean)((Object[])tableContentsVector.elementAt(i))[2]).booleanValue();
			
			// if null type, just ignore
			if (!(elementTypeString.equals("")))
			{
				
				
				try
				{
					
					// set start byte: 
					//		Send string = 0x40, Expect string = 0x80, Delay = 0xC0
					//			plus number of characters to follow, or delay value
					
					if (elementTypeString.equals("Expect string") || elementTypeString.equals("Send string"))
					{
						if (elementTypeString.equals("Expect string"))
							loginBytes[currentPosition] = (byte)0x80;
						else if (elementTypeString.equals("Send string"))
							loginBytes[currentPosition] = (byte)0x40;
						
						loginBytes[currentPosition] += elementValueString.length();
						
						if (carriageReturnFlag == true) 
							++loginBytes[currentPosition];
							
						++currentPosition;
						
						byte[] valueBytes = elementValueString.getBytes();
						for (int j = 0; j < elementValueString.length(); j++)
						{
							loginBytes[currentPosition] = valueBytes[j];
							++currentPosition;
						}
						
						if (carriageReturnFlag == true)
						{
							loginBytes[currentPosition] = 0x0D;	// CR in ACSCII
							++currentPosition;
						}
						
					}
					else if (elementTypeString.equals("Delay (seconds)"))
					{
						int delayValue = Integer.parseInt(elementValueString);
						loginBytes[currentPosition] = (byte)(0xC0 + delayValue);
						++currentPosition;
						if ((delayValue < 0) || (delayValue > 63))
						{
							//table.editCellAt(i,1);
							throw new ValueFormatException("Delay must be between 0 and 63 seconds");
						}
					}
					
					
				}
				catch (NumberFormatException e)
				{
					//table.editCellAt(i,1);
					throw new ValueFormatException("Bad delay value");
				}
				catch (ArrayIndexOutOfBoundsException e)
				{
					throw new ValueFormatException("Login string too long (maximum size 127 characters)");
				}
				
				
				loginStringBlockRecord.byteBlockWindow.writeBytes(loginBytes);
				
			
			}
				
		}
		
		
	}
		
		
		
	
	/**
	*	Refresh the display based on the current data in the underlying byte block.
	*/
	
	public void refreshDisplay()
	{
		
		AirportInfoRecord loginStringBlockRecord = airportInfo.get("Login information string");
			
		byte[] loginBytes = loginStringBlockRecord.byteBlockWindow.getBytes();
		
		int currentPosition = 0;
		
	WhileLoop:
	
		while (currentPosition < loginBytes.length)
		{
			
			int typeAndLength = loginBytes[currentPosition];
			++currentPosition;
			
			// make positive
			if (typeAndLength < 0)
				typeAndLength += 256;
				
			int length = typeAndLength % 64;
			int type = typeAndLength - length;
			
					
			switch (type)
			{
				case 0xC0:	// delay entry
				{
					Object[] addressEntry = {"Delay (seconds)", Integer.toString(length), new Boolean(false)};
					tableContentsVector.insertElementAt(addressEntry, tableContentsVector.size());
					break;
				}
				
				case 0x40:	// send string, receive string entry
				case 0x80:
				{
					String valueString = new String(loginBytes);
					valueString = valueString.substring(currentPosition, currentPosition + length);
					currentPosition += length;
					boolean crFlag;
					
					if (loginBytes[currentPosition - 1] == 0x0d)	// see if last char is CR
					{
						// if last char is CR, strip it off and set crFlag
						valueString = valueString.substring(0, length - 1);
						crFlag = true;
					}
					else
					{
						crFlag = false;
					}
					
					Object[] addressEntry = new Object[3];
					
					addressEntry[1] = valueString;
					addressEntry[2] = new Boolean(crFlag);
						
					if (type == 0x40)
					{
						addressEntry[0] = "Send string";
					}
					else	// (type == 0x80)
					{
						addressEntry[0] = "Expect string";
					}
						
					tableContentsVector.insertElementAt(addressEntry, tableContentsVector.size());
					break;
				}
				
				
				default:
				{
					// break out of while loop...
					break WhileLoop;
				}
				
			}	
			
		}
		
		
		
		
		// remove all components...
		this.removeAll();
		
		// create new table model and table...
		tableModel = new LoginStringTableModel();
		table = new JTable(tableModel);
		table.setCellSelectionEnabled(true);
		
		//Set the editor for the interface column.
		table.getColumn("Element type").setCellEditor(new DefaultCellEditor(elementTypeComboBox));
		
		table.setPreferredScrollableViewportSize(new Dimension(300,150));
		
		// add it to a scroll pane
		tableScrollPane = new JScrollPane(table);
		
		
		this.add(tableScrollPane);
		
		
		
	}
	
	
	
	/**
	*	Enable and disable JTable as well as any other contained components.
	*/
	
	public void setEnabled(boolean enabled)
	{
		super.setEnabled(enabled);
		
		table.setEnabled(enabled);
		
	}
	
	
	

}