Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / odk / examples / java / Inspector / UnoPropertyNode.java
blob51b6569b2002c34070d2914600896e014bd6f158
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 import com.sun.star.beans.Property;
37 import com.sun.star.beans.PropertyValue;
38 import com.sun.star.lang.XServiceInfo;
39 import com.sun.star.reflection.XPropertyTypeDescription;
40 import com.sun.star.uno.UnoRuntime;
42 public class UnoPropertyNode extends UnoNode{
44 private Property aProperty;
45 private PropertyValue aPropertyValue;
46 private String m_sPropertyName;
47 private Object m_oUnoReturnObject;
48 private int m_nPropertyType = XUnoPropertyNode.nDEFAULT;
49 private String sLabel = "";
52 /** Creates a new instance of UnoMethodNode */
53 public UnoPropertyNode(Property _aProperty, Object _oUnoObject, Object _oUnoReturnObject) {
54 super(_oUnoObject);
55 aProperty = _aProperty;
56 m_sPropertyName = aProperty.Name;
57 m_oUnoReturnObject = _oUnoReturnObject;
61 public UnoPropertyNode(Property _aProperty){
62 super(null);
63 aProperty = _aProperty;
64 m_sPropertyName = aProperty.Name;
65 m_oUnoReturnObject = null;
68 public UnoPropertyNode(PropertyValue _aPropertyValue, Object _oUnoObject, Object _oUnoReturnObject) {
69 super(_oUnoObject);
70 m_oUnoReturnObject = _oUnoReturnObject;
71 aPropertyValue = _aPropertyValue;
72 m_sPropertyName = aPropertyValue.Name;
76 public int getPropertyNodeType(){
77 return m_nPropertyType;
81 public void setPropertyNodeType(int _nPropertyType){
82 m_nPropertyType = _nPropertyType;
86 public String getPropertyName(){
87 return m_sPropertyName;
90 public String getName(){
91 return this.m_sPropertyName;
95 @Override
96 public String getClassName(){
97 String sClassName = "";
98 if (m_oUnoObject != null){
99 XServiceInfo xServiceInfo = UnoRuntime.queryInterface(XServiceInfo.class, m_oUnoObject);
100 if (xServiceInfo != null){
101 String[] sServiceNames = xServiceInfo.getSupportedServiceNames();
102 for (int i = 0; i < sServiceNames.length; i++){
103 if (doesServiceSupportProperty(sServiceNames[i], m_sPropertyName)){
104 sClassName = sServiceNames[i];
105 break;
110 else{
111 sClassName = "com.sun.star.beans.Property";
113 return sClassName;
117 @Override
118 public String getAnchor(){
119 return m_sPropertyName;
124 private boolean doesServiceSupportProperty(String _sServiceName, String _sPropertyName){
125 try {
126 XPropertyTypeDescription[] xPropertyTypeDescriptions = Introspector.getIntrospector().getPropertyDescriptionsOfService(_sServiceName);
127 for (int i = 0; i < xPropertyTypeDescriptions.length; i++){
128 if (xPropertyTypeDescriptions[i].getName().equals(_sServiceName + "." + _sPropertyName)){
129 return true;
132 } catch ( java.lang.Exception e) {
133 System.out.println(System.out);
135 return false;
139 public Object getUnoReturnObject(){
140 return m_oUnoReturnObject;
144 private boolean isPrimitive(){
145 boolean bIsPrimitive = true;
146 if (getUnoReturnObject() != null){
147 if (getProperty() != null){
148 bIsPrimitive = Introspector.isObjectPrimitive(getUnoReturnObject().getClass(), getProperty().Type.getTypeClass());
150 else{
151 bIsPrimitive = Introspector.isObjectPrimitive(getUnoReturnObject().getClass());
154 else{
155 bIsPrimitive = Introspector.isObjectPrimitive(aProperty.Type.getTypeClass());
157 return bIsPrimitive;
161 protected boolean isFoldable(){
162 boolean bIsFoldable = false;
163 if (! isPrimitive()){
164 String sTypeName = getUnoReturnObject().getClass().getName();
165 bIsFoldable = (!sTypeName.equals("com.sun.star.uno.Type"));
167 return bIsFoldable;
171 protected String getLabel(){
172 if (!sLabel.equals("")){
173 if (! isPrimitive()){
174 if (isFoldable()){
175 sLabel = getPropertyTypeDescription(aProperty, getUnoReturnObject());
177 else{
178 sLabel = getStandardPropertyDescription(aProperty, getUnoReturnObject());
181 else {
182 sLabel = getStandardPropertyDescription(aProperty, getUnoReturnObject());
185 return sLabel;
188 public Property getProperty(){
189 return aProperty;
192 private static String getPropertyTypeDescription(Property _aProperty, Object _oUnoObject){
193 return _aProperty.Type.getTypeName() + " " + _aProperty.Name + " = " + _oUnoObject.toString();
197 protected static String getStandardPropertyDescription(Property _aProperty, Object _objectElement){
198 if (!Introspector.isObjectPrimitive(_objectElement)){
199 return _aProperty.Name + " = (" + _aProperty.Type.getTypeName() + ") ";
201 else{
202 return _aProperty.Name + " (" + _aProperty.Type.getTypeName() + ") = " + getDisplayValueOfPrimitiveType(_objectElement);
207 protected static String getStandardPropertyValueDescription(PropertyValue _aPropertyValue){
208 if (!Introspector.isObjectPrimitive(_aPropertyValue.Value)){
209 return _aPropertyValue.Name;
211 else{
212 return _aPropertyValue.Name + " : " + UnoNode.getDisplayValueOfPrimitiveType(_aPropertyValue.Value);
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */