view src/myVncClient/TextBox.java @ 9:56251926d766

update
author one
date Tue, 20 Dec 2011 18:09:34 +0900
parents 8f9bf4708785
children
line wrap: on
line source

package myVncClient;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class TextBox extends JFrame implements ActionListener, ItemListener {

	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;
	ArrayList<String> temp = new ArrayList<String>();
	private int counter = 0;
	//private JCheckBox[] check = new JCheckBox[20];
	private Checkbox[] check = new Checkbox[20];
	private boolean firstFlag = true;
	private String hostAddress;
	private String port;
	private CheckboxGroup ch = new CheckboxGroup();

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

	public TextBox() {
		setTitle("Informatin Connection Address");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

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

	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("5999", 5);
		panel.add(t1);
		panel.add(t2);
		panel.add(button);
		button.addActionListener(this);
		label = new JLabel();
		Container contentPane = getContentPane();
		contentPane.add(panel, BorderLayout.CENTER);
		contentPane.add(label, BorderLayout.SOUTH);
	}

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

	void setButton() {
		panel.add(button);
		//panel.setBackground(Color.blue);
		button.addActionListener(this);
		Container contentPane = getContentPane();
		contentPane.add(panel, BorderLayout.CENTER);
	}


	private void reportWindow() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container pane = getContentPane();
		JLabel label = new JLabel("Not found Proxy. If you use Client-mode you must set-up Proxy");
		label.setPreferredSize(new Dimension(580,50));
		label.setFont(new Font("Arial", Font.PLAIN, 20));
		pane.add(label);
	}

	public String getAddress() {
		int i = 0;
		while (!(flag)) {
			if (i >= 20) {
				reportWindow();
				visible();
				break;
			}
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			i++;
		}
		return hostAddress;
		// return t1.getText();
	}

	public String getPort() {
		return port;
		// return t2.getText();
	}

	public void actionPerformed(ActionEvent e) {
		flag = true;
		for (int t = 0; t < counter; t++) {
			if (check[t].getState()) {
				System.out.println(check[t].getLabel());
				setStatus(check[t].getLabel());
			}
		}
	}
	

	private void setStatus(String str) {
		String[] temp = str.split(":");
		port = temp[0];
		System.out.println("port=" + port);
		hostAddress = temp[3];
	}
	
	
	String splitString(String str) {
		String[] split = str.split("\\*");
		String comper;
		if (split.length > 4) {
			split[4] = null;
		}
		comper = split[1] + split[3];
		if (firstFlag) {
			temp.add(comper);
			firstFlag = false;
			return "port:" + split[0] + ":host:" + split[1] + ":proxy:"	+ split[3];
		}
		for (int t = 0; t < temp.size(); t++) {
			if (!(comper.equals(temp.get(t)))) {
				if (t == temp.size() - 1) {
					temp.add(comper);
					return "port:" + split[0] + ":host:" + split[1] + ":proxy:" + split[3];
				}
			} else {
				break;
			}
		}
		return null;
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
	}
}