package aji;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Text;


/*
 * InsertWindow.java レシピ登録画面
 * 作成日: 2005/11/04
 *
 * TODO この生成されたファイルのテンプレートを変更するには次へジャンプ:
 * ウィンドウ - 設定 - Java - コード・スタイル - コード・テンプレート
 */

/**z
 * @author yui
 *
 * TODO この生成された型コメントのテンプレートを変更するには次へジャンプ:
 * ウィンドウ - 設定 - Java - コード・スタイル - コード・テンプレート
 */


public class InsertWindow{
	
		private Composite c1;
		private int a = 4,b = 2;
		private Text r_text,h_text,c_text,t_text,n_text,p_text,f_text,s_text;
		private Button add,del,send,radio1,radio2,radio3;
		private Combo cm1,cm2,cm3;
		private List list1,list2;
		private Label r_label,h_label,c_label,t_label,n_label,p_label,f_label,s_label;
		private String vaildValues;
		
		
		private String Bigitem[] = {"野菜","肉類","魚類","その他"};
		
		private String Miditem[][] = {	{"きゅうり","とまと"},
										{"豚","牛"},
										{"さんま","あじ"},
										{"卵","ちーず"},
										{""}
									};
		
		private String Smlitem[][] = {	{"ちち","しり","ふともも"},
										{"ロース","カルビ","ればー"},
										{""}
									};


		public InsertWindow(){


//####ここのレイアウト
		    c1 = new Composite(BaseWin.shell, SWT.NULL); // shellが親
		    GridData gridDataText = new GridData(GridData.FILL);
		    gridDataText.widthHint = 800; // 横幅を最低300確保
		    c1.setLayoutData(gridDataText);
		    c1.setSize(800,536);
		     
//####コンボ		    		    
		    makeComboBig();
		    makeComboMiddle();
		    makeComboSmall();
//####レシピ名		    
		    makeRecipe();
//####れしぴめい
		    makeReshipi();
//####和洋中
		    makeGroup();
//####カロリー
		    makeCalory();
//####リスト
		    makeList();
//####パスワード
		    makePassword();
//####数量
		    makeNumber();
//####時間
		    makeTime();
//####人数
		    makeParson();
//####手順
		    makeTextField();
//####追加ボタン
		    makeButtonAdd();
//####削除ボタン
		    makeButtonDelete();
//####送信ボタン
		    makeButtonSend();
		    
		    Label bl = new Label(c1,SWT.NULL);
		    bl.setText("*必須");
		    bl.setBounds(750,525,40,20);

		    c1.setVisible(true);
	}

//########METHOD#########
		
//####ジャンル		
		public void makeGroup(){
			
		    //r_label = new Label(c1, SWT.NULL);
		    //r_label.setText("和洋中:");
		    //r_label.setBounds(10,12,50,20);
		    
		    Group grp1 = new Group(c1,SWT.SHADOW_NONE);
		    GridLayout grid = new GridLayout();
		    grid.numColumns = 6;
		    grp1.setLayout(grid);
		    grp1.setText("*料理ジャンル");
		    radio1 = new Button(grp1,SWT.RADIO);
		    Label label1 = new Label(grp1,SWT.NULL);
		    radio2 = new Button(grp1,SWT.RADIO);
		    Label label2 = new Label(grp1,SWT.NULL);
		    radio3 = new Button(grp1,SWT.RADIO);
		    Label label3 = new Label(grp1,SWT.NULL);
		    label1.setText(":和");
		    label2.setText(":洋");
		    label3.setText(":中");
		    grp1.setBounds(400,20,120,40);
		    
		}
		
//####レシピ名		
		public void makeRecipe(){
			
			r_label = new Label(c1,SWT.NULL);
		    r_label.setText("*レシピ名:");
		    r_label.setBounds(10,42,45,20);
		    
		    r_text = new Text(c1,SWT.SINGLE|SWT.BORDER);
		    r_text.setBounds(55,40,300,20);
		    r_text.setFocus();
		    //r_text.setText("レシピ");
			
		}
		
//####れしぴめい		
		public void makeReshipi(){
			
		    h_label = new Label(c1,SWT.NULL);
		    h_label.setText("*ふりがな:");
		    h_label.setBounds(10,22,45,20);
		    
		    h_text = new Text(c1,SWT.SINGLE|SWT.BORDER);
		    h_text.setBounds(55,20,300,20);
		    //h_text.setText("れしぴ");
		    for(char i = 'ぁ';i <= 'ん';i++){
		    	vaildValues += (char)i;
		    }
		    h_text.addListener(SWT.Verify,new NumericCheck(vaildValues));
		    
		    
		}
		
//####大分類		
		public void makeComboBig(){
			
			Label lie = new Label(c1,SWT.NULL);
			lie.setText("*材 料 :");
			lie.setBounds(10,102,40,20);
			
	
			cm1 = new Combo(c1,SWT.DROP_DOWN);
			cm1.setBounds(55,120,150,20);
			cm1.setItems(Bigitem);
		    cm1.addSelectionListener(new SelectionAdapter(){
		    	  public void widgetSelected(SelectionEvent e){
		    	  
		    	  	System.out.println("1 - " + cm1.getText());
		    	  	a = cm1.getSelectionIndex();
		    	  	cm2.setItems(Miditem[a]);
		    	  	cm3.setItems(Smlitem[2]);
		    	  }
				  
		    });
			
		    
		}

//####中分類		
		public void makeComboMiddle(){
			
			cm2 = new Combo(c1,SWT.DROP_DOWN);
			cm2.setBounds(55,140,150,20);
			cm2.setItems(Miditem[a]);
			cm2.addSelectionListener(new SelectionAdapter(){
				    public void widgetSelected(SelectionEvent e){
				    	  
				    	System.out.println("2 - " + cm2.getText());
				    	b = cm2.getSelectionIndex();
				    	//0:豚　1:牛
				    	if(b == 1 && a == 1){
				    		cm3.setItems(Smlitem[1]);
				    	}else if(b == 0 && a == 1){
				    		cm3.setItems(Smlitem[0]);
				    	}else{
				    		cm3.setItems(Smlitem[2]);
				    	}
				    	  	
				    }
			});
			
		}
		
//####小分類		
		public void makeComboSmall(){
			
			cm3 = new Combo(c1,SWT.DROP_DOWN);
			cm3.setBounds(55,160,150,20);
			cm3.setItems(Smlitem[b]);
			cm3.addSelectionListener(new SelectionAdapter(){
				    public void widgetSelected(SelectionEvent e){
				    	if(a == 1 && b == 1){
				    		System.out.println("3 - " + cm3.getText());
				    	}else if(a == 1 && b == 0){
				    		System.out.println("3 - " + cm3.getText());
				    	}else{
				    		e.doit = false;
				    		if(e.doit) System.out.println("3 - " + cm3.getText());
				    	}
				    	  	
				    }
			});
					
			
		}
		
//####数量		
		public void makeNumber(){
			
			s_label = new Label(c1, SWT.NULL);
			s_label.setText("*数量 :");
		    s_label.setBounds(10,192,40,20);
		    
		    s_text = new Text(c1,SWT.SINGLE|SWT.BORDER);
	        s_text.setBounds(55,190,100,20);
	        //s_text.setText("数量");
	        
		}		
		
//####追加ボタン		
		public void makeButtonAdd(){
			add = new Button(c1,SWT.PUSH);
		    add.setBounds(225,150,100,20);
		    add.setText("追加 →");
		    add.addSelectionListener(new SelectionAdapter(){
		    	  public void widgetSelected(SelectionEvent e){
		    	  	if(s_text.getText() != ""){
		    	  		if(cm1.getText() != ""){
		    	  			if(cm2.getText() != ""){
		    	  				if(cm3.getText() != ""){
		    	  					list1.add(cm2.getText() + cm3.getText());
		    	  					list2.add(s_text.getText());
		    	  				}else{
		    	  					list1.add(cm2.getText()); 
		    	  					list2.add(s_text.getText());
		    	  				}
		    	  			
		    	  			}
		    	  		}
		    	  		
		    	  	}
		    	  	
		    	  	for(int i=0;i<list1.getItemCount();i++)
	    	  		System.out.println("###"+list1.getItems()[i]);
		    	  	
		    	  }
		    });
		}
		
//####削除ボタン		
		public void makeButtonDelete(){
			
		    del = new Button(c1,SWT.PUSH);
		    del.setBounds(225,180,100,20);
		    del.setText("← 削除");
		    del.addSelectionListener(new SelectionAdapter(){
		    	  public void widgetSelected(SelectionEvent e){
		    	  	int i[],j[];
		    	  	i = list1.getSelectionIndices();
		    	    list1.remove(i);
		    	    list2.remove(i);
		    	    j = list2.getSelectionIndices();
		    	    list1.remove(j);
		    	    list2.remove(j);
		    	    
		    	  }
		    }); 
		}
		
//####材料と数量を入れるリスト		
		public void makeList(){
			
			Label be = new Label(c1,SWT.NONE);
			be.setText("*材料表:");
			be.setBounds(305,102,40,20);
			
		    list1 = new List(c1,SWT.MULTI|SWT.BORDER|SWT.V_SCROLL);
		    list1.setBounds(350,120,250,100);
		    
		    
		    list2 = new List(c1,SWT.MULTI|SWT.BORDER|SWT.V_SCROLL);
		    list2.setBounds(600,120,100,100);
		    
		}
		
//####時間		
		public void makeTime(){
			
			t_label = new Label(c1, SWT.NULL);
			t_label.setText("時 間 :");
		    t_label.setBounds(10,252,40,20);
		    
		    t_text = new Text(c1,SWT.SIMPLE|SWT.BORDER);
		    //t_text.setText("時間");
		    t_text.addListener(SWT.Verify,new IntegerCheck());
		    t_text.setBounds(55,250,100,20);
			
		}
		
//####カロリー		
		public void makeCalory(){
			
			c_label = new Label(c1, SWT.NULL);
			c_label.setText("カロリー:");
		    c_label.setBounds(10,282,40,20);
		    
		    c_text = new Text(c1,SWT.SIMPLE|SWT.BORDER);
		    //c_text.setText("カロリー");
		    c_text.addListener(SWT.Verify,new IntegerCheck());
		    c_text.setBounds(55,280,100,20);
	        
		}
		
//####人数		
		public void makeParson(){
			
			n_label = new Label(c1, SWT.NULL);
			n_label.setText("人数:");
		    n_label.setBounds(10,312,40,20);
		    
			n_text = new Text(c1,SWT.SINGLE|SWT.BORDER);
			n_text.addListener(SWT.Verify,new IntegerCheck());
			n_text.setBounds(55,310,100,20);
			//n_text.setText("人数");
			
		}
		
//####送信ボタン		
		public void makeButtonSend(){
			send = new Button(c1,SWT.PUSH);
		    send.setBounds(10,500,100,30);
		    send.setText("送信");
		    send.addSelectionListener(new SelectionAdapter(){
		    	  public void widgetSelected(SelectionEvent e){
		    	  	String str[] = new String[6];
		    	  	if(r_text.getText() != "" && h_text.getText() != "" && p_text.getText() != "" && f_text.getText() != "" && list1.getItemCount() > 0){
		    	  		if(c_text.getText() == "") c_text.setText("0");
		    	  		if(t_text.getText() == "") t_text.setText("0");
		    	  		if(n_text.getText() == "") n_text.setText("0");
		    	  		System.out.println(str[0] = r_text.getText());
		    	  		System.out.println(str[1] = h_text.getText());
		    	  		System.out.println(str[2] = c_text.getText());
		    	  		System.out.println(str[3] = t_text.getText());
		    	  		System.out.println(str[4] = n_text.getText());
		    	  		System.out.println(str[5] = p_text.getText());
		    	  		
		    	  		MyPac[] mp = new MyPac[3];
		    	  		SendText[] st = new SendText[3];
		    	  		
		    	  		st[0].setSendText(str);//その他テキスト
						mp[0].setObj(st[0]);
						mp[0].setEndFlag(0);
						mp[0].setFlag(4);
						//
						
						st[1].setSendText(list1.getItems(),list2.getItems());//材料と数量
						mp[1].setObj(st[1]);
						mp[1].setEndFlag(0);
						mp[1].setFlag(4);
						//
						
		    	  		mp[2].setObj(f_text.getText());//手順
						mp[2].setEndFlag(1);
						mp[2].setFlag(4);
						//
		    	  		
		    	  		visiWin();
		    	  		
		    	  	}else{
		    	  		MessageBox mes = new MessageBox(BaseWin.shell,SWT.OK);
		    	  		mes.setText("エラー");
		    	  		mes.setMessage("必須項目をきちんと入力してください");
		    	  		mes.open();
		    	  	}
		    	  }
		    });
		}
		
//####パスワード		
		public void makePassword(){
			
			p_label = new Label(c1, SWT.NULL);
			p_label.setText("*PASS:");
		    p_label.setBounds(690,22,35,20);
		    
			p_text = new Text(c1,SWT.SINGLE|SWT.BORDER);
			p_text.setBounds(730,20,50,20);
			p_text.addListener(SWT.Verify,new IntegerCheck());
			p_text.setEchoChar('*');
		    p_text.setTextLimit(4);
			
		}
		
		
//####手順		
		public void makeTextField(){
			
			f_label = new Label(c1,SWT.NULL);
			f_label.setText("*手順");
			f_label.setBounds(200,300,30,20);
			
			f_text = new Text(c1,SWT.MULTI|SWT.BORDER|SWT.V_SCROLL);
			f_text.setBounds(230,300,500,230);
			
		}
        
//####不可視にする		
		public void visiWin(){
			c1.setVisible(false);
		}
}
