merge the formfield patch from ooo-build
[ooovba.git] / bean / com / sun / star / comp / beans / OOoBean.java
blob53c6405b77f1370f89ea5e0cae3069b049e212a1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: OOoBean.java,v $
10 * $Revision: 1.15 $
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 ************************************************************************/
31 package com.sun.star.comp.beans;
33 import com.sun.star.uno.UnoRuntime;
35 // @requirement FUNC.PERF.LRN/0.6
36 // @requirement FUNC.PERF.LOC/0.6
37 // @requirement FUNC.PERF.FIX/0.6
38 /** This is the basic JavaBean for all OOo application modules.
40 @requirement FUNC.RES.OTH/0.2
41 No other resources are needed yet.
43 @since OOo 2.0.0
45 public class OOoBean
47 // @requirement FUNC.BEAN.VIEW/0.4
48 extends java.awt.Container
50 implements
51 // @requirement FUNC.PER/0.2
52 java.io.Externalizable
54 // timeout values (milli secs)
55 int nOOoStartTimeOut = 60000;
56 int nOOoCallTimeOut = 3000;
57 int nOOoCheckCycle = 1000;
59 // This member contains the connection to an OOo instance if established.
60 private transient OfficeConnection iConnection;
61 private transient EventListener xConnectionListener;
63 // @requirement FUNC.BEAN.VIEW/0.4
64 // @requirement FUNC.BEAN.EDIT/0.4
65 // This member contains the OOo window
66 // if a connection is established.
67 // It is a child of the OOoBean canvas.
68 private OfficeWindow xFrameWindow;
70 // application environment
71 private transient com.sun.star.lang.XMultiServiceFactory xServiceFactory;
72 private transient com.sun.star.frame.XDesktop xDesktop;
74 // document and frame
75 private transient Frame aFrame;
76 private transient Controller aController;
77 private transient OfficeDocument aDocument;
79 // slot command execution environment
80 private transient com.sun.star.frame.XDispatchProvider xDispatcher;
81 private transient com.sun.star.util.XURLTransformer xURLTransformer;
83 // properties
84 private boolean bIgnoreVisibility = false; // to show even if already visible
85 private boolean bMenuBarVisible = true;
86 private boolean bStandardBarVisible = true;
87 private boolean bToolBarVisible = true;
88 private boolean bStatusBarVisible = true;
91 // debugging method
92 private void dbgPrint( String aMessage )
94 // System.err.println( "OOoBean: " + aMessage );
97 // @requirement FUNC.PER/0.2
98 /** @internal
99 * @deprecated
101 public void writeExternal( java.io.ObjectOutput aObjOut )
103 // TBD
106 // @requirement FUNC.PER/0.2
107 /** @internal
108 * @deprecated
110 public void readExternal( java.io.ObjectInput aObjIn )
112 // TBD
115 /** Generic constructor of the OOoBean.
117 Neither a connection is established nor any document loaded.
119 public OOoBean()
122 // @requirement FUNC.CON.MULT/0.3
123 /** Constructor for an OOoBean which uses a specific office connection.
125 The connection must be established but no document is loaded.
127 @throws NoConnectionException
128 if the connection is not established.
130 @deprecated Clients could use the getOOoConnection to obtain an OfficeConnection
131 and use it as argument in a constructor for another OOoBean instance. Calling
132 the dispose method of the OfficeConnection or the OOoBean's stopOOoConnection
133 method would make all instances of OOoBean stop working.
135 public OOoBean( OfficeConnection iConnection )
136 throws NoConnectionException
138 try { setOOoConnection( iConnection ); }
139 catch ( HasConnectionException aExc )
140 { /* impossible here */ }
143 /** Sets the timeout for methods which launch OOo in milli seconds.
145 This method does not need a connection to an OOo instance.
147 public void setOOoStartTimeOut( int nMilliSecs )
149 nOOoStartTimeOut = nMilliSecs;
152 /** Sets the timeout for normal OOO methods calls in milli seconds.
154 This method does not need a connection to an OOo instance.
156 public void setOOoCallTimeOut( int nMilliSecs )
158 nOOoCallTimeOut = nMilliSecs;
161 /** Sets the period length in milli seconds to check the OOo connection.
163 This method does not need a connection to an OOo instance.
165 public void setOOoCheckCycle( int nMilliSecs )
167 nOOoCheckCycle = nMilliSecs;
170 /** Sets the a connection to an OOo instance.
172 @internal
174 private synchronized void setOOoConnection( OfficeConnection iNewConnection )
175 throws HasConnectionException, NoConnectionException
177 // the connection cannot be exchanged
178 if ( iConnection != null )
179 throw new HasConnectionException();
181 // is there a real connection, not just the proxy?
182 com.sun.star.uno.XComponentContext xComponentContext = null;
183 try { xComponentContext = iNewConnection.getComponentContext(); }
184 catch ( java.lang.Throwable aExc )
185 { throw new NoConnectionException(); }
186 if ( xComponentContext == null )
187 throw new NoConnectionException();
189 // set the connection
190 iConnection = iNewConnection;
192 // get notified when connection dies
193 if ( xConnectionListener != null )
194 xConnectionListener.end();
195 xConnectionListener = this.new EventListener("setOOoConnection");
198 // @requirement FUNC.CON.STRT/0.4
199 /** Starts a connection to an OOo instance which is lauched if not running.
201 @throws HasConnectionException
202 if a connection was already established.
204 @throws NoConnectionException
205 if the specified connection cannot be established
207 public void startOOoConnection( String aConnectionURL )
208 throws java.net.MalformedURLException,
209 HasConnectionException,
210 NoConnectionException
212 // create a new connection from the given connection URL
213 LocalOfficeConnection aConnection = new LocalOfficeConnection();
214 aConnection.setUnoUrl( aConnectionURL );
215 setOOoConnection( aConnection );
218 // @requirement FUNC.CON.CHK/0.7
219 /** Returns true if this OOoBean is connected to an OOo instance,
220 false otherwise.
222 @deprecated This method is not useful in a multithreaded environment. Then
223 all threads accessing the instance would have to be synchronized in order to
224 make is method work. It is better
225 to call OOoBean's methods and be prepared to catch a NoConnectionException.
227 public boolean isOOoConnected()
229 return iConnection != null;
232 // @requirement FUNC.CON.STOP/0.4
233 /** Disconnects from the connected OOo instance.
235 If there was no connection yet or anymore, this method can be called
236 anyway.
238 When the OOoBean is displayed in an applet by a web browser, then this
239 method must be called from within java.applet.Applet.stop.
241 public synchronized void stopOOoConnection()
243 // clear OOo document, frame etc.
244 clear();
246 // cut the connection
247 OfficeConnection iExConnection = iConnection;
248 if ( iConnection != null )
250 if ( xConnectionListener != null )
252 xConnectionListener.end();
254 iConnection = null;
255 iExConnection.dispose();
260 // @requirement FUNC.CON.STOP/0.4 (via XComponent.dispose())
261 // @requirement FUNC.CON.NTFY/0.4 (via XComponent.addEventListener())
262 /** Returns the a connection to an OOo instance.
264 If no connection exists, a default connection will be created. An OfficeConnection
265 can be used to register listeners of type com.sun.star.lang.EventListener,
266 which are notified when the connection to the
267 office dies. One should not call the dispose method, because this may result
268 in receiving com.sun.star.lang.DisposedExceptions when calling
269 {@link #stopOOoConnection stopOOoConnection} or other API methods. If other instances share the
270 same connection then they will stop function properly, because they loose their
271 connection as well. The recommended way to end the connection is
272 calling {@link #stopOOoConnection stopOOoConnection}.
274 @throws NoConnectionException
275 if no connection can be established
278 public synchronized OfficeConnection getOOoConnection()
279 throws NoConnectionException
281 if ( iConnection == null )
283 try { setOOoConnection( new LocalOfficeConnection() ); }
284 catch ( HasConnectionException aExc )
285 { /* impossible here */ }
287 if ( iConnection.getComponentContext() == null )
288 throw new NoConnectionException();
289 return iConnection;
292 /** Returns the service factory used by this OOoBean instance.
294 @throws NoConnectionException
295 if no connection is established and no default connection can be established.
297 public synchronized com.sun.star.lang.XMultiServiceFactory getMultiServiceFactory()
298 throws NoConnectionException
300 if ( xServiceFactory == null )
302 // avoid concurrent access from multiple threads
303 final OfficeConnection iConn = getOOoConnection();
305 Thread aConnectorThread = new Thread() {
306 public void run()
308 com.sun.star.lang.XMultiComponentFactory aFactory =
309 iConn.getComponentContext().getServiceManager();
310 xServiceFactory = (com.sun.star.lang.XMultiServiceFactory)
311 UnoRuntime.queryInterface(
312 com.sun.star.lang.XMultiServiceFactory.class, aFactory );
315 aConnectorThread.start();
316 try { aConnectorThread.join(nOOoStartTimeOut); }
317 catch ( java.lang.InterruptedException aExc )
318 { throw new NoConnectionException(); }
319 if ( xServiceFactory == null )
320 throw new NoConnectionException();
323 return xServiceFactory;
326 /** Returns the XDesktop interface of the OOo instance used by this OOoBean.
328 @throws NoConnectionException
329 if no connection is established and no default connection can be established.
331 public synchronized com.sun.star.frame.XDesktop getOOoDesktop()
332 throws NoConnectionException
334 if ( xDesktop == null )
338 Object aObject = getMultiServiceFactory().createInstance( "com.sun.star.frame.Desktop");
339 xDesktop = (com.sun.star.frame.XDesktop) UnoRuntime.queryInterface(
340 com.sun.star.frame.XDesktop.class, aObject );
342 catch ( com.sun.star.uno.Exception aExc )
343 {} // TBD: what if no connection exists?
346 return xDesktop;
349 /** Resets this bean to an empty document.
351 If a document is loaded and the content modified,
352 the changes are dismissed. Otherwise nothing happens.
354 This method is intended to be overridden in derived classes.
355 This implementation simply calls clear.
357 @param bClearStateToo
358 Not only the document content but also the state of the bean,
359 like visibility of child components is cleared.
361 @deprecated There is currently no way to dismiss changes, except for loading
362 of the unchanged initial document. Furthermore it is unclear how derived classes
363 handle this and what exactly their state is (e.g. what members make up their state).
364 Calling this method on a derived class requires knowledge about their implementation.
365 Therefore a deriving class should declare their own clearDocument if needed. Clients
366 should call the clearDocument of the deriving class or {@link #clear} which discards
367 the currently displayed document.
369 public synchronized void clearDocument( boolean bClearStateToo )
370 throws
371 com.sun.star.util.CloseVetoException,
372 NoConnectionException
374 // TBD
375 clear();
378 /** Resets the OOoBean to an empty status.
380 Any loaded document is unloaded, no matter whether it is modified or not.
381 After calling this method, the OOoBean has no office document and no frame
382 anymore. The connection will stay, though.
384 This method works with or without an established connection.
386 public synchronized void clear()
388 dbgPrint( "clear()" );
392 CallWatchThread aCallWatchThread =
393 new CallWatchThread( nOOoCallTimeOut, "clear" );
394 //By closing the frame we avoid that dialogs are displayed, for example when
395 //the document is modified.
396 com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable)
397 UnoRuntime.queryInterface( com.sun.star.util.XCloseable.class, aFrame );
398 if ( xCloseable != null )
400 try
402 xCloseable.close(true);
404 catch (com.sun.star.util.CloseVetoException exc)
405 { // a print job may be running
409 aDocument = null;
410 xDispatcher = null;
411 aFrame = null;
413 // clear xFrameWindow
414 if ( xFrameWindow != null )
416 try { releaseSystemWindow(); }
417 catch ( NoConnectionException aExc )
418 {} // ignore
419 catch ( SystemWindowException aExc )
420 {} // ignore
421 remove( xFrameWindow.getAWTComponent() );
422 xFrameWindow = null;
425 // clear xURTTransformer
426 if ( xURLTransformer != null )
430 com.sun.star.lang.XComponent xComp = (com.sun.star.lang.XComponent)
431 UnoRuntime.queryInterface(
432 com.sun.star.lang.XComponent.class, xURLTransformer );
433 if ( xComp != null )
434 xComp.dispose();
436 catch ( java.lang.Throwable aExc )
437 {} // ignore
438 xURLTransformer = null;
441 xDesktop = null;
442 xServiceFactory = null;
444 aCallWatchThread.cancel();
446 catch ( java.lang.InterruptedException aExc )
447 { /* can be ignored */ }
450 // @requirement FUNC.PAR.LWP/0.4
451 /** This method causes the office window to be displayed.
453 If no document is loaded and the instance is added to a Java container that
454 is showing, then this method needs not to be called. If later one of the methods
455 {@link #loadFromURL loadFromURL}, {@link #loadFromStream loadFromStream1},
456 or {@link #loadFromByteArray loadFromByteArray}
457 is called, then the document is automatically displayed.
459 Should one of the load methods have been called before the Java container
460 was showing, then this method needs to be called after the container window
461 was made visible (java.lang.Component.setVisible(true)).
463 Another scenario is that a OOoBean contains a document and is removed
464 from a Java container and later added again. Then aquireSystemWindow needs
465 to be called after the container window is displayed.
467 @throws SystemWindowException
468 if no system window can be aquired.
470 @throws NoConnectionException
471 if the connection is not established.
473 public synchronized void aquireSystemWindow()
474 throws
475 SystemWindowException,
477 // @requirement FUNC.CON.LOST/0.2
478 NoConnectionException
480 if ( iConnection == null )
481 throw new NoConnectionException();
482 if ( !isShowing() )
483 throw new SystemWindowException();
485 if ( xFrameWindow != null )
486 xFrameWindow.getAWTComponent().setVisible(true);
487 doLayout();
490 // @requirement FUNC.PAR.RWL/0.4
491 // @estimation 16h
492 /** This method must be called when the OOoBean before the
493 sytem window may be released by it's parent AWT/Swing component.
495 This is the case when java.awt.Component.isDisplayable() returns
496 true. This is definitely the case when the OOoBean is removed
497 from it's parent container.
499 @throws SystemWindowException
500 if system window is not aquired.
502 @throws NoConnectionException
503 if the connection is not established.
505 @deprecated When Component.removeNotify of the parent window of the actual
506 office window is called, then the actions are performed for which this method
507 needed to be called previously.
509 public synchronized void releaseSystemWindow()
510 throws
511 SystemWindowException,
513 // @requirement FUNC.CON.LOST/0.2
514 NoConnectionException
516 if ( iConnection == null )
517 throw new NoConnectionException();
519 try { xFrameWindow.getAWTComponent().setVisible(false); }
520 catch ( com.sun.star.lang.DisposedException aExc )
521 { throw new NoConnectionException(); }
524 // @requirement FUNC.BEAN.LOAD/0.4
525 // @requirement FUNC.CON.AUTO/0.3
526 /** Loads the bean from the given URL.
528 If a document is already loaded and the content modified,
529 the changes are dismissed.
531 If no connection exists, a default connection is established.
533 @throws IllegalArgumentException
534 if either of the arguments is out of the specified range.
536 @throws java.io.IOException
537 if an IO error occurs reading the ressource specified by the URL.
539 @throws com.sun.star.lang.NoConnectionException
540 if no connection can be established.
542 @throws com.sun.star.util.CloseVetoException
543 if the currently displayed document cannot be closed because it is
544 still be used, for example it is printed.
546 public void loadFromURL(
547 final String aURL,
548 final com.sun.star.beans.PropertyValue aArguments[] )
549 throws
550 // @requirement FUNC.CON.LOST/0.2
551 NoConnectionException,
552 java.io.IOException,
553 com.sun.star.lang.IllegalArgumentException,
554 com.sun.star.util.CloseVetoException
556 dbgPrint( "loadFromURL()" );
557 // try loading
558 try
560 boolean bLoaded = false;
561 while ( !bLoaded )
563 // watch loading in a thread with a timeout (if OOo hangs)
564 CallWatchThread aCallWatchThread =
565 new CallWatchThread( nOOoStartTimeOut, "loadFromURL" );
569 // get window from OOo on demand
570 if ( xFrameWindow == null )
572 // Establish the connection by request of the ServiceFactory.
573 getMultiServiceFactory();
575 // remove existing child windows
576 removeAll();
578 // Create the OfficeWindow.
579 xFrameWindow = getOOoConnection().createOfficeWindow(OOoBean.this);
580 add( xFrameWindow.getAWTComponent() );
583 // create the document frame from UNO window.
584 if ( aFrame == null )
586 // create the frame
587 com.sun.star.awt.XWindow xWindow =
588 (com.sun.star.awt.XWindow) UnoRuntime.queryInterface(
589 com.sun.star.awt.XWindow.class, xFrameWindow.getUNOWindowPeer());
590 Object xFrame = xServiceFactory.createInstance( "com.sun.star.frame.Frame");
591 aFrame = new Frame( (com.sun.star.frame.XFrame)UnoRuntime.queryInterface(
592 com.sun.star.frame.XFrame.class, xFrame ) );
593 aFrame.initialize( xWindow );
594 aFrame.setName( aFrame.toString() );
596 // register the frame at the desktop
597 com.sun.star.frame.XFrames xFrames =
598 ( (com.sun.star.frame.XFramesSupplier)UnoRuntime.queryInterface(
599 com.sun.star.frame.XFramesSupplier.class, getOOoDesktop() ) ).getFrames();
600 xFrames.append( aFrame );
603 // Initializes the slot command execution environment.
604 xURLTransformer = (com.sun.star.util.XURLTransformer) UnoRuntime.queryInterface(
605 com.sun.star.util.XURLTransformer.class,
606 xServiceFactory.createInstance( "com.sun.star.util.URLTransformer") );
607 xDispatcher = (com.sun.star.frame.XDispatchProvider)UnoRuntime.queryInterface(
608 com.sun.star.frame.XDispatchProvider.class, aFrame );
610 // get XComponentLoader from frame
611 com.sun.star.frame.XComponentLoader xLoader = (com.sun.star.frame.XComponentLoader)
612 UnoRuntime.queryInterface( com.sun.star.frame.XComponentLoader.class, aFrame );
613 if ( xLoader == null )
615 throw new java.lang.RuntimeException(
616 "com.sun.star.frame.Frame(" + aFrame +
617 ") without com.sun.star.frame.XComponentLoader" );
620 // Avoid Dialog 'Document changed' while reloading
621 if ( aDocument != null )
623 try {
624 aDocument.setModified(false);
625 } catch (com.sun.star.beans.PropertyVetoException ep) {
626 //it dosn't make sense to throw the exception here. The interface does not
627 //offer a way to add/remove respective listeners.
628 } catch (com.sun.star.lang.DisposedException ed) {
629 // can be disposed if user closed document via UI
632 com.sun.star.frame.XController xOldController = null;
633 if ( aFrame != null )
634 xOldController = aFrame.getController();
636 try
639 if ( aFrame != null && xOldController != null )
640 if (xOldController.suspend(true) == false)
641 throw new com.sun.star.util.CloseVetoException(
642 "Dokument is still being used and cannot be closed.", this);
645 catch (java.lang.IllegalStateException exp)
649 // load the document.
650 com.sun.star.beans.PropertyValue aArgs[] =
651 addArgument( aArguments, new com.sun.star.beans.PropertyValue(
652 "MacroExecutionMode", -1,
653 new Short( com.sun.star.document.MacroExecMode.USE_CONFIG ),
654 com.sun.star.beans.PropertyState.DIRECT_VALUE ) );
655 //String fn = aFRame.getName();
657 com.sun.star.lang.XComponent xComponent = xLoader.loadComponentFromURL(
658 aURL, /*aFrame.getName()*/"_self", 0, aArgs );
660 // nothing loaded?
661 if ( xComponent == null && aDocument != null )
663 // reactivate old document
664 if ( aFrame != null && aFrame.getController() != null )
665 aFrame.getController().suspend(false);
666 aDocument.setModified(true);
668 // throw exception
669 throw new java.io.IOException(
670 "Can not load a document: \"" + aURL + "\"");
672 // mDocumentURL = aURL; TBD: still needed?
674 // Get document's XModifiable interface if any.
675 aDocument = new OfficeDocument(
676 (com.sun.star.frame.XModel) UnoRuntime.queryInterface(
677 com.sun.star.frame.XModel.class, xComponent ) );
678 bLoaded = true;
680 catch ( NoConnectionException aExc )
682 // stop, clear and retry
683 stopOOoConnection();
685 catch ( com.sun.star.lang.DisposedException aExc )
687 // stop, clear and retry
688 stopOOoConnection();
690 catch ( com.sun.star.uno.Exception aExc )
692 // TDB: handling failure in createInstance
693 aExc.printStackTrace();
694 throw new java.io.IOException();
697 aCallWatchThread.cancel();
698 if ( xServiceFactory == null )
699 throw new NoConnectionException();
701 if ( iConnection == null )
703 throw new NoConnectionException();
706 applyToolVisibilities();
708 catch ( java.lang.InterruptedException aExc )
710 throw new NoConnectionException();
714 /** Loads a document from a Java stream.
716 See loadFromURL() for further information.
718 public void loadFromStream(
719 final java.io.InputStream iInStream,
720 final com.sun.star.beans.PropertyValue aArguments[] )
721 throws
722 // @requirement FUNC.CON.LOST/0.2
723 NoConnectionException,
724 java.io.IOException,
725 com.sun.star.lang.IllegalArgumentException,
726 com.sun.star.util.CloseVetoException
728 // wrap Java stream into UNO stream
730 com.sun.star.io.XInputStream xStream =
731 new com.sun.star.lib.uno.adapter.InputStreamToXInputStreamAdapter(
732 iInStream );
734 // copy stream....
736 int s = 4096;
737 int r=0 ,n = 0;
738 byte[] buffer = new byte[s];
739 byte[] newBuffer = null;
740 while ((r = iInStream.read(buffer, n, buffer.length-n))>0) {
741 n += r;
742 if (iInStream.available() > buffer.length - n) {
743 newBuffer = new byte[buffer.length*2];
744 System.arraycopy(buffer, 0, newBuffer, 0, n);
745 buffer = newBuffer;
748 if (buffer.length != n) {
749 newBuffer = new byte[n];
750 System.arraycopy(buffer, 0, newBuffer, 0, n);
751 buffer = newBuffer;
753 com.sun.star.io.XInputStream xStream =
754 new com.sun.star.lib.uno.adapter.ByteArrayToXInputStreamAdapter(buffer);
756 // add stream to arguments
757 com.sun.star.beans.PropertyValue[] aExtendedArguments =
758 addArgument( aArguments, new com.sun.star.beans.PropertyValue(
759 "InputStream", -1, xStream, com.sun.star.beans.PropertyState.DIRECT_VALUE ) );
761 // call normal load method
762 loadFromURL( "private:stream", aExtendedArguments );
765 /** Loads a document from a byte array.
767 See loadFromURL() for further information.
769 public void loadFromByteArray(
770 final byte aInBuffer[],
771 final com.sun.star.beans.PropertyValue aArguments[] )
772 throws
773 // @requirement FUNC.CON.LOST/0.2
774 NoConnectionException,
775 java.io.IOException,
776 com.sun.star.lang.IllegalArgumentException,
777 com.sun.star.util.CloseVetoException
779 // wrap byte arrray into UNO stream
780 com.sun.star.io.XInputStream xStream =
781 new com.sun.star.lib.uno.adapter.ByteArrayToXInputStreamAdapter(
782 aInBuffer );
784 // add stream to arguments
785 com.sun.star.beans.PropertyValue[] aExtendedArguments =
786 addArgument( aArguments, new com.sun.star.beans.PropertyValue(
787 "InputStream", -1, xStream, com.sun.star.beans.PropertyState.DIRECT_VALUE ) );
789 // call normal load method
790 loadFromURL( "private:stream", aExtendedArguments );
793 /** Stores a document to the given URL.
795 Due due a bug (50651) calling this method may cause the office to crash,
796 when at the same time the office writes a backup of the document. This bug
797 also affects {@link #storeToByteArray storeToByteArray} and
798 {@link #storeToStream storeToStream}. The workaround
799 is to start the office with the option -norestore, which disables the automatic
800 backup and recovery mechanism. OOoBean offers currently no supported way of providing
801 startup options for OOo. But it is possible to set a Java property when starting
802 Java, which is examined by OOoBean:
803 <pre>
804 java -Dcom.sun.star.officebean.Options=-norestore ...
805 </pre>
806 It is planned to offer a way of specifying startup options in a future version.
807 The property can be used until then. When using this property only one option
808 can be provided.
810 @throws IllegalArgumentException
811 if either of the arguments is out of the specified range.
813 @throws java.io.IOException
814 if an IO error occurs reading the ressource specified by the URL.
816 @throws com.sun.star.lang.NoConnectionException
817 if no connection is established.
819 @throws NoDocumentException
820 if no document is loaded
822 public void storeToURL(
823 final String aURL,
824 final com.sun.star.beans.PropertyValue aArguments[] )
825 throws
826 // @requirement FUNC.CON.LOST/0.2
827 NoConnectionException,
828 java.io.IOException,
829 com.sun.star.lang.IllegalArgumentException,
830 NoDocumentException
832 // no document available?
833 if ( aDocument == null )
834 throw new NoDocumentException();
838 // start runtime timeout
839 CallWatchThread aCallWatchThread =
840 new CallWatchThread( nOOoCallTimeOut, "storeToURL" );
842 // store the document
843 try { aDocument.storeToURL( aURL, aArguments ); }
844 catch ( com.sun.star.io.IOException aExc )
845 { throw new java.io.IOException(); }
847 // end runtime timeout
848 aCallWatchThread.cancel();
850 catch ( java.lang.InterruptedException aExc )
851 { throw new NoConnectionException(); }
854 /** Stores a document to a stream.
856 See {@link #storeToURL storeToURL} for further information.
857 @see #storeToURL storeToURL
859 public java.io.OutputStream storeToStream(
860 java.io.OutputStream aOutStream,
861 final com.sun.star.beans.PropertyValue aArguments[] )
862 throws
863 // @requirement FUNC.CON.LOST/0.2
864 NoConnectionException,
865 NoDocumentException,
866 java.io.IOException,
867 com.sun.star.lang.IllegalArgumentException
870 // wrap Java stream into UNO stream
871 com.sun.star.lib.uno.adapter.OutputStreamToXOutputStreamAdapter aStream =
872 new com.sun.star.lib.uno.adapter.OutputStreamToXOutputStreamAdapter(
873 aOutStream );
875 // add stream to arguments
876 com.sun.star.beans.PropertyValue[] aExtendedArguments =
877 addArgument( aArguments, new com.sun.star.beans.PropertyValue(
878 "OutputStream", -1, aStream, com.sun.star.beans.PropertyState.DIRECT_VALUE ) );
880 // call normal store method
881 storeToURL( "private:stream", aExtendedArguments );
883 // get byte array from document stream
884 try { aStream.closeOutput(); }
885 catch ( com.sun.star.io.NotConnectedException aExc )
886 { /* TDB */ }
887 catch ( com.sun.star.io.BufferSizeExceededException aExc )
888 { /* TDB */ }
889 catch ( com.sun.star.io.IOException aExc )
890 { throw new java.io.IOException(); }
891 return aOutStream;
894 /** Stores a document to a byte array.
896 See {@link #storeToURL storeToURL} for further information.
897 @see #storeToURL storeToURL
899 public byte[] storeToByteArray(
900 byte aOutBuffer[],
901 final com.sun.star.beans.PropertyValue aArguments[] )
902 throws
903 // @requirement FUNC.CON.LOST/0.2
904 NoConnectionException,
905 NoDocumentException,
906 java.io.IOException,
907 com.sun.star.lang.IllegalArgumentException
909 // wrap byte arrray into UNO stream
910 com.sun.star.lib.uno.adapter.XOutputStreamToByteArrayAdapter aStream =
911 new com.sun.star.lib.uno.adapter.XOutputStreamToByteArrayAdapter(
912 aOutBuffer );
914 // add stream to arguments
915 com.sun.star.beans.PropertyValue[] aExtendedArguments =
916 addArgument( aArguments, new com.sun.star.beans.PropertyValue(
917 "OutputStream", -1, aStream, com.sun.star.beans.PropertyState.DIRECT_VALUE ) );
919 // call normal store method
920 storeToURL( "private:stream", aExtendedArguments );
922 // get byte array from document stream
923 try { aStream.closeOutput(); }
924 catch ( com.sun.star.io.NotConnectedException aExc )
925 { /* TDB */ }
926 catch ( com.sun.star.io.BufferSizeExceededException aExc )
927 { /* TDB */ }
928 catch ( com.sun.star.io.IOException aExc )
929 { throw new java.io.IOException(); }
930 return aStream.getBuffer();
933 // @requirement FUNC.BEAN.PROG/0.5
934 // @requirement API.SIM.SEAP/0.2
935 /** returns the <type scope="com::sun::star::frame">Frame</a>
936 of the bean.
938 @returns
939 a Java class which implements all interfaces which the service
940 <type scope="com::sun::star::frame">Frame</a> implements.
941 Thus, methods can be called directly without queryInterface.
942 This feature might be implemented by UNO or explicitely coded.
944 @throws NoConnectionException
945 if the connection is not established.
947 @throws NotDocumentException
948 if no document is loaded an thus no frame is available.
950 public Frame getFrame()
952 throws
953 NoConnectionException // @requirement FUNC.CON.LOST/0.2
955 if ( iConnection == null )
956 throw new NoConnectionException();
957 return aFrame;
960 // @requirement FUNC.BEAN.PROG/0.5
961 // @requirement API.SIM.SEAP/0.2
962 /** returns the <type scope="com::sun::star::frame::Controller"> of the bean.
964 @returns
965 a Java class which implements all interfaces which the service
966 <type scope="com::sun::star::frame">Controller</a> implements.
967 Thus, methods can be called directly without queryInterface.
968 This feature might be implemented by UNO or explicitely coded.
970 @throws NoConnectionException
971 if the connection is not established.
973 public Controller getController()
975 // @requirement FUNC.CON.LOST/0.2
976 throws NoConnectionException
978 if ( iConnection == null )
979 throw new NoConnectionException();
980 if ( aController == null )
981 aController = new Controller( aFrame.getController() );
982 return aController;
985 // @requirement FUNC.BEAN.PROG/0.5
986 // @requirement FUNC.BEAN.STOR/0.4
987 // @requirement FUNC.BEAN.PRNT/0.4
988 // @requirement API.SIM.SEAP/0.2
989 /** returns the <type scope="com::sun::star::document::OfficeDocument">
990 of the bean.
992 @returns
993 a Java class which implements all interfaces which the service
994 <type scope="com::sun::star::document">OfficeDocument</a>
995 implements.
996 Thus, methods can be called directly without queryInterface.
997 This feature might be implemented by UNO or explicitely coded.
999 @throws NoConnectionException
1000 if the connection is not established.
1002 public OfficeDocument getDocument()
1004 // @requirement FUNC.CON.LOST/0.2
1005 throws NoConnectionException
1007 if ( iConnection == null )
1008 throw new NoConnectionException();
1009 return aDocument;
1012 /** Sets visibility of all tool bars known by this OOoBean version.
1014 Initially all tool bars are visible. By hiding all tool bars
1015 utilizing this method, it is possible to turn just a subset of
1016 tool bars on afterwards, no matter whether all available tool
1017 bars are known or not.
1019 If an older OOoBean instance is used with a newer OOo instance,
1020 some tool bars might not be affected by this method.
1022 If no connection is established or no document is loaded,
1023 the setting is memorized until a document is loaded. Same
1024 is valid when the connection dies within this function call.
1026 @deprecated Clients should use the service com.sun.star.frame.LayoutManager,
1027 which can be obtained from a frame, to control toolbars. For example:
1028 <pre>
1029 com.sun.star.beans.XPropertySet xPropSet =
1030 (com.sun.star.beans.XPropertySet) UnoRuntime.queryInterface(
1031 com.sun.star.beans.XPropertySet.class, aFrame );
1032 com.sun.star.frame.XLayoutManager xLayoutManager =
1033 (com.sun.star.frame.XLayoutManager) UnoRuntime.queryInterface(
1034 com.sun.star.frame.XLayoutManager.class,
1035 xPropSet.getPropertyValue( "LayoutManager" ) );
1036 xLayoutManager.showElement("private:resource/menubar/menubar");
1037 </pre>
1039 public void setAllBarsVisible( boolean bVisible )
1041 bIgnoreVisibility = true;
1042 setMenuBarVisible( bVisible );
1043 setStandardBarVisible( bVisible );
1044 setToolBarVisible( bVisible );
1045 setStatusBarVisible( bVisible );
1046 bIgnoreVisibility = false;
1049 //--------------------------------------------------------------------------
1050 /** Applies all tool visiblities to the real thing.
1052 @deprecated Clients should use the service com.sun.star.frame.LayoutManager,
1053 which can be obtained from a frame, to control toolbars. See also
1054 {@link #setAllBarsVisible setAllBarsVisible}.
1056 protected void applyToolVisibilities()
1057 throws
1058 java.lang.InterruptedException
1060 bIgnoreVisibility = true;
1061 setMenuBarVisible( bMenuBarVisible );
1062 setStandardBarVisible( bStandardBarVisible );
1063 setToolBarVisible( bToolBarVisible );
1064 setStatusBarVisible( bStatusBarVisible );
1065 bIgnoreVisibility = false;
1068 /** Helper method to set tool bar visibilty.
1070 @param bnewValue
1071 If false, the tool bar is disabled,
1072 If true, the tool bar is visible.
1074 @deprecated Clients should use the service com.sun.star.frame.LayoutManager,
1075 which can be obtained from a frame, to control toolbars. See also
1076 {@link #setAllBarsVisible}.
1078 protected boolean setToolVisible( String aProperty, String aResourceURL,
1079 boolean bOldValue, boolean bNewValue )
1081 throws
1082 java.lang.InterruptedException
1084 // start runtime timeout
1085 CallWatchThread aCallWatchThread =
1086 new CallWatchThread( nOOoCallTimeOut, "setToolVisible" );
1088 // Does a frame exist?
1089 if ( aFrame != null )
1091 if ( bIgnoreVisibility || bOldValue != bNewValue )
1093 try
1095 com.sun.star.beans.XPropertySet xPropSet =
1096 (com.sun.star.beans.XPropertySet) UnoRuntime.queryInterface(
1097 com.sun.star.beans.XPropertySet.class, aFrame );
1098 com.sun.star.frame.XLayoutManager xLayoutManager =
1099 (com.sun.star.frame.XLayoutManager) UnoRuntime.queryInterface(
1100 com.sun.star.frame.XLayoutManager.class,
1101 xPropSet.getPropertyValue( "LayoutManager" ) );
1102 if ( bNewValue )
1103 xLayoutManager.showElement( aResourceURL );
1104 else
1105 xLayoutManager.hideElement( aResourceURL );
1107 catch ( com.sun.star.beans.UnknownPropertyException aExc )
1109 throw new RuntimeException( "not layout manager found" );
1111 catch ( com.sun.star.lang.WrappedTargetException aExc )
1113 throw new RuntimeException( "not layout manager found" );
1116 // notify change
1117 firePropertyChange( aProperty, new Boolean(bOldValue), new Boolean(bNewValue) );
1121 // end runtime timeout
1122 aCallWatchThread.cancel();
1124 // the new value will be stored by caller
1125 return bNewValue;
1128 /** Sets the visibility of the menu bar.
1130 Initially the menu bar is visible.
1132 If not connected or no document loaded, the value is stored
1133 and automatically applied to the document after it is loaded.
1134 Same is valid when the connection dies within this function call.
1136 @param bVisible
1137 If false, the menu bar is disabled,
1138 If true, the menu bar is visible.
1140 @deprecated Clients should use the service com.sun.star.frame.LayoutManager,
1141 which can be obtained from a frame, to control toolbars. See also
1142 {@link #setAllBarsVisible}.
1144 public void setMenuBarVisible(boolean bVisible)
1148 bMenuBarVisible = setToolVisible( "MenuBarVisible",
1149 "private:resource/menubar/menubar", bMenuBarVisible, bVisible );
1151 catch ( java.lang.InterruptedException aExc )
1153 bMenuBarVisible = bVisible;
1157 /** Returns the visibility of the menu bar.
1159 This method works independently from a connetion or loaded document.
1160 If no connection is established or no document is loaded,
1161 this method just returns a memorized status.
1163 @return
1164 True if the menu bar is visible,
1165 false if the menu bar is hidden.
1167 @deprecated Clients should use the service com.sun.star.frame.LayoutManager,
1168 which can be obtained from a frame, to control toolbars. See also
1169 {@link #setAllBarsVisible}.
1171 public boolean isMenuBarVisible()
1173 return bMenuBarVisible;
1176 /** Sets the main function bar visibilty.
1178 Initially the standard bar is visible.
1180 If not connected or no document loaded, the value is stored
1181 and automatically applied to the document after it is loaded.
1182 Same is valid when the connection dies within this function call.
1184 @param bVisible
1185 If false, the main function bar is disabled,
1186 If true, the main function bar is visible.
1188 @deprecated Clients should use the service com.sun.star.frame.LayoutManager,
1189 which can be obtained from a frame, to control toolbars. See also
1190 {@link #setAllBarsVisible}.
1192 public void setStandardBarVisible(boolean bVisible)
1196 bStandardBarVisible = setToolVisible( "StandardBarVisible",
1197 "private:resource/toolbar/standardbar", bStandardBarVisible, bVisible );
1199 catch ( java.lang.InterruptedException aExc )
1201 bMenuBarVisible = bVisible;
1205 /** Returns the visibility of the main function bar.
1207 This method works independently from a connetion or loaded document.
1208 If no connection is established or no document is loaded,
1209 this method just returns a memorized status.
1211 @return
1212 True if the main function bar is visible,
1213 false if the main function bar is hidden.
1215 @deprecated Clients should use the service com.sun.star.frame.LayoutManager,
1216 which can be obtained from a frame, to control toolbars. See also
1217 {@link #setAllBarsVisible}.
1219 public boolean isStandardBarVisible()
1221 return bStandardBarVisible;
1224 /** Sets the tool function bar visibilty.
1226 Initially the tool bar is visible.
1228 If not connected or no document loaded, the value is stored
1229 and automatically applied to the document after it is loaded.
1230 Same is valid when the connection dies within this function call.
1232 @param bVisible
1233 If false, the tool function bar is disabled,
1234 If true, the tool function bar is visible.
1236 @deprecated Clients should use the service com.sun.star.frame.LayoutManager,
1237 which can be obtained from a frame, to control toolbars. See also
1238 {@link #setAllBarsVisible}.
1240 public void setToolBarVisible(boolean bVisible)
1242 try
1244 bToolBarVisible = setToolVisible( "ToolBarVisible",
1245 "private:resource/toolbar/toolbar", bToolBarVisible, bVisible );
1247 catch ( java.lang.InterruptedException aExc )
1249 bMenuBarVisible = bVisible;
1253 /** Returns the visibility of the tool function bar.
1255 This method works independently from a connetion or loaded document.
1256 If no connection is established or no document is loaded,
1257 this method just returns a memorized status.
1259 @return
1260 True if the tool function bar is visible,
1261 false if the tool function bar is hidden.
1263 @deprecated Clients should use the service com.sun.star.frame.LayoutManager,
1264 which can be obtained from a frame, to control toolbars. See also
1265 {@link #setAllBarsVisible}.
1267 public boolean isToolBarVisible()
1269 return bToolBarVisible;
1272 /** Sets the status function bar visibilty.
1274 Initially the status bar is visible.
1276 If not connected or no document loaded, the value is stored
1277 and automatically applied to the document after it is loaded.
1278 Same is valid when the connection dies within this function call.
1280 @param bVisible
1281 If false, the status function bar is disabled,
1282 If true, the status function bar is visible.
1284 @deprecated Clients should use the service com.sun.star.frame.LayoutManager,
1285 which can be obtained from a frame, to control toolbars. See also
1286 {@link #setAllBarsVisible}.
1288 public void setStatusBarVisible(boolean bVisible)
1290 try
1292 bStatusBarVisible = setToolVisible( "StatusBarVisible",
1293 "private:resource/statusbar/statusbar", bStatusBarVisible, bVisible );
1295 catch ( java.lang.InterruptedException aExc )
1297 bMenuBarVisible = bVisible;
1301 /** Returns the visibility of the status function bar.
1303 This method works independently from a connetion or loaded document.
1304 If no connection is established or no document is loaded,
1305 this method just returns a memorized status.
1307 @return
1308 True if the status function bar is visible,
1309 false if the status function bar is hidden.
1311 @deprecated Clients should use the service com.sun.star.frame.LayoutManager,
1312 which can be obtained from a frame, to control toolbars. See also
1313 {@link #setAllBarsVisible}.
1315 public boolean isStatusBarVisible()
1317 return bStatusBarVisible;
1320 //===========================================================================
1321 // Helper Methods / Internal Methods
1322 //---------------------------------------------------------------------------
1324 // general instance intializer
1326 setLayout(new java.awt.BorderLayout());
1330 @deprecated
1332 public void paint( java.awt.Graphics aGraphics )
1336 /** Adds a single argument to an array of arguments.
1338 If the argument by its name is already in aArguments
1339 it is exchanged and aArguments is returned.
1341 If the argument by its name is not yet in aArguments,
1342 a new array is created, aArgument added and the new
1343 array returned.
1345 protected com.sun.star.beans.PropertyValue[] addArgument(
1346 com.sun.star.beans.PropertyValue aArguments[],
1347 final com.sun.star.beans.PropertyValue aArgument )
1349 // get number of current arguments
1350 int nNumArgs = 0;
1351 if ( aArguments != null )
1352 nNumArgs = aArguments.length;
1354 // is new argument already set?
1355 for ( int n = 0; n < nNumArgs; ++n )
1357 if ( aArguments[n].Name == aArgument.Name )
1359 // substitute this argument
1360 aArguments[n] = aArgument;
1362 // return current array
1363 return aArguments;
1367 // create extended arguments
1368 com.sun.star.beans.PropertyValue[] aExtendedArguments =
1369 new com.sun.star.beans.PropertyValue[ nNumArgs + 1 ];
1371 // copy current arguments
1372 for ( int n = 0; n < nNumArgs; ++n )
1373 aExtendedArguments[n] = aArguments[n];
1375 // add new argument
1376 aExtendedArguments[ nNumArgs ] = aArgument;
1378 // return new arguments
1379 return aExtendedArguments;
1382 //===========================================================================
1383 // Helper Classes
1384 //---------------------------------------------------------------------------
1386 /** Helper class to listen on the connection to learn when it dies.
1388 @internal
1390 private class EventListener
1391 extends Thread
1392 implements
1393 com.sun.star.lang.XEventListener,
1394 com.sun.star.frame.XTerminateListener
1396 String aTag;
1398 EventListener( String aTag )
1399 throws NoConnectionException
1401 // init members
1402 this.aTag = aTag;
1404 // listen on a dying connection
1405 iConnection.addEventListener( this );
1407 // listen on a terminating OOo
1408 getOOoDesktop().addTerminateListener( this );
1410 // start this thread as a daemon
1411 setDaemon( true );
1412 start();
1415 public void end()
1417 // do not listen on a dying connection anymore
1418 try {
1419 iConnection.removeEventListener( this );
1421 catch ( Throwable aExc ) {};
1423 // do not listen on a terminating OOo anymore
1424 try {
1425 getOOoDesktop().removeTerminateListener( this );
1427 catch ( Throwable aExc ) {};
1429 // stop thread
1430 this.interrupt();
1433 /// gets called when the connection dies
1434 public void disposing( /*IN*/ com.sun.star.lang.EventObject Source )
1436 // empty the OOoBean and cut the connection
1437 stopOOoConnection();
1440 /// gets called when the user wants to terminate OOo
1441 public void queryTermination( /*IN*/ com.sun.star.lang.EventObject Event )
1442 throws com.sun.star.frame.TerminationVetoException
1444 // disallow termination of OOo while a OOoBean exists
1445 throw new com.sun.star.frame.TerminationVetoException();
1448 /// gets called when OOo terminates
1449 public void notifyTermination( /*IN*/ com.sun.star.lang.EventObject Event )
1451 // empty the OOoBean and cut the connection
1452 stopOOoConnection();
1455 /// watching the connection
1456 public void run()
1458 dbgPrint( "EventListener(" + aTag + ").run()" );
1460 // remote call might hang => watch try
1461 CallWatchThread aCallWatchThread =
1462 new CallWatchThread( nOOoCallTimeOut, "EventListener(" + aTag + ")" );
1464 // continue to trying to connect the OOo instance
1465 long n = 0;
1466 while ( isInterrupted() == false
1467 && iConnection != null
1468 && iConnection.getComponentContext() != null )
1470 dbgPrint( "EventListener(" + aTag + ").running() #" + ++n );
1472 // still alive?
1473 com.sun.star.lang.XMultiComponentFactory xServiceManager = null;
1474 try
1476 // an arbitrary (but cheap) call into OOo
1477 xServiceManager = iConnection.getComponentContext().getServiceManager();
1479 // call successfully performed, restart watch for next loop
1482 aCallWatchThread.restart();
1484 catch ( java.lang.InterruptedException aExc )
1486 // ignore late interrupt
1489 catch ( java.lang.RuntimeException aExc )
1491 // hung
1492 OfficeConnection iDeadConn = iConnection;
1493 iConnection = null;
1494 iDeadConn.dispose();
1497 // sleep
1498 try {
1499 sleep(nOOoCheckCycle);
1501 catch ( java.lang.InterruptedException aExc )
1503 dbgPrint("EventListener(" + aTag + ") interupted.");
1504 //thread can be ended by EvendListener.end();
1505 break;