1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DocumentBasedExample.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package org
.openoffice
.sdk
.forms
;
32 import com
.sun
.star
.bridge
.XUnoUrlResolver
;
33 import com
.sun
.star
.lang
.XMultiComponentFactory
;
34 import com
.sun
.star
.uno
.UnoRuntime
;
35 import com
.sun
.star
.uno
.XComponentContext
;
36 import com
.sun
.star
.util
.XCloseable
;
38 public abstract class DocumentBasedExample
implements com
.sun
.star
.lang
.XEventListener
40 /// the intial remote context from the office
41 protected XComponentContext m_xCtx
;
42 /// our current test document
43 protected DocumentHelper m_document
;
44 protected FormLayer m_formLayer
;
45 protected DocumentType m_documentType
;
47 /** Creates a new instance of DocumentBasedExample */
48 public DocumentBasedExample( DocumentType documentType
)
51 m_documentType
= documentType
;
54 /* ------------------------------------------------------------------ */
55 private void bootstrapUNO()
60 final XComponentContext componentContext = com.sun.star.comp.helper.Bootstrap.
61 createInitialComponentContext( null );
62 final XMultiComponentFactory localServiceManager = componentContext.getServiceManager();
64 final XUnoUrlResolver urlResolver = (XUnoUrlResolver) UnoRuntime.queryInterface(
65 XUnoUrlResolver.class, localServiceManager.createInstanceWithContext(
66 "com.sun.star.bridge.UnoUrlResolver", componentContext) );
68 final String connectStr = "uno:pipe,name=<pipename>;urp;StarOffice.ComponentContext";
69 final Object initialObject = urlResolver.resolve( connectStr );
71 m_xCtx = (XComponentContext)UnoRuntime.queryInterface( XComponentContext.class,
75 // get the remote office component context
76 m_xCtx
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
77 System
.out
.println("Connected to a running office ...");
79 catch (java
.lang
.Exception e
)
81 e
.printStackTrace( System
.err
);
86 /* ------------------------------------------------------------------ */
87 /** main method for running the sample
89 public void run( String argv
[] )
93 // collect whatever parameters were given
94 collectParameters( argv
);
96 // prepare our sample document
99 // switch the document view's form layer to alive mode
100 m_document
.getCurrentView().toggleFormDesignMode();
103 // grab the focus to the first control
104 m_document
.getCurrentView().grabControlFocus();
106 // ----------------------------------------------
107 // wait for the user to confirm that we can exit
108 if ( waitForUserInput() )
114 // if waitForUserInput returns false, the user closed the document manually - no need to do a clean up
117 catch(com
.sun
.star
.uno
.Exception e
)
119 System
.out
.println(e
);
122 catch(java
.lang
.Exception e
)
124 System
.out
.println(e
);
131 /* ------------------------------------------------------------------ */
132 /** collect the RuntimeArguments
134 protected void collectParameters(String argv
[])
136 // not interested in. Derived classes may want to use it.
139 /* ------------------------------------------------------------------ */
140 /** prepares a new document to work with
142 protected void prepareDocument() throws com
.sun
.star
.uno
.Exception
, java
.lang
.Exception
144 m_document
= DocumentHelper
.blankDocument(m_xCtx
, m_documentType
);
145 m_document
.getDocument( ).addEventListener( this );
146 m_formLayer
= new FormLayer( m_document
);
149 /* ------------------------------------------------------------------ */
150 /** called when the form layer has been switched to alive mode
152 protected void onFormsAlive()
156 /* ------------------------------------------------------------------ */
157 /** performs any cleanup before exiting the program
159 protected void cleanUp( ) throws java
.lang
.Exception
161 // do not listen at the document any longer
162 m_document
.getDocument().removeEventListener( this );
164 // close the document
168 /* ------------------------------------------------------------------ */
169 /** closes our document, if we have an open one
171 private void closeDocument()
175 // close our document
176 if ( m_document
!= null )
178 XCloseable closeDoc
= (XCloseable
)
179 UnoRuntime
.queryInterface( XCloseable
.class,
180 m_document
.getDocument() );
181 if (closeDoc
!= null)
182 closeDoc
.close( true );
184 m_document
.getDocument().dispose();
187 catch ( com
.sun
.star
.uno
.Exception e
)
189 e
.printStackTrace( System
.out
);
193 /* ------------------------------------------------------------------ */
194 /* internal methods */
195 /* ------------------------------------------------------------------ */
196 /** waits for the user to press a key (on the console where she started
197 the java program) or the document to be closed by the user.
199 @return <TRUE/> if the user pressed a key on the console,
200 <FALSE/> if she closed the document
202 protected boolean waitForUserInput() throws java
.lang
.Exception
206 WaitForInput aWait
= new WaitForInput( this );
210 // if the waiter thread is done, the user pressed enter
211 boolean bKeyPressed
= aWait
.isDone();
219 /* ------------------------------------------------------------------ */
220 /* XEventListener overridables */
221 /* ------------------------------------------------------------------ */
222 public void disposing( com
.sun
.star
.lang
.EventObject eventObject
)
224 if ( m_document
.getDocument().equals( eventObject
.Source
) )
226 // notify ourself that we can stop waiting for user input