src/jdk.jconsole/share/classes/sun/tools/jconsole/BorderedComponent.java
author egahlin
Mon, 30 Sep 2019 15:35:56 +0200
branchJEP-349-branch
changeset 58402 d284ff23fa4c
parent 47216 71c04702a3d5
permissions -rw-r--r--
Remove test applications
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.jconsole;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.plaf.basic.BasicGraphicsUtils;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    36
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import static javax.swing.SwingConstants.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import static sun.tools.jconsole.JConsole.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
@SuppressWarnings("serial")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class BorderedComponent extends JPanel implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    JButton moreOrLessButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    String valueLabelStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    JLabel label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    JComponent comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    boolean collapsed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private Icon collapseIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private Icon expandIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static Image getImage(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        Toolkit tk = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        name = "resources/" + name + ".png";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        return tk.getImage(BorderedComponent.class.getResource(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public BorderedComponent(String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this(text, null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public BorderedComponent(String text, JComponent comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this(text, comp, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public BorderedComponent(String text, JComponent comp, boolean collapsible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        super(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.comp = comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // Only add border if text is not null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            TitledBorder border;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            if (collapsible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                final JLabel textLabel = new JLabel(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                JPanel borderLabel = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    public int getBaseline(int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                        Dimension dim = textLabel.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                        return textLabel.getBaseline(dim.width, dim.height) + textLabel.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                borderLabel.add(textLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                border = new LabeledBorder(borderLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                textLabel.setForeground(border.getTitleColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                if (IS_WIN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    collapseIcon = new ImageIcon(getImage("collapse-winlf"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    expandIcon = new ImageIcon(getImage("expand-winlf"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    collapseIcon = new ArrowIcon(SOUTH, textLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    expandIcon = new ArrowIcon(EAST, textLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                moreOrLessButton = new JButton(collapseIcon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                moreOrLessButton.setContentAreaFilled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                moreOrLessButton.setBorderPainted(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                moreOrLessButton.setMargin(new Insets(0, 0, 0, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                moreOrLessButton.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                String toolTip =
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   100
                    Messages.BORDERED_COMPONENT_MORE_OR_LESS_BUTTON_TOOLTIP;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                moreOrLessButton.setToolTipText(toolTip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                borderLabel.add(moreOrLessButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                borderLabel.setSize(borderLabel.getPreferredSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                add(borderLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                border = new TitledBorder(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            setBorder(new CompoundBorder(new FocusBorder(this), border));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            setBorder(new FocusBorder(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (comp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            add(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public void setComponent(JComponent comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (this.comp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            remove(this.comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.comp = comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (!collapsed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            LayoutManager lm = getLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (lm instanceof BorderLayout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                add(comp, BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                add(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public void setValueLabel(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        this.valueLabelStr = str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (label != null) {
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   136
            label.setText(Resources.format(Messages.CURRENT_VALUE,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   137
                                           valueLabelStr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public void actionPerformed(ActionEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (collapsed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (label != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                remove(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            add(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            moreOrLessButton.setIcon(collapseIcon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            remove(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (valueLabelStr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                if (label == null) {
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   152
                    label = new JLabel(Resources.format(Messages.CURRENT_VALUE,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   153
                                                        valueLabelStr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                add(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            moreOrLessButton.setIcon(expandIcon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        collapsed = !collapsed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        JComponent container = (JComponent)getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (container != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            container.getLayout() instanceof VariableGridLayout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            ((VariableGridLayout)container.getLayout()).setFillRow(this, !collapsed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            container.revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public Dimension getMinimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (getLayout() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            // A layout manager has been set, so delegate to it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            return super.getMinimumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (moreOrLessButton != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            Dimension d = moreOrLessButton.getMinimumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            Insets i = getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            d.width  += i.left + i.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            d.height += i.top + i.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return super.getMinimumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public void doLayout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (getLayout() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            // A layout manager has been set, so delegate to it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            super.doLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        Dimension d = getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        Insets i = getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (collapsed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (label != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                Dimension p = label.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                label.setBounds(i.left,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                i.top + (d.height - i.top - i.bottom - p.height) / 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                p.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                p.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (comp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                comp.setBounds(i.left,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                               i.top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                               d.width - i.left - i.right,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                               d.height - i.top - i.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    private static class ArrowIcon implements Icon {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        private int direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        private JLabel textLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        public ArrowIcon(int direction, JLabel textLabel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            this.direction = direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            this.textLabel = textLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        public void paintIcon(Component c, Graphics g, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            int w = getIconWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            int h = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            Polygon p = new Polygon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            switch (direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
              case EAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                p.addPoint(x + 2,     y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                p.addPoint(x + w - 2, y + h / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                p.addPoint(x + 2,     y + h - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
              case SOUTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                p.addPoint(x,         y + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                p.addPoint(x + w / 2, y + h - 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                p.addPoint(x + w - 1, y + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            g.fillPolygon(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        public int getIconWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return getIconHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        public int getIconHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            Graphics g = textLabel.getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if (g != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                int h = g.getFontMetrics(textLabel.getFont()).getAscent() * 6/10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                if (h % 2 == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    h += 1;     // Make it odd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                return 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * A subclass of <code>TitledBorder</code> which implements an arbitrary border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * with the addition of a JComponent (JLabel, JPanel, etc) in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * default position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * If the border property value is not
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 14342
diff changeset
   269
     * specified in the constructor or by invoking the appropriate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * set method, the property value will be defined by the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * look and feel, using the following property name in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Defaults Table:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * <li>&quot;TitledBorder.border&quot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    protected static class LabeledBorder extends TitledBorder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        protected JComponent label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        private Point compLoc = new Point();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         * Creates a LabeledBorder instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
         * @param label  the label the border should display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        public LabeledBorder(JComponent label)     {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            this(null, label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
         * Creates a LabeledBorder instance with the specified border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         * and an empty label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
         * @param border  the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        public LabeledBorder(Border border)       {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            this(border, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
         * Creates a LabeledBorder instance with the specified border and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
         * label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
         * @param border  the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
         * @param label  the label the border should display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        public LabeledBorder(Border border, JComponent label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            super(border);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            this.label = label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if (label instanceof JLabel &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                label.getForeground() instanceof ColorUIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                label.setForeground(getTitleColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         * Paints the border for the specified component with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         * specified position and size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
         * @param c the component for which this border is being painted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
         * @param g the paint graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
         * @param x the x position of the painted border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         * @param y the y position of the painted border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         * @param width the width of the painted border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         * @param height the height of the painted border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            Border border = getBorder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (label == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                if (border != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    border.paintBorder(c, g, x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            Rectangle grooveRect = new Rectangle(x + EDGE_SPACING, y + EDGE_SPACING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                                 width - (EDGE_SPACING * 2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                                 height - (EDGE_SPACING * 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            Dimension   labelDim = label.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            int baseline = label.getBaseline(labelDim.width, labelDim.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            int         ascent = Math.max(0, baseline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            int         descent = labelDim.height - ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            int         diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            Insets      insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            if (border != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                insets = border.getBorderInsets(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                insets = new Insets(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            diff = Math.max(0, ascent/2 + TEXT_SPACING - EDGE_SPACING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            grooveRect.y += diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            grooveRect.height -= diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            compLoc.y = grooveRect.y + insets.top/2 - (ascent + descent) / 2 - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            int justification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            if (c.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                justification = LEFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                justification = RIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            switch (justification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                case LEFT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    compLoc.x = grooveRect.x + TEXT_INSET_H + insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                case RIGHT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    compLoc.x = (grooveRect.x + grooveRect.width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                 - (labelDim.width + TEXT_INSET_H + insets.right));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            // If title is positioned in middle of border AND its fontsize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            // is greater than the border's thickness, we'll need to paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            // the border in sections to leave space for the component's background
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            // to show through the title.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            if (border != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                if (grooveRect.y > compLoc.y - ascent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    Rectangle clipRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    // save original clip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    Rectangle saveClip = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    // paint strip left of text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    clipRect.setBounds(saveClip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    if (computeIntersection(clipRect, x, y, compLoc.x-1-x, height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                        g.setClip(clipRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                        border.paintBorder(c, g, grooveRect.x, grooveRect.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                                      grooveRect.width, grooveRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    // paint strip right of text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    clipRect.setBounds(saveClip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    if (computeIntersection(clipRect, compLoc.x+ labelDim.width +1, y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                                   x+width-(compLoc.x+ labelDim.width +1), height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                        g.setClip(clipRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                        border.paintBorder(c, g, grooveRect.x, grooveRect.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                                      grooveRect.width, grooveRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    // paint strip below text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    clipRect.setBounds(saveClip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    if (computeIntersection(clipRect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                                            compLoc.x - 1, compLoc.y + ascent + descent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                                            labelDim.width + 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                                            y + height - compLoc.y - ascent - descent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                        g.setClip(clipRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                        border.paintBorder(c, g, grooveRect.x, grooveRect.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                                  grooveRect.width, grooveRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    // restore clip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    g.setClip(saveClip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    border.paintBorder(c, g, grooveRect.x, grooveRect.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                                      grooveRect.width, grooveRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                label.setLocation(compLoc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                label.setSize(labelDim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
         * Reinitialize the insets parameter with this Border's current Insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
         * @param c the component for which this border insets value applies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
         * @param insets the object to be reinitialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        public Insets getBorderInsets(Component c, Insets insets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            Border border = getBorder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            if (border != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                if (border instanceof AbstractBorder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    ((AbstractBorder)border).getBorderInsets(c, insets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    // Can't reuse border insets because the Border interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    // can't be enhanced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    Insets i = border.getBorderInsets(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    insets.top = i.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    insets.right = i.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    insets.bottom = i.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    insets.left = i.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                insets.left = insets.top = insets.right = insets.bottom = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            insets.left += EDGE_SPACING + TEXT_SPACING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            insets.right += EDGE_SPACING + TEXT_SPACING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            insets.top += EDGE_SPACING + TEXT_SPACING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            insets.bottom += EDGE_SPACING + TEXT_SPACING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            if (c == null || label == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                return insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            insets.top += label.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            return insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
         * Returns the label of the labeled border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        public JComponent getLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            return label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
         * Sets the title of the titled border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
         * param title the title for the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        public void setLabel(JComponent label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            this.label = label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         * Returns the minimum dimensions this border requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         * in order to fully display the border and title.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
         * @param c the component where this border will be drawn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        public Dimension getMinimumSize(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            Insets insets = getBorderInsets(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            Dimension minSize = new Dimension(insets.right + insets.left,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                                              insets.top + insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            minSize.width += label.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            return minSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        private static boolean computeIntersection(Rectangle dest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                                                   int rx, int ry, int rw, int rh) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            int x1 = Math.max(rx, dest.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            int x2 = Math.min(rx + rw, dest.x + dest.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            int y1 = Math.max(ry, dest.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            int y2 = Math.min(ry + rh, dest.y + dest.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            dest.x = x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            dest.y = y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            dest.width = x2 - x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            dest.height = y2 - y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            if (dest.width <= 0 || dest.height <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    protected static class FocusBorder extends AbstractBorder implements FocusListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        private Component comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        private Color focusColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        private boolean focusLostTemporarily = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        public FocusBorder(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            this.comp = comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            comp.addFocusListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            // This is the best guess for a L&F specific color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            focusColor = UIManager.getColor("TabbedPane.focus");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            if (comp.hasFocus() || focusLostTemporarily) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                Color color = g.getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                g.setColor(focusColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                BasicGraphicsUtils.drawDashedRect(g, x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                g.setColor(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        public Insets getBorderInsets(Component c, Insets insets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            insets.set(2, 2, 2, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            return insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            comp.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        public void focusLost(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            // We will still paint focus even if lost temporarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            focusLostTemporarily = e.isTemporary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            if (!focusLostTemporarily) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                comp.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
}