~ubuntu-branches/ubuntu/trusty/libswingx-java/trusty

« back to all changes in this revision

Viewing changes to src/java/org/jdesktop/swingx/JXTitledSeparator.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2008-03-08 16:18:24 UTC
  • Revision ID: james.westby@ubuntu.com-20080308161824-wsahvl9pwzjcea3g
Tags: upstream-0.9.2
ImportĀ upstreamĀ versionĀ 0.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: JXTitledSeparator.java,v 1.12 2007/10/20 16:42:52 rah003 Exp $
 
3
 *
 
4
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
 
5
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Lesser General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2.1 of the License, or (at your option) any later version.
 
11
 * 
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 * 
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
20
 */
 
21
 
 
22
package org.jdesktop.swingx;
 
23
 
 
24
import java.awt.Color;
 
25
import java.awt.Font;
 
26
import java.awt.GridBagConstraints;
 
27
import java.awt.GridBagLayout;
 
28
import java.awt.Insets;
 
29
import javax.swing.Box;
 
30
import javax.swing.Icon;
 
31
import javax.swing.JLabel;
 
32
import javax.swing.JSeparator;
 
33
import javax.swing.SwingConstants;
 
34
import javax.swing.UIManager;
 
35
import javax.swing.plaf.ColorUIResource;
 
36
import javax.swing.plaf.FontUIResource;
 
37
 
 
38
/**
 
39
 * <p>A simple horizontal separator that contains a title.<br/>
 
40
 *
 
41
 * <p>JXTitledSeparator allows you to specify the title via the {@link #setTitle} method.
 
42
 * The title alignment may be specified by using the {@link #setHorizontalAlignment}
 
43
 * method, and accepts all the same arguments as the {@link javax.swing.JLabel#setHorizontalAlignment}
 
44
 * method.</p>
 
45
 *
 
46
 * <p>In addition, you may specify an Icon to use with this separator. The icon
 
47
 * will appear "leading" the title (on the left in left-to-right languages,
 
48
 * on the right in right-to-left languages). To change the position of the
 
49
 * title with respect to the icon, call {@link #setHorizontalTextPosition}.</p>
 
50
 *
 
51
 * <p>The default font and color of the title comes from the <code>LookAndFeel</code>, mimicking
 
52
 * the font and color of the {@link javax.swing.border.TitledBorder}</p>
 
53
 *
 
54
 * <p>Here are a few example code snippets:
 
55
 * <pre><code>
 
56
 *  //create a plain separator
 
57
 *  JXTitledSeparator sep = new JXTitledSeparator();
 
58
 *  sep.setText("Customer Info");
 
59
 *
 
60
 *  //create a separator with an icon
 
61
 *  sep = new JXTitledSeparator();
 
62
 *  sep.setText("Customer Info");
 
63
 *  sep.setIcon(new ImageIcon("myimage.png"));
 
64
 *
 
65
 *  //create a separator with an icon to the right of the title,
 
66
 *  //center justified
 
67
 *  sep = new JXTitledSeparator();
 
68
 *  sep.setText("Customer Info");
 
69
 *  sep.setIcon(new ImageIcon("myimage.png"));
 
70
 *  sep.setHorizontalAlignment(SwingConstants.CENTER);
 
71
 *  sep.setHorizontalTextPosition(SwingConstants.TRAILING);
 
72
 * </code></pre>
 
73
 *
 
74
 * @status REVIEWED
 
75
 * @author rbair
 
76
 */
 
77
public class JXTitledSeparator extends JXPanel {
 
78
    /**
 
79
     * Implementation detail: the label used to display the title
 
80
     */
 
81
    private JLabel label;
 
82
    /**
 
83
     * Implementation detail: a separator to use on the left of the
 
84
     * title if alignment is centered or right justified
 
85
     */
 
86
    private JSeparator leftSeparator;
 
87
    /**
 
88
     * Implementation detail: a separator to use on the right of the
 
89
     * title if alignment is centered or left justified
 
90
     */
 
91
    private JSeparator rightSeparator;
 
92
    
 
93
    /** 
 
94
     * Creates a new instance of <code>JXTitledSeparator</code>. The default title is simply
 
95
     * an empty string. Default justification is <code>LEADING</code>, and the default
 
96
     * horizontal text position is <code>TRAILING</code> (title follows icon)
 
97
     */
 
98
    public JXTitledSeparator() {
 
99
        this("Untitled");
 
100
    }
 
101
    
 
102
    /** 
 
103
     * Creates a new instance of <code>JXTitledSeparator</code> with the specified
 
104
     * title. Default horizontal alignment is <code>LEADING</code>, and the default
 
105
     * horizontal text position is <code>TRAILING</code> (title follows icon)
 
106
     */
 
107
    public JXTitledSeparator(String title) {
 
108
        this(title, SwingConstants.LEADING, null);
 
109
    }
 
110
    
 
111
    /** 
 
112
     * Creates a new instance of <code>JXTitledSeparator</code> with the specified
 
113
     * title and horizontal alignment. The default
 
114
     * horizontal text position is <code>TRAILING</code> (title follows icon)
 
115
     */
 
116
    public JXTitledSeparator(String title, int horizontalAlignment) {
 
117
        this(title, horizontalAlignment, null);
 
118
    }
 
119
    
 
120
    /** 
 
121
     * Creates a new instance of <code>JXTitledSeparator</code> with the specified
 
122
     * title, icon, and horizontal alignment. The default
 
123
     * horizontal text position is <code>TRAILING</code> (title follows icon)
 
124
     */
 
125
    public JXTitledSeparator(String title, int horizontalAlignment, Icon icon) {
 
126
        setLayout(new GridBagLayout());
 
127
        
 
128
        label = new JLabel(title) {
 
129
            @Override
 
130
            public void updateUI(){
 
131
              super.updateUI();
 
132
              updateTitle();
 
133
            }
 
134
        };
 
135
        label.setIcon(icon);
 
136
        label.setHorizontalAlignment(horizontalAlignment);
 
137
        leftSeparator = new JSeparator();
 
138
        rightSeparator = new JSeparator();
 
139
        
 
140
        layoutSeparator();
 
141
        
 
142
        updateTitle();
 
143
        setOpaque(false);
 
144
    }
 
145
    
 
146
    /**
 
147
     * Implementation detail. Handles updates of title color and font on LAF change. For more 
 
148
     * details see swingx#451.
 
149
     */
 
150
    protected void updateTitle()
 
151
    {
 
152
      if (label == null) return;
 
153
      
 
154
      Color c = label.getForeground();
 
155
      if (c == null || c instanceof ColorUIResource)
 
156
        setForeground(UIManager.getColor("TitledBorder.titleColor"));
 
157
 
 
158
      Font f = label.getFont();
 
159
      if (f == null || f instanceof FontUIResource)
 
160
        setFont(UIManager.getFont("TitledBorder.font"));
 
161
    }
 
162
 
 
163
    /**
 
164
     * Implementation detail. lays out this component, showing/hiding components
 
165
     * as necessary. Actually changes the containment (removes and adds components).
 
166
     * <code>JXTitledSeparator</code> is treated as a single component rather than 
 
167
     * a container.
 
168
     */
 
169
    private void layoutSeparator() {
 
170
        removeAll();
 
171
        
 
172
        switch (label.getHorizontalAlignment()) {
 
173
            case SwingConstants.LEFT:
 
174
            case SwingConstants.LEADING:
 
175
            case SwingConstants.WEST:
 
176
                add(label, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
 
177
                add(Box.createHorizontalStrut(3), new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
 
178
                add(rightSeparator, new GridBagConstraints(2, 0, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));
 
179
                break;
 
180
            case SwingConstants.RIGHT:
 
181
            case SwingConstants.TRAILING:
 
182
            case SwingConstants.EAST:
 
183
                add(rightSeparator, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));
 
184
                add(Box.createHorizontalStrut(3), new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
 
185
                add(label, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
 
186
                break;
 
187
            case SwingConstants.CENTER:
 
188
            default:
 
189
                add(leftSeparator, new GridBagConstraints(0, 0, 1, 1, 0.5, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));
 
190
                add(Box.createHorizontalStrut(3), new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
 
191
                add(label, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
 
192
                add(Box.createHorizontalStrut(3), new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
 
193
                add(rightSeparator, new GridBagConstraints(4, 0, 1, 1, 0.5, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));
 
194
        }
 
195
    }
 
196
    
 
197
    /**
 
198
     * Sets the title for the separator. This may be simple html, or plain
 
199
     * text.
 
200
     *
 
201
     * @param title the new title. Any string input is acceptable
 
202
     */
 
203
    public void setTitle(String title) {
 
204
        String old = getTitle();
 
205
        label.setText(title);
 
206
        firePropertyChange("title", old, getTitle());
 
207
    }
 
208
    
 
209
    /**
 
210
     * Gets the title.
 
211
     * 
 
212
     * @return the title being used for this <code>JXTitledSeparator</code>. 
 
213
     *         This will be the raw title text, and so may include html tags etc 
 
214
     *         if they were so specified in #setTitle.
 
215
     */
 
216
    public String getTitle() {
 
217
        return label.getText();
 
218
    }
 
219
    
 
220
    /**
 
221
     * <p>Sets the alignment of the title along the X axis. If leading, then
 
222
     * the title will lead the separator (in left-to-right languages,
 
223
     * the title will be to the left and the separator to the right). If centered,
 
224
     * then a separator will be to the left, followed by the title (centered),
 
225
     * followed by a separator to the right. Trailing will have the title
 
226
     * on the right with a separator to its left, in left-to-right languages.</p>
 
227
     * 
 
228
     * <p>LEFT and RIGHT always position the text left or right of the separator,
 
229
     * respectively, regardless of the language orientation.</p>
 
230
     *
 
231
     * @param alignment  One of the following constants
 
232
     *           defined in <code>SwingConstants</code>:
 
233
     *           <code>LEFT</code>,
 
234
     *           <code>CENTER</code>,
 
235
     *           <code>RIGHT</code>,
 
236
     *           <code>LEADING</code> (the default) or
 
237
     *           <code>TRAILING</code>.
 
238
     *
 
239
     * @throws IllegalArgumentException if the alignment does not match one of
 
240
     *         the accepted inputs.
 
241
     * @see SwingConstants
 
242
     * @see #getHorizontalAlignment
 
243
     */
 
244
    public void setHorizontalAlignment(int alignment) {
 
245
        int old = getHorizontalAlignment();
 
246
        label.setHorizontalAlignment(alignment);
 
247
        if (old != getHorizontalAlignment()) {
 
248
            layoutSeparator();
 
249
        }
 
250
        firePropertyChange("horizontalAlignment", old, getHorizontalAlignment());
 
251
    }
 
252
    
 
253
    /**
 
254
     * Returns the alignment of the title contents along the X axis.
 
255
     *
 
256
     * @return   The value of the horizontalAlignment property, one of the 
 
257
     *           following constants defined in <code>SwingConstants</code>:
 
258
     *           <code>LEFT</code>,
 
259
     *           <code>CENTER</code>, 
 
260
     *           <code>RIGHT</code>,
 
261
     *           <code>LEADING</code> or
 
262
     *           <code>TRAILING</code>.
 
263
     *
 
264
     * @see #setHorizontalAlignment
 
265
     * @see SwingConstants
 
266
     */
 
267
    public int getHorizontalAlignment() {
 
268
        return label.getHorizontalAlignment();
 
269
    }
 
270
 
 
271
    /**
 
272
     * Sets the horizontal position of the title's text,
 
273
     * relative to the icon.
 
274
     *
 
275
     * @param position  One of the following constants
 
276
     *           defined in <code>SwingConstants</code>:
 
277
     *           <code>LEFT</code>,
 
278
     *           <code>CENTER</code>,
 
279
     *           <code>RIGHT</code>,
 
280
     *           <code>LEADING</code>, or
 
281
     *           <code>TRAILING</code> (the default).
 
282
     * @throws IllegalArgumentException if the position does not match one of
 
283
     *         the accepted inputs.
 
284
     */
 
285
    public void setHorizontalTextPosition(int position) {
 
286
        int old = getHorizontalTextPosition();
 
287
        label.setHorizontalTextPosition(position);
 
288
        firePropertyChange("horizontalTextPosition", old, getHorizontalTextPosition());
 
289
    }
 
290
    
 
291
    /**
 
292
     * Returns the horizontal position of the title's text,
 
293
     * relative to the icon.
 
294
     *
 
295
     * @return   One of the following constants
 
296
     *           defined in <code>SwingConstants</code>:
 
297
     *           <code>LEFT</code>,
 
298
     *           <code>CENTER</code>, 
 
299
     *           <code>RIGHT</code>,
 
300
     *           <code>LEADING</code> or
 
301
     *           <code>TRAILING</code>.
 
302
     *
 
303
     * @see SwingConstants
 
304
     */
 
305
    public int getHorizontalTextPosition() {
 
306
        return label.getHorizontalTextPosition();
 
307
    }
 
308
    
 
309
    /**
 
310
     * Defines the icon this component will display.  If
 
311
     * the value of icon is null, nothing is displayed.
 
312
     * <p>
 
313
     * The default value of this property is null.
 
314
     * 
 
315
     * @see #setHorizontalTextPosition
 
316
     * @see #getIcon
 
317
     */
 
318
    public void setIcon(Icon icon) {
 
319
        Icon old = getIcon();
 
320
        label.setIcon(icon);
 
321
        firePropertyChange("icon", old, getIcon());
 
322
    }
 
323
    
 
324
    /**
 
325
     * Returns the graphic image (glyph, icon) that the 
 
326
     * <code>JXTitledSeparator</code> displays.
 
327
     *
 
328
     * @return an Icon
 
329
     * @see #setIcon
 
330
     */
 
331
    public Icon getIcon() {
 
332
        return label.getIcon();
 
333
    }
 
334
 
 
335
    /**
 
336
     * @inheritDoc
 
337
     */
 
338
    @Override
 
339
    public void setForeground(Color foreground) {
 
340
        if (label != null) {
 
341
            label.setForeground(foreground);
 
342
        }
 
343
        super.setForeground(foreground);
 
344
    }
 
345
    
 
346
    /**
 
347
     * @inheritDoc
 
348
     */
 
349
    @Override
 
350
    public void setFont(Font font) {
 
351
        if (label != null) {
 
352
            label.setFont(font);
 
353
        }
 
354
        super.setFont(font);
 
355
    }
 
356
}