001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.cleverphl.gui;
025:
026: import jacareto.cleverphl.menu.CleverPHLAction;
027:
028: import java.awt.event.ActionEvent;
029: import java.awt.event.ActionListener;
030: import java.awt.event.MouseEvent;
031: import java.awt.event.MouseListener;
032:
033: import javax.swing.ImageIcon;
034: import javax.swing.JButton;
035: import javax.swing.border.EmptyBorder;
036:
037: /**
038: * A button with highlighting etc.
039: *
040: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
041: * @version 1.0
042: */
043: public class CleverPHLButton extends JButton implements MouseListener,
044: ActionListener {
045: /** Whether or not the button is selected. */
046: private boolean isSelected;
047:
048: /** Whether or not the mouse is pressed on the button. */
049: private boolean isMousePressed;
050:
051: /** The images. */
052: private ImageIcon pressedIcon;
053:
054: /** The images. */
055: private ImageIcon unpressedIcon;
056:
057: /** The images. */
058: private ImageIcon selectedIcon;
059:
060: /**
061: * Creates a new Jacareto Button.
062: *
063: * @param action the action
064: */
065: public CleverPHLButton(CleverPHLAction action) {
066: super (action);
067: loadIcons();
068: setBorderPainted(false);
069: setBorder(new EmptyBorder(3, 3, 3, 3));
070: setSelected(false);
071: isMousePressed = false;
072: setText("");
073: addMouseListener(this );
074: addActionListener(this );
075: changeIcon();
076: }
077:
078: /**
079: * {@inheritDoc}
080: */
081: public void setSelected(boolean isSelected) {
082: this .isSelected = isSelected;
083: changeIcon();
084: }
085:
086: private void loadIcons() {
087: CleverPHLAction action = (CleverPHLAction) getAction();
088: Object imagePressed = action
089: .getValue(CleverPHLAction.SMALL_ICON_PRESSED);
090:
091: if (imagePressed != null) {
092: pressedIcon = (ImageIcon) imagePressed;
093: unpressedIcon = (ImageIcon) action
094: .getValue(CleverPHLAction.SMALL_ICON_UNPRESSED);
095: selectedIcon = (ImageIcon) action
096: .getValue(CleverPHLAction.SMALL_ICON_SELECTED);
097: }
098: }
099:
100: private void changeIcon() {
101: if (pressedIcon != null) {
102: if (isMousePressed) {
103: setIcon(pressedIcon);
104: } else if (isSelected()) {
105: setIcon(selectedIcon);
106: } else {
107: setIcon(unpressedIcon);
108: }
109: }
110: }
111:
112: /**
113: * {@inheritDoc}
114: */
115: public boolean isSelected() {
116: return isSelected;
117: }
118:
119: /**
120: * {@inheritDoc}
121: */
122: public void mousePressed(MouseEvent event) {
123: isMousePressed = true;
124: changeIcon();
125: }
126:
127: /**
128: * {@inheritDoc}
129: */
130: public void mouseReleased(MouseEvent event) {
131: isMousePressed = false;
132: changeIcon();
133: }
134:
135: /**
136: * {@inheritDoc}
137: */
138: public void mouseClicked(MouseEvent event) {
139: }
140:
141: /**
142: * {@inheritDoc}
143: */
144: public void mouseEntered(MouseEvent event) {
145: }
146:
147: /**
148: * {@inheritDoc}
149: */
150: public void mouseExited(MouseEvent event) {
151: }
152:
153: /**
154: * {@inheritDoc}
155: */
156: public void actionPerformed(ActionEvent event) {
157: setSelected(true);
158: }
159: }
|