1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbaccess.hxx"
26 
27 #include "columnsettings.hxx"
28 #include "dbastrings.hrc"
29 
30 /** === begin UNO includes === **/
31 #include <com/sun/star/beans/PropertyAttribute.hpp>
32 /** === end UNO includes === **/
33 
34 #include <cppuhelper/typeprovider.hxx>
35 #include <comphelper/property.hxx>
36 #include <tools/debug.hxx>
37 #include <tools/diagnose_ex.h>
38 
39 //........................................................................
40 namespace dbaccess
41 {
42 //........................................................................
43 
44 	/** === begin UNO using === **/
45 	using ::com::sun::star::uno::Reference;
46 	using ::com::sun::star::uno::XInterface;
47 	using ::com::sun::star::uno::UNO_QUERY;
48 	using ::com::sun::star::uno::UNO_QUERY_THROW;
49 	using ::com::sun::star::uno::UNO_SET_THROW;
50 	using ::com::sun::star::uno::Exception;
51 	using ::com::sun::star::uno::RuntimeException;
52 	using ::com::sun::star::uno::Any;
53 	using ::com::sun::star::uno::makeAny;
54 	using ::com::sun::star::uno::Sequence;
55 	using ::com::sun::star::uno::Type;
56     using ::com::sun::star::lang::IllegalArgumentException;
57     using ::com::sun::star::beans::XPropertySet;
58     using ::com::sun::star::beans::XPropertySetInfo;
59 	/** === end UNO using === **/
60     namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
61 
62     //==============================================================================
63     //= OColumnSettings
64     //==============================================================================
DBG_NAME(OColumnSettings)65     DBG_NAME( OColumnSettings )
66     //------------------------------------------------------------------------------
67     OColumnSettings::OColumnSettings()
68 	    :m_bHidden(sal_False)
69     {
70 	    DBG_CTOR( OColumnSettings, NULL );
71     }
72 
73     //------------------------------------------------------------------------------
~OColumnSettings()74     OColumnSettings::~OColumnSettings()
75     {
76 	    DBG_DTOR( OColumnSettings, NULL );
77     }
78 
79     //------------------------------------------------------------------------------
registerProperties(IPropertyContainer & _rPropertyContainer)80     void OColumnSettings::registerProperties( IPropertyContainer& _rPropertyContainer )
81     {
82         const sal_Int32 nBoundAttr = PropertyAttribute::BOUND;
83         const sal_Int32 nMayBeVoidAttr = PropertyAttribute::MAYBEVOID | nBoundAttr;
84 
85         const Type& rSalInt32Type = ::getCppuType( static_cast< sal_Int32* >( NULL ) );
86         const Type& rStringType = ::getCppuType( static_cast< ::rtl::OUString* >( NULL ) );
87 
88         _rPropertyContainer.registerMayBeVoidProperty( PROPERTY_ALIGN, PROPERTY_ID_ALIGN, nMayBeVoidAttr, &m_aAlignment, rSalInt32Type );
89         _rPropertyContainer.registerMayBeVoidProperty( PROPERTY_NUMBERFORMAT, PROPERTY_ID_NUMBERFORMAT, nMayBeVoidAttr, &m_aFormatKey, rSalInt32Type );
90         _rPropertyContainer.registerMayBeVoidProperty( PROPERTY_RELATIVEPOSITION, PROPERTY_ID_RELATIVEPOSITION, nMayBeVoidAttr, &m_aRelativePosition, rSalInt32Type );
91         _rPropertyContainer.registerMayBeVoidProperty( PROPERTY_WIDTH, PROPERTY_ID_WIDTH, nMayBeVoidAttr, &m_aWidth, rSalInt32Type );
92         _rPropertyContainer.registerMayBeVoidProperty( PROPERTY_HELPTEXT, PROPERTY_ID_HELPTEXT, nMayBeVoidAttr, &m_aHelpText, rStringType );
93         _rPropertyContainer.registerMayBeVoidProperty( PROPERTY_CONTROLDEFAULT, PROPERTY_ID_CONTROLDEFAULT, nMayBeVoidAttr, &m_aControlDefault, rStringType );
94         _rPropertyContainer.registerProperty( PROPERTY_CONTROLMODEL, PROPERTY_ID_CONTROLMODEL, nBoundAttr, &m_xControlModel, ::getCppuType( &m_xControlModel ) );
95         _rPropertyContainer.registerProperty( PROPERTY_HIDDEN, PROPERTY_ID_HIDDEN, nBoundAttr, &m_bHidden, ::getCppuType( &m_bHidden ) );
96     }
97 
98     //------------------------------------------------------------------------------
isColumnSettingProperty(const sal_Int32 _nPropertyHandle)99     bool OColumnSettings::isColumnSettingProperty( const sal_Int32 _nPropertyHandle )
100     {
101         return  ( _nPropertyHandle == PROPERTY_ID_ALIGN )
102             ||  ( _nPropertyHandle == PROPERTY_ID_NUMBERFORMAT )
103             ||  ( _nPropertyHandle == PROPERTY_ID_RELATIVEPOSITION )
104             ||  ( _nPropertyHandle == PROPERTY_ID_WIDTH )
105             ||  ( _nPropertyHandle == PROPERTY_ID_HELPTEXT )
106             ||  ( _nPropertyHandle == PROPERTY_ID_CONTROLDEFAULT )
107             ||  ( _nPropertyHandle == PROPERTY_ID_CONTROLMODEL )
108             ||  ( _nPropertyHandle == PROPERTY_ID_HIDDEN );
109     }
110 
111     //------------------------------------------------------------------------------
isDefaulted(const sal_Int32 _nPropertyHandle,const Any & _rPropertyValue)112     bool OColumnSettings::isDefaulted( const sal_Int32 _nPropertyHandle, const Any& _rPropertyValue )
113     {
114         switch ( _nPropertyHandle )
115         {
116         case PROPERTY_ID_ALIGN:
117         case PROPERTY_ID_NUMBERFORMAT:
118         case PROPERTY_ID_RELATIVEPOSITION:
119         case PROPERTY_ID_WIDTH:
120         case PROPERTY_ID_HELPTEXT:
121         case PROPERTY_ID_CONTROLDEFAULT:
122             return !_rPropertyValue.hasValue();
123 
124         case PROPERTY_ID_CONTROLMODEL:
125             return !Reference< XPropertySet >( _rPropertyValue, UNO_QUERY ).is();
126 
127         case PROPERTY_ID_HIDDEN:
128             {
129                 sal_Bool bHidden = sal_False;
130                 OSL_VERIFY( _rPropertyValue >>= bHidden );
131                 return !bHidden;
132             }
133         }
134         OSL_ENSURE( false, "OColumnSettings::isDefaulted: illegal property handle!" );
135         return sal_False;
136     }
137 
138     //------------------------------------------------------------------------------
hasDefaultSettings(const Reference<XPropertySet> & _rxColumn)139     bool OColumnSettings::hasDefaultSettings( const Reference< XPropertySet >& _rxColumn )
140     {
141         ENSURE_OR_THROW( _rxColumn.is(), "illegal column" );
142         try
143         {
144             Reference< XPropertySetInfo > xPSI( _rxColumn->getPropertySetInfo(), UNO_SET_THROW );
145 
146             struct PropertyDescriptor
147             {
148                 ::rtl::OUString sName;
149                 sal_Int32       nHandle;
150             };
151             PropertyDescriptor aProps[] =
152             {
153                 { PROPERTY_ALIGN,            PROPERTY_ID_ALIGN },
154                 { PROPERTY_NUMBERFORMAT,     PROPERTY_ID_NUMBERFORMAT },
155                 { PROPERTY_RELATIVEPOSITION, PROPERTY_ID_RELATIVEPOSITION },
156                 { PROPERTY_WIDTH,            PROPERTY_ID_WIDTH },
157                 { PROPERTY_HELPTEXT,         PROPERTY_ID_HELPTEXT },
158                 { PROPERTY_CONTROLDEFAULT,   PROPERTY_ID_CONTROLDEFAULT },
159                 { PROPERTY_CONTROLMODEL,     PROPERTY_ID_CONTROLMODEL },
160                 { PROPERTY_HIDDEN,           PROPERTY_ID_HIDDEN }
161             };
162 
163             for ( size_t i=0; i < sizeof( aProps ) / sizeof( aProps[0] ); ++i )
164             {
165                 if ( xPSI->hasPropertyByName( aProps[i].sName ) )
166                     if ( !isDefaulted( aProps[i].nHandle, _rxColumn->getPropertyValue( aProps[i].sName ) ) )
167                         return false;
168             }
169         }
170         catch( const Exception& )
171         {
172         	DBG_UNHANDLED_EXCEPTION();
173         }
174         return true;
175     }
176 
177 //........................................................................
178 } // namespace dbaccess
179 //........................................................................
180