1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.xulux.guilayer.swing.extensions;
17
18 import java.awt.Color;
19 import java.awt.Component;
20 import java.awt.Font;
21 import java.awt.FontMetrics;
22 import java.awt.Graphics;
23 import java.awt.Image;
24 import java.awt.Insets;
25 import java.awt.Rectangle;
26
27 import javax.swing.ImageIcon;
28 import javax.swing.UIManager;
29 import javax.swing.border.AbstractBorder;
30
31
32
33
34
35
36
37 public class IconTitledBorder extends AbstractBorder {
38
39 public static final int EDGE_SPACING = 2;
40 public static final int TITLE_SPACING = 5;
41 public static final int TEXTICON_SPACING = 5;
42
43 protected int thickness = 1;
44
45 protected String title;
46 protected ImageIcon image;
47
48
49
50
51 public IconTitledBorder() {
52 super();
53 }
54
55
56
57
58 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
59
60 int lc = (getLineCorrection(c)/2);
61 Color tmpColor = g.getColor();
62 g.setColor(Color.gray);
63 for (int i = 0; i < getThickness(); i++) {
64 g.drawRect(x+EDGE_SPACING+i, y+EDGE_SPACING+i+lc, width-EDGE_SPACING-i-i-1, height-EDGE_SPACING-i-i-1-lc);
65 }
66 g.setColor(tmpColor);
67 clearLineForTitle(c, g, x, y, width, height);
68 paintImage(c, g, x, y, width, height);
69 paintTitle(c, g, x, y, width, height);
70
71 }
72
73 protected void clearLineForTitle(Component c, Graphics g, int x, int y, int width, int height) {
74 Rectangle rect = getTitleRectangle(c, x, y, width);
75 Color tmpColor = g.getColor();
76 g.setColor(c.getBackground());
77 if (getImage() != null) {
78 rect.width+=EDGE_SPACING;
79 rect.width+=getImage().getIconWidth();
80 }
81 g.fillRect(rect.x, rect.y, rect.width, rect.height);
82 g.setColor(tmpColor);
83 }
84
85
86
87
88
89
90
91
92 protected Rectangle getTitleRectangle(Component c, int x, int y, int width) {
93 Rectangle rectangle = new Rectangle();
94 rectangle.x = x+EDGE_SPACING+TITLE_SPACING;
95 rectangle.y = y+EDGE_SPACING;
96 rectangle.width = getTitleWidth(c);
97 rectangle.height = getMaxHeight(c);
98 return rectangle;
99 }
100
101 protected void paintTitle(Component c, Graphics g, int x, int y, int width, int height) {
102 if (getTitle() == null) {
103 return;
104 }
105 int imageCorrection = 0;
106 if (getImage() != null) {
107 imageCorrection = EDGE_SPACING+getImage().getIconWidth();
108 }
109 Font tmpFont = g.getFont();
110 Color tmpColor = g.getColor();
111 g.setFont(UIManager.getFont("TitledBorder.font"));
112 g.setColor(UIManager.getColor("TitledBorder.titleColor"));
113 g.drawString(title, x+EDGE_SPACING+TITLE_SPACING+imageCorrection, y+EDGE_SPACING+getLineCorrection(c));
114 g.setFont(tmpFont);
115 g.setColor(tmpColor);
116 }
117
118
119 public int getTitleWidth(Component c) {
120 int titleWidth = 0;
121 if (getImage() != null) {
122 titleWidth+=getImage().getIconWidth();
123 }
124 if (getTitle() != null && getImage() != null) {
125 titleWidth+=TEXTICON_SPACING;
126 }
127 if (getTitle() != null) {
128 titleWidth = c.getFontMetrics(UIManager.getFont("TitledBorder.font")).stringWidth(getTitle());
129 }
130 return titleWidth;
131 }
132
133 protected void paintImage(Component c, Graphics g, int x, int y, int width, int height) {
134 if (getImage() != null) {
135 getImage().paintIcon(c, g, x+EDGE_SPACING+TITLE_SPACING, y);
136 }
137 }
138
139
140
141
142
143
144 protected int getLineCorrection(Component c) {
145 return getMaxHeight(c) / 2;
146 }
147
148 public int getMaxHeight(Component c) {
149 int textHeight = 0;
150 int imageHeight = 0;
151 if (getTitle() != null) {
152 FontMetrics metrics = c.getFontMetrics(c.getFont());
153 textHeight = metrics.getHeight();
154 }
155 if (getImage() != null) {
156 imageHeight = getImage().getIconHeight();
157 }
158 return Math.max(textHeight, imageHeight);
159 }
160
161
162
163
164 public ImageIcon getImage() {
165 return image;
166 }
167
168
169
170
171 public int getThickness() {
172 return thickness;
173 }
174
175
176
177
178 public String getTitle() {
179 return title;
180 }
181
182
183
184
185
186
187 public void setImage(Image image) {
188 this.image = new ImageIcon(image);
189 }
190
191
192
193
194 public void setThickness(int thickness) {
195 this.thickness = thickness;
196 }
197
198
199
200
201 public void setTitle(String string) {
202 title = string;
203 }
204
205
206
207
208 public Insets getBorderInsets(Component c) {
209 return new Insets(EDGE_SPACING+getMaxHeight(c), EDGE_SPACING+thickness, EDGE_SPACING+thickness, EDGE_SPACING+thickness);
210 }
211
212
213
214
215 public Insets getBorderInsets(Component c, Insets insets) {
216 return super.getBorderInsets(c, insets);
217 }
218
219 }