📄 portframe.java
字号:
{
try
{
fos.close();
}
catch (IOException ex){}
}
}
}
}//end inner class SaveA
/**
* @todo 向表格中装入存储在文件中的历史数据的事件处理
*/
private class LoadHistoryA extends com.zcsoft.swing.ZCAction
{
private LoadHistoryA()
{
super(res.getString("command.loadHistory.caption"));
this.setIcon(com.zcsoft.util.ResourceLoader.createImageIcon(this.getClass(), "loadHistory"));
}
/**
* 事件处理实现
* 读取工作目录下对象存储文件JpscCOMN.msg(N代表串口号,不同串口对应不同的文件),
* 然后判断当前表格中是否存在数据行。如果存在,则提醒用户是将文件中的内容覆盖表格中的内容,
* 还是追加到表格末尾,抑或取消操作;如果没有数据行,则直接将文件中的数据填入表格中。
* 该文件存储唯一个二维Vector对象,该对象完全对应表格数据模型中dataVector。
* 如果该文件不存在,则给出提示,提示消息中应该包含文件的完整路径。
* @param e 事件
*/
public void actionPerformed(java.awt.event.ActionEvent e)
{
Vector dataInFile = fetchDataFromFile(e);
if (dataInFile != null)
{
if (tm.getRowCount() > 0)
{
int option = JOptionPane.showConfirmDialog(SendPane.this,
res.getString("append.dialog.message"),
res.getString("sure.dialog.title"),
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (option == JOptionPane.YES_OPTION)
{
tm.addRows(dataInFile);
}
else if(option == JOptionPane.NO_OPTION)
{
tm.setDataVector(dataInFile);
}
}
else
{
tm.setDataVector(dataInFile);
}
}
}
private Vector fetchDataFromFile(ActionEvent e)
{
Vector dataVectorInFile = null;
ObjectInputStream fos = null;
File outputFile = getHistoryFile();
try
{
if (!outputFile.exists())
{
JpscToolkit.showMessageDialog((Component)e.getSource(),
JpscToolkit.getFormatedString("cannot.find.file"
, outputFile.getAbsolutePath()));
}
else
{
fos = new ObjectInputStream(new FileInputStream(outputFile.getAbsolutePath()));
dataVectorInFile = (Vector)fos.readObject();
}
}
catch (IOException ex)
{
JpscToolkit.showMessageDialog((Component)e.getSource(), ex.toString());
}
catch (ClassNotFoundException ex)
{
System.out.println(ex);
}
finally
{
if (fos != null)
{
try
{
fos.close();
}
catch (IOException ex){}
}
}
return dataVectorInFile;
}
}
/**
* @todo 数据格式变化的事件处理
*/
private class FormatItemListener implements ItemListener
{
private int oldRadix = JpscToolkit.getRadixForItem(cbFormat.getSelectedItem());
/**
* 事件处理实现
* 将不同于新格式的那些选中行中的数据的显示更换成新格式下的显示
* @param e 事件
*/
public void itemStateChanged(ItemEvent e)
{
if (e.getStateChange() == ItemEvent.DESELECTED)
{
oldRadix = JpscToolkit.getRadixForItem(e.getItem());
return;
}
int newRadix = JpscToolkit.getRadixForItem(e.getItem());
String strData = txtTelegram.getText();
byte[] byteArrayData = JpscToolkit.byteArrayOfTelegram(strData, oldRadix);
strData = JpscToolkit.toString(byteArrayData, 0, byteArrayData.length, newRadix);
txtTelegram.setText(strData);
}
}
}
/** 接收面板 */
class ReceivePane extends JComponent implements com.zcsoft.comm.MessageHandler
{
private static ResourceBundle res = JpscToolkit.getStringResourceBundle();
/** 串口接收实现 */
private SerialPortComm portCommunicator;
/** 数据格式 */
private JComboBox cbFormat = JpscToolkit.createFormatCompnent();
/** 复制按钮 */
private JButton cmdCopy = new UncapableButton(new CopyA());
/** 显示接收到的数据的表格 */
private RWTableModel tm = new RWTableModel(new Object[]{
res.getString("table.column.format")
, res.getString("table.column.length")
, res.getString("table.column.telegram")}, 0);
private RowIndexTable table = new RowIndexTable(tm);
/** 表格中保留的显示行数,超过该行数时,最先收到的将被自动地从表格中删除 */
private transient int cntKeepedRows = -1;
/** 存储接收到的电报的文件之输出流 */
private transient PrintStream fosReceivedMessage;
ReceivePane()
{
constructInterface();
this.cbFormat.addItemListener(new FormatItemListener());
SelectedRowDeleteAction.enableTable(table);
}
private void constructInterface()
{
ZCFixPanel fp = new ZCFixPanel();
fp.addComToModel(new JScrollPane(table), 6, 1, GridBagConstraints.BOTH);
fp.newLine();
fp.addComToModel(JpscToolkit.createLabelForComp(("receivePane.field.format.caption"), this.cbFormat));
fp.addComToModel(this.cbFormat);
fp.addComToModel(new UncapableButton(new SetKeepedRowsA()));
fp.addComToModel(Box.createGlue(), true);
fp.addComToModel(this.cmdCopy);
this.setLayout(new BorderLayout());
this.add(fp.getSubPane());
table.setVisibleRowCount(15);
// table.setOrderable(false);
table.setShowHorizontalLines(false);
com.zcsoft.swing.TableChecker.bindTable(table, new Component[]{this.cmdCopy});
}
void setPortCommunicator(SerialPortComm spc)
{
if (this.portCommunicator != null)
{
this.portCommunicator.removeMessageHandler(this);
}
spc.addMessageHandler(this);
this.portCommunicator = spc;
}
/**
* com.zcsoft.comm.MessageHandler接口实现
* 对串口接收到的数据进行处理
*/
public void handleMessage(byte[] msg, int offset, int length)
{
if (cntKeepedRows == 0)
{
if (fosReceivedMessage != null)
{
try
{
storeReceivedDataIntoFile(fosReceivedMessage, msg, offset, length);
}
catch (IOException ex)
{
JpscToolkit.showMessageDialog(this, JpscToolkit.getFormatedString("error.writing.data", ex));
fosReceivedMessage.close();
fosReceivedMessage = null;
}
}
}
else
{
storeReceivedDataIntoTable(msg, offset, length);
}
}
private void storeReceivedDataIntoFile(PrintStream out, byte[] msg, int offset, int length)
throws IOException
{
out.print(cbFormat.getSelectedItem().toString());
out.print(' ');
out.print(Integer.toString(length));
out.print('\t');
out.print(JpscToolkit.toString(msg, offset, length,
JpscToolkit.getRadixForSelectedFormat(cbFormat)));
out.println();
out.flush();
}
private void storeReceivedDataIntoTable(byte[] msg, int offset, int length)
{
Vector newRowData = new Vector(2);
newRowData.add(cbFormat.getSelectedItem());
newRowData.add(new Integer(length));
newRowData.add(JpscToolkit.toString(msg, offset, length, JpscToolkit.getRadixForSelectedFormat(cbFormat)));
synchronized(tm)
{
if (cntKeepedRows > 0)
{
while(tm.getRowCount() >= cntKeepedRows)
{
tm.removeRow(0);
}
}
tm.addRow(newRowData);
table.changeSelection(tm.getRowCount() - 1, 0, false, false);
}
}
/**
* @todo 复制表格中数据到系统剪贴板上的事件处理
*/
private class CopyA extends com.zcsoft.swing.ZCAction
{
private CopyA()
{
super(res.getString("command.copy.caption"));
setIcon(com.zcsoft.util.ResourceLoader.createImageIcon(this.getClass(), "copy"));
}
/**
* 事件处理实现
* @param e 事件
*/
public void actionPerformed(java.awt.event.ActionEvent e)
{
int selectedRows[] = table.getSelectedRows();
if (selectedRows.length < 1)
{
return;
}
Clipboard clip = getClipboard();
if (clip == null)
{
return;
}
clip.setContents(getTransfer(ReceivePane.this.tm, selectedRows), null);
}
private Clipboard getClipboard()
{
try
{
return Toolkit.getDefaultToolkit().getSystemClipboard();
}
catch (SecurityException ex)
{
return null;
}
}
private Transferable getTransfer(javax.swing.table.TableModel tm, int[] rows)
{
StringBuffer buffer = new StringBuffer();
int cntColumns = tm.getColumnCount();
for (int i = 0; i < rows.length; i++)
{
int row = rows[i];
for (int j = 0; j < cntColumns; j++)
{
buffer.append(tm.getValueAt(row, j));
buffer.append('\t');
}
buffer.setCharAt(buffer.length() - 1, '\n');
}
if (buffer.length() > 0)
{
buffer.setLength(buffer.length() - 1);//delete last \n
}
return new StringSelection(buffer.toString());
}
}
/**
* @todo 设定保留行数的事件处理
*/
private class SetKeepedRowsA extends com.zcsoft.swing.ZCAction
{
private SetKeepedRowsA()
{
super(res.getString("command.keepRows.caption"));
}
/**
* 事件处理实现
* 弹出输入对话框,并在输入框上显示当前的保留行数。
* 判断用户输入的新值:1)=0,清空表格内容;2)>0,从首行开始,删除多余的行。
* @param e 事件
*/
public void actionPerformed(java.awt.event.ActionEvent e)
{
Component parent = (Component)e.getSource();
String strRows = (String)JOptionPane.showInputDialog(parent,
res.getString("keepRows.dialog.message"),
res.getString("input.dialog.title"),
JOptionPane.QUESTION_MESSAGE,
null,
null,
String.valueOf(cntKeepedRows));
if (strRows == null) return;
try
{
cntKeepedRows = Integer.parseInt(strRows);
synchronized(tm)
{
if (cntKeepedRows == 0)
{
tm.setRowCount(0);
openFileToSaveMessage(parent);
}
else if(cntKeepedRows > 0 && tm.getRowCount() > cntKeepedRows)
{
int[] deletedRows = new int[tm.getRowCount() - cntKeepedRows];
for (int i = 0; i < deletedRows.length; i++)
{
deletedRows[i] = i;
}
tm.removeRows(deletedRows);
}
if (cntKeepedRows != 0 && fosReceivedMessage != null)
{
fosReceivedMessage.close();
fosReceivedMessage = null;
}
}
}
catch (NumberFormatException ex){}
}
/**
* 打开用于存储接收到的电报之文件的输出流
* 如果流已经打开,则返回;否则,弹出文件选择输入框,然后选择文件
*/
private void openFileToSaveMessage(Component parent)
{
if (fosReceivedMessage != null)
{
return;
}
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.setCurrentDirectory(new File(System.getProperty("user.dir")));
fc.setDialogTitle(res.getString("file.receive.data.dialog.title"));
int cmd = fc.showOpenDialog(parent);
if (cmd == JFileChooser.APPROVE_OPTION)
{
File f = fc.getSelectedFile();
try
{
fosReceivedMessage = new PrintStream(new FileOutputStream(f.getPath(), true));
}
catch (FileNotFoundException ex)
{
JpscToolkit.showMessageDialog(parent, JpscToolkit.getFormatedString("cannot.open.file", f));
}
}
}
}
/**
* @todo 显示接收数据时所使用的格式变化的事件处理
*/
private class FormatItemListener implements ItemListener
{
/**
* 事件处理实现
* 将不同于新格式的那些选中行中的数据的显示更换成新格式下的显示
* @param e 事件
*/
public void itemStateChanged(ItemEvent e)
{
if (e.getStateChange() == ItemEvent.DESELECTED) return;
int[] selectedRows = table.getSelectedRows();
if (selectedRows.length == 0) return;
int newRadix = JpscToolkit.getRadixForItem(e.getItem());
for (int i = 0; i < selectedRows.length; i++)
{
int row = selectedRows[i];
int oldRadix = JpscToolkit.getRadixForItem(tm.getValueAt(row, 0));
if (oldRadix != newRadix)
{
String strData = (String)tm.getValueAt(row, 2);
byte[] byteArrayData = JpscToolkit.byteArrayOfTelegram(strData, oldRadix);
strData = JpscToolkit.toString(byteArrayData, 0, byteArrayData.length, newRadix);
tm.setValueAt(strData, row, 2);
tm.setValueAt(e.getItem(), row, 0);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -