001: package com.xoetrope.awt;
002:
003: import java.awt.Color;
004: import java.awt.Dimension;
005: import java.awt.Font;
006: import java.awt.FontMetrics;
007: import java.awt.Graphics;
008: import java.awt.Image;
009:
010: import com.xoetrope.event.XClickListener;
011: import com.xoetrope.event.XStateListener;
012: import com.xoetrope.util.XGraphicUtils;
013: import java.awt.Component;
014: import java.awt.Point;
015: import java.awt.Rectangle;
016: import java.awt.SystemColor;
017: import javax.swing.JComponent;
018: import net.xoetrope.xui.XAttributedComponent;
019: import net.xoetrope.xui.XProjectManager;
020: import net.xoetrope.xui.XTextHolder;
021: import net.xoetrope.xui.style.XStyle;
022: import net.xoetrope.xui.style.XStyleManager;
023:
024: /**
025: * An XKeyPad component is a visual representation of a numeric keypad.
026: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
027: * the GNU Public License (GPL), please see license.txt for more details. If
028: * you make commercial use of this software you must purchase a commercial
029: * license from Xoetrope.</p>
030: * <p>$Revision: 1.6 $</p>
031: */
032: public class XKeypad extends Component implements XStateListener,
033: XTextHolder, XAttributedComponent {
034: protected int borderHeight, borderWidth, keyHeight, keyWidth;
035: protected XClickListener clickListener;
036: protected XStyleManager styleManager;
037:
038: private String text;
039:
040: private String style;
041: private String style3D;
042: private String styleShade;
043:
044: /**
045: * The button clicked. 100 = the enter button
046: */
047: private int maxLength, minLength;
048:
049: private XTextHolder outputField;
050: private String value;
051:
052: private static Color bkClr, highlight3dColor, frameClr,
053: shadow3dClr, textColor;
054:
055: private Rectangle[] buttonAreas;
056: private int selectedArea;
057:
058: /**
059: * Constructor for a new XKeypad. This control displays a numeric keypad
060: */
061: public XKeypad() {
062: minLength = maxLength = 3;
063:
064: value = new String("");
065: clickListener = new XClickListener(this , 0);
066: addMouseListener(clickListener);
067: addMouseMotionListener(clickListener);
068:
069: style = "base";
070: style3D = "base";
071: styleShade = "base";
072: styleManager = XProjectManager.getStyleManager();
073:
074: if (bkClr == null) {
075: XStyle threeDStyle = styleManager.getStyle(style3D);
076: XStyle shadeStyle = styleManager.getStyle(styleShade);
077: bkClr = SystemColor.control;
078: textColor = SystemColor.controlText;
079: highlight3dColor = SystemColor.controlLtHighlight;
080: frameClr = SystemColor.controlShadow;
081: shadow3dClr = SystemColor.controlDkShadow;
082: }
083: buttonAreas = null;
084: selectedArea = Integer.MIN_VALUE;
085: }
086:
087: /**
088: * Refresh the display
089: * @param g
090: */
091: public void update(Graphics g) {
092: paint(g);
093: }
094:
095: /**
096: * Renders the keypad
097: * @param sg
098: */
099: public void paint(Graphics sg) {
100: Image offscreen = createImage(getSize().width, getSize().height);
101: Graphics g = offscreen.getGraphics();
102: super .paint(g);
103:
104: Dimension size = getSize();
105: int width = size.width;
106: int height = size.height;
107: g.setClip(0, 0, width, height);
108:
109: g.setColor(getBackground());
110: g.fillRect(0, 0, width, height);
111:
112: borderHeight = height / 17;
113: borderWidth = width / 13;
114: keyHeight = (height - borderHeight - borderHeight) / 5;
115: keyWidth = (width - borderWidth) / 4;
116:
117: int top = borderHeight;
118: int left = borderWidth;
119: int spacing = keyWidth + borderWidth;
120:
121: XStyle responseStyle = styleManager.getStyle(style3D);
122: Font f = styleManager.getFont(responseStyle);
123: if (f == null)
124: f = getFont();
125: FontMetrics fm = g.getFontMetrics(f);
126: int xTextInc = (keyWidth - fm.charWidth('8')) / 2;
127: int yTextInc = (fm.getAscent() + keyHeight) / 2;
128: g.setFont(f);
129:
130: g.setColor(bkClr);
131: g.draw3DRect(0, 0, width, height, true);
132:
133: if (buttonAreas == null) {
134: selectedArea = Integer.MIN_VALUE;
135: buttonAreas = new Rectangle[11];
136: buttonAreas[7] = new Rectangle(left, top, keyWidth,
137: keyHeight);
138: buttonAreas[8] = new Rectangle(left + spacing, top,
139: keyWidth, keyHeight);
140: buttonAreas[9] = new Rectangle(left + spacing + spacing,
141: top, keyWidth, keyHeight);
142: top += borderHeight + keyHeight;
143: buttonAreas[4] = new Rectangle(left, top, keyWidth,
144: keyHeight);
145: buttonAreas[5] = new Rectangle(left + spacing, top,
146: keyWidth, keyHeight);
147: buttonAreas[6] = new Rectangle(left + spacing + spacing,
148: top, keyWidth, keyHeight);
149: top += borderHeight + keyHeight;
150: buttonAreas[1] = new Rectangle(left, top, keyWidth,
151: keyHeight);
152: buttonAreas[2] = new Rectangle(left + spacing, top,
153: keyWidth, keyHeight);
154: buttonAreas[3] = new Rectangle(left + spacing + spacing,
155: top, keyWidth, keyHeight);
156: top += borderHeight + keyHeight;
157: buttonAreas[0] = new Rectangle(left, top, keyWidth,
158: keyHeight);
159: buttonAreas[10] = new Rectangle(left + spacing, top,
160: keyWidth + borderWidth + keyWidth, keyHeight);
161: }
162: top = borderHeight;
163: XGraphicUtils.draw3dObject(g, (selectedArea == 7), left, top,
164: keyWidth, keyHeight, bkClr, highlight3dColor, frameClr,
165: shadow3dClr, highlight3dColor);
166: g.setColor(textColor);
167: g.drawString("7", left + xTextInc, top + yTextInc);
168:
169: XGraphicUtils.draw3dObject(g, (selectedArea == 8), left
170: + spacing, top, keyWidth, keyHeight, bkClr,
171: highlight3dColor, frameClr, shadow3dClr,
172: highlight3dColor);
173: g.setColor(textColor);
174: g.drawString("8", left + xTextInc + spacing, top + yTextInc);
175:
176: XGraphicUtils.draw3dObject(g, (selectedArea == 9), left
177: + spacing + spacing, top, keyWidth, keyHeight, bkClr,
178: highlight3dColor, frameClr, shadow3dClr,
179: highlight3dColor);
180: g.setColor(textColor);
181: g.drawString("9", left + xTextInc + spacing + spacing, top
182: + yTextInc);
183:
184: top += borderHeight + keyHeight;
185: XGraphicUtils.draw3dObject(g, (selectedArea == 4), left, top,
186: keyWidth, keyHeight, bkClr, highlight3dColor, frameClr,
187: shadow3dClr, highlight3dColor);
188: g.setColor(textColor);
189: g.drawString("4", left + xTextInc, top + yTextInc);
190:
191: XGraphicUtils.draw3dObject(g, (selectedArea == 5), left
192: + spacing, top, keyWidth, keyHeight, bkClr,
193: highlight3dColor, frameClr, shadow3dClr,
194: highlight3dColor);
195: g.setColor(textColor);
196: g.drawString("5", left + xTextInc + spacing, top + yTextInc);
197:
198: XGraphicUtils.draw3dObject(g, (selectedArea == 6), left
199: + spacing + spacing, top, keyWidth, keyHeight, bkClr,
200: highlight3dColor, frameClr, shadow3dClr,
201: highlight3dColor);
202: g.setColor(textColor);
203: g.drawString("6", left + xTextInc + spacing + spacing, top
204: + yTextInc);
205:
206: top += borderHeight + keyHeight;
207: XGraphicUtils.draw3dObject(g, (selectedArea == 1), left, top,
208: keyWidth, keyHeight, bkClr, highlight3dColor, frameClr,
209: shadow3dClr, highlight3dColor);
210: g.setColor(textColor);
211: g.drawString("1", left + xTextInc, top + yTextInc);
212:
213: XGraphicUtils.draw3dObject(g, (selectedArea == 2), left
214: + spacing, top, keyWidth, keyHeight, bkClr,
215: highlight3dColor, frameClr, shadow3dClr,
216: highlight3dColor);
217: g.setColor(textColor);
218: g.drawString("2", left + xTextInc + spacing, top + yTextInc);
219:
220: XGraphicUtils.draw3dObject(g, (selectedArea == 3), left
221: + spacing + spacing, top, keyWidth, keyHeight, bkClr,
222: highlight3dColor, frameClr, shadow3dClr,
223: highlight3dColor);
224: g.setColor(textColor);
225: g.drawString("3", left + xTextInc + spacing + spacing, top
226: + yTextInc);
227:
228: top += borderHeight + keyHeight;
229: XGraphicUtils.draw3dObject(g, (selectedArea == 0), left, top,
230: keyWidth, keyHeight, bkClr, highlight3dColor, frameClr,
231: shadow3dClr, highlight3dColor);
232: g.setColor(textColor);
233: g.drawString("0", left + xTextInc, top + yTextInc);
234:
235: XGraphicUtils.draw3dObject(g, (selectedArea == 10), left
236: + spacing, top, keyWidth + borderWidth + keyWidth,
237: keyHeight, bkClr, highlight3dColor, frameClr,
238: shadow3dClr, highlight3dColor);
239: g.setColor(textColor);
240: g.drawString("ENTER", left + xTextInc + xTextInc + spacing, top
241: + yTextInc);
242:
243: if ((selectedArea >= 0) && clickListener.getIsSelected()) {
244: g.setColor(new Color(shadow3dClr.getRed(), shadow3dClr
245: .getGreen(), shadow3dClr.getBlue(), 128));
246: g.fillRect(buttonAreas[selectedArea].x,
247: buttonAreas[selectedArea].y,
248: buttonAreas[selectedArea].width,
249: buttonAreas[selectedArea].height);
250: }
251: sg.drawImage(offscreen, 0, 0, width, height, null);
252: g.dispose();
253: offscreen = null;
254: }
255:
256: /**
257: * Repaint the responses
258: */
259: public void paintStates() {
260: repaint();
261: }
262:
263: /**
264: * Find a response
265: * @param x the x coordinate of the mouse click
266: * @param y the y coordinate of the mouse click
267: * @param defResponse the default response
268: * @return true if a response was found
269: */
270: public boolean setState(int x, int y, int defResponse) {
271: int oldResponse = selectedArea;
272: selectedArea = findCurrentResponse(x, y);
273:
274: if (clickListener.getIsSelected()) {
275: if (selectedArea == clickListener.getActiveResponse())
276: return true;
277: else {
278: selectedArea = Integer.MIN_VALUE;
279: return true;
280: }
281: }
282: clickListener.setActiveResponse(selectedArea);
283: return (selectedArea != oldResponse);
284: }
285:
286: /**
287: * Find the response corresponding to the current point
288: * @param x the x coordinate of the mouse click
289: * @param y the y coordinate of the mouse click
290: * @return the response value if the mouse were click at the specified location,
291: * or Integer.MIN_VALUE if not selection is available
292: */
293: public int findCurrentResponse(int x, int y) {
294: if (buttonAreas == null)
295: return Integer.MIN_VALUE;
296:
297: Point pt = new Point(x, y);
298: for (int i = 0; i < buttonAreas.length; i++) {
299: if (buttonAreas[i].contains(pt)) {
300: return i;
301: }
302: }
303: return Integer.MIN_VALUE;
304: }
305:
306: /**
307: * Does nothing in this instance
308: */
309: public void updateSelectedState() {
310: }
311:
312: /**
313: * Called by XClickListener to check if a response event should be sent to the
314: * parent form. The control can also use this event to do post click processing
315: * @return true if the parent is to be notified
316: */
317: public boolean respond() {
318: if (selectedArea < 10) {
319: if (value.length() >= maxLength)
320: value = "";
321: value += Integer.toString(selectedArea);
322: if (outputField != null) {
323: outputField.setText(value);
324: ((JComponent) outputField).invalidate();
325: ((JComponent) outputField).repaint();
326: }
327: } else
328: return true;
329:
330: return false;
331: }
332:
333: /**
334: * Gets an indexed color.
335: *
336: * @param idx The index of the color to get.
337: * @param callerClassId The ID of this calling class
338: * @return The color
339: * @see com.xoetrope.miniui.XFrame#X_BUTTON
340: */
341: public Color getColor(int idx, int callerClassId) {
342: return styleManager.getStyle("keypad").getStyleAsColor(
343: XStyle.COLOR_FORE);
344: }
345:
346: /**
347: * Get the style asociated with three dimensional objects/elements
348: * @return the style name
349: */
350: public String getStyle3D() {
351: return style3D;
352: }
353:
354: /**
355: * Get the style asociated with shaded elements
356: * @return the style name
357: */
358: public String getStyleShade() {
359: return styleShade;
360: }
361:
362: /**
363: * Set the style asociated with shaded elements, background and text colors
364: * @param newStyle the style name
365: */
366: public void setStyle(String newStyle) {
367: styleShade = newStyle;
368: XStyle style = styleManager.getStyle(styleShade);
369: bkClr = style.getStyleAsColor(XStyle.COLOR_BACK);
370: textColor = style.getStyleAsColor(XStyle.COLOR_FORE);
371: repaint();
372: }
373:
374: /**
375: * Get the style asociated with three dimensional objects/elements
376: * @param newStyle the style name
377: */
378: public void setStyle3D(String newStyle) {
379: style3D = newStyle;
380: XStyle threeDStyle = styleManager.getStyle(style3D);
381: highlight3dColor = threeDStyle
382: .getStyleAsColor(XStyle.COLOR_FORE);
383: shadow3dClr = threeDStyle.getStyleAsColor(XStyle.COLOR_BACK);
384: repaint();
385: }
386:
387: /**
388: * Set the style asociated with shaded elements, frame color
389: * @param newStyle the style name
390: */
391: public void setStyleShade(String newStyle) {
392: styleShade = newStyle;
393: XStyle shadeStyle = styleManager.getStyle(styleShade);
394: frameClr = shadeStyle.getStyleAsColor(XStyle.COLOR_FORE);
395: repaint();
396: }
397:
398: /**
399: * Sets the control's text. Used to localize the control.
400: * @param newText The new text to display.
401: */
402: public void setText(String newText) {
403: text = newText;
404: invalidate();
405: }
406:
407: /**
408: * Sets a text field in which to display the key value.
409: * @param of the output text field
410: */
411: public void setOutputField(XTextHolder of) {
412: outputField = of;
413: }
414:
415: /**
416: * Clears the output value.
417: */
418: public void clear() {
419: if (outputField != null)
420: outputField.setText("");
421:
422: value = "";
423: }
424:
425: /**
426: * Gets the output value
427: * @return the keyed value
428: */
429: public String getValue() {
430: return value;
431: }
432:
433: /**
434: * Gets the output value
435: * @return the keyed value
436: */
437: public String getText() {
438: return value;
439: }
440:
441: /**
442: * Sets the output value
443: */
444: public void setValue(String newValue) {
445: value = newValue;
446: }
447:
448: /**
449: * Sets the maximum length of the output value
450: * @param len the new maximum length
451: */
452: public void setMaxLength(int len) {
453: maxLength = len;
454: }
455:
456: /**
457: * Sets the minimum length of the output value
458: * @param len the new minimum length
459: */
460: public void setMinLength(int len) {
461: minLength = len;
462: }
463:
464: /**
465: * Set one or more attributes of the component.
466: * @param attribName the name of the attribute
467: * @param attribValue the value of the attribute
468: * @return 0 for success, non zero otherwise
469: */
470: public int setAttribute(String attribName, Object attribValue) {
471: String attribNameLwr = attribName.toLowerCase();
472: String attribValueStr = (String) attribValue;
473: if (attribNameLwr.equals("style"))
474: setStyle(attribValueStr);
475: else if (attribNameLwr.equals("style3d"))
476: setStyle3D(attribValueStr);
477: else if (attribNameLwr.equals("styleshade"))
478: setStyleShade(attribValueStr);
479:
480: return 0;
481: }
482: }
|