001    /*
002     * $Id: LabelProperties.java,v 1.4 2005/10/26 14:29:54 kleopatra Exp $
003     *
004     * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005     * Santa Clara, California 95054, U.S.A. All rights reserved.
006     *
007     * This library is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU Lesser General Public
009     * License as published by the Free Software Foundation; either
010     * version 2.1 of the License, or (at your option) any later version.
011     * 
012     * This library is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015     * Lesser General Public License for more details.
016     * 
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this library; if not, write to the Free Software
019     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020     */
021    
022    package org.jdesktop.swingx;
023    
024    import java.awt.Color;
025    import java.awt.Font;
026    import java.beans.PropertyChangeEvent;
027    import java.beans.PropertyChangeListener;
028    
029    import javax.swing.AbstractButton;
030    import javax.swing.Icon;
031    import javax.swing.JLabel;
032    import javax.swing.table.TableCellRenderer;
033    
034    /**
035     * Class used to store label properties in a single object so that they
036     * may be applied as a set on renderers.
037     * @author Amy Fowler
038     * @version 1.0
039     */
040    
041    public class LabelProperties extends JLabel {
042        private static final int BACKGROUND_SET = 1;
043        private static final int FOREGROUND_SET = 2;
044        private static final int FONT_SET = 4;
045        private static final int HORIZONTAL_ALIGNMENT_SET = 8;
046        private static final int HORIZONTAL_TEXT_POSITION_SET = 16;
047        private static final int ICON_SET = 32;
048        private static final int ICON_TEXT_GAP_SET = 64;
049        private static final int TEXT_SET = 128;
050        private static final int VERTICAL_ALIGNMENT_SET = 256;
051        private static final int VERTICAL_TEXT_POSITION_SET = 512;
052    
053        private int setFlags = 0;
054    
055        public LabelProperties() {
056            super();
057            addPropertyChangeListener(new PropertyChangeListener() {
058                public void propertyChange(PropertyChangeEvent e) {
059                    String propertyName = e.getPropertyName();
060                    Object value = e.getNewValue();
061                    if (propertyName.equals("background")) {
062                        if (value != null) {
063                            setFlags |= BACKGROUND_SET;
064                        } else {
065                            setFlags &= (~BACKGROUND_SET);
066                        }
067                    }
068                    else if (propertyName.equals("font")) {
069                        if (value != null) {
070                            setFlags |= FONT_SET;
071                        } else {
072                            setFlags &= (~FONT_SET);
073                        }
074                    }
075                    else if (propertyName.equals("foreground")) {
076                        if (value != null) {
077                            setFlags |= FOREGROUND_SET;
078                        } else {
079                            setFlags &= (~FOREGROUND_SET);
080                        }
081                    }
082                    else if (propertyName.equals("horizontalAlignment")) {
083                        if (value != null && ((Integer)value).intValue() != -1) {
084                            setFlags |= HORIZONTAL_ALIGNMENT_SET;
085                        } else {
086                            setFlags &= (~HORIZONTAL_ALIGNMENT_SET);
087                        }
088                    }
089                    else if (propertyName.equals("horizontalTextPosition")) {
090                        if (value != null && ((Integer)value).intValue() != -1) {
091                            setFlags |= HORIZONTAL_TEXT_POSITION_SET;
092                        } else {
093                            setFlags &= (~HORIZONTAL_TEXT_POSITION_SET);
094                        }
095                    }
096                    else if (propertyName.equals("icon")) {
097                        if (value != null) {
098                            setFlags |= ICON_SET;
099                        } else {
100                            setFlags &= (~ICON_SET);
101                        }
102                    }
103                    else if (propertyName.equals("iconTextGap")) {
104                        if (value != null && ((Integer)value).intValue() != -1) {
105                            setFlags |= ICON_TEXT_GAP_SET;
106                        } else {
107                            setFlags &= (~ICON_TEXT_GAP_SET);
108                        }
109                    }
110                    else if (propertyName.equals("text")) {
111                        if (value != null) {
112                            setFlags |= TEXT_SET;
113                        } else {
114                            setFlags &= (~TEXT_SET);
115                        }
116                    }
117                    else if (propertyName.equals("verticalAlignment")) {
118                        if (value != null && ((Integer)value).intValue() != -1) {
119                            setFlags |= VERTICAL_ALIGNMENT_SET;
120                        } else {
121                            setFlags &= (~VERTICAL_ALIGNMENT_SET);
122                        }
123                    }
124                    else if (propertyName.equals("verticalTextPosition")) {
125                        if (value != null && ((Integer)value).intValue() != -1) {
126                            setFlags |= VERTICAL_TEXT_POSITION_SET;
127                        } else {
128                            setFlags &= (~VERTICAL_TEXT_POSITION_SET);
129                        }
130                    }
131                }
132            });
133        }
134    
135        public LabelProperties(Color background, Color foreground, Font font,
136                               int horizontalAlignment, int horizontalTextPosition,
137                               int verticalAlignment, int verticalTextPosition,
138                               Icon icon, int iconTextGap, String text) {
139            this();
140            setBackground(background);
141            setForeground(foreground);
142            setFont(font);
143            setHorizontalAlignment(horizontalAlignment);
144            setHorizontalTextPosition(horizontalTextPosition);
145            setVerticalAlignment(verticalAlignment);
146            setVerticalTextPosition(verticalTextPosition);
147            setIcon(icon);
148            setIconTextGap(iconTextGap);
149            setText(text);
150        }
151    
152        public boolean isBackgroundSet() {
153            return (setFlags & BACKGROUND_SET) > 0;
154        }
155    
156        public boolean isForegroundSet() {
157            return (setFlags & FOREGROUND_SET) > 0;
158        }
159    
160        public boolean isFontSet() {
161            return (setFlags & FONT_SET) > 0;
162        }
163    
164        public boolean isHorizontalAlignmentSet() {
165            return (setFlags & HORIZONTAL_ALIGNMENT_SET) > 0;
166        }
167    
168        public boolean isHorizontalTextPositionSet() {
169            return (setFlags & HORIZONTAL_TEXT_POSITION_SET) > 0;
170        }
171    
172        public boolean isIconSet() {
173            return (setFlags & ICON_SET) > 0;
174        }
175    
176        public boolean isIconTextGapSet() {
177            return (setFlags & ICON_TEXT_GAP_SET) > 0;
178        }
179    
180        public boolean isTextSet() {
181            return (setFlags & TEXT_SET) > 0;
182        }
183    
184        public boolean isVerticalAlignmentSet() {
185            return (setFlags & VERTICAL_ALIGNMENT_SET) > 0;
186        }
187    
188        public boolean isVerticalTextPositionSet() {
189            return (setFlags & VERTICAL_TEXT_POSITION_SET) > 0;
190        }
191    
192        public boolean noPropertiesSet() {
193            return setFlags == 0;
194        }
195    
196        public void applyPropertiesTo(JLabel label) {
197            if (noPropertiesSet()) {
198                return;
199            }
200            if (isBackgroundSet()) {
201                label.setBackground(getBackground());
202            }
203            if (isForegroundSet()) {
204                label.setForeground(getForeground());
205            }
206            if (isFontSet()) {
207                label.setFont(getFont());
208            }
209            if (isHorizontalAlignmentSet()) {
210                label.setHorizontalAlignment(getHorizontalAlignment());
211            }
212            if (isHorizontalTextPositionSet()) {
213                label.setHorizontalTextPosition(getHorizontalTextPosition());
214            }
215            if (isIconSet()) {
216                label.setIcon(getIcon());
217            }
218            if (isIconTextGapSet()) {
219                label.setIconTextGap(getIconTextGap());
220            }
221            if (isTextSet()) {
222                label.setText(getText());
223            }
224            if (isVerticalAlignmentSet()) {
225                label.setVerticalAlignment(getVerticalAlignment());
226            }
227            if (isVerticalTextPositionSet()) {
228                label.setVerticalTextPosition(getVerticalTextPosition());
229            }
230        }
231    
232        public void applyPropertiesTo(AbstractButton button) {
233             if (noPropertiesSet()) {
234                 return;
235             }
236             if (isBackgroundSet()) {
237                 button.setBackground(getBackground());
238             }
239             if (isForegroundSet()) {
240                 button.setForeground(getForeground());
241             }
242             if (isFontSet()) {
243                 button.setFont(getFont());
244             }
245             if (isHorizontalAlignmentSet()) {
246                 button.setHorizontalAlignment(getHorizontalAlignment());
247             }
248             if (isHorizontalTextPositionSet()) {
249                 button.setHorizontalTextPosition(getHorizontalTextPosition());
250             }
251             if (isIconSet()) {
252                 button.setIcon(getIcon());
253             }
254             if (isIconTextGapSet()) {
255                 button.setIconTextGap(getIconTextGap());
256             }
257             if (isTextSet()) {
258                 button.setText(getText());
259             }
260             if (isVerticalAlignmentSet()) {
261                 button.setVerticalAlignment(getVerticalAlignment());
262             }
263             if (isVerticalTextPositionSet()) {
264                 button.setVerticalTextPosition(getVerticalTextPosition());
265             }
266         }
267    
268         public void applyPropertiesTo(LabelProperties props) {
269             if (noPropertiesSet()) {
270                 return;
271             }
272             if (isBackgroundSet()) {
273                 props.setBackground(getBackground());
274             }
275             if (isForegroundSet()) {
276                 props.setForeground(getForeground());
277             }
278             if (isFontSet()) {
279                 props.setFont(getFont());
280             }
281             if (isHorizontalAlignmentSet()) {
282                 props.setHorizontalAlignment(getHorizontalAlignment());
283             }
284             if (isHorizontalTextPositionSet()) {
285                 props.setHorizontalTextPosition(getHorizontalTextPosition());
286             }
287             if (isIconSet()) {
288                 props.setIcon(getIcon());
289             }
290             if (isIconTextGapSet()) {
291                 props.setIconTextGap(getIconTextGap());
292             }
293             if (isTextSet()) {
294                 props.setText(getText());
295             }
296             if (isVerticalAlignmentSet()) {
297                 props.setVerticalAlignment(getVerticalAlignment());
298             }
299             if (isVerticalTextPositionSet()) {
300                 props.setVerticalTextPosition(getVerticalTextPosition());
301             }
302         }
303    
304         public void applyPropertiesTo(TableCellRenderer renderer) {
305             if (renderer instanceof JLabel) {
306                 applyPropertiesTo( (JLabel) renderer);
307             }
308             else if (renderer instanceof AbstractButton) {
309                 applyPropertiesTo( (AbstractButton) renderer);
310             }
311         }
312    }