001: /*
002: * @(#)VisualMargin.java 2.2 2005-10-01
003: *
004: * Copyright (c) 2004-2005 Werner Randelshofer
005: * Staldenmattweg 2, Immensee, CH-6405, Switzerland.
006: * All rights reserved.
007: *
008: * This software is the confidential and proprietary information of
009: * Werner Randelshofer. ("Confidential Information"). You shall not
010: * disclose such Confidential Information and shall use it only in
011: * accordance with the terms of the license agreement you entered into
012: * with Werner Randelshofer.
013: */
014:
015: package contrib.ch.randelshofer.quaqua;
016:
017: import java.awt.*;
018: import javax.swing.*;
019: import javax.swing.border.*;
020: import javax.swing.plaf.*;
021:
022: /**
023: * The VisualMargin is used to visually align components using bounds
024: * based on other criterias than the clip bounds of the component.
025: *
026: * For example: The clip bounds of a JButton includes its cast shadow and its
027: * focus ring. When we align the JButton with a JLabel, we want to align the
028: * baseline of the Text of the JButton with the text in the JLabel.
029: *
030: * The visual margin may be quite large. We allow to programmatically set a
031: * smaller margin using the client property "Quaqua.Component.margin".
032: *
033: * @author Werner Randelshofer
034: * @version 2.2 2005-10-01 Added method getVisualMargin.
035: * <br>2.1 2005-06-21 Implements UIResource.
036: * <br>2.0 2005-05-08 Renamed from BorderMargin to VisualMargin. Reworked
037: * API.
038: * <br>1.0 31 March 2005 Created.
039: */
040: public class VisualMargin extends AbstractBorder implements UIResource {
041: /**
042: * Defines the margin from the clip bounds of the
043: * component to its visually perceived borderline.
044: */
045: private Insets layoutMargin;
046:
047: /**
048: * The UIManager Property to be used for the default margin.
049: */
050: private String uiManagerPropertyName = "Component.visualMargin";
051: /**
052: * The Client Property to be used for the default margin.
053: */
054: private String propertyName = "Quaqua.Component.visualMargin";
055:
056: private boolean isTopFixed, isLeftFixed, isBottomFixed,
057: isRightFixed;
058:
059: /**
060: * Creates a new VisualMargin.
061: */
062: public VisualMargin() {
063: layoutMargin = new Insets(0, 0, 0, 0);
064: }
065:
066: /**
067: * The UIManager Property to be used for the default margin.
068: */
069: public void setPropertyName(String propertyName) {
070: // this.propertyName = propertyName;
071: }
072:
073: /*
074: * Specifies SwingConstants.TOP, LEFT, BOTTOM, RIGHT to be fixed.
075: * Set to false to unfix.
076: */
077: public void setFixed(boolean top, boolean left, boolean bottom,
078: boolean right) {
079: isTopFixed = top;
080: isLeftFixed = left;
081: isBottomFixed = bottom;
082: isRightFixed = right;
083: }
084:
085: /**
086: * Creates a new VisualMargin.
087: *
088: * @param top Defines the margin from the clip bounds of the
089: * component to its visual bounds.
090: * @param left Defines the margin from the clip bounds of the
091: * component to its visual bounds.
092: * @param bottom Defines the margin from the clip bounds of the
093: * component to its visual bounds.
094: * @param right Defines the margin from the clip bounds of the
095: * component to its visual bounds.
096: */
097: public VisualMargin(int top, int left, int bottom, int right) {
098: layoutMargin = new Insets(top, left, bottom, right);
099: }
100:
101: public VisualMargin(int top, int left, int bottom, int right,
102: boolean ftop, boolean fleft, boolean fbottom, boolean fright) {
103: layoutMargin = new Insets(top, left, bottom, right);
104: isTopFixed = ftop;
105: isLeftFixed = fleft;
106: isBottomFixed = fbottom;
107: isRightFixed = fright;
108: }
109:
110: public VisualMargin(boolean ftop, boolean fleft, boolean fbottom,
111: boolean fright) {
112: layoutMargin = new Insets(0, 0, 0, 0);
113: isTopFixed = ftop;
114: isLeftFixed = fleft;
115: isBottomFixed = fbottom;
116: isRightFixed = fright;
117: }
118:
119: /**
120: * Creates a new VisualMargin.
121: *
122: * @param layoutMargin Defines the margin from the clip bounds of the
123: * component to its visual bounds. The margin has usually negative values!
124: */
125: public VisualMargin(Insets layoutMargin) {
126: this .layoutMargin = layoutMargin;
127: }
128:
129: public Insets getVisualMargin(Component c) {
130: Insets insets = new Insets(layoutMargin.top, layoutMargin.left,
131: layoutMargin.bottom, layoutMargin.right);
132:
133: if (c instanceof JComponent) {
134: Insets componentMargin = (Insets) ((JComponent) c)
135: .getClientProperty(propertyName);
136: if (componentMargin == null && propertyName != null) {
137: componentMargin = UIManager
138: .getInsets(uiManagerPropertyName);
139: }
140: if (componentMargin != null) {
141: if (!isTopFixed)
142: insets.top = componentMargin.top;
143: if (!isLeftFixed)
144: insets.left = componentMargin.left;
145: if (!isBottomFixed)
146: insets.bottom = componentMargin.bottom;
147: if (!isRightFixed)
148: insets.right = componentMargin.right;
149: }
150: }
151: return insets;
152: }
153:
154: public Insets getBorderInsets(Component c) {
155: return getBorderInsets(c, new Insets(0, 0, 0, 0));
156: }
157:
158: /**
159: * Reinitializes the insets parameter with this Border's current Insets.
160: * @param c the component for which this border insets value applies
161: * @param insets the object to be reinitialized
162: * @return the <code>insets</code> object
163: */
164: public Insets getBorderInsets(Component c, Insets insets) {
165: return getVisualMargin(c, insets);
166: }
167:
168: /**
169: * Reinitializes the insets parameter with this Border's current Insets.
170: * @param c the component for which this border insets value applies
171: * @param insets the object to be reinitialized
172: * @return the <code>insets</code> object
173: */
174: protected Insets getVisualMargin(Component c, Insets insets) {
175: insets.top = -layoutMargin.top;
176: insets.left = -layoutMargin.left;
177: insets.bottom = -layoutMargin.bottom;
178: insets.right = -layoutMargin.right;
179:
180: if (c instanceof JComponent) {
181: Insets componentMargin = (Insets) ((JComponent) c)
182: .getClientProperty(propertyName);
183: if (componentMargin == null && propertyName != null) {
184: componentMargin = UIManager
185: .getInsets(uiManagerPropertyName);
186: }
187: if (componentMargin != null) {
188: if (!isTopFixed)
189: insets.top += componentMargin.top;
190: if (!isLeftFixed)
191: insets.left += componentMargin.left;
192: if (!isBottomFixed)
193: insets.bottom += componentMargin.bottom;
194: if (!isRightFixed)
195: insets.right += componentMargin.right;
196: }
197: }
198: return insets;
199: }
200: }
|