1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
28*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
29*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
30*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
31*cdf0e10cSrcweir import com.sun.star.util.XCloseable;
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir public abstract class DocumentBasedExample implements com.sun.star.lang.XEventListener
34*cdf0e10cSrcweir {
35*cdf0e10cSrcweir     /// the intial remote context from the office
36*cdf0e10cSrcweir     protected XComponentContext       m_xCtx;
37*cdf0e10cSrcweir     /// our current test document
38*cdf0e10cSrcweir     protected DocumentHelper          m_document;
39*cdf0e10cSrcweir     protected FormLayer               m_formLayer;
40*cdf0e10cSrcweir     protected DocumentType            m_documentType;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir     /** Creates a new instance of DocumentBasedExample */
43*cdf0e10cSrcweir     public DocumentBasedExample( DocumentType documentType )
44*cdf0e10cSrcweir     {
45*cdf0e10cSrcweir         bootstrapUNO();
46*cdf0e10cSrcweir         m_documentType = documentType;
47*cdf0e10cSrcweir     }
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
50*cdf0e10cSrcweir     private void bootstrapUNO()
51*cdf0e10cSrcweir     {
52*cdf0e10cSrcweir         try
53*cdf0e10cSrcweir         {
54*cdf0e10cSrcweir             /*
55*cdf0e10cSrcweir             final XComponentContext componentContext = com.sun.star.comp.helper.Bootstrap.
56*cdf0e10cSrcweir                 createInitialComponentContext( null );
57*cdf0e10cSrcweir             final XMultiComponentFactory localServiceManager = componentContext.getServiceManager();
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir             final XUnoUrlResolver urlResolver = (XUnoUrlResolver) UnoRuntime.queryInterface(
60*cdf0e10cSrcweir                 XUnoUrlResolver.class, localServiceManager.createInstanceWithContext(
61*cdf0e10cSrcweir                     "com.sun.star.bridge.UnoUrlResolver", componentContext) );
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir             final String connectStr = "uno:pipe,name=<pipename>;urp;StarOffice.ComponentContext";
64*cdf0e10cSrcweir             final Object initialObject = urlResolver.resolve( connectStr );
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir             m_xCtx = (XComponentContext)UnoRuntime.queryInterface( XComponentContext.class,
67*cdf0e10cSrcweir                 initialObject );
68*cdf0e10cSrcweir             */
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir             // get the remote office component context
71*cdf0e10cSrcweir             m_xCtx = com.sun.star.comp.helper.Bootstrap.bootstrap();
72*cdf0e10cSrcweir             System.out.println("Connected to a running office ...");
73*cdf0e10cSrcweir         }
74*cdf0e10cSrcweir         catch (java.lang.Exception e)
75*cdf0e10cSrcweir         {
76*cdf0e10cSrcweir             e.printStackTrace( System.err );
77*cdf0e10cSrcweir             System.exit(1);
78*cdf0e10cSrcweir         }
79*cdf0e10cSrcweir     }
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
82*cdf0e10cSrcweir     /** main method for running the sample
83*cdf0e10cSrcweir      */
84*cdf0e10cSrcweir     public void run( String argv[] )
85*cdf0e10cSrcweir     {
86*cdf0e10cSrcweir         try
87*cdf0e10cSrcweir         {
88*cdf0e10cSrcweir             // collect whatever parameters were given
89*cdf0e10cSrcweir             collectParameters( argv );
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir             // prepare our sample document
92*cdf0e10cSrcweir             prepareDocument();
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir             // switch the document view's form layer to alive mode
95*cdf0e10cSrcweir             m_document.getCurrentView().toggleFormDesignMode();
96*cdf0e10cSrcweir             onFormsAlive();
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir             // grab the focus to the first control
99*cdf0e10cSrcweir             m_document.getCurrentView().grabControlFocus();
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir             // ----------------------------------------------
102*cdf0e10cSrcweir             // wait for the user to confirm that we can exit
103*cdf0e10cSrcweir             if ( waitForUserInput() )
104*cdf0e10cSrcweir             {
105*cdf0e10cSrcweir                 // clean up
106*cdf0e10cSrcweir                 cleanUp();
107*cdf0e10cSrcweir             }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir             // if waitForUserInput returns false, the user closed the document manually - no need to do a clean up
110*cdf0e10cSrcweir             // then
111*cdf0e10cSrcweir         }
112*cdf0e10cSrcweir         catch(com.sun.star.uno.Exception e)
113*cdf0e10cSrcweir         {
114*cdf0e10cSrcweir             System.out.println(e);
115*cdf0e10cSrcweir             e.printStackTrace();
116*cdf0e10cSrcweir         }
117*cdf0e10cSrcweir         catch(java.lang.Exception e)
118*cdf0e10cSrcweir         {
119*cdf0e10cSrcweir             System.out.println(e);
120*cdf0e10cSrcweir             e.printStackTrace();
121*cdf0e10cSrcweir         }
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir         System.exit(0);
124*cdf0e10cSrcweir     }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
127*cdf0e10cSrcweir     /** collect the RuntimeArguments
128*cdf0e10cSrcweir     */
129*cdf0e10cSrcweir     protected void collectParameters(String argv[])
130*cdf0e10cSrcweir     {
131*cdf0e10cSrcweir         // not interested in. Derived classes may want to use it.
132*cdf0e10cSrcweir     }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
135*cdf0e10cSrcweir     /** prepares a new document to work with
136*cdf0e10cSrcweir      */
137*cdf0e10cSrcweir     protected void prepareDocument() throws com.sun.star.uno.Exception, java.lang.Exception
138*cdf0e10cSrcweir     {
139*cdf0e10cSrcweir         m_document = DocumentHelper.blankDocument(m_xCtx, m_documentType);
140*cdf0e10cSrcweir         m_document.getDocument( ).addEventListener( this );
141*cdf0e10cSrcweir         m_formLayer = new FormLayer( m_document );
142*cdf0e10cSrcweir     }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
145*cdf0e10cSrcweir     /** called when the form layer has been switched to alive mode
146*cdf0e10cSrcweir     */
147*cdf0e10cSrcweir     protected void onFormsAlive()
148*cdf0e10cSrcweir     {
149*cdf0e10cSrcweir     }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
152*cdf0e10cSrcweir     /** performs any cleanup before exiting the program
153*cdf0e10cSrcweir     */
154*cdf0e10cSrcweir     protected void cleanUp( ) throws java.lang.Exception
155*cdf0e10cSrcweir     {
156*cdf0e10cSrcweir         // do not listen at the document any longer
157*cdf0e10cSrcweir         m_document.getDocument().removeEventListener( this );
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir         // close the document
160*cdf0e10cSrcweir         closeDocument();
161*cdf0e10cSrcweir     }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
164*cdf0e10cSrcweir     /** closes our document, if we have an open one
165*cdf0e10cSrcweir      */
166*cdf0e10cSrcweir     private void closeDocument()
167*cdf0e10cSrcweir     {
168*cdf0e10cSrcweir         try
169*cdf0e10cSrcweir         {
170*cdf0e10cSrcweir             // close our document
171*cdf0e10cSrcweir             if ( m_document != null )
172*cdf0e10cSrcweir             {
173*cdf0e10cSrcweir                 XCloseable closeDoc = (XCloseable)
174*cdf0e10cSrcweir                     UnoRuntime.queryInterface( XCloseable.class,
175*cdf0e10cSrcweir                                                m_document.getDocument() );
176*cdf0e10cSrcweir                 if (closeDoc != null)
177*cdf0e10cSrcweir                     closeDoc.close( true );
178*cdf0e10cSrcweir                 else
179*cdf0e10cSrcweir                     m_document.getDocument().dispose();
180*cdf0e10cSrcweir             }
181*cdf0e10cSrcweir         }
182*cdf0e10cSrcweir         catch ( com.sun.star.uno.Exception e )
183*cdf0e10cSrcweir         {
184*cdf0e10cSrcweir             e.printStackTrace( System.out );
185*cdf0e10cSrcweir         }
186*cdf0e10cSrcweir     }
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
189*cdf0e10cSrcweir     /* internal methods                                                   */
190*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
191*cdf0e10cSrcweir     /** waits for the user to press a key (on the console where she started
192*cdf0e10cSrcweir         the java program) or the document to be closed by the user.
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir         @return <TRUE/> if the user pressed a key on the console,
195*cdf0e10cSrcweir                 <FALSE/> if she closed the document
196*cdf0e10cSrcweir     */
197*cdf0e10cSrcweir     protected boolean waitForUserInput() throws java.lang.Exception
198*cdf0e10cSrcweir     {
199*cdf0e10cSrcweir         synchronized (this)
200*cdf0e10cSrcweir         {
201*cdf0e10cSrcweir             WaitForInput aWait = new WaitForInput( this );
202*cdf0e10cSrcweir             aWait.start();
203*cdf0e10cSrcweir             wait();
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir             // if the waiter thread is done, the user pressed enter
206*cdf0e10cSrcweir             boolean bKeyPressed = aWait.isDone();
207*cdf0e10cSrcweir             if ( !bKeyPressed )
208*cdf0e10cSrcweir                 aWait.interrupt();
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir             return bKeyPressed;
211*cdf0e10cSrcweir         }
212*cdf0e10cSrcweir     }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
215*cdf0e10cSrcweir     /* XEventListener overridables                                        */
216*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
217*cdf0e10cSrcweir     public void disposing( com.sun.star.lang.EventObject eventObject )
218*cdf0e10cSrcweir     {
219*cdf0e10cSrcweir         if ( m_document.getDocument().equals( eventObject.Source ) )
220*cdf0e10cSrcweir 	{
221*cdf0e10cSrcweir             // notify ourself that we can stop waiting for user input
222*cdf0e10cSrcweir             synchronized (this)
223*cdf0e10cSrcweir             {
224*cdf0e10cSrcweir                 notify();
225*cdf0e10cSrcweir             }
226*cdf0e10cSrcweir         }
227*cdf0e10cSrcweir     }
228*cdf0e10cSrcweir }
229