001: /**
002: * @(#)BasicGripperUI.java
003: *
004: * Copyright 2002 - 2004 JIDE Software. All rights reserved.
005: */package com.jidesoft.plaf.basic;
006:
007: import com.jidesoft.plaf.GripperUI;
008: import com.jidesoft.plaf.UIDefaultsLookup;
009: import com.jidesoft.swing.Gripper;
010: import com.jidesoft.swing.JideSwingUtilities;
011: import com.jidesoft.utils.SecurityUtils;
012:
013: import javax.swing.*;
014: import javax.swing.plaf.ComponentUI;
015: import java.awt.*;
016: import java.awt.event.MouseAdapter;
017: import java.awt.event.MouseEvent;
018: import java.awt.event.MouseListener;
019:
020: /**
021: * A Basic L&F implementation of GripperUI.
022: */
023: public class BasicGripperUI extends GripperUI {
024: private int _size;
025:
026: protected ThemePainter _painter;
027:
028: protected Painter _gripperPainter;
029:
030: public static ComponentUI createUI(JComponent c) {
031: return new BasicGripperUI();
032: }
033:
034: @Override
035: public void installUI(JComponent c) {
036: _painter = (ThemePainter) UIDefaultsLookup.get("Theme.painter");
037: _gripperPainter = (Painter) UIDefaultsLookup
038: .get("Gripper.painter");
039: installDefaults((Gripper) c);
040: installListeners((Gripper) c);
041: }
042:
043: @Override
044: public void uninstallUI(JComponent c) {
045: _painter = null;
046: _gripperPainter = null;
047: uninstallDefaults((Gripper) c);
048: uninstallListeners((Gripper) c);
049: }
050:
051: protected void installDefaults(Gripper s) {
052: _size = UIDefaultsLookup.getInt("Gripper.size");
053: }
054:
055: protected void uninstallDefaults(Gripper s) {
056: }
057:
058: protected MouseListener createMouseListener() {
059: return new GripperMouseListener();
060: }
061:
062: protected void installListeners(Gripper g) {
063: MouseListener listener = createMouseListener();
064: if (listener != null) {
065: // put the listener in the gripper's client properties so that
066: // we can get at it later
067: g.putClientProperty(this , listener);
068: g.addMouseListener(listener);
069: }
070: }
071:
072: protected void uninstallListeners(Gripper g) {
073: MouseListener listener = (MouseListener) g
074: .getClientProperty(this );
075: g.putClientProperty(this , null);
076: if (listener != null) {
077: g.removeMouseListener(listener);
078: }
079: }
080:
081: protected void paintBackground(Graphics g, Gripper b) {
082: Rectangle rect = new Rectangle(0, 0, b.getWidth(), b
083: .getHeight());
084: if (b.isRollover()) {
085: getPainter().paintButtonBackground(b, g, rect, 0,
086: ThemePainter.STATE_ROLLOVER);
087: } else {
088: if (b.isOpaque()) {
089: getPainter().paintButtonBackground(
090: b,
091: g,
092: rect,
093: 0,
094: b.isSelected() ? ThemePainter.STATE_SELECTED
095: : ThemePainter.STATE_DEFAULT, false);
096: if ("true".equals(SecurityUtils.getProperty(
097: "shadingtheme", "false"))) {
098: JideSwingUtilities.fillGradient(g, rect,
099: SwingConstants.HORIZONTAL);
100: }
101: }
102: }
103: }
104:
105: @Override
106: public void paint(Graphics g, JComponent c) {
107: Gripper gripper = (Gripper) c;
108: paintBackground(g, gripper);
109: int state = gripper.isSelected() ? ThemePainter.STATE_SELECTED
110: : ThemePainter.STATE_DEFAULT;
111: if (_gripperPainter == null) {
112: getPainter().paintGripper(c, g,
113: new Rectangle(0, 0, c.getWidth(), c.getHeight()),
114: gripper.getOrientation(), state);
115: } else {
116: _gripperPainter.paint(c, g, new Rectangle(0, 0, c
117: .getWidth(), c.getHeight()), gripper
118: .getOrientation(), state);
119: }
120: }
121:
122: @Override
123: public Dimension getPreferredSize(JComponent c) {
124: return new Dimension(_size, _size);
125: }
126:
127: @Override
128: public Dimension getMinimumSize(JComponent c) {
129: return getPreferredSize(c);
130: }
131:
132: @Override
133: public Dimension getMaximumSize(JComponent c) {
134: Gripper gripper = (Gripper) c;
135: if (gripper.getOrientation() == SwingConstants.HORIZONTAL)
136: return new Dimension(_size, c.getParent().getHeight());
137: else
138: return new Dimension(c.getParent().getWidth(), _size);
139: }
140:
141: public ThemePainter getPainter() {
142: return _painter;
143: }
144:
145: class GripperMouseListener extends MouseAdapter {
146: @Override
147: public void mouseEntered(MouseEvent e) {
148: super .mouseEntered(e);
149: if (e.getSource() instanceof Gripper) {
150: if (((Gripper) e.getSource()).isRolloverEnabled()) {
151: ((Gripper) e.getSource()).setRollover(true);
152: }
153: }
154: }
155:
156: @Override
157: public void mouseExited(MouseEvent e) {
158: super .mouseExited(e);
159: if (e.getSource() instanceof Gripper) {
160: if (((Gripper) e.getSource()).isRolloverEnabled()) {
161: ((Gripper) e.getSource()).setRollover(false);
162: }
163: }
164: }
165: }
166: }
|