001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: ComponentProperties.java,v 1.17 2005/12/04 13:46:05 jesper Exp $
023: package net.infonode.properties.gui.util;
024:
025: import net.infonode.gui.InsetsUtil;
026: import net.infonode.gui.panel.BaseContainer;
027: import net.infonode.gui.panel.BaseContainerUtil;
028: import net.infonode.properties.propertymap.*;
029: import net.infonode.properties.types.BorderProperty;
030: import net.infonode.properties.types.ColorProperty;
031: import net.infonode.properties.types.FontProperty;
032: import net.infonode.properties.types.InsetsProperty;
033: import net.infonode.util.Direction;
034:
035: import javax.swing.*;
036: import javax.swing.border.Border;
037: import javax.swing.border.CompoundBorder;
038: import javax.swing.border.EmptyBorder;
039: import java.awt.*;
040:
041: /**
042: * Properties and property values for a {@link JComponent}.
043: *
044: * @author $Author: jesper $
045: * @version $Revision: 1.17 $
046: */
047: public class ComponentProperties extends PropertyMapContainer {
048: /**
049: * Property group for all component properties.
050: */
051: public static final PropertyMapGroup PROPERTIES = new PropertyMapGroup(
052: "Component Properties", "");
053:
054: /**
055: * Component border.
056: */
057: public static final BorderProperty BORDER = new BorderProperty(
058: PROPERTIES, "Border", "Component border.",
059: PropertyMapValueHandler.INSTANCE);
060:
061: /**
062: * Component insets inside the border.
063: */
064: public static final InsetsProperty INSETS = new InsetsProperty(
065: PROPERTIES, "Insets",
066: "Component insets inside the border.",
067: PropertyMapValueHandler.INSTANCE);
068:
069: /**
070: * Component foreground color.
071: */
072: public static final ColorProperty FOREGROUND_COLOR = new ColorProperty(
073: PROPERTIES, "Foreground Color",
074: "Component foreground color.",
075: PropertyMapValueHandler.INSTANCE);
076:
077: /**
078: * Component text font.
079: */
080: public static final FontProperty FONT = new FontProperty(
081: PROPERTIES, "Font", "Component text font.",
082: PropertyMapValueHandler.INSTANCE);
083:
084: /**
085: * Component background color. A null value means that no background will be painted.
086: */
087: public static final ColorProperty BACKGROUND_COLOR = new ColorProperty(
088: PROPERTIES,
089: "Background Color",
090: "Component background color. A null value means that no background will be painted.",
091: PropertyMapValueHandler.INSTANCE);
092:
093: static {
094: ComponentProperties properties = new ComponentProperties(
095: PROPERTIES.getDefaultMap());
096:
097: properties.setBackgroundColor(null).setBorder(null).setInsets(
098: null);
099: }
100:
101: /**
102: * Creates an empty property object.
103: */
104: public ComponentProperties() {
105: super (PROPERTIES);
106: }
107:
108: /**
109: * Creates a property map containing the map.
110: *
111: * @param map the property map
112: */
113: public ComponentProperties(PropertyMap map) {
114: super (map);
115: }
116:
117: /**
118: * Creates a property object that inherit values from another property object.
119: *
120: * @param inheritFrom the object from which to inherit property values
121: */
122: public ComponentProperties(ComponentProperties inheritFrom) {
123: super (PropertyMapFactory.create(inheritFrom.getMap()));
124: }
125:
126: /**
127: * Adds a super object from which property values are inherited.
128: *
129: * @param properties the object from which to inherit property values
130: * @return this
131: */
132: public ComponentProperties addSuperObject(
133: ComponentProperties properties) {
134: getMap().addSuperMap(properties.getMap());
135:
136: return this ;
137: }
138:
139: /**
140: * Removes the last added super object.
141: *
142: * @return this
143: */
144: public ComponentProperties removeSuperObject() {
145: getMap().removeSuperMap();
146: return this ;
147: }
148:
149: /**
150: * Removes the given super object.
151: *
152: * @param superObject super object to remove
153: * @return this
154: */
155: public ComponentProperties removeSuperObject(
156: ComponentProperties super Object) {
157: getMap().removeSuperMap(super Object.getMap());
158: return this ;
159: }
160:
161: /**
162: * Sets the component border.
163: *
164: * @param border the component border
165: * @return this
166: */
167: public ComponentProperties setBorder(Border border) {
168: BORDER.set(getMap(), border);
169:
170: return this ;
171: }
172:
173: /**
174: * Sets the component insets inside the border.
175: *
176: * @param insets the component insets
177: * @return this
178: */
179: public ComponentProperties setInsets(Insets insets) {
180: INSETS.set(getMap(), insets);
181:
182: return this ;
183: }
184:
185: /**
186: * Sets the component background color.
187: *
188: * @param color the background color, null means no background
189: * @return this
190: */
191: public ComponentProperties setBackgroundColor(Color color) {
192: BACKGROUND_COLOR.set(getMap(), color);
193:
194: return this ;
195: }
196:
197: /**
198: * Returns the component insets inside the border.
199: *
200: * @return the component insets inside the border
201: */
202: public Insets getInsets() {
203: return INSETS.get(getMap());
204: }
205:
206: /**
207: * Returns the component border.
208: *
209: * @return the component border
210: */
211: public Border getBorder() {
212: return BORDER.get(getMap());
213: }
214:
215: /**
216: * Returns the component background color.
217: *
218: * @return the component background color
219: */
220: public Color getBackgroundColor() {
221: return BACKGROUND_COLOR.get(getMap());
222: }
223:
224: /**
225: * Returns the component text font.
226: *
227: * @return the component text font
228: */
229: public Font getFont() {
230: return FONT.get(getMap());
231: }
232:
233: /**
234: * Returns the component foreground color.
235: *
236: * @return the component foreground color
237: */
238: public Color getForegroundColor() {
239: return FOREGROUND_COLOR.get(getMap());
240: }
241:
242: /**
243: * Sets the component foreground color.
244: *
245: * @param foregroundColor the component foreground color
246: * @return this
247: */
248: public ComponentProperties setForegroundColor(Color foregroundColor) {
249: FOREGROUND_COLOR.set(getMap(), foregroundColor);
250: return this ;
251: }
252:
253: /**
254: * Sets the component text font.
255: *
256: * @param font the component text font
257: * @return this
258: */
259: public ComponentProperties setFont(Font font) {
260: FONT.set(getMap(), font);
261: return this ;
262: }
263:
264: /**
265: * Applies the property values to a component.
266: *
267: * @param component the component on which to apply the property values
268: */
269: public void applyTo(JComponent component) {
270: applyTo(component, Direction.RIGHT);
271: }
272:
273: /**
274: * Applies the property values to a component and rotates the insets in the
275: * given direction.
276: *
277: * @param component the component on which to apply the property values
278: * @param insetsDirection insets direction
279: */
280: public void applyTo(JComponent component, Direction insetsDirection) {
281: Insets insets = getInsets() == null ? null : InsetsUtil.rotate(
282: insetsDirection, getInsets());
283: Border innerBorder = (insets == null) ? null : new EmptyBorder(
284: insets);
285: component
286: .setBorder((getBorder() == null) ? innerBorder
287: : ((innerBorder == null) ? getBorder()
288: : new CompoundBorder(getBorder(),
289: innerBorder)));
290: //component.setOpaque(getBackgroundColor() != null);
291:
292: if (component instanceof BaseContainer) {
293: BaseContainer c = (BaseContainer) component;
294: BaseContainerUtil.setOverridedBackground(c,
295: getBackgroundColor());
296: BaseContainerUtil.setOverridedForeground(c,
297: getForegroundColor());
298: BaseContainerUtil.setOverridedFont(c, getFont());
299: } else {
300: component.setBackground(getBackgroundColor());
301: component.setFont(getFont());
302: component.setForeground(getForegroundColor());
303: }
304: }
305: }
|