Initialize
[sdk/ide/product.git] / org.eclipse.jst.pagedesigner / src / org / eclipse / jst / pagedesigner / css2 / property / BorderStyleMeta.java
1 /*******************************************************************************\r
2  * Copyright (c) 2006 Sybase, Inc. and others.\r
3  *\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     Sybase, Inc. - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.eclipse.jst.pagedesigner.css2.property;\r
13 \r
14 import org.eclipse.jst.pagedesigner.IHTMLConstants;\r
15 import org.eclipse.jst.pagedesigner.css2.ICSSStyle;\r
16 import org.eclipse.jst.pagedesigner.utils.DOMUtil;\r
17 import org.w3c.dom.Element;\r
18 import org.w3c.dom.Node;\r
19 \r
20 /**\r
21  * @author mengbo\r
22  */\r
23 public class BorderStyleMeta extends CSSPropertyMeta {\r
24         static final String[] KEYWORDS = new String[] { ICSSPropertyID.VAL_NONE,\r
25                         ICSSPropertyID.VAL_HIDDEN, ICSSPropertyID.VAL_DOTTED,\r
26                         ICSSPropertyID.VAL_DASHED, ICSSPropertyID.VAL_SOLID,\r
27                         ICSSPropertyID.VAL_DOUBLE, ICSSPropertyID.VAL_GROOVE,\r
28                         ICSSPropertyID.VAL_RIDGE, ICSSPropertyID.VAL_INSET,\r
29                         ICSSPropertyID.VAL_OUTSET };\r
30 \r
31         /**\r
32          */\r
33         public BorderStyleMeta() {\r
34                 super(false, ICSSPropertyID.VAL_NONE);\r
35         }\r
36 \r
37         /*\r
38          * (non-Javadoc)\r
39          * \r
40          * @see org.eclipse.jst.pagedesigner.css2.property.CSSPropertyMeta#getKeywordValues()\r
41          */\r
42         protected String[] getKeywordValues() {\r
43                 return KEYWORDS;\r
44         }\r
45 \r
46         /*\r
47          * (non-Javadoc)\r
48          * \r
49          * @see org.eclipse.jst.pagedesigner.css2.property.CSSPropertyMeta#calculateHTMLAttributeOverride(org.w3c.dom.Element,\r
50          *      java.lang.String, java.lang.String,\r
51          *      org.eclipse.jst.pagedesigner.css2.ICSSStyle)\r
52          */\r
53         public Object calculateHTMLAttributeOverride(Element element,\r
54                         String htmltag, String propertyName, ICSSStyle style) {\r
55                 if (IHTMLConstants.TAG_TABLE.equalsIgnoreCase(htmltag)) {\r
56                         return calculateTableOverride(element, propertyName);\r
57                 } else if (IHTMLConstants.TAG_TD.equalsIgnoreCase(htmltag)\r
58                                 || IHTMLConstants.TAG_TH.equalsIgnoreCase(htmltag)) {\r
59                         return calculateTDOverride(element, propertyName, style);\r
60                 } else if (IHTMLConstants.TAG_IMG.equalsIgnoreCase(htmltag)) {\r
61                         String border = DOMUtil.getAttributeIgnoreCase(element,\r
62                                         IHTMLConstants.ATTR_BORDER);\r
63                         if (border != null) {\r
64                                 return ICSSPropertyID.VAL_SOLID;\r
65                         }\r
66                 }\r
67                 return super.calculateHTMLAttributeOverride(element, htmltag,\r
68                                 propertyName, style);\r
69         }\r
70 \r
71         /**\r
72          * @param element\r
73          * @param propertyName\r
74          * @return\r
75          */\r
76         private Object calculateTDOverride(Element element, String propertyName,\r
77                         ICSSStyle style) {\r
78                 // if element is empty, and "empty-cells" property is hide, then we\r
79                 // don't display border.\r
80                 Object obj = style.getStyleProperty(ICSSPropertyID.ATTR_EMPTY_CELLS);\r
81                 if (EmptyCellsMeta.HIDE.equals(obj) && TableUtil.isEmptyCell(element)) {\r
82                         return ICSSPropertyID.VAL_NONE;\r
83                 }\r
84 \r
85                 // find containing table first\r
86                 Node parent = element;\r
87                 Element tableEle = null;\r
88                 while ((parent = parent.getParentNode()) != null\r
89                                 && parent instanceof Element) {\r
90                         if (((Element) parent).getTagName().equalsIgnoreCase(\r
91                                         IHTMLConstants.TAG_TABLE)) {\r
92                                 tableEle = (Element) parent;\r
93                                 break;\r
94                         }\r
95 \r
96                 }\r
97                 if (tableEle != null) {\r
98                         String rules = DOMUtil.getAttributeIgnoreCase(tableEle,\r
99                                         IHTMLConstants.ATTR_RULES);\r
100                         String borderstr = DOMUtil.getAttributeIgnoreCase(tableEle,\r
101                                         IHTMLConstants.ATTR_BORDER);\r
102                         if (rules == null || rules.length() == 0) {\r
103                                 if (borderstr == null || "0".equals(borderstr.trim())) { //$NON-NLS-1$\r
104                                         return ICSSPropertyID.VAL_NONE;\r
105                                 }\r
106                 return ICSSPropertyID.VAL_TDBORDERSTYLE;\r
107                         }\r
108             // ok, we got a value for rules\r
109             if (TableUtil.matchRules(extractEdge(propertyName), rules)) {\r
110                 return ICSSPropertyID.VAL_TDBORDERSTYLE;\r
111             }\r
112             return ICSSPropertyID.VAL_NONE;\r
113                 }\r
114         return null;\r
115         }\r
116 \r
117         /**\r
118          * @param element\r
119          * @param propertyName\r
120          * @return\r
121          */\r
122         private Object calculateTableOverride(Element element, String propertyName) {\r
123                 String frame = DOMUtil.getAttributeIgnoreCase(element,\r
124                                 IHTMLConstants.ATTR_FRAME);\r
125                 String borderstr = DOMUtil.getAttributeIgnoreCase(element,\r
126                                 IHTMLConstants.ATTR_BORDER);\r
127                 // border="0" implies frame="void"\r
128                 if ("0".equals(borderstr)) { //$NON-NLS-1$\r
129                         return ICSSPropertyID.VAL_NONE;\r
130                 }\r
131                 if (frame == null || frame.length() == 0) {\r
132                         if (borderstr == null) {\r
133                                 return ICSSPropertyID.VAL_NONE;\r
134                         } else if (borderstr.trim().length() == 0) {\r
135                                 frame = IHTMLConstants.ATTR_BORDER;\r
136                         } else {\r
137                                 try {\r
138                                         Integer.parseInt(borderstr);\r
139                                         frame = IHTMLConstants.ATTR_BORDER;\r
140                                 } catch (Exception ex) {\r
141                                         frame = borderstr;\r
142                                 }\r
143                         }\r
144                 }\r
145                 // ok, we got a value for frame.\r
146                 if (TableUtil.matchFrame(extractEdge(propertyName), frame)) {\r
147                         return ICSSPropertyID.VAL_OUTSET;\r
148                 }\r
149         return ICSSPropertyID.VAL_NONE;\r
150         }\r
151 \r
152         static String extractEdge(String propertyName) {\r
153                 if (ICSSPropertyID.ATTR_BORDER_BOTTOM_STYLE\r
154                                 .equalsIgnoreCase(propertyName)) {\r
155                         return "bottom"; //$NON-NLS-1$\r
156                 } else if (ICSSPropertyID.ATTR_BORDER_LEFT_STYLE\r
157                                 .equalsIgnoreCase(propertyName)) {\r
158                         return "left"; //$NON-NLS-1$\r
159                 } else if (ICSSPropertyID.ATTR_BORDER_RIGHT_STYLE\r
160                                 .equalsIgnoreCase(propertyName)) {\r
161                         return "right"; //$NON-NLS-1$\r
162                 } else {\r
163                         return "top"; //$NON-NLS-1$\r
164                 }\r
165         }\r
166 \r
167         /**\r
168          * @param propertyName\r
169          * @return true if property name is a border style\r
170          */\r
171         public static boolean isBorderStyle(String propertyName) {\r
172                 return ICSSPropertyID.ATTR_BORDER_BOTTOM_STYLE\r
173                                 .equalsIgnoreCase(propertyName)\r
174                                 || ICSSPropertyID.ATTR_BORDER_LEFT_STYLE\r
175                                                 .equalsIgnoreCase(propertyName)\r
176                                 || ICSSPropertyID.ATTR_BORDER_RIGHT_STYLE\r
177                                                 .equalsIgnoreCase(propertyName)\r
178                                 || ICSSPropertyID.ATTR_BORDER_TOP_STYLE\r
179                                                 .equalsIgnoreCase(propertyName);\r
180         }\r
181 }\r