001 /*
002 * Copyright (c) Holger Pfaff - http://pfaff.ws
003 *
004 * This software maybe used for any purpose provided the
005 * above copyright notice is retained. It is supplied as is.
006 * No warranty expressed or implied - Use at your own risk.
007 */
008
009 import java.awt.*;
010 import java.awt.event.*;
011 import java.util.*;
012
013 /**
014 * (#)Xbox.java
015 * @author Holger Pfaff
016 * @version 3.2 19-Mar-2004<br><br>
017 *
018 * A simple on/off style box with an Caption
019 */
020
021 public class Xbox extends Knob {
022
023 /**
024 * the state
025 */
026 protected boolean state = false;
027
028 /**
029 * Standard constructor
030 *
031 * @param label the label
032 */
033 public Xbox(String label) {
034 super(label);
035 borderType = NONE;
036 borderDepth = 0;
037 iconDist = 3;
038 iconPos = LEFT;
039 mode = LEFT;
040 }
041
042 /**
043 * overwrite Caption.paintIcon()
044 *
045 * @param g Graphics object to use
046 * @param x x-position of describing rect
047 * @param y y-position of describing rect
048 * @param w width of describing rect
049 * @param h height of describing rect
050 */
051 public void paintIcon(Graphics g, int x, int y, int w, int h) {
052 ++x; ++y; w-=2; h-=2;
053 drawRectangle(g, getBackground(), x, y, --w, --h, 1, RAISED); w--; h--;
054 if(state && isEnabled()) {
055 drawLine(g, null, x + 1, y + 2, x + w - 1, y + h, 1, RAISED);
056 drawLine(g, dim(g.getColor(), 128), x + 1, y + 1, x + w, y + h, 1, RAISED);
057 drawLine(g, null, x + 2, y + 1, x + w, y + h - 1, 1, RAISED);
058 drawLine(g, null, x + w - 1, y + 1, x + 1, y + h - 1, 1, RAISED);
059 drawLine(g, dim(g.getColor(), 128), x + w, y + 1, x + 1, y + h, 1, RAISED);
060 drawLine(g, null, x + w, y + 2, x + 2, y + h, 1, RAISED);
061 }
062 }
063
064 /**
065 * overwrite Caption.measureIcon()
066 *
067 * @return Dimension of Icon
068 */
069 public Dimension measureIcon() {
070 int h = measureCaption("X", getFont(), getFont(), "").height;
071 return new Dimension(h, h);
072 }
073
074 /**
075 * change state on mousePress Event
076 */
077 public void onPress() {
078 entered = false;
079 setState(!state);
080 notifyItemListeners(new ItemEvent(this, 0, this, state ? ItemEvent.SELECTED : ItemEvent.DESELECTED));
081 }
082
083 /**
084 * ignore mouseRelease Event
085 */
086 public void onRelease() {
087 }
088
089 /**
090 * set a new state
091 *
092 * @param state the state
093 */
094 public void setState(boolean state) {
095 this.state = state;
096 repaint();
097 }
098
099 /**
100 * @return current state
101 */
102 public boolean getState() {
103 return state;
104 }
105 }