Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 884568db3e757ce671ed96443671b30d27235f00 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*******************************************************************************
 * Copyright (c) 2004, 2008 John Krasnay and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     John Krasnay - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.xml.vex.core.internal.css;

import org.eclipse.wst.xml.vex.core.internal.core.DisplayDevice;
import org.eclipse.wst.xml.vex.core.internal.dom.Element;
import org.w3c.css.sac.LexicalUnit;

/**
 * The border-XXX-width CSS property. Since the value of this property depends
 * on the corresponding border-XXX-style property, the style property must be
 * calculated first and placed in the styles, and its name given to the
 * constructor of this class.
 */
public class BorderWidthProperty extends AbstractProperty {

	// Name of the corresponding border style property
	private String borderStyleName;

	// Axis along which the border width is measured.
	private Axis axis;

	// named border widths
	private static final int BORDER_WIDTH_THIN = 1;
	private static final int BORDER_WIDTH_MEDIUM = 3;
	private static final int BORDER_WIDTH_THICK = 5;

	private static int getBorderWidth(LexicalUnit lu, float fontSize, int ppi) {
		if (isLength(lu)) {
			return getIntLength(lu, fontSize, ppi);
		} else if (lu.getLexicalUnitType() == LexicalUnit.SAC_IDENT) {
			String s = lu.getStringValue();
			if (s.equals(CSS.THIN)) {
				return BORDER_WIDTH_THIN;
			} else if (s.equals(CSS.MEDIUM)) {
				return BORDER_WIDTH_MEDIUM;
			} else if (s.equals(CSS.THICK)) {
				return BORDER_WIDTH_THICK;
			} else {
				return 0;
			}
		} else {
			return 0;
		}
	}

	/**
	 * Class constructor.
	 * 
	 * @param name
	 *            Name of the property.
	 * @param borderStyleName
	 *            Name of the corresponding border style property. For example,
	 *            if name is CSS.BORDER_TOP_WIDTH, then borderStyleName should
	 *            be CSS.BORDER_TOP_STYLE.
	 * @param axis
	 *            AXIS_HORIZONTAL (for left and right borders) or AXIS_VERTICAL
	 *            (for top and bottom borders).
	 */
	public BorderWidthProperty(String name, String borderStyleName, Axis axis) {
		super(name);
		this.borderStyleName = borderStyleName;
		this.axis = axis;
	}

	/**
	 * Returns true if the given lexical unit represents a border width.
	 * 
	 * @param lu
	 *            LexicalUnit to check.
	 */
	public static boolean isBorderWidth(LexicalUnit lu) {
		if (lu == null) {
			return false;
		} else if (isLength(lu)) {
			return true;
		} else if (lu.getLexicalUnitType() == LexicalUnit.SAC_IDENT) {
			String s = lu.getStringValue();
			return s.equals(CSS.THIN) || s.equals(CSS.MEDIUM)
					|| s.equals(CSS.THICK);
		} else {
			return false;
		}
	}

	public Object calculate(LexicalUnit lu, Styles parentStyles, Styles styles, Element element) {
		return Integer.valueOf(calculateInternal(lu, parentStyles, styles));
	}

	private int calculateInternal(LexicalUnit lu, Styles parentStyles,
			Styles styles) {

		DisplayDevice device = DisplayDevice.getCurrent();
		int ppi = this.axis == Axis.HORIZONTAL ? device.getHorizontalPPI()
				: device.getVerticalPPI();

		String borderStyle = (String) styles.get(this.borderStyleName);

		if (borderStyle.equals(CSS.NONE) || borderStyle.equals(CSS.HIDDEN)) {
			return 0;
		} else if (isBorderWidth(lu)) {
			return getBorderWidth(lu, styles.getFontSize(), ppi);
		} else if (isInherit(lu) && parentStyles != null) {
			return ((Integer) parentStyles.get(this.getName())).intValue();
		} else {
			// not specified, "none", or other unknown value
			return BORDER_WIDTH_MEDIUM;
		}
	}
}

Back to the top