/*
* JPicButton.java
*
* Created on 01.09.2009, 15:07:55
*
* Copyright (C) 01.09.2009 reiner
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
*
* @author reiner
*/
package shared.swing;
import java.awt.AWTEvent;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JComponent;
public class JPicButton extends JComponent
{
public static final int MOUSEBUTTON1 = 0x01;
public static final int MOUSEBUTTON2 = 0x02;
public static final int MOUSEBUTTON3 = 0x04;
private ArrayList<ActionListener> m_actionListenersArray = new ArrayList<ActionListener>();
protected Image m_uImg;
protected Image m_dImg;
protected Image m_xuImg;
protected Image m_xdImg;
private boolean m_bEnabled;
private boolean m_bDown;
private boolean m_bLastDown;
private boolean m_bToggleMode;
private int m_buttonMask;
private String m_actionCommand;
private boolean m_bMouseDown = false;
private String m_upPicFile;
private String m_downPicFile;
private String m_xupPicFile;
private String m_xdownPicFile;
private Object m_data = null;
private Object m_interpolation = RenderingHints.VALUE_INTERPOLATION_BICUBIC;
public JPicButton()
{
doInit(false);
}
public JPicButton(boolean bToggleMode)
{
doInit(bToggleMode);
}
private void doInit(boolean bToggleMode)
{
m_bEnabled = true;
m_bDown = false;
m_bLastDown = false;
m_bToggleMode = bToggleMode;
m_buttonMask = MOUSEBUTTON1;
m_bMouseDown = false;
final JPicButton button = this;
addMouseListener(new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent ev)
{
if (m_bEnabled)
{
if (((1 << (ev.getButton() - 1)) & m_buttonMask) != 0)
{
m_bLastDown = m_bDown;
m_bDown = true;
m_bMouseDown = true;
repaint();
}
}
}
@Override
public void mouseReleased(MouseEvent ev)
{
if (m_bEnabled && m_bMouseDown)
{
boolean bInCtrl = false;
Point pt = ev.getPoint();
if (pt.x >= 0 && pt.x <= getWidth() && pt.y >= 0 && pt.y <= getHeight())
bInCtrl = true;
if (((1 << (ev.getButton() - 1)) & m_buttonMask) != 0)
{
m_bDown = bInCtrl ? (m_bToggleMode ? !m_bLastDown : false) : m_bLastDown;
m_bMouseDown = false;
repaint();
if (bInCtrl)
fireActionPerformed();
}
}
}
});
}
public void setActionCommand(String actionCommand)
{
m_actionCommand = actionCommand;
}
public String getActionCommand()
{
return m_actionCommand;
}
public void addActionListener(ActionListener actionListener)
{
if (m_actionListenersArray == null)
m_actionListenersArray = new ArrayList<ActionListener>();
m_actionListenersArray.add(actionListener);
}
public void removeActionListener(ActionListener al)
{
if (m_actionListenersArray != null)
{
int index = 0;
for (ActionListener item : m_actionListenersArray)
{
if (item.equals(al))
{
m_actionListenersArray.remove(index);
return;
}
else
index++;
}
}
}
protected void fireActionPerformed()
{
if (m_actionListenersArray != null && !m_actionListenersArray.isEmpty())
{
AWTEvent event = EventQueue.getCurrentEvent();
int modifier = 0;
if (event instanceof InputEvent)
modifier = ((InputEvent)event).getModifiers();
else if (event instanceof ActionEvent)
modifier = ((ActionEvent)event).getModifiers();
else assert (false);
ActionEvent actionEvent = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, m_actionCommand, EventQueue.getMostRecentEventTime(), modifier);
for (ActionListener listener : m_actionListenersArray)
listener.actionPerformed(actionEvent);
}
}
public Image getUPic(boolean bEnabled)
{
return (bEnabled ? m_uImg : m_xuImg);
}
public Image getDPic(boolean bEnabled)
{
return (bEnabled ? m_dImg : m_xdImg);
}
public void setUPic(Image img, boolean bEnabled)
{
if (bEnabled)
m_uImg = img;
else m_xuImg = img;
}
public void setDPic(Image img, boolean bEnabled)
{
if (bEnabled)
m_dImg = img;
else m_xdImg = img;
}
public void setUPic(Image img)
{
setUPic(img, true);
}
public void setDPic(Image img)
{
m_dImg = img;
}
public void setXPic(Image img)
{
m_xuImg = m_xdImg = img;
}
public void setEnabledUpPicture(String pic)
{
setUPic(Toolkit.getDefaultToolkit().createImage(m_upPicFile = pic), true);
}
public Image getEnabledUpPicture()
{
return m_uImg;
}
public void setEnabledDownPicture(String pic)
{
setDPic(Toolkit.getDefaultToolkit().createImage(m_downPicFile = pic), true);
}
public Image getEnabledDownPicture()
{
return m_dImg;
}
public void setDisabledUpPicture(String pic)
{
setUPic(Toolkit.getDefaultToolkit().createImage(m_xupPicFile = pic), false);
}
public Image getDisabledUpPicture()
{
return m_xuImg;
}
public void setDisabledDownPicture(String pic)
{
setDPic(Toolkit.getDefaultToolkit().createImage(m_xdownPicFile = pic), false);
}
public Image getDisabledDownPicture()
{
return m_xdImg;
}
public void waitForAllPictures()
{
MediaTracker mediaTracker = new MediaTracker(this);
if (m_uImg != null)
mediaTracker.addImage(m_uImg, 1);
if (m_dImg != null)
mediaTracker.addImage(m_dImg, 2);
if (m_xuImg != null)
mediaTracker.addImage(m_xuImg, 3);
if (m_xdImg != null)
mediaTracker.addImage(m_xdImg, 4);
try
{
mediaTracker.waitForAll();
}
catch(InterruptedException ex)
{}
}
public boolean isDown()
{
return m_bDown;
}
public boolean isEnabled()
{
return m_bEnabled;
}
public void setEnabled(boolean bEnabled)
{
m_bEnabled = bEnabled;
m_bMouseDown = false;
repaint();
}
public void setDown(boolean bDown)
{
m_bDown = bDown;
m_bMouseDown = false;
repaint();
}
public void setToggleMode(boolean bToggleMode)
{
m_bToggleMode = bToggleMode;
}
public boolean getToggleMode()
{
return m_bToggleMode;
}
public void setButtonMask(int buttonMask)
{
m_buttonMask = buttonMask;
}
public int getButtonMask()
{
return m_buttonMask;
}
public void paintComponent(Graphics g)
{
Image img = null;
if (m_bEnabled)
{
img = (m_bDown ? m_dImg : m_uImg);
}
else
{
img = (m_bDown ? m_xdImg : m_xuImg);
}
Rectangle rect = getBounds();
Insets ins = getInsets();
rect = new Rectangle(ins.left, ins.top, rect.width - ins.left - ins.right, rect.height - ins.bottom - ins.top);
if (img != null)
{
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, m_interpolation);
if (rect != null)
{
int w = img.getWidth(this);
int h = img.getHeight(this);
if (w >= 0 && h >= 0)
g2d.drawImage(img, 0, 0, rect.width, rect.height, 0, 0, w, h, this);
}
}
}
public boolean getMouseDown()
{
return m_bMouseDown;
}
public void setMouseDown(boolean bMouseDown)
{
m_bMouseDown = bMouseDown;
}
/*
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
{
if ((infoflags & (FRAMEBITS | ALLBITS)) != 0)
repaint();
return (infoflags & (ALLBITS | ABORT)) == 0;
}
*/
public void setData(Object data)
{
m_data = data;
}
public Object getData()
{
return m_data;
}
}