Update ooo320-m1
[ooovba.git] / chart2 / source / controller / main / ChartDropTargetHelper.cxx
blob2c18c31ff82c9364ecb54d6d61692665ceaf0506
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ChartDropTargetHelper.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
34 #include "ChartDropTargetHelper.hxx"
35 #include "DiagramHelper.hxx"
36 #include "DataSourceHelper.hxx"
38 #include <com/sun/star/chart2/XChartDocument.hpp>
39 #include <com/sun/star/chart2/data/XDataProvider.hpp>
40 #include <com/sun/star/container/XChild.hpp>
42 #include <sot/formats.hxx>
43 #include <vector>
45 using namespace ::com::sun::star;
47 using ::com::sun::star::uno::Reference;
48 using ::com::sun::star::uno::Sequence;
49 using ::rtl::OUString;
51 namespace
54 ::std::vector< OUString > lcl_getStringsFromByteSequence(
55 const Sequence< sal_Int8 > & aByteSequence )
57 ::std::vector< OUString > aResult;
58 const sal_Int32 nLength = aByteSequence.getLength();
59 const sal_Char * pBytes( reinterpret_cast< const sal_Char* >( aByteSequence.getConstArray()));
60 sal_Int32 nStartPos = 0;
61 for( sal_Int32 nPos=0; nPos<nLength; ++nPos )
63 if( pBytes[nPos] == '\0' )
65 aResult.push_back( OUString( pBytes + nStartPos, (nPos - nStartPos), RTL_TEXTENCODING_ASCII_US ));
66 nStartPos = nPos + 1;
69 return aResult;
72 } // anonymous namespace
74 namespace chart
77 ChartDropTargetHelper::ChartDropTargetHelper(
78 const Reference< datatransfer::dnd::XDropTarget >& rxDropTarget,
79 const Reference< chart2::XChartDocument > & xChartDocument ) :
80 DropTargetHelper( rxDropTarget ),
81 m_xChartDocument( xChartDocument )
84 ChartDropTargetHelper::~ChartDropTargetHelper()
87 bool ChartDropTargetHelper::satisfiesPrerequisites() const
89 return ( m_xChartDocument.is() &&
90 ! m_xChartDocument->hasInternalDataProvider());
93 sal_Int8 ChartDropTargetHelper::AcceptDrop( const AcceptDropEvent& rEvt )
95 sal_Int8 nResult = DND_ACTION_NONE;
97 if( ( rEvt.mnAction == DND_ACTION_COPY ||
98 rEvt.mnAction == DND_ACTION_MOVE ) &&
99 satisfiesPrerequisites() &&
100 IsDropFormatSupported( SOT_FORMATSTR_ID_LINK ) )
102 // @todo: check if the data is suitable. Is this possible without XTransferable?
103 nResult = rEvt.mnAction;
106 return nResult;
109 sal_Int8 ChartDropTargetHelper::ExecuteDrop( const ExecuteDropEvent& rEvt )
111 sal_Int8 nResult = DND_ACTION_NONE;
113 if( ( rEvt.mnAction == DND_ACTION_COPY ||
114 rEvt.mnAction == DND_ACTION_MOVE ) &&
115 rEvt.maDropEvent.Transferable.is() &&
116 satisfiesPrerequisites())
118 TransferableDataHelper aDataHelper( rEvt.maDropEvent.Transferable );
119 if( aDataHelper.HasFormat( SOT_FORMATSTR_ID_LINK ))
121 Sequence< sal_Int8 > aBytes;
122 if( aDataHelper.GetSequence( SOT_FORMATSTR_ID_LINK, aBytes ))
124 ::std::vector< OUString > aStrings( lcl_getStringsFromByteSequence( aBytes ));
125 if( aStrings.size() >= 3 &&
126 aStrings[0].equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "soffice" )))
128 OUString aDocName( aStrings[1] );
129 OUString aRangeString( aStrings[2] );
130 Reference< container::XChild > xChild( m_xChartDocument, uno::UNO_QUERY );
131 if( xChild.is())
133 Reference< frame::XModel > xParentModel( xChild->getParent(), uno::UNO_QUERY );
134 if( xParentModel.is() &&
135 m_xChartDocument.is())
137 bool bDataComesFromParent = true;
138 // @todo: get the title somehow and compare it to
139 // aDocName if successful (the document is the
140 // parent)
141 if( bDataComesFromParent )
143 Reference< chart2::data::XDataProvider > xDataProvider( m_xChartDocument->getDataProvider());
144 if( xDataProvider.is() &&
145 DataSourceHelper::allArgumentsForRectRangeDetected( m_xChartDocument ))
147 DiagramHelper::tTemplateWithServiceName aTempWithServ(
148 DiagramHelper::getTemplateForDiagram(
149 m_xChartDocument->getFirstDiagram(),
150 Reference< lang::XMultiServiceFactory >(
151 m_xChartDocument->getChartTypeManager(), uno::UNO_QUERY )));
152 if( aTempWithServ.first.is())
154 Reference< chart2::data::XDataSource > xDataSource(
155 DataSourceHelper::pressUsedDataIntoRectangularFormat( m_xChartDocument ));
156 Sequence< beans::PropertyValue > aArguments(
157 xDataProvider->detectArguments( xDataSource ));
159 OUString aOldRange;
160 beans::PropertyValue * pCellRange = 0;
161 for( sal_Int32 i=0; i<aArguments.getLength(); ++i )
163 if( aArguments[i].Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("CellRangeRepresentation")))
165 pCellRange = (aArguments.getArray() + i);
166 aArguments[i].Value >>= aOldRange;
167 break;
170 if( pCellRange )
172 // copy means add ranges, move means replace
173 if( rEvt.mnAction == DND_ACTION_COPY )
175 // @todo: using implcit knowledge that ranges can be
176 // merged with ";". This should be done more general
177 pCellRange->Value <<= (aOldRange + OUString( sal_Unicode(';')) + aRangeString );
179 // move means replace range
180 else
182 pCellRange->Value <<= aRangeString;
185 xDataSource.set( xDataProvider->createDataSource( aArguments ));
186 aTempWithServ.first->changeDiagramData(
187 m_xChartDocument->getFirstDiagram(),
188 xDataSource,
189 aArguments );
191 // always return copy state to avoid deletion of the
192 // dragged range
193 nResult = DND_ACTION_COPY;
204 return nResult;
207 } // namespace chart