001: package org.zaval.awt;
002:
003: import java.awt.*;
004: import java.util.*;
005:
006: public abstract class BaseCheckbox extends Canvas {
007: public final static int LEFT = 1;
008: public final static int RIGHT = 2;
009:
010: private Dimension sqSize = new Dimension(12, 12);
011: private int textInset = 3;
012:
013: boolean state = false;
014: boolean hasFocus = false;
015: boolean isCalc = false;
016: boolean mouse_down;
017:
018: private int align = LEFT;
019: int posY = 0, posX = 0;
020: Rectangle textArea = new Rectangle();
021: Rectangle eventBox = new Rectangle();
022: private TextAlignArea alignArea = new TextAlignArea();
023:
024: public String getLabel() {
025: return alignArea.getText();
026: }
027:
028: public void disable() {
029: super .disable();
030: repaint();
031: }
032:
033: public void enable() {
034: super .enable();
035: repaint();
036: }
037:
038: public void setLabel(String label) {
039: alignArea.setText(label);
040: if (alignArea.isValid())
041: return;
042: invalidate();
043: repaint();
044: }
045:
046: public void setAlign(int a) {
047: if (a == align)
048: return;
049: align = a;
050: if (alignArea.isValid())
051: return;
052: invalidate();
053: repaint();
054: }
055:
056: public int getAlign() {
057: return align;
058: }
059:
060: public boolean getState() {
061: return state;
062: }
063:
064: public void setState(boolean state) {
065: this .state = state;
066: repaint();
067: }
068:
069: public boolean gotFocus(Event e, Object o) {
070: hasFocus = true;
071: repaint();
072: return super .gotFocus(e, o);
073: }
074:
075: public boolean lostFocus(Event e, Object o) {
076: hasFocus = false;
077: repaint();
078: return super .lostFocus(e, o);
079: }
080:
081: private void calc() {
082: FontMetrics fm = getFontMetrics(getFont());
083: if (fm == null)
084: return;
085:
086: Dimension d = size();
087: posY = d.height / 2 - sqSize.height / 2;
088: posX = 0;
089:
090: if (align == RIGHT)
091: posX = d.width - sqSize.width - 1;
092:
093: eventBox.x = 0;
094: eventBox.y = 0;
095: eventBox.width = d.width;
096: eventBox.height = d.height;
097:
098: alignArea.setSize(d);
099: if (align == LEFT)
100: alignArea.setInsets(new Insets(0, textInset + sqSize.width
101: + 1, 0, 0));
102: else
103: alignArea.setInsets(new Insets(0, 1, 0, 0));
104:
105: alignArea.setFontMetrics(fm);
106:
107: textArea = alignArea.getAlignRectangle();
108: if ((textArea.x + textArea.width) >= d.width)
109: textArea.width = (d.width - textArea.x - 1);
110:
111: if ((textArea.y + textArea.height) >= d.height)
112: textArea.height = (d.height - textArea.y - 1);
113:
114: if (align == RIGHT && (textArea.x + textArea.width) > posX)
115: textArea.width = (posX - textArea.x - 1);
116:
117: isCalc = true;
118: }
119:
120: public void invalidate() {
121: isCalc = false;
122: super .invalidate();
123: }
124:
125: public void paint(Graphics g) {
126: super .paint(g);
127: if (!isCalc)
128: calc();
129: String lab = getLabel();
130:
131: if (isEnabled())
132: g.setColor(Color.black);
133: else
134: g.setColor(Color.gray);
135:
136: int yy = posY + sqSize.height;
137: if (lab != null && lab.length() > 0) {
138: if (!isEnabled())
139: alignArea.draw(g, 0, 1, Color.white);
140:
141: alignArea.draw(g, getForeground());
142: }
143:
144: if (hasFocus) {
145: g.setColor(Color.black);
146: if (lab != null && lab.length() > 0)
147: drawRect(g, textArea.x - 1, textArea.y, textArea.width,
148: textArea.height);
149: }
150:
151: paint(g, posX, posY, sqSize.width, sqSize.height);
152: }
153:
154: protected boolean insideBox(int x, int y) {
155: return eventBox.inside(x, y);
156: }
157:
158: public synchronized boolean mouseDown(Event ev, int x, int y) {
159: if (isEnabled()) {
160: if (insideBox(x, y)) {
161: mouse_down = true;
162: requestFocus();
163: if (condition())
164: stateChanged();
165: }
166: }
167: return super .mouseDown(ev, x, y);
168: }
169:
170: public synchronized boolean mouseUp(Event ev, int x, int y) {
171: if (isEnabled())
172: if (condition() && insideBox(x, y)) {
173: mouse_down = false;
174:
175: if (state)
176: state = false;
177: else
178: state = true;
179: repaint();
180:
181: Event e = new Event(this , Event.ACTION_EVENT, "1");
182: getParent().postEvent(e);
183: return true;
184: }
185: return super .mouseDown(ev, x, y);
186: }
187:
188: public boolean keyDown(Event ev, int key) {
189: if (!hasFocus || key != ' ' || (!condition()))
190: return super .keyDown(ev, key);
191:
192: setState(!getState());
193: Event e = new Event(this , Event.ACTION_EVENT, "1");
194: getParent().postEvent(e);
195: return true;
196: }
197:
198: public synchronized boolean mouseDrag(Event ev, int x, int y) {
199: if (isEnabled()) {
200: if (insideBox(x, y)) {
201: mouse_down = false;
202: repaint();
203: }
204: }
205: return super .mouseDrag(ev, x, y);
206: }
207:
208: public Dimension preferredSize() {
209: FontMetrics fm = getFontMetrics(getFont());
210: if (fm == null)
211: return super .preferredSize();
212:
213: int w = textInset + sqSize.width
214: + fm.stringWidth(alignArea.getText()) + 1;
215: int h = Math.max(fm.getHeight(), sqSize.height);
216: return new Dimension(w, h);
217: }
218:
219: public void stateChanged() {
220: }
221:
222: protected boolean condition() {
223: return true;
224: }
225:
226: public TextAlignArea getAlignArea() {
227: return alignArea;
228: }
229:
230: public boolean mouseMove(Event e, int x, int y) {
231: return true;
232: }
233:
234: public boolean mouseExit(Event e, int x, int y) {
235: return true;
236: }
237:
238: public boolean mouseEnter(Event e, int x, int y) {
239: return true;
240: }
241:
242: public abstract void paint(Graphics g, int x, int y, int width,
243: int height);
244:
245: public void drawRect(Graphics gr, int x, int y, int w, int h) {
246: drawVLine(gr, y, y + h, x);
247: drawVLine(gr, y, y + h, x + w);
248: drawHLine(gr, x, x + w, y);
249: drawHLine(gr, x, x + w, y + h);
250: }
251:
252: public void drawHLine(Graphics gr, int x1, int x2, int y1) {
253: int dx = x2 - x1;
254: int count = dx / 2 + dx % 2;
255: for (int i = 0; i < count; i++) {
256: gr.drawLine(x1, y1, x1, y1);
257: x1 += 2;
258: }
259: gr.drawLine(x2, y1, x2, y1);
260: }
261:
262: public void drawVLine(Graphics gr, int y1, int y2, int x1) {
263: int dy = y2 - y1;
264: int count = dy / 2 + dy % 2;
265: ;
266: for (int i = 0; i < count; i++) {
267: gr.drawLine(x1, y1, x1, y1);
268: y1 += 2;
269: }
270: gr.drawLine(x1, y2, x1, y2);
271: }
272: }
|