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