165d6a383696e0d98b99753e4329207067439959
[scilab.git] / scilab / modules / gui / src / java / org / scilab / modules / gui / utils / BorderConvertor.java
1 package org.scilab.modules.gui.utils;
2
3 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_LINE_THICKNESS__;
4 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_POSITION__;
5 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_TITLE__;
6 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTANGLE__;
7 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTNAME__;
8 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTSIZE__;
9 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTWEIGHT__;
10 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_COLOR__;
11 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_HIGHLIGHT_IN__;
12 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_HIGHLIGHT_OUT__;
13 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_IN_BORDER__;
14 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_JUSTIFICATION__;
15 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_OUT_BORDER__;
16 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_POSITION__;
17 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_ROUNDED__;
18 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_SHADOW_IN__;
19 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_SHADOW_OUT__;
20 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_STYLE__;
21 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_TITLE__;
22 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_TYPE__;
23
24 import java.awt.Color;
25 import java.awt.Font;
26 import java.lang.reflect.Field;
27
28 import javax.swing.BorderFactory;
29 import javax.swing.UIManager;
30 import javax.swing.border.BevelBorder;
31 import javax.swing.border.Border;
32 import javax.swing.border.TitledBorder;
33
34 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
35 import org.scilab.modules.graphic_objects.uicontrol.frame.border.FrameBorder.BorderType;
36 import org.scilab.modules.graphic_objects.uicontrol.frame.border.FrameBorder.JustificationType;
37 import org.scilab.modules.graphic_objects.uicontrol.frame.border.FrameBorder.TitlePositionType;
38 import org.scilab.modules.graphic_objects.uicontrol.frame.border.FrameBorderType;
39
40 public class BorderConvertor {
41     public static Color getColor(String str) {
42         try {
43             Field field = Class.forName("java.awt.Color").getField(str);
44             return (Color) field.get(null);
45         } catch (Exception e) {
46             return Color.black;
47         }
48     }
49
50     public static Border getBorder(Integer borderId) {
51         GraphicController controller = GraphicController.getController();
52         Integer styleInt = (Integer) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_STYLE__);
53         if (styleInt == null) {
54             controller.getProperty(borderId, __GO_UI_FRAME_BORDER_STYLE__);
55         }
56         Border border = null;
57
58         FrameBorderType style = FrameBorderType.intToEnum(styleInt);
59         switch (style) {
60             case BEVEL: {
61                 BorderType type = BorderType.intToEnum((Integer) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_TYPE__));
62                 int bevelType = type == BorderType.LOWERED ? BevelBorder.LOWERED : BevelBorder.RAISED;
63                 String hlOut = (String) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_HIGHLIGHT_OUT__);
64                 String hlIn = (String) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_HIGHLIGHT_IN__);
65                 String shadowOut = (String) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_SHADOW_OUT__);
66                 String shadowIn = (String) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_SHADOW_IN__);
67
68                 if (hlOut != null && shadowOut != null) {
69                     if (hlIn != null && shadowIn != null) {
70                         border = BorderFactory.createBevelBorder(bevelType, getColor(hlOut), getColor(hlIn), getColor(shadowOut), getColor(shadowIn));
71                     } else {
72                         border = BorderFactory.createBevelBorder(bevelType, getColor(hlOut), getColor(shadowOut));
73                     }
74                 } else {
75                     border = BorderFactory.createBevelBorder(bevelType);
76                 }
77                 break;
78             }
79             case COMPOUND: {
80                 Integer borderId1 = (Integer) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_OUT_BORDER__);
81                 Integer borderId2 = (Integer) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_IN_BORDER__);
82
83                 if (borderId1 != null && borderId2 != null) {
84                     Border border1 = getBorder(borderId1);
85                     Border border2 = getBorder(borderId2);
86
87                     border = BorderFactory.createCompoundBorder(border1, border2);
88                 } else {
89                     border = BorderFactory.createCompoundBorder();
90                 }
91                 break;
92             }
93             case EMPTY: {
94                 // TLBR
95                 Double[] pos = (Double[]) controller.getProperty(borderId, __GO_POSITION__);
96
97                 if (pos != null) {
98                     border = BorderFactory.createEmptyBorder(pos[0].intValue(), pos[1].intValue(), pos[2].intValue(), pos[3].intValue());
99                 } else {
100                     border = BorderFactory.createEmptyBorder();
101                 }
102                 break;
103             }
104             case ETCHED: {
105                 Integer type = (Integer) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_TYPE__);
106                 String hlOut = (String) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_HIGHLIGHT_OUT__);
107                 String shadowOut = (String) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_SHADOW_OUT__);
108
109                 if (type == null) {
110                     if (hlOut != null && shadowOut != null) {
111                         border = BorderFactory.createEtchedBorder(getColor(hlOut), getColor(shadowOut));
112                     } else {
113                         border = BorderFactory.createEtchedBorder();
114                     }
115
116                 } else {
117                     BorderType borderType = BorderType.intToEnum(type);
118                     int bevelType = borderType == BorderType.RAISED ? BevelBorder.RAISED : BevelBorder.LOWERED;
119
120                     if (hlOut != null && shadowOut != null) {
121                         border = BorderFactory.createEtchedBorder(bevelType, getColor(hlOut), getColor(shadowOut));
122                     } else {
123                         border = BorderFactory.createEtchedBorder(bevelType);
124                     }
125                 }
126                 break;
127             }
128             case LINE: {
129                 String color = (String) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_COLOR__);
130                 Integer thickness = (Integer) controller.getProperty(borderId, __GO_LINE_THICKNESS__);
131                 Boolean rounded = (Boolean) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_ROUNDED__);
132
133                 if (thickness != null) {
134                     if (rounded != null) {
135                         // Java 1.7
136                         // border = BorderFactory.createLineBorder(
137                         // getColor(color),
138                         // thickness,
139                         // rounded);
140                         border = BorderFactory.createLineBorder(getColor(color), thickness);
141                     } else {
142                         border = BorderFactory.createLineBorder(getColor(color), thickness);
143                     }
144                 } else {
145                     border = BorderFactory.createLineBorder(getColor(color));
146                 }
147                 break;
148             }
149             case MATTE: {
150                 // TLBR
151                 Double[] pos = (Double[]) controller.getProperty(borderId, __GO_POSITION__);
152                 String color = (String) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_COLOR__);
153
154                 border = BorderFactory.createMatteBorder(pos[0].intValue(), pos[1].intValue(), pos[2].intValue(), pos[3].intValue(), getColor(color));
155                 break;
156             }
157             case SOFTBEVEL: {
158
159                 BorderType type = BorderType.intToEnum((Integer) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_TYPE__));
160                 int bevelType = type == BorderType.LOWERED ? BevelBorder.LOWERED : BevelBorder.RAISED;
161                 String hlOut = (String) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_HIGHLIGHT_OUT__);
162                 String hlIn = (String) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_HIGHLIGHT_IN__);
163                 String shadowOut = (String) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_SHADOW_OUT__);
164                 String shadowIn = (String) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_SHADOW_IN__);
165
166                 /* in Java 1.6 use normal bevel function */
167                 if (hlOut != null && shadowOut != null) {
168                     if (hlIn != null && shadowIn != null) {
169                         border = BorderFactory.createBevelBorder(bevelType, getColor(hlOut), getColor(hlIn), getColor(shadowOut), getColor(shadowIn));
170                     } else {
171                         border = BorderFactory.createBevelBorder(bevelType, getColor(hlOut), getColor(shadowOut));
172                     }
173                 } else {
174                     border = BorderFactory.createBevelBorder(bevelType);
175                 }
176                 break;
177             }
178             case TITLED: {
179                 Integer borderId1 = (Integer) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_TITLE__);
180                 String title = (String) controller.getProperty(borderId, __GO_TITLE__);
181                 Integer justification = (Integer) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_JUSTIFICATION__);
182                 String fontName = (String) controller.getProperty(borderId, __GO_UI_FONTNAME__);
183                 String fontAngle = (String) controller.getProperty(borderId, __GO_UI_FONTANGLE__);
184                 Integer fontSize = (Integer) controller.getProperty(borderId, __GO_UI_FONTSIZE__);
185                 String fontWeight = (String) controller.getProperty(borderId, __GO_UI_FONTWEIGHT__);
186                 Integer pos = (Integer) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_POSITION__);
187                 String color = (String) controller.getProperty(borderId, __GO_UI_FRAME_BORDER_COLOR__);
188
189                 if (borderId1 == null && title != null) {
190                     border = BorderFactory.createTitledBorder(title);
191                 } else {
192                     // create sub border
193                     Border border1 = getBorder(borderId1);
194
195                     if (title != null) {
196                         if (justification != null && pos != null) {
197                             int justify = TitledBorder.LEADING;
198                             switch (JustificationType.intToEnum(justification)) {
199                                 case CENTER:
200                                     justify = TitledBorder.CENTER;
201                                     break;
202                                 case LEFT:
203                                     justify = TitledBorder.LEFT;
204                                     break;
205                                 case RIGHT:
206                                     justify = TitledBorder.RIGHT;
207                                     break;
208                                 case TRAILING:
209                                     justify = TitledBorder.TRAILING;
210                                     break;
211                                 case LEADING:
212                                 default:
213                                     justify = TitledBorder.LEADING;
214                                     break;
215                             }
216
217                             int position = TitledBorder.TOP;
218                             switch (TitlePositionType.intToEnum(pos)) {
219                                 case ABOVE_BOTTOM:
220                                     position = TitledBorder.ABOVE_BOTTOM;
221                                     break;
222                                 case ABOVE_TOP:
223                                     position = TitledBorder.ABOVE_TOP;
224                                     break;
225                                 case BELOW_BOTTOM:
226                                     position = TitledBorder.BELOW_BOTTOM;
227                                     break;
228                                 case BELOW_TOP:
229                                     position = TitledBorder.BELOW_TOP;
230                                     break;
231                                 case BOTTOM:
232                                     position = TitledBorder.BOTTOM;
233                                     break;
234                                 case TOP:
235                                 default:
236                                     position = TitledBorder.TOP;
237                                     break;
238                             }
239
240                             if (fontName != null && fontAngle != null && fontSize != null && fontWeight != null) {
241                                 // create font
242                                 int fontStyle = Font.PLAIN;
243                                 if (fontAngle.equals("italic")) {
244                                     fontStyle |= Font.ITALIC;
245                                 }
246
247                                 if (fontWeight.equals("bold")) {
248                                     fontStyle |= Font.BOLD;
249                                 }
250
251                                 Font defaultFont = UIManager.getFont("Button.font");
252                                 Font font = new Font(fontName.equals("") == false ? fontName : defaultFont.getFontName(),
253                                                      fontStyle,
254                                                      fontSize != 0 ? fontSize : defaultFont.getSize());
255                                 border = BorderFactory.createTitledBorder(border1, title, justify, position, font, getColor(color));
256                             } else {
257                                 border = BorderFactory.createTitledBorder(border1, title, justify, position);
258                             }
259                         } else {
260                             border = BorderFactory.createTitledBorder(border1, title);
261                         }
262                     } else {
263                         border = BorderFactory.createTitledBorder(border1);
264                     }
265                 }
266                 break;
267             }
268             default:
269             case NONE: {
270                 border = BorderFactory.createEmptyBorder();
271                 break;
272             }
273         }
274
275         return border;
276     }
277 }