view src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncRootSelectionPanel.java @ 399:28d67bbfa9f1

Implement SelectionPanel processing
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Tue, 13 Oct 2015 05:05:04 +0900
parents b3ef6d14b2b7
children 845698fd6fb6
line wrap: on
line source

package jp.ac.u_ryukyu.treevnc;

import javax.swing.*;

import com.glavsoft.viewer.swing.ConnectionParams;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

public class TreeVncRootSelectionPanel extends JFrame implements ActionListener,
		ItemListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
    private final FindRoot findRoot;
    private JPanel panel = new JPanel();
	private JButton button = new JButton("Connect");
    private JButton startRootButton = new JButton("Start as TreeVNC Root");
    private JButton startDisplayButton = new JButton("Start Display Mode");
    private TextField t1;
	private TextField t2;
	private double width = 750;
	private double height = 500;
	private JLabel label;
	private boolean flag;
	private int counter = 0;
	// private JCheckBox[] check = new JCheckBox[20];
	private Checkbox[] check = new Checkbox[20];
	private String port;
	private CheckboxGroup ch = new CheckboxGroup();
	private Container contentPane = getContentPane();
    private CreateConnectionParam cp;

    public TreeVncRootSelectionPanel(final FindRoot findRoot) {
        this.findRoot = findRoot;
        setTitle("TreeVNC Root Address");
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.add(startRootButton);
        panel.add(startDisplayButton);
        startRootButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				cp.setRootMode();
				cp.restart();
				unVisible();
			}
		});

		startDisplayButton.addActionListener(new ActionListener() {
            @Override
            public synchronized void actionPerformed(ActionEvent e) {
				cp.setDisplayMode();
				findRoot.findRoot();
			}
        });
		contentPane.add(panel, BorderLayout.CENTER);
	}

    public void ipRegister() {
		setSize();
		setText();
		setButton();
		visible();
	}

	private void setSize() {
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		width = (d.getWidth() / 2);
		height = (d.getHeight() / 2);
	}

	public void visible() {
		Point point = new Point();
		point.setLocation(width - 250, height - 80);
		setLocation(point.getLocation());
		pack();
		setVisible(true);
	}

	public void unVisible() {
		setVisible(false);
	}

	private  void setText() {
		t1 = new TextField("Address", 30);
		t2 = new TextField(Integer.toString(ConnectionParams.DEFAULT_VNC_ROOT), 5);
		panel.add(t1);
		panel.add(t2);
		//panel.add(button);
		//button.addActionListener(this);
		label = new JLabel();
		contentPane.add(panel, BorderLayout.CENTER);
		contentPane.add(label, BorderLayout.SOUTH);
	}

	public void checkBox(String str) {
		if (counter == 0)
			check[counter] = new Checkbox(str, true, ch);
		else
			check[counter] = new Checkbox(str, false, ch);
		check[counter].addItemListener(this);
		panel.add(check[counter]);
		panel.setLayout(new GridLayout(counter + 2, 0));
		panel.setLocation((int) width - 250, (int) height - 80);
		counter++;
	}

	public void setButton() {
		panel.add(button);
		button.addActionListener(this);
		contentPane.add(panel, BorderLayout.CENTER);
	}

	public String getAddressOption() {
		while (!(flag)) {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return t1.getText();
	}

	public String getPortOption() {
		return t2.getText();
	}

	public String getPort() {
		return port;
	}

	public void actionPerformed(ActionEvent e) {
		flag = true;
		for (int t = 0; t < counter; t++) {
            int ipv4AddressLength = 3;
            int ipv6AddressLength = 17;
            String port = null;
            String hostAddress = null;
            String rootAddress = null;
 			if (check[t].getState()) {
				System.out.println(check[t].getLabel());
				String str = check[t].getLabel();
		        String[] temp = str.split(":");
                if (temp.length == ipv4AddressLength) {
                    // IPv4 Address
                    port = temp[0];
                    hostAddress = temp[1];
                    rootAddress = temp[2];
                } else if (temp.length == ipv6AddressLength) {
                    // IPv6 Address
                    port = temp[0];
                    hostAddress = temp[1] + ":" + temp[2] + ":" + temp[3] + ":" + temp[4] + ":" + temp[5] + ":" + temp[6] + ":" + temp[7] + ":" + temp[8];
                    rootAddress = temp[9] + ":" + temp[10] + ":" + temp[11] + ":" + temp[12] + ":" + temp[13] + ":" + temp[14] + ":" + temp[15] + ":" + temp[16];
                }
				cp.setHostName(hostAddress,Integer.parseInt(port),rootAddress);
                unVisible();
			}
		}
	}


	public void itemStateChanged(ItemEvent e) {
	}

    public void setCp(CreateConnectionParam cp) {
        this.cp = cp;
    }
}