1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.swing; |
|
27 |
|
28 import java.awt.*; |
|
29 import javax.swing.*; |
|
30 import javax.swing.border.Border; |
|
31 |
|
32 /** |
|
33 * A {@code ClippedBorder} clips one or more sides off an existing {@code |
|
34 * Border}. The resulting {@code Border} has insets of zero on the sides that |
|
35 * are clipped. |
|
36 */ |
|
37 @SuppressWarnings({"serial"}) |
|
38 public class ClippedBorder implements Border { |
|
39 // |
|
40 // Instance data |
|
41 // |
|
42 |
|
43 protected Border border; |
|
44 private boolean top; |
|
45 private boolean left; |
|
46 private boolean bottom; |
|
47 private boolean right; |
|
48 |
|
49 // |
|
50 // Constructors |
|
51 // |
|
52 |
|
53 /** |
|
54 * Constructs a {@code ClippedBorder} with the given side mask. |
|
55 * |
|
56 * @param border |
|
57 * the {@code Border} to clip |
|
58 * |
|
59 * @param top |
|
60 * {@code true} to include the top side of {@code border} in |
|
61 * this {@code ClippedBorder}, {@code false} to exclude it |
|
62 * |
|
63 * @param left |
|
64 * {@code true} to include the left side of {@code border} in |
|
65 * this {@code ClippedBorder}, {@code false} to exclude it |
|
66 * |
|
67 * @param bottom |
|
68 * {@code true} to include the bottom side of {@code border} in |
|
69 * this {@code ClippedBorder}, {@code false} to exclude it |
|
70 * |
|
71 * @param right |
|
72 * {@code true} to include the right side of {@code border} in |
|
73 * this {@code ClippedBorder}, {@code false} to exclude it |
|
74 */ |
|
75 public ClippedBorder(Border border, boolean top, boolean left, |
|
76 boolean bottom, boolean right) { |
|
77 |
|
78 assert border != null; |
|
79 |
|
80 this.border = border; |
|
81 this.top = top; |
|
82 this.left = left; |
|
83 this.bottom = bottom; |
|
84 this.right = right; |
|
85 } |
|
86 |
|
87 // |
|
88 // Border methods |
|
89 // |
|
90 |
|
91 @Override |
|
92 public Insets getBorderInsets(Component c) { |
|
93 Insets i = border.getBorderInsets(c); |
|
94 |
|
95 int top = this.top ? i.top : 0; |
|
96 int left = this.left ? i.left : 0; |
|
97 int bottom = this.bottom ? i.bottom : 0; |
|
98 int right = this.right ? i.right : 0; |
|
99 |
|
100 return new Insets(top, left, bottom, right); |
|
101 } |
|
102 |
|
103 @Override |
|
104 public boolean isBorderOpaque() { |
|
105 return border.isBorderOpaque(); |
|
106 } |
|
107 |
|
108 @Override |
|
109 public void paintBorder(Component c, Graphics g, int x, int y, int width, |
|
110 int height) { |
|
111 |
|
112 // g.setClip(x, y, width, height); |
|
113 |
|
114 Insets i = border.getBorderInsets(c); |
|
115 |
|
116 if (!top) { |
|
117 y -= i.top; |
|
118 height += i.top; |
|
119 } |
|
120 |
|
121 if (!bottom) { |
|
122 height += i.bottom; |
|
123 } |
|
124 |
|
125 if (!left) { |
|
126 x -= i.left; |
|
127 width += i.left; |
|
128 } |
|
129 |
|
130 if (!right) { |
|
131 width += i.right; |
|
132 } |
|
133 |
|
134 border.paintBorder(c, g, x, y, width, height); |
|
135 } |
|
136 |
|
137 // |
|
138 // ClippedBorder methods |
|
139 // |
|
140 |
|
141 public Border getBorder() { |
|
142 return border; |
|
143 } |
|
144 |
|
145 public boolean getIncludeBottom() { |
|
146 return bottom; |
|
147 } |
|
148 |
|
149 public boolean getIncludeLeft() { |
|
150 return left; |
|
151 } |
|
152 |
|
153 public boolean getIncludeRight() { |
|
154 return right; |
|
155 } |
|
156 |
|
157 public boolean getIncludeTop() { |
|
158 return top; |
|
159 } |
|
160 |
|
161 // |
|
162 // Static methods |
|
163 // |
|
164 |
|
165 // XXX Unit test - Remove |
|
166 public static void main(String[] args) { |
|
167 Border etched = BorderFactory.createLineBorder(Color.black); |
|
168 Border clipped = new ClippedBorder( |
|
169 etched, false, true, false, true); |
|
170 Border empty = BorderFactory.createEmptyBorder(15, 15, 15, 15); |
|
171 |
|
172 Border mainBorder = BorderFactory.createCompoundBorder(clipped, empty); |
|
173 Border cornerBorder = |
|
174 BorderFactory.createCompoundBorder(etched, empty); |
|
175 |
|
176 JLabel label = new JLabel("Hello"); |
|
177 label.setHorizontalAlignment(SwingConstants.CENTER); |
|
178 label.setBackground(label.getBackground().darker()); |
|
179 label.setOpaque(true); |
|
180 label.setPreferredSize(new Dimension(300, 200)); |
|
181 label.setBorder(clipped); |
|
182 |
|
183 JPanel mainPanel = new JPanel(new GridBagLayout()); |
|
184 mainPanel.setPreferredSize(new Dimension(600, 600)); |
|
185 mainPanel.add(label); |
|
186 |
|
187 JFrame frame = new JFrame(); |
|
188 Container c = frame.getContentPane(); |
|
189 c.setLayout(new BorderLayout()); |
|
190 c.add(mainPanel, BorderLayout.CENTER); |
|
191 frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
|
192 frame.pack(); |
|
193 frame.setVisible(true); |
|
194 } |
|
195 } |
|