LCOV - code coverage report
Current view: top level - svx/source/form - tabwin.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 2 184 1.1 %
Date: 2015-06-13 12:38:46 Functions: 3 32 9.4 %
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             : 
      21             : #include "tabwin.hxx"
      22             : #include "svx/fmtools.hxx"
      23             : #include "fmservs.hxx"
      24             : #include "stringlistresource.hxx"
      25             : 
      26             : #include <svx/svxids.hrc>
      27             : #include <svx/dbaexchange.hxx>
      28             : #include <com/sun/star/sdb/CommandType.hpp>
      29             : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
      30             : #include <com/sun/star/sdb/XQueriesSupplier.hpp>
      31             : #include <com/sun/star/sdbc/XPreparedStatement.hpp>
      32             : #include <com/sun/star/awt/XControlContainer.hpp>
      33             : #include <com/sun/star/util/XLocalizedAliases.hpp>
      34             : #include <comphelper/processfactory.hxx>
      35             : 
      36             : #include "fmhelp.hrc"
      37             : #include <svx/fmshell.hxx>
      38             : #include "fmshimp.hxx"
      39             : #include <svx/fmpage.hxx>
      40             : 
      41             : #include "fmpgeimp.hxx"
      42             : 
      43             : #include "fmprop.hrc"
      44             : 
      45             : #include "svx/fmresids.hrc"
      46             : #include <svx/dialmgr.hxx>
      47             : #include <svx/svdpagv.hxx>
      48             : #include <sfx2/objitem.hxx>
      49             : #include <sfx2/dispatch.hxx>
      50             : #include <comphelper/property.hxx>
      51             : #include <sfx2/frame.hxx>
      52             : #include <svx/dataaccessdescriptor.hxx>
      53             : #include "svtools/treelistentry.hxx"
      54             : #include <vcl/settings.hxx>
      55             : 
      56             : const long STD_WIN_SIZE_X = 120;
      57             : const long STD_WIN_SIZE_Y = 150;
      58             : 
      59             : const long LISTBOX_BORDER = 2;
      60             : 
      61             : using namespace ::com::sun::star::sdbc;
      62             : using namespace ::com::sun::star::sdb;
      63             : using namespace ::com::sun::star::uno;
      64             : using namespace ::com::sun::star::datatransfer;
      65             : using namespace ::com::sun::star::beans;
      66             : using namespace ::com::sun::star::lang;
      67             : using namespace ::com::sun::star::form;
      68             : using namespace ::com::sun::star::container;
      69             : using namespace ::com::sun::star;
      70             : using namespace ::svxform;
      71             : using namespace ::svx;
      72             : using namespace ::dbtools;
      73             : 
      74             : namespace {
      75             : 
      76             : struct ColumnInfo
      77             : {
      78             :     OUString sColumnName;
      79             :     OUString sLabel;
      80           0 :     ColumnInfo(const OUString& i_sColumnName,const OUString& i_sLabel)
      81             :         : sColumnName(i_sColumnName)
      82           0 :         , sLabel(i_sLabel)
      83             :     {
      84           0 :     }
      85             : };
      86             : 
      87             : }
      88             : 
      89           0 : static void lcl_addToList( SvTreeListBox& _rListBox, const uno::Reference< container::XNameAccess>& i_xColumns )
      90             : {
      91           0 :     uno::Sequence< OUString > aEntries = i_xColumns->getElementNames();
      92           0 :     const OUString* pEntries = aEntries.getConstArray();
      93           0 :     sal_Int32 nEntries = aEntries.getLength();
      94           0 :     for ( sal_Int32 i = 0; i < nEntries; ++i, ++pEntries )
      95             :     {
      96           0 :         uno::Reference< beans::XPropertySet> xColumn(i_xColumns->getByName(*pEntries),UNO_QUERY_THROW);
      97           0 :         OUString sLabel;
      98           0 :         if ( xColumn->getPropertySetInfo()->hasPropertyByName(FM_PROP_LABEL) )
      99           0 :             xColumn->getPropertyValue(FM_PROP_LABEL) >>= sLabel;
     100           0 :         if ( !sLabel.isEmpty() )
     101           0 :             _rListBox.InsertEntry( sLabel, NULL, false, TREELIST_APPEND, new ColumnInfo(*pEntries,sLabel) );
     102             :         else
     103           0 :             _rListBox.InsertEntry( *pEntries, NULL, false, TREELIST_APPEND, new ColumnInfo(*pEntries,sLabel) );
     104           0 :     }
     105           0 : }
     106             : 
     107           0 : FmFieldWinListBox::FmFieldWinListBox( FmFieldWin* pParent )
     108             :     :SvTreeListBox( pParent, WB_HASBUTTONS|WB_BORDER )
     109           0 :     ,pTabWin( pParent )
     110             : {
     111           0 :     SetHelpId( HID_FIELD_SEL );
     112             : 
     113           0 :     SetHighlightRange( );
     114           0 : }
     115             : 
     116           0 : FmFieldWinListBox::~FmFieldWinListBox()
     117             : {
     118           0 :     disposeOnce();
     119           0 : }
     120             : 
     121           0 : void FmFieldWinListBox::dispose()
     122             : {
     123           0 :     pTabWin.clear();
     124           0 :     SvTreeListBox::dispose();
     125           0 : }
     126             : 
     127             : 
     128           0 : sal_Int8 FmFieldWinListBox::AcceptDrop( const AcceptDropEvent& /*rEvt*/ )
     129             : {
     130           0 :     return DND_ACTION_NONE;
     131             : }
     132             : 
     133             : 
     134           0 : sal_Int8 FmFieldWinListBox::ExecuteDrop( const ExecuteDropEvent& /*rEvt*/ )
     135             : {
     136           0 :     return DND_ACTION_NONE;
     137             : }
     138             : 
     139             : 
     140           0 : bool FmFieldWinListBox::DoubleClickHdl()
     141             : {
     142           0 :     if ( pTabWin->createSelectionControls() )
     143           0 :         return true;
     144             : 
     145           0 :     return SvTreeListBox::DoubleClickHdl();
     146             : }
     147             : 
     148             : 
     149           0 : void FmFieldWinListBox::StartDrag( sal_Int8 /*_nAction*/, const Point& /*_rPosPixel*/ )
     150             : {
     151           0 :     SvTreeListEntry* pSelected = FirstSelected();
     152           0 :     if (!pSelected)
     153             :         // no drag without a field
     154           0 :         return;
     155             : 
     156           0 :     svx::ODataAccessDescriptor aDescriptor;
     157           0 :     aDescriptor[ daDataSource ] <<= pTabWin->GetDatabaseName();
     158           0 :     aDescriptor[ daConnection ] <<= pTabWin->GetConnection().getTyped();
     159           0 :     aDescriptor[ daCommand ]    <<= pTabWin->GetObjectName();
     160           0 :     aDescriptor[ daCommandType ]<<= pTabWin->GetObjectType();
     161           0 :     ColumnInfo* pInfo = static_cast<ColumnInfo*>(pSelected->GetUserData());
     162           0 :     aDescriptor[ daColumnName ] <<= pInfo->sColumnName;
     163             : 
     164             :     TransferableHelper* pTransferColumn = new OColumnTransferable(
     165           0 :         aDescriptor, ColumnTransferFormatFlags::FIELD_DESCRIPTOR | ColumnTransferFormatFlags::CONTROL_EXCHANGE | ColumnTransferFormatFlags::COLUMN_DESCRIPTOR
     166           0 :     );
     167           0 :     Reference< XTransferable> xEnsureDelete = pTransferColumn;
     168           0 :     EndSelection();
     169           0 :     pTransferColumn->StartDrag( this, DND_ACTION_COPY );
     170             : }
     171             : 
     172           0 : FmFieldWinData::FmFieldWinData()
     173             : {
     174           0 : }
     175             : 
     176             : 
     177           0 : FmFieldWinData::~FmFieldWinData()
     178             : {
     179           0 : }
     180             : 
     181           0 : FmFieldWin::FmFieldWin(SfxBindings* _pBindings, SfxChildWindow* _pMgr, vcl::Window* _pParent)
     182             :             :SfxFloatingWindow(_pBindings, _pMgr, _pParent, WinBits(WB_STDMODELESS|WB_SIZEABLE))
     183             :             ,SfxControllerItem(SID_FM_FIELDS_CONTROL, *_pBindings)
     184             :             ,::comphelper::OPropertyChangeListener(m_aMutex)
     185           0 :             ,pData(new FmFieldWinData)
     186             :             ,m_nObjectType(0)
     187           0 :             ,m_pChangeListener(NULL)
     188             : {
     189           0 :     SetHelpId( HID_FIELD_SEL_WIN );
     190             : 
     191           0 :     SetBackground( Wallpaper( Application::GetSettings().GetStyleSettings().GetFaceColor()) );
     192           0 :     pListBox = VclPtr<FmFieldWinListBox>::Create( this );
     193           0 :     pListBox->Show();
     194           0 :     UpdateContent(NULL);
     195           0 :     SetSizePixel(Size(STD_WIN_SIZE_X,STD_WIN_SIZE_Y));
     196           0 : }
     197             : 
     198             : 
     199           0 : FmFieldWin::~FmFieldWin()
     200             : {
     201           0 :     disposeOnce();
     202           0 : }
     203             : 
     204           0 : void FmFieldWin::dispose()
     205             : {
     206           0 :     if (m_pChangeListener)
     207             :     {
     208           0 :         m_pChangeListener->dispose();
     209           0 :         m_pChangeListener->release();
     210             :         //  delete m_pChangeListener;
     211             :     }
     212           0 :     pListBox.disposeAndClear();
     213           0 :     delete pData;
     214           0 :     ::SfxControllerItem::dispose();
     215           0 :     SfxFloatingWindow::dispose();
     216           0 : }
     217             : 
     218             : 
     219           0 : void FmFieldWin::GetFocus()
     220             : {
     221           0 :     if ( pListBox )
     222           0 :         pListBox->GrabFocus();
     223             :     else
     224           0 :         SfxFloatingWindow::GetFocus();
     225           0 : }
     226             : 
     227             : 
     228           0 : bool FmFieldWin::createSelectionControls( )
     229             : {
     230           0 :     SvTreeListEntry* pSelected = pListBox->FirstSelected();
     231           0 :     if ( pSelected )
     232             :     {
     233             :         // build a descriptor for the currently selected field
     234           0 :         ODataAccessDescriptor aDescr;
     235           0 :         aDescr.setDataSource(GetDatabaseName());
     236             : 
     237           0 :         aDescr[ daConnection ]  <<= GetConnection().getTyped();
     238             : 
     239           0 :         aDescr[ daCommand ]     <<= GetObjectName();
     240           0 :         aDescr[ daCommandType ] <<= GetObjectType();
     241           0 :         ColumnInfo* pInfo = static_cast<ColumnInfo*>(pSelected->GetUserData());
     242           0 :         aDescr[ daColumnName ]  <<= pInfo->sColumnName;//OUString( pListBox->GetEntryText( pSelected) );
     243             : 
     244             :         // transfer this to the SFX world
     245           0 :         SfxUnoAnyItem aDescriptorItem( SID_FM_DATACCESS_DESCRIPTOR, makeAny( aDescr.createPropertyValueSequence() ) );
     246             :         const SfxPoolItem* pArgs[] =
     247             :         {
     248             :             &aDescriptorItem, NULL
     249           0 :         };
     250             : 
     251             :         // execute the create slot
     252           0 :         GetBindings().Execute( SID_FM_CREATE_FIELDCONTROL, pArgs );
     253             :     }
     254             : 
     255           0 :     return NULL != pSelected;
     256             : }
     257             : 
     258             : 
     259           0 : bool FmFieldWin::PreNotify( NotifyEvent& _rNEvt )
     260             : {
     261           0 :     if ( MouseNotifyEvent::KEYINPUT == _rNEvt.GetType() )
     262             :     {
     263           0 :         const vcl::KeyCode& rKeyCode = _rNEvt.GetKeyEvent()->GetKeyCode();
     264           0 :         if ( ( 0 == rKeyCode.GetModifier() ) && ( KEY_RETURN == rKeyCode.GetCode() ) )
     265             :         {
     266           0 :             if ( createSelectionControls() )
     267           0 :                 return true;
     268             :         }
     269             :     }
     270             : 
     271           0 :     return SfxFloatingWindow::PreNotify( _rNEvt );
     272             : }
     273             : 
     274             : 
     275           0 : bool FmFieldWin::Close()
     276             : {
     277           0 :     return SfxFloatingWindow::Close();
     278             : }
     279             : 
     280             : 
     281           0 : void FmFieldWin::_propertyChanged(const ::com::sun::star::beans::PropertyChangeEvent& evt) throw( ::com::sun::star::uno::RuntimeException )
     282             : {
     283           0 :     ::com::sun::star::uno::Reference< ::com::sun::star::form::XForm >  xForm(evt.Source, ::com::sun::star::uno::UNO_QUERY);
     284           0 :     UpdateContent(xForm);
     285           0 : }
     286             : 
     287             : 
     288           0 : void FmFieldWin::StateChanged(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState)
     289             : {
     290           0 :     if (!pState  || SID_FM_FIELDS_CONTROL != nSID)
     291           0 :         return;
     292             : 
     293           0 :     if (eState >= SfxItemState::DEFAULT)
     294             :     {
     295           0 :         FmFormShell* pShell = PTR_CAST(FmFormShell, static_cast<const SfxObjectItem*>(pState)->GetShell());
     296           0 :         UpdateContent(pShell);
     297             :     }
     298             :     else
     299           0 :         UpdateContent(NULL);
     300             : }
     301             : 
     302             : 
     303           0 : void FmFieldWin::UpdateContent(FmFormShell* pShell)
     304             : {
     305           0 :     pListBox->Clear();
     306           0 :     OUString aTitle(SVX_RESSTR(RID_STR_FIELDSELECTION));
     307           0 :     SetText( aTitle );
     308             : 
     309           0 :     if (!pShell || !pShell->GetImpl())
     310           0 :         return;
     311             : 
     312           0 :     Reference< XForm >  xForm = pShell->GetImpl()->getCurrentForm();
     313           0 :     if ( xForm.is() )
     314           0 :         UpdateContent( xForm );
     315             : }
     316             : 
     317             : 
     318           0 : void FmFieldWin::UpdateContent(const ::com::sun::star::uno::Reference< ::com::sun::star::form::XForm > & xForm)
     319             : {
     320             :     try
     321             :     {
     322             :         // delete ListBox
     323           0 :         pListBox->Clear();
     324           0 :         OUString aTitle(SVX_RES(RID_STR_FIELDSELECTION));
     325           0 :         SetText(aTitle);
     326             : 
     327           0 :         if (!xForm.is())
     328           0 :             return;
     329             : 
     330           0 :         Reference< XPreparedStatement >  xStatement;
     331           0 :         Reference< XPropertySet >  xSet(xForm, UNO_QUERY);
     332             : 
     333           0 :         m_aObjectName   = ::comphelper::getString(xSet->getPropertyValue(FM_PROP_COMMAND));
     334           0 :         m_aDatabaseName = ::comphelper::getString(xSet->getPropertyValue(FM_PROP_DATASOURCE));
     335           0 :         m_nObjectType   = ::comphelper::getINT32(xSet->getPropertyValue(FM_PROP_COMMANDTYPE));
     336             : 
     337             :         // get the connection of the form
     338             :         m_aConnection.reset(
     339             :             connectRowset( Reference< XRowSet >( xForm, UNO_QUERY ), ::comphelper::getProcessComponentContext(), true ),
     340             :             SharedConnection::NoTakeOwnership
     341           0 :         );
     342             :         // TODO: When incompatible changes (such as extending the "virtualdbtools" interface by ensureRowSetConnection)
     343             :         // are allowed, again, we should change this: dbtools should consistently use SharedConnection all over
     344             :         // the place, and connectRowset should be replaced with ensureRowSetConnection
     345             : 
     346             :         // get the fields of the object
     347             : 
     348           0 :         if ( m_aConnection.is() && !m_aObjectName.isEmpty() )
     349             :         {
     350           0 :             Reference< XComponent > xKeepFieldsAlive;
     351           0 :             Reference< XNameAccess > xColumns = getFieldsByCommandDescriptor( m_aConnection, m_nObjectType, m_aObjectName,xKeepFieldsAlive );
     352           0 :             if ( xColumns.is() )
     353           0 :                 lcl_addToList(*pListBox,xColumns);
     354             :         }
     355             : 
     356             :         // set prefix
     357           0 :         OUString  aPrefix;
     358           0 :         StringListResource aPrefixes( SVX_RES( RID_RSC_TABWIN_PREFIX ) );
     359             : 
     360           0 :         switch (m_nObjectType)
     361             :         {
     362             :             case CommandType::TABLE:
     363           0 :                 aPrefix = aPrefixes[0];
     364           0 :                 break;
     365             :             case CommandType::QUERY:
     366           0 :                 aPrefix = aPrefixes[1];
     367           0 :                 break;
     368             :             default:
     369           0 :                 aPrefix = aPrefixes[2];
     370           0 :                 break;
     371             :         }
     372             : 
     373             :         // listen for changes at ControlSource in PropertySet
     374           0 :         if (m_pChangeListener)
     375             :         {
     376           0 :             m_pChangeListener->dispose();
     377           0 :             m_pChangeListener->release();
     378             :         }
     379           0 :         m_pChangeListener = new ::comphelper::OPropertyChangeMultiplexer(this, xSet);
     380           0 :         m_pChangeListener->acquire();
     381           0 :         m_pChangeListener->addProperty(FM_PROP_DATASOURCE);
     382           0 :         m_pChangeListener->addProperty(FM_PROP_COMMAND);
     383           0 :         m_pChangeListener->addProperty(FM_PROP_COMMANDTYPE);
     384             : 
     385             :         // Titel setzen
     386           0 :         aTitle = aTitle + " " + aPrefix + " " + OUString(m_aObjectName.getStr());
     387           0 :         SetText( aTitle );
     388             :     }
     389           0 :     catch( const Exception& )
     390             :     {
     391             :         OSL_FAIL( "FmTabWin::UpdateContent: caught an exception!" );
     392             :     }
     393             : }
     394             : 
     395             : 
     396           0 : void FmFieldWin::Resize()
     397             : {
     398           0 :     SfxFloatingWindow::Resize();
     399             : 
     400           0 :     Size aOutputSize( GetOutputSizePixel() );
     401             : 
     402             : 
     403             : 
     404             :     // adapt size of ::com::sun::star::form::ListBox
     405           0 :     Point aLBPos( LISTBOX_BORDER, LISTBOX_BORDER );
     406           0 :     Size aLBSize( aOutputSize );
     407           0 :     aLBSize.Width() -= (2*LISTBOX_BORDER);
     408           0 :     aLBSize.Height() -= (2*LISTBOX_BORDER);
     409             : 
     410           0 :     pListBox->SetPosSizePixel( aLBPos, aLBSize );
     411           0 : }
     412             : 
     413             : 
     414           0 : void FmFieldWin::FillInfo( SfxChildWinInfo& rInfo ) const
     415             : {
     416           0 :     rInfo.bVisible = false;
     417           0 : }
     418             : 
     419             : 
     420         114 : SFX_IMPL_FLOATINGWINDOW(FmFieldWinMgr, SID_FM_ADD_FIELD)
     421             : 
     422             : 
     423           0 : FmFieldWinMgr::FmFieldWinMgr(vcl::Window* _pParent, sal_uInt16 _nId,
     424             :                SfxBindings* _pBindings, SfxChildWinInfo* _pInfo)
     425           0 :               :SfxChildWindow(_pParent, _nId)
     426             : {
     427           0 :     pWindow = VclPtr<FmFieldWin>::Create(_pBindings, this, _pParent);
     428           0 :     SetHideNotDelete(true);
     429           0 :     eChildAlignment = SfxChildAlignment::NOALIGNMENT;
     430           0 :     static_cast<SfxFloatingWindow*>(pWindow.get())->Initialize( _pInfo );
     431         390 : }
     432             : 
     433             : 
     434             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11