001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013: package org.rapla.components.calendar;
014:
015: import java.awt.Color;
016: import java.awt.Dimension;
017: import java.awt.Graphics;
018: import java.awt.Insets;
019: import java.awt.event.ActionEvent;
020: import java.awt.event.MouseAdapter;
021: import java.awt.event.MouseEvent;
022: import java.awt.image.BufferedImage;
023:
024: import javax.swing.ImageIcon;
025: import javax.swing.JButton;
026: import javax.swing.SwingUtilities;
027:
028: public class RaplaArrowButton extends JButton {
029: private static final long serialVersionUID = 1L;
030:
031: ButtonStateChecker m_checker = new ButtonStateChecker();
032:
033: int m_delay = 0;
034: boolean m_buttonDown = false;
035:
036: ArrowPolygon poly;
037: char c;
038:
039: public RaplaArrowButton(char c) {
040: this (c, 18);
041:
042: }
043:
044: public RaplaArrowButton(char c, int size) {
045: super ();
046: this .c = c;
047: setMargin(new Insets(0, 0, 0, 0));
048: setSize(size, size);
049: addMouseListener(new MouseAdapter() {
050: public void mousePressed(MouseEvent me) {
051: if (!isEnabled())
052: return;
053: m_buttonDown = true;
054: // repaint();
055: m_checker.start();
056: }
057:
058: /** Implementation-specific. Should be private.*/
059: public void mouseReleased(MouseEvent me) {
060: m_buttonDown = false;
061: // repaint();
062: }
063:
064: /** Implementation-specific. Should be private.*/
065: public void mouseClicked(MouseEvent me) {
066: m_buttonDown = false;
067: //repaint();
068: }
069: });
070: }
071:
072: /** Here you can set if the button should fire repeated clicks. If
073: set to 0 the button will fire only once when pressed.
074: */
075: public void setClickRepeatDelay(int millis) {
076: m_delay = millis;
077: }
078:
079: public int getClickRepeatDelay() {
080: return m_delay;
081: }
082:
083: /** Set the size of the drop-down button. A drop-down button is square.
084: The maximum of width and height will be used as new size.
085: */
086: public void setSize(int width, int height) {
087: int size = Math.max(width, height);
088: int imageSize = size - 8;
089: if (imageSize > 0) {
090: poly = new ArrowPolygon(c, imageSize);
091: BufferedImage image = new BufferedImage(imageSize,
092: imageSize, BufferedImage.TYPE_INT_ARGB);
093: Graphics g = image.createGraphics();
094: g.setColor(new Color(0, 0, 0, 0));
095: g.fillRect(0, 0, imageSize, imageSize);
096:
097: g.setColor(Color.darkGray);
098: g.fillPolygon(poly);
099: g.setColor(Color.black);
100: g.drawPolygon(poly);
101: setIcon(new ImageIcon(image));
102: } else {
103: setIcon(null);
104: }
105: super .setSize(size, size);
106: Dimension dim = new Dimension(size, size);
107: setPreferredSize(dim);
108: setMaximumSize(dim);
109: setMinimumSize(dim);
110: }
111:
112: class ButtonStateChecker implements Runnable {
113: long startMillis;
114: long startDelay;
115:
116: public void start() {
117: startDelay = m_delay * 10;
118: startMillis = System.currentTimeMillis();
119: if (m_delay > 0)
120: SwingUtilities.invokeLater(this );
121: }
122:
123: private void fireAndReset() {
124: fireActionPerformed(new ActionEvent(this ,
125: ActionEvent.ACTION_PERFORMED, ""));
126: startMillis = System.currentTimeMillis();
127: }
128:
129: public void run() {
130: if (!m_buttonDown)
131: return;
132: if ((Math.abs(System.currentTimeMillis() - startMillis) > startDelay)) {
133: if (startDelay > m_delay)
134: startDelay = startDelay / 2;
135: fireAndReset();
136: }
137: try {
138: Thread.sleep(10);
139: } catch (Exception ex) {
140: return;
141: }
142: SwingUtilities.invokeLater(this);
143: };
144: }
145: }
|