Java tutorial
/*********************************************************** * Portfinder * Copyright 2010-2014 Christian Parkinson * Licensed under the GNU GPL. See COPYING for full terms. ***********************************************************/ package ca.parkie.portfinder.client; import ca.parkie.portfinder.MacAddress; import ca.parkie.portfinder.Location; import com.jgoodies.forms.layout.CellConstraints; import com.jgoodies.forms.layout.FormLayout; import javax.swing.*; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.HashSet; import java.util.Set; public class MainWindow { private JPanel contentPane; private JTextField roomTextField; private JButton sendButton; private JLabel interfaceLabel; private JLabel linkStatusLabel; private JLabel encodedMacLabel; private JTextField portNumberTextField; private static final Color RED = new Color(204, 0, 0); private static final Color GREEN = new Color(0, 204, 0); private Client client; private Timer pollTimer; private MacAddress macAddress; private boolean linkUp; private Set<Location> sentLocations; private Location currentLocation; public MainWindow(Client client) { interfaceLabel.setText(client.getNetworkInterface()); this.client = client; NumberListener nl = new NumberListener(); roomTextField.addKeyListener(nl); portNumberTextField.addKeyListener(nl); DocumentListener dl = new TextFieldUpdateListener(); roomTextField.getDocument().addDocumentListener(dl); portNumberTextField.getDocument().addDocumentListener(dl); sentLocations = new HashSet<Location>(); sendButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { sendFrame(); } }); } public JPanel getContentPane() { return contentPane; } public void startInterfacePoll() { if (pollTimer != null && pollTimer.isRunning()) pollTimer.stop(); pollTimer = new Timer(200, new ActionListener() { public void actionPerformed(ActionEvent e) { try { setLinkUp(client.poll()); } catch (EthernetException e1) { JOptionPane.showMessageDialog(contentPane, "Could not poll ethernet interface.", "Network Interface Error", JOptionPane.ERROR_MESSAGE); linkStatusLabel.setText("Interface Error"); linkStatusLabel.setForeground(RED); pollTimer.stop(); } } }); pollTimer.start(); } public void stopInterfacePoll() { pollTimer.stop(); } private void sendFrame() { if (macAddress == null || !linkUp) return; boolean success; try { success = client.generateFrame(macAddress); } catch (EthernetException e) { System.out.println(e.getMessage()); success = false; } if (success) { sentLocations.add(currentLocation); String text = encodedMacLabel.getText(); encodedMacLabel.setText(text + " sent."); updateSendButtonStatus(); } else { JOptionPane.showMessageDialog(contentPane, "There was an error generating the layer 2 frame.", "Frame Generation Error", JOptionPane.ERROR_MESSAGE); } } private void setLinkUp(boolean up) { if (up) { linkStatusLabel.setText("UP"); linkStatusLabel.setForeground(GREEN); } else { linkStatusLabel.setText("DOWN"); linkStatusLabel.setForeground(RED); } linkUp = up; updateSendButtonStatus(); } private void buildMacAddress() { try { int room = Integer.valueOf(roomTextField.getText()); int port = Integer.valueOf(portNumberTextField.getText()); currentLocation = client.getLocation(room, port); MacAddress mac = client.getMacAddress(currentLocation); setMacAddress(mac); } catch (Exception e) { setMacAddress(null); } } private void setMacAddress(MacAddress mac) { this.macAddress = mac; if (mac == null) encodedMacLabel.setText("Invalid"); else { StringBuilder sb = new StringBuilder(mac.toString()); if (sentLocations.contains(currentLocation)) sb.append(" (already sent)"); encodedMacLabel.setText(sb.toString()); } updateSendButtonStatus(); } private void updateSendButtonStatus() { sendButton.setEnabled(macAddress != null && linkUp && !sentLocations.contains(currentLocation)); } { // GUI initializer generated by IntelliJ IDEA GUI Designer // >>> IMPORTANT!! <<< // DO NOT EDIT OR ADD ANY CODE HERE! $$$setupUI$$$(); } /** * Method generated by IntelliJ IDEA GUI Designer * >>> IMPORTANT!! <<< * DO NOT edit this method OR call it in your code! * * @noinspection ALL */ private void $$$setupUI$$$() { contentPane = new JPanel(); contentPane.setLayout(new FormLayout("fill:d:grow", "center:d:grow")); final JPanel panel1 = new JPanel(); panel1.setLayout(new FormLayout( "fill:d:noGrow,left:4dlu:noGrow,fill:100px:grow,left:4dlu:noGrow,fill:max(d;4px):noGrow,left:4dlu:noGrow,fill:100px:grow", "center:30px:noGrow,top:3dlu:noGrow,center:30px:noGrow,top:3dlu:noGrow,center:30px:noGrow,top:3dlu:noGrow,center:30px:noGrow")); panel1.setPreferredSize(new Dimension(520, 160)); CellConstraints cc = new CellConstraints(); contentPane.add(panel1, new CellConstraints(1, 1, 1, 1, CellConstraints.DEFAULT, CellConstraints.FILL, new Insets(11, 11, 11, 11))); final JLabel label1 = new JLabel(); label1.setFont(new Font(label1.getFont().getName(), label1.getFont().getStyle(), 14)); label1.setText("Room:"); panel1.add(label1, cc.xy(1, 1)); roomTextField = new JTextField(); roomTextField.setFont(new Font(roomTextField.getFont().getName(), roomTextField.getFont().getStyle(), 14)); panel1.add(roomTextField, cc.xy(3, 1, CellConstraints.FILL, CellConstraints.CENTER)); sendButton = new JButton(); sendButton.setEnabled(false); sendButton.setFont(new Font(sendButton.getFont().getName(), sendButton.getFont().getStyle(), 14)); sendButton.setText("Send"); panel1.add(sendButton, cc.xyw(1, 7, 7, CellConstraints.DEFAULT, CellConstraints.FILL)); final JLabel label2 = new JLabel(); label2.setFont(new Font(label2.getFont().getName(), label2.getFont().getStyle(), 14)); label2.setText("Port Number:"); panel1.add(label2, cc.xy(5, 1)); final JLabel label3 = new JLabel(); label3.setFont(new Font(label3.getFont().getName(), label3.getFont().getStyle(), 14)); label3.setText("Interface:"); panel1.add(label3, cc.xy(1, 3)); encodedMacLabel = new JLabel(); encodedMacLabel .setFont(new Font(encodedMacLabel.getFont().getName(), encodedMacLabel.getFont().getStyle(), 14)); encodedMacLabel.setText("Invalid"); panel1.add(encodedMacLabel, cc.xyw(3, 5, 5, CellConstraints.LEFT, CellConstraints.DEFAULT)); interfaceLabel = new JLabel(); interfaceLabel .setFont(new Font(interfaceLabel.getFont().getName(), interfaceLabel.getFont().getStyle(), 14)); interfaceLabel.setText("Unknown"); panel1.add(interfaceLabel, cc.xy(3, 3, CellConstraints.LEFT, CellConstraints.DEFAULT)); final JLabel label4 = new JLabel(); label4.setFont(new Font(label4.getFont().getName(), label4.getFont().getStyle(), 14)); label4.setText("Encoded MAC:"); panel1.add(label4, cc.xy(1, 5)); final JLabel label5 = new JLabel(); label5.setFont(new Font(label5.getFont().getName(), label5.getFont().getStyle(), 14)); label5.setText("Link Status:"); panel1.add(label5, cc.xy(5, 3)); linkStatusLabel = new JLabel(); linkStatusLabel .setFont(new Font(linkStatusLabel.getFont().getName(), linkStatusLabel.getFont().getStyle(), 14)); linkStatusLabel.setForeground(new Color(-3407872)); linkStatusLabel.setText("Unknown"); panel1.add(linkStatusLabel, cc.xy(7, 3, CellConstraints.LEFT, CellConstraints.DEFAULT)); portNumberTextField = new JTextField(); panel1.add(portNumberTextField, cc.xy(7, 1, CellConstraints.FILL, CellConstraints.DEFAULT)); label1.setLabelFor(roomTextField); label2.setLabelFor(portNumberTextField); } /** * @noinspection ALL */ public JComponent $$$getRootComponent$$$() { return contentPane; } private class NumberListener extends KeyAdapter { @Override public void keyTyped(KeyEvent e) { char key = e.getKeyChar(); if (!Character.isDigit(key)) e.consume(); } } private class TextFieldUpdateListener implements DocumentListener { public void insertUpdate(DocumentEvent e) { buildMacAddress(); } public void removeUpdate(DocumentEvent e) { buildMacAddress(); } public void changedUpdate(DocumentEvent e) { buildMacAddress(); } } }