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