]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/SheetBorder.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.spreadsheet.ui / src / org / simantics / spreadsheet / ui / SheetBorder.java
1 package org.simantics.spreadsheet.ui;\r
2 \r
3 import java.awt.Graphics;\r
4 import java.awt.Insets;\r
5 import java.awt.Component;\r
6 import java.awt.Color;\r
7 \r
8 import javax.swing.Icon;\r
9 import javax.swing.border.EmptyBorder;\r
10 \r
11 import org.simantics.ui.colors.Colors;\r
12 \r
13 public class SheetBorder extends EmptyBorder\r
14 {\r
15         private static final long serialVersionUID = 1L;\r
16         protected boolean bottomColor;\r
17         protected boolean rightColor;\r
18         \r
19         final Color NONE = Colors.awt(Colors.rgb(0.9,0.9,0.9));\r
20         \r
21 \r
22 \r
23     /**\r
24      * Creates a matte border with the specified insets and color.\r
25      * @param top the top inset of the border\r
26      * @param left the left inset of the border\r
27      * @param bottom the bottom inset of the border\r
28      * @param right the right inset of the border\r
29      * @param matteColor the color rendered for the border\r
30      */\r
31     public SheetBorder(int top, int left, int bottom, int right, boolean bottomColor, boolean rightColor)   {\r
32         super(top, left, bottom, right);\r
33         this.bottomColor = bottomColor;\r
34         this.rightColor = rightColor;\r
35     }\r
36 \r
37     /**\r
38      * Paints the matte border.\r
39      */\r
40     public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {\r
41         \r
42         Insets insets = getBorderInsets(c);\r
43         Color oldColor = g.getColor();\r
44         g.translate(x, y);\r
45 \r
46         boolean same = bottomColor == rightColor;\r
47         \r
48         if(same) {\r
49                 \r
50                 g.setColor(bottomColor ? Color.BLACK : NONE);\r
51                 g.fillRect(0, height - insets.bottom, 2 + width - insets.left, insets.bottom);\r
52                 g.fillRect(width - insets.right, 0, insets.right, height - insets.bottom + 2);\r
53                 \r
54         } else {\r
55         \r
56                 // Not the same color - paint black on top\r
57 \r
58                 if(bottomColor) {\r
59                         // Black bottom - no right\r
60                         g.setColor(NONE);\r
61                         g.fillRect(width - insets.right, 0, insets.right, height - insets.bottom + 2);\r
62                         g.setColor(Color.BLACK);\r
63                         g.fillRect(0, height - insets.bottom, 2 + width - insets.left, insets.bottom);\r
64                 } else {\r
65                         // Black right - no bottom\r
66                         g.setColor(NONE);\r
67                         g.fillRect(0, height - insets.bottom, 2 + width - insets.left, insets.bottom);\r
68                         g.setColor(Color.BLACK);\r
69                         g.fillRect(width - insets.right, 0, insets.right, height - insets.bottom + 2);\r
70                 }\r
71         \r
72         }\r
73         \r
74         g.translate(-x, -y);\r
75         g.setColor(oldColor);\r
76 \r
77     }\r
78 \r
79     /**\r
80      * Reinitialize the insets parameter with this Border's current Insets.\r
81      * @param c the component for which this border insets value applies\r
82      * @param insets the object to be reinitialized\r
83      * @since 1.3\r
84      */\r
85     public Insets getBorderInsets(Component c, Insets insets) {\r
86         return computeInsets(insets);\r
87     }\r
88 \r
89     /**\r
90      * Returns the insets of the border.\r
91      * @since 1.3\r
92      */\r
93     public Insets getBorderInsets() {\r
94         return computeInsets(new Insets(0,0,0,0));\r
95     }\r
96 \r
97     /* should be protected once api changes area allowed */\r
98     private Insets computeInsets(Insets insets) {\r
99         insets.left = left;\r
100         insets.top = top;\r
101         insets.right = right;\r
102         insets.bottom = bottom;\r
103         return insets;\r
104     }\r
105 \r
106     /**\r
107      * Returns the color used for tiling the border or null\r
108      * if a tile icon is being used.\r
109      * @since 1.3\r
110      */\r
111     public Color getMatteColor() {\r
112         return NONE;\r
113     }\r
114 \r
115    /**\r
116      * Returns the icon used for tiling the border or null\r
117      * if a solid color is being used.\r
118      * @since 1.3\r
119      */\r
120     public Icon getTileIcon() {\r
121         return null;\r
122     }\r
123 \r
124     /**\r
125      * Returns whether or not the border is opaque.\r
126      */\r
127     public boolean isBorderOpaque() {\r
128         // If a tileIcon is set, then it may contain transparent bits\r
129         return true;\r
130     }\r
131 \r
132 }\r