📄 console.java
字号:
package com.zcsoft.jpsc.gui;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import com.zcsoft.comm.SerialPortComm;
import javax.comm.CommPortIdentifier;
import com.zcsoft.swing.ZCFixPanel;
import java.io.File;
import java.util.ResourceBundle;
import java.util.*;
import java.io.*;
import java.net.*;
/**
* <p>Title: 串口通信</p>
* <p>Description: 串口通信实验</p>
* <p>Copyright: Copyright (c) 2004-2005</p>
* <p>Company: Zhicheng Software&Service Co. Ltd.</p>
* @author 蒋智湘
* @version 1.0
*/
/** 串口通信测试操作平台 */
public class Console extends JFrame
{
private static ResourceBundle res = JpscToolkit.getStringResourceBundle();
/** 用于存放各个串口之操作界面窗体的桌面 */
private JDesktopPane desktop = new JDesktopPane();
/**
* 用于存储界面大小、位置等属性的映射表。
* @see #fetchGuiPropertiesFromFile
* @see #saveGuiProperties
*/
private Properties guiProperties = new Properties();
/** 存储guiProperties内容的文件 */
private static File fileGuiProperties = new File(res.getString("file.gui.properties"));
/** 工具条 */
private JToolBar tb;
/** 放在菜单和工具条中的按钮事件 */
private Action actionOpen = new OpenPortAction();
private Action actionConvert = new ConvertAction();
/** 退出是否存储界面属性的复选菜单项
* @see #dispose
* @see #saveGuiProperties
*/
private JCheckBoxMenuItem miStoreGuiProperties;
private Console()
{
super(res.getString("console.title"));
JOptionPane.setRootFrame(this);
guiProperties = fetchGuiPropertiesFromFile();
setWorkDir(guiProperties.getProperty("work.dir"));
if (JpscToolkit.booleanValue(guiProperties.getProperty("showToolBar")))
{
installToolBar();
}
installMenuBar();
installDesktop();
setBounds(guiProperties);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
private void setBounds(Properties guiProperties)
{
Rectangle thisBounds;
if ( (thisBounds = JpscToolkit.rectValue(guiProperties.getProperty("console.bounds"))) != null)
{
this.setBounds(thisBounds);
}
else
{
this.setBounds(0, 50, 800, 550);
}
}
private void installToolBar()
{
tb = new JToolBar();
tb.setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
// tb.setFloatable(false);
JButton button = tb.add(this.actionOpen);
button.setFocusPainted(false);
tb.addSeparator();
button = tb.add(this.actionConvert);
button.setFocusPainted(false);
this.getContentPane().add(tb, BorderLayout.NORTH);
}
private void installMenuBar()
{
JMenuBar mb = new JMenuBar();
JMenu mn = mb.add(new JMenu());
JpscToolkit.setCommandPropterties(mn, "menu.file");
JMenuItem mi;
mi = mn.add(new SetWorkDirAction());
JpscToolkit.setCommandPropterties(mi, "menuItem.setWorkDir");
mi.setAccelerator(KeyStroke.getKeyStroke(res.getString("menuItem.setWorkDir.accelerator")));
mi = mn.add(this.actionOpen);
mi.setAccelerator(KeyStroke.getKeyStroke(res.getString("menuItem.open.accelerator")));
mi = mn.add(this.actionConvert);
mi.setAccelerator(KeyStroke.getKeyStroke(res.getString("menuItem.convert.accelerator")));
mn.addSeparator();
mi = mn.add(new QuitAction());
JpscToolkit.setCommandPropterties(mi, "menuItem.quit");
mn = mb.add(new JMenu());
JpscToolkit.setCommandPropterties(mn, "menu.view");
mi = mn.add(new JCheckBoxMenuItem());
mi.setSelected(this.tb != null);
mi.addItemListener(new ShowToolBarListener());
JpscToolkit.setCommandPropterties(mi, "menuItem.showToolBar");
mn.addSeparator();
mi = mn.add(this.miStoreGuiProperties = new JCheckBoxMenuItem());
JpscToolkit.setCommandPropterties(mi, "menuItem.storeGuiProperties");
mn = mb.add(new JMenu());
JpscToolkit.setCommandPropterties(mn, "menu.help");
mn.add(new ShowAboutAction());
this.setJMenuBar(mb);
}
private void installDesktop()
{
this.getContentPane().add(desktop);
}
public void dispose()
{
closePortFrames();
if (miStoreGuiProperties.isSelected())
{
saveGuiProperties();
}
super.dispose();
System.exit(0);
}
/** 根据串口名称找到可能已被打开的它的操作界面 */
private boolean portOpened(String port)
{
JInternalFrame[] allPortFrames = this.desktop.getAllFrames();
for (int i = 0; i < allPortFrames.length; i++)
{
PortFrame f = (PortFrame)allPortFrames[i];
if(f.getPortID().getName().equals(port))
{
return true;
}
}
return false;
}
/**
* 关闭桌面中已经被打开的串口操作界面窗体。
* 串口操作界面窗体关闭时,程序会释放串口资源,并更新guiProperties中有关这些窗体的界面属性。
*/
private void closePortFrames()
{
JInternalFrame[] allPortFrames = this.desktop.getAllFrames();
for (int i = 0; i < allPortFrames.length; i++)
{
allPortFrames[i].dispose();
}
}
/** 提取启动路径下的gui.properties文件中的内容,并依据这些内容构建guiProperties */
private Properties fetchGuiPropertiesFromFile()
{
FileInputStream is = null;
try
{
is = new FileInputStream(fileGuiProperties);
guiProperties.load(is);
}
catch (FileNotFoundException ex){}
catch (IOException ex)
{
System.out.println(ex);
}
return guiProperties;
}
/**
* 将guiProperties永久记录到启动目录下的文本文件gui.properties中
*/
private void saveGuiProperties()
{
guiProperties.setProperty("console.bounds", JpscToolkit.stringValue(this.getBounds()));
guiProperties.setProperty("showToolBar", JpscToolkit.stringValue(this.tb != null));
guiProperties.setProperty("work.dir", System.getProperty("work.dir"));
FileOutputStream out = null;
try
{
out = new FileOutputStream(fileGuiProperties);
guiProperties.store(out, null);
}
catch(Exception ex)
{
System.err.println(ex);
}
finally
{
if (out != null)
{
try
{
out.close();
}
catch (IOException ex){}
}
}
}
private static void setWorkDir(String dir) throws MissingResourceException
{
File workDir = new File(dir==null?String.valueOf('.'):dir);
if (!workDir.exists())
{
workDir.mkdirs();
}
if (!workDir.isDirectory())
{
throw new RuntimeException(JpscToolkit.getFormatedString("startup.invalid.workDir", dir));
}
//System.out.println("workDir = " + workDir.getAbsolutePath());
System.setProperty("work.dir", workDir.toString());
}
/**
* @todo 打开串口事件处理
*/
private class OpenPortAction extends com.zcsoft.swing.ZCAction
{
private OpenPortAction()
{
super(res.getString("command.open.caption"));
this.setTooltip(getName());
this.setIcon(com.zcsoft.util.ResourceLoader.createImageIcon(this.getClass(), "open"));
}
/**
* 事件处理实现
* @param e 事件
*/
public void actionPerformed(java.awt.event.ActionEvent e)
{
java.util.List portNames = SerialPortComm.getAvailablePorts();
for (int i = 0; i < portNames.size(); i++)
{
String name = (String)portNames.get(i);
if (portOpened(name))
{
portNames.remove(name);
i --;
}
}
Component eventSource = (Component)e.getSource();
//对于菜单触发的事件,放置输入框整个界面的中间
//而对于工具条上按钮触发的事件,则放置输入框于触发按钮的中间
if ( JOptionPane.getFrameForComponent(eventSource) == null )
{
eventSource = Console.this;
}
if (portNames.size() == 0)
{
JpscToolkit.showMessageDialog(eventSource, res.getString("warning.nonOpenablePort"));
return;
}
String selectedPort =
(String)JOptionPane.showInputDialog(eventSource
, res.getString("port.open.dialog.message")
, this.getName()
, JOptionPane.QUESTION_MESSAGE, null
, portNames.toArray(), portNames.get(0));
if (selectedPort != null)
{
PortFrame frame = new PortFrame(new SerialPortComm(selectedPort), guiProperties);
desktop.add(frame, JLayeredPane.DEFAULT_LAYER);
frame.show();
}
}
}
/**
* @todo 数值转换的事件处理
*/
private class ConvertAction extends com.zcsoft.swing.ZCAction
{
JDialog convertorDialog;
private ConvertAction()
{
super(res.getString("command.convert.caption"));
this.setTooltip(this.getName());
this.setIcon(com.zcsoft.util.ResourceLoader.createImageIcon(this.getClass(), "convert"));
}
/**
* 事件处理实现
* @param e 事件
*/
public void actionPerformed(java.awt.event.ActionEvent e)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -