LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/forms/source/component - entrylisthelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 46 116 39.7 %
Date: 2013-07-09 Functions: 11 22 50.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "entrylisthelper.hxx"
      21             : #include "FormComponent.hxx"
      22             : 
      23             : #include <osl/diagnose.h>
      24             : #include <comphelper/sequence.hxx>
      25             : #include <comphelper/property.hxx>
      26             : #include <algorithm>
      27             : 
      28             : //.........................................................................
      29             : namespace frm
      30             : {
      31             : //.........................................................................
      32             : 
      33             :     using namespace ::com::sun::star::uno;
      34             :     using namespace ::com::sun::star::lang;
      35             :     using namespace ::com::sun::star::util;
      36             :     using namespace ::com::sun::star::form::binding;
      37             : 
      38             :     //=====================================================================
      39             :     //= OEntryListHelper
      40             :     //=====================================================================
      41             :     //---------------------------------------------------------------------
      42          24 :     OEntryListHelper::OEntryListHelper( OControlModel& _rControlModel )
      43             :         :m_rControlModel( _rControlModel )
      44          24 :         ,m_aRefreshListeners( _rControlModel.getInstanceMutex() )
      45             :     {
      46          24 :     }
      47             : 
      48             :     //---------------------------------------------------------------------
      49           2 :     OEntryListHelper::OEntryListHelper( const OEntryListHelper& _rSource, OControlModel& _rControlModel )
      50             :         :m_rControlModel( _rControlModel )
      51             :         ,m_xListSource ( _rSource.m_xListSource  )
      52             :         ,m_aStringItems( _rSource.m_aStringItems )
      53           2 :         ,m_aRefreshListeners( _rControlModel.getInstanceMutex() )
      54             :     {
      55           2 :     }
      56             : 
      57             :     //---------------------------------------------------------------------
      58          20 :     OEntryListHelper::~OEntryListHelper( )
      59             :     {
      60          20 :     }
      61             : 
      62             :     //---------------------------------------------------------------------
      63           3 :     void SAL_CALL OEntryListHelper::setListEntrySource( const Reference< XListEntrySource >& _rxSource ) throw (RuntimeException)
      64             :     {
      65           3 :         ControlModelLock aLock( m_rControlModel );
      66             : 
      67             :         // disconnect from the current external list source
      68           3 :         disconnectExternalListSource();
      69             : 
      70             :         // and connect to the new one
      71           3 :         if ( _rxSource.is() )
      72           3 :             connectExternalListSource( _rxSource, aLock );
      73           3 :     }
      74             : 
      75             :     //---------------------------------------------------------------------
      76          66 :     Reference< XListEntrySource > SAL_CALL OEntryListHelper::getListEntrySource(  ) throw (RuntimeException)
      77             :     {
      78          66 :         return m_xListSource;
      79             :     }
      80             : 
      81             : 
      82             :     //---------------------------------------------------------------------
      83           0 :     void SAL_CALL OEntryListHelper::entryChanged( const ListEntryEvent& _rEvent ) throw (RuntimeException)
      84             :     {
      85           0 :         ControlModelLock aLock( m_rControlModel );
      86             : 
      87             :         OSL_ENSURE( _rEvent.Source == m_xListSource,
      88             :             "OEntryListHelper::entryChanged: where did this come from?" );
      89             :         OSL_ENSURE( ( _rEvent.Position >= 0 ) && ( _rEvent.Position < m_aStringItems.getLength() ),
      90             :             "OEntryListHelper::entryChanged: invalid index!" );
      91             :         OSL_ENSURE( _rEvent.Entries.getLength() == 1,
      92             :             "OEntryListHelper::entryChanged: invalid string list!" );
      93             : 
      94           0 :         if  (   ( _rEvent.Position >= 0 )
      95           0 :             &&  ( _rEvent.Position < m_aStringItems.getLength() )
      96           0 :             &&  ( _rEvent.Entries.getLength() > 0 )
      97             :             )
      98             :         {
      99           0 :             m_aStringItems[ _rEvent.Position ] = _rEvent.Entries[ 0 ];
     100           0 :             stringItemListChanged( aLock );
     101           0 :         }
     102           0 :     }
     103             : 
     104             :     //---------------------------------------------------------------------
     105           0 :     void SAL_CALL OEntryListHelper::entryRangeInserted( const ListEntryEvent& _rEvent ) throw (RuntimeException)
     106             :     {
     107           0 :         ControlModelLock aLock( m_rControlModel );
     108             : 
     109             :         OSL_ENSURE( _rEvent.Source == m_xListSource,
     110             :             "OEntryListHelper::entryRangeInserted: where did this come from?" );
     111             :         OSL_ENSURE( ( _rEvent.Position > 0 ) && ( _rEvent.Position < m_aStringItems.getLength() ) && ( _rEvent.Entries.getLength() > 0 ),
     112             :             "OEntryListHelper::entryRangeRemoved: invalid count and/or position!" );
     113             : 
     114           0 :         if  (   ( _rEvent.Position > 0 )
     115           0 :             &&  ( _rEvent.Position < m_aStringItems.getLength() )
     116           0 :             &&  ( _rEvent.Entries.getLength() > 0 )
     117             :             )
     118             :         {
     119             :             // the entries *before* the insertion pos
     120             :             Sequence< OUString > aKeepEntries(
     121             :                 m_aStringItems.getConstArray(),
     122             :                 _rEvent.Position
     123           0 :             );
     124             :             // the entries *behind* the insertion pos
     125             :             Sequence< OUString > aMovedEntries(
     126           0 :                 m_aStringItems.getConstArray() + _rEvent.Position,
     127           0 :                 m_aStringItems.getLength() - _rEvent.Position
     128           0 :             );
     129             : 
     130             :             // concat all three parts
     131           0 :             m_aStringItems = ::comphelper::concatSequences(
     132             :                 aKeepEntries,
     133             :                 _rEvent.Entries,
     134             :                 aMovedEntries
     135           0 :             );
     136             : 
     137           0 :             stringItemListChanged( aLock );
     138           0 :         }
     139           0 :     }
     140             : 
     141             :     //---------------------------------------------------------------------
     142           0 :     void SAL_CALL OEntryListHelper::entryRangeRemoved( const ListEntryEvent& _rEvent ) throw (RuntimeException)
     143             :     {
     144           0 :         ControlModelLock aLock( m_rControlModel );
     145             : 
     146             :         OSL_ENSURE( _rEvent.Source == m_xListSource,
     147             :             "OEntryListHelper::entryRangeRemoved: where did this come from?" );
     148             :         OSL_ENSURE( ( _rEvent.Position > 0 ) && ( _rEvent.Count > 0 ) && ( _rEvent.Position + _rEvent.Count <= m_aStringItems.getLength() ),
     149             :             "OEntryListHelper::entryRangeRemoved: invalid count and/or position!" );
     150             : 
     151           0 :         if  (   ( _rEvent.Position > 0 )
     152           0 :             &&  ( _rEvent.Count > 0 )
     153           0 :             &&  ( _rEvent.Position + _rEvent.Count <= m_aStringItems.getLength() )
     154             :             )
     155             :         {
     156             :             // copy all items after the removed ones
     157             :             ::std::copy(
     158           0 :                 m_aStringItems.getConstArray() + _rEvent.Position + _rEvent.Count,
     159           0 :                 m_aStringItems.getConstArray() + m_aStringItems.getLength(),
     160           0 :                 m_aStringItems.getArray( ) + _rEvent.Position
     161           0 :             );
     162             :             // shrink the array
     163           0 :             m_aStringItems.realloc( m_aStringItems.getLength() - _rEvent.Count );
     164             : 
     165           0 :             stringItemListChanged( aLock );
     166           0 :         }
     167           0 :     }
     168             : 
     169             :     //---------------------------------------------------------------------
     170           0 :     void SAL_CALL OEntryListHelper::allEntriesChanged( const EventObject& _rEvent ) throw (RuntimeException)
     171             :     {
     172           0 :         ControlModelLock aLock( m_rControlModel );
     173             : 
     174             :         OSL_ENSURE( _rEvent.Source == m_xListSource,
     175             :             "OEntryListHelper::allEntriesChanged: where did this come from?" );
     176             : 
     177           0 :         Reference< XListEntrySource > xSource( _rEvent.Source, UNO_QUERY );
     178           0 :         if ( _rEvent.Source == m_xListSource )
     179             :         {
     180           0 :             impl_lock_refreshList( aLock );
     181           0 :         }
     182           0 :     }
     183             : 
     184             :     // XRefreshable
     185             :     //------------------------------------------------------------------------------
     186           0 :     void SAL_CALL OEntryListHelper::addRefreshListener(const Reference<XRefreshListener>& _rxListener) throw(RuntimeException)
     187             :     {
     188           0 :         if ( _rxListener.is() )
     189           0 :             m_aRefreshListeners.addInterface( _rxListener );
     190           0 :     }
     191             : 
     192             :     //------------------------------------------------------------------------------
     193           0 :     void SAL_CALL OEntryListHelper::removeRefreshListener(const Reference<XRefreshListener>& _rxListener) throw(RuntimeException)
     194             :     {
     195           0 :         if ( _rxListener.is() )
     196           0 :             m_aRefreshListeners.removeInterface( _rxListener );
     197           0 :     }
     198             : 
     199             :     //------------------------------------------------------------------------------
     200           0 :     void SAL_CALL OEntryListHelper::refresh() throw(RuntimeException)
     201             :     {
     202             :         {
     203           0 :             ControlModelLock aLock( m_rControlModel );
     204           0 :             impl_lock_refreshList( aLock );
     205             :         }
     206             : 
     207           0 :         EventObject aEvt( static_cast< XRefreshable* >( this ) );
     208           0 :         m_aRefreshListeners.notifyEach( &XRefreshListener::refreshed, aEvt );
     209           0 :     }
     210             : 
     211             :     //---------------------------------------------------------------------
     212           0 :     void OEntryListHelper::impl_lock_refreshList( ControlModelLock& _rInstanceLock )
     213             :     {
     214           0 :         if ( hasExternalListSource() )
     215             :         {
     216           0 :             m_aStringItems = m_xListSource->getAllListEntries( );
     217           0 :             stringItemListChanged( _rInstanceLock );
     218             :         }
     219             :         else
     220           0 :             refreshInternalEntryList();
     221           0 :     }
     222             : 
     223             :     //---------------------------------------------------------------------
     224           4 :     bool OEntryListHelper::handleDisposing( const EventObject& _rEvent )
     225             :     {
     226           4 :         if ( m_xListSource .is() && ( _rEvent.Source == m_xListSource ) )
     227             :         {
     228           0 :             disconnectExternalListSource( );
     229           0 :             return true;
     230             :         }
     231           4 :         return false;
     232             :     }
     233             : 
     234             :     //---------------------------------------------------------------------
     235          22 :     void OEntryListHelper::disposing( )
     236             :     {
     237          22 :         EventObject aEvt( static_cast< XRefreshable* >( this ) );
     238          22 :         m_aRefreshListeners.disposeAndClear(aEvt);
     239             : 
     240          22 :         if ( hasExternalListSource( ) )
     241           1 :             disconnectExternalListSource( );
     242          22 :     }
     243             : 
     244             :     //---------------------------------------------------------------------
     245           4 :     void OEntryListHelper::disconnectExternalListSource( )
     246             :     {
     247           4 :         if ( m_xListSource.is() )
     248           1 :             m_xListSource->removeListEntryListener( this );
     249             : 
     250           4 :         m_xListSource.clear();
     251             : 
     252           4 :         disconnectedExternalListSource();
     253           4 :     }
     254             : 
     255             :     //---------------------------------------------------------------------
     256           0 :     void OEntryListHelper::connectedExternalListSource( )
     257             :     {
     258             :         // nothing to do here
     259           0 :     }
     260             : 
     261             :     //---------------------------------------------------------------------
     262           0 :     void OEntryListHelper::disconnectedExternalListSource( )
     263             :     {
     264             :         // nothing to do here
     265           0 :     }
     266             : 
     267             :     //---------------------------------------------------------------------
     268           3 :     void OEntryListHelper::connectExternalListSource( const Reference< XListEntrySource >& _rxSource, ControlModelLock& _rInstanceLock )
     269             :     {
     270             :         OSL_ENSURE( !hasExternalListSource(), "OEntryListHelper::connectExternalListSource: only to be called if no external source is active!" );
     271             :         OSL_ENSURE( _rxSource.is(), "OEntryListHelper::connectExternalListSource: invalid list source!" );
     272             : 
     273             :         // remember it
     274           3 :         m_xListSource = _rxSource;
     275             : 
     276             :         // initially fill our item list
     277           3 :         if ( m_xListSource.is() )
     278             :         {
     279             :             // be notified when the list changes ...
     280           3 :             m_xListSource->addListEntryListener( this );
     281             : 
     282           3 :             m_aStringItems = m_xListSource->getAllListEntries( );
     283           3 :             stringItemListChanged( _rInstanceLock );
     284             : 
     285             :             // let derivees react on the new list source
     286           3 :             connectedExternalListSource();
     287             :         }
     288           3 :     }
     289             : 
     290             :     //---------------------------------------------------------------------
     291         130 :     sal_Bool OEntryListHelper::convertNewListSourceProperty( Any& _rConvertedValue,
     292             :         Any& _rOldValue, const Any& _rValue ) SAL_THROW( ( IllegalArgumentException ) )
     293             :     {
     294         130 :         if ( hasExternalListSource() )
     295           0 :             throw IllegalArgumentException( );
     296             :             // TODO: error message
     297             : 
     298         130 :         return ::comphelper::tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_aStringItems );
     299             :     }
     300             : 
     301             :     //---------------------------------------------------------------------
     302          39 :     void OEntryListHelper::setNewStringItemList( const ::com::sun::star::uno::Any& _rValue, ControlModelLock& _rInstanceLock )
     303             :     {
     304             :         OSL_PRECOND( !hasExternalListSource(), "OEntryListHelper::setNewStringItemList: this should never have survived convertNewListSourceProperty!" );
     305          39 :         OSL_VERIFY( _rValue >>= m_aStringItems );
     306          39 :         stringItemListChanged( _rInstanceLock );
     307          39 :     }
     308             : 
     309             : //.........................................................................
     310             : }   // namespace frm
     311             : //.........................................................................
     312             : 
     313             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10