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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
30 #include "listselectiondlg.hxx"
31 #ifndef EXTENSIONS_SOURCE_PROPCTRLR_LISTSELECTIONDLG_HRC
32 #include "listselectiondlg.hrc"
33 #endif
34 
35 #ifndef _EXTENSIONS_PROPCTRLR_MODULEPRC_HXX_
36 #include "modulepcr.hxx"
37 #endif
38 #ifndef _EXTENSIONS_FORMCTRLR_PROPRESID_HRC_
39 #include "formresid.hrc"
40 #endif
41 #include "formstrings.hxx"
42 #include <vcl/msgbox.hxx>
43 
44 /** === begin UNO includes === **/
45 /** === end UNO includes === **/
46 
47 //........................................................................
48 namespace pcr
49 {
50 //........................................................................
51 
52     using namespace ::com::sun::star::uno;
53     using namespace ::com::sun::star::beans;
54 
55 	//====================================================================
56 	//= ListSelectionDialog
57 	//====================================================================
58 	//--------------------------------------------------------------------
59     ListSelectionDialog::ListSelectionDialog( Window* _pParent, const Reference< XPropertySet >& _rxListBox,
60             const ::rtl::OUString& _rPropertyName, const String& _rPropertyUIName )
61         :ModalDialog( _pParent, PcrRes( RID_DLG_SELECTION ) )
62         ,m_aLabel   ( this, PcrRes( FT_ENTRIES ) )
63         ,m_aEntries ( this, PcrRes( LB_ENTRIES ) )
64         ,m_aOK      ( this, PcrRes( PB_OK      ) )
65         ,m_aCancel  ( this, PcrRes( PB_CANCEL  ) )
66         ,m_aHelp    ( this, PcrRes( PB_HELP    ) )
67         ,m_xListBox     ( _rxListBox     )
68         ,m_sPropertyName( _rPropertyName )
69     {
70         FreeResource();
71 
72         OSL_PRECOND( m_xListBox.is(), "ListSelectionDialog::ListSelectionDialog: invalid list box!" );
73 
74         SetText( _rPropertyUIName );
75         m_aLabel.SetText( _rPropertyUIName );
76 
77         initialize( );
78     }
79 
80     //------------------------------------------------------------------------
81     short ListSelectionDialog::Execute()
82     {
83         short nResult = ModalDialog::Execute();
84 
85         if ( RET_OK == nResult )
86             commitSelection();
87 
88         return nResult;
89     }
90 
91 	//--------------------------------------------------------------------
92     void ListSelectionDialog::initialize( )
93     {
94         if ( !m_xListBox.is() )
95             return;
96 
97         m_aEntries.SetStyle( GetStyle() | WB_SIMPLEMODE );
98 
99         try
100         {
101             // initialize the multi-selection flag
102             sal_Bool bMultiSelection = sal_False;
103             OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_MULTISELECTION ) >>= bMultiSelection );
104             m_aEntries.EnableMultiSelection( bMultiSelection );
105 
106             // fill the list box with all entries
107             Sequence< ::rtl::OUString > aListEntries;
108             OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_STRINGITEMLIST ) >>= aListEntries );
109             fillEntryList( aListEntries );
110 
111             // select entries according to the property
112             Sequence< sal_Int16 > aSelection;
113             OSL_VERIFY( m_xListBox->getPropertyValue( m_sPropertyName ) >>= aSelection );
114             selectEntries( aSelection );
115         }
116         catch( const Exception& )
117         {
118         	OSL_ENSURE( sal_False, "ListSelectionDialog::initialize: caught an exception!" );
119         }
120     }
121 
122     //--------------------------------------------------------------------
123     void ListSelectionDialog::commitSelection()
124     {
125         if ( !m_xListBox.is() )
126             return;
127 
128         Sequence< sal_Int16 > aSelection;
129         collectSelection( aSelection );
130 
131         try
132         {
133             m_xListBox->setPropertyValue( m_sPropertyName, makeAny( aSelection ) );
134         }
135         catch( const Exception& )
136         {
137         	OSL_ENSURE( sal_False, "ListSelectionDialog::commitSelection: caught an exception!" );
138         }
139     }
140 
141 	//--------------------------------------------------------------------
142     void ListSelectionDialog::fillEntryList( const Sequence< ::rtl::OUString >& _rListEntries )
143     {
144         m_aEntries.Clear();
145         const ::rtl::OUString* _pListEntries = _rListEntries.getConstArray();
146         const ::rtl::OUString* _pListEntriesEnd = _rListEntries.getConstArray() + _rListEntries.getLength();
147         for ( ; _pListEntries < _pListEntriesEnd; ++_pListEntries )
148             m_aEntries.InsertEntry( *_pListEntries );
149     }
150 
151 	//--------------------------------------------------------------------
152     void ListSelectionDialog::collectSelection( Sequence< sal_Int16 >& /* [out] */ _rSelection )
153     {
154         sal_uInt16 nSelectedCount = m_aEntries.GetSelectEntryCount( );
155         _rSelection.realloc( nSelectedCount );
156         sal_Int16* pSelection = _rSelection.getArray();
157         for ( sal_uInt16 selected = 0; selected < nSelectedCount; ++selected, ++pSelection )
158             *pSelection = static_cast< sal_Int16 >( m_aEntries.GetSelectEntryPos( selected ) );
159     }
160 
161 	//--------------------------------------------------------------------
162     void ListSelectionDialog::selectEntries( const Sequence< sal_Int16 >& /* [in ] */ _rSelection )
163     {
164         m_aEntries.SetNoSelection();
165         const sal_Int16* pSelection = _rSelection.getConstArray();
166         const sal_Int16* pSelectionEnd = _rSelection.getConstArray() + _rSelection.getLength();
167         for ( ; pSelection != pSelectionEnd; ++pSelection )
168             m_aEntries.SelectEntryPos( *pSelection );
169     }
170 
171 //........................................................................
172 }   // namespace pcr
173 //........................................................................
174