xref: /AOO41X/main/sw/source/core/doc/docxforms.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 
31 
32 
33 
34 #include <doc.hxx>
35 #include <docsh.hxx>
36 #include <com/sun/star/uno/Reference.hxx>
37 #include <com/sun/star/container/XNameContainer.hpp>
38 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 #include <com/sun/star/frame/XModule.hpp>
40 #include <com/sun/star/xforms/XModel.hpp>
41 #include <com/sun/star/xforms/XFormsUIHelper1.hpp>
42 #include <unotools/processfactory.hxx>
43 #include <tools/diagnose_ex.h>
44 
45 
46 using namespace ::com::sun::star;
47 
48 using uno::Reference;
49 using uno::XInterface;
50 using uno::UNO_QUERY;
51 using uno::makeAny;
52 using uno::Exception;
53 using container::XNameContainer;
54 using xforms::XModel;
55 using frame::XModule;
56 using xforms::XFormsUIHelper1;
57 using rtl::OUString;
58 
59 
60 Reference<XNameContainer> SwDoc::getXForms() const
61 {
62     return xXForms;
63 }
64 
65 bool SwDoc::isXForms() const
66 {
67     return xXForms.is();
68 }
69 
70 Reference<XInterface> lcl_createInstance( const sal_Char* pServiceName )
71 {
72     DBG_ASSERT( pServiceName != NULL, "no service name" );
73     return utl::getProcessServiceFactory()->createInstance(
74         OUString::createFromAscii( pServiceName  ) );
75 }
76 
77 void SwDoc::initXForms( bool bCreateDefaultModel )
78 {
79     DBG_ASSERT( ! isXForms(), "please initialize only once" );
80 
81     try
82     {
83         // create XForms components
84         xXForms.set( lcl_createInstance( "com.sun.star.xforms.XForms" ),
85                     UNO_QUERY );
86         DBG_ASSERT( xXForms.is(), "can't create XForms container" );
87 
88         // change our module identifier, to be able to have a dedicated UI
89         Reference< XModule > xModule;
90         SwDocShell* pShell( GetDocShell() );
91         if ( pShell )
92             xModule = xModule.query( pShell->GetModel() );
93         DBG_ASSERT( xModule.is(), "SwDoc::initXForms: no XModule at the document!" );
94         if ( xModule.is() )
95             xModule->setIdentifier( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xforms.XMLFormDocument" ) ) );
96 
97         // create default model
98         if( bCreateDefaultModel && xXForms.is() )
99         {
100             OUString sName(RTL_CONSTASCII_USTRINGPARAM("Model 1"));
101             Reference<XModel> xModel(
102                 lcl_createInstance( "com.sun.star.xforms.Model" ),
103                 UNO_QUERY );
104             DBG_ASSERT( xModel.is(), "no model?" );
105             if( xModel.is() )
106             {
107                 xModel->setID( sName );
108                 Reference<XFormsUIHelper1>( xModel, UNO_QUERY )->newInstance(
109                     OUString(RTL_CONSTASCII_USTRINGPARAM("Instance 1")),
110                     OUString(), sal_True );
111                 xModel->initialize();
112                 xXForms->insertByName( sName, makeAny( xModel ) );
113             }
114             DBG_ASSERT( xXForms->hasElements(), "can't create XForms model" );
115         }
116 
117         DBG_ASSERT( isXForms(), "initialization failed" );
118     }
119     catch( const Exception& )
120     {
121     	DBG_UNHANDLED_EXCEPTION();
122     }
123 }
124