/******************************************************************************* * Copyright (c) 2008, Original authors. * 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: * Angelo ZERR *******************************************************************************/ package org.akrogen.tkui.css.swing.dom; import java.awt.Component; import java.awt.Container; import java.io.StringReader; import java.io.StringWriter; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JToolTip; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.akrogen.tkui.css.core.dom.ElementAdapter; import org.akrogen.tkui.css.core.engine.CSSEngine; import org.akrogen.tkui.css.core.utils.ClassUtils; import org.apache.commons.beanutils.PropertyUtils; import org.apache.xml.serialize.OutputFormat; import org.apache.xml.serialize.XMLSerializer; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; /** * w3c Element which wrap Swing component. * * @version 1.0.0 * @author Angelo ZERR * */ public class SwingElement extends ElementAdapter implements NodeList { private String defaultHTMLContent = null; private Document document; protected String localName; protected String namespaceURI; public SwingElement(Component component, CSSEngine engine) { super(component, engine); this.localName = computeLocalName(); this.namespaceURI = computeNamespaceURI(); storeDefaultHTMLContentIfNeeded(component); } protected String computeLocalName() { // The localName is simple class name // of the Swing component. For instance // for the javax.swing.JLabel // localName is JLabel // CSS selector will use this localName // ex : JLabel {background-color:red;} Component component = getComponent(); Class clazz = component.getClass(); return ClassUtils.getSimpleName(clazz); } protected String computeNamespaceURI() { // The NamespaceURI is package name // of the Swing component. For instance // for the javax.swing.JLabel // namespaceURI is javax.swing // CSS selector will use this localName // @namespace swing javax.swing // ex : swing|JLabel {background-color:red;} Component component = getComponent(); Class clazz = component.getClass(); return ClassUtils.getPackageName(clazz); } public String getAttribute(String attr) { Component component = getComponent(); if (component instanceof JComponent) { JComponent c = (JComponent) component; Object o = c.getClientProperty(attr.toLowerCase()); if (o != null) return o.toString(); } try { Object o = PropertyUtils.getProperty(component, attr); if (o != null) return o.toString(); } catch (Exception e) { // e.printStackTrace(); } return ""; } /* * (non-Javadoc) * * @see org.akrogen.tkui.core.css.dom.ElementAdapter#getLocalName() */ public String getLocalName() { return localName; } public String getNamespaceURI() { return namespaceURI; } public Node getParentNode() { Component component = getComponent(); Container parent = component.getParent(); if (component instanceof JToolTip) { // Component is JToolTip, parent is Component parent = ((JToolTip)component).getComponent(); } if (parent != null) { Element element = getElement(parent); return element; } return null; } public NodeList getChildNodes() { if (defaultHTMLContent != null) { // load the HTML Content into DOM try { // Load XML into DOM DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); this.document = builder.parse(new InputSource(new StringReader( defaultHTMLContent))); return document.getDocumentElement().getChildNodes(); } catch (Exception e) { e.printStackTrace(); } } return this; } public int getLength() { Component component = getComponent(); if (component instanceof Container) return ((Container) component).getComponentCount(); return 0; } public Node item(int index) { Component component = getComponent(); if (component instanceof Container) { Component c = ((Container) component).getComponent(index); return getElement(c); } return null; } protected Component getComponent() { return (Component) getNativeWidget(); } protected JComponent getJComponent() { if (getNativeWidget() instanceof JComponent) return (JComponent) getNativeWidget(); return null; } public String getCSSId() { Component component = getComponent(); if (component instanceof JComponent) { Object id = ((JComponent) component).getClientProperty("id"); if (id != null) return id.toString(); } return component.getName(); } public String getCSSClass() { Component component = getComponent(); if (component instanceof JComponent) { Object id = ((JComponent) component).getClientProperty("class"); if (id != null) return id.toString(); } return null; } public String getCSSStyle() { Component component = getComponent(); if (component instanceof JComponent) { Object style = ((JComponent) component).getClientProperty("style"); if (style != null) return style.toString(); } return null; } public boolean isPseudoInstanceOf(String s) { if ("disabled".equals(s)) { Component component = getComponent(); return !component.isEnabled(); } if ("focus".equals(s)) { Component component = getComponent(); return component.hasFocus(); } if ("hover".equals(s)) { JComponent component = getJComponent(); if (component == null) return false; return (component.getClientProperty("mouseMoved") != null); } return false; } public void onStylesApplied(NodeList nodes) { if (defaultHTMLContent != null && getNativeWidget() instanceof JLabel) { JLabel label = (JLabel) getNativeWidget(); Element root = (Element) nodes.item(0).getParentNode(); String htmlContent = getHTMLContent(root); if (htmlContent != null) label.setText(htmlContent); } super.onStylesApplied(nodes); } protected void storeDefaultHTMLContentIfNeeded(Component component) { if (!(component instanceof JLabel)) return; JLabel label = (JLabel) component; String text = label.getText(); if (text.startsWith("") && text.endsWith("")) { this.defaultHTMLContent = text; } } protected String getHTMLContent(Element element) { StringWriter writer = new StringWriter(); OutputFormat outputFormat = new OutputFormat(); outputFormat.setOmitXMLDeclaration(true); XMLSerializer serializer = new XMLSerializer(writer, outputFormat); try { serializer.serialize(element); return writer.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } }