/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.applet.components;

import javax.swing.*;
import java.beans.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;

public class LocalFileFrame extends javax.swing.JInternalFrame
    implements ActionListener, ProgressListener
	{
	LocalFilePanel panel;
	
    JMenuBar menu_bar;
    JMenu file_menu, edit_menu;
    JMenuItem   copy_menu_item, paste_menu_item, properties_menu_item; 
    
	Clipboard clipboard;
	
	public LocalFileFrame()
		{
		super( "Files on My Computer", true, false, true, true );

        copy_menu_item  = new JMenuItem( "Copy Files", 'C' );
        paste_menu_item  = new JMenuItem( "Paste (Download) Files", 'P' );
        properties_menu_item  = new JMenuItem( "Properties", 'r' );

        copy_menu_item.addActionListener( this );
        paste_menu_item.addActionListener( this );
        properties_menu_item.addActionListener( this );
        
        file_menu = new JMenu( "File" );
        file_menu.add( properties_menu_item );

        edit_menu = new JMenu( "Edit" );
        edit_menu.add( copy_menu_item );
        edit_menu.add( paste_menu_item );

        menu_bar = new JMenuBar();
        menu_bar.add( file_menu );
        menu_bar.add( edit_menu );
        
        setJMenuBar( menu_bar );

	panel = new LocalFilePanel();
	getContentPane().add( panel );
	}

	public LocalFileFrame(String title)
		{
		this();
		setTitle(title);
		}

    public void setClipboard( Clipboard clipboard )
        {
        this.clipboard = clipboard;
        panel.setClipboard( clipboard );
        }
        
    public void actionPerformed(ActionEvent e)
        {
        System.out.println( "LocalFileFrame action performed" );
        if ( e.getSource() == copy_menu_item )
            {
            panel.doFileCopy();
            }
        else if ( e.getSource() == paste_menu_item )
            {
            panel.doFilePaste();
            }
        else if ( e.getSource() == properties_menu_item )
            {
            panel.doFileProperties();
            }
        }
        
    public void progressCompleted( ProgressDialog dialog, boolean error )
	{
	doRefresh();
	}
	    
    public void doRefresh()
        {
        if ( panel!=null )
	    getContentPane().remove( panel );
	panel = new LocalFilePanel();
	getContentPane().add( panel );
        }

    public void addNotify()
		{
		// Record the size of the window prior to calling parents addNotify.
		Dimension d = getSize();

		super.addNotify();

		if (fComponentsAdjusted)
			return;

		// Adjust components according to the insets
		Insets ins = getInsets();
		setSize(ins.left + ins.right + d.width, ins.top + ins.bottom + d.height);
		Component components[] = getContentPane().getComponents();
		for (int i = 0; i < components.length; i++)
			{
			Point p = components[i].getLocation();
			p.translate(ins.left, ins.top);
			components[i].setLocation(p);
		}
		fComponentsAdjusted = true;
		}

	// Used for addNotify check.
	boolean fComponentsAdjusted = false;

	//{{DECLARE_CONTROLS
	//}}

	}
