Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / odk / examples / DevelopersGuide / Forms / DocumentBasedExample.java
blob34735221f5424fbb8c2d3cbfd9b7dc16ffbff9ac
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 import com.sun.star.uno.UnoRuntime;
20 import com.sun.star.uno.XComponentContext;
21 import com.sun.star.util.XCloseable;
23 public abstract class DocumentBasedExample implements com.sun.star.lang.XEventListener
25 /// the initial remote context from the office
26 protected XComponentContext m_xCtx;
27 /// our current test document
28 protected DocumentHelper m_document;
29 protected FormLayer m_formLayer;
30 private DocumentType m_documentType;
32 /** Creates a new instance of DocumentBasedExample */
33 public DocumentBasedExample( DocumentType documentType )
35 bootstrapUNO();
36 m_documentType = documentType;
39 /* ------------------------------------------------------------------ */
40 private void bootstrapUNO()
42 try
45 final XComponentContext componentContext = com.sun.star.comp.helper.Bootstrap.
46 createInitialComponentContext( null );
47 final XMultiComponentFactory localServiceManager = componentContext.getServiceManager();
49 final XUnoUrlResolver urlResolver = (XUnoUrlResolver) UnoRuntime.queryInterface(
50 XUnoUrlResolver.class, localServiceManager.createInstanceWithContext(
51 "com.sun.star.bridge.UnoUrlResolver", componentContext) );
53 final String connectStr = "uno:pipe,name=<pipename>;urp;StarOffice.ComponentContext";
54 final Object initialObject = urlResolver.resolve( connectStr );
56 m_xCtx = (XComponentContext)UnoRuntime.queryInterface( XComponentContext.class,
57 initialObject );
60 // get the remote office component context
61 m_xCtx = com.sun.star.comp.helper.Bootstrap.bootstrap();
62 System.out.println("Connected to a running office ...");
64 catch (java.lang.Exception e)
66 e.printStackTrace( System.err );
67 System.exit(1);
71 /* ------------------------------------------------------------------ */
72 /** main method for running the sample
74 public void run( String argv[] )
76 try
78 // collect whatever parameters were given
79 collectParameters( argv );
81 // prepare our sample document
82 prepareDocument();
84 // switch the document view's form layer to alive mode
85 m_document.getCurrentView().toggleFormDesignMode();
86 onFormsAlive();
88 // grab the focus to the first control
89 m_document.getCurrentView().grabControlFocus();
92 // wait for the user to confirm that we can exit
93 if ( waitForUserInput() )
95 // clean up
96 cleanUp();
99 // if waitForUserInput returns false, the user closed the document manually - no need to do a clean up
100 // then
102 catch(com.sun.star.uno.Exception e)
104 System.out.println(e);
105 e.printStackTrace();
107 catch(java.lang.Exception e)
109 System.out.println(e);
110 e.printStackTrace();
113 System.exit(0);
116 /* ------------------------------------------------------------------ */
117 /** collect the RuntimeArguments
119 private void collectParameters(String argv[])
121 // not interested in. Derived classes may want to use it.
124 /* ------------------------------------------------------------------ */
125 /** prepares a new document to work with
127 protected void prepareDocument() throws com.sun.star.uno.Exception, java.lang.Exception
129 m_document = DocumentHelper.blankDocument(m_xCtx, m_documentType);
130 m_document.getDocument( ).addEventListener( this );
131 m_formLayer = new FormLayer( m_document );
134 /* ------------------------------------------------------------------ */
135 /** called when the form layer has been switched to alive mode
137 protected void onFormsAlive()
141 /* ------------------------------------------------------------------ */
142 /** performs any cleanup before exiting the program
144 protected void cleanUp( ) throws java.lang.Exception
146 // do not listen at the document any longer
147 m_document.getDocument().removeEventListener( this );
149 // close the document
150 closeDocument();
153 /* ------------------------------------------------------------------ */
154 /** closes our document, if we have an open one
156 private void closeDocument()
160 // close our document
161 if ( m_document != null )
163 XCloseable closeDoc = UnoRuntime.queryInterface( XCloseable.class,
164 m_document.getDocument() );
165 if (closeDoc != null)
166 closeDoc.close( true );
167 else
168 m_document.getDocument().dispose();
171 catch ( com.sun.star.uno.Exception e )
173 e.printStackTrace( System.err );
177 /* ------------------------------------------------------------------ */
178 /* internal methods */
179 /* ------------------------------------------------------------------ */
180 /** waits for the user to press a key (on the console where she started
181 the java program) or the document to be closed by the user.
183 @return <TRUE/> if the user pressed a key on the console,
184 <FALSE/> if she closed the document
186 protected boolean waitForUserInput() throws java.lang.Exception
188 synchronized (this)
190 WaitForInput aWait = new WaitForInput( this );
191 aWait.start();
192 wait();
194 // if the waiter thread is done, the user pressed enter
195 boolean bKeyPressed = aWait.isDone();
196 if ( !bKeyPressed )
197 aWait.interrupt();
199 return bKeyPressed;
203 /* ------------------------------------------------------------------ */
204 /* XEventListener overridables */
205 /* ------------------------------------------------------------------ */
206 public void disposing( com.sun.star.lang.EventObject eventObject )
208 if ( m_document.getDocument().equals( eventObject.Source ) )
210 // notify ourself that we can stop waiting for user input
211 synchronized (this)
213 notify();
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */