/*
 * Created on Dec 29, 2003
 *
 */

import java.awt.*;
import java.awt.event.*;
import java.util.Hashtable;

import javax.swing.*;

import LumiSoft.Data.*;
import LumiSoft.UI.Controls.*;
import LumiSoft.UI.Controls.Grid.*;

/**
 * 
 */
public class wctrl_Controls extends JPanel implements IDisposable
{
	private ImageList m_pImageList  = null;
	private Component m_pMenuParent = null;
//	private WDateTime m_pDate       = null; 
	
	WMenuBar menubar = new WMenuBar();
	WTextEdit textEdit = new WTextEdit();
	private WControl p = new WControl();
	private DataSet ds = new DataSet();
	private WGridControl grid = new WGridControl();
	WButtonEdit buttonEdit = new WButtonEdit();
	private WColorPicker m_pColorPicker = new WColorPicker();

	public void Dispose()	
	{
		for(int i=0;i<this.getComponentCount();i++){
			if(this.getComponent(i) instanceof IDisposable){
				((IDisposable)this.getComponent(i)).Dispose();
			}
		}
	}
	
	public wctrl_Controls(ImageList imageList,ImageList imageListSmall)
	{		
		this.setLayout(null);
		this.setSize(100,100);
										
	/*	WButton b = new WButton();
		b.setSize(100,22);
		b.setLocation(200,10);
		b.addClickedListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){				
				p.requestFocus();
				try{Thread.sleep(10000);}catch(Exception x){}
				System.out.println(p.hasFocus());
			}
		});
		
		// JPanel p = new JPanel();
		p.setSize(200,50);
		p.setLocation(0,0);
		p.setLayout(null);
	//	p.setFocusable(true);
		
		WTextEdit t = new WTextEdit();
		t.setSize(100,20);
		t.setLocation(5,5);
		p.add(t);*/
		
		
		m_pImageList = imageListSmall;
		
		WHyperlink hyperlink = new WHyperlink();
		hyperlink.setSize(60,28);
		hyperlink.setLocation(600,50);
		hyperlink.setText("Contact");
		
		WButton button = new WButton();
		button.setText("Button1 dddddddddddd");
		button.setSize(100,28);
		button.setLocation(5,10);
	//	button.setEnabled(false);
		button.addClickedListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
		//		System.out.println(javax.swing.SwingUtilities.getRoot(buttonEdit) instanceof JDialog);			
				ViewStyle.getstaticViewStyle().setButtonColor(Color.RED);
				ViewStyle.getstaticViewStyle().setBarColor(Color.RED);
				
				if(grid.getMainView().getDataSource().getRowFilter().length() == 0){
					grid.getMainView().getDataSource().setRowFilter("KREEDIT LIKE '*81*'");
				}
				else{
					grid.getMainView().getDataSource().setRowFilter("");
				}
				
				buttonEdit.FlashControl();
				
				if(menubar.getEnabled()){
					menubar.setEnabled(false);
				}
				else{
					menubar.setEnabled(true);
				}
			}
		});
		
		
		WButton button2 = new WButton();
		m_pMenuParent = button2;
		button2.setText("Menu test");
		button2.setSize(70,24);
		button2.setLocation(5,70);
		button2.addClickedListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
		//		ShowPopupMenu();
				
				long startTime = System.currentTimeMillis();
				
				try{
			/*		String aaa = "";
					Hashtable<String,String> tbl = new Hashtable<String,String>();
					for(int i=0;i<20;i++){
						tbl.put("aaa" + i,"aa");
					}
					for(int i=0;i<100000;i++){
						String ss = "aaa18";
						aaa = tbl.get(ss.toLowerCase());
					}
					System.out.println(aaa);*/
					
					DataSet ds = new DataSet();
					DataTable dt = ds.getTables().Add("test");
					for(int i=0;i<20;i++){
						dt.getColumns().Add("aaa" + i);						
					}
					
					for(int i=0;i<10000;i++){						
						DataRow dr = dt.NewRow();
						for(int c=0;c<20;c++){
						//	dr.set(c,"a");
							dr.set("aaa" + c,"a");
						}
						dt.getRows().Add(dr);						
					}
				}
				catch(Exception x){
					x.printStackTrace();
				}
					
				System.out.println("Exec time(ms): " + (System.currentTimeMillis() - startTime));
				
		/*		long s = System.currentTimeMillis();
				for(int i=0;i<100;i++){
					LumiSoft.UI.Controls.Paint.CalcTextHeight((Graphics2D)getGraphics(),getFont(),"xgdgd dsdgsdg agggs",150);
				}
				System.out.println((double)(System.currentTimeMillis() - s) / 1000);*/
			}
		});
				
		WTabBar tabBar = new WTabBar();
		tabBar.setSize(250,24);
		tabBar.setLocation(5,100);
		tabBar.setImageList(imageListSmall);
		tabBar.getTabs().Add("Tab1",null,0);
		tabBar.getTabs().Add("Tab2 dggg agsg",null,0);
		WTabBarItem t = tabBar.getTabs().Add("Tab3 fjgähäöte eätöe",null,0);
		tabBar.getTabs().Add("Tab4 disabled",null,0).setEnabled(false);
	//	tabBar.setSelectedTab(t);
		
		WTabControl tab = new WTabControl();
		tab.setSize(250,100);
		tab.setLocation(5,150);
		tab.getTabPages().Add("Tab1",new JButton("tab page 1"));
		tab.getTabPages().Add("Tab2",new JButton("tab page 2"));
		
		WLabel label = new WLabel();
		label.setSize(145,40);
		label.setLocation(5,240);
	//	label.setAnchorStyle(AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
		label.setTextAlign(HorizontalAlignment.Right);
		label.setText("Funktsionaalne grupeering");
		
		WSpinEdit_base spinedit = new WSpinEdit_base();
		spinedit.setSize(50,22);
		spinedit.setLocation(100,50);
				
		WComboBox combobox = new WComboBox();
		combobox.setSize(80,22);
		combobox.setLocation(130,15);
		combobox.getItems().Add("Item 1");
		combobox.getItems().Add("Item 2");
		combobox.getItems().Add("Item 3");
		combobox.getItems().Add("Item 4");
		combobox.getItems().Add("Item 5");
		combobox.getItems().Add("Item 6");
		combobox.getItems().Add("Item 7");
		combobox.setSelectedIndex(0);
		
		
		buttonEdit.setSize(80,22);
		buttonEdit.setLocation(220,15);
	//	buttonEdit.setEnabled(false);
		buttonEdit.setButtonImage(imageListSmall.get(0));
	//	buttonEdit.setSelectable(false);
	//	buttonEdit.setEditable(false);
		buttonEdit.addButtonClickedListener(new  ActionListener(){
			public void actionPerformed(ActionEvent e){
				ShowPopupMenu();
			}
		});
		
		WDateTime dateTime = new WDateTime();
	//	m_pDate = dateTime;
		dateTime.setSize(85,22);
		dateTime.setLocation(160,50);
		
		WNumericEdit numericedit = new WNumericEdit();
		numericedit.setSize(80,22);
		numericedit.setLocation(300,50);	
		numericedit.setSpinnersVisible(false);
		numericedit.setReadOnly(true);
		
		WCheckBox checkbox = new WCheckBox();
		checkbox.setSize(20,18);
		checkbox.setLocation(360,10);
		
		WPictureBox picturebox = new WPictureBox();
		picturebox.setSize(100,100);
		picturebox.setLocation(265,100);		
		picturebox.setImage(imageList.get(0));
		
	
	//	picturebox.setStrech(false);
		
		LumiSoft.UI.Controls.WScrollBar scrollbar = new LumiSoft.UI.Controls.WScrollBar();
		scrollbar.setSize(20,100);
		scrollbar.setLocation(370,100);		
		scrollbar.setMaximum(6);
				
	//	WTextEdit textEdit = new WTextEdit();
		textEdit.setSize(160,40);
		textEdit.setLocation(270,220);
		textEdit.setMultiline(true);
		textEdit.setText("sjksf\r\naaa\r\ndddddd\r\ndggd");
		textEdit.setMaxLength(10);
		textEdit.FlashControl();
		textEdit.addValueChangedListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				System.out.println("sss");				
			}
		});
	//	textEdit.setReadOnly(true);
	//	textEdit.setPasswordChar("*");
				
	//	WMenuBar menubar = new WMenuBar();				
		menubar.setSize(120,20);
		menubar.setLocation(5,300);
		menubar.Add("File");
		menubar.Add("Menu");
		menubar.Add("About","100");
		menubar.addMenuListener(new WContextMenuListener(){
			public void ContextMenuClosed(){}
			
			public void MenuItemSelected(WMenuItem item){
				System.out.println(item.getText());
			}
		});
			
		WMenuItem file = menubar.get(0);
		WContextMenu fileMenu = new WContextMenu(this);
		fileMenu.Add("Change Language");
		fileMenu.Add(true);
		fileMenu.Add("Exit");
		file.setSubMenu(fileMenu);
		
		WMenuItem menu = menubar.get(1);
		WContextMenu menuMenu = new WContextMenu(this);
		menuMenu.Add("Item 1");
		menuMenu.Add("Item 2");
		menu.setSubMenu(menuMenu);
		
	//	WMenuItem menu2 = menubar.get(1);
		WContextMenu menuMenu2 = new WContextMenu(this);
		menuMenu2.Add("Item 1");
		menuMenu2.Add("Item 2");
		menuMenu.get(0).setSubMenu(menuMenu2);
		try{
		WToolBar toolbar = new WToolBar();
		toolbar.setSize(120,22);
		toolbar.setLocation(5,340);
		toolbar.setImageList(imageListSmall);
		toolbar.getItems().Add(0);
		toolbar.getItems().Add(0);
		toolbar.getItems().Add(true);
		toolbar.getItems().Add(0);
		(toolbar.getItems().get(1)).setEnabled(false);
		toolbar.addToolBarListener(new WToolBarListener(){
			public void ToolBarItemClicked(WToolBarItem item){
				System.out.println("ToolBar Item pressedX");
			}
		});
		(toolbar.getItems().get(0)).setMenu(fileMenu);
		
		toolbar.getItems().get(0).setToolTipText("button1");
		toolbar.getItems().get(1).setToolTipText("button2");
		
		
		grid.setSize(400,250);
		grid.setLocation(130,280);
		grid.getMainView().setShowMaximizeForMainView(true);
	//	grid.setAnchorStyle(AnchorStyles.All);
		grid.addGridListener(new WGridAdapter(){
	//		public boolean OnAddRow(DataRowView dr){
	//			System.out.println("Add");
	//			return true;
	//		}
	//		public void OnDeleteRow(DataRowView dr){
		//		System.out.println("Delete");
	//		}
	//		public void OnUpdateRow(DataRowView dr){
			//	System.out.println("Update");
	//		}
			
	//		public void OnRowDoubleClicked(DataRowView dr){
	//			System.out.println("OnRowDoubleClicked");
	//		}			
		});
//		grid.getMainView().setEditable(true);
		
			WGridColumn col1 = grid.getMainView().getColumns().AddColumn("Col 1",130,"DEEBET");
			WGridColumn col2 = grid.getMainView().getColumns().AddColumn("Col 2",130,"KREEDIT");
			WGridColumn col3 = grid.getMainView().getColumns().AddColumn("Col 3",70,"SUMMA");
		//	col2.setEditor(new WButtonEditEditor());
		//	col2.setEditor(new WNumericEditEditor());
			col3.setFooterText("Sum: ");
			col3.setCellTextFormat("f2");
			col3.setFooterType(WGridViewFooterType.Sum);
			col1.setVisible(false);		
		//	col2.setEditable(false);
		//	m_pFocusedView.getColumns().AddColumn("Col 1",130,"KUUP");
		//	m_pFocusedView.getColumns().AddColumn("Col 1",30,"MOOT");
		//	m_pFocusedView.getColumns().AddColumn("Col 2",240,"NIMETUS");
		//	m_pFocusedView.getColumns().AddColumn("Col 3",60,"HHIND");
				
	//	LumiSoft.Wisk.Client.Data.wcl_o service = new LumiSoft.Wisk.Client.Data.wcl_o();
							
		//	ds.ReadXml(new java.io.ByteArrayInputStream(service.PriceList_Search("junior","1")));
		//	ds.ReadXml(new java.io.ByteArrayInputStream(service.PriceList_Search("klemmliist","1")));
		//	ds.ReadXml("d:\\a_myyk.xml");			
			ds.ReadXml(this.getClass().getResourceAsStream("Data/w_bil.xml"));
			ds.getRelations().Add("Relation1",ds.getTables().get("Parent").getColumns().get("DK"),ds.getTables().get("Child").getColumns().get("DK"));
		
		//	ds.WriteXml("d:\\testSch.xml");
			
		//	grid.getMainView().setDataSource(ds.getTables().get("Detail").getDefaultView());			
			DataView dv = ds.getTables().get("Parent").getDefaultView();
		//	dv.setSort("DEEBET");
			grid.getMainView().setDataSource(dv);
			
			WGridTableView detailView = new WGridTableView(grid);
			detailView.getColumns().AddColumn("Col 1",70,"DEEBET");
			grid.getMainView().setDetailView(detailView);
			grid.getMainView().setDataRelation(ds.getRelations().get(0));
			
		//	grid.AddTableMapping("Child",detailView);
		
		
		WCalendar calendar = new WCalendar();
		calendar.setSize(194,157);
		calendar.setLocation(397,10);
		
		WStatusBar statusBar = new WStatusBar();
		statusBar.setSize(220,20);
		statusBar.setLocation(397,170);
		WStatusBarPanel panel = statusBar.getPanels().Add(true);
		panel.setIcon(new ImageIcon(imageListSmall.get(0)));
		statusBar.getPanels().Add(100,"Panel2");
		
		WBarCode barCode = new WBarCode();
		barCode.setSize(136,48);
		barCode.setLocation(440,190);
		barCode.setBarCode("9789055900510");
	//	barCode.setBarCode("55123457");
		
		m_pColorPicker.setSize(120,22);
		m_pColorPicker.setLocation(580,190);
		m_pColorPicker.setColor(Color.RED);
			
		this.add(hyperlink);
		this.add(button);
		this.add(button2);
		this.add(tabBar);
		this.add(tab);
		this.add(label);
		this.add(spinedit);
		this.add(combobox);
		this.add(buttonEdit);
		this.add(dateTime);
		this.add(numericedit);
		this.add(checkbox);
		this.add(picturebox);
		this.add(scrollbar);
		this.add(textEdit);
		this.add(menubar);
		this.add(toolbar);
		this.add(grid);
		this.add(calendar);
		this.add(statusBar);
		this.add(barCode);
		this.add(m_pColorPicker);
		
	//	this.add(p);
	//	this.add(b);
		this.setLayout(null);
		
		}
		catch(Exception x){
			x.printStackTrace();
		}
	}
	
	
	private void ShowPopupMenu()
	{							
		WContextMenu frame = new WContextMenu(m_pMenuParent);
		frame.setImageList(m_pImageList);
		frame.addContextMenuListener(new WContextMenuListener(){
			public void ContextMenuClosed(){				
			}
			
			public void MenuItemSelected(WMenuItem item){
				System.out.println(item.getText());
			}
		});
		
		frame.Add("Menu item 1 gsdgdggdggddgdg");
		frame.Add("Menu item 2");
		frame.Add("Menu item 3");
		frame.Add(true);
		frame.Add("Menu item 4");
		frame.Add("Menu item 5");
		
		WContextMenu subMenu = new WContextMenu(m_pMenuParent);
		subMenu.Add("Submenu item 1");
		subMenu.Add("Submenu item 2");
		frame.get(1).setSubMenu(subMenu);
		
		frame.Show(m_pMenuParent.getLocationOnScreen().x + m_pMenuParent.getWidth(),m_pMenuParent.getLocationOnScreen().y);		
	}
}
