0N/A/*
2362N/A * Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing.text.html;
0N/A
0N/Aimport java.util.Enumeration;
0N/Aimport java.awt.*;
0N/Aimport javax.swing.*;
0N/Aimport javax.swing.text.*;
0N/Aimport java.beans.*;
0N/Aimport java.lang.reflect.*;
0N/A
0N/A/**
0N/A * Component decorator that implements the view interface
0N/A * for <object> elements.
0N/A * <p>
0N/A * This view will try to load the class specified by the
0N/A * <code>classid</code> attribute. If possible, the Classloader
0N/A * used to load the associated Document is used.
0N/A * This would typically be the same as the ClassLoader
0N/A * used to load the EditorKit. If the document's
0N/A * ClassLoader is null, <code>Class.forName</code> is used.
0N/A * <p>
0N/A * If the class can successfully be loaded, an attempt will
0N/A * be made to create an instance of it by calling
0N/A * <code>Class.newInstance</code>. An attempt will be made
0N/A * to narrow the instance to type <code>java.awt.Component</code>
0N/A * to display the object.
0N/A * <p>
0N/A * This view can also manage a set of parameters with limitations.
0N/A * The parameters to the &lt;object&gt; element are expected to
0N/A * be present on the associated elements attribute set as simple
0N/A * strings. Each bean property will be queried as a key on
0N/A * the AttributeSet, with the expectation that a non-null value
0N/A * (of type String) will be present if there was a parameter
0N/A * specification for the property. Reflection is used to
0N/A * set the parameter. Currently, this is limited to a very
0N/A * simple single parameter of type String.
0N/A * <p>
0N/A * A simple example HTML invocation is:
0N/A * <pre>
0N/A * &lt;object classid="javax.swing.JLabel"&gt;
0N/A * &lt;param name="text" value="sample text"&gt;
0N/A * &lt;/object&gt;
0N/A * </pre>
0N/A *
0N/A * @author Timothy Prinzing
0N/A */
0N/Apublic class ObjectView extends ComponentView {
0N/A
0N/A /**
0N/A * Creates a new ObjectView object.
0N/A *
0N/A * @param elem the element to decorate
0N/A */
0N/A public ObjectView(Element elem) {
0N/A super(elem);
0N/A }
0N/A
0N/A /**
0N/A * Create the component. The classid is used
0N/A * as a specification of the classname, which
0N/A * we try to load.
0N/A */
0N/A protected Component createComponent() {
0N/A AttributeSet attr = getElement().getAttributes();
0N/A String classname = (String) attr.getAttribute(HTML.Attribute.CLASSID);
0N/A try {
0N/A Class c = Class.forName(classname, true,Thread.currentThread().
0N/A getContextClassLoader());
0N/A Object o = c.newInstance();
0N/A if (o instanceof Component) {
0N/A Component comp = (Component) o;
0N/A setParameters(comp, attr);
0N/A return comp;
0N/A }
0N/A } catch (Throwable e) {
0N/A // couldn't create a component... fall through to the
0N/A // couldn't load representation.
0N/A }
0N/A
0N/A return getUnloadableRepresentation();
0N/A }
0N/A
0N/A /**
0N/A * Fetch a component that can be used to represent the
0N/A * object if it can't be created.
0N/A */
0N/A Component getUnloadableRepresentation() {
0N/A // PENDING(prinz) get some artwork and return something
0N/A // interesting here.
0N/A Component comp = new JLabel("??");
0N/A comp.setForeground(Color.red);
0N/A return comp;
0N/A }
0N/A
0N/A /**
0N/A * Get a Class object to use for loading the
0N/A * classid. If possible, the Classloader
0N/A * used to load the associated Document is used.
0N/A * This would typically be the same as the ClassLoader
0N/A * used to load the EditorKit. If the documents
0N/A * ClassLoader is null,
0N/A * <code>Class.forName</code> is used.
0N/A */
0N/A private Class getClass(String classname) throws ClassNotFoundException {
0N/A Class klass;
0N/A
0N/A Class docClass = getDocument().getClass();
0N/A ClassLoader loader = docClass.getClassLoader();
0N/A if (loader != null) {
0N/A klass = loader.loadClass(classname);
0N/A } else {
0N/A klass = Class.forName(classname);
0N/A }
0N/A return klass;
0N/A }
0N/A
0N/A /**
0N/A * Initialize this component according the KEY/VALUEs passed in
0N/A * via the &lt;param&gt; elements in the corresponding
0N/A * &lt;object&gt; element.
0N/A */
0N/A private void setParameters(Component comp, AttributeSet attr) {
0N/A Class k = comp.getClass();
0N/A BeanInfo bi;
0N/A try {
0N/A bi = Introspector.getBeanInfo(k);
0N/A } catch (IntrospectionException ex) {
0N/A System.err.println("introspector failed, ex: "+ex);
0N/A return; // quit for now
0N/A }
0N/A PropertyDescriptor props[] = bi.getPropertyDescriptors();
0N/A for (int i=0; i < props.length; i++) {
0N/A // System.err.println("checking on props[i]: "+props[i].getName());
0N/A Object v = attr.getAttribute(props[i].getName());
0N/A if (v instanceof String) {
0N/A // found a property parameter
0N/A String value = (String) v;
0N/A Method writer = props[i].getWriteMethod();
0N/A if (writer == null) {
0N/A // read-only property. ignore
0N/A return; // for now
0N/A }
0N/A Class[] params = writer.getParameterTypes();
0N/A if (params.length != 1) {
0N/A // zero or more than one argument, ignore
0N/A return; // for now
0N/A }
0N/A Object [] args = { value };
0N/A try {
0N/A writer.invoke(comp, args);
0N/A } catch (Exception ex) {
0N/A System.err.println("Invocation failed");
0N/A // invocation code
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A}