1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_extensions.hxx"
26 #include "listselectiondlg.hxx"
27 #ifndef EXTENSIONS_SOURCE_PROPCTRLR_LISTSELECTIONDLG_HRC
28 #include "listselectiondlg.hrc"
29 #endif
30 
31 #ifndef _EXTENSIONS_PROPCTRLR_MODULEPRC_HXX_
32 #include "modulepcr.hxx"
33 #endif
34 #ifndef _EXTENSIONS_FORMCTRLR_PROPRESID_HRC_
35 #include "formresid.hrc"
36 #endif
37 #include "formstrings.hxx"
38 #include <vcl/msgbox.hxx>
39 
40 /** === begin UNO includes === **/
41 /** === end UNO includes === **/
42 
43 //........................................................................
44 namespace pcr
45 {
46 //........................................................................
47 
48     using namespace ::com::sun::star::uno;
49     using namespace ::com::sun::star::beans;
50 
51 	//====================================================================
52 	//= ListSelectionDialog
53 	//====================================================================
54 	//--------------------------------------------------------------------
ListSelectionDialog(Window * _pParent,const Reference<XPropertySet> & _rxListBox,const::rtl::OUString & _rPropertyName,const String & _rPropertyUIName)55     ListSelectionDialog::ListSelectionDialog( Window* _pParent, const Reference< XPropertySet >& _rxListBox,
56             const ::rtl::OUString& _rPropertyName, const String& _rPropertyUIName )
57         :ModalDialog( _pParent, PcrRes( RID_DLG_SELECTION ) )
58         ,m_aLabel   ( this, PcrRes( FT_ENTRIES ) )
59         ,m_aEntries ( this, PcrRes( LB_ENTRIES ) )
60         ,m_aOK      ( this, PcrRes( PB_OK      ) )
61         ,m_aCancel  ( this, PcrRes( PB_CANCEL  ) )
62         ,m_aHelp    ( this, PcrRes( PB_HELP    ) )
63         ,m_xListBox     ( _rxListBox     )
64         ,m_sPropertyName( _rPropertyName )
65     {
66         FreeResource();
67 
68         OSL_PRECOND( m_xListBox.is(), "ListSelectionDialog::ListSelectionDialog: invalid list box!" );
69 
70         SetText( _rPropertyUIName );
71         m_aLabel.SetText( _rPropertyUIName );
72 
73         initialize( );
74     }
75 
76     //------------------------------------------------------------------------
Execute()77     short ListSelectionDialog::Execute()
78     {
79         short nResult = ModalDialog::Execute();
80 
81         if ( RET_OK == nResult )
82             commitSelection();
83 
84         return nResult;
85     }
86 
87 	//--------------------------------------------------------------------
initialize()88     void ListSelectionDialog::initialize( )
89     {
90         if ( !m_xListBox.is() )
91             return;
92 
93         m_aEntries.SetStyle( GetStyle() | WB_SIMPLEMODE );
94 
95         try
96         {
97             // initialize the multi-selection flag
98             sal_Bool bMultiSelection = sal_False;
99             OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_MULTISELECTION ) >>= bMultiSelection );
100             m_aEntries.EnableMultiSelection( bMultiSelection );
101 
102             // fill the list box with all entries
103             Sequence< ::rtl::OUString > aListEntries;
104             OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_STRINGITEMLIST ) >>= aListEntries );
105             fillEntryList( aListEntries );
106 
107             // select entries according to the property
108             Sequence< sal_Int16 > aSelection;
109             OSL_VERIFY( m_xListBox->getPropertyValue( m_sPropertyName ) >>= aSelection );
110             selectEntries( aSelection );
111         }
112         catch( const Exception& )
113         {
114         	OSL_ENSURE( sal_False, "ListSelectionDialog::initialize: caught an exception!" );
115         }
116     }
117 
118     //--------------------------------------------------------------------
commitSelection()119     void ListSelectionDialog::commitSelection()
120     {
121         if ( !m_xListBox.is() )
122             return;
123 
124         Sequence< sal_Int16 > aSelection;
125         collectSelection( aSelection );
126 
127         try
128         {
129             m_xListBox->setPropertyValue( m_sPropertyName, makeAny( aSelection ) );
130         }
131         catch( const Exception& )
132         {
133         	OSL_ENSURE( sal_False, "ListSelectionDialog::commitSelection: caught an exception!" );
134         }
135     }
136 
137 	//--------------------------------------------------------------------
fillEntryList(const Sequence<::rtl::OUString> & _rListEntries)138     void ListSelectionDialog::fillEntryList( const Sequence< ::rtl::OUString >& _rListEntries )
139     {
140         m_aEntries.Clear();
141         const ::rtl::OUString* _pListEntries = _rListEntries.getConstArray();
142         const ::rtl::OUString* _pListEntriesEnd = _rListEntries.getConstArray() + _rListEntries.getLength();
143         for ( ; _pListEntries < _pListEntriesEnd; ++_pListEntries )
144             m_aEntries.InsertEntry( *_pListEntries );
145     }
146 
147 	//--------------------------------------------------------------------
collectSelection(Sequence<sal_Int16> & _rSelection)148     void ListSelectionDialog::collectSelection( Sequence< sal_Int16 >& /* [out] */ _rSelection )
149     {
150         sal_uInt16 nSelectedCount = m_aEntries.GetSelectEntryCount( );
151         _rSelection.realloc( nSelectedCount );
152         sal_Int16* pSelection = _rSelection.getArray();
153         for ( sal_uInt16 selected = 0; selected < nSelectedCount; ++selected, ++pSelection )
154             *pSelection = static_cast< sal_Int16 >( m_aEntries.GetSelectEntryPos( selected ) );
155     }
156 
157 	//--------------------------------------------------------------------
selectEntries(const Sequence<sal_Int16> & _rSelection)158     void ListSelectionDialog::selectEntries( const Sequence< sal_Int16 >& /* [in ] */ _rSelection )
159     {
160         m_aEntries.SetNoSelection();
161         const sal_Int16* pSelection = _rSelection.getConstArray();
162         const sal_Int16* pSelectionEnd = _rSelection.getConstArray() + _rSelection.getLength();
163         for ( ; pSelection != pSelectionEnd; ++pSelection )
164             m_aEntries.SelectEntryPos( *pSelection );
165     }
166 
167 //........................................................................
168 }   // namespace pcr
169 //........................................................................
170