summaryrefslogtreecommitdiff
path: root/svtools/workben/treecontrol/treetest.cxx
blob: f82c4ae388f51894922c14dbb32e90b73152eede (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#include <stdio.h>
#include <wchar.h>

#include <cppuhelper/bootstrap.hxx>

#include <osl/file.hxx>
#include <osl/process.h>

#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/awt/tree/XMutableTreeDataModel.hpp>
#include <com/sun/star/awt/tree/XTreeControl.hpp>
#include <com/sun/star/awt/tree/XTreeExpansionListener.hpp>
#include <com/sun/star/awt/tree/XTreeEditListener.hpp>
#include <com/sun/star/awt/XDialog.hpp>
#include <com/sun/star/awt/XControlModel.hpp>
#include <com/sun/star/container/XNameContainer.hpp>
#include <com/sun/star/awt/XControl.hpp>
#include <com/sun/star/awt/XControlContainer.hpp>
#include <com/sun/star/view/SelectionType.hpp>

#include <tools/urlobj.hxx>
#include <vcl/image.hxx>
#include <vcl/graph.hxx>

#include <cppuhelper/implbase2.hxx>

#include <string.h>
#include <rtl/ref.hxx>

#include "imagemgr.hxx"

using rtl::OUString;
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::beans;
using namespace com::sun::star::bridge;
using namespace com::sun::star::frame;
using namespace com::sun::star::registry;
using namespace com::sun::star::awt;
using namespace com::sun::star::awt::tree;
using namespace com::sun::star::container;
using namespace com::sun::star::view;
using namespace com::sun::star::util;

class DirectoryTree : public ::cppu::WeakImplHelper2< XTreeExpansionListener, XTreeEditListener >
{
public:
    DirectoryTree( const Reference< XComponentContext >& xComponentContext );
    virtual ~DirectoryTree();

    void fillNode( const Reference< XMutableTreeNode >& xNode );
    void display( const OUString& rURL );

    // XTreeExpansionListener
    virtual void SAL_CALL requestChildNodes( const TreeExpansionEvent& Event ) throw (RuntimeException);
    virtual void SAL_CALL treeExpanding( const TreeExpansionEvent& Event ) throw (ExpandVetoException, RuntimeException);
    virtual void SAL_CALL treeCollapsing( const TreeExpansionEvent& Event ) throw (ExpandVetoException, RuntimeException);
    virtual void SAL_CALL treeExpanded( const TreeExpansionEvent& Event ) throw (RuntimeException);
    virtual void SAL_CALL treeCollapsed( const TreeExpansionEvent& Event ) throw (RuntimeException);

    // XTreeEditListener
    virtual void SAL_CALL nodeEditing( const Reference< XTreeNode >& Node ) throw (VetoException, RuntimeException);
    virtual void SAL_CALL nodeEdited( const Reference< XTreeNode >& Node, const OUString& NewText ) throw (RuntimeException);

    // XEventListener
    virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (RuntimeException);

    Reference< XMultiComponentFactory > mxMultiComponentFactoryServer;
    Reference< XComponentContext > mxComponentContext;
    Reference< XTreeControl > mxTreeControl;
    Reference< XMutableTreeDataModel > mxTreeDataModel;
};

DirectoryTree::DirectoryTree( const Reference< XComponentContext >& xComponentContext )
: mxComponentContext( xComponentContext )
, mxMultiComponentFactoryServer( xComponentContext->getServiceManager() )
{
}

DirectoryTree::~DirectoryTree()
{
}

void DirectoryTree::display( const OUString& rURL )
{
    // some property names for later use
    const OUString sPositionX( RTL_CONSTASCII_USTRINGPARAM( "PositionX" ) );
    const OUString sPositionY( RTL_CONSTASCII_USTRINGPARAM( "PositionY" ) );
    const OUString sWidth( RTL_CONSTASCII_USTRINGPARAM( "Width" ) );
    const OUString sHeight( RTL_CONSTASCII_USTRINGPARAM( "Height" ) );
    const OUString sDataModel( RTL_CONSTASCII_USTRINGPARAM( "DataModel" ) );
    const OUString sSelectionType( RTL_CONSTASCII_USTRINGPARAM( "SelectionType" ) );
    const OUString sShowsRootHandles( RTL_CONSTASCII_USTRINGPARAM( "ShowsRootHandles" ) );
    const OUString sShowsHandles( RTL_CONSTASCII_USTRINGPARAM( "ShowsHandles" ) );
    const OUString sRootDisplayed( RTL_CONSTASCII_USTRINGPARAM( "RootDisplayed" ) );
    const OUString sEditable( RTL_CONSTASCII_USTRINGPARAM( "Editable" ) );
    const OUString sTitle( RTL_CONSTASCII_USTRINGPARAM( "Title" ) );
    const OUString sRowHeight( RTL_CONSTASCII_USTRINGPARAM( "RowHeight" ) );

    // first create a data model for our tree control
    mxTreeDataModel = Reference< XMutableTreeDataModel >(
        mxMultiComponentFactoryServer->createInstanceWithContext(
            OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.awt.tree.MutableTreeDataModel" ) ),
            mxComponentContext ), UNO_QUERY_THROW );

    // now fill it with some sample data
    Reference< XMutableTreeNode > xNode( mxTreeDataModel->createNode( Any( rURL ), false ), UNO_QUERY_THROW );
    xNode->setDataValue( Any( rURL ) );
    xNode->setExpandedGraphicURL( OUString( RTL_CONSTASCII_USTRINGPARAM( "private:graphicrepository/sd/res/triangle_down.png" ) ) );
    xNode->setCollapsedGraphicURL( OUString( RTL_CONSTASCII_USTRINGPARAM( "private:graphicrepository/sd/res/triangle_right.png" ) ) );

    fillNode( xNode );
    mxTreeDataModel->setRoot( xNode );

    // now create the dialog
    Reference< XControlModel > xDialogModel(
        mxMultiComponentFactoryServer->createInstanceWithContext(
            OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.awt.UnoControlDialogModel" ) ),
            mxComponentContext ), UNO_QUERY_THROW );

    Reference< XPropertySet > xDialogPropertySet( xDialogModel, UNO_QUERY_THROW );
    xDialogPropertySet->setPropertyValue( sPositionX,   Any( sal_Int32(50) ) );
    xDialogPropertySet->setPropertyValue( sPositionY,   Any( sal_Int32(50) ) );
    xDialogPropertySet->setPropertyValue( sWidth,       Any( sal_Int32(256) ) );
    xDialogPropertySet->setPropertyValue( sHeight,      Any( sal_Int32(256) ) );
    xDialogPropertySet->setPropertyValue( sTitle,       Any( OUString( RTL_CONSTASCII_USTRINGPARAM( "Tree Control Test" ) ) ) );

    Reference< XMultiServiceFactory > xDialogMSF( xDialogModel, UNO_QUERY_THROW );

    // now create our tree control
    Reference< XControlModel > xTreeControlModel(
        xDialogMSF->createInstance(
            OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.awt.tree.TreeControlModel" ) ) ),
            UNO_QUERY_THROW );

    Reference< XPropertySet > XTreeControlModelSet( xTreeControlModel, UNO_QUERY_THROW );

    XTreeControlModelSet->setPropertyValue( sSelectionType, Any( SelectionType_NONE ) );
    XTreeControlModelSet->setPropertyValue( sPositionX,     Any( sal_Int32(3) ) );
    XTreeControlModelSet->setPropertyValue( sPositionY,     Any( sal_Int32(3) ) );
    XTreeControlModelSet->setPropertyValue( sWidth,         Any( sal_Int32(253) ) );
    XTreeControlModelSet->setPropertyValue( sHeight,        Any( sal_Int32(253) ) );
    XTreeControlModelSet->setPropertyValue( sDataModel,     Any( mxTreeDataModel ) );
    XTreeControlModelSet->setPropertyValue( sShowsRootHandles,Any( sal_False ) );
    XTreeControlModelSet->setPropertyValue( sShowsHandles,  Any( sal_False ) );
    XTreeControlModelSet->setPropertyValue( sRootDisplayed, Any( sal_True ) );
    XTreeControlModelSet->setPropertyValue( sEditable,      Any( sal_True ) );
//  XTreeControlModelSet->setPropertyValue( sRowHeight,     Any( sal_Int32( 12 ) ) );

    Reference< XNameContainer > xDialogModelContainer( xDialogModel, UNO_QUERY_THROW );

    const OUString sTreeControlName( RTL_CONSTASCII_USTRINGPARAM( "tree" ) );

    xDialogModelContainer->insertByName( sTreeControlName, Any( xTreeControlModel ) );

    // now create the peers
    Reference< XControl > xDialogControl(
        mxMultiComponentFactoryServer->createInstanceWithContext(
            OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.awt.UnoControlDialog" ) ),
            mxComponentContext ), UNO_QUERY_THROW );

    xDialogControl->setModel( xDialogModel );

    Reference< XToolkit > xToolkit(
        mxMultiComponentFactoryServer->createInstanceWithContext(
            OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.awt.Toolkit" ) ),
            mxComponentContext ), UNO_QUERY_THROW );
    xDialogControl->createPeer( xToolkit, 0 );

    // get the peers of the sub controls from the dialog peer container
    Reference< XControlContainer > xDialogContainer( xDialogControl, UNO_QUERY_THROW );
    mxTreeControl = Reference< XTreeControl >( xDialogContainer->getControl( sTreeControlName ), UNO_QUERY_THROW );

    Reference< XTreeExpansionListener > xListener( static_cast<XWeak*>(this), UNO_QUERY_THROW );
    mxTreeControl->addTreeExpansionListener( xListener );
    Reference< XDialog > xDialog( xDialogControl, UNO_QUERY_THROW );
    xDialog->execute();
}

void DirectoryTree::fillNode( const Reference< XMutableTreeNode >& xNode )
{
    if( xNode->getChildCount() == 0 )
    {
        OUString sParentPath;
        xNode->getDataValue() >>= sParentPath;

        osl::Directory aDirectory( sParentPath );
        if( aDirectory.open() == osl::Directory::E_None )
        {
            osl::DirectoryItem aItem;
            osl::FileStatus fs( FileStatusMask_Type | FileStatusMask_FileURL);
            while( aDirectory.getNextItem( aItem, 0xffffffff ) == osl::Directory::E_None )
            {
                if (aItem.getFileStatus(fs) == osl::FileBase::E_None)
                {
                    bool bDirectory = fs.getFileType() == osl::FileStatus::Directory;
                    Reference< XMutableTreeNode > xChildNode( mxTreeDataModel->createNode( Any( fs.getFileName() ), bDirectory ), UNO_QUERY_THROW );
                    xChildNode->setDataValue( Any( fs.getFileURL() ) );
                    if( bDirectory )
                    {
                        xChildNode->setExpandedGraphicURL( OUString( RTL_CONSTASCII_USTRINGPARAM( "private:graphicrepository/sd/res/triangle_down.png" ) ) );
                        xChildNode->setCollapsedGraphicURL( OUString( RTL_CONSTASCII_USTRINGPARAM( "private:graphicrepository/sd/res/triangle_right.png" ) ) );
                    }
                    else
                    {
                        xChildNode->setNodeGraphicURL( OUString( RTL_CONSTASCII_USTRINGPARAM("private:graphicrepository/sw/imglst/nc20010.png") ) );
                    }
                    xNode->appendChild( xChildNode );
                }
            }
        }
    }
}

// XTreeExpansionListener
void SAL_CALL DirectoryTree::requestChildNodes( const TreeExpansionEvent& rEvent ) throw (RuntimeException)
{
    if( rEvent.Node.is() && rEvent.Node->hasChildsOnDemand() )
    {
        Reference< XMutableTreeNode > xNode( rEvent.Node, UNO_QUERY );
        if( xNode.is() )
        {
            fillNode( xNode );
            xNode->setHasChildsOnDemand( sal_False );
        }
    }
}

void SAL_CALL DirectoryTree::treeExpanding( const TreeExpansionEvent& /*rEvent*/ ) throw (ExpandVetoException, RuntimeException)
{
}

void SAL_CALL DirectoryTree::treeCollapsing( const TreeExpansionEvent&  ) throw (ExpandVetoException, RuntimeException)
{
}

void SAL_CALL DirectoryTree::treeExpanded( const TreeExpansionEvent&  ) throw (RuntimeException)
{
}

void SAL_CALL DirectoryTree::treeCollapsed( const TreeExpansionEvent& /*rEvent*/ ) throw (RuntimeException)
{
/*
    if( rEvent.Node != mxTreeDataModel->getRoot() )
    {
        Reference< XMutableTreeNode > xNode( rEvent.Node, UNO_QUERY );
        if( xNode.is() )
        {
            while( xNode->getChildCount() )
                xNode->removeChildByIndex(0);
            xNode->setHasChildsOnDemand( sal_True );
        }
    }
*/
}

// XTreeEditListener
void SAL_CALL DirectoryTree::nodeEditing( const Reference< XTreeNode >&  ) throw (VetoException, RuntimeException)
{
}

void SAL_CALL DirectoryTree::nodeEdited( const Reference< XTreeNode >& , const OUString&  ) throw (RuntimeException)
{
}

// XEventListener
void SAL_CALL DirectoryTree::disposing( const ::com::sun::star::lang::EventObject&  ) throw (RuntimeException)
{
}

//============================================================================
int SAL_CALL main( int argc, char **argv )
{
    OUString sConnectionString(RTL_CONSTASCII_USTRINGPARAM("uno:socket,host=localhost,port=5678;urp;StarOffice.ServiceManager"));

    if (argc < 2)
    {
        printf("using: treetest <directory> [<uno_connection_url>]\n\n"
#ifdef WNT
                "example: treetest  \"c:\" \"uno:socket,host=localhost,port=5678;urp;StarOffice.ServiceManager\"\n");
#else
                "example: treetest  \"/etc\" \"uno:socket,host=localhost,port=5678;urp;StarOffice.ServiceManager\"\n");
#endif
        exit(1);
    }
     if (argc == 3)
    {
        sConnectionString = OUString::createFromAscii(argv[2]);
    }

    // Creates a simple registry service instance.
    Reference< XSimpleRegistry > xSimpleRegistry(::cppu::createSimpleRegistry() );

    // Connects the registry to a persistent data source represented by an URL.
    xSimpleRegistry->open(
        OUString( RTL_CONSTASCII_USTRINGPARAM("treetest.rdb") ), sal_True, sal_False );

    /* Bootstraps an initial component context with service manager upon a given
       registry. This includes insertion of initial services:
       - (registry) service manager, shared lib loader,
       - simple registry, nested registry,
       - implementation registration
       - registry typedescription provider, typedescription manager (also
         installs it into cppu core)
    */
    Reference< XComponentContext > xComponentContext(
        ::cppu::bootstrap_InitialComponentContext( xSimpleRegistry ) );

    /* Gets the service manager instance to be used (or null). This method has
       been added for convenience, because the service manager is a often used
       object.
    */
    Reference< XMultiComponentFactory > xMultiComponentFactoryClient(
        xComponentContext->getServiceManager() );

    /* Creates an instance of a component which supports the services specified
       by the factory.
    */
    Reference< XInterface > xInterface =
        xMultiComponentFactoryClient->createInstanceWithContext(
            OUString::createFromAscii( "com.sun.star.bridge.UnoUrlResolver" ),
            xComponentContext );

    Reference< XUnoUrlResolver > resolver( xInterface, UNO_QUERY );

    // Resolves the component context from the office, on the uno URL given by argv[1].
    try
    {
        xInterface = Reference< XInterface >(
            resolver->resolve( sConnectionString ), UNO_QUERY );
    }
    catch ( Exception& e )
    {
        printf("Error: cannot establish a connection using '%s':\n       %s\n",
               OUStringToOString(sConnectionString, RTL_TEXTENCODING_ASCII_US).getStr(),
               OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US).getStr());
        exit(1);
    }

    // gets the server component context as property of the office component factory
    Reference< XPropertySet > xPropSet( xInterface, UNO_QUERY );
    xPropSet->getPropertyValue( OUString::createFromAscii("DefaultContext") ) >>= xComponentContext;

    // gets the service manager from the office
    Reference< XMultiComponentFactory > xMultiComponentFactoryServer(
        xComponentContext->getServiceManager() );

    try
    {
        OUString ustrFileURL;
        osl::FileBase::getFileURLFromSystemPath( OUString::createFromAscii(argv[1]), ustrFileURL );

        rtl::Reference< DirectoryTree >(
            new DirectoryTree( xComponentContext ) )->display(ustrFileURL);
    }
    catch( Exception& e )
    {
        printf("Error: exception caught during test:\n       %s\n",
               OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US).getStr());
        exit(1);
    }

    // dispose the local service manager
    Reference< XComponent >::query( xMultiComponentFactoryClient )->dispose();

    return 0;
}