001: /**
002: * Caption: Zaval Java Resource Editor
003: * $Revision: 0.37 $
004: * $Date: 2002/03/28 9:24:42 $
005: *
006: * @author: Victor Krapivin
007: * @version: 1.3
008: *
009: * Zaval JRC Editor is a visual editor which allows you to manipulate
010: * localization strings for all Java based software with appropriate
011: * support embedded.
012: *
013: * For more info on this product read Zaval Java Resource Editor User's Guide
014: * (It comes within this package).
015: * The latest product version is always available from the product's homepage:
016: * http://www.zaval.org/products/jrc-editor/
017: * and from the SourceForge:
018: * http://sourceforge.net/projects/zaval0002/
019: *
020: * Contacts:
021: * Support : support@zaval.org
022: * Change Requests : change-request@zaval.org
023: * Feedback : feedback@zaval.org
024: * Other : info@zaval.org
025: *
026: * Copyright (C) 2001-2002 Zaval Creative Engineering Group (http://www.zaval.org)
027: *
028: * This program is free software; you can redistribute it and/or
029: * modify it under the terms of the GNU General Public License
030: * (version 2) as published by the Free Software Foundation.
031: *
032: * This program is distributed in the hope that it will be useful,
033: * but WITHOUT ANY WARRANTY; without even the implied warranty of
034: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
035: * GNU General Public License for more details.
036: *
037: * You should have received a copy of the GNU General Public License
038: * along with this program; if not, write to the Free Software
039: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
040: *
041: */package org.zaval.awt;
042:
043: import org.zaval.awt.image.*;
044: import java.awt.*;
045: import java.awt.image.*;
046: import java.util.*;
047:
048: public class SpeedButton extends Canvas {
049: private static final int FREE = 1;
050: private static final int UP = 2;
051: private static final int DOWN = 3;
052:
053: private Image normImg, upImg, disImg, downImg;
054:
055: private int w, h, state = FREE;
056:
057: public void setImageSize(Dimension d) {
058: w = d.width;
059: h = d.height;
060: }
061:
062: public SpeedButton(Image src, int border, int light,
063: ButtonImageFilter filt) {
064: w = src.getWidth(this );
065: h = src.getHeight(this );
066: if (border < 0) {
067: border = -border;
068: border = (w * border) / 100;
069: if (border < 2)
070: border = 2;
071: }
072: init(src, getFilter(src, filt, light, border, false),
073: getFilter(src, filt, -light, border, true), getFilter(
074: src, filt, -Math.abs(light), 0, false));
075: }
076:
077: public SpeedButton(Image src, Image up, Image down, Image dis) {
078: if (src == null)
079: w = h = 0;
080: else {
081: w = src.getWidth(this );
082: h = src.getHeight(this );
083: }
084: init(src, up, down, dis);
085: }
086:
087: public SpeedButton(Image src) {
088: this (src, -5, 30, new BoxButtonFilter());
089: }
090:
091: private void init(Image src, Image up, Image down, Image dis) {
092: normImg = src;
093: upImg = up;
094: disImg = dis;
095: downImg = down;
096: }
097:
098: public Image getFilter(Image src, ButtonImageFilter filt,
099: int light, int border, boolean b) {
100: filt = (ButtonImageFilter) filt.clone();
101: filt.setup(light, border, w, h, b);
102:
103: //ImageFilter cropfilter = new CropImageFilter(0, 0, w, h);
104: //ImageProducer prod = new FilteredImageSource(src.getSource(), cropfilter);
105: ImageProducer prod = src.getSource();
106: ImageProducer ip = new FilteredImageSource(prod, filt);
107: return createImage(ip);
108: }
109:
110: public int getState() {
111: return state;
112: }
113:
114: public void setState(int s) {
115: state = s;
116: repaint();
117: }
118:
119: public Dimension preferredSize() {
120: if (w < 0)
121: w = 0;
122: if (h < 0)
123: h = 0;
124: return new Dimension(w, h);
125: }
126:
127: public void paint(Graphics gr) {
128: Dimension d = size();
129: int ww = d.width;
130: int hh = d.height;
131:
132: if (disImg != null && !isEnabled())
133: gr.drawImage(disImg, 0, 0, ww, hh, this );
134: else if (state == FREE && normImg != null)
135: gr.drawImage(normImg, 0, 0, ww, hh, this );
136: else if (state == DOWN && downImg != null)
137: gr.drawImage(downImg, 0, 0, ww, hh, this );
138: else if (state == UP && upImg != null)
139: gr.drawImage(upImg, 0, 0, ww, hh, this );
140: }
141:
142: public boolean mouseUp(Event ev, int x, int y) {
143: if (!isEnabled() || state == FREE)
144: return true;
145: setState(UP);
146: getParent()
147: .postEvent(new Event(this , Event.ACTION_EVENT, null));
148: return true;
149: }
150:
151: public boolean mouseDown(Event ev, int x, int y) {
152: if (!isEnabled())
153: return true;
154: setState(DOWN);
155: return true;
156: }
157:
158: public boolean mouseEnter(Event ev, int x, int y) {
159: setState(UP);
160: return true;
161: }
162:
163: public boolean mouseExit(Event ev, int x, int y) {
164: setState(FREE);
165: return true;
166: }
167: }
|