xref: /AOO41X/main/odk/examples/java/Inspector/UnoPropertyNode.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir import com.sun.star.beans.Property;
36*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
37*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
38*cdf0e10cSrcweir import com.sun.star.reflection.TypeDescriptionSearchDepth;
39*cdf0e10cSrcweir import com.sun.star.reflection.XConstantTypeDescription;
40*cdf0e10cSrcweir import com.sun.star.reflection.XPropertyTypeDescription;
41*cdf0e10cSrcweir import com.sun.star.reflection.XServiceTypeDescription;
42*cdf0e10cSrcweir import com.sun.star.reflection.XTypeDescription;
43*cdf0e10cSrcweir import com.sun.star.reflection.XTypeDescriptionEnumeration;
44*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
45*cdf0e10cSrcweir import com.sun.star.uno.TypeClass;
46*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
47*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
48*cdf0e10cSrcweir import javax.swing.tree.DefaultMutableTreeNode;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir public class UnoPropertyNode extends UnoNode{
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir     Property aProperty;
53*cdf0e10cSrcweir     PropertyValue aPropertyValue;
54*cdf0e10cSrcweir     String m_sPropertyName;
55*cdf0e10cSrcweir     Object m_oUnoReturnObject;
56*cdf0e10cSrcweir     private int m_nPropertyType = XUnoPropertyNode.nDEFAULT;
57*cdf0e10cSrcweir     private String sLabel = "";
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     private static XConstantTypeDescription[] xPropertyAttributesTypeDescriptions = null;
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir     /** Creates a new instance of UnoMethodNode */
63*cdf0e10cSrcweir     public UnoPropertyNode(Property _aProperty, Object _oUnoObject, Object _oUnoReturnObject) {
64*cdf0e10cSrcweir         super(_oUnoObject);
65*cdf0e10cSrcweir         aProperty = _aProperty;
66*cdf0e10cSrcweir         m_sPropertyName = aProperty.Name;
67*cdf0e10cSrcweir         m_oUnoReturnObject = _oUnoReturnObject;
68*cdf0e10cSrcweir     }
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir     public UnoPropertyNode(Property _aProperty){
72*cdf0e10cSrcweir         super(null);
73*cdf0e10cSrcweir         aProperty = _aProperty;
74*cdf0e10cSrcweir         m_sPropertyName = aProperty.Name;
75*cdf0e10cSrcweir         m_oUnoReturnObject = null;
76*cdf0e10cSrcweir     }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir     public UnoPropertyNode(PropertyValue _aPropertyValue, Object _oUnoObject, Object _oUnoReturnObject) {
79*cdf0e10cSrcweir         super(_oUnoObject);
80*cdf0e10cSrcweir         m_oUnoReturnObject = _oUnoReturnObject;
81*cdf0e10cSrcweir         aPropertyValue = _aPropertyValue;
82*cdf0e10cSrcweir         m_sPropertyName = aPropertyValue.Name;
83*cdf0e10cSrcweir     }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir     public int getPropertyNodeType(){
87*cdf0e10cSrcweir         return m_nPropertyType;
88*cdf0e10cSrcweir     }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir     public void setPropertyNodeType(int _nPropertyType){
92*cdf0e10cSrcweir         m_nPropertyType = _nPropertyType;
93*cdf0e10cSrcweir     }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir     public String getPropertyName(){
97*cdf0e10cSrcweir         return m_sPropertyName;
98*cdf0e10cSrcweir     }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir     public String getName(){
101*cdf0e10cSrcweir         return this.m_sPropertyName;
102*cdf0e10cSrcweir     }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     public String getClassName(){
106*cdf0e10cSrcweir         String sClassName = "";
107*cdf0e10cSrcweir         if (m_oUnoObject != null){
108*cdf0e10cSrcweir             XServiceInfo xServiceInfo = (XServiceInfo) UnoRuntime.queryInterface(XServiceInfo.class, m_oUnoObject);
109*cdf0e10cSrcweir             if (xServiceInfo != null){
110*cdf0e10cSrcweir                 String[] sServiceNames = xServiceInfo.getSupportedServiceNames();
111*cdf0e10cSrcweir                 for (int i = 0; i < sServiceNames.length; i++){
112*cdf0e10cSrcweir                     if (doesServiceSupportProperty(sServiceNames[i], m_sPropertyName)){
113*cdf0e10cSrcweir                         sClassName = sServiceNames[i];
114*cdf0e10cSrcweir                         break;
115*cdf0e10cSrcweir                     }
116*cdf0e10cSrcweir                 }
117*cdf0e10cSrcweir             }
118*cdf0e10cSrcweir         }
119*cdf0e10cSrcweir         else{
120*cdf0e10cSrcweir             sClassName = "com.sun.star.beans.Property";
121*cdf0e10cSrcweir         }
122*cdf0e10cSrcweir         return sClassName;
123*cdf0e10cSrcweir     }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     public String getAnchor(){
127*cdf0e10cSrcweir         return m_sPropertyName;
128*cdf0e10cSrcweir     }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir     protected boolean doesServiceSupportProperty(String _sServiceName, String _sPropertyName){
133*cdf0e10cSrcweir     try {
134*cdf0e10cSrcweir         XPropertyTypeDescription[] xPropertyTypeDescriptions = Introspector.getIntrospector().getPropertyDescriptionsOfService(_sServiceName);
135*cdf0e10cSrcweir         for (int i = 0; i < xPropertyTypeDescriptions.length; i++){
136*cdf0e10cSrcweir             if (xPropertyTypeDescriptions[i].getName().equals(_sServiceName + "." + _sPropertyName)){
137*cdf0e10cSrcweir                 return true;
138*cdf0e10cSrcweir             }
139*cdf0e10cSrcweir         }
140*cdf0e10cSrcweir     } catch ( java.lang.Exception e) {
141*cdf0e10cSrcweir         System.out.println(System.out);
142*cdf0e10cSrcweir     }
143*cdf0e10cSrcweir         return false;
144*cdf0e10cSrcweir     }
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir     public Object getUnoReturnObject(){
148*cdf0e10cSrcweir         return m_oUnoReturnObject;
149*cdf0e10cSrcweir     }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir     private boolean isPrimitive(){
153*cdf0e10cSrcweir         boolean bIsPrimitive = true;
154*cdf0e10cSrcweir         if (getUnoReturnObject() != null){
155*cdf0e10cSrcweir             if (getProperty() != null){
156*cdf0e10cSrcweir                 bIsPrimitive = Introspector.isObjectPrimitive(getUnoReturnObject().getClass(), getProperty().Type.getTypeClass());
157*cdf0e10cSrcweir             }
158*cdf0e10cSrcweir             else{
159*cdf0e10cSrcweir                 bIsPrimitive = Introspector.isObjectPrimitive(getUnoReturnObject().getClass());
160*cdf0e10cSrcweir             }
161*cdf0e10cSrcweir         }
162*cdf0e10cSrcweir         else{
163*cdf0e10cSrcweir             bIsPrimitive = Introspector.isObjectPrimitive(aProperty.Type.getTypeClass());
164*cdf0e10cSrcweir         }
165*cdf0e10cSrcweir         return bIsPrimitive;
166*cdf0e10cSrcweir     }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir     protected boolean isFoldable(){
170*cdf0e10cSrcweir         boolean bIsFoldable = false;
171*cdf0e10cSrcweir         if (! isPrimitive()){
172*cdf0e10cSrcweir             String sTypeName = getUnoReturnObject().getClass().getName();
173*cdf0e10cSrcweir             bIsFoldable = (!sTypeName.equals("com.sun.star.uno.Type"));
174*cdf0e10cSrcweir         }
175*cdf0e10cSrcweir         return bIsFoldable;
176*cdf0e10cSrcweir     }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir     protected String getLabel(){
180*cdf0e10cSrcweir         if (!sLabel.equals("")){
181*cdf0e10cSrcweir             if (! isPrimitive()){
182*cdf0e10cSrcweir                 if (isFoldable()){
183*cdf0e10cSrcweir                     sLabel = getPropertyTypeDescription(aProperty, getUnoReturnObject());
184*cdf0e10cSrcweir                 }
185*cdf0e10cSrcweir                 else{
186*cdf0e10cSrcweir                     sLabel = getStandardPropertyDescription(aProperty, getUnoReturnObject());
187*cdf0e10cSrcweir                 }
188*cdf0e10cSrcweir             }
189*cdf0e10cSrcweir             else {
190*cdf0e10cSrcweir                 sLabel =  getStandardPropertyDescription(aProperty, getUnoReturnObject());
191*cdf0e10cSrcweir             }
192*cdf0e10cSrcweir         }
193*cdf0e10cSrcweir         return sLabel;
194*cdf0e10cSrcweir     }
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir     public Property getProperty(){
197*cdf0e10cSrcweir         return aProperty;
198*cdf0e10cSrcweir     }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir     protected  static String getPropertyTypeDescription(Property _aProperty, Object _oUnoObject){
201*cdf0e10cSrcweir         return _aProperty.Type.getTypeName() + " " + _aProperty.Name + " = " + _oUnoObject.toString();
202*cdf0e10cSrcweir     }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir     protected static String getStandardPropertyDescription(Property _aProperty, Object _objectElement){
206*cdf0e10cSrcweir         if (!Introspector.isObjectPrimitive(_objectElement)){
207*cdf0e10cSrcweir             return _aProperty.Name + " = (" + _aProperty.Type.getTypeName() + ") ";
208*cdf0e10cSrcweir         }
209*cdf0e10cSrcweir         else{
210*cdf0e10cSrcweir             return _aProperty.Name + " (" + _aProperty.Type.getTypeName() + ") = " + getDisplayValueOfPrimitiveType(_objectElement);
211*cdf0e10cSrcweir         }
212*cdf0e10cSrcweir     }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir     protected static String getStandardPropertyValueDescription(PropertyValue _aPropertyValue){
216*cdf0e10cSrcweir         if (!Introspector.isObjectPrimitive(_aPropertyValue.Value)){
217*cdf0e10cSrcweir             return _aPropertyValue.Name;
218*cdf0e10cSrcweir         }
219*cdf0e10cSrcweir         else{
220*cdf0e10cSrcweir             return _aPropertyValue.Name + " : " + UnoNode.getDisplayValueOfPrimitiveType(_aPropertyValue.Value);
221*cdf0e10cSrcweir         }
222*cdf0e10cSrcweir     }
223*cdf0e10cSrcweir }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir 
226