1 /*******************************************************************************
\r
2 * Copyright (c) 2008 Oracle Corporation.
\r
3 * All rights reserved. This program and the accompanying materials
\r
4 * are made available under the terms of the Eclipse Public License v1.0
\r
5 * which accompanies this distribution, and is available at
\r
6 * http://www.eclipse.org/legal/epl-v10.html
\r
9 * Ian Trimble - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.eclipse.jst.pagedesigner.css2.property;
\r
13 import java.net.MalformedURLException;
\r
14 import java.net.URI;
\r
15 import java.net.URL;
\r
17 import org.eclipse.core.resources.IContainer;
\r
18 import org.eclipse.core.resources.IResource;
\r
19 import org.eclipse.core.resources.IWorkspaceRoot;
\r
20 import org.eclipse.core.resources.ResourcesPlugin;
\r
21 import org.eclipse.jface.resource.ImageDescriptor;
\r
22 import org.eclipse.jface.resource.ImageRegistry;
\r
23 import org.eclipse.jst.pagedesigner.IHTMLConstants;
\r
24 import org.eclipse.jst.pagedesigner.PDPlugin;
\r
25 import org.eclipse.jst.pagedesigner.css2.ICSSStyle;
\r
26 import org.eclipse.jst.pagedesigner.utils.DOMUtil;
\r
27 import org.eclipse.swt.graphics.Image;
\r
28 import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
\r
29 import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
\r
30 import org.w3c.dom.Element;
\r
31 import org.w3c.dom.css.CSSValue;
\r
34 * Provides metadata for the "background-image" CSS property.
\r
36 * @author Ian Trimble - Oracle
\r
38 public class BackgroundImageMeta extends CSSPropertyMeta {
\r
40 private static final String[] _keywords = {ICSSPropertyID.VAL_NONE};
\r
43 * Construct an instance.
\r
45 public BackgroundImageMeta() {
\r
46 super(false, ICSSPropertyID.VAL_NONE);
\r
50 * @see org.eclipse.jst.pagedesigner.css2.property.CSSPropertyMeta#calculateCSSValueResult(org.w3c.dom.css.CSSValue, java.lang.String, org.eclipse.jst.pagedesigner.css2.ICSSStyle)
\r
53 public Object calculateCSSValueResult(CSSValue value, String propertyName,
\r
56 String valueText = value.getCssText();
\r
57 if (valueText != null && valueText.length() > 0) {
\r
58 valueText = stripURLSyntax(valueText);
\r
59 ret = getImage(valueText, null);
\r
62 ret = getInitialValue(propertyName, style);
\r
68 * @see org.eclipse.jst.pagedesigner.css2.property.CSSPropertyMeta#calculateHTMLAttributeOverride(org.w3c.dom.Element, java.lang.String, java.lang.String, org.eclipse.jst.pagedesigner.css2.ICSSStyle)
\r
71 public Object calculateHTMLAttributeOverride(Element element,
\r
72 String htmltag, String propertyName, ICSSStyle style) {
\r
76 element.getNodeName() != null &&
\r
77 element.getNodeName().equalsIgnoreCase(IHTMLConstants.TAG_BODY)) {
\r
78 if (ICSSPropertyID.ATTR_BACKGROUND_IMAGE.equalsIgnoreCase(propertyName)) {
\r
79 String attrValue = DOMUtil.getAttributeIgnoreCase(element, IHTMLConstants.ATTR_BACKGROUND);
\r
80 if (attrValue != null && attrValue.trim().length() > 0) {
\r
81 image = getImage(attrValue.trim(), element);
\r
89 * @see org.eclipse.jst.pagedesigner.css2.property.CSSPropertyMeta#getKeywordValues()
\r
92 protected String[] getKeywordValues() {
\r
96 private String stripURLSyntax(String input) {
\r
97 String output = null;
\r
98 if (input != null) {
\r
100 int startPos = input.indexOf("url(") + 4; //$NON-NLS-1$
\r
101 if (startPos > -1 && startPos < input.length() - 1) {
\r
102 int endPos = input.indexOf(')', startPos);
\r
103 if (endPos > startPos) {
\r
104 String insideURL = input.substring(startPos, endPos).trim();
\r
105 //strip double-quotes
\r
106 if (insideURL.startsWith("\"") && insideURL.endsWith("\"")) { //$NON-NLS-1$ //$NON-NLS-2$
\r
107 output = insideURL.substring(1, insideURL.length() - 1);
\r
108 //strip single-quotes
\r
109 } else if (insideURL.startsWith("'") && insideURL.endsWith("'")) { //$NON-NLS-1$ //$NON-NLS-2$
\r
110 output = insideURL.substring(1, insideURL.length() - 1);
\r
112 output = insideURL;
\r
117 return output != null ? output : input;
\r
120 /* Image instances returned from this method should not be disposed because
\r
121 * they are cached in the plug-in's ImageRegistry and will be disposed of
\r
124 private Image getImage(String imagePath, Element element) {
\r
125 Image image = null;
\r
126 if (imagePath != null && imagePath.length() > 0) {
\r
127 ImageRegistry registry = PDPlugin.getDefault().getImageRegistry();
\r
128 image = registry.get(imagePath);
\r
129 if (image == null) {
\r
131 URL imageURL = new URL(imagePath);
\r
132 ImageDescriptor imageDescriptor = ImageDescriptor.createFromURL(imageURL);
\r
133 image = imageDescriptor.createImage();
\r
134 if (image != null) {
\r
135 registry.put(imagePath, image);
\r
137 } catch(MalformedURLException mue) {
\r
138 //attempt to resolve as relative to document
\r
139 if (element instanceof IDOMNode) {
\r
140 IDOMModel model = ((IDOMNode)element).getModel();
\r
141 if (model != null) {
\r
142 String baseLocation = model.getBaseLocation();
\r
143 if (baseLocation != null && baseLocation.length() > 0) {
\r
144 IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();
\r
145 if (wsRoot != null) {
\r
146 IResource jspRes = wsRoot.findMember(baseLocation);
\r
147 if (jspRes != null) {
\r
148 IContainer jspFolder = jspRes.getParent();
\r
149 if (jspFolder != null) {
\r
150 IResource imageRes = jspFolder.findMember(imagePath);
\r
151 if (imageRes != null) {
\r
152 URI imageURI = imageRes.getLocationURI();
\r
153 if (imageURI != null) {
\r
155 URL imageURL = imageURI.toURL();
\r
156 ImageDescriptor imageDescriptor = ImageDescriptor.createFromURL(imageURL);
\r
157 image = imageDescriptor.createImage();
\r
158 if (image != null) {
\r
159 registry.put(imagePath, image);
\r
161 } catch(MalformedURLException mue2) {
\r
162 //ignore - what else can be done?
\r
173 } else if (image.isDisposed()) {
\r
174 //shouldn't be able to get here from there, but...just in case
\r
175 registry.remove(imagePath);
\r
176 image = getImage(imagePath, element);
\r