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