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_chart2.hxx"
30 
31 #include "RangeSelectionHelper.hxx"
32 #include "RangeSelectionListener.hxx"
33 #include "macros.hxx"
34 #include "ControllerLockGuard.hxx"
35 #include <com/sun/star/frame/XModel.hpp>
36 #include <com/sun/star/awt/XTopWindow.hpp>
37 #include <com/sun/star/text/XText.hpp>
38 #include <com/sun/star/embed/XEmbeddedObject.hpp>
39 #include <com/sun/star/embed/EmbedStates.hpp>
40 #include <com/sun/star/embed/XComponentSupplier.hpp>
41 #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
42 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
43 #include <com/sun/star/sheet/XCellRangesAccess.hpp>
44 #include <com/sun/star/chart2/data/XRangeXMLConversion.hpp>
45 #include <rtl/ustrbuf.hxx>
46 
47 using namespace ::com::sun::star;
48 
49 using ::com::sun::star::uno::Reference;
50 using ::com::sun::star::uno::Sequence;
51 using ::rtl::OUString;
52 
53 // ----------------------------------------
54 
55 namespace chart
56 {
57 
58 RangeSelectionHelper::RangeSelectionHelper(
59     const Reference< chart2::XChartDocument > & xChartDocument ) :
60         m_xChartDocument( xChartDocument )
61 {}
62 
63 RangeSelectionHelper::~RangeSelectionHelper()
64 {}
65 
66 bool RangeSelectionHelper::hasRangeSelection()
67 {
68     return getRangeSelection().is();
69 }
70 
71 Reference< sheet::XRangeSelection > RangeSelectionHelper::getRangeSelection()
72 {
73     if( !m_xRangeSelection.is() &&
74         m_xChartDocument.is() )
75     {
76         try
77         {
78             Reference< chart2::data::XDataProvider > xDataProvider( m_xChartDocument->getDataProvider());
79             if( xDataProvider.is())
80                 m_xRangeSelection.set( xDataProvider->getRangeSelection());
81         }
82         catch( uno::Exception & ex )
83         {
84             ASSERT_EXCEPTION( ex );
85 
86             m_xRangeSelection.clear();
87         }
88     }
89 
90     return m_xRangeSelection;
91 }
92 
93 void RangeSelectionHelper::raiseRangeSelectionDocument()
94 {
95     Reference< sheet::XRangeSelection > xRangeSel( getRangeSelection());
96     if( xRangeSel.is())
97     {
98         try
99         {
100             // bring document to front
101             Reference< frame::XController > xCtrl( xRangeSel, uno::UNO_QUERY );
102             if( xCtrl.is())
103             {
104                 Reference< frame::XFrame > xFrame( xCtrl->getFrame());
105                 if( xFrame.is())
106                 {
107                     Reference< awt::XTopWindow > xWin( xFrame->getContainerWindow(),
108                                                        uno::UNO_QUERY_THROW );
109                     xWin->toFront();
110                 }
111             }
112         }
113         catch( uno::Exception & ex )
114         {
115             ASSERT_EXCEPTION( ex );
116         }
117     }
118 }
119 
120 bool RangeSelectionHelper::chooseRange(
121     const OUString & aCurrentRange,
122     const OUString & aUIString,
123     RangeSelectionListenerParent & rListenerParent )
124 {
125     ControllerLockGuard aGuard( Reference< frame::XModel >(m_xChartDocument, uno::UNO_QUERY ) );
126 
127     bool bResult = true;
128     raiseRangeSelectionDocument();
129 
130     try
131     {
132         Reference< sheet::XRangeSelection > xRangeSel( getRangeSelection());
133         if( xRangeSel.is())
134         {
135             Sequence< beans::PropertyValue > aArgs( 4 );
136             aArgs[0] = beans::PropertyValue(
137                 C2U("InitialValue"), -1, uno::makeAny( aCurrentRange ),
138                 beans::PropertyState_DIRECT_VALUE );
139             aArgs[1] = beans::PropertyValue(
140                 C2U("Title"), -1,
141                 uno::makeAny( aUIString ),
142                 beans::PropertyState_DIRECT_VALUE );
143             aArgs[2] = beans::PropertyValue(
144                 C2U("CloseOnMouseRelease"), -1, uno::makeAny( true ),
145                 beans::PropertyState_DIRECT_VALUE );
146             aArgs[3] = beans::PropertyValue(
147                 C2U("MultiSelectionMode"), -1, uno::makeAny( true ),
148                 beans::PropertyState_DIRECT_VALUE );
149 
150             if( m_xRangeSelectionListener.is() )
151                 stopRangeListening();
152             m_xRangeSelectionListener.set( Reference< sheet::XRangeSelectionListener >(
153                 new RangeSelectionListener( rListenerParent, aCurrentRange, Reference< frame::XModel >(m_xChartDocument, uno::UNO_QUERY ) )));
154 
155             xRangeSel->addRangeSelectionListener( m_xRangeSelectionListener );
156             xRangeSel->startRangeSelection( aArgs );
157         }
158     }
159     catch( uno::Exception & ex )
160     {
161         bResult = false;
162         ASSERT_EXCEPTION( ex );
163     }
164 
165     return bResult;
166 }
167 
168 void RangeSelectionHelper::stopRangeListening( bool bRemoveListener /* = true */ )
169 {
170     if( bRemoveListener &&
171         m_xRangeSelectionListener.is() &&
172         m_xRangeSelection.is() )
173     {
174         m_xRangeSelection->removeRangeSelectionListener( m_xRangeSelectionListener );
175     }
176 
177     m_xRangeSelectionListener = 0;
178 }
179 
180 bool RangeSelectionHelper::verifyCellRange( const OUString & rRangeStr )
181 {
182     Reference< chart2::data::XDataProvider > xDataProvider( m_xChartDocument->getDataProvider());
183     if( ! xDataProvider.is())
184         return false;
185 
186     return xDataProvider->createDataSequenceByRangeRepresentationPossible( rRangeStr );
187 }
188 
189 bool RangeSelectionHelper::verifyArguments( const Sequence< beans::PropertyValue > & rArguments )
190 {
191     Reference< chart2::data::XDataProvider > xDataProvider( m_xChartDocument->getDataProvider());
192     if( ! xDataProvider.is())
193         return false;
194 
195     return xDataProvider->createDataSourcePossible( rArguments );
196 }
197 
198 } //  namespace chart
199