1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "precompiled_toolkit.hxx"
29 #include "gridcolumn.hxx"
30 
31 #include <comphelper/sequence.hxx>
32 #include <cppuhelper/typeprovider.hxx>
33 #include <toolkit/helper/servicenames.hxx>
34 
35 namespace toolkit
36 {
37     using namespace ::com::sun::star;
38     using namespace ::com::sun::star::uno;
39     using namespace ::com::sun::star::awt;
40     using namespace ::com::sun::star::awt::grid;
41     using namespace ::com::sun::star::lang;
42     using namespace ::com::sun::star::util;
43     using namespace ::com::sun::star::style;
44 
45     //==================================================================================================================
46     //= DefaultGridColumnModel
47     //==================================================================================================================
48     //------------------------------------------------------------------------------------------------------------------
49     GridColumn::GridColumn()
50         :GridColumn_Base( m_aMutex )
51         ,m_aIdentifier()
52         ,m_nIndex(-1)
53         ,m_nDataColumnIndex(-1)
54         ,m_nColumnWidth(4)
55         ,m_nMaxWidth(0)
56         ,m_nMinWidth(0)
57         ,m_nFlexibility(1)
58         ,m_bResizeable(true)
59         ,m_eHorizontalAlign( HorizontalAlignment_LEFT )
60     {
61     }
62 
63     //------------------------------------------------------------------------------------------------------------------
64     GridColumn::GridColumn( GridColumn const & i_copySource )
65         :cppu::BaseMutex()
66         ,GridColumn_Base( m_aMutex )
67         ,m_aIdentifier( i_copySource.m_aIdentifier )
68         ,m_nIndex( -1 )
69         ,m_nDataColumnIndex( i_copySource.m_nDataColumnIndex )
70         ,m_nColumnWidth( i_copySource.m_nColumnWidth )
71         ,m_nMaxWidth( i_copySource.m_nMaxWidth )
72         ,m_nMinWidth( i_copySource.m_nMinWidth )
73         ,m_nFlexibility( i_copySource.m_nFlexibility )
74         ,m_bResizeable( i_copySource.m_bResizeable )
75         ,m_sTitle( i_copySource.m_sTitle )
76         ,m_sHelpText( i_copySource.m_sHelpText )
77         ,m_eHorizontalAlign( i_copySource.m_eHorizontalAlign )
78     {
79     }
80 
81     //------------------------------------------------------------------------------------------------------------------
82     GridColumn::~GridColumn()
83     {
84     }
85 
86     //------------------------------------------------------------------------------------------------------------------
87     void GridColumn::broadcast_changed( sal_Char const * const i_asciiAttributeName, Any i_oldValue, Any i_newValue,
88         ::comphelper::ComponentGuard& i_Guard )
89     {
90 	    Reference< XInterface > const xSource( static_cast< ::cppu::OWeakObject* >( this ) );
91         GridColumnEvent const aEvent(
92             xSource, ::rtl::OUString::createFromAscii( i_asciiAttributeName ),
93             i_oldValue, i_newValue, m_nIndex
94         );
95 
96         ::cppu::OInterfaceContainerHelper* pIter = rBHelper.getContainer( XGridColumnListener::static_type() );
97 
98         i_Guard.clear();
99 	    if( pIter )
100             pIter->notifyEach( &XGridColumnListener::columnChanged, aEvent );
101     }
102 
103     //------------------------------------------------------------------------------------------------------------------
104     ::com::sun::star::uno::Any SAL_CALL GridColumn::getIdentifier() throw (::com::sun::star::uno::RuntimeException)
105     {
106         ::comphelper::ComponentGuard aGuard( *this, rBHelper );
107 	    return m_aIdentifier;
108     }
109 
110     //------------------------------------------------------------------------------------------------------------------
111     void SAL_CALL GridColumn::setIdentifier(const ::com::sun::star::uno::Any & value) throw (::com::sun::star::uno::RuntimeException)
112     {
113         ::comphelper::ComponentGuard aGuard( *this, rBHelper );
114 	    m_aIdentifier = value;
115     }
116 
117     //------------------------------------------------------------------------------------------------------------------
118     ::sal_Int32 SAL_CALL GridColumn::getColumnWidth() throw (::com::sun::star::uno::RuntimeException)
119     {
120         ::comphelper::ComponentGuard aGuard( *this, rBHelper );
121 	    return m_nColumnWidth;
122     }
123 
124     //------------------------------------------------------------------------------------------------------------------
125     void SAL_CALL GridColumn::setColumnWidth(::sal_Int32 value) throw (::com::sun::star::uno::RuntimeException)
126     {
127         impl_set( m_nColumnWidth, value, "ColumnWidth" );
128     }
129 
130     //------------------------------------------------------------------------------------------------------------------
131     ::sal_Int32 SAL_CALL GridColumn::getMaxWidth() throw (::com::sun::star::uno::RuntimeException)
132     {
133         ::comphelper::ComponentGuard aGuard( *this, rBHelper );
134 	    return m_nMaxWidth;
135     }
136 
137     //------------------------------------------------------------------------------------------------------------------
138     void SAL_CALL GridColumn::setMaxWidth(::sal_Int32 value) throw (::com::sun::star::uno::RuntimeException)
139     {
140         impl_set( m_nMaxWidth, value, "MaxWidth" );
141     }
142 
143     //------------------------------------------------------------------------------------------------------------------
144     ::sal_Int32 SAL_CALL GridColumn::getMinWidth() throw (::com::sun::star::uno::RuntimeException)
145     {
146         ::comphelper::ComponentGuard aGuard( *this, rBHelper );
147 	    return m_nMinWidth;
148     }
149 
150     //------------------------------------------------------------------------------------------------------------------
151     void SAL_CALL GridColumn::setMinWidth(::sal_Int32 value) throw (::com::sun::star::uno::RuntimeException)
152     {
153         impl_set( m_nMinWidth, value, "MinWidth" );
154     }
155 
156     //------------------------------------------------------------------------------------------------------------------
157     ::rtl::OUString SAL_CALL GridColumn::getTitle() throw (::com::sun::star::uno::RuntimeException)
158     {
159         ::comphelper::ComponentGuard aGuard( *this, rBHelper );
160 	    return m_sTitle;
161     }
162 
163     //------------------------------------------------------------------------------------------------------------------
164     void SAL_CALL GridColumn::setTitle(const ::rtl::OUString & value) throw (::com::sun::star::uno::RuntimeException)
165     {
166         impl_set( m_sTitle, value, "Title" );
167     }
168 
169     //------------------------------------------------------------------------------------------------------------------
170     ::rtl::OUString SAL_CALL GridColumn::getHelpText() throw (RuntimeException)
171     {
172         ::comphelper::ComponentGuard aGuard( *this, rBHelper );
173 	    return m_sHelpText;
174     }
175 
176     //------------------------------------------------------------------------------------------------------------------
177     void SAL_CALL GridColumn::setHelpText( const ::rtl::OUString & value ) throw (RuntimeException)
178     {
179         impl_set( m_sHelpText, value, "HelpText" );
180     }
181 
182     //------------------------------------------------------------------------------------------------------------------
183     sal_Bool SAL_CALL GridColumn::getResizeable() throw (::com::sun::star::uno::RuntimeException)
184     {
185         ::comphelper::ComponentGuard aGuard( *this, rBHelper );
186 	    return m_bResizeable;
187     }
188 
189     //------------------------------------------------------------------------------------------------------------------
190     void SAL_CALL GridColumn::setResizeable(sal_Bool value) throw (::com::sun::star::uno::RuntimeException)
191     {
192         impl_set( m_bResizeable, value, "Resizeable" );
193     }
194 
195     //------------------------------------------------------------------------------------------------------------------
196     ::sal_Int32 SAL_CALL GridColumn::getFlexibility() throw (RuntimeException)
197     {
198         ::comphelper::ComponentGuard aGuard( *this, rBHelper );
199         return m_nFlexibility;
200     }
201 
202     //------------------------------------------------------------------------------------------------------------------
203     void SAL_CALL GridColumn::setFlexibility( ::sal_Int32 i_value ) throw (IllegalArgumentException, RuntimeException)
204     {
205         if ( i_value < 0 )
206             throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
207         impl_set( m_nFlexibility, i_value, "Flexibility" );
208     }
209 
210     //------------------------------------------------------------------------------------------------------------------
211     HorizontalAlignment SAL_CALL GridColumn::getHorizontalAlign() throw (::com::sun::star::uno::RuntimeException)
212     {
213         ::comphelper::ComponentGuard aGuard( *this, rBHelper );
214 	    return m_eHorizontalAlign;
215     }
216 
217     //------------------------------------------------------------------------------------------------------------------
218     void SAL_CALL GridColumn::setHorizontalAlign(HorizontalAlignment align) throw (::com::sun::star::uno::RuntimeException)
219     {
220         impl_set( m_eHorizontalAlign, align, "HorizontalAlign" );
221     }
222 
223     //------------------------------------------------------------------------------------------------------------------
224     void SAL_CALL GridColumn::addGridColumnListener( const Reference< XGridColumnListener >& xListener ) throw (RuntimeException)
225     {
226 	    rBHelper.addListener( XGridColumnListener::static_type(), xListener );
227     }
228 
229     //------------------------------------------------------------------------------------------------------------------
230     void SAL_CALL GridColumn::removeGridColumnListener( const Reference< XGridColumnListener >& xListener ) throw (RuntimeException)
231     {
232 	    rBHelper.removeListener( XGridColumnListener::static_type(), xListener );
233     }
234 
235     //------------------------------------------------------------------------------------------------------------------
236     void SAL_CALL GridColumn::disposing()
237     {
238         ::osl::MutexGuard aGuard( m_aMutex );
239         m_aIdentifier.clear();
240         m_sTitle = m_sHelpText = ::rtl::OUString();
241     }
242 
243     //------------------------------------------------------------------------------------------------------------------
244     ::sal_Int32 SAL_CALL GridColumn::getIndex() throw (RuntimeException)
245     {
246         ::comphelper::ComponentGuard aGuard( *this, rBHelper );
247         return m_nIndex;
248     }
249 
250     //------------------------------------------------------------------------------------------------------------------
251     void GridColumn::setIndex( sal_Int32 const i_index )
252     {
253         ::comphelper::ComponentGuard aGuard( *this, rBHelper );
254 	    m_nIndex = i_index;
255     }
256 
257     //------------------------------------------------------------------------------------------------------------------
258     ::sal_Int32 SAL_CALL GridColumn::getDataColumnIndex() throw(RuntimeException)
259     {
260         ::comphelper::ComponentGuard aGuard( *this, rBHelper );
261         return m_nDataColumnIndex;
262     }
263 
264     //------------------------------------------------------------------------------------------------------------------
265     void SAL_CALL GridColumn::setDataColumnIndex( ::sal_Int32 i_dataColumnIndex ) throw(RuntimeException)
266     {
267         impl_set( m_nDataColumnIndex, i_dataColumnIndex, "DataColumnIndex" );
268     }
269 
270     //------------------------------------------------------------------------------------------------------------------
271     ::rtl::OUString SAL_CALL GridColumn::getImplementationName(  ) throw (RuntimeException)
272     {
273         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.comp.toolkit.GridColumn" ) );
274     }
275 
276     //------------------------------------------------------------------------------------------------------------------
277     sal_Bool SAL_CALL GridColumn::supportsService( const ::rtl::OUString& i_serviceName ) throw (RuntimeException)
278     {
279         const Sequence< ::rtl::OUString > aServiceNames( getSupportedServiceNames() );
280         for ( sal_Int32 i=0; i<aServiceNames.getLength(); ++i )
281             if ( aServiceNames[i] == i_serviceName )
282                 return sal_True;
283         return sal_False;
284     }
285 
286     //------------------------------------------------------------------------------------------------------------------
287     ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL GridColumn::getSupportedServiceNames(  ) throw (RuntimeException)
288     {
289         const ::rtl::OUString aServiceName( ::rtl::OUString::createFromAscii( szServiceName_GridColumn ) );
290         const Sequence< ::rtl::OUString > aSeq( &aServiceName, 1 );
291         return aSeq;
292     }
293 
294     //------------------------------------------------------------------------------------------------------------------
295     Reference< XCloneable > SAL_CALL GridColumn::createClone(  ) throw (RuntimeException)
296     {
297         return new GridColumn( *this );
298     }
299 
300     //------------------------------------------------------------------------------------------------------------------
301     sal_Int64 SAL_CALL GridColumn::getSomething( const Sequence< sal_Int8 >& i_identifier ) throw(RuntimeException)
302     {
303         if ( ( i_identifier.getLength() == 16 ) && ( i_identifier == getUnoTunnelId() ) )
304             return ::sal::static_int_cast< sal_Int64 >( reinterpret_cast< sal_IntPtr >( this ) );
305         return 0;
306     }
307 
308     //------------------------------------------------------------------------------------------------------------------
309     Sequence< sal_Int8 > GridColumn::getUnoTunnelId() throw()
310     {
311         static ::cppu::OImplementationId const aId;
312         return aId.getImplementationId();
313     }
314 
315     //------------------------------------------------------------------------------------------------------------------
316     GridColumn* GridColumn::getImplementation( const Reference< XInterface >& i_component )
317     {
318         Reference< XUnoTunnel > const xTunnel( i_component, UNO_QUERY );
319         if ( xTunnel.is() )
320             return reinterpret_cast< GridColumn* >( ::sal::static_int_cast< sal_IntPtr >( xTunnel->getSomething( getUnoTunnelId() ) ) );
321         return NULL;
322     }
323 }
324 
325 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL GridColumn_CreateInstance( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& )
326 {
327     return *( new ::toolkit::GridColumn );
328 }
329 
330