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