001    /*
002     * $Id: LabelProperties.java 2951 2008-06-17 10:07:49Z kleopatra $
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.table;
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. <p>
037     * 
038     * NOTE JW: no longer used except in ColumnHeaderRenderer which is EOL (will be
039     * removed once we switch over to jdk16). So probably will be removed as well.
040     * 
041     * @author Amy Fowler
042     * @version 1.0
043     */
044    
045    public class LabelProperties extends JLabel {
046        private static final int BACKGROUND_SET = 1;
047        private static final int FOREGROUND_SET = 2;
048        private static final int FONT_SET = 4;
049        private static final int HORIZONTAL_ALIGNMENT_SET = 8;
050        private static final int HORIZONTAL_TEXT_POSITION_SET = 16;
051        private static final int ICON_SET = 32;
052        private static final int ICON_TEXT_GAP_SET = 64;
053        private static final int TEXT_SET = 128;
054        private static final int VERTICAL_ALIGNMENT_SET = 256;
055        private static final int VERTICAL_TEXT_POSITION_SET = 512;
056    
057        private int setFlags = 0;
058    
059        public LabelProperties() {
060            super();
061            addPropertyChangeListener(new PropertyChangeListener() {
062                public void propertyChange(PropertyChangeEvent e) {
063                    String propertyName = e.getPropertyName();
064                    Object value = e.getNewValue();
065                    if (propertyName.equals("background")) {
066                        if (value != null) {
067                            setFlags |= BACKGROUND_SET;
068                        } else {
069                            setFlags &= (~BACKGROUND_SET);
070                        }
071                    }
072                    else if (propertyName.equals("font")) {
073                        if (value != null) {
074                            setFlags |= FONT_SET;
075                        } else {
076                            setFlags &= (~FONT_SET);
077                        }
078                    }
079                    else if (propertyName.equals("foreground")) {
080                        if (value != null) {
081                            setFlags |= FOREGROUND_SET;
082                        } else {
083                            setFlags &= (~FOREGROUND_SET);
084                        }
085                    }
086                    else if (propertyName.equals("horizontalAlignment")) {
087                        if (value != null && ((Integer)value).intValue() != -1) {
088                            setFlags |= HORIZONTAL_ALIGNMENT_SET;
089                        } else {
090                            setFlags &= (~HORIZONTAL_ALIGNMENT_SET);
091                        }
092                    }
093                    else if (propertyName.equals("horizontalTextPosition")) {
094                        if (value != null && ((Integer)value).intValue() != -1) {
095                            setFlags |= HORIZONTAL_TEXT_POSITION_SET;
096                        } else {
097                            setFlags &= (~HORIZONTAL_TEXT_POSITION_SET);
098                        }
099                    }
100                    else if (propertyName.equals("icon")) {
101                        if (value != null) {
102                            setFlags |= ICON_SET;
103                        } else {
104                            setFlags &= (~ICON_SET);
105                        }
106                    }
107                    else if (propertyName.equals("iconTextGap")) {
108                        if (value != null && ((Integer)value).intValue() != -1) {
109                            setFlags |= ICON_TEXT_GAP_SET;
110                        } else {
111                            setFlags &= (~ICON_TEXT_GAP_SET);
112                        }
113                    }
114                    else if (propertyName.equals("text")) {
115                        if (value != null) {
116                            setFlags |= TEXT_SET;
117                        } else {
118                            setFlags &= (~TEXT_SET);
119                        }
120                    }
121                    else if (propertyName.equals("verticalAlignment")) {
122                        if (value != null && ((Integer)value).intValue() != -1) {
123                            setFlags |= VERTICAL_ALIGNMENT_SET;
124                        } else {
125                            setFlags &= (~VERTICAL_ALIGNMENT_SET);
126                        }
127                    }
128                    else if (propertyName.equals("verticalTextPosition")) {
129                        if (value != null && ((Integer)value).intValue() != -1) {
130                            setFlags |= VERTICAL_TEXT_POSITION_SET;
131                        } else {
132                            setFlags &= (~VERTICAL_TEXT_POSITION_SET);
133                        }
134                    }
135                }
136            });
137        }
138    
139        public LabelProperties(Color background, Color foreground, Font font,
140                               int horizontalAlignment, int horizontalTextPosition,
141                               int verticalAlignment, int verticalTextPosition,
142                               Icon icon, int iconTextGap, String text) {
143            this();
144            setBackground(background);
145            setForeground(foreground);
146            setFont(font);
147            setHorizontalAlignment(horizontalAlignment);
148            setHorizontalTextPosition(horizontalTextPosition);
149            setVerticalAlignment(verticalAlignment);
150            setVerticalTextPosition(verticalTextPosition);
151            setIcon(icon);
152            setIconTextGap(iconTextGap);
153            setText(text);
154        }
155    
156        @Override
157        public boolean isBackgroundSet() {
158            return (setFlags & BACKGROUND_SET) > 0;
159        }
160    
161        @Override
162        public boolean isForegroundSet() {
163            return (setFlags & FOREGROUND_SET) > 0;
164        }
165    
166        @Override
167        public boolean isFontSet() {
168            return (setFlags & FONT_SET) > 0;
169        }
170    
171        public boolean isHorizontalAlignmentSet() {
172            return (setFlags & HORIZONTAL_ALIGNMENT_SET) > 0;
173        }
174    
175        public boolean isHorizontalTextPositionSet() {
176            return (setFlags & HORIZONTAL_TEXT_POSITION_SET) > 0;
177        }
178    
179        public boolean isIconSet() {
180            return (setFlags & ICON_SET) > 0;
181        }
182    
183        public boolean isIconTextGapSet() {
184            return (setFlags & ICON_TEXT_GAP_SET) > 0;
185        }
186    
187        public boolean isTextSet() {
188            return (setFlags & TEXT_SET) > 0;
189        }
190    
191        public boolean isVerticalAlignmentSet() {
192            return (setFlags & VERTICAL_ALIGNMENT_SET) > 0;
193        }
194    
195        public boolean isVerticalTextPositionSet() {
196            return (setFlags & VERTICAL_TEXT_POSITION_SET) > 0;
197        }
198    
199        public boolean noPropertiesSet() {
200            return setFlags == 0;
201        }
202    
203        public void applyPropertiesTo(JLabel label) {
204            if (noPropertiesSet()) {
205                return;
206            }
207            if (isBackgroundSet()) {
208                label.setBackground(getBackground());
209            }
210            if (isForegroundSet()) {
211                label.setForeground(getForeground());
212            }
213            if (isFontSet()) {
214                label.setFont(getFont());
215            }
216            if (isHorizontalAlignmentSet()) {
217                label.setHorizontalAlignment(getHorizontalAlignment());
218            }
219            if (isHorizontalTextPositionSet()) {
220                label.setHorizontalTextPosition(getHorizontalTextPosition());
221            }
222            if (isIconSet()) {
223                label.setIcon(getIcon());
224            }
225            if (isIconTextGapSet()) {
226                label.setIconTextGap(getIconTextGap());
227            }
228            if (isTextSet()) {
229                label.setText(getText());
230            }
231            if (isVerticalAlignmentSet()) {
232                label.setVerticalAlignment(getVerticalAlignment());
233            }
234            if (isVerticalTextPositionSet()) {
235                label.setVerticalTextPosition(getVerticalTextPosition());
236            }
237        }
238    
239        public void applyPropertiesTo(AbstractButton button) {
240             if (noPropertiesSet()) {
241                 return;
242             }
243             if (isBackgroundSet()) {
244                 button.setBackground(getBackground());
245             }
246             if (isForegroundSet()) {
247                 button.setForeground(getForeground());
248             }
249             if (isFontSet()) {
250                 button.setFont(getFont());
251             }
252             if (isHorizontalAlignmentSet()) {
253                 button.setHorizontalAlignment(getHorizontalAlignment());
254             }
255             if (isHorizontalTextPositionSet()) {
256                 button.setHorizontalTextPosition(getHorizontalTextPosition());
257             }
258             if (isIconSet()) {
259                 button.setIcon(getIcon());
260             }
261             if (isIconTextGapSet()) {
262                 button.setIconTextGap(getIconTextGap());
263             }
264             if (isTextSet()) {
265                 button.setText(getText());
266             }
267             if (isVerticalAlignmentSet()) {
268                 button.setVerticalAlignment(getVerticalAlignment());
269             }
270             if (isVerticalTextPositionSet()) {
271                 button.setVerticalTextPosition(getVerticalTextPosition());
272             }
273         }
274    
275         public void applyPropertiesTo(LabelProperties props) {
276             if (noPropertiesSet()) {
277                 return;
278             }
279             if (isBackgroundSet()) {
280                 props.setBackground(getBackground());
281             }
282             if (isForegroundSet()) {
283                 props.setForeground(getForeground());
284             }
285             if (isFontSet()) {
286                 props.setFont(getFont());
287             }
288             if (isHorizontalAlignmentSet()) {
289                 props.setHorizontalAlignment(getHorizontalAlignment());
290             }
291             if (isHorizontalTextPositionSet()) {
292                 props.setHorizontalTextPosition(getHorizontalTextPosition());
293             }
294             if (isIconSet()) {
295                 props.setIcon(getIcon());
296             }
297             if (isIconTextGapSet()) {
298                 props.setIconTextGap(getIconTextGap());
299             }
300             if (isTextSet()) {
301                 props.setText(getText());
302             }
303             if (isVerticalAlignmentSet()) {
304                 props.setVerticalAlignment(getVerticalAlignment());
305             }
306             if (isVerticalTextPositionSet()) {
307                 props.setVerticalTextPosition(getVerticalTextPosition());
308             }
309         }
310    
311         public void applyPropertiesTo(TableCellRenderer renderer) {
312             if (renderer instanceof JLabel) {
313                 applyPropertiesTo( (JLabel) renderer);
314             }
315             else if (renderer instanceof AbstractButton) {
316                 applyPropertiesTo( (AbstractButton) renderer);
317             }
318         }
319    }