1 /*************************************************************************
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * Copyright 2000, 2010 Oracle and/or its affiliates.
5  *
6  * OpenOffice.org - a multi-platform office productivity suite
7  *
8  * This file is part of OpenOffice.org.
9  *
10  * OpenOffice.org is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License version 3
12  * only, as published by the Free Software Foundation.
13  *
14  * OpenOffice.org is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License version 3 for more details
18  * (a copy is included in the LICENSE file that accompanied this code).
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * version 3 along with OpenOffice.org.  If not, see
22  * <http://www.openoffice.org/license.html>
23  * for a copy of the LGPLv3 License.
24  *
25 ************************************************************************/
26 
27 // MARKER(update_precomp.py): autogen include statement, do not remove
28 #include "precompiled_dbaccess.hxx"
29 
30 #include "columnsettings.hxx"
31 #include "dbastrings.hrc"
32 
33 /** === begin UNO includes === **/
34 #include <com/sun/star/beans/PropertyAttribute.hpp>
35 /** === end UNO includes === **/
36 
37 #include <cppuhelper/typeprovider.hxx>
38 #include <comphelper/property.hxx>
39 #include <tools/debug.hxx>
40 #include <tools/diagnose_ex.h>
41 
42 //........................................................................
43 namespace dbaccess
44 {
45 //........................................................................
46 
47 	/** === begin UNO using === **/
48 	using ::com::sun::star::uno::Reference;
49 	using ::com::sun::star::uno::XInterface;
50 	using ::com::sun::star::uno::UNO_QUERY;
51 	using ::com::sun::star::uno::UNO_QUERY_THROW;
52 	using ::com::sun::star::uno::UNO_SET_THROW;
53 	using ::com::sun::star::uno::Exception;
54 	using ::com::sun::star::uno::RuntimeException;
55 	using ::com::sun::star::uno::Any;
56 	using ::com::sun::star::uno::makeAny;
57 	using ::com::sun::star::uno::Sequence;
58 	using ::com::sun::star::uno::Type;
59     using ::com::sun::star::lang::IllegalArgumentException;
60     using ::com::sun::star::beans::XPropertySet;
61     using ::com::sun::star::beans::XPropertySetInfo;
62 	/** === end UNO using === **/
63     namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
64 
65     //==============================================================================
66     //= OColumnSettings
67     //==============================================================================
68     DBG_NAME( OColumnSettings )
69     //------------------------------------------------------------------------------
70     OColumnSettings::OColumnSettings()
71 	    :m_bHidden(sal_False)
72     {
73 	    DBG_CTOR( OColumnSettings, NULL );
74     }
75 
76     //------------------------------------------------------------------------------
77     OColumnSettings::~OColumnSettings()
78     {
79 	    DBG_DTOR( OColumnSettings, NULL );
80     }
81 
82     //------------------------------------------------------------------------------
83     void OColumnSettings::registerProperties( IPropertyContainer& _rPropertyContainer )
84     {
85         const sal_Int32 nBoundAttr = PropertyAttribute::BOUND;
86         const sal_Int32 nMayBeVoidAttr = PropertyAttribute::MAYBEVOID | nBoundAttr;
87 
88         const Type& rSalInt32Type = ::getCppuType( static_cast< sal_Int32* >( NULL ) );
89         const Type& rStringType = ::getCppuType( static_cast< ::rtl::OUString* >( NULL ) );
90 
91         _rPropertyContainer.registerMayBeVoidProperty( PROPERTY_ALIGN, PROPERTY_ID_ALIGN, nMayBeVoidAttr, &m_aAlignment, rSalInt32Type );
92         _rPropertyContainer.registerMayBeVoidProperty( PROPERTY_NUMBERFORMAT, PROPERTY_ID_NUMBERFORMAT, nMayBeVoidAttr, &m_aFormatKey, rSalInt32Type );
93         _rPropertyContainer.registerMayBeVoidProperty( PROPERTY_RELATIVEPOSITION, PROPERTY_ID_RELATIVEPOSITION, nMayBeVoidAttr, &m_aRelativePosition, rSalInt32Type );
94         _rPropertyContainer.registerMayBeVoidProperty( PROPERTY_WIDTH, PROPERTY_ID_WIDTH, nMayBeVoidAttr, &m_aWidth, rSalInt32Type );
95         _rPropertyContainer.registerMayBeVoidProperty( PROPERTY_HELPTEXT, PROPERTY_ID_HELPTEXT, nMayBeVoidAttr, &m_aHelpText, rStringType );
96         _rPropertyContainer.registerMayBeVoidProperty( PROPERTY_CONTROLDEFAULT, PROPERTY_ID_CONTROLDEFAULT, nMayBeVoidAttr, &m_aControlDefault, rStringType );
97         _rPropertyContainer.registerProperty( PROPERTY_CONTROLMODEL, PROPERTY_ID_CONTROLMODEL, nBoundAttr, &m_xControlModel, ::getCppuType( &m_xControlModel ) );
98         _rPropertyContainer.registerProperty( PROPERTY_HIDDEN, PROPERTY_ID_HIDDEN, nBoundAttr, &m_bHidden, ::getCppuType( &m_bHidden ) );
99     }
100 
101     //------------------------------------------------------------------------------
102     bool OColumnSettings::isColumnSettingProperty( const sal_Int32 _nPropertyHandle )
103     {
104         return  ( _nPropertyHandle == PROPERTY_ID_ALIGN )
105             ||  ( _nPropertyHandle == PROPERTY_ID_NUMBERFORMAT )
106             ||  ( _nPropertyHandle == PROPERTY_ID_RELATIVEPOSITION )
107             ||  ( _nPropertyHandle == PROPERTY_ID_WIDTH )
108             ||  ( _nPropertyHandle == PROPERTY_ID_HELPTEXT )
109             ||  ( _nPropertyHandle == PROPERTY_ID_CONTROLDEFAULT )
110             ||  ( _nPropertyHandle == PROPERTY_ID_CONTROLMODEL )
111             ||  ( _nPropertyHandle == PROPERTY_ID_HIDDEN );
112     }
113 
114     //------------------------------------------------------------------------------
115     bool OColumnSettings::isDefaulted( const sal_Int32 _nPropertyHandle, const Any& _rPropertyValue )
116     {
117         switch ( _nPropertyHandle )
118         {
119         case PROPERTY_ID_ALIGN:
120         case PROPERTY_ID_NUMBERFORMAT:
121         case PROPERTY_ID_RELATIVEPOSITION:
122         case PROPERTY_ID_WIDTH:
123         case PROPERTY_ID_HELPTEXT:
124         case PROPERTY_ID_CONTROLDEFAULT:
125             return !_rPropertyValue.hasValue();
126 
127         case PROPERTY_ID_CONTROLMODEL:
128             return !Reference< XPropertySet >( _rPropertyValue, UNO_QUERY ).is();
129 
130         case PROPERTY_ID_HIDDEN:
131             {
132                 sal_Bool bHidden = sal_False;
133                 OSL_VERIFY( _rPropertyValue >>= bHidden );
134                 return !bHidden;
135             }
136         }
137         OSL_ENSURE( false, "OColumnSettings::isDefaulted: illegal property handle!" );
138         return sal_False;
139     }
140 
141     //------------------------------------------------------------------------------
142     bool OColumnSettings::hasDefaultSettings( const Reference< XPropertySet >& _rxColumn )
143     {
144         ENSURE_OR_THROW( _rxColumn.is(), "illegal column" );
145         try
146         {
147             Reference< XPropertySetInfo > xPSI( _rxColumn->getPropertySetInfo(), UNO_SET_THROW );
148 
149             struct PropertyDescriptor
150             {
151                 ::rtl::OUString sName;
152                 sal_Int32       nHandle;
153             };
154             PropertyDescriptor aProps[] =
155             {
156                 { PROPERTY_ALIGN,            PROPERTY_ID_ALIGN },
157                 { PROPERTY_NUMBERFORMAT,     PROPERTY_ID_NUMBERFORMAT },
158                 { PROPERTY_RELATIVEPOSITION, PROPERTY_ID_RELATIVEPOSITION },
159                 { PROPERTY_WIDTH,            PROPERTY_ID_WIDTH },
160                 { PROPERTY_HELPTEXT,         PROPERTY_ID_HELPTEXT },
161                 { PROPERTY_CONTROLDEFAULT,   PROPERTY_ID_CONTROLDEFAULT },
162                 { PROPERTY_CONTROLMODEL,     PROPERTY_ID_CONTROLMODEL },
163                 { PROPERTY_HIDDEN,           PROPERTY_ID_HIDDEN }
164             };
165 
166             for ( size_t i=0; i < sizeof( aProps ) / sizeof( aProps[0] ); ++i )
167             {
168                 if ( xPSI->hasPropertyByName( aProps[i].sName ) )
169                     if ( !isDefaulted( aProps[i].nHandle, _rxColumn->getPropertyValue( aProps[i].sName ) ) )
170                         return false;
171             }
172         }
173         catch( const Exception& )
174         {
175         	DBG_UNHANDLED_EXCEPTION();
176         }
177         return true;
178     }
179 
180 //........................................................................
181 } // namespace dbaccess
182 //........................................................................
183