
import dog.gui.*;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.net.MalformedURLException;
	
public class KnifeTest 
	extends DApplet 
	implements ActionListener 
{

	boolean painted = false;
	
	public void init() 
	{
		try 
		{
			URL docBase = getDocumentBase();
			
			DComponent.setPreloadImages(true);
			
			long time = System.currentTimeMillis();
			DFolder folder = new DFolder();
			
			// Buttons, checkboxes, options
			DGroup bcGroup = new DGroup();
			bcGroup.setLayout(new BoxLayout());
			
			DGroup cGroup = new DGroup("Checkboxes");
			cGroup.setLayout(new FlowLayout());
			cGroup.add(new DCheckbox("Checkbox 1"));
			cGroup.add(new DCheckbox("Checkbox 2"));
			cGroup.add(new DCheckbox("Checkbox 3\nThis is a multiline checkbox."));
			
			DGroup oGroup = new DGroup("Options");
			oGroup.setLayout(new FlowLayout());
			DCheckboxGroup cg = new DCheckboxGroup();
			oGroup.add(new DCheckbox("Option 1", cg));
			oGroup.add(new DCheckbox("Option 2", cg));
			oGroup.add(new DCheckbox("Option 3\nThis is a multiline option.", cg));
			
			DGroup bGroup = new DGroup("Buttons");
			bGroup.setLayout(new FlowLayout());
			bGroup.add(new DButton("Button 1"));
			DButton b2 = new DButton("Button 2\nThis is a multiline button.");
			bGroup.add(b2);
			setDefaultButton(b2);
			b2.addActionListener(this);
			bGroup.add(new DButton("Button 3", new URL(docBase, "smlbike.gif")));

			bcGroup.add(cGroup);
			bcGroup.add(oGroup);
			bcGroup.add(bGroup);
			
			// Labels, text
			DGroup ltGroup = new DGroup();
			ltGroup.setLayout(new BoxLayout());
			
			DGroup lGroup = new DGroup("Labels");
			lGroup.setLayout(new GridLayout(3, 3));
			lGroup.add(new DLabel("Northwest", DLabel.NORTHWEST));
			lGroup.add(new DLabel("North", DLabel.NORTH));
			lGroup.add(new DLabel("Northeast", DLabel.NORTHEAST));
			lGroup.add(new DLabel("West", DLabel.WEST));
			lGroup.add(new DLabel("Centre\nThis is a multiline label", DLabel.CENTER));
			lGroup.add(new DLabel("East", DLabel.EAST));
			lGroup.add(new DLabel("Southwest", DLabel.SOUTHWEST));
			lGroup.add(new DLabel("South", DLabel.SOUTH));
			lGroup.add(new DLabel("Southeast", DLabel.SOUTHEAST));
			
			DGroup tfGroup = new DGroup("Text fields");
			tfGroup.setLayout(new FlowLayout());
			tfGroup.add(new DTextField("Left aligned", DTextField.LEFT));
			tfGroup.add(new DTextField("Centre aligned", DTextField.CENTER));
			tfGroup.add(new DTextField("Right aligned", DTextField.RIGHT));
			
			DGroup spGroup = new DGroup("Spinboxes");
			spGroup.setLayout(new FlowLayout());
			spGroup.add(new DSpinbox(50, 1, 100, DSpinbox.LEFT));
			spGroup.add(new DSpinbox(10, 1, 20, DSpinbox.CENTER));
			spGroup.add(new DSpinbox(50, 50, 100, DSpinbox.RIGHT));
			
			DGroup taGroup = new DGroup("Text areas");
			taGroup.setLayout(new BoxLayout(BoxLayout.HORIZONTAL));
			taGroup.add(new DTextArea("Left aligned\nThis is the second line.", DTextArea.LEFT));
			taGroup.add(new DTextArea("Centre aligned\nThis is the second line.", DTextArea.CENTER));
			taGroup.add(new DTextArea("Right aligned\nThis is the second line.", DTextArea.RIGHT));
			
			ltGroup.add(lGroup);
			ltGroup.add(tfGroup);
			ltGroup.add(spGroup);
			ltGroup.add(taGroup);
			
			// lists and choices
			DGroup lcGroup = new DGroup();
			lcGroup.setLayout(new VerticalFlowLayout());
			
			DGroup chGroup = new DGroup("Choices");
			chGroup.setLayout(new FlowLayout());
			DChoice ch1 = new DChoice();
			DChoice ch2 = new DChoice();
			
			DGroup lsGroup = new DGroup("Lists");
			lsGroup.setLayout(new GridLayout(2, 2));
			DList ls1 = new DList();
			DList ls2 = new DList(DList.COLUMNS);
			
			DList ls3 = new DList();
			DList ls4 = new DList(DList.COLUMNS);
			ls2.addColumn("Number");
			ls2.addColumn("Date");
			ls2.addColumn("Boolean");
			ls4.addColumn("Number");
			ls4.addColumn("Date");
			ls4.addColumn("Boolean");
			ls1.setSize(250, 300);
			ls2.setSize(250, 300);
			ls3.setSize(250, 300);
			ls4.setSize(250, 300);
			
			// items for lists
			long time2 = System.currentTimeMillis();
			
			URL redBullet = new URL(docBase, "bullet.red.png");
			URL greenBullet = new URL(docBase, "bullet.green.png");
			URL blueBullet = new URL(docBase, "bullet.blue.png");
			DItem[] items = new DItem[5];
			for (int i=0; i<items.length; i++) {
				items[i] = new DItem("Root "+(i+1), greenBullet, null);
				items[i].addField("Number", new Integer(i));
				items[i].addField("Date", new java.util.Date());
				items[i].addField("Boolean", new Boolean((i%2)==0));
			}
			ch1.add(items);
			ch2.add(items);
			ls1.add(items);
			ls2.add(items);
			ls3.add(items);
			ls4.add(items);
			for (int i=0; i<items.length; i++) {
				DItem[] children = new DItem[5];
				for (int j=0; j<children.length; j++) {
					children[j] = new DItem("Child "+(i+1)+"."+(j+1), greenBullet, null);
					children[j].addField("Number", new Integer(j));
				}
				ch1.add(children);
				ch2.add(items[i], children);
				ls1.add(children);
				ls2.add(children);
				ls3.add(items[i], children);
				ls4.add(items[i], children);
				for (int j=0; j<children.length; j++) {
					DItem[] grandchildren = new DItem[5];
					for (int k=0; k<grandchildren.length; k++) {
						grandchildren[k] = new DItem("Grandchild "+(i+1)+"."+(j+1)+"."+(k+1), blueBullet, null);
						grandchildren[k].addField("Boolean", new Boolean((k%2)==0));
					}
					ch1.add(grandchildren);
					ch2.add(children[j], grandchildren);
					ls1.add(grandchildren);
					ls2.add(grandchildren);
					ls3.add(children[j], grandchildren);
					ls4.add(children[j], grandchildren);
				}
			}
			System.out.println("KnifeTest: created and added items in "+(System.currentTimeMillis()-time2)+"ms");
			
			chGroup.add(ch1);
			chGroup.add(ch2);
			lsGroup.add(ls1);
			lsGroup.add(ls2);
			lsGroup.add(ls3);
			lsGroup.add(ls4);
			
			lcGroup.add(chGroup);
			lcGroup.add(lsGroup);
			
			// Frameset, tooltips
			DGroup ftGroup = new DGroup();
			ftGroup.setLayout(new BorderLayout());
			
			DFrameset framesetv = new DFrameset(DFrameset.VERTICAL, true);
			DFrameset frameseth = new DFrameset(DFrameset.HORIZONTAL, true);
			
			frameseth.setTooltip("Drag on this edge to resize the labels horizontally.");
			framesetv.setTooltip("Drag on this edge to resize the labels vertically.");
			
			TooltipManager.setEnabled(true);
			DLabel flabel1 = new DLabel("You can resize the borders\nbetween these labels", DLabel.CENTER);
			flabel1.setTooltip("Hey, a tooltip!");
			DLabel flabel2 = new DLabel("using the frameset container.", DLabel.CENTER);
			flabel2.setTooltip("Hey, another tooltip!\nThis one is multiline.");
			DLabel flabel3 = new DLabel("The labels also have tooltips set\nwhich will pop up after around 4 seconds.", DLabel.CENTER);
			flabel3.setTooltip("Damn. These tooltips are getting a bit annoying now.");
			
			Border border1 = new Border(); border1.add("Center", flabel1);
			Border border2 = new Border(); border2.add("Center", flabel2);
			Border border3 = new Border(); border3.add("Center", flabel3);
			
			frameseth.add(border1, "60%");
			frameseth.add(border2, "40%");
			framesetv.add(frameseth, "60%");
			framesetv.add(border3, "40%");
			ftGroup.add("Center", framesetv);
			
			folder.add(new DItem("Buttons"), bcGroup);
			folder.add(new DItem("Text"), ltGroup);
			folder.add(new DItem("Lists"), lcGroup);
			folder.add(new DItem("Framesets and tooltips"), ftGroup);
			add("Center", folder);
			
			setBackgroundImage(new URL(docBase, "background.jpg"));
			
			System.out.println("KnifeTest: constructed in "+(System.currentTimeMillis()-time)+"ms");
		} 
		catch (MalformedURLException e) 
		{
			e.printStackTrace();
		}
	}

	public void actionPerformed(ActionEvent event) 
	{
		if (event.getSource() instanceof DButton) 
		{
			DMenu menu = new DMenu(this);
			menu.add(new DItem("Item 1"));
			menu.add(new DItem("Item 2"));
			menu.add(new DItem("Item 3"));
			menu.add(menu.new Separator());
			menu.add(new DItem("Item 4"));
			menu.addActionListener(this);
			menu.setConstraints((DButton)event.getSource(), menu.BELOW, menu.MOVE);
			menu.setVisible(true);
		}
		else 
		{
			DMenu menu = (DMenu)event.getSource();
			menu.dispose();
		}
	}

	public void paint(Graphics g) 
	{
		long time = System.currentTimeMillis();
		super.paint(g);
		if (!painted)
			System.out.println("KnifeTest: painted in "+(System.currentTimeMillis()-time)+"ms");
		painted = true;
	}
		

}

class Border
	extends DContainer 
{

   Border() 
   {
	   super(new BorderLayout());
   }
   
   public Insets getInsets() 
   {
	   return new Insets(1, 1, 1, 1); 
   }

   public void paint(Graphics g) 
   {
	   Dimension size = getSize();
	   DTheme theme = DTheme.getTheme();
	   g.setColor(theme.getControlShadow());
	   g.drawRect(0, 0, size.width-1, size.height-1);
	   super.paint(g);
   }
	
}
