view src/main/java/jp/ac/u_ryukyu/treevnc/client/TreeVncRootSelectionPanel.java @ 145:649794dfb9d5

add my hostname to handle multiple network
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 12 Jun 2014 22:01:05 +0900
parents 1e004d9c599d
children 93d903877c6b
line wrap: on
line source

package jp.ac.u_ryukyu.treevnc.client;

import javax.swing.*;

import com.glavsoft.viewer.swing.ConnectionParams;

import java.awt.*;
import java.awt.event.*;
import jp.ac.u_ryukyu.treevnc.CreateConnectionParam;

public class TreeVncRootSelectionPanel extends JFrame implements ActionListener,
		ItemListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel panel = new JPanel();
	private JButton button = new JButton("Connect");
	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 hostAddress;
	private String port;
	private CheckboxGroup ch = new CheckboxGroup();
	private Container contentPane = getContentPane();
    private CreateConnectionParam cp;

	public TreeVncRootSelectionPanel() {
		setTitle("TreeVNC Root Address");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	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++) {
			if (check[t].getState()) {
				System.out.println(check[t].getLabel());
				String str = check[t].getLabel();
		        String[] temp = str.split(":");
		        String port = temp[0];
		        String hostAddress = temp[1];
		        String rootAddress = temp[2];
				cp.setHostName(hostAddress,Integer.parseInt(port),rootAddress);
                unVisible();
			}
		}
	}


	public void itemStateChanged(ItemEvent e) {
	}

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