1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54:
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68:
69: public class BasicButtonListener
70: implements MouseListener, MouseMotionListener, FocusListener, ChangeListener,
71: PropertyChangeListener
72: {
73:
76: private class ButtonAction
77: extends AbstractAction
78: {
79:
82: static final String PRESSED = "pressed";
83:
84:
87: static final String RELEASED = "released";
88:
89:
92: public void actionPerformed(ActionEvent event)
93: {
94: Object cmd = getValue("__command__");
95: AbstractButton b = (AbstractButton) event.getSource();
96: ButtonModel m = b.getModel();
97: if (PRESSED.equals(cmd))
98: {
99: m.setArmed(true);
100: m.setPressed(true);
101: if (! b.isFocusOwner())
102: b.requestFocus();
103: }
104: else if (RELEASED.equals(cmd))
105: {
106: m.setPressed(false);
107: m.setArmed(false);
108: }
109: }
110:
111:
118: public boolean isEnabled(Object source)
119: {
120: boolean enabled = true;
121: if (source instanceof AbstractButton)
122: {
123: AbstractButton b = (AbstractButton) source;
124: enabled = b.isEnabled();
125: }
126: return enabled;
127: }
128: }
129:
130: public BasicButtonListener(AbstractButton b)
131: {
132:
133: }
134:
135: public void propertyChange(PropertyChangeEvent e)
136: {
137:
138:
139: String property = e.getPropertyName();
140: AbstractButton b = (AbstractButton) e.getSource();
141: if ((property.equals(AbstractButton.TEXT_CHANGED_PROPERTY)
142: || property.equals("font"))
143: && SystemProperties.getProperty("gnu.javax.swing.noGraphics2D")
144: == null)
145: {
146: String text = b.getText();
147: if (text == null)
148: text = "";
149: FontRenderContext frc = new FontRenderContext(new AffineTransform(),
150: false, false);
151: TextLayout layout = new TextLayout(text, b.getFont(), frc);
152: b.putClientProperty(BasicGraphicsUtils.CACHED_TEXT_LAYOUT, layout);
153:
154:
155: BasicHTML.updateRenderer(b, b.getText());
156: }
157: else if (property.equals(AbstractButton.CONTENT_AREA_FILLED_CHANGED_PROPERTY))
158: {
159: checkOpacity(b);
160: }
161: }
162:
163:
169: protected void checkOpacity(AbstractButton b)
170: {
171: b.setOpaque(b.isContentAreaFilled());
172: }
173:
174: public void focusGained(FocusEvent e)
175: {
176: if (e.getSource() instanceof AbstractButton)
177: {
178: AbstractButton button = (AbstractButton) e.getSource();
179: if (button.isFocusPainted())
180: button.repaint();
181: }
182: }
183:
184: public void focusLost(FocusEvent e)
185: {
186: if (e.getSource() instanceof AbstractButton)
187: {
188: AbstractButton button = (AbstractButton) e.getSource();
189: if (button.isFocusPainted())
190: button.repaint();
191: }
192: }
193:
194: public void installKeyboardActions(JComponent c)
195: {
196: ButtonUI ui = ((AbstractButton) c).getUI();
197: if (ui instanceof BasicButtonUI)
198: {
199:
200: BasicButtonUI basicUI = (BasicButtonUI) ui;
201: String prefix = basicUI.getPropertyPrefix();
202: InputMap focusInputMap =
203: (InputMap) UIManager.get(prefix + "focusInputMap");
204: SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED,
205: focusInputMap);
206:
207: ActionMap am = (ActionMap) UIManager.get(prefix + "actionMap");
208: if (am == null)
209: {
210: am = createDefaultActionMap();
211: UIManager.put(prefix + "actionMap", am);
212: }
213: SwingUtilities.replaceUIActionMap(c, am);
214: }
215:
216: c.getActionMap().put("pressed",
217: new AbstractAction()
218: {
219: public void actionPerformed(ActionEvent e)
220: {
221: AbstractButton button = (AbstractButton) e.getSource();
222: ButtonModel model = button.getModel();
223:
224: model.setArmed(true);
225: model.setPressed(true);
226: }
227: });
228:
229: c.getActionMap().put("released",
230: new AbstractAction()
231: {
232: public void actionPerformed(ActionEvent e)
233: {
234: AbstractButton button = (AbstractButton) e.getSource();
235: ButtonModel model = button.getModel();
236:
237: model.setPressed(false);
238: model.setArmed(false);
239: }
240: });
241: }
242:
243:
248: private ActionMap createDefaultActionMap()
249: {
250: Action action = new ButtonAction();
251: ActionMapUIResource am = new ActionMapUIResource();
252: am.put(ButtonAction.PRESSED, action);
253: am.put(ButtonAction.RELEASED, action);
254: return am;
255: }
256:
257: public void uninstallKeyboardActions(JComponent c)
258: {
259: SwingUtilities.replaceUIActionMap(c, null);
260: SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED, null);
261: }
262:
263: public void stateChanged(ChangeEvent e)
264: {
265:
266: ((AbstractButton) e.getSource()).repaint();
267: }
268:
269: public void mouseMoved(MouseEvent e)
270: {
271:
272: }
273:
274: public void mouseDragged(MouseEvent e)
275: {
276:
277: }
278:
279: public void mouseClicked(MouseEvent e)
280: {
281:
282: }
283:
284:
289: public void mousePressed(MouseEvent e)
290: {
291: if (e.getSource() instanceof AbstractButton)
292: {
293: AbstractButton button = (AbstractButton) e.getSource();
294: ButtonModel model = button.getModel();
295: if (SwingUtilities.isLeftMouseButton(e))
296: {
297:
298: model.setArmed(true);
299: model.setPressed(true);
300:
301: if (! button.isFocusOwner() && button.isRequestFocusEnabled())
302: button.requestFocus();
303: }
304: }
305: }
306:
307:
314: public void mouseReleased(MouseEvent e)
315: {
316: if (e.getSource() instanceof AbstractButton)
317: {
318: AbstractButton button = (AbstractButton) e.getSource();
319: ButtonModel model = button.getModel();
320: if (e.getButton() == MouseEvent.BUTTON1)
321: {
322:
323: model.setPressed(false);
324: model.setArmed(false);
325: }
326: }
327: }
328:
329:
337: public void mouseEntered(MouseEvent e)
338: {
339: if (e.getSource() instanceof AbstractButton)
340: {
341: AbstractButton button = (AbstractButton) e.getSource();
342: ButtonModel model = button.getModel();
343: if (button.isRolloverEnabled()
344: && ! SwingUtilities.isLeftMouseButton(e))
345: model.setRollover(true);
346:
347: if (model.isPressed())
348: model.setArmed(true);
349: }
350: }
351:
352:
359: public void mouseExited(MouseEvent e)
360: {
361: if (e.getSource() instanceof AbstractButton)
362: {
363: AbstractButton button = (AbstractButton) e.getSource();
364: ButtonModel model = button.getModel();
365: if (button.isRolloverEnabled())
366: model.setRollover(false);
367: model.setArmed(false);
368: }
369: }
370: }