001: /*
002: * @(#)BasicHeaderBoxUI.java 4/27/2006
003: *
004: * Copyright 2002 - 2006 JIDE Software Inc. All rights reserved.
005: */
006:
007: package com.jidesoft.plaf.basic;
008:
009: import com.jidesoft.plaf.HeaderBoxUI;
010: import com.jidesoft.plaf.UIDefaultsLookup;
011: import com.jidesoft.swing.HeaderBox;
012:
013: import javax.swing.*;
014: import javax.swing.border.Border;
015: import javax.swing.event.MouseInputAdapter;
016: import javax.swing.plaf.ComponentUI;
017: import java.awt.*;
018: import java.awt.event.MouseEvent;
019: import java.awt.event.MouseMotionListener;
020:
021: /**
022: * JideButtonUI implementation
023: */
024: public class BasicHeaderBoxUI extends HeaderBoxUI {
025: // Shared UI object
026: private static HeaderBoxUI _headerBoxUI;
027: private Border _border;
028:
029: public static ComponentUI createUI(JComponent c) {
030: if (_headerBoxUI == null) {
031: _headerBoxUI = new BasicHeaderBoxUI();
032: }
033: return _headerBoxUI;
034: }
035:
036: @Override
037: public void installUI(JComponent c) {
038: HeaderBox p = (HeaderBox) c;
039: super .installUI(p);
040: installDefaults(p);
041: installListeners(p);
042: }
043:
044: @Override
045: public void uninstallUI(JComponent c) {
046: HeaderBox p = (HeaderBox) c;
047: uninstallDefaults(p);
048: uninstallListeners(p);
049: super .uninstallUI(c);
050: }
051:
052: protected class RolloverMouseInputAdapter extends MouseInputAdapter {
053: private long lastPressedTimestamp = -1;
054: private boolean shouldDiscardRelease = false;
055:
056: @Override
057: public void mousePressed(MouseEvent e) {
058: if (SwingUtilities.isLeftMouseButton(e)) {
059: AbstractButton b = (AbstractButton) e.getSource();
060:
061: if (b.contains(e.getX(), e.getY())) {
062: long multiClickThreshhold = b
063: .getMultiClickThreshhold();
064: long lastTime = lastPressedTimestamp;
065: long currentTime = lastPressedTimestamp = e
066: .getWhen();
067: if (lastTime != -1
068: && currentTime - lastTime < multiClickThreshhold) {
069: shouldDiscardRelease = true;
070: return;
071: }
072:
073: ButtonModel model = b.getModel();
074: if (!model.isEnabled()) {
075: // Disabled buttons ignore all input...
076: return;
077: }
078: if (!model.isArmed()) {
079: // button not armed, should be
080: model.setArmed(true);
081: }
082: model.setPressed(true);
083: if (!b.hasFocus() && b.isRequestFocusEnabled()) {
084: b.requestFocus();
085: }
086: }
087: }
088: }
089:
090: @Override
091: public void mouseReleased(MouseEvent e) {
092: if (SwingUtilities.isLeftMouseButton(e)) {
093: // Support for multiClickThreshhold
094: if (shouldDiscardRelease) {
095: shouldDiscardRelease = false;
096: return;
097: }
098: AbstractButton b = (AbstractButton) e.getSource();
099: ButtonModel model = b.getModel();
100: model.setPressed(false);
101: model.setArmed(false);
102: }
103: }
104:
105: @Override
106: public void mouseEntered(MouseEvent e) {
107: AbstractButton b = (AbstractButton) e.getSource();
108: ButtonModel model = b.getModel();
109: if (b.isRolloverEnabled()
110: && !SwingUtilities.isLeftMouseButton(e)) {
111: model.setRollover(true);
112: }
113: if (model.isPressed())
114: model.setArmed(true);
115: }
116:
117: @Override
118: public void mouseExited(MouseEvent e) {
119: AbstractButton b = (AbstractButton) e.getSource();
120: ButtonModel model = b.getModel();
121: if (b.isRolloverEnabled() && !b.contains(e.getPoint())) {
122: model.setRollover(false);
123: }
124: model.setArmed(false);
125: }
126: }
127:
128: protected void installListeners(JComponent c) {
129: MouseInputAdapter l = createHeaderBoxMouseListener();
130: c.addMouseListener(l);
131: }
132:
133: /**
134: * Returns the ButtonListener for the passed in Button, or null if one
135: * could not be found.
136: */
137: private RolloverMouseInputAdapter getMouseListener(HeaderBox b) {
138: MouseMotionListener[] listeners = b.getMouseMotionListeners();
139:
140: if (listeners != null) {
141: for (int counter = 0; counter < listeners.length; counter++) {
142: if (listeners[counter] instanceof RolloverMouseInputAdapter) {
143: return (RolloverMouseInputAdapter) listeners[counter];
144: }
145: }
146: }
147: return null;
148: }
149:
150: private RolloverMouseInputAdapter createHeaderBoxMouseListener() {
151: return new RolloverMouseInputAdapter();
152: }
153:
154: protected void uninstallListeners(JComponent c) {
155: HeaderBox b = (HeaderBox) c;
156: RolloverMouseInputAdapter listener = getMouseListener(b);
157: if (listener != null) {
158: b.removeMouseListener(listener);
159: }
160: }
161:
162: protected void installDefaults(HeaderBox p) {
163: LookAndFeel.installColorsAndFont(p, "Panel.background",
164: "Panel.foreground", "Panel.font");
165: LookAndFeel.installBorder(p, "Panel.border");
166: p.setOpaque(true);
167: _border = UIDefaultsLookup
168: .getBorder("NestedTableHeader.cellBorder");
169: }
170:
171: @Override
172: public void paint(Graphics g, JComponent c) {
173: super .paint(g, c);
174: paintBackground(g, c);
175: paintBorder(g, c);
176: }
177:
178: protected void paintBorder(Graphics g, JComponent c) {
179: if (_border != null) {
180: _border
181: .paintBorder(c, g, 0, 0, c.getWidth(), c
182: .getHeight());
183: }
184: }
185:
186: protected void paintBackground(Graphics g, JComponent c) {
187: }
188:
189: protected void uninstallDefaults(HeaderBox p) {
190: LookAndFeel.uninstallBorder(p);
191: _border = null;
192: }
193: }
|