package swingbrowser.test.server;

import java.awt.*;
import java.util.*;

import javax.swing.*;
import uib.server.*;
import uib.server.components.*;
import uib.server.components.event.*;
import uib.server.components.table.*;
import uib.server.layout.*;

public class UTestApp extends USecureNetworkApplication
{
    UFrame frame;
    UDesktopPane deskPane;
    UButton openAsyncDemo, openLazyTableDemo, openTreeDemo;

    public UTestApp()
    {
    }

    protected UPanel createButtonPanel()
    {
        UPanel btnPanel = new UPanel();
        btnPanel.setLayout(new UFlowLayout());
        openAsyncDemo = new UButton("AsyncEventDemo");
        openLazyTableDemo = new UButton("LazyLoadingTable");
        openTreeDemo = new UButton("TreeDemo");

        openAsyncDemo.addActionListener(new UDefaultActionListener()
        {
            public void actionPerformed()
            {
                final UInternalFrame asyncFrame = new UInternalFrame();
                asyncFrame.setLayout(new UFlowLayout());
                UActiveTextField field = new UActiveTextField("StartText")
                {
                    public void textChanged()
                    {
                        if (getText().equalsIgnoreCase("right"))
                        {
                            setBackground(Color.green);
                        } else
                        {
                            setBackground(Color.red);
                        }
                    }
                };
                asyncFrame.add(new ULabel("Asynchronous check for \"right\":"));
                asyncFrame.add(field);

                deskPane.add(asyncFrame);
                asyncFrame.pack();
                asyncFrame.setVisible(true);
                asyncFrame.setResizable(true);
                asyncFrame.setCloseable(true);
            }
        });

        openLazyTableDemo.addActionListener(new UDefaultActionListener()
        {
            public void actionPerformed()
            {
                UInternalFrame asyncFrame = new UInternalFrame();

                UTableModell model = new UTableModell()
                {
                    public String[] getColumnNames()
                    {
                        String[] columNames = { "Zeile1", "Zeile2", "Zeile3", "Zeile4" };
                        return columNames;
                    }

                    public int getRowCount()
                    {
                        return 150;
                    }

                    public UComponent getValueAt(int rowIndex, int columnIndex)
                    {
                        UComponent comp = null;
                        if(columnIndex == 0)
                        {
                            comp = new UImage("1.png", 16, 16);
                        }else
                        {
                           comp = new ULabel(columnIndex+" / "+rowIndex);
                        }

                        return comp;
                    }
                };

                UTable table = new UTable(model);
                UScrollPane spane = new UScrollPane(table);

                asyncFrame.add(spane);

                deskPane.add(asyncFrame);
                asyncFrame.pack();
                asyncFrame.setVisible(true);
                asyncFrame.setResizable(true);
                asyncFrame.setCloseable(true);
            }
        });

        openTreeDemo.addActionListener(new UDefaultActionListener()
        {
            public void actionPerformed()
            {
                UInternalFrame asyncFrame = new UInternalFrame();
                
                UMutableTreeNode node = new UMutableTreeNode("Root");
                UMutableTreeNode btnNode = new UMutableTreeNode(new UButton("Button in a Tree"));
                btnNode.add(new UMutableTreeNode("A normal text node"));
                node.add(btnNode);
                UTree tree = new UTree(node);
            
                asyncFrame.add(new UScrollPane(tree));
                deskPane.add(asyncFrame);
                asyncFrame.pack();
                asyncFrame.setVisible(true);
                asyncFrame.setResizable(true);
                asyncFrame.setCloseable(true);
            }
        });
        

        btnPanel.add(openAsyncDemo);
        btnPanel.add(openLazyTableDemo);
        btnPanel.add(openTreeDemo);

        return btnPanel;
    }

    public void init(UApplet applet)
    {
        super.init();

        UParentContainer cont = applet;
        if (cont == null)
        {
            cont = new UFrame();
            frame = (UFrame) cont;
            ((UFrame) cont).setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            cont.setSize(300, 300);
        }
        cont.getContentPane().setLayout(new UBorderLayout());
        deskPane = new UDesktopPane();

        cont.getContentPane().add(createButtonPanel(), BorderLayout.NORTH);
        cont.add(deskPane, BorderLayout.CENTER);
        
        cont.setVisible(true);
    }
}