|
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.border.Border; |
|
30 |
|
31 @SuppressWarnings({"serial"}) |
|
32 public class FocusBorder implements Border { |
|
33 // |
|
34 // Instance data |
|
35 // |
|
36 |
|
37 private Border focusBorder; |
|
38 private Border noFocusBorder; |
|
39 |
|
40 // |
|
41 // Constructors |
|
42 // |
|
43 |
|
44 /** |
|
45 * Constructs a {@code FocusBorder} with the given wrapped borders. |
|
46 * |
|
47 * @param focusBorder |
|
48 * the {@code Border} to paint when the {@code Component} has |
|
49 * focus, or {@code null} to paint no border in this case |
|
50 * |
|
51 * @param noFocusBorder |
|
52 * the {@code Border} to paint when the {@code Component} does |
|
53 * not have focus, or {@code null} to paint no border in this |
|
54 * case |
|
55 */ |
|
56 public FocusBorder(Border focusBorder, Border noFocusBorder) { |
|
57 this.focusBorder = focusBorder; |
|
58 this.noFocusBorder = noFocusBorder; |
|
59 } |
|
60 |
|
61 /** |
|
62 * Constructs a {@code FocusBorder} with the given focus border, and no |
|
63 * no-focus border. |
|
64 */ |
|
65 public FocusBorder(Border focusBorder) { |
|
66 this(focusBorder, null); |
|
67 } |
|
68 |
|
69 /** |
|
70 * Constructs a {@code FocusBorder} with a {@link DottedBorder} focus |
|
71 * border, and no no-focus border. |
|
72 */ |
|
73 public FocusBorder() { |
|
74 this(new DottedBorder()); |
|
75 } |
|
76 |
|
77 // |
|
78 // Border methods |
|
79 // |
|
80 |
|
81 @Override |
|
82 public Insets getBorderInsets(Component c) { |
|
83 return focusBorder.getBorderInsets(c); |
|
84 } |
|
85 |
|
86 /** |
|
87 * Returns {@code false} if any wrapped border is non-opaque, {@code true} |
|
88 * otherwise. |
|
89 */ |
|
90 @Override |
|
91 public boolean isBorderOpaque() { |
|
92 if ((focusBorder != null && !focusBorder.isBorderOpaque()) || |
|
93 (noFocusBorder != null && !noFocusBorder.isBorderOpaque())) { |
|
94 return false; |
|
95 } |
|
96 |
|
97 return true; |
|
98 } |
|
99 |
|
100 @Override |
|
101 public void paintBorder(Component c, Graphics g, int x, int y, |
|
102 int width, int height) { |
|
103 |
|
104 if (c.isFocusOwner()) { |
|
105 if (focusBorder != null) { |
|
106 focusBorder.paintBorder(c, g, x, y, width, height); |
|
107 } |
|
108 } else { |
|
109 if (noFocusBorder != null) { |
|
110 noFocusBorder.paintBorder(c, g, x, y, width, height); |
|
111 } |
|
112 } |
|
113 } |
|
114 |
|
115 // |
|
116 // FocusBorder methods |
|
117 // |
|
118 |
|
119 public Border getFocusBorder() { |
|
120 return focusBorder; |
|
121 } |
|
122 |
|
123 public Border getNoFocusBorder() { |
|
124 return noFocusBorder; |
|
125 } |
|
126 } |