1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ElementSelector.cxx,v $
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 "ElementSelector.hxx"
36 #include "ObjectNameProvider.hxx"
37 #include "ObjectHierarchy.hxx"
38 #include "servicenames.hxx"
39 #include <chartview/ExplicitValueProvider.hxx>
41 #include <toolkit/helper/vclunohelper.hxx>
42 #include <vos/mutex.hxx>
43 #include <vcl/svapp.hxx>
45 #include <com/sun/star/chart2/XChartDocument.hpp>
46 #include <com/sun/star/frame/XControlNotificationListener.hpp>
47 #include <com/sun/star/util/XURLTransformer.hpp>
48 #include <com/sun/star/view/XSelectionSupplier.hpp>
53 using namespace com::sun::star
;
54 using namespace com::sun::star::uno
;
55 using ::com::sun::star::uno::Any
;
56 using ::com::sun::star::uno::Reference
;
57 using ::com::sun::star::uno::Sequence
;
61 static const ::rtl::OUString
lcl_aServiceName(
62 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart.ElementSelectorToolbarController" ));
65 //------------------------------------------------------------------------------
67 SelectorListBox::SelectorListBox( Window
* pParent
, WinBits nStyle
)
68 : ListBox( pParent
, nStyle
)
69 , m_bReleaseFocus( true )
73 SelectorListBox::~SelectorListBox()
77 void lcl_addObjectsToList( const ObjectHierarchy
& rHierarchy
, const ObjectHierarchy::tCID
& rParent
, std::vector
< ListBoxEntryData
>& rEntries
78 , const sal_Int32 nHierarchyDepth
, const Reference
< chart2::XChartDocument
>& xChartDoc
)
80 ObjectHierarchy::tChildContainer
aChildren( rHierarchy
.getChildren(rParent
) );
81 ObjectHierarchy::tChildContainer::const_iterator
aIt( aChildren
.begin());
82 while( aIt
!= aChildren
.end() )
84 ::rtl::OUString aCID
= *aIt
;
85 ListBoxEntryData aEntry
;
87 aEntry
.UIName
+= ObjectNameProvider::getNameForCID( aCID
, xChartDoc
);
88 aEntry
.nHierarchyDepth
= nHierarchyDepth
;
89 rEntries
.push_back(aEntry
);
90 lcl_addObjectsToList( rHierarchy
, aCID
, rEntries
, nHierarchyDepth
+1, xChartDoc
);
95 void SelectorListBox::SetChartController( const Reference
< frame::XController
>& xChartController
)
97 m_xChartController
= xChartController
;
100 void SelectorListBox::UpdateChartElementsListAndSelection()
105 Reference
< frame::XController
> xChartController( m_xChartController
);
106 if( xChartController
.is() )
108 Reference
< view::XSelectionSupplier
> xSelectionSupplier( xChartController
, uno::UNO_QUERY
);
109 rtl::OUString aSelectedCID
;
110 if( xSelectionSupplier
.is() )
111 xSelectionSupplier
->getSelection() >>= aSelectedCID
;
113 Reference
< chart2::XChartDocument
> xChartDoc( xChartController
->getModel(), uno::UNO_QUERY
);
114 ObjectType
eType( ObjectIdentifier::getObjectType( aSelectedCID
));
115 bool bAddSelectionToList
= false;
116 if( eType
== OBJECTTYPE_DATA_POINT
|| eType
== OBJECTTYPE_DATA_LABEL
)
117 bAddSelectionToList
= true;
119 Reference
< uno::XInterface
> xChartView
;
120 Reference
< lang::XMultiServiceFactory
> xFact( xChartController
->getModel(), uno::UNO_QUERY
);
122 xChartView
= xFact
->createInstance( CHART_VIEW_SERVICE_NAME
);
123 ExplicitValueProvider
* pExplicitValueProvider
= 0;//ExplicitValueProvider::getExplicitValueProvider(xChartView); dies erzeugt alle sichtbaren datenpinkte, das ist zu viel
124 ObjectHierarchy
aHierarchy( xChartDoc
, pExplicitValueProvider
, true /*bFlattenDiagram*/, true /*bOrderingForElementSelector*/ );
125 lcl_addObjectsToList( aHierarchy
, aHierarchy
.getRootNodeCID(), m_aEntries
, 0, xChartDoc
);
127 std::vector
< ListBoxEntryData
>::iterator
aIt( m_aEntries
.begin() );
128 if( bAddSelectionToList
)
130 rtl::OUString aSeriesCID
= ObjectIdentifier::createClassifiedIdentifierForParticle( ObjectIdentifier::getSeriesParticleFromCID( aSelectedCID
) );
131 for( aIt
= m_aEntries
.begin(); aIt
!= m_aEntries
.end(); ++aIt
)
133 if( aIt
->CID
.match( aSeriesCID
) )
135 ListBoxEntryData aEntry
;
136 aEntry
.UIName
= ObjectNameProvider::getNameForCID( aSelectedCID
, xChartDoc
);
137 aEntry
.CID
= aSelectedCID
;
139 if( aIt
!= m_aEntries
.end() )
140 m_aEntries
.insert(aIt
, aEntry
);
142 m_aEntries
.push_back( aEntry
);
148 USHORT nEntryPosToSelect
= 0; bool bSelectionFound
= false;
149 aIt
= m_aEntries
.begin();
150 for( USHORT nN
=0; aIt
!= m_aEntries
.end(); ++aIt
, ++nN
)
152 InsertEntry( aIt
->UIName
);
153 if( !bSelectionFound
&& aSelectedCID
.equals( aIt
->CID
) )
155 nEntryPosToSelect
= nN
;
156 bSelectionFound
= true;
160 if( bSelectionFound
)
161 SelectEntryPos(nEntryPosToSelect
);
163 USHORT nEntryCount
= GetEntryCount();
164 if( nEntryCount
> 100 )
166 SetDropDownLineCount( nEntryCount
);
168 SaveValue();//remind current selection pos
171 void SelectorListBox::ReleaseFocus_Impl()
173 if ( !m_bReleaseFocus
)
175 m_bReleaseFocus
= true;
179 Reference
< frame::XController
> xController( m_xChartController
);
180 Reference
< frame::XFrame
> xFrame( xController
->getFrame() );
181 if ( xFrame
.is() && xFrame
->getContainerWindow().is() )
182 xFrame
->getContainerWindow()->setFocus();
185 void SelectorListBox::Select()
189 if ( !IsTravelSelect() )
191 USHORT nPos
= GetSelectEntryPos();
192 if( nPos
< m_aEntries
.size() )
194 rtl::OUString aCID
= m_aEntries
[nPos
].CID
;
195 uno::Any
aASelection( uno::makeAny(aCID
) );
196 Reference
< view::XSelectionSupplier
> xSelectionSupplier( m_xChartController
.get(), uno::UNO_QUERY
);
197 if( xSelectionSupplier
.is() )
198 xSelectionSupplier
->select(aASelection
);
204 long SelectorListBox::Notify( NotifyEvent
& rNEvt
)
208 if ( rNEvt
.GetType() == EVENT_KEYINPUT
)
210 USHORT nCode
= rNEvt
.GetKeyEvent()->GetKeyCode().GetCode();
217 if ( KEY_TAB
== nCode
)
218 m_bReleaseFocus
= false;
226 SelectEntryPos( GetSavedValue() ); //restore saved selection
231 else if ( EVENT_LOSEFOCUS
== rNEvt
.GetType() )
234 SelectEntryPos( GetSavedValue() );
237 return nHandled
? nHandled
: ListBox::Notify( rNEvt
);
240 Reference
< ::com::sun::star::accessibility::XAccessible
> SelectorListBox::CreateAccessible()
242 UpdateChartElementsListAndSelection();
243 return ListBox::CreateAccessible();
246 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
247 APPHELPER_XSERVICEINFO_IMPL( ElementSelectorToolbarController
, lcl_aServiceName
);
249 //------------------------------------------------------------------------------
250 Sequence
< ::rtl::OUString
> ElementSelectorToolbarController::getSupportedServiceNames_Static()
252 Sequence
< ::rtl::OUString
> aServices(1);
253 aServices
[ 0 ] = C2U( "com.sun.star.frame.ToolbarController" );
256 // -----------------------------------------------------------------------------
257 ElementSelectorToolbarController::ElementSelectorToolbarController( const uno::Reference
< uno::XComponentContext
> & xContext
)
261 // -----------------------------------------------------------------------------
262 ElementSelectorToolbarController::~ElementSelectorToolbarController()
265 // -----------------------------------------------------------------------------
267 Any SAL_CALL
ElementSelectorToolbarController::queryInterface( const Type
& _rType
) throw (RuntimeException
)
269 Any aReturn
= ToolboxController::queryInterface(_rType
);
270 if (!aReturn
.hasValue())
271 aReturn
= ElementSelectorToolbarController_BASE::queryInterface(_rType
);
274 // -----------------------------------------------------------------------------
275 void SAL_CALL
ElementSelectorToolbarController::acquire() throw ()
277 ToolboxController::acquire();
279 // -----------------------------------------------------------------------------
280 void SAL_CALL
ElementSelectorToolbarController::release() throw ()
282 ToolboxController::release();
284 // -----------------------------------------------------------------------------
285 void SAL_CALL
ElementSelectorToolbarController::initialize( const Sequence
< Any
>& rArguments
) throw (Exception
, RuntimeException
)
287 ToolboxController::initialize(rArguments
);
289 // -----------------------------------------------------------------------------
290 void SAL_CALL
ElementSelectorToolbarController::statusChanged( const frame::FeatureStateEvent
& rEvent
) throw ( RuntimeException
)
292 if( m_apSelectorListBox
.get() )
294 vos::OGuard
aSolarMutexGuard( Application::GetSolarMutex() );
295 if( rEvent
.FeatureURL
.Path
.equalsAscii( "ChartElementSelector" ) )
297 Reference
< frame::XController
> xChartController
;
298 rEvent
.State
>>= xChartController
;
299 m_apSelectorListBox
->SetChartController( xChartController
);
300 m_apSelectorListBox
->UpdateChartElementsListAndSelection();
304 // -----------------------------------------------------------------------------
305 uno::Reference
< awt::XWindow
> SAL_CALL
ElementSelectorToolbarController::createItemWindow( const uno::Reference
< awt::XWindow
>& xParent
)
306 throw (uno::RuntimeException
)
308 uno::Reference
< awt::XWindow
> xItemWindow
;
309 if( !m_apSelectorListBox
.get() )
311 Window
* pParent
= VCLUnoHelper::GetWindow( xParent
);
314 m_apSelectorListBox
= ::std::auto_ptr
< SelectorListBox
>( new SelectorListBox( pParent
, WB_DROPDOWN
|WB_AUTOHSCROLL
|WB_BORDER
) );
315 ::Size
aLogicalSize( 95, 160 );
316 ::Size aPixelSize
= m_apSelectorListBox
->LogicToPixel( aLogicalSize
, MAP_APPFONT
);
317 m_apSelectorListBox
->SetSizePixel( aPixelSize
);
318 m_apSelectorListBox
->SetDropDownLineCount( 5 );
321 if( m_apSelectorListBox
.get() )
322 xItemWindow
= VCLUnoHelper::GetInterface( m_apSelectorListBox
.get() );
326 //..........................................................................
328 //..........................................................................