OSDN Git Service

Maven3対応。
[jindolf/Jindolf.git] / src / main / java / jp / sfjp / jindolf / editor / BalloonBorder.java
1 /*
2  * baloon border
3  *
4  * License : The MIT License
5  * Copyright(c) 2008 olyutorskii
6  */
7
8 package jp.sfjp.jindolf.editor;
9
10 import java.awt.BorderLayout;
11 import java.awt.Color;
12 import java.awt.Component;
13 import java.awt.Graphics;
14 import java.awt.Graphics2D;
15 import java.awt.Insets;
16 import java.awt.LayoutManager;
17 import java.awt.RenderingHints;
18 import javax.swing.JComponent;
19 import javax.swing.border.Border;
20
21 /**
22  * フキダシ風Border。
23  */
24 public class BalloonBorder implements Border{
25
26     private static final int RADIUS = 5;
27
28
29     /**
30      * コンストラクタ。
31      */
32     public BalloonBorder(){
33         super();
34         return;
35     }
36
37
38     /**
39      * 隙間が透明なフキダシ装飾を任意のコンポーネントに施す。
40      * @param inner 装飾対象のコンポーネント
41      * @return 装飾されたコンポーネント
42      */
43     public static JComponent decorateTransparentBorder(JComponent inner){
44         JComponent result = new TransparentContainer(inner);
45
46         Border border = new BalloonBorder();
47         result.setBorder(border);
48
49         return result;
50     }
51
52     /**
53      * {@inheritDoc}
54      * @param comp {@inheritDoc}
55      * @return {@inheritDoc}
56      */
57     @Override
58     public Insets getBorderInsets(Component comp){
59         Insets insets = new Insets(RADIUS, RADIUS, RADIUS, RADIUS);
60         return insets;
61     }
62
63     /**
64      * {@inheritDoc}
65      * 必ずfalseを返す(このBorderは透明)。
66      * @return {@inheritDoc}
67      */
68     @Override
69     public boolean isBorderOpaque(){
70         return false;
71     }
72
73     /**
74      * {@inheritDoc}
75      * @param comp {@inheritDoc}
76      * @param g {@inheritDoc}
77      * @param x {@inheritDoc}
78      * @param y {@inheritDoc}
79      * @param width {@inheritDoc}
80      * @param height {@inheritDoc}
81      */
82     @Override
83     public void paintBorder(Component comp,
84                               Graphics g,
85                               int x, int y,
86                               int width, int height ){
87         final int diameter = RADIUS * 2;
88         final int innerWidth  = width - diameter;
89         final int innerHeight = height - diameter;
90
91         Graphics2D g2d = (Graphics2D) g;
92
93         Color bgColor = comp.getBackground();
94         g2d.setColor(bgColor);
95
96         Object antiAliaseHint =
97                 g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
98         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
99                              RenderingHints.VALUE_ANTIALIAS_ON );
100
101         g2d.fillRect(x + RADIUS, y,
102                    innerWidth, RADIUS);
103         g2d.fillRect(x, y + RADIUS,
104                    RADIUS, innerHeight);
105         g2d.fillRect(x + RADIUS + innerWidth, y + RADIUS,
106                    RADIUS, innerHeight);
107         g2d.fillRect(x + RADIUS, y + RADIUS + innerHeight,
108                    innerWidth, RADIUS);
109
110         int right = 90;  // 90 degree right angle
111
112         g2d.fillArc(x + innerWidth, y,
113                   diameter, diameter, right * 0, right);
114         g2d.fillArc(x, y,
115                   diameter, diameter, right * 1, right);
116         g2d.fillArc(x, y + innerHeight,
117                   diameter, diameter, right * 2, right);
118         g2d.fillArc(x + innerWidth, y + innerHeight,
119                   diameter, diameter, right * 3, right);
120
121         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antiAliaseHint);
122
123         return;
124     }
125
126     /**
127      * 透明コンテナ。
128      * 1つの子を持ち、背景色操作を委譲する。
129      * つまりこのコンテナにBorderを設定すると子の背景色が反映される。
130      */
131     @SuppressWarnings("serial")
132     private static class TransparentContainer extends JComponent{
133
134         private final JComponent inner;
135
136         /**
137          * コンストラクタ。
138          * @param inner 内部コンポーネント
139          */
140         public TransparentContainer(JComponent inner){
141             super();
142
143             this.inner = inner;
144
145             setOpaque(false);
146
147             LayoutManager layout = new BorderLayout();
148             setLayout(layout);
149             add(this.inner, BorderLayout.CENTER);
150
151             return;
152         }
153
154         /**
155          * {@inheritDoc}
156          * 子の背景色を返す。
157          * @return {@inheritDoc}
158          */
159         @Override
160         public Color getBackground(){
161             Color bg = this.inner.getBackground();
162             return bg;
163         }
164
165         /**
166          * {@inheritDoc}
167          * 背景色指定をフックし、子の背景色を指定する。
168          * @param bg {@inheritDoc}
169          */
170         @Override
171         public void setBackground(Color bg){
172             this.inner.setBackground(bg);
173             return;
174         }
175     }
176
177 }