2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 import com
.sun
.star
.uno
.UnoRuntime
;
19 import com
.sun
.star
.uno
.XComponentContext
;
20 import com
.sun
.star
.util
.XCloseable
;
22 public abstract class DocumentBasedExample
implements com
.sun
.star
.lang
.XEventListener
24 /// the initial remote context from the office
25 protected XComponentContext m_xCtx
;
26 /// our current test document
27 protected DocumentHelper m_document
;
28 protected FormLayer m_formLayer
;
29 private DocumentType m_documentType
;
31 /** Creates a new instance of DocumentBasedExample */
32 public DocumentBasedExample( DocumentType documentType
)
35 m_documentType
= documentType
;
38 /* ------------------------------------------------------------------ */
39 private void bootstrapUNO()
44 final XComponentContext componentContext = com.sun.star.comp.helper.Bootstrap.
45 createInitialComponentContext( null );
46 final XMultiComponentFactory localServiceManager = componentContext.getServiceManager();
48 final XUnoUrlResolver urlResolver = (XUnoUrlResolver) UnoRuntime.queryInterface(
49 XUnoUrlResolver.class, localServiceManager.createInstanceWithContext(
50 "com.sun.star.bridge.UnoUrlResolver", componentContext) );
52 final String connectStr = "uno:pipe,name=<pipename>;urp;StarOffice.ComponentContext";
53 final Object initialObject = urlResolver.resolve( connectStr );
55 m_xCtx = (XComponentContext)UnoRuntime.queryInterface( XComponentContext.class,
59 // get the remote office component context
60 m_xCtx
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
61 System
.out
.println("Connected to a running office ...");
63 catch (java
.lang
.Exception e
)
65 e
.printStackTrace( System
.err
);
70 /* ------------------------------------------------------------------ */
71 /** main method for running the sample
73 public void run( String argv
[] )
77 // collect whatever parameters were given
78 collectParameters( argv
);
80 // prepare our sample document
83 // switch the document view's form layer to alive mode
84 m_document
.getCurrentView().toggleFormDesignMode();
87 // grab the focus to the first control
88 m_document
.getCurrentView().grabControlFocus();
91 // wait for the user to confirm that we can exit
92 if ( waitForUserInput() )
98 // if waitForUserInput returns false, the user closed the document manually - no need to do a clean up
101 catch(com
.sun
.star
.uno
.Exception e
)
103 System
.out
.println(e
);
106 catch(java
.lang
.Exception e
)
108 System
.out
.println(e
);
115 /* ------------------------------------------------------------------ */
116 /** collect the RuntimeArguments
118 private void collectParameters(String argv
[])
120 // not interested in. Derived classes may want to use it.
123 /* ------------------------------------------------------------------ */
124 /** prepares a new document to work with
126 protected void prepareDocument() throws com
.sun
.star
.uno
.Exception
, java
.lang
.Exception
128 m_document
= DocumentHelper
.blankDocument(m_xCtx
, m_documentType
);
129 m_document
.getDocument( ).addEventListener( this );
130 m_formLayer
= new FormLayer( m_document
);
133 /* ------------------------------------------------------------------ */
134 /** called when the form layer has been switched to alive mode
136 protected void onFormsAlive()
140 /* ------------------------------------------------------------------ */
141 /** performs any cleanup before exiting the program
143 protected void cleanUp( ) throws java
.lang
.Exception
145 // do not listen at the document any longer
146 m_document
.getDocument().removeEventListener( this );
148 // close the document
152 /* ------------------------------------------------------------------ */
153 /** closes our document, if we have an open one
155 private void closeDocument()
159 // close our document
160 if ( m_document
!= null )
162 XCloseable closeDoc
= UnoRuntime
.queryInterface( XCloseable
.class,
163 m_document
.getDocument() );
164 if (closeDoc
!= null)
165 closeDoc
.close( true );
167 m_document
.getDocument().dispose();
170 catch ( com
.sun
.star
.uno
.Exception e
)
172 e
.printStackTrace( System
.err
);
176 /* ------------------------------------------------------------------ */
177 /* internal methods */
178 /* ------------------------------------------------------------------ */
179 /** waits for the user to press a key (on the console where she started
180 the java program) or the document to be closed by the user.
182 @return <TRUE/> if the user pressed a key on the console,
183 <FALSE/> if she closed the document
185 protected boolean waitForUserInput() throws java
.lang
.Exception
189 WaitForInput aWait
= new WaitForInput( this );
193 // if the waiter thread is done, the user pressed enter
194 boolean bKeyPressed
= aWait
.isDone();
202 /* ------------------------------------------------------------------ */
203 /* XEventListener overridables */
204 /* ------------------------------------------------------------------ */
205 public void disposing( com
.sun
.star
.lang
.EventObject eventObject
)
207 if ( m_document
.getDocument().equals( eventObject
.Source
) )
209 // notify ourself that we can stop waiting for user input