001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: */package javax.swing.plaf.basic;
021:
022: import java.awt.event.ActionEvent;
023: import java.awt.event.FocusEvent;
024: import java.awt.event.FocusListener;
025: import java.awt.event.MouseEvent;
026: import java.awt.event.MouseListener;
027: import java.awt.event.MouseMotionListener;
028: import java.beans.PropertyChangeEvent;
029: import java.beans.PropertyChangeListener;
030:
031: import javax.swing.AbstractAction;
032: import javax.swing.AbstractButton;
033: import javax.swing.Action;
034: import javax.swing.ActionMap;
035: import javax.swing.ButtonModel;
036: import javax.swing.JComponent;
037: import javax.swing.LookAndFeel;
038: import javax.swing.SwingUtilities;
039: import javax.swing.event.ChangeEvent;
040: import javax.swing.event.ChangeListener;
041: import javax.swing.plaf.ActionMapUIResource;
042:
043: import org.apache.harmony.x.swing.StringConstants;
044:
045: public class BasicButtonListener implements MouseListener,
046: MouseMotionListener, FocusListener, ChangeListener,
047: PropertyChangeListener {
048:
049: private static final class PressButtonAction extends AbstractAction {
050: public static void press(final AbstractButton button) {
051: if (button.isEnabled()) {
052: final ButtonModel model = button.getModel();
053: model.setArmed(true);
054: model.setPressed(true);
055: button.requestFocusInWindow();
056: }
057: }
058:
059: public void actionPerformed(final ActionEvent event) {
060: press((AbstractButton) event.getSource());
061: }
062: };
063:
064: private static final class ReleaseButtonAction extends
065: AbstractAction {
066: public static void release(final AbstractButton button) {
067: if (button.isEnabled()) {
068: final ButtonModel model = button.getModel();
069: model.setPressed(false);
070: model.setArmed(false);
071: }
072: }
073:
074: public void actionPerformed(final ActionEvent event) {
075: release((AbstractButton) event.getSource());
076: }
077: };
078:
079: private static final class MnemonicAction extends AbstractAction {
080: public void actionPerformed(final ActionEvent event) {
081: final AbstractButton button = (AbstractButton) event
082: .getSource();
083: if (button.isEnabled()) {
084: button.requestFocusInWindow();
085: button.doClick();
086: }
087: }
088: };
089:
090: private static final Action PRESS_ACTION = new PressButtonAction();
091: private static final Action RELEASE_ACTION = new ReleaseButtonAction();
092: private static final Action MNEMONIC_ACTION = new MnemonicAction();
093:
094: private long previousPressTime;
095: private final AbstractButton button;
096:
097: public BasicButtonListener(final AbstractButton button) {
098: this .button = button;
099: }
100:
101: public void stateChanged(final ChangeEvent event) {
102: button.repaint();
103: }
104:
105: public void propertyChange(final PropertyChangeEvent event) {
106: if (AbstractButton.CONTENT_AREA_FILLED_CHANGED_PROPERTY
107: .equals(event.getPropertyName())) {
108: LookAndFeel.installProperty(button,
109: StringConstants.OPAQUE_PROPERTY, event
110: .getNewValue());
111: }
112:
113: button.revalidate();
114: button.repaint();
115: }
116:
117: public void uninstallKeyboardActions(final JComponent c) {
118: SwingUtilities.replaceUIActionMap(c, null);
119: }
120:
121: public void installKeyboardActions(final JComponent c) {
122: ActionMap actionMap = new ActionMapUIResource();
123: actionMap.put(StringConstants.BUTTON_PRESSED_ACTION,
124: PRESS_ACTION);
125: actionMap.put(StringConstants.BUTTON_RELEASED_ACTION,
126: RELEASE_ACTION);
127: actionMap.put(StringConstants.MNEMONIC_ACTION, MNEMONIC_ACTION);
128: SwingUtilities.replaceUIActionMap(c, actionMap);
129: }
130:
131: public void mouseReleased(final MouseEvent event) {
132: if (event.getButton() == MouseEvent.BUTTON1) {
133: ReleaseButtonAction.release(button);
134: }
135: }
136:
137: public void mousePressed(final MouseEvent event) {
138: if ((event.getButton() == MouseEvent.BUTTON1)
139: && mouseInside(event) && isMultiClickTimePassed(event)) {
140: PressButtonAction.press(button);
141: }
142: previousPressTime = event.getWhen();
143: }
144:
145: public void mouseExited(final MouseEvent event) {
146: final ButtonModel model = button.getModel();
147: if (model.isEnabled()) {
148: if (button.isRolloverEnabled()) {
149: model.setRollover(false);
150: }
151: model.setArmed(false);
152: }
153: }
154:
155: public void mouseEntered(final MouseEvent event) {
156: final ButtonModel model = button.getModel();
157: if (button.isRolloverEnabled()) {
158: model.setRollover(true);
159: }
160: if (model.isEnabled() && model.isPressed()) {
161: model.setArmed(true);
162: }
163: }
164:
165: public void mouseDragged(final MouseEvent event) {
166: }
167:
168: public void mouseClicked(final MouseEvent event) {
169: }
170:
171: public void mouseMoved(final MouseEvent event) {
172: }
173:
174: protected void checkOpacity(final AbstractButton button) {
175: }
176:
177: public void focusLost(final FocusEvent event) {
178: if (button.isEnabled()) {
179: final ButtonModel model = button.getModel();
180: if (model.isEnabled()) {
181: model.setArmed(false);
182: model.setPressed(false);
183: }
184: button.repaint();
185: }
186: }
187:
188: public void focusGained(final FocusEvent event) {
189: if (button.isEnabled()) {
190: button.repaint();
191: }
192: }
193:
194: private boolean mouseInside(final MouseEvent event) {
195: return button.contains(event.getPoint());
196: }
197:
198: private boolean isMultiClickTimePassed(final MouseEvent event) {
199: return (event.getWhen() - previousPressTime) > button
200: .getMultiClickThreshhold();
201: }
202: }
|