Update ooo320-m1
[ooovba.git] / svtools / workben / treecontrol / treetest.cxx
bloba898909139773cfa8d836cb0f2e11542beff7734
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: treetest.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 #include <stdio.h>
32 #include <wchar.h>
34 #include <cppuhelper/bootstrap.hxx>
36 #include <osl/file.hxx>
37 #include <osl/process.h>
39 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
40 #include <com/sun/star/frame/XComponentLoader.hpp>
41 #include <com/sun/star/beans/XPropertySet.hpp>
42 #include <com/sun/star/awt/tree/XMutableTreeDataModel.hpp>
43 #include <com/sun/star/awt/tree/XTreeControl.hpp>
44 #include <com/sun/star/awt/tree/XTreeExpansionListener.hpp>
45 #include <com/sun/star/awt/tree/XTreeEditListener.hpp>
46 #include <com/sun/star/awt/XDialog.hpp>
47 #include <com/sun/star/awt/XControlModel.hpp>
48 #include <com/sun/star/container/XNameContainer.hpp>
49 #include <com/sun/star/awt/XControl.hpp>
50 #include <com/sun/star/awt/XControlContainer.hpp>
51 #include <com/sun/star/view/SelectionType.hpp>
53 #include <tools/urlobj.hxx>
54 #include <vcl/image.hxx>
55 #include <vcl/graph.hxx>
57 #include <cppuhelper/implbase2.hxx>
59 #include <string.h>
60 #include <rtl/ref.hxx>
62 #include "imagemgr.hxx"
64 using rtl::OUString;
65 using namespace com::sun::star::uno;
66 using namespace com::sun::star::lang;
67 using namespace com::sun::star::beans;
68 using namespace com::sun::star::bridge;
69 using namespace com::sun::star::frame;
70 using namespace com::sun::star::registry;
71 using namespace com::sun::star::awt;
72 using namespace com::sun::star::awt::tree;
73 using namespace com::sun::star::container;
74 using namespace com::sun::star::view;
75 using namespace com::sun::star::util;
77 class DirectoryTree : public ::cppu::WeakImplHelper2< XTreeExpansionListener, XTreeEditListener >
79 public:
80 DirectoryTree( const Reference< XComponentContext >& xComponentContext );
81 virtual ~DirectoryTree();
83 void fillNode( const Reference< XMutableTreeNode >& xNode );
84 void display( const OUString& rURL );
86 // XTreeExpansionListener
87 virtual void SAL_CALL requestChildNodes( const TreeExpansionEvent& Event ) throw (RuntimeException);
88 virtual void SAL_CALL treeExpanding( const TreeExpansionEvent& Event ) throw (ExpandVetoException, RuntimeException);
89 virtual void SAL_CALL treeCollapsing( const TreeExpansionEvent& Event ) throw (ExpandVetoException, RuntimeException);
90 virtual void SAL_CALL treeExpanded( const TreeExpansionEvent& Event ) throw (RuntimeException);
91 virtual void SAL_CALL treeCollapsed( const TreeExpansionEvent& Event ) throw (RuntimeException);
93 // XTreeEditListener
94 virtual void SAL_CALL nodeEditing( const Reference< XTreeNode >& Node ) throw (VetoException, RuntimeException);
95 virtual void SAL_CALL nodeEdited( const Reference< XTreeNode >& Node, const OUString& NewText ) throw (RuntimeException);
97 // XEventListener
98 virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (RuntimeException);
100 Reference< XMultiComponentFactory > mxMultiComponentFactoryServer;
101 Reference< XComponentContext > mxComponentContext;
102 Reference< XTreeControl > mxTreeControl;
103 Reference< XMutableTreeDataModel > mxTreeDataModel;
106 DirectoryTree::DirectoryTree( const Reference< XComponentContext >& xComponentContext )
107 : mxComponentContext( xComponentContext )
108 , mxMultiComponentFactoryServer( xComponentContext->getServiceManager() )
112 DirectoryTree::~DirectoryTree()
116 void DirectoryTree::display( const OUString& rURL )
118 // some property names for later use
119 const OUString sPositionX( RTL_CONSTASCII_USTRINGPARAM( "PositionX" ) );
120 const OUString sPositionY( RTL_CONSTASCII_USTRINGPARAM( "PositionY" ) );
121 const OUString sWidth( RTL_CONSTASCII_USTRINGPARAM( "Width" ) );
122 const OUString sHeight( RTL_CONSTASCII_USTRINGPARAM( "Height" ) );
123 const OUString sDataModel( RTL_CONSTASCII_USTRINGPARAM( "DataModel" ) );
124 const OUString sSelectionType( RTL_CONSTASCII_USTRINGPARAM( "SelectionType" ) );
125 const OUString sShowsRootHandles( RTL_CONSTASCII_USTRINGPARAM( "ShowsRootHandles" ) );
126 const OUString sShowsHandles( RTL_CONSTASCII_USTRINGPARAM( "ShowsHandles" ) );
127 const OUString sRootDisplayed( RTL_CONSTASCII_USTRINGPARAM( "RootDisplayed" ) );
128 const OUString sEditable( RTL_CONSTASCII_USTRINGPARAM( "Editable" ) );
129 const OUString sTitle( RTL_CONSTASCII_USTRINGPARAM( "Title" ) );
130 const OUString sRowHeight( RTL_CONSTASCII_USTRINGPARAM( "RowHeight" ) );
132 // first create a data model for our tree control
133 mxTreeDataModel = Reference< XMutableTreeDataModel >(
134 mxMultiComponentFactoryServer->createInstanceWithContext(
135 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.awt.tree.MutableTreeDataModel" ) ),
136 mxComponentContext ), UNO_QUERY_THROW );
138 // now fill it with some sample data
139 Reference< XMutableTreeNode > xNode( mxTreeDataModel->createNode( Any( rURL ), false ), UNO_QUERY_THROW );
140 xNode->setDataValue( Any( rURL ) );
141 xNode->setExpandedGraphicURL( OUString( RTL_CONSTASCII_USTRINGPARAM( "private:graphicrepository/sd/res/triangle_down.png" ) ) );
142 xNode->setCollapsedGraphicURL( OUString( RTL_CONSTASCII_USTRINGPARAM( "private:graphicrepository/sd/res/triangle_right.png" ) ) );
144 fillNode( xNode );
145 mxTreeDataModel->setRoot( xNode );
147 // now create the dialog
148 Reference< XControlModel > xDialogModel(
149 mxMultiComponentFactoryServer->createInstanceWithContext(
150 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.awt.UnoControlDialogModel" ) ),
151 mxComponentContext ), UNO_QUERY_THROW );
153 Reference< XPropertySet > xDialogPropertySet( xDialogModel, UNO_QUERY_THROW );
154 xDialogPropertySet->setPropertyValue( sPositionX, Any( sal_Int32(50) ) );
155 xDialogPropertySet->setPropertyValue( sPositionY, Any( sal_Int32(50) ) );
156 xDialogPropertySet->setPropertyValue( sWidth, Any( sal_Int32(256) ) );
157 xDialogPropertySet->setPropertyValue( sHeight, Any( sal_Int32(256) ) );
158 xDialogPropertySet->setPropertyValue( sTitle, Any( OUString( RTL_CONSTASCII_USTRINGPARAM( "Tree Control Test" ) ) ) );
160 Reference< XMultiServiceFactory > xDialogMSF( xDialogModel, UNO_QUERY_THROW );
162 // now create our tree control
163 Reference< XControlModel > xTreeControlModel(
164 xDialogMSF->createInstance(
165 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.awt.tree.TreeControlModel" ) ) ),
166 UNO_QUERY_THROW );
168 Reference< XPropertySet > XTreeControlModelSet( xTreeControlModel, UNO_QUERY_THROW );
170 XTreeControlModelSet->setPropertyValue( sSelectionType, Any( SelectionType_NONE ) );
171 XTreeControlModelSet->setPropertyValue( sPositionX, Any( sal_Int32(3) ) );
172 XTreeControlModelSet->setPropertyValue( sPositionY, Any( sal_Int32(3) ) );
173 XTreeControlModelSet->setPropertyValue( sWidth, Any( sal_Int32(253) ) );
174 XTreeControlModelSet->setPropertyValue( sHeight, Any( sal_Int32(253) ) );
175 XTreeControlModelSet->setPropertyValue( sDataModel, Any( mxTreeDataModel ) );
176 XTreeControlModelSet->setPropertyValue( sShowsRootHandles,Any( sal_False ) );
177 XTreeControlModelSet->setPropertyValue( sShowsHandles, Any( sal_False ) );
178 XTreeControlModelSet->setPropertyValue( sRootDisplayed, Any( sal_True ) );
179 XTreeControlModelSet->setPropertyValue( sEditable, Any( sal_True ) );
180 // XTreeControlModelSet->setPropertyValue( sRowHeight, Any( sal_Int32( 12 ) ) );
182 Reference< XNameContainer > xDialogModelContainer( xDialogModel, UNO_QUERY_THROW );
184 const OUString sTreeControlName( RTL_CONSTASCII_USTRINGPARAM( "tree" ) );
186 xDialogModelContainer->insertByName( sTreeControlName, Any( xTreeControlModel ) );
188 // now create the peers
189 Reference< XControl > xDialogControl(
190 mxMultiComponentFactoryServer->createInstanceWithContext(
191 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.awt.UnoControlDialog" ) ),
192 mxComponentContext ), UNO_QUERY_THROW );
194 xDialogControl->setModel( xDialogModel );
196 Reference< XToolkit > xToolkit(
197 mxMultiComponentFactoryServer->createInstanceWithContext(
198 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.awt.Toolkit" ) ),
199 mxComponentContext ), UNO_QUERY_THROW );
200 xDialogControl->createPeer( xToolkit, 0 );
202 // get the peers of the sub controls from the dialog peer container
203 Reference< XControlContainer > xDialogContainer( xDialogControl, UNO_QUERY_THROW );
204 mxTreeControl = Reference< XTreeControl >( xDialogContainer->getControl( sTreeControlName ), UNO_QUERY_THROW );
206 Reference< XTreeExpansionListener > xListener( static_cast<XWeak*>(this), UNO_QUERY_THROW );
207 mxTreeControl->addTreeExpansionListener( xListener );
208 Reference< XDialog > xDialog( xDialogControl, UNO_QUERY_THROW );
209 xDialog->execute();
212 void DirectoryTree::fillNode( const Reference< XMutableTreeNode >& xNode )
214 if( xNode->getChildCount() == 0 )
216 OUString sParentPath;
217 xNode->getDataValue() >>= sParentPath;
219 osl::Directory aDirectory( sParentPath );
220 if( aDirectory.open() == osl::Directory::E_None )
222 osl::DirectoryItem aItem;
223 osl::FileStatus fs( FileStatusMask_Type | FileStatusMask_FileURL);
224 while( aDirectory.getNextItem( aItem, 0xffffffff ) == osl::Directory::E_None )
226 if (aItem.getFileStatus(fs) == osl::FileBase::E_None)
228 bool bDirectory = fs.getFileType() == osl::FileStatus::Directory;
229 Reference< XMutableTreeNode > xChildNode( mxTreeDataModel->createNode( Any( fs.getFileName() ), bDirectory ), UNO_QUERY_THROW );
230 xChildNode->setDataValue( Any( fs.getFileURL() ) );
231 if( bDirectory )
233 xChildNode->setExpandedGraphicURL( OUString( RTL_CONSTASCII_USTRINGPARAM( "private:graphicrepository/sd/res/triangle_down.png" ) ) );
234 xChildNode->setCollapsedGraphicURL( OUString( RTL_CONSTASCII_USTRINGPARAM( "private:graphicrepository/sd/res/triangle_right.png" ) ) );
236 else
238 xChildNode->setNodeGraphicURL( OUString( RTL_CONSTASCII_USTRINGPARAM("private:graphicrepository/sw/imglst/nc20010.png") ) );
240 xNode->appendChild( xChildNode );
247 // XTreeExpansionListener
248 void SAL_CALL DirectoryTree::requestChildNodes( const TreeExpansionEvent& rEvent ) throw (RuntimeException)
250 if( rEvent.Node.is() && rEvent.Node->hasChildsOnDemand() )
252 Reference< XMutableTreeNode > xNode( rEvent.Node, UNO_QUERY );
253 if( xNode.is() )
255 fillNode( xNode );
256 xNode->setHasChildsOnDemand( sal_False );
261 void SAL_CALL DirectoryTree::treeExpanding( const TreeExpansionEvent& /*rEvent*/ ) throw (ExpandVetoException, RuntimeException)
265 void SAL_CALL DirectoryTree::treeCollapsing( const TreeExpansionEvent& ) throw (ExpandVetoException, RuntimeException)
269 void SAL_CALL DirectoryTree::treeExpanded( const TreeExpansionEvent& ) throw (RuntimeException)
273 void SAL_CALL DirectoryTree::treeCollapsed( const TreeExpansionEvent& /*rEvent*/ ) throw (RuntimeException)
276 if( rEvent.Node != mxTreeDataModel->getRoot() )
278 Reference< XMutableTreeNode > xNode( rEvent.Node, UNO_QUERY );
279 if( xNode.is() )
281 while( xNode->getChildCount() )
282 xNode->removeChildByIndex(0);
283 xNode->setHasChildsOnDemand( sal_True );
289 // XTreeEditListener
290 void SAL_CALL DirectoryTree::nodeEditing( const Reference< XTreeNode >& ) throw (VetoException, RuntimeException)
294 void SAL_CALL DirectoryTree::nodeEdited( const Reference< XTreeNode >& , const OUString& ) throw (RuntimeException)
298 // XEventListener
299 void SAL_CALL DirectoryTree::disposing( const ::com::sun::star::lang::EventObject& ) throw (RuntimeException)
303 //============================================================================
304 int SAL_CALL main( int argc, char **argv )
306 OUString sConnectionString(RTL_CONSTASCII_USTRINGPARAM("uno:socket,host=localhost,port=5678;urp;StarOffice.ServiceManager"));
308 if (argc < 2)
310 printf("using: treetest <directory> [<uno_connection_url>]\n\n"
311 #ifdef WNT
312 "example: treetest \"c:\" \"uno:socket,host=localhost,port=5678;urp;StarOffice.ServiceManager\"\n");
313 #else
314 "example: treetest \"/etc\" \"uno:socket,host=localhost,port=5678;urp;StarOffice.ServiceManager\"\n");
315 #endif
316 exit(1);
318 if (argc == 3)
320 sConnectionString = OUString::createFromAscii(argv[2]);
323 // Creates a simple registry service instance.
324 Reference< XSimpleRegistry > xSimpleRegistry(::cppu::createSimpleRegistry() );
326 // Connects the registry to a persistent data source represented by an URL.
327 xSimpleRegistry->open(
328 OUString( RTL_CONSTASCII_USTRINGPARAM("treetest.rdb") ), sal_True, sal_False );
330 /* Bootstraps an initial component context with service manager upon a given
331 registry. This includes insertion of initial services:
332 - (registry) service manager, shared lib loader,
333 - simple registry, nested registry,
334 - implementation registration
335 - registry typedescription provider, typedescription manager (also
336 installs it into cppu core)
338 Reference< XComponentContext > xComponentContext(
339 ::cppu::bootstrap_InitialComponentContext( xSimpleRegistry ) );
341 /* Gets the service manager instance to be used (or null). This method has
342 been added for convenience, because the service manager is a often used
343 object.
345 Reference< XMultiComponentFactory > xMultiComponentFactoryClient(
346 xComponentContext->getServiceManager() );
348 /* Creates an instance of a component which supports the services specified
349 by the factory.
351 Reference< XInterface > xInterface =
352 xMultiComponentFactoryClient->createInstanceWithContext(
353 OUString::createFromAscii( "com.sun.star.bridge.UnoUrlResolver" ),
354 xComponentContext );
356 Reference< XUnoUrlResolver > resolver( xInterface, UNO_QUERY );
358 // Resolves the component context from the office, on the uno URL given by argv[1].
361 xInterface = Reference< XInterface >(
362 resolver->resolve( sConnectionString ), UNO_QUERY );
364 catch ( Exception& e )
366 printf("Error: cannot establish a connection using '%s':\n %s\n",
367 OUStringToOString(sConnectionString, RTL_TEXTENCODING_ASCII_US).getStr(),
368 OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US).getStr());
369 exit(1);
372 // gets the server component context as property of the office component factory
373 Reference< XPropertySet > xPropSet( xInterface, UNO_QUERY );
374 xPropSet->getPropertyValue( OUString::createFromAscii("DefaultContext") ) >>= xComponentContext;
376 // gets the service manager from the office
377 Reference< XMultiComponentFactory > xMultiComponentFactoryServer(
378 xComponentContext->getServiceManager() );
382 OUString ustrFileURL;
383 osl::FileBase::getFileURLFromSystemPath( OUString::createFromAscii(argv[1]), ustrFileURL );
385 rtl::Reference< DirectoryTree >(
386 new DirectoryTree( xComponentContext ) )->display(ustrFileURL);
388 catch( Exception& e )
390 printf("Error: exception caught during test:\n %s\n",
391 OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US).getStr());
392 exit(1);
395 // dispose the local service manager
396 Reference< XComponent >::query( xMultiComponentFactoryClient )->dispose();
398 return 0;