1 package aurelienribon.utils.swing;
2 
3 import java.awt.Component;
4 import java.awt.Graphics;
5 import java.awt.Graphics2D;
6 import java.awt.Insets;
7 import javax.swing.border.Border;
8 
9 public class GroupBorder implements Border {
10 	private final int titleHeight = 20;
11 	private final int borderPadding = 0;
12 	private String title = "";
13 
14 	public String getTitle() {
15 		return title;
16 	}
17 
18 	public void setTitle(String title) {
19 		this.title = title;
20 	}
21 
22 	@Override
23 	public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
24 		Graphics2D gg = (Graphics2D) g.create();
25 
26 		int titleW = gg.getFontMetrics().stringWidth(title) + 20;
27 		int titleDescent = gg.getFontMetrics().getDescent();
28 
29 		gg.setColor(c.getBackground());
30 
31 		if (!title.equals("")) {
32 			int[] xs = {0, titleW, titleW + titleHeight, 0};
33 			int[] ys = {0, 0, titleHeight, titleHeight};
34 			gg.fillPolygon(xs, ys, 4);
35 			gg.fillRect(0, titleHeight, width, height);
36 			gg.setColor(c.getForeground());
37 			gg.drawString(title, 10, titleHeight - titleDescent);
38 		} else {
39 			gg.fillRect(0, 0, width, height);
40 		}
41 
42 		gg.dispose();
43 	}
44 
45 	@Override
46 	public Insets getBorderInsets(Component c) {
47 		return new Insets(!title.equals("") ? borderPadding + titleHeight : borderPadding, borderPadding, borderPadding, borderPadding);
48 	}
49 
50 	@Override
51 	public boolean isBorderOpaque() {
52 		return false;
53 	}
54 }
55