File: LabeledComponentGroup.java

package info (click to toggle)
mac-widgets 0.9.5%2Bsvn369-dfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,920 kB
  • sloc: java: 8,318; makefile: 13; sh: 12
file content (92 lines) | stat: -rw-r--r-- 3,527 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.explodingpixels.macwidgets;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import java.util.Arrays;
import java.util.List;

/**
 * Creates a group of components and provides a label underneath those components. The added
 * components will be placed side by side, with no spacing in between them, like this:
 * <br><br>
 * <img src="../../../../graphics/LabeledComponentGroup-anatomy.png">
 * <br><br>
 * Here are a couple more practical applications of {@code LabledComponentGroup}:
 * <br><br>
 * <img src="../../../../graphics/LabeledComponentGroup-view.png">&nbsp;&nbsp;&nbsp;&nbsp;<img src="../../../../graphics/LabeledComponentGroup-search.png">
 * <br><br>
 * Here's how to create a {@code LabeledComponentGroup} with two buttons:
 * <pre>
 * JToggleButton leftButton = new JToggleButton("Left Button");
 * leftButton.putClientProperty("JButton.buttonType", "segmentedTextured");
 * leftButton.putClientProperty("JButton.segmentPosition", "first");
 * <p/>
 * JToggleButton rightButton = new JToggleButton("Right Button");
 * rightButton.putClientProperty("JButton.buttonType", "segmentedTextured");
 * rightButton.putClientProperty("JButton.segmentPosition", "last");
 * <p/>
 * LabeledComponentGroup group = new LabeledComponentGroup("Group", leftButton, rightButton);
 * </pre>
 */
public class LabeledComponentGroup {

    private JComponent fComponent;

    /**
     * Creates a labeled component group using the given label and components.
     *
     * @param labelString the label of the group.
     * @param components  the components in the group.
     */
    public LabeledComponentGroup(String labelString, JComponent... components) {
        this(labelString, Arrays.asList(components));
    }

    /**
     * Creates a labeled component group using the given label and components.
     *
     * @param labelString the label of the group.
     * @param components  the components in the group.
     */
    public LabeledComponentGroup(String labelString, List<JComponent> components) {
        JComponent componentToAdd;
        if (components.size() == 1) {
            componentToAdd = components.get(0);
        } else {
            componentToAdd = new JPanel(new FlowLayout(0, 0, FlowLayout.CENTER));
            componentToAdd.setOpaque(false);
            for (JComponent component : components) {
                componentToAdd.add(component);
            }
        }

        // definte the FormLayout columns and rows.
        FormLayout layout = new FormLayout("p", "fill:p:grow, p");
        // create the cell constraints to use in the layout.
        CellConstraints cc = new CellConstraints();
        // create the builder with our panel as the component to be filled.
        PanelBuilder builder = new PanelBuilder(layout, new JPanel());

        builder.add(componentToAdd, cc.xy(1, 1, "center, center"));
        builder.add(createLabel(labelString), cc.xy(1, 2, "center, top"));

        fComponent = builder.getPanel();
        fComponent.setOpaque(false);
    }

    public JComponent getComponent() {
        return fComponent;
    }

    private JLabel createLabel(String labelString) {
        JLabel label = MacWidgetFactory.makeEmphasizedLabel(new JLabel(labelString));
        label.setFont(MacFontUtils.TOOLBAR_LABEL_FONT);
        return label;
    }
}