⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 messagetable.java

📁 无线通信的主要编程软件,是无线通信工作人员的必备工具,关天相关教程我会在后续传上.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * Copyright (c) 2003, Vanderbilt University * All rights reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written agreement is * hereby granted, provided that the above copyright notice, the following * two paragraphs and the author appear in all copies of this software. *  * IN NO EVENT SHALL THE VANDERBILT UNIVERSITY BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE VANDERBILT * UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *  * THE VANDERBILT UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS * ON AN "AS IS" BASIS, AND THE VANDERBILT UNIVERSITY HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. */package net.tinyos.mcenter;import net.tinyos.packet.*;import java.util.*;import java.util.prefs.*;import javax.swing.table.*;import java.awt.event.*;/** * @author  Miklos Maroti (miklos.maroti@vanderbilt.edu) */public class MessageTable extends MessageCenterInternalFrame implements PacketListenerIF{	static final int TYPE_INT8 = 1;	static final int TYPE_UINT8 = 2;	static final int TYPE_HEX8 = 3;	static final int TYPE_INT16 = 4;	static final int TYPE_UINT16 = 5;	static final int TYPE_HEX16 = 6;	static final int TYPE_INT32 = 7;	static final int TYPE_UINT32 = 8;	static final int TYPE_HEX32 = 9;	static final int TYPE_FLOAT = 10;	static final int TYPE_CHAR = 11;	static final int TYPE_UINT24 = 16;	static final int TYPE_INT24 = 17;	static final int TYPE_HEX24 = 18;	static final int TYPE_NONE = 0;			// invalid type	static final int TYPE_TIME = 101;		// current time	static final int TYPE_COUNTER = 102;		// increasing counter	static final int MODIFIER_OMIT = 0x01;		// do not display this idtem	static final int MODIFIER_UNIQUE = 0x02;	// must be unique	static final int MODIFIER_CONST = 0x04;		// value must match title		static final int PACKET_TYPE = 2;	static final int PACKET_LENGTH = 4;	static final int PACKET_DATA = 5;	static final int PACKET_TOTAL = 36;			private class MyTableModel extends DefaultTableModel	{		java.text.SimpleDateFormat timestamp = new java.text.SimpleDateFormat("HH:mm:ss.SSS");		class Entry		{			String title;			int type;			int modifier;			int column;			String value;			String defValue;		};		ArrayList entries = new ArrayList();		int[] uniqueEntries = new int[0];		// the index of unique entries		int packetLength;				byte[] packet;		int packetIndex;		byte getByte()		{			return (byte)(packet[packetIndex++] & 0xFF);		}		short getShort()		{			return (short)((packet[packetIndex++] & 0xFF) 				+ ((packet[packetIndex++] & 0xFF) << 8));		}		int getInt24()		{			return (packet[packetIndex++] & 0xFF) 				+ ((packet[packetIndex++] & 0xFF) << 8)				+ ((packet[packetIndex++] & 0xFF) << 16);		}		int getInt()		{			return (packet[packetIndex++] & 0xFF) 				+ ((packet[packetIndex++] & 0xFF) << 8)				+ ((packet[packetIndex++] & 0xFF) << 16)				+ ((packet[packetIndex++] & 0xFF) << 24);		}		String toHex(int value, int len)		{			String a = Integer.toHexString(value);			String b = "0x";			for(int i = a.length(); i < len; ++i)				b += '0';			b += a;			return b;		}		String decodeField(int type)		{			switch(type)			{			case TYPE_UINT8:				return Integer.toString(getByte() & 0xFF);			case TYPE_INT8:				return Byte.toString(getByte());			case TYPE_HEX8:				return toHex(getByte() & 0xFF, 2);			case TYPE_UINT16:				return Integer.toString(getShort() & 0xFFFF);			case TYPE_INT16:				return Short.toString(getShort());			case TYPE_HEX16:				return toHex(getShort() & 0xFFFF, 4);			case TYPE_UINT24:				return Integer.toString(getInt24());			case TYPE_INT24:				int a = getInt24();				return Integer.toString(a < 0x800000 ? a : a - 0x1000000);			case TYPE_HEX24:				return toHex(getInt24(), 6);			case TYPE_UINT32:				return Long.toString(getInt() & 0xFFFFFFFFL);			case TYPE_INT32:				return Integer.toString(getInt());			case TYPE_HEX32:				return toHex(getInt(), 8);			case TYPE_FLOAT:				return Float.toString(Float.intBitsToFloat(getInt()));			case TYPE_CHAR:				return "'" + (char)getByte() + "'";			case TYPE_TIME:				return timestamp.format(new java.util.Date());							case TYPE_COUNTER:				return "1";			}			return null;		}			// returns true if the packet is valid		boolean decodePacket(byte[] packet)		{			if( (packet[PACKET_LENGTH] & 0xFF) != packetLength )				return false;						this.packet = packet;			packetIndex = PACKET_DATA;			Iterator iter = entries.iterator();			while( iter.hasNext() )			{				Entry entry = (Entry)iter.next();				entry.value = decodeField(entry.type);				// const value does not match				if( (entry.modifier & MODIFIER_CONST) != 0 && ! entry.value.equals(entry.defValue) )					return false;			}				return true;		}		int findReplaceIndex()		{			if( uniqueEntries.length == 0 )				return -1;			outer: for(int i = 0; i < getRowCount(); ++i)			{				for(int j = 0; j < uniqueEntries.length; ++j)				{					Entry entry = (Entry)entries.get(uniqueEntries[j]);					if( ! entry.value.equals(getValueAt(i,entry.column)) )						continue outer;				}								return i;			}						return -1;		}		void writeRow(int index, boolean updateCounterOnly)		{			for(int i = 0; i < entries.size(); ++i)			{				Entry entry = (Entry)entries.get(i);				if( entry.column >= 0 )				{					if( entry.type == TYPE_COUNTER )						entry.value = Integer.toString(parseInt((String)getValueAt(index, entry.column)) + 1);										if (!updateCounterOnly || entry.type == TYPE_COUNTER)						setValueAt(entry.value, index, entry.column);				}			}		}				void addPacket(byte[] packet)		{			if( ! decodePacket(packet) )				return;						int index = findReplaceIndex();			if( index < 0 )			{				int c = getRowCount();				setRowCount(c+1);				rowCount.setText("row count: " + getRowCount());				writeRow(c, false);				fireTableRowsInserted(c, c);			}			else			{				writeRow(index, firstUniqueBox.isSelected());				fireTableRowsUpdated(index, index);			}		}		void addEntry(Entry entry)		{			if( entry.title == null )				entry.title = "unnamed";						if( entry.type == TYPE_NONE )			{				errorText.setText("missing type");				return;			}						if( (entry.modifier & MODIFIER_OMIT) == 0 )			{				entry.column = getColumnCount();				addColumn(entry.title);			}			else				entry.column = -1;			entries.add(entry);		}				void addEntry(String line)		{			int i = line.indexOf(';');			if( i >= 0 )				line = line.substring(0, i);						Entry entry = new Entry();			entry.type = TYPE_NONE;			StringTokenizer tokens = new StringTokenizer(line, " \t,");			while( tokens.hasMoreTokens() )			{				String token = tokens.nextToken();				String ltoken = token.toLowerCase();								if( ltoken.endsWith("_t") )					ltoken = ltoken.substring(0, ltoken.length()-2);				if( entry.type == TYPE_NONE )				{					if( ltoken.equals("uint8") )					{						entry.type = TYPE_UINT8;						packetLength += 1;					}					else if( ltoken.equals("int8") )					{						entry.type = TYPE_INT8;						packetLength += 1;					}					else if( ltoken.equals("hex8") )					{						entry.type = TYPE_HEX8;						packetLength += 1;					}					else if( ltoken.equals("uint16") )					{						entry.type = TYPE_UINT16;						packetLength += 2;					}					else if( ltoken.equals("int16") )					{						entry.type = TYPE_INT16;						packetLength += 2;					}					else if( ltoken.equals("hex16") )					{						entry.type = TYPE_HEX16;						packetLength += 2;					}					else if( ltoken.equals("uint24") )					{						entry.type = TYPE_UINT24;						packetLength += 3;					}					else if( ltoken.equals("int24") )					{						entry.type = TYPE_INT24;						packetLength += 3;					}					else if( ltoken.equals("hex24") )					{						entry.type = TYPE_HEX24;						packetLength += 3;					}					else if( ltoken.equals("uint32") )					{						entry.type = TYPE_UINT32;						packetLength += 4;					}					else if( ltoken.equals("int32") )					{						entry.type = TYPE_INT32;						packetLength += 4;					}					else if( ltoken.equals("hex32") )					{						entry.type = TYPE_HEX32;						packetLength += 4;					}					else if( ltoken.equals("float") )					{						entry.type = TYPE_FLOAT;						packetLength += 4;					}					else if( ltoken.equals("char") )					{						entry.type = TYPE_CHAR;						packetLength += 1;					}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -