jdk/src/share/classes/javax/swing/text/html/ObjectView.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text.html;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Component decorator that implements the view interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * for <object> elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This view will try to load the class specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <code>classid</code> attribute.  If possible, the Classloader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * used to load the associated Document is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This would typically be the same as the ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * used to load the EditorKit.  If the document's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * ClassLoader is null, <code>Class.forName</code> is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * If the class can successfully be loaded, an attempt will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * be made to create an instance of it by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <code>Class.newInstance</code>.  An attempt will be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * to narrow the instance to type <code>java.awt.Component</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * to display the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * This view can also manage a set of parameters with limitations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The parameters to the &lt;object&gt; element are expected to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * be present on the associated elements attribute set as simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * strings.  Each bean property will be queried as a key on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * the AttributeSet, with the expectation that a non-null value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * (of type String) will be present if there was a parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * specification for the property.  Reflection is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * set the parameter.  Currently, this is limited to a very
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * simple single parameter of type String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * A simple example HTML invocation is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *      &lt;object classid="javax.swing.JLabel"&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *      &lt;param name="text" value="sample text"&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *      &lt;/object&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @author Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
public class ObjectView extends ComponentView  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Creates a new ObjectView object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param elem the element to decorate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public ObjectView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        super(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Create the component.  The classid is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * as a specification of the classname, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * we try to load.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected Component createComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        AttributeSet attr = getElement().getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        String classname = (String) attr.getAttribute(HTML.Attribute.CLASSID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            Class c = Class.forName(classname, true,Thread.currentThread().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                    getContextClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            Object o = c.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (o instanceof Component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                Component comp = (Component) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                setParameters(comp, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            // couldn't create a component... fall through to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            // couldn't load representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return getUnloadableRepresentation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Fetch a component that can be used to represent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * object if it can't be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    Component getUnloadableRepresentation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // PENDING(prinz) get some artwork and return something
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // interesting here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        Component comp = new JLabel("??");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        comp.setForeground(Color.red);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Get a Class object to use for loading the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * classid.  If possible, the Classloader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * used to load the associated Document is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * This would typically be the same as the ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * used to load the EditorKit.  If the documents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * ClassLoader is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <code>Class.forName</code> is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private Class getClass(String classname) throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        Class klass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        Class docClass = getDocument().getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        ClassLoader loader = docClass.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            klass = loader.loadClass(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            klass = Class.forName(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return klass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Initialize this component according the KEY/VALUEs passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * via the &lt;param&gt; elements in the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * &lt;object&gt; element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private void setParameters(Component comp, AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        Class k = comp.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        BeanInfo bi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            bi = Introspector.getBeanInfo(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } catch (IntrospectionException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            System.err.println("introspector failed, ex: "+ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            return;             // quit for now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        PropertyDescriptor props[] = bi.getPropertyDescriptors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        for (int i=0; i < props.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            //      System.err.println("checking on props[i]: "+props[i].getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            Object v = attr.getAttribute(props[i].getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            if (v instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                // found a property parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                String value = (String) v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                Method writer = props[i].getWriteMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                if (writer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    // read-only property. ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    return;     // for now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                Class[] params = writer.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                if (params.length != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    // zero or more than one argument, ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    return;     // for now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                Object [] args = { value };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    writer.invoke(comp, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    System.err.println("Invocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    // invocation code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
}