/*
 * PopupApplicaitonContextAdapter.java  $Revision: 1.5 $  $Date: 2007/10/03 04:59:32 $
 *
 * Copyright (C) 2007 Meldcraft Corporation
 *
 * This file is part of Scaffold.
 * 
 * Scaffold is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Scaffold is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Scaffold.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.meldcraft.application.guis;

import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.ref.WeakReference;

import javax.swing.JPopupMenu;

import com.meldcraft.application.ApplicationContext;
import com.meldcraft.application.ApplicationContextEvent;
import com.meldcraft.application.ApplicationContextListener;
import com.meldcraft.application.util.BeanUtil;
import com.meldcraft.gui.swing.SwingUtil;
import com.meldcraft.util.PropertiesHelper;

/**
 * @version $Revision: 1.5 $ $Date: 2007/10/03 04:59:32 $
 * @author Rob Richards
 */
public class PopupApplicationContextAdapter extends MouseAdapter implements ApplicationContextListener
{
// Static variables
// Member variables
    private String              mElementId;
    private ApplicationContext  mApplicationContext;
    private JPopupMenu          mPopup;
    private WeakReference       mCompRef;

// Constructors and initialization
    public PopupApplicationContextAdapter(ApplicationContext context, String elementId, Component comp, JPopupMenu popup)
    {
        mElementId = elementId;
        mApplicationContext = context;
        mPopup = popup;
        mCompRef = new WeakReference(comp);

        mApplicationContext.addApplicationContextListener(this, getPopupApplicationContextKey());
        comp.addMouseListener(this);
    }

// Interface implemenations
// Public methods
    public void mousePressed(MouseEvent e)
    {
        checkPopup(e);
    }

    public void mouseReleased(MouseEvent e)
    {
        checkPopup(e);
    }

    public String getPopupApplicationContextKey()
    {
        return mElementId + ".popup";
    }

    public void applicationContextChanged(ApplicationContextEvent e)
    {
        Object val = e.getValue();
        final int x = PropertiesHelper.getInt(BeanUtil.getPropertyValue(val, "x"), -1);
        final int y = PropertiesHelper.getInt(BeanUtil.getPropertyValue(val, "y"), -1);

        final Component comp = (Component)mCompRef.get();
        if (comp != null)
        {
            SwingUtil.invokeLater(new Runnable() {
                public void run()
                {
                    if (comp.isDisplayable())
                    {
                        mPopup.show(comp, x, y);
                    }
                }
            });
        }
        else
        {
            mApplicationContext.removeApplicationContextListener(this, getPopupApplicationContextKey());
        }
    }

// Protected methods
    protected void checkPopup(MouseEvent e)
    {
        if (e.isPopupTrigger())
        {
            mApplicationContext.contextChanged(getPopupApplicationContextKey(), e);
        }
    }

// Private methods
// Inner classes
}
