public class Main extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
protected static final Object String = null;
String imagePath = "Images/";
JLabel timeLabel = new JLabel("00:00");
JButton testButton;
TextArea textArea;
String url;
String programname = "Programmname";
Config config;
/* JMenuBar menuBar;
JMenu fileMenu, optionsMenu, helpMenu;
JMenuItem fileOpenMenuItem, fileSaveMenuItem, fileExitMenuItem,
optionsSettingsMenuItem, helpAboutMenuItem;*/
JTextField leadTextField, sendTextField, trailTextField;
static JFrame frame;
JTextArea logTextArea, sendTextArea, recTextArea;
DatagramClient udpClient;
JTextField localHostTextField, localPortTextField;
JTextField connectIPTextField, connectPortTextField;
JButton connectButton;
JButton disconnectButton;
InetAddress connectAddress;
int connectPort;
public static void main(String[] args) {
frame = new JFrame(); // Frame = Window
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(1199, 0, 720, 1100);
frame.add(new Main());
frame.setVisible(true);
}
public Main() {
frame.setTitle(programname);
try {
/* // Menubar
menuBar = new JMenuBar();
//frame.setJMenuBar(menuBar);
// Hauptmenus
fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
optionsMenu = new JMenu("Options");
optionsMenu.setMnemonic(KeyEvent.VK_O);
menuBar.add(optionsMenu);
helpMenu = new JMenu("Help");
helpMenu.setMnemonic(KeyEvent.VK_H);
menuBar.add(helpMenu);
// Untermenus
fileOpenMenuItem = new JMenuItem("Open", KeyEvent.VK_O);
fileOpenMenuItem.setToolTipText("Opens a configuration from a file");
fileOpenMenuItem.addActionListener(this);
fileMenu.add(fileOpenMenuItem);
fileSaveMenuItem = new JMenuItem("Save", KeyEvent.VK_S);
fileSaveMenuItem.setToolTipText("Saves current configuration to a file");
fileSaveMenuItem.addActionListener(this);
fileMenu.add(fileSaveMenuItem);
fileExitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X);
fileExitMenuItem.setToolTipText("Closes the program");
fileExitMenuItem.addActionListener(this);
fileMenu.add(fileExitMenuItem);
optionsSettingsMenuItem = new JMenuItem("Connection settings", KeyEvent.VK_C);
optionsSettingsMenuItem.setToolTipText("Change connection settings");
optionsSettingsMenuItem.addActionListener(this);
optionsMenu.add(optionsSettingsMenuItem);
helpAboutMenuItem = new JMenuItem("About", KeyEvent.VK_O);
helpAboutMenuItem.setToolTipText("About this program");
helpAboutMenuItem.addActionListener(this);
helpMenu.add(helpAboutMenuItem); */
config = new Config(frame, "UDPClient.cfg");
// Layout
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {}
setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
topPanel.setBorder(new BevelBorder(0));
add(topPanel, BorderLayout.NORTH);
topPanel.setLayout(null);
topPanel.setPreferredSize(new Dimension(0, 260));
// local host IP and port numbers
JLabel ownIPTextLabel = new JLabel("Own IP:");
ownIPTextLabel.setBounds(10, 15, 90, 20);
topPanel.add(ownIPTextLabel);
localHostTextField = new JTextField(config.getProperty("localHost"));
localHostTextField.setBounds(90, 15, 100, 22);
topPanel.add(localHostTextField);
JLabel ownPortTextLabel = new JLabel("Own Port:");
ownPortTextLabel.setBounds(210, 15, 90, 20);
topPanel.add(ownPortTextLabel);
localPortTextField = new JTextField(config.getProperty("localPort"));
localPortTextField.setBounds(300, 15, 50, 22);
topPanel.add(localPortTextField);
// connecting host IP and port numbers
JLabel hostIPTextLabel = new JLabel("Connect to IP:");
hostIPTextLabel.setBounds(10, 40, 90, 20);
topPanel.add(hostIPTextLabel);
connectIPTextField = new JTextField(config.getProperty("connectIp"));
connectIPTextField.setBounds(90, 40, 100, 22);
topPanel.add(connectIPTextField);
JLabel hostPortTextLabel = new JLabel("Connect to Port:");
hostPortTextLabel.setBounds(210, 40, 90, 20);
topPanel.add(hostPortTextLabel);
connectPortTextField = new JTextField(config.getProperty("connectHost"));
connectPortTextField.setBounds(300, 40, 50, 22);
topPanel.add(connectPortTextField);
connectButton = new JButton("Connect");
connectButton.setBounds(380, 15, 90, 22);
connectButton.addActionListener(this);
topPanel.add(connectButton);
disconnectButton = new JButton("Disconnect");
disconnectButton.setBounds(380, 40, 90, 22);
disconnectButton.addActionListener(this);
topPanel.add(disconnectButton);
disconnectButton.setEnabled(false);
disconnectButton = new JButton("Save configuration");
disconnectButton.setBounds(500, 30, 130, 22);
disconnectButton.addActionListener(this);
topPanel.add(disconnectButton);
JLabel inputFormatTextLabel = new JLabel("Input format: Hex");
inputFormatTextLabel.setBounds(10, 80, 90, 20);
topPanel.add(inputFormatTextLabel);
JLabel leadTextLabel = new JLabel("Lead. Data:");
leadTextLabel.setBounds(10, 105, 60, 20);
topPanel.add(leadTextLabel);
leadTextField = new JTextField(config.getProperty("leadingData"));
leadTextField.setBounds(70, 105, 50, 22);
topPanel.add(leadTextField);
JLabel sendTextLabel = new JLabel("Send Data:");
sendTextLabel.setBounds(130, 105, 60, 20);
topPanel.add(sendTextLabel);
sendTextField = new JTextField();
sendTextField.setBounds(190, 105, 250, 22);
topPanel.add(sendTextField);
JLabel trailTextLabel = new JLabel("Trail. Data:");
trailTextLabel.setBounds(455, 105, 60, 20);
topPanel.add(trailTextLabel);
trailTextField = new JTextField(config.getProperty("trailingData"));
trailTextField.setBounds(515, 105, 50, 22);
topPanel.add(trailTextField);
JButton sendButton = new JButton("Send");
sendButton.setBounds(575, 105, 60, 22);
topPanel.add(sendButton);
sendButton.addActionListener(this);
logTextArea = new JTextArea();
logTextArea.setAutoscrolls(true);
ScrollPane logTextAreaScrollPane = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
logTextAreaScrollPane.add(logTextArea);
logTextAreaScrollPane.getVAdjustable().setUnitIncrement(16);
logTextAreaScrollPane.setBounds(10, 140, 626, 100);
topPanel.add(logTextAreaScrollPane);
testButton = new JButton("Next");
testButton.addActionListener(this);
topPanel.add(testButton);
JPanel inOutPanel = new JPanel();
inOutPanel.setLayout(new GridLayout(2, 1));
add(inOutPanel, BorderLayout.CENTER);
sendTextArea = new JTextArea();
sendTextArea.setAutoscrolls(true);
ScrollPane sendTextAreaScrollPane = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
sendTextAreaScrollPane.add(sendTextArea);
sendTextAreaScrollPane.getVAdjustable().setUnitIncrement(16);
inOutPanel.add(sendTextAreaScrollPane);
recTextArea = new JTextArea();
recTextArea.setAutoscrolls(true);
ScrollPane recTextAreaScrollPane = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
recTextAreaScrollPane.add(recTextArea);
recTextAreaScrollPane.getVAdjustable().setUnitIncrement(16);
inOutPanel.add(recTextAreaScrollPane);
textArea = new TextArea();
new Thread(new TimeThread()).start();
new Thread(new RecieceThread()).start();
/* if (udpClient.isClosed())
openConnection();*/
} catch (RuntimeException runtimeExeption) {
runtimeExeption.printStackTrace();
JOptionPane.showMessageDialog(this.getRootPane(), runtimeExeption.getMessage());
}
}
void showRecievedValues(byte[] rec) {
for (int i=0; i<rec.length; i++) {
recTextArea.append(Byte.toString(rec[i]) + " ");
}
}
public int placeWidthControlledJLabel(JPanel panel, JLabel c, int x, int y, int height) {
/* panel.add(c);
FontMetrics fm = c.getw
fm.g
c.setBounds(x, y, width, height);*/
return 0;
}
public void actionPerformed(ActionEvent event) {
String cmd = event.getActionCommand();
if (cmd.equals("Next")) {
logTextArea.append("World" + "\n");
logTextArea.repaint();
}
// Menu-Teil
if (cmd.equals("Open")) {
}
if (cmd.equals("Open")) {
}
if (cmd.equals("Exit")) {
System.exit(0);
}
if (cmd.equals("Connect")) {
try {
// openConnection();
disconnectButton.setEnabled(true);
connectButton.setEnabled(false);
repaint();
} catch (Exception e) {
JOptionPane.showMessageDialog(frame,
"Error on opening Connection: Try annother host IP address",
"Error", JOptionPane.WARNING_MESSAGE);
}
}
if (cmd.equals("Disconnect")) {
try {
closeConnection();
connectButton.setEnabled(true);
disconnectButton.setEnabled(false);
} catch (Exception e) {
logTextArea.append("Error on closing Connection manually");
}
}
if (cmd.equals("Save configuration")) {
config.setProperty("localHost", localHostTextField.getText());
config.setProperty("localPort", localPortTextField.getText());
config.setProperty("connectIp", connectIPTextField.getText());
config.setProperty("connectHost", connectPortTextField.getText());
config.save();
}
if (cmd.equals("Connection settings")) {
}
if (cmd.equals("About")) {}
if (cmd.equals("Send")) {
String[] splittArray = (leadTextField.getText() + " " +
sendTextField.getText() + " " +
trailTextField.getText()).split(" ");
// remove empty strings
ArrayList<String> dataString = new ArrayList<String>();
for (int i=0; i<splittArray.length; i++)
if (splittArray[i].length() != 0) dataString.add(splittArray[i]); // copy just strings greater than 0
byte[] data = new byte[dataString.size()];
for (int i=0; i<dataString.size(); i++) {
// break on wrong format
if (dataString.get(i).length() != 2) {
JOptionPane.showMessageDialog(frame,
"Format error. Hex values has to written with 2 chars with Spaces bethween",
"Error", JOptionPane.WARNING_MESSAGE);
return;
}
data[i] = (byte)Integer.parseInt(dataString.get(i), 16);
}
try {
InetAddress host = InetAddress.getByName(connectIPTextField.getText()) ;
int port = Integer.parseInt(connectPortTextField.getText());
udpClient.sendto(data, host, port);
for (int i=0; i<data.length; i++) {
Integer.toHexString(data[0]);
}
sendTextArea.append(bytesToHexString(data, ' ') + "\n");
} catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Error on sending on host IP and port number",
"Error", JOptionPane.WARNING_MESSAGE);
}
}
}
public void updateView() {
logTextArea.update(this.getGraphics());
System.out.println((char) 7);
}
class TimeThread implements Runnable {
@Override public void run() {
DateFormat parseDateFormatter = new SimpleDateFormat("HH:mm:ss");
while (true) {
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// readTimer.schedule(new TimerTask(fetchMail)) {
// config.saveLastParseTime(webData.readStationValues());
Date time = new Date();
timeLabel.setText(parseDateFormatter.format(time));
}
}
}
public String bytesToHexString(byte[] b, char delimiter) {
String ss = Integer.toHexString(0x00 | (b[0] & 0x000000FF));
if (ss.length() == 1) ss = "0" + ss;
String s = ss;
for (int i=1; i<b.length; i++) {
ss = Integer.toHexString(0x00 | (b[i] & 0x000000FF));
if (ss.length() == 1) ss = "0" + ss;
s = s + delimiter + ss;
}
return s;
}
public void closeConnection() {
if (udpClient != null) {
try {
if (!udpClient.isClosed())
udpClient.close();
udpClient = null;
} catch (Throwable e) {
JOptionPane.showMessageDialog(frame, "Error on closing connection",
"Error", JOptionPane.WARNING_MESSAGE);
udpClient = null;
}
}
}
public void openConnection() throws Exception {
/* try {*/
InetAddress onwnAddress = InetAddress.getByName(localHostTextField.getText());
int localPort = Integer.parseInt(localPortTextField.getText());
connectAddress = InetAddress.getByName(connectIPTextField.getText());
connectPort = Integer.parseInt(connectPortTextField.getText());
udpClient = new DatagramClient(localPort, onwnAddress, logTextArea);
// if (udpClient.isClosed()) throw new Exception("Error on opening Client.");
/* } catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Error on opening with this inputs of local IP and port numbers",
"Error", JOptionPane.WARNING_MESSAGE);
}*/
}
class RecieceThread implements Runnable {
@Override public void run() {
while (true) {
synchronized(this) {
if ((udpClient != null) && (!udpClient.isClosed())) {
try {
byte[] rec = udpClient.recieveFrom(connectAddress, connectPort);
System.out.println(rec.length);
String s = bytesToHexString(rec, ' ');
recTextArea.append(s + "\n");
} catch( Exception e ) {
System.out.println("ReceiveThread Error: " + e);
}
}
}
}
}
}
}