/*
 * ConfigBorderPanel.java
 * 
 * Copyright (c) 2003-2004 Digi International
 * This program and the information contained in it is confidential and
 * proprietary to Digi International and may not be used, copied, or re-
 * produced without the prior written permission of Digi International.
 * 
 */

package com.digi.config.ui;

import com.digi.config.util.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.border.*;
import javax.swing.colorchooser.*;
import javax.swing.filechooser.*;
import javax.accessibility.*;

import javax.swing.plaf.metal.DefaultMetalTheme;
import javax.swing.plaf.metal.MetalLookAndFeel;

import java.lang.reflect.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.util.*;
import java.io.*;
import java.applet.*;
import java.net.*;



/** 
 * This class represents the top level of the configuration user interface. This class is
 * a JPanel that can be imbedded in other constructs such as a JFrame or an Applet. 
 * This panel utilizes a border layout and the content of outer edges of the border are defined in
 * a resource file and are loaded on startup. This allows these items to be customized by the user
 * if desired. The center area of this panel contains the main configuration interface.
 */


public class ConfigBorderPanel extends JPanel {

    JPanel northPanel = null;
    JPanel southPanel = null;
    JPanel eastPanel = null;
    JPanel westPanel = null;

    ConfigAppPanel configAppPanel = null;

    // The preferred size of the demo
    private static final int PREFERRED_WIDTH = 800;
    private static final int PREFERRED_HEIGHT = 720;


    // Resource bundle for internationalized and accessible text
    private ResourceBundle bundle = null;


    /**
     * ConfigBorderPanel Constructor
     */
    public ConfigBorderPanel() throws Exception {
        SplashScreenPanel.currentScreen().updateProgress();

        // setLayout(new BorderLayout());
        setLayout(new BorderLayout());

        // set the preferred size of the demo
//        setPreferredSize(new Dimension(PREFERRED_WIDTH,PREFERRED_HEIGHT));

        initializeContent();
    }


    // *******************************************************
    // *************** Demo Loading Methods ******************
    // *******************************************************

    public void initializeContent() throws Exception {        
        if (northPanel==null) {            
            northPanel = getUserPanel("NorthBorder");
            add(northPanel, BorderLayout.NORTH);
            SplashScreenPanel.currentScreen().updateProgress();
        }
        if (southPanel==null) {            
            southPanel = getUserPanel("SouthBorder");
            add(southPanel, BorderLayout.SOUTH);
            SplashScreenPanel.currentScreen().updateProgress();
        }
        if (eastPanel==null) {            
            eastPanel = getUserPanel("EastBorder");
            add(eastPanel, BorderLayout.EAST);
            SplashScreenPanel.currentScreen().updateProgress();
        }
        if (westPanel==null) {
            westPanel = getUserPanel("WestBorder");
            add(westPanel, BorderLayout.WEST);
            SplashScreenPanel.currentScreen().updateProgress();
        }

        add(getConfigAppPanel(), BorderLayout.CENTER);
        SplashScreenPanel.currentScreen().updateProgress();
    }



    public JPanel getUserPanel(String panelName) {
        JPanel panel = new JPanel() {
            ImageIcon backgroundIcon = ConfigResource.getImageRbIcon("BorderBackgroundImage");
            Image backgroundImage;
            boolean tileImage = true; // finish this... get from resource bundle
            boolean stretchImage = false; // finish this... get from resource bundle
            {
                setOpaque(false);
                if (backgroundIcon!=null) {
                    backgroundImage = backgroundIcon.getImage();
                }
            }
            public void paint (Graphics g) {
                if (backgroundImage!=null) {
                    // paint the background image
                    if (tileImage) {
                        for (int x=0;x<this.getWidth();x+=backgroundIcon.getIconWidth()) {
                            for (int y=0; y<this.getHeight(); y+= backgroundIcon.getIconHeight()) {
                                g.drawImage(backgroundImage, x, y, this);
                            }
                        }
                    } else if (stretchImage) {
                        g.drawImage(backgroundImage, 0, 0, this.getWidth(), this.getHeight(), this);
                    } else {
                        g.drawImage(backgroundImage, 0, 0, this);
                    }
                }
                super.paint(g);
            }
        };
        panel.setBackground(Color.WHITE);
        // properly size ourselves
        int width=ConfigSettings.getInteger("WindowSettings",
            "BorderAreaWidth");
        int height=ConfigSettings.getInteger("WindowSettings",
            "BorderAreaHeight");
        panel.setSize(width, height);
//        panel.add(new JLabel(panelName));
        // temp hack to put some bogus content in here
        if (panelName.equals("NorthBorder")) {
            //panel.setLayout(new BorderLayout());
            /*
            URLClassLoader urlLoader = (URLClassLoader)this.getClass().getClassLoader();
            URL fileLoc = urlLoader.findResource("images/logo.gif");
            ImageIcon iconLogo  = new ImageIcon(urlLoader.findResource("images/logo.gif"));
            ImageIcon iconDevice  = new ImageIcon(urlLoader.findResource("images/digiconnectME.jpg"));
            */
            ImageIcon iconLogo = ConfigResource.getImageRbIcon("logo");
            ImageIcon iconDevice = ConfigResource.getImageRbIcon("device");
//            ImageIcon iconLogo  = getImage("logo.gif", this);
//            panel.add(new JLabel(iconLogo), BorderLayout.WEST);
//            panel.add(new JLabel(iconDevice), BorderLayout.EAST);
        }
        return panel;
    }

    public ConfigAppPanel getConfigAppPanel() throws Exception {
        if (configAppPanel==null) {
            configAppPanel = new ConfigAppPanel();
            SplashScreenPanel.currentScreen().updateProgress();
        }
        return configAppPanel;
    }


    /**
     * This method returns a string from the demo's resource bundle.
     */
    public String getString(String key) {
        String value = null;
        try {
            value = getResourceBundle().getString(key);
        } catch (MissingResourceException e) {
            System.out.println("java.util.MissingResourceException: Couldn't find value for: " + key);
        }
        if (value == null) {
            value = "Could not find resource: " + key + "  ";
        }
        return value;
    }

    /**
     * Returns the resource bundle associated with this demo. Used
     * to get accessable and internationalized strings.
     */
    public ResourceBundle getResourceBundle() {
        if (bundle == null) {
            bundle = ResourceBundle.getBundle("resources.swingset");
        }
        return bundle;
    }


    public static ImageIcon getImage(String name, Component cmp) {
        Image img = null;
        URLClassLoader urlLoader = (URLClassLoader)cmp.getClass().getClassLoader();
        URL fileLoc = urlLoader.findResource("images/" + name);
        img = cmp.getToolkit().createImage(fileLoc);

        MediaTracker tracker = new MediaTracker(cmp);
        tracker.addImage(img, 0);
        try {
            tracker.waitForID(0);
            if (tracker.isErrorAny()) {
                System.out.println("Error loading image " + name);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ImageIcon(img);
    }





}

