Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Inspector / UnoPropertyNode.java
blob0417f71d16d11f83a87a1ecfc0e1dc846941e0a9
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
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.
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.
33 *************************************************************************/
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.XPropertyTypeDescription;
39 import com.sun.star.uno.UnoRuntime;
41 public class UnoPropertyNode extends UnoNode{
43 private Property aProperty;
44 private PropertyValue aPropertyValue;
45 private String m_sPropertyName;
46 private Object m_oUnoReturnObject;
47 private int m_nPropertyType = XUnoPropertyNode.nDEFAULT;
48 private String sLabel = "";
51 /** Creates a new instance of UnoMethodNode */
52 public UnoPropertyNode(Property _aProperty, Object _oUnoObject, Object _oUnoReturnObject) {
53 super(_oUnoObject);
54 aProperty = _aProperty;
55 m_sPropertyName = aProperty.Name;
56 m_oUnoReturnObject = _oUnoReturnObject;
60 public UnoPropertyNode(Property _aProperty){
61 super(null);
62 aProperty = _aProperty;
63 m_sPropertyName = aProperty.Name;
64 m_oUnoReturnObject = null;
67 public UnoPropertyNode(PropertyValue _aPropertyValue, Object _oUnoObject, Object _oUnoReturnObject) {
68 super(_oUnoObject);
69 m_oUnoReturnObject = _oUnoReturnObject;
70 aPropertyValue = _aPropertyValue;
71 m_sPropertyName = aPropertyValue.Name;
75 public int getPropertyNodeType(){
76 return m_nPropertyType;
80 public void setPropertyNodeType(int _nPropertyType){
81 m_nPropertyType = _nPropertyType;
85 public String getPropertyName(){
86 return m_sPropertyName;
89 public String getName(){
90 return this.m_sPropertyName;
94 @Override
95 public String getClassName(){
96 String sClassName = "";
97 if (m_oUnoObject != null){
98 XServiceInfo xServiceInfo = UnoRuntime.queryInterface(XServiceInfo.class, m_oUnoObject);
99 if (xServiceInfo != null){
100 String[] sServiceNames = xServiceInfo.getSupportedServiceNames();
101 for (int i = 0; i < sServiceNames.length; i++){
102 if (doesServiceSupportProperty(sServiceNames[i], m_sPropertyName)){
103 sClassName = sServiceNames[i];
104 break;
109 else{
110 sClassName = "com.sun.star.beans.Property";
112 return sClassName;
116 @Override
117 public String getAnchor(){
118 return m_sPropertyName;
123 private boolean doesServiceSupportProperty(String _sServiceName, String _sPropertyName){
124 try {
125 XPropertyTypeDescription[] xPropertyTypeDescriptions = Introspector.getIntrospector().getPropertyDescriptionsOfService(_sServiceName);
126 for (int i = 0; i < xPropertyTypeDescriptions.length; i++){
127 if (xPropertyTypeDescriptions[i].getName().equals(_sServiceName + "." + _sPropertyName)){
128 return true;
131 } catch ( java.lang.Exception e) {
132 System.out.println(System.out);
134 return false;
138 public Object getUnoReturnObject(){
139 return m_oUnoReturnObject;
143 private boolean isPrimitive(){
144 boolean bIsPrimitive = true;
145 if (getUnoReturnObject() != null){
146 if (getProperty() != null){
147 bIsPrimitive = Introspector.isObjectPrimitive(getUnoReturnObject().getClass(), getProperty().Type.getTypeClass());
149 else{
150 bIsPrimitive = Introspector.isObjectPrimitive(getUnoReturnObject().getClass());
153 else{
154 bIsPrimitive = Introspector.isObjectPrimitive(aProperty.Type.getTypeClass());
156 return bIsPrimitive;
160 protected boolean isFoldable(){
161 boolean bIsFoldable = false;
162 if (! isPrimitive()){
163 String sTypeName = getUnoReturnObject().getClass().getName();
164 bIsFoldable = (!sTypeName.equals("com.sun.star.uno.Type"));
166 return bIsFoldable;
170 protected String getLabel(){
171 if (!sLabel.equals("")){
172 if (! isPrimitive()){
173 if (isFoldable()){
174 sLabel = getPropertyTypeDescription(aProperty, getUnoReturnObject());
176 else{
177 sLabel = getStandardPropertyDescription(aProperty, getUnoReturnObject());
180 else {
181 sLabel = getStandardPropertyDescription(aProperty, getUnoReturnObject());
184 return sLabel;
187 public Property getProperty(){
188 return aProperty;
191 private static String getPropertyTypeDescription(Property _aProperty, Object _oUnoObject){
192 return _aProperty.Type.getTypeName() + " " + _aProperty.Name + " = " + _oUnoObject.toString();
196 protected static String getStandardPropertyDescription(Property _aProperty, Object _objectElement){
197 if (!Introspector.isObjectPrimitive(_objectElement)){
198 return _aProperty.Name + " = (" + _aProperty.Type.getTypeName() + ") ";
200 else{
201 return _aProperty.Name + " (" + _aProperty.Type.getTypeName() + ") = " + getDisplayValueOfPrimitiveType(_objectElement);
206 protected static String getStandardPropertyValueDescription(PropertyValue _aPropertyValue){
207 if (!Introspector.isObjectPrimitive(_aPropertyValue.Value)){
208 return _aPropertyValue.Name;
210 else{
211 return _aPropertyValue.Name + " : " + UnoNode.getDisplayValueOfPrimitiveType(_aPropertyValue.Value);