Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

aboutsummaryrefslogtreecommitdiffstats
blob: e106190fb79556c1aa8f651dfc3cbe4e9d9d36c7 (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
/*******************************************************************************
 * Copyright (c) 2001, 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.xml.ui.internal.editor;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.wst.xml.core.internal.contentmodel.CMNode;
import org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery;
import org.eclipse.wst.xml.core.internal.modelquery.ModelQueryUtil;
import org.eclipse.wst.xml.ui.internal.XMLUIPlugin;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * @author nsd
 */
public class CMImageUtil {

	public static CMNode getDeclaration(Node node) {
		CMNode decl = null;
		ModelQuery mq = null;
		switch (node.getNodeType()) {
			case Node.ATTRIBUTE_NODE : {
				mq = ModelQueryUtil.getModelQuery(((Attr) node).getOwnerDocument());
				decl = mq.getCMAttributeDeclaration((Attr) node);
			}
				break;
			case Node.ELEMENT_NODE : {
				mq = ModelQueryUtil.getModelQuery(node.getOwnerDocument());
				decl = mq.getCMElementDeclaration((Element) node);
			}
				break;
		}
		return decl;
	}

	public static Image getImage(CMNode cmnode) {
		if (cmnode == null)
			return null;
		Image image = null;
		ImageDescriptor descriptor = getImageDescriptor(cmnode);
		if (descriptor != null) {
			image = descriptor.createImage(false);
		}
		return image;
	}

	public static ImageDescriptor getImageDescriptor(CMNode cmnode) {
		if (cmnode == null)
			return null;
		// cache CM-specified images with the XML UI plugin
		String imageURLString = (String) cmnode.getProperty("small-icon"); //$NON-NLS-1$
		ImageDescriptor descriptor = null;
		if (imageURLString != null && imageURLString.length() > 0) {
			descriptor = XMLUIPlugin.getInstance().getImageRegistry()
					.getDescriptor(imageURLString);
			if (descriptor == null) {
				try {
					URL imageURL = new URL(imageURLString);
					URLConnection connection = imageURL.openConnection();
					connection.setUseCaches(false);
					ImageData data = new ImageData(connection.getInputStream());
					descriptor = ImageDescriptor.createFromImageData(data);
					XMLUIPlugin.getInstance().getImageRegistry().put(
							imageURLString, descriptor);
				} catch (MalformedURLException e) {
					descriptor = null;
				} catch (IOException e) {
					descriptor = null;
				}
			}
		}
		return descriptor;
	}

	/**
	 * 
	 */
	private CMImageUtil() {
		super();
	}
}

Back to the top