1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 /* 3 * This file is part of the LibreOffice project. 4 * 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/. 8 * 9 * This file incorporates work covered by the following license notice: 10 * 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 . 18 */ 19 import com.sun.star.uno.UnoRuntime; 20 import com.sun.star.uno.XComponentContext; 21 import com.sun.star.util.XCloseable; 22 23 public abstract class DocumentBasedExample implements com.sun.star.lang.XEventListener 24 { 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; 31 32 /** Creates a new instance of DocumentBasedExample */ DocumentBasedExample( DocumentType documentType )33 public DocumentBasedExample( DocumentType documentType ) 34 { 35 bootstrapUNO(); 36 m_documentType = documentType; 37 } 38 39 /* ------------------------------------------------------------------ */ bootstrapUNO()40 private void bootstrapUNO() 41 { 42 try 43 { 44 /* 45 final XComponentContext componentContext = com.sun.star.comp.helper.Bootstrap. 46 createInitialComponentContext( null ); 47 final XMultiComponentFactory localServiceManager = componentContext.getServiceManager(); 48 49 final XUnoUrlResolver urlResolver = (XUnoUrlResolver) UnoRuntime.queryInterface( 50 XUnoUrlResolver.class, localServiceManager.createInstanceWithContext( 51 "com.sun.star.bridge.UnoUrlResolver", componentContext) ); 52 53 final String connectStr = "uno:pipe,name=<pipename>;urp;StarOffice.ComponentContext"; 54 final Object initialObject = urlResolver.resolve( connectStr ); 55 56 m_xCtx = (XComponentContext)UnoRuntime.queryInterface( XComponentContext.class, 57 initialObject ); 58 */ 59 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 ..."); 63 } 64 catch (java.lang.Exception e) 65 { 66 e.printStackTrace( System.err ); 67 System.exit(1); 68 } 69 } 70 71 /* ------------------------------------------------------------------ */ 72 /** main method for running the sample 73 */ run( String argv[] )74 public void run( String argv[] ) 75 { 76 try 77 { 78 // collect whatever parameters were given 79 collectParameters( argv ); 80 81 // prepare our sample document 82 prepareDocument(); 83 84 // switch the document view's form layer to alive mode 85 m_document.getCurrentView().toggleFormDesignMode(); 86 onFormsAlive(); 87 88 // grab the focus to the first control 89 m_document.getCurrentView().grabControlFocus(); 90 91 92 // wait for the user to confirm that we can exit 93 if ( waitForUserInput() ) 94 { 95 // clean up 96 cleanUp(); 97 } 98 99 // if waitForUserInput returns false, the user closed the document manually - no need to do a clean up 100 // then 101 } 102 catch(com.sun.star.uno.Exception e) 103 { 104 System.out.println(e); 105 e.printStackTrace(); 106 } 107 catch(java.lang.Exception e) 108 { 109 System.out.println(e); 110 e.printStackTrace(); 111 } 112 113 System.exit(0); 114 } 115 116 /* ------------------------------------------------------------------ */ 117 /** collect the RuntimeArguments 118 */ collectParameters(String argv[])119 private void collectParameters(String argv[]) 120 { 121 // not interested in. Derived classes may want to use it. 122 } 123 124 /* ------------------------------------------------------------------ */ 125 /** prepares a new document to work with 126 */ prepareDocument()127 protected void prepareDocument() throws com.sun.star.uno.Exception, java.lang.Exception 128 { 129 m_document = DocumentHelper.blankDocument(m_xCtx, m_documentType); 130 m_document.getDocument( ).addEventListener( this ); 131 m_formLayer = new FormLayer( m_document ); 132 } 133 134 /* ------------------------------------------------------------------ */ 135 /** called when the form layer has been switched to alive mode 136 */ onFormsAlive()137 protected void onFormsAlive() 138 { 139 } 140 141 /* ------------------------------------------------------------------ */ 142 /** performs any cleanup before exiting the program 143 */ cleanUp( )144 protected void cleanUp( ) throws java.lang.Exception 145 { 146 // do not listen at the document any longer 147 m_document.getDocument().removeEventListener( this ); 148 149 // close the document 150 closeDocument(); 151 } 152 153 /* ------------------------------------------------------------------ */ 154 /** closes our document, if we have an open one 155 */ closeDocument()156 private void closeDocument() 157 { 158 try 159 { 160 // close our document 161 if ( m_document != null ) 162 { 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(); 169 } 170 } 171 catch ( com.sun.star.uno.Exception e ) 172 { 173 e.printStackTrace( System.err ); 174 } 175 } 176 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. 182 183 @return <TRUE/> if the user pressed a key on the console, 184 <FALSE/> if she closed the document 185 */ waitForUserInput()186 protected boolean waitForUserInput() throws java.lang.Exception 187 { 188 synchronized (this) 189 { 190 WaitForInput aWait = new WaitForInput( this ); 191 aWait.start(); 192 wait(); 193 194 // if the waiter thread is done, the user pressed enter 195 boolean bKeyPressed = aWait.isDone(); 196 if ( !bKeyPressed ) 197 aWait.interrupt(); 198 199 return bKeyPressed; 200 } 201 } 202 203 /* ------------------------------------------------------------------ */ 204 /* XEventListener overridables */ 205 /* ------------------------------------------------------------------ */ disposing( com.sun.star.lang.EventObject eventObject )206 public void disposing( com.sun.star.lang.EventObject eventObject ) 207 { 208 if ( m_document.getDocument().equals( eventObject.Source ) ) 209 { 210 // notify ourself that we can stop waiting for user input 211 synchronized (this) 212 { 213 notify(); 214 } 215 } 216 } 217 } 218 219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 220