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 import com.sun.star.bridge.XUnoUrlResolver;
28 import com.sun.star.lang.XMultiComponentFactory;
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.uno.XComponentContext;
31 import com.sun.star.util.XCloseable;
32 
33 public abstract class DocumentBasedExample implements com.sun.star.lang.XEventListener
34 {
35     /// the intial remote context from the office
36     protected XComponentContext       m_xCtx;
37     /// our current test document
38     protected DocumentHelper          m_document;
39     protected FormLayer               m_formLayer;
40     protected DocumentType            m_documentType;
41 
42     /** Creates a new instance of DocumentBasedExample */
43     public DocumentBasedExample( DocumentType documentType )
44     {
45         bootstrapUNO();
46         m_documentType = documentType;
47     }
48 
49     /* ------------------------------------------------------------------ */
50     private void bootstrapUNO()
51     {
52         try
53         {
54             /*
55             final XComponentContext componentContext = com.sun.star.comp.helper.Bootstrap.
56                 createInitialComponentContext( null );
57             final XMultiComponentFactory localServiceManager = componentContext.getServiceManager();
58 
59             final XUnoUrlResolver urlResolver = (XUnoUrlResolver) UnoRuntime.queryInterface(
60                 XUnoUrlResolver.class, localServiceManager.createInstanceWithContext(
61                     "com.sun.star.bridge.UnoUrlResolver", componentContext) );
62 
63             final String connectStr = "uno:pipe,name=<pipename>;urp;StarOffice.ComponentContext";
64             final Object initialObject = urlResolver.resolve( connectStr );
65 
66             m_xCtx = (XComponentContext)UnoRuntime.queryInterface( XComponentContext.class,
67                 initialObject );
68             */
69 
70             // get the remote office component context
71             m_xCtx = com.sun.star.comp.helper.Bootstrap.bootstrap();
72             System.out.println("Connected to a running office ...");
73         }
74         catch (java.lang.Exception e)
75         {
76             e.printStackTrace( System.err );
77             System.exit(1);
78         }
79     }
80 
81     /* ------------------------------------------------------------------ */
82     /** main method for running the sample
83      */
84     public void run( String argv[] )
85     {
86         try
87         {
88             // collect whatever parameters were given
89             collectParameters( argv );
90 
91             // prepare our sample document
92             prepareDocument();
93 
94             // switch the document view's form layer to alive mode
95             m_document.getCurrentView().toggleFormDesignMode();
96             onFormsAlive();
97 
98             // grab the focus to the first control
99             m_document.getCurrentView().grabControlFocus();
100 
101             // ----------------------------------------------
102             // wait for the user to confirm that we can exit
103             if ( waitForUserInput() )
104             {
105                 // clean up
106                 cleanUp();
107             }
108 
109             // if waitForUserInput returns false, the user closed the document manually - no need to do a clean up
110             // then
111         }
112         catch(com.sun.star.uno.Exception e)
113         {
114             System.out.println(e);
115             e.printStackTrace();
116         }
117         catch(java.lang.Exception e)
118         {
119             System.out.println(e);
120             e.printStackTrace();
121         }
122 
123         System.exit(0);
124     }
125 
126     /* ------------------------------------------------------------------ */
127     /** collect the RuntimeArguments
128     */
129     protected void collectParameters(String argv[])
130     {
131         // not interested in. Derived classes may want to use it.
132     }
133 
134     /* ------------------------------------------------------------------ */
135     /** prepares a new document to work with
136      */
137     protected void prepareDocument() throws com.sun.star.uno.Exception, java.lang.Exception
138     {
139         m_document = DocumentHelper.blankDocument(m_xCtx, m_documentType);
140         m_document.getDocument( ).addEventListener( this );
141         m_formLayer = new FormLayer( m_document );
142     }
143 
144     /* ------------------------------------------------------------------ */
145     /** called when the form layer has been switched to alive mode
146     */
147     protected void onFormsAlive()
148     {
149     }
150 
151     /* ------------------------------------------------------------------ */
152     /** performs any cleanup before exiting the program
153     */
154     protected void cleanUp( ) throws java.lang.Exception
155     {
156         // do not listen at the document any longer
157         m_document.getDocument().removeEventListener( this );
158 
159         // close the document
160         closeDocument();
161     }
162 
163     /* ------------------------------------------------------------------ */
164     /** closes our document, if we have an open one
165      */
166     private void closeDocument()
167     {
168         try
169         {
170             // close our document
171             if ( m_document != null )
172             {
173                 XCloseable closeDoc = (XCloseable)
174                     UnoRuntime.queryInterface( XCloseable.class,
175                                                m_document.getDocument() );
176                 if (closeDoc != null)
177                     closeDoc.close( true );
178                 else
179                     m_document.getDocument().dispose();
180             }
181         }
182         catch ( com.sun.star.uno.Exception e )
183         {
184             e.printStackTrace( System.out );
185         }
186     }
187 
188     /* ------------------------------------------------------------------ */
189     /* internal methods                                                   */
190     /* ------------------------------------------------------------------ */
191     /** waits for the user to press a key (on the console where she started
192         the java program) or the document to be closed by the user.
193 
194         @return <TRUE/> if the user pressed a key on the console,
195                 <FALSE/> if she closed the document
196     */
197     protected boolean waitForUserInput() throws java.lang.Exception
198     {
199         synchronized (this)
200         {
201             WaitForInput aWait = new WaitForInput( this );
202             aWait.start();
203             wait();
204 
205             // if the waiter thread is done, the user pressed enter
206             boolean bKeyPressed = aWait.isDone();
207             if ( !bKeyPressed )
208                 aWait.interrupt();
209 
210             return bKeyPressed;
211         }
212     }
213 
214     /* ------------------------------------------------------------------ */
215     /* XEventListener overridables                                        */
216     /* ------------------------------------------------------------------ */
217     public void disposing( com.sun.star.lang.EventObject eventObject )
218     {
219         if ( m_document.getDocument().equals( eventObject.Source ) )
220 	{
221             // notify ourself that we can stop waiting for user input
222             synchronized (this)
223             {
224                 notify();
225             }
226         }
227     }
228 }
229