Skip to main content
summaryrefslogtreecommitdiffstats
blob: edf94f0d4b071b24d0cae7a0ac01fd2bed76a3c5 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*******************************************************************************
 * Copyright (c) 2004 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.css.core.util.declaration;



import java.util.Collection;
import java.util.Iterator;

import org.eclipse.wst.css.core.document.ICSSPrimitiveValue;
import org.eclipse.wst.css.core.document.ICSSValue;
import org.eclipse.wst.css.core.document.ICSSValueList;
import org.eclipse.wst.css.core.internal.contentmodel.IValID;
import org.eclipse.wst.css.core.internal.contentmodel.PropCMProperty;
import org.eclipse.wst.css.core.internal.contentmodel.PropCMSubProperty;
import org.eclipse.wst.css.core.internal.contentmodel.PropCMUtil;
import org.eclipse.wst.css.core.parser.CSSRegionContexts;
import org.eclipse.wst.css.core.parser.CSSTextParser;
import org.eclipse.wst.css.core.parser.CSSTextToken;
import org.w3c.dom.css.CSSPrimitiveValue;
import org.w3c.dom.css.CSSValue;


/**
 * for horizontal value of 'background-position'
 */
public class BackgroundPositionXSubStyleAdapter implements ISubPropertyAdapter {

	/**
	 * 
	 */
	public BackgroundPositionXSubStyleAdapter() {
		super();
	}

	/**
	 * 
	 */
	protected CSSTextToken[] correctMeaningToken(CSSTextToken[] src) {
		java.util.ArrayList list = new java.util.ArrayList();
		for (int i = 0; i < src.length; i++) {
			if (src[i].kind != CSSRegionContexts.CSS_S && src[i].kind != CSSRegionContexts.CSS_DECLARATION_VALUE_S && src[i].kind != CSSRegionContexts.CSS_COMMENT)
				list.add(src[i]);
		}
		CSSTextToken[] ret = new CSSTextToken[list.size()];
		list.toArray(ret);
		return ret;
	}

	/**
	 * 
	 */
	public String get(ICSS2Properties properties) {
		String str = null;
		Object obj = properties.get(PropCMProperty.getInstanceOf(PropCMProperty.P_BG_POSITION));
		if (obj != null) {
			PropCMProperty propX = PropCMSubProperty.getInstanceOf(PropCMSubProperty.PSUB_BG_POSITION_X);
			PropCMProperty propY = PropCMSubProperty.getInstanceOf(PropCMSubProperty.PSUB_BG_POSITION_Y);
			if (obj instanceof ICSSValueList) {
				ICSSValueList list = (ICSSValueList) obj;
				ICSSValue value = (ICSSValue) list.item(0);
				if (value.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE) {
					ICSSPrimitiveValue prim = (ICSSPrimitiveValue) value;
					if (prim.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT) {
						// check not top or bottom
						if (!propX.canHave(prim.getStringValue()) && propY.canHave(prim.getStringValue())) {
							// case order is vertical -> horizontal
							value = (ICSSValue) list.item(1);
						}
					}
				}
				str = value.getCSSValueText();
			}
			else if (obj instanceof ICSSValue) {
				str = ((ICSSValue) obj).getCSSValueText();
				if (str.equalsIgnoreCase(IValID.V_BOTTOM) || str.equalsIgnoreCase(IValID.V_TOP))
					str = "";//$NON-NLS-1$
			}
			else {
				str = obj.toString();
				CSSTextParser parser = new CSSTextParser(CSSTextParser.MODE_DECLARATION_VALUE, str);
				CSSTextToken[] tokens = parser.getTokens();

				tokens = correctMeaningToken(tokens);
				if (tokens.length == 0)
					str = "";//$NON-NLS-1$
				else if (tokens.length == 1 && (tokens[0].image.equalsIgnoreCase(IValID.V_BOTTOM) || tokens[0].image.equalsIgnoreCase(IValID.V_TOP)))
					str = "";//$NON-NLS-1$
				else
					str = tokens[0].image;
			}
		}
		return (str != null) ? str : "";//$NON-NLS-1$
	}

	/**
	 * 
	 */
	public void set(ICSS2Properties properties, String value) throws org.w3c.dom.DOMException {
		String newValue = null;
		String valH = value;
		String valV = properties.getBackgroundPositionY();
		if (valV == null || valV.length() == 0)
			newValue = valH;
		else if (valH == null || valH.length() == 0) {
			Collection valX = PropCMSubProperty.getInstanceOf(PropCMSubProperty.PSUB_BG_POSITION_X).getValues();
			Collection valY = PropCMSubProperty.getInstanceOf(PropCMSubProperty.PSUB_BG_POSITION_Y).getValues();
			PropCMUtil.minus(valY, valX);
			Iterator it = valY.iterator();
			while (it.hasNext()) {
				Object obj = it.next();
				if (obj.toString().equals(valV.toLowerCase())) {
					// need not compensate for ...
					newValue = valV;
					break;
				}
			}
			// compensate for Horizontal value
			if (newValue == null) {
				// check valV is length or not
				CSSTextParser parser = new CSSTextParser(CSSTextParser.MODE_DECLARATION_VALUE, valV.trim());
				CSSTextToken[] tokens = parser.getTokens();
				if (tokens != null && tokens.length > 0 && tokens[0].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_IDENT) {
					newValue = IValID.V_LEFT + " " + valV;//$NON-NLS-1$
				}
				else
					newValue = "0% " + valV;//$NON-NLS-1$
			}
		}
		else
			newValue = valH + " " + valV;//$NON-NLS-1$
		properties.setBackgroundPosition(newValue);
	}
}

Back to the top