?? serialdemo.java
字號:
/*
* @(#)SerialDemo.java 1.9 98/06/05 SMI
*
* Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license
* to use, modify and redistribute this software in source and binary
* code form, provided that i) this copyright notice and license appear
* on all copies of the software; and ii) Licensee does not utilize the
* software in a manner which is disparaging to Sun.
*
* This software is provided "AS IS," without a warranty of any kind.
* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
* ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
* LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE
* SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS
* BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
* HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING
* OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control
* of aircraft, air traffic, aircraft navigation or aircraft
* communications; or in the design, construction, operation or
* maintenance of any nuclear facility. Licensee represents and
* warrants that it will not use or redistribute the Software for such
* purposes.
*/
import javax.comm.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Properties;
import java.util.Enumeration;
/**
Main file for SerialDemo program. This program illustrates many of the
abilities of the javax.comm api. This file contains the GUI framework that
the program runs in.
*/
public class SerialDemo extends Frame implements ActionListener {
final int HEIGHT = 450;
final int WIDTH = 410;
private MenuBar mb;
private Menu fileMenu;
private MenuItem openItem;
private MenuItem saveItem;
private MenuItem exitItem;
private Button openButton;
private Button closeButton;
private Button breakButton;
private Panel buttonPanel;
private Panel messagePanel;
private TextArea messageAreaOut;
private TextArea messageAreaIn;
private ConfigurationPanel configurationPanel;
private SerialParameters parameters;
private SerialConnection connection;
private Properties props = null;
/**
Main method. Checks to see if the command line agrument is requesting
usage informaition (-h, -help), if it is, display a usage message and
exit, otherwise create a new <code>SerialDemo</code> and set it visible.
*/
public static void main(String[] args) {
if ((args.length > 0)
&& (args[0].equals("-h")
|| args[0].equals("-help"))) {
System.out.println("usage: java SerialDemo [configuration File]");
System.exit(1);
}
SerialDemo serialDemo = new SerialDemo(args);
serialDemo.setVisible(true);
serialDemo.repaint();
}
/**
Create new <code>SerialDemo</code> and initilizes it. Parses args to
find configuration file. If found, initial state it set to parameters
in configuration file.
@param args command line arguments used when program was invoked.
*/
public SerialDemo(String[] args){
super("Serial Demo");
parameters = new SerialParameters();
// Set up the GUI for the program
addWindowListener(new CloseHandler(this));
mb = new MenuBar();
fileMenu = new Menu("File");
openItem = new MenuItem("Load");
openItem.addActionListener(this);
fileMenu.add(openItem);
saveItem = new MenuItem("Save");
saveItem.addActionListener(this);
fileMenu.add(saveItem);
exitItem = new MenuItem("Exit");
exitItem.addActionListener(this);
fileMenu.add(exitItem);
mb.add(fileMenu);
setMenuBar(mb);
messagePanel = new Panel();
messagePanel.setLayout(new GridLayout(2, 1));
messageAreaOut = new TextArea();
messagePanel.add(messageAreaOut);
messageAreaIn = new TextArea();
messageAreaIn.setEditable(false);
messagePanel.add(messageAreaIn);
add(messagePanel, "Center");
configurationPanel = new ConfigurationPanel(this);
buttonPanel = new Panel();
openButton = new Button("Open Port");
openButton.addActionListener(this);
buttonPanel.add(openButton);
closeButton = new Button("Close Port");
closeButton.addActionListener(this);
closeButton.setEnabled(false);
buttonPanel.add(closeButton);
breakButton = new Button("Send Break");
breakButton.addActionListener(this);
breakButton.setEnabled(false);
buttonPanel.add(breakButton);
Panel southPanel = new Panel();
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints cons = new GridBagConstraints();
southPanel.setLayout(gridBag);
cons.gridwidth = GridBagConstraints.REMAINDER;
gridBag.setConstraints(configurationPanel, cons);
cons.weightx = 1.0;
southPanel.add(configurationPanel);
gridBag.setConstraints(buttonPanel, cons);
southPanel.add(buttonPanel);
add(southPanel, "South");
parseArgs(args);
connection = new SerialConnection(this, parameters,
messageAreaOut, messageAreaIn);
setConfigurationPanel();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(screenSize.width/2 - WIDTH/2,
screenSize.height/2 - HEIGHT/2);
setSize(WIDTH, HEIGHT);
}
/**
Sets the GUI elements on the configurationPanel.
*/
public void setConfigurationPanel() {
configurationPanel.setConfigurationPanel();
}
/**
Responds to the menu items and buttons.
*/
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
// Loads a configuration file.
if (cmd.equals("Load")) {
if (connection.isOpen()) {
AlertDialog ad = new AlertDialog(this, "Port Open!",
"Configuration may not",
"be loaded",
"while a port is open.");
} else {
FileDialog fd = new FileDialog(this,
"Load Port Configuration",
FileDialog.LOAD);
fd.setVisible(true);
String file = fd.getFile();
if (file != null) {
String dir = fd.getDirectory();
File f = new File(dir + file);
try {
FileInputStream fis = new FileInputStream(f);
props = new Properties();
props.load(fis);
fis.close();
} catch (FileNotFoundException e1) {
System.err.println(e1);
} catch (IOException e2) {
System.err.println(e2);
}
loadParams();
}
}
}
// Saves a configuration file.
if (cmd.equals("Save")) {
configurationPanel.setParameters();
FileDialog fd = new FileDialog(this, "Save Port Configuration",
FileDialog.SAVE);
fd.setFile("serialdemo.properties");
fd.setVisible(true);
String fileName = fd.getFile();
String directory = fd.getDirectory();
if ((fileName != null) && (directory != null)) {
writeFile(directory + fileName);
}
}
// Calls shutdown, which exits the program.
if (cmd.equals("Exit")) {
shutdown();
}
// Opens a port.
if (cmd.equals("Open Port")) {
openButton.setEnabled(false);
Cursor previousCursor = getCursor();
setNewCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
configurationPanel.setParameters();
try {
connection.openConnection();
} catch (SerialConnectionException e2) {
AlertDialog ad = new AlertDialog(this,
"Error Opening Port!",
"Error opening port,",
e2.getMessage() + ".",
"Select new settings, try again.");
openButton.setEnabled(true);
setNewCursor(previousCursor);
return;
}
portOpened();
setNewCursor(previousCursor);
}
// Closes a port.
if (cmd.equals("Close Port")) {
portClosed();
}
// Sends a break signal to the port.
if (cmd.equals("Send Break")) {
connection.sendBreak();
}
}
/**
Toggles the buttons to an open port state.
*/
public void portOpened() {
openButton.setEnabled(false);
closeButton.setEnabled(true);
breakButton.setEnabled(true);
}
/**
Calls closeConnection on the SerialConnection and toggles the buttons
to a closed port state.
*/
public void portClosed() {
connection.closeConnection();
openButton.setEnabled(true);
closeButton.setEnabled(false);
breakButton.setEnabled(false);
}
/**
Sets the <code>Cursor</code> for the application.
@param c New <code>Cursor</code>
*/
private void setNewCursor(Cursor c) {
setCursor(c);
messageAreaIn.setCursor(c);
messageAreaOut.setCursor(c);
}
/**
Writes the current parameters to a configuration file of the
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -