view src/remoteeditor/ui/REPSelectWindow.java @ 204:aaab17635d0c

source code cleanup
author one
date Sat, 18 Dec 2010 17:33:47 +0900
parents 75fc44fda583
children 563057fe244e
line wrap: on
line source

package remoteeditor.ui;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import java.util.StringTokenizer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

import remoteeditor.command.REPCommand;
import rep.REP;
import remoteeditor.network.REPPacketReceive;
import remoteeditor.network.REPPacketSend;

public class REPSelectWindow {
	private Shell shell;
	private Combo combo;
	private Display display;
	private SocketChannel channel;
	private REPPacketReceive repreceive;
	private REPPacketSend repsend;
	private String filename;
	private REPCommand command;
	private int mysid;
	private int myeid;
	private int myseq;
	
	public REPSelectWindow(Display display){
		this.display = display;
		shell = new Shell(display);
		shell.setLayout(new FillLayout());
		shell.setText("test");
	}
	
	public REPSelectWindow(Shell shell2) {
		this.shell = shell2;
		display = shell.getDisplay();
	}

	public void initWindow(){
		combo = new Combo(shell, SWT.READ_ONLY);
		combo.add("null");
		//combo.add("test2");
		//combo.add("test3");
		combo.select(0);
		
		//String text;
		
		combo.addSelectionListener(new SelectionListener() {
			  public void widgetDefaultSelected(SelectionEvent e){
			  }
			  public void widgetSelected(SelectionEvent e){
			    Combo bCombo = (Combo)e.widget;
			    System.out.println(bCombo.getText());
			  }
			});
		
		Button button1 = new Button(shell,SWT.NULL);
	    button1.setText("select");
	    
	    button1.addSelectionListener(new SelectionAdapter(){
	    	public void widgetSelected(SelectionEvent e){
	    		//MessageBox mesBox = new MessageBox(shell);
	    		if(combo.getSelectionIndex() == 0){
	    			put();
	    			select();
	    		}else{
	    			mysid = combo.getSelectionIndex() - 1;
	    			select();
	    		}
	    		//mesBox.setMessage("select : " + combo.getText());
	    		//mesBox.open();
	    		shell.close();
	    	}
	    });
		
	}
	
	public String getSessionList(){
		return null;
	}
	
	public int open(){
		shell.pack();
		shell.open();
		join();
		//put();
		
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();
			}
		}
		//dispose();
		//put();
		//select();
		return 0;
	}

	private void select() {
		repsend.send(new REPCommand(REP.SMCMD_SELECT, mysid, myeid, myseq, 0, 0, ""));
	}

	private void put() {
		repsend.send(new REPCommand(REP.SMCMD_PUT, mysid, myeid, myseq, 0, filename.length(), filename));
		command = repreceive.unpackUConv();
		mysid = command.sid;
	}

	public void setName(String name) {
		this.filename = name;
		combo.add(name, 0);
	}

	public String getSessionName() {
		return combo.getText();
	}

	public void setChannel(SocketChannel sc) {
		this.channel = sc;
		repreceive = new REPPacketReceive(sc);
		repsend = new REPPacketSend(sc);
	}
	public void join(){
		repsend.send(new REPCommand(REP.SMCMD_JOIN, 0, 0, 0, 0, 0, ""));
		command = repreceive.unpackUConv();
		myeid = command.eid;
		//System.out.println(command.toString());
		StringTokenizer tokenizer = new StringTokenizer(command.string, ",{} ");
		while (tokenizer.hasMoreTokens()) {
		    //System.out.println(tokenizer.nextToken());
		    combo.add(tokenizer.nextToken());
		}
	}
}