/* ======================================================================
   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 org.bodington.server.resources.*;

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

public class MenuItemPanel extends javax.swing.JPanel
	implements PropertyChangeListener, DocumentListener
	{
    JTextField title_text_field;
    JTextField href_text_field;
    
    JLabel title_label, href_label, help_label;
    
    boolean unsaved = false;
    boolean ignore_changes = false;
    
    org.bodington.server.resources.MenuItem menuitem=null;
    
	public MenuItemPanel()
	    {
	    JPanel shrinkpanel = new JPanel();
	    
	    GridBagLayout gridbag = new GridBagLayout();
	    GridBagConstraints constraints= new GridBagConstraints();
	    
		shrinkpanel.setLayout( gridbag );
		
		constraints.fill = GridBagConstraints.NONE;
		constraints.insets = new Insets( 8, 8, 8, 8 );
		constraints.anchor = GridBagConstraints.NORTHEAST;
		
		title_label = new JLabel( "Title:" );
		constraints.gridx=0;
		constraints.gridy=0;
		gridbag.setConstraints( title_label, constraints );
		shrinkpanel.add( title_label );
		
		title_text_field = new JTextField( 20 );
		title_text_field.setEditable( false );
		title_text_field.addPropertyChangeListener( this );
		title_text_field.getDocument().addDocumentListener( this );
		
		constraints.gridx=1;
		constraints.gridy=0;
		gridbag.setConstraints( title_text_field, constraints );
		shrinkpanel.add( title_text_field );
		
		href_label = new JLabel( "File:" );
		constraints.gridx=0;
		constraints.gridy=1;
		gridbag.setConstraints( href_label, constraints );
		shrinkpanel.add( href_label );
		
		href_text_field = new JTextField( 20 );
		href_text_field.setEditable( false );
		href_text_field.addPropertyChangeListener( this );
		href_text_field.getDocument().addDocumentListener( this );

		constraints.gridx=1;
		constraints.gridy=1;
		gridbag.setConstraints( href_text_field, constraints );
		shrinkpanel.add( href_text_field );
		
		help_label = new JLabel( "Paste published files into the menu." );
		constraints.gridx=0;
		constraints.gridy=2;
		constraints.gridwidth=2;
		gridbag.setConstraints( help_label, constraints );
		shrinkpanel.add( help_label );
		
		setLayout( new FlowLayout(FlowLayout.LEFT) );
		add( shrinkpanel );
	    }

    public void setMenuItem( org.bodington.server.resources.MenuItem menuitem )
        {
        this.menuitem = menuitem;
     
        try
            {
            ignore_changes = true;
            if ( menuitem == null )
                {
                title_text_field.setText( "" );
                title_text_field.setEditable( false );
                href_text_field.setText( "" );
                href_text_field.setEditable( false );
                }
            else
                {
                System.out.println( "Title: " + menuitem.getTitle() );
                title_text_field.setText( menuitem.getTitle() );
                title_text_field.setEditable( true );
                System.out.println( "HRef: " + menuitem.getHRef() );
                href_text_field.setText( menuitem.getHRef() );
                href_text_field.setEditable( false );
                }
            }
        finally
            {
            ignore_changes = false;
            }
        }

	public void propertyChange( PropertyChangeEvent evt )
		{
		System.err.println( evt.getPropertyName() );
		}
		
    public void changedUpdate( DocumentEvent e )
        {
        textUpdate( e );
        }
    public void insertUpdate( DocumentEvent e )
        {
        textUpdate( e );
        }
    public void removeUpdate( DocumentEvent e )
        {
        textUpdate( e );
        }
    
    private void textUpdate( DocumentEvent e )
        {
		System.err.println( "textUpdate( DocumentEvent e )" );
		if ( ignore_changes )
		    return;
		    
        if ( menuitem==null )
            return;
            
  		menuitem.setTitle( title_text_field.getText() );
  		menuitem.setHRef( href_text_field.getText() );
   		firePropertyChange( "MenuItem", menuitem, menuitem );
        }

    }
