/* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ package com.sun.star.lib.uno.helper; import com.sun.star.beans.Property; import com.sun.star.beans.PropertyAttribute; import com.sun.star.beans.PropertyChangeEvent; import com.sun.star.beans.PropertyState; import com.sun.star.beans.PropertyValue; import com.sun.star.beans.PropertyVetoException; import com.sun.star.beans.UnknownPropertyException; import com.sun.star.beans.XPropertyChangeListener; import com.sun.star.beans.XPropertySetInfo; import com.sun.star.beans.XVetoableChangeListener; import com.sun.star.container.NoSuchElementException; import com.sun.star.container.XHierarchicalNameAccess; import com.sun.star.lang.DisposedException; import com.sun.star.lang.EventObject; import com.sun.star.lang.WrappedTargetException; import com.sun.star.lang.WrappedTargetRuntimeException; import com.sun.star.reflection.XCompoundTypeDescription; import com.sun.star.reflection.XIdlClass; import com.sun.star.reflection.XIdlField2; import com.sun.star.reflection.XIndirectTypeDescription; import com.sun.star.reflection.XInterfaceAttributeTypeDescription2; import com.sun.star.reflection.XInterfaceMemberTypeDescription; import com.sun.star.reflection.XInterfaceTypeDescription2; import com.sun.star.reflection.XStructTypeDescription; import com.sun.star.reflection.XTypeDescription; import com.sun.star.reflection.theCoreReflection; import com.sun.star.uno.Any; import com.sun.star.uno.AnyConverter; import com.sun.star.uno.Type; import com.sun.star.uno.TypeClass; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XComponentContext; import com.sun.star.uno.XInterface; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; /** A helper mixin to implement certain UNO interfaces related to property set handling on top of the attributes of a given UNO interface type.
A client will mix in this class by keeping a reference to an instance of
this class, and forwarding all methods of (a subset of the interfaces)
com.sun.star.beans.XPropertySet
,
com.sun.star.beans.XFastPropertySet
, and
com.sun.star.beans.XPropertyAccess
to it.
Client code should not use the monitors associated with instances of this class, as they are used for internal purposes.
@since UDK 3.2 */ public final class PropertySetMixin { /** The constructor. @param context the component context used by this instance; must not be null, and must supply thecom.sun.star.reflection.theCoreReflection
and
com.sun.star.reflection.theTypeDescriptionManager
singletons
@param object the client UNO object into which this instance is mixed in;
must not be null, and must support the given type
@param type the UNO interface type whose attributes are mapped to
properties; must not be null, and must represent a UNO interface type
@param absentOptional a list of optional properties that are not present,
and should thus not be visible via
com.sun.star.beans.XPropertySet.getPropertySetInfo
,
com.sun.star.beans.XPropertySet.addPropertyChangeListener
,
com.sun.star.beans.XPropertySet.removePropertyChangeListener
,
com.sun.star.beans.XPropertySet.addVetoableChangeListener
,
and com.sun.star.beans.XPropertySet.removeVetoableChangeListener
; null is treated the same as an
empty list; if non-null, the given array must not be modified after it is
passed to this constructor. For consistency reasons, the given
absentOptional
should only contain the names of attributes
that represent optional properties that are not present (that is, the
attribute getters and setters always throw a
com.sun.star.beans.UnknownPropertyException
), and should
contain each such name only once. If an optional property is not present
(that is, the corresponding attribute getter and setter always throw a
com.sun.star.beans.UnknownPropertyException
) but is not
contained in the given absentOptional
, then it will be
visible via
com.sun.star.beans.XPropertySet.getPropertySetInfo
as a
com.sun.star.beans.Property
with a set
com.sun.star.beans.PropertyAttribute.OPTIONAL
. If the given
object
does not implement
com.sun.star.beans.XPropertySet
, then the given
absentOptional
is effectively ignored and can be null or
empty.
*/
public PropertySetMixin(
XComponentContext context, XInterface object, Type type,
String[] absentOptional)
{
this.context = context;
this.object = object;
this.type = type;
this.absentOptional = absentOptional;
idlClass = getReflection(type.getTypeName());
XTypeDescription ifc;
try {
XHierarchicalNameAccess xhna = UnoRuntime.queryInterface(
XHierarchicalNameAccess.class,
context.getValueByName(
"/singletons/com.sun.star.reflection."
+ "theTypeDescriptionManager"));
ifc = UnoRuntime.queryInterface(
XTypeDescription.class,
xhna.getByHierarchicalName(type.getTypeName()));
} catch (NoSuchElementException e) {
throw new RuntimeException(e);
}
HashMapFirst, this method checks whether this instance has already been
disposed (see {@link #dispose}), and throws a
com.sun.star.beans.DisposedException
if applicable. For a
constrained attribute (whose setter can explicitly raise
com.sun.star.beans.PropertyVetoException
), this method
notifies any com.sun.star.beans.XVetoableChangeListener
s.
For a bound attribute, this method modifies the passed-in
bound
so that it can afterwards be used to notify any
com.sun.star.beans.XPropertyChangeListener
s. This method
should be called before storing the new attribute value, and
bound.notifyListeners()
should be called exactly once after
storing the new attribute value (in case the attribute is bound;
otherwise, calling bound.notifyListeners()
is ignored).
Furthermore, bound.notifyListeners()
and this method have to
be called from the same thread.
com.sun.star.beans.PropertyChangeEvent.OldValue
, which is
rather useless, anyway (see “Using the Observer Pattern” in
OpenOffice.org Coding Guidelines). If the attribute
that is going to be set is neither bound nor constrained, or if
com.sun.star.beans.PropertyChangeEvent.OldValue
should not
be set, {@link Any#VOID} can be used instead.
@param newValue the property value corresponding to the new
attribute value. This is only used as
com.sun.star.beans.PropertyChangeEvent.NewValue
, which is
rather useless, anyway (see “Using the Observer Pattern” in
OpenOffice.org Coding Guidelines), unless the
attribute that is going to be set is constrained. If the attribute
that is going to be set is neither bound nor constrained, or if it is
only bound but
com.sun.star.beans.PropertyChangeEvent.NewValue
should not
be set, {@link Any#VOID} can be used instead.
@param bound a reference to a fresh {@link BoundListeners} instance
(which has not been passed to this method before, and on which
{@link BoundListeners#notifyListeners} has not yet been called); may only
be null if the attribute that is going to be set is not bound
@throws PropertyVetoException if a vetoable listener throws it.
*/
public void prepareSet(
String propertyName, Object oldValue, Object newValue,
BoundListeners bound)
throws PropertyVetoException
{
// assert properties.get(propertyName) != null;
Property p = properties.get(propertyName).property;
ArrayListThis method is useful for attributes that are not constrained.
@param propertyName the name of the property (which is the same as the name of the attribute that is going to be set) @param bound a reference to a fresh {@link BoundListeners} instance (which has not been passed to this method before, and on which {@link BoundListeners#notifyListeners} has not yet been called); may only be null if the attribute that is going to be set is not bound */ public void prepareSet(String propertyName, BoundListeners bound) { try { prepareSet(propertyName, Any.VOID, Any.VOID, bound); } catch (PropertyVetoException e) { throw new RuntimeException("unexpected " + e); } } /** Marks this instance as being disposed.See com.sun.star.lang.XComponent
for the general concept
of disposing UNO objects. On the first call to this method, all
registered listeners
(com.sun.star.beans.XPropertyChangeListener
s and
com.sun.star.beans.XVetoableChangeListener
s) are notified of
the disposing source. Any subsequent calls to this method are
ignored.
com.sun.star.beans.XPropertySet.getPropertySetInfo
.
@return See com.sun.star.beans.XPropertySet
*/
public XPropertySetInfo getPropertySetInfo() {
return new Info(properties);
}
/**
Implements com.sun.star.beans.XPropertySet.setPropertyValue
.
@param propertyName
See com.sun.star.beans.XPropertySet
@param value
See com.sun.star.beans.XPropertySet
@throws UnknownPropertyException
See com.sun.star.beans.XPropertySet
@throws PropertyVetoException
See com.sun.star.beans.XPropertySet
@throws WrappedTargetException
See com.sun.star.beans.XPropertySet
*/
public void setPropertyValue(String propertyName, Object value)
throws UnknownPropertyException, PropertyVetoException,
com.sun.star.lang.IllegalArgumentException, WrappedTargetException
{
setProperty(propertyName, value, false, false, (short) 1);
}
/**
Implements com.sun.star.beans.XPropertySet.getPropertyValue
.
@param propertyName
See com.sun.star.beans.XPropertySet
@throws UnknownPropertyException
See com.sun.star.beans.XPropertySet
@throws WrappedTargetException
See com.sun.star.beans.XPropertySet
@return
See com.sun.star.beans.XPropertySet
*/
public Object getPropertyValue(String propertyName)
throws UnknownPropertyException, WrappedTargetException
{
return getProperty(propertyName, null);
}
/**
Implements
com.sun.star.beans.XPropertySet.addPropertyChangeListener
.
If a listener is added more than once, it will receive all relevant notifications multiple times.
@param propertyName See com.sun.star.beans.XPropertySet @param listener See com.sun.star.beans.XPropertySet @throws UnknownPropertyException See com.sun.star.beans.XPropertySet @throws WrappedTargetException See com.sun.star.beans.XPropertySet */ public void addPropertyChangeListener( String propertyName, XPropertyChangeListener listener) throws UnknownPropertyException, WrappedTargetException { // assert listener != null; checkUnknown(propertyName); boolean disp; synchronized (this) { disp = disposed; if (!disp) { ArrayList
com.sun.star.beans.XPropertySet.removePropertyChangeListener
.
@param propertyName
See com.sun.star.beans.XPropertySet
@param listener
See com.sun.star.beans.XPropertySet
@throws UnknownPropertyException
See com.sun.star.beans.XPropertySet
@throws WrappedTargetException
See com.sun.star.beans.XPropertySet
*/
public void removePropertyChangeListener(
String propertyName, XPropertyChangeListener listener)
throws UnknownPropertyException, WrappedTargetException
{
// assert listener != null;
checkUnknown(propertyName);
synchronized (this) {
if (boundListeners != null) {
ArrayListcom.sun.star.beans.XPropertySet.addVetoableChangeListener
.
If a listener is added more than once, it will receive all relevant notifications multiple times.
@param propertyName See com.sun.star.beans.XPropertySet @param listener See com.sun.star.beans.XPropertySet @throws UnknownPropertyException See com.sun.star.beans.XPropertySet @throws WrappedTargetException See com.sun.star.beans.XPropertySet */ public void addVetoableChangeListener( String propertyName, XVetoableChangeListener listener) throws UnknownPropertyException, WrappedTargetException { // assert listener != null; checkUnknown(propertyName); boolean disp; synchronized (this) { disp = disposed; if (!disp) { ArrayList
com.sun.star.beans.XPropertySet.removeVetoableChangeListener
.
@param propertyName
See com.sun.star.beans.XPropertySet
@param listener
See com.sun.star.beans.XPropertySet
@throws UnknownPropertyException
See com.sun.star.beans.XPropertySet
@throws WrappedTargetException
See com.sun.star.beans.XPropertySet
*/
public void removeVetoableChangeListener(
String propertyName, XVetoableChangeListener listener)
throws UnknownPropertyException, WrappedTargetException
{
// assert listener != null;
checkUnknown(propertyName);
synchronized (this) {
if (vetoListeners != null) {
ArrayListcom.sun.star.beans.XFastPropertySet.setFastPropertyValue
.
@param handle
See com.sun.star.beans.XFastPropertySet
@param value
See com.sun.star.beans.XFastPropertySet
@throws UnknownPropertyException
See com.sun.star.beans.XFastPropertySet
@throws PropertyVetoException
See com.sun.star.beans.XFastPropertySet
@throws WrappedTargetException
See com.sun.star.beans.XFastPropertySet
*/
public void setFastPropertyValue(int handle, Object value)
throws UnknownPropertyException, PropertyVetoException,
com.sun.star.lang.IllegalArgumentException, WrappedTargetException
{
setProperty(translateHandle(handle), value, false, false, (short) 1);
}
/**
Implements
com.sun.star.beans.XFastPropertySet.getFastPropertyValue
.
@param handle
See com.sun.star.beans.XFastPropertySet
@throws UnknownPropertyException
See com.sun.star.beans.XFastPropertySet
@throws WrappedTargetException
See com.sun.star.beans.XFastPropertySet
@return
See com.sun.star.beans.XFastPropertySet
*/
public Object getFastPropertyValue(int handle)
throws UnknownPropertyException, WrappedTargetException
{
return getProperty(translateHandle(handle), null);
}
/**
Implements
com.sun.star.beans.XPropertyAccess.getPropertyValues
.
@return
See com.sun.star.beans.XPropertyAccess
*/
public PropertyValue[] getPropertyValues() {
PropertyValue[] s = new PropertyValue[handleMap.length];
int n = 0;
for (int i = 0; i < handleMap.length; ++i) {
PropertyState[] state = new PropertyState[1];
Object value;
try {
value = getProperty(handleMap[i], state);
} catch (UnknownPropertyException e) {
continue;
} catch (WrappedTargetException e) {
throw new WrappedTargetRuntimeException(e.getCause(),
e.getMessage(), object, e.TargetException);
}
s[n++] = new PropertyValue(handleMap[i], i, value, state[0]);
}
if (n < handleMap.length) {
PropertyValue[] s2 = new PropertyValue[n];
System.arraycopy(s, 0, s2, 0, n);
s = s2;
}
return s;
}
/**
Implements
com.sun.star.beans.XPropertyAccess.setPropertyValues
.
@param props
See com.sun.star.beans.XPropertyAccess
@throws UnknownPropertyException
See com.sun.star.beans.XPropertyAccess
@throws PropertyVetoException
See com.sun.star.beans.XPropertyAccess
@throws WrappedTargetException
See com.sun.star.beans.XPropertyAccess
*/
public void setPropertyValues(PropertyValue[] props)
throws UnknownPropertyException, PropertyVetoException,
com.sun.star.lang.IllegalArgumentException, WrappedTargetException
{
for (int i = 0; i < props.length; ++i) {
if (props[i].Handle != -1
&& !props[i].Name.equals(translateHandle(props[i].Handle)))
{
throw new UnknownPropertyException(
("name " + props[i].Name + " does not match handle "
+ props[i].Handle),
object);
}
setProperty(
props[i].Name, props[i].Value,
props[i].State == PropertyState.AMBIGUOUS_VALUE,
props[i].State == PropertyState.DEFAULT_VALUE, (short) 0);
}
}
/**
A class used by clients of {@link PropertySetMixin} when implementing UNO
interface type attribute setter functions.
@see #prepareSet(String, Object, Object, PropertySetMixin.BoundListeners)
*/
public static final class BoundListeners {
/**
Notifies any
com.sun.star.beans.XPropertyChangeListener
s.
@see #prepareSet(String, Object, Object,
PropertySetMixin.BoundListeners)
*/
public void notifyListeners() {
if (specificListeners != null) {
for (Iterator