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 
20 import java.awt.Dimension;
21 import java.util.ArrayList;
22 
23 import com.sun.star.beans.XPropertySet;
24 import com.sun.star.configuration.theDefaultProvider;
25 import com.sun.star.container.XNameAccess;
26 import com.sun.star.embed.VisualRepresentation;
27 import com.sun.star.embed.XStorage;
28 import com.sun.star.embed.XTransactedObject;
29 import com.sun.star.io.XInputStream;
30 import com.sun.star.io.XOutputStream;
31 import com.sun.star.io.XStream;
32 import com.sun.star.io.XTruncate;
33 import com.sun.star.lang.XComponent;
34 import com.sun.star.lang.XMultiServiceFactory;
35 import com.sun.star.lib.uno.helper.WeakBase;
36 import com.sun.star.uno.AnyConverter;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.uno.XComponentContext;
39 
40 public final class OwnEmbeddedObject extends WeakBase
41    implements com.sun.star.embed.XEmbedPersist,
42               com.sun.star.embed.XEmbeddedObject
43 {
44     private final XComponentContext m_xContext;
45     private final byte[] m_aClassID;
46 
47     private boolean m_bDisposed = false;
48     private int m_nObjectState = -1;
49 
50     private com.sun.star.embed.XStorage m_xParentStorage;
51     private com.sun.star.embed.XStorage m_xOwnStorage;
52     private String m_aEntryName;
53 
54     private com.sun.star.embed.XStorage m_xNewParentStorage;
55     private com.sun.star.embed.XStorage m_xNewOwnStorage;
56     private String m_aNewEntryName;
57     private boolean m_bWaitSaveCompleted = false;
58 
59     private EditorFrame m_aEditorFrame;
60 
61     private ArrayList<Object> m_aListeners;
62 
63     private com.sun.star.embed.VerbDescriptor[] m_pOwnVerbs;
64 
65     private com.sun.star.embed.XEmbeddedClient m_xClient;
66 
67     private Dimension m_aObjSize;
68 
69 
GetListeners()70     private ArrayList<Object> GetListeners()
71     {
72         if ( m_aListeners == null )
73             m_aListeners = new ArrayList<Object>(10);
74 
75         return m_aListeners;
76     }
77 
78 
UpdateSizeAndGetFromActive()79     private Dimension UpdateSizeAndGetFromActive()
80     {
81         if ( m_nObjectState == com.sun.star.embed.EmbedStates.ACTIVE )
82             m_aObjSize = m_aEditorFrame.getAppSize();
83 
84         if ( m_aObjSize != null )
85             return m_aObjSize;
86         else
87             return new Dimension();
88     }
89 
90 
SwitchOwnPersistence( XStorage xParentStorage, XStorage xOwnStorage, String aEntryName )91     private void SwitchOwnPersistence( XStorage xParentStorage, XStorage xOwnStorage, String aEntryName )
92     {
93         if ( xOwnStorage != m_xOwnStorage )
94         {
95             if ( m_xOwnStorage != null )
96                 m_xOwnStorage.dispose();
97             m_xParentStorage = xParentStorage;
98             m_xOwnStorage = xOwnStorage;
99             m_aEntryName = aEntryName;
100         }
101     }
102 
103 
SwitchOwnPersistence( XStorage xParentStorage, String aEntryName )104     private void SwitchOwnPersistence( XStorage xParentStorage, String aEntryName ) throws com.sun.star.io.IOException
105     {
106         if ( xParentStorage != m_xParentStorage || !aEntryName.equals( m_aEntryName ) )
107         {
108             try
109             {
110                 XStorage xOwnStorage = xParentStorage.openStorageElement( aEntryName, com.sun.star.embed.ElementModes.READWRITE );
111                 SwitchOwnPersistence( xParentStorage, xOwnStorage, aEntryName );
112             }
113             catch( com.sun.star.io.IOException e )
114             {
115                 throw e;
116             }
117             catch( com.sun.star.uno.Exception e )
118             {
119                 com.sun.star.io.IOException e2 = new com.sun.star.io.IOException( "Error while switching object storage!" );
120                 e2.initCause(e);
121                 throw e2;
122             }
123         }
124     }
125 
126 
SaveDataToStorage( XStorage xStorage, String aString, Dimension aDimension )127     private static void SaveDataToStorage( XStorage xStorage, String aString, Dimension aDimension ) throws com.sun.star.io.IOException
128     {
129         try
130         {
131             // save the text
132             XStream xStream = xStorage.openStreamElement( "content.txt", com.sun.star.embed.ElementModes.READWRITE );
133             XComponent xStreamComp = UnoRuntime.queryInterface( XComponent.class, xStream );
134             if ( xStreamComp == null )
135                 throw new com.sun.star.uno.RuntimeException();
136 
137             XOutputStream xOutStream = xStream.getOutputStream();
138             XTruncate xTruncate = UnoRuntime.queryInterface( XTruncate.class, xOutStream );
139             if ( xTruncate == null )
140                 throw new com.sun.star.io.IOException();
141 
142             xTruncate.truncate();
143             xOutStream.writeBytes( aString.getBytes() );
144 
145             // save the size
146             xStream = xStorage.openStreamElement( "properties.txt", com.sun.star.embed.ElementModes.READWRITE );
147             xStreamComp = UnoRuntime.queryInterface( XComponent.class, xStream );
148             if ( xStreamComp == null )
149                 throw new com.sun.star.uno.RuntimeException();
150 
151             xOutStream = xStream.getOutputStream();
152             xTruncate = UnoRuntime.queryInterface( XTruncate.class, xOutStream );
153             if ( xTruncate == null )
154                 throw new com.sun.star.io.IOException();
155 
156             xTruncate.truncate();
157             String aProps = Integer.toString( (int)aDimension.getWidth() ) + "|" + Integer.toString( (int)aDimension.getHeight() );
158             xOutStream.writeBytes( aProps.getBytes() );
159 
160             // set the media type
161             XPropertySet xPropSet = UnoRuntime.queryInterface( XPropertySet.class, xStorage );
162             if ( xPropSet == null )
163                 throw new com.sun.star.uno.RuntimeException();
164             xPropSet.setPropertyValue( "MediaType", "application/x-openoffice-embedded-69474366-FD6F-4806-8374-8EDD1B6E771D" );
165 
166             XTransactedObject xTransact = UnoRuntime.queryInterface( XTransactedObject.class, xStorage );
167             if ( xTransact != null )
168                 xTransact.commit();
169 
170             xStreamComp.dispose();
171         }
172         catch( com.sun.star.io.IOException e )
173         {
174             throw e;
175         }
176         catch( com.sun.star.uno.Exception e )
177         {
178             com.sun.star.io.IOException e2 = new com.sun.star.io.IOException( "Error while switching object storage!" );
179             e2.initCause(e);
180             throw e2;
181         }
182     }
183 
184 
PostEvent( String aEvEntryName )185     private void PostEvent( String aEvEntryName )
186     {
187         if ( m_aListeners != null )
188         {
189             com.sun.star.document.EventObject aEventObject = new com.sun.star.document.EventObject( this, aEvEntryName );
190             for ( int nInd = 0; nInd < m_aListeners.size(); nInd++ )
191             {
192                 try
193                 {
194                     com.sun.star.document.XEventListener xListener = UnoRuntime.queryInterface( com.sun.star.document.XEventListener.class, m_aListeners.get( nInd ) );
195 
196                     if ( xListener != null )
197                         xListener.notifyEvent( aEventObject );
198                 }
199                 catch( java.lang.Exception e )
200                 {
201                     m_aListeners.remove( nInd );
202                 }
203             }
204         }
205     }
206 
207 
StateChangeNotification( boolean bBeforeChange, int nOldState, int nNewState )208     private void StateChangeNotification( boolean bBeforeChange, int nOldState, int nNewState )
209     {
210         if ( m_aListeners != null )
211         {
212             com.sun.star.lang.EventObject aEventObject = new com.sun.star.lang.EventObject( this );
213             for ( int nInd = 0; nInd < m_aListeners.size(); nInd++ )
214             {
215                 try
216                 {
217                     com.sun.star.embed.XStateChangeListener xListener = UnoRuntime.queryInterface( com.sun.star.embed.XStateChangeListener.class, m_aListeners.get( nInd ) );
218 
219                     if ( xListener != null )
220                     {
221                         if ( bBeforeChange )
222                             xListener.changingState( aEventObject, nOldState, nNewState );
223                         else
224                             xListener.stateChanged( aEventObject, nOldState, nNewState );
225                     }
226                 }
227                 catch( java.lang.Exception e )
228                 {
229                     m_aListeners.remove( nInd );
230                 }
231             }
232         }
233     }
234 
235 
ReadStringFromStream( XStorage xStorage, String aStreamName )236     private String ReadStringFromStream( XStorage xStorage, String aStreamName ) throws com.sun.star.io.IOException
237     {
238         if ( xStorage == null )
239             throw new com.sun.star.uno.RuntimeException();
240 
241         try
242         {
243             XStream xStream = xStorage.openStreamElement( aStreamName, com.sun.star.embed.ElementModes.READWRITE );
244             XComponent xStreamComp = UnoRuntime.queryInterface( XComponent.class, xStream );
245             if ( xStreamComp == null )
246                 throw new com.sun.star.uno.RuntimeException();
247 
248             XInputStream xInStream = xStream.getInputStream();
249             byte[][] aData = new byte[1][];
250             aData[0] = new byte[0];
251             String aResult = "";
252 
253             int nLen = 0;
254             do
255             {
256                 nLen = xInStream.readBytes( aData, 10 );
257                 if ( aData.length == 0 || aData[0] == null )
258                     throw new com.sun.star.io.IOException();
259                 aResult += new String( aData[0] );
260             } while( nLen > 0 );
261 
262             xStreamComp.dispose();
263 
264             return aResult;
265         }
266         catch( com.sun.star.io.IOException e )
267         {
268             throw e;
269         }
270         catch( com.sun.star.uno.Exception e )
271         {
272             com.sun.star.io.IOException e2 = new com.sun.star.io.IOException( "Error while reading one of object streams!" );
273             e2.initCause(e);
274             throw e2;
275         }
276     }
277 
278 
ReadSizeFromOwnStorage()279     private void ReadSizeFromOwnStorage() throws com.sun.star.io.IOException
280     {
281         String aSize = ReadStringFromStream( m_xOwnStorage, "properties.txt" );
282 
283         int nSeparator = aSize.indexOf( '|' );
284         if ( nSeparator > 0 && nSeparator < aSize.length() - 1 )
285         {
286             int nWidth = Integer.parseInt( aSize.substring( 0, nSeparator ) );
287             int nHeight = Integer.parseInt( aSize.substring( nSeparator + 1, aSize.length() ) );
288             m_aObjSize = new Dimension( nWidth, nHeight );
289         }
290     }
291 
292 
OwnEmbeddedObject( XComponentContext context, byte[] aClassID )293     public OwnEmbeddedObject( XComponentContext context, byte[] aClassID )
294     {
295         m_xContext = context;
296         m_aClassID = aClassID;
297     }
298 
299 
CloseFrameRequest()300     public void CloseFrameRequest()
301     {
302         com.sun.star.embed.XEmbeddedClient xClient = m_xClient;
303         if ( xClient == null )
304             return;
305 
306         UpdateSizeAndGetFromActive();
307         StateChangeNotification( true, com.sun.star.embed.EmbedStates.ACTIVE, com.sun.star.embed.EmbedStates.RUNNING );
308 
309         try{
310             xClient.visibilityChanged( false );
311         } catch( com.sun.star.uno.Exception e ){}
312 
313         try{
314             xClient.saveObject();
315         } catch( com.sun.star.uno.Exception e ){}
316 
317         m_nObjectState = com.sun.star.embed.EmbedStates.RUNNING;
318         StateChangeNotification( false, com.sun.star.embed.EmbedStates.ACTIVE, m_nObjectState );
319 
320         PostEvent( "OnVisAreaChanged" );
321     }
322 
323     // com.sun.star.embed.XCommonEmbedPersist:
324 
storeOwn()325     public void storeOwn() throws com.sun.star.embed.WrongStateException, com.sun.star.io.IOException, com.sun.star.uno.Exception
326     {
327         if ( m_bDisposed )
328             throw new com.sun.star.lang.DisposedException();
329 
330         if ( m_nObjectState == -1 || m_bWaitSaveCompleted )
331             throw new com.sun.star.embed.WrongStateException();
332 
333         // nothing to do, if the object is in loaded state
334         if ( m_nObjectState == com.sun.star.embed.EmbedStates.LOADED )
335             return;
336 
337         if ( m_xOwnStorage == null )
338             throw new com.sun.star.io.IOException();
339 
340         PostEvent( "OnSave" );
341 
342         if ( m_aEditorFrame == null )
343             throw new com.sun.star.uno.RuntimeException();
344 
345         SaveDataToStorage( m_xOwnStorage, m_aEditorFrame.getText(), UpdateSizeAndGetFromActive() );
346 
347         PostEvent( "OnSaveDone" );
348     }
349 
350 
isReadonly()351     public boolean isReadonly() throws com.sun.star.embed.WrongStateException
352     {
353         return false;
354     }
355 
356 
reload(com.sun.star.beans.PropertyValue[] aMediaArgs, com.sun.star.beans.PropertyValue[] aObjectArgs)357     public void reload(com.sun.star.beans.PropertyValue[] aMediaArgs, com.sun.star.beans.PropertyValue[] aObjectArgs) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.embed.WrongStateException, com.sun.star.io.IOException, com.sun.star.uno.Exception
358     {
359         // not implemented currently
360     }
361 
362     // com.sun.star.embed.XEmbedPersist:
363 
setPersistentEntry(com.sun.star.embed.XStorage xStorage, String aEntryName, int nEntryConnectionMode, com.sun.star.beans.PropertyValue[] aMediaArgs, com.sun.star.beans.PropertyValue[] aObjectArgs)364     public void setPersistentEntry(com.sun.star.embed.XStorage xStorage, String aEntryName, int nEntryConnectionMode, com.sun.star.beans.PropertyValue[] aMediaArgs, com.sun.star.beans.PropertyValue[] aObjectArgs) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.embed.WrongStateException, com.sun.star.io.IOException, com.sun.star.uno.Exception
365     {
366         if ( m_bDisposed )
367             throw new com.sun.star.lang.DisposedException();
368 
369         if ( xStorage == null || aEntryName.length() == 0 )
370             throw new com.sun.star.lang.IllegalArgumentException();
371 
372         if ( ( m_nObjectState != -1 || nEntryConnectionMode == com.sun.star.embed.EntryInitModes.NO_INIT )
373           && ( m_nObjectState == -1 || nEntryConnectionMode != com.sun.star.embed.EntryInitModes.NO_INIT ) )
374         {
375             // if the object is not loaded
376             // it can not get persistent representation without initialization
377 
378             // if the object is loaded
379             // it can switch persistent representation only without initialization
380 
381             throw new com.sun.star.embed.WrongStateException();
382         }
383 
384         if ( m_bWaitSaveCompleted )
385         {
386             if ( nEntryConnectionMode == com.sun.star.embed.EntryInitModes.NO_INIT )
387             {
388                 if ( m_xParentStorage == xStorage && m_aEntryName.equals( aEntryName ) )
389                     saveCompleted( false );
390                 else if ( m_xNewParentStorage == xStorage && m_aNewEntryName.equals( aEntryName ) )
391                     saveCompleted( true );
392                 else
393                     throw new com.sun.star.embed.WrongStateException();
394             }
395             else
396                 throw new com.sun.star.embed.WrongStateException();
397 
398             return;
399         }
400 
401         boolean bElExists = xStorage.hasByName( aEntryName );
402 
403         if ( nEntryConnectionMode == com.sun.star.embed.EntryInitModes.DEFAULT_INIT )
404         {
405             SwitchOwnPersistence( xStorage, aEntryName );
406             if ( bElExists )
407             {
408                 XPropertySet xPropSet = UnoRuntime.queryInterface( XPropertySet.class, m_xOwnStorage );
409                 if ( xPropSet == null )
410                     throw new com.sun.star.uno.RuntimeException();
411 
412                 String aMediaType = AnyConverter.toString( xPropSet.getPropertyValue( "MediaType" ) );
413                 if ( !aMediaType.equals( "application/x-openoffice-embedded-69474366-FD6F-4806-8374-8EDD1B6E771D" ) )
414                     throw new com.sun.star.lang.IllegalArgumentException();
415 
416                 m_nObjectState = com.sun.star.embed.EmbedStates.LOADED;
417             }
418             else
419             {
420                 m_aEditorFrame = new EditorFrame( m_aEntryName, this, 5, 20 );
421                 m_nObjectState = com.sun.star.embed.EmbedStates.RUNNING;
422                 m_aObjSize = m_aEditorFrame.getAppSize();
423             }
424         }
425         else if ( nEntryConnectionMode == com.sun.star.embed.EntryInitModes.TRUNCATE_INIT )
426         {
427             SwitchOwnPersistence( xStorage, aEntryName );
428             m_aEditorFrame = new EditorFrame( m_aEntryName, this, 5, 20 );
429             m_nObjectState = com.sun.star.embed.EmbedStates.RUNNING;
430             m_aObjSize = m_aEditorFrame.getAppSize();
431         }
432         else
433             throw new com.sun.star.lang.IllegalArgumentException();
434     }
435 
436 
storeToEntry(com.sun.star.embed.XStorage xStorage, String aEntryName, com.sun.star.beans.PropertyValue[] aMediaArgs, com.sun.star.beans.PropertyValue[] aObjectArgs)437     public void storeToEntry(com.sun.star.embed.XStorage xStorage, String aEntryName, com.sun.star.beans.PropertyValue[] aMediaArgs, com.sun.star.beans.PropertyValue[] aObjectArgs) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.embed.WrongStateException, com.sun.star.io.IOException, com.sun.star.uno.Exception
438     {
439         if ( m_bDisposed )
440             throw new com.sun.star.lang.DisposedException();
441 
442         if ( m_nObjectState == -1 || m_bWaitSaveCompleted )
443             throw new com.sun.star.embed.WrongStateException();
444 
445         if ( m_nObjectState == com.sun.star.embed.EmbedStates.LOADED )
446         {
447             m_xParentStorage.copyElementTo( m_aEntryName, xStorage, aEntryName );
448         }
449         else
450         {
451             com.sun.star.embed.XStorage xSubStorage =
452                 xStorage.openStorageElement( aEntryName,
453                             com.sun.star.embed.ElementModes.READWRITE | com.sun.star.embed.ElementModes.TRUNCATE );
454 
455             SaveDataToStorage( xSubStorage, m_aEditorFrame.getText(), UpdateSizeAndGetFromActive() );
456         }
457     }
458 
459 
storeAsEntry(com.sun.star.embed.XStorage xStorage, String aEntryName, com.sun.star.beans.PropertyValue[] aMediaArgs, com.sun.star.beans.PropertyValue[] aObjectArgs)460     public void storeAsEntry(com.sun.star.embed.XStorage xStorage, String aEntryName, com.sun.star.beans.PropertyValue[] aMediaArgs, com.sun.star.beans.PropertyValue[] aObjectArgs) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.embed.WrongStateException, com.sun.star.io.IOException, com.sun.star.uno.Exception
461     {
462         if ( m_bDisposed )
463             throw new com.sun.star.lang.DisposedException();
464 
465         if ( m_nObjectState == -1 || m_bWaitSaveCompleted )
466             throw new com.sun.star.embed.WrongStateException();
467 
468         com.sun.star.embed.XStorage xSubStorage = null;
469 
470         if ( m_nObjectState == com.sun.star.embed.EmbedStates.LOADED )
471         {
472             xSubStorage =
473                 xStorage.openStorageElement( aEntryName,
474                             com.sun.star.embed.ElementModes.READWRITE | com.sun.star.embed.ElementModes.NOCREATE );
475 
476             m_xOwnStorage.copyToStorage( xSubStorage );
477         }
478         else
479         {
480             xSubStorage =
481                 xStorage.openStorageElement( aEntryName,
482                             com.sun.star.embed.ElementModes.READWRITE | com.sun.star.embed.ElementModes.TRUNCATE );
483 
484             SaveDataToStorage( xSubStorage, m_aEditorFrame.getText(), UpdateSizeAndGetFromActive() );
485         }
486 
487         m_bWaitSaveCompleted = true;
488         m_xNewOwnStorage = xSubStorage;
489         m_xNewParentStorage = xStorage;
490         m_aNewEntryName = aEntryName;
491 
492     }
493 
494 
saveCompleted(boolean bUseNew)495     public void saveCompleted(boolean bUseNew) throws com.sun.star.embed.WrongStateException, com.sun.star.uno.Exception
496     {
497         if ( m_bDisposed )
498             throw new com.sun.star.lang.DisposedException();
499 
500         if ( m_nObjectState == -1 )
501             throw new com.sun.star.embed.WrongStateException();
502 
503         // it is allowed to call saveCompleted( false ) for nonstored objects
504         if ( !m_bWaitSaveCompleted && !bUseNew )
505             return;
506 
507         if ( !m_bWaitSaveCompleted )
508             throw new com.sun.star.io.IOException();
509 
510         if ( bUseNew )
511         {
512             SwitchOwnPersistence( m_xNewParentStorage, m_xNewOwnStorage, m_aNewEntryName );
513             PostEvent( "OnSaveAsDone" );
514         }
515         else
516         {
517             try
518             {
519                 m_xNewOwnStorage.dispose();
520             }
521             catch( com.sun.star.uno.RuntimeException e )
522             {}
523         }
524 
525         m_xNewOwnStorage = null;
526         m_xNewParentStorage = null;
527         m_aNewEntryName = null;
528         m_bWaitSaveCompleted = false;
529     }
530 
531 
hasEntry()532     public boolean hasEntry() throws com.sun.star.embed.WrongStateException
533     {
534         if ( m_bDisposed )
535             throw new com.sun.star.lang.DisposedException();
536 
537         if ( m_bWaitSaveCompleted )
538             throw new com.sun.star.embed.WrongStateException();
539 
540         if ( m_xOwnStorage != null )
541             return true;
542 
543         return false;
544     }
545 
546 
getEntryName()547     public String getEntryName() throws com.sun.star.embed.WrongStateException
548     {
549         if ( m_bDisposed )
550             throw new com.sun.star.lang.DisposedException();
551 
552         if ( m_nObjectState == -1 || m_bWaitSaveCompleted )
553             throw new com.sun.star.embed.WrongStateException();
554 
555         return m_aEntryName;
556     }
557 
558     // com.sun.star.embed.XVisualObject:
559 
setVisualAreaSize(long nAspect, com.sun.star.awt.Size aSize)560     public void setVisualAreaSize(long nAspect, com.sun.star.awt.Size aSize) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.embed.WrongStateException, com.sun.star.uno.Exception
561     {
562         if ( m_bDisposed )
563             throw new com.sun.star.lang.DisposedException();
564 
565         if ( m_nObjectState == -1 )
566             throw new com.sun.star.embed.WrongStateException();
567 
568         if ( nAspect == com.sun.star.embed.Aspects.MSOLE_ICON )
569             // the ICON aspect should be handled by the container
570             throw new com.sun.star.embed.WrongStateException();
571 
572         if ( m_nObjectState == com.sun.star.embed.EmbedStates.LOADED )
573             changeState( com.sun.star.embed.EmbedStates.RUNNING );
574 
575         if ( m_aEditorFrame == null )
576             throw new com.sun.star.uno.RuntimeException();
577 
578         m_aObjSize.setSize( aSize.Width, aSize.Height );
579         m_aEditorFrame.setAppSize( m_aObjSize );
580     }
581 
582 
getVisualAreaSize(long nAspect)583     public com.sun.star.awt.Size getVisualAreaSize(long nAspect) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.embed.WrongStateException, com.sun.star.uno.Exception
584     {
585         if ( m_bDisposed )
586             throw new com.sun.star.lang.DisposedException();
587 
588         if ( m_nObjectState == -1 )
589             throw new com.sun.star.embed.WrongStateException();
590 
591         if ( nAspect == com.sun.star.embed.Aspects.MSOLE_ICON )
592             // the ICON aspect should be handled by the container
593             throw new com.sun.star.embed.WrongStateException();
594 
595         if ( m_nObjectState == com.sun.star.embed.EmbedStates.LOADED )
596             changeState( com.sun.star.embed.EmbedStates.RUNNING );
597 
598         UpdateSizeAndGetFromActive();
599 
600         return new com.sun.star.awt.Size( (int)m_aObjSize.getWidth(), (int)m_aObjSize.getHeight() );
601     }
602 
603 
getPreferredVisualRepresentation(long nAspect)604     public VisualRepresentation getPreferredVisualRepresentation(long nAspect) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.embed.WrongStateException, com.sun.star.uno.Exception
605     {
606         if ( m_bDisposed )
607             throw new com.sun.star.lang.DisposedException();
608 
609         if ( m_nObjectState == -1 )
610             throw new com.sun.star.embed.WrongStateException();
611 
612         if ( nAspect == com.sun.star.embed.Aspects.MSOLE_ICON )
613             // the ICON aspect should be handled by the container
614             throw new com.sun.star.embed.WrongStateException();
615 
616         if ( m_nObjectState == com.sun.star.embed.EmbedStates.LOADED )
617             changeState( com.sun.star.embed.EmbedStates.RUNNING );
618 
619         byte[] aData = m_aEditorFrame.getReplacementImage();
620         VisualRepresentation aVisRep = new VisualRepresentation();
621         aVisRep.Data = aData;
622         aVisRep.Flavor = new com.sun.star.datatransfer.DataFlavor( "image/png", "png", new com.sun.star.uno.Type( byte[].class ) );
623         return aVisRep;
624     }
625 
626 
getMapUnit(long nAspect)627     public int getMapUnit(long nAspect) throws com.sun.star.uno.Exception
628     {
629         if ( m_bDisposed )
630             throw new com.sun.star.lang.DisposedException();
631 
632         if ( m_nObjectState == -1 )
633             throw new com.sun.star.embed.WrongStateException();
634 
635         if ( nAspect == com.sun.star.embed.Aspects.MSOLE_ICON )
636             // the ICON aspect should be handled by the container
637             throw new com.sun.star.embed.WrongStateException();
638 
639         return com.sun.star.embed.EmbedMapUnits.PIXEL;
640     }
641 
642     // com.sun.star.embed.XClassifiedObject:
643 
getClassID()644     public byte[] getClassID()
645     {
646         if ( m_bDisposed )
647             throw new com.sun.star.lang.DisposedException();
648 
649         return m_aClassID;
650     }
651 
652 
getClassName()653     public String getClassName()
654     {
655         if ( m_bDisposed )
656             throw new com.sun.star.lang.DisposedException();
657 
658         return "";
659     }
660 
661 
setClassInfo(byte[] aClassID, String sClassName)662     public void setClassInfo(byte[] aClassID, String sClassName) throws com.sun.star.lang.NoSupportException
663     {
664         throw new com.sun.star.lang.NoSupportException();
665     }
666 
667     // com.sun.star.embed.XComponentSupplier:
668 
getComponent()669     public com.sun.star.util.XCloseable getComponent()
670     {
671         if ( m_bDisposed )
672             throw new com.sun.star.lang.DisposedException();
673 
674         // allows no access to the component, this simple example just has no
675         return null;
676     }
677 
678     // com.sun.star.embed.XStateChangeBroadcaster:
679 
addStateChangeListener(com.sun.star.embed.XStateChangeListener xListener)680     public void addStateChangeListener(com.sun.star.embed.XStateChangeListener xListener)
681     {
682         if ( m_bDisposed )
683             return;
684 
685         GetListeners().add( xListener );
686     }
687 
688 
removeStateChangeListener(com.sun.star.embed.XStateChangeListener xListener)689     public void removeStateChangeListener(com.sun.star.embed.XStateChangeListener xListener)
690     {
691         if ( m_bDisposed )
692             return;
693 
694         if ( m_aListeners != null )
695             m_aListeners.remove( xListener );
696     }
697 
698     // com.sun.star.document.XEventBroadcaster:
699 
addEventListener(com.sun.star.document.XEventListener xListener)700     public void addEventListener(com.sun.star.document.XEventListener xListener)
701     {
702         if ( m_bDisposed )
703             return;
704 
705         GetListeners().add( xListener );
706     }
707 
708 
removeEventListener(com.sun.star.document.XEventListener xListener)709     public void removeEventListener(com.sun.star.document.XEventListener xListener)
710     {
711         if ( m_bDisposed )
712             return;
713 
714         if ( m_aListeners != null )
715             m_aListeners.remove( xListener );
716     }
717 
718     // com.sun.star.util.XCloseBroadcaster:
719 
addCloseListener(com.sun.star.util.XCloseListener xListener)720     public void addCloseListener(com.sun.star.util.XCloseListener xListener)
721     {
722         if ( m_bDisposed )
723             return;
724 
725         GetListeners().add( xListener );
726     }
727 
728 
removeCloseListener(com.sun.star.util.XCloseListener xListener)729     public void removeCloseListener(com.sun.star.util.XCloseListener xListener)
730     {
731         if ( m_bDisposed )
732             return;
733 
734         if ( m_aListeners != null )
735             m_aListeners.remove( xListener );
736     }
737 
738     // com.sun.star.util.XCloseable:
739 
close(boolean bDeliverOwnership)740     public void close(boolean bDeliverOwnership) throws com.sun.star.util.CloseVetoException
741     {
742         if ( m_bDisposed )
743             throw new com.sun.star.lang.DisposedException();
744 
745         com.sun.star.lang.EventObject aEventObject = new com.sun.star.lang.EventObject( this );
746 
747         if ( m_aListeners != null )
748         {
749             for ( int nInd = 0; nInd < m_aListeners.size(); nInd++ )
750             {
751                 try
752                 {
753                     com.sun.star.util.XCloseListener xListener = ( com.sun.star.util.XCloseListener )
754                         UnoRuntime.queryInterface( com.sun.star.document.XEventListener.class, m_aListeners.get( nInd ) );
755 
756                     if ( xListener != null )
757                         xListener.queryClosing( aEventObject, bDeliverOwnership );
758                 }
759                 catch( com.sun.star.util.CloseVetoException e )
760                 {
761                     throw e;
762                 }
763                 catch( java.lang.Exception e )
764                 {
765                     m_aListeners.remove( nInd );
766                 }
767             }
768 
769             m_bDisposed = true;
770 
771             for ( int nInd = 0; nInd < m_aListeners.size(); nInd++ )
772             {
773                 try
774                 {
775                     com.sun.star.util.XCloseListener xListener = ( com.sun.star.util.XCloseListener )
776                         UnoRuntime.queryInterface( com.sun.star.document.XEventListener.class, m_aListeners.get( nInd ) );
777 
778                     if ( xListener != null )
779                         xListener.notifyClosing( aEventObject );
780                 }
781                 catch( java.lang.Exception e )
782                 {
783                     m_aListeners.remove( nInd );
784                 }
785             }
786 
787             m_aListeners.clear();
788         }
789 
790         m_bDisposed = true;
791 
792         if ( m_aEditorFrame != null )
793         {
794             m_aEditorFrame.dispose();
795             m_aEditorFrame = null;
796         }
797 
798         if ( m_xOwnStorage != null )
799         {
800             try {
801                 m_xOwnStorage.dispose();
802             } catch( java.lang.Exception e ) {}
803             m_xOwnStorage = null;
804         }
805     }
806 
807     // com.sun.star.embed.XEmbeddedObject:
808 
changeState(int nNewState)809     public void changeState(int nNewState) throws com.sun.star.embed.UnreachableStateException, com.sun.star.embed.WrongStateException, com.sun.star.uno.Exception
810     {
811         if ( m_bDisposed )
812             throw new com.sun.star.lang.DisposedException();
813 
814         if ( m_nObjectState == -1 )
815             throw new com.sun.star.embed.WrongStateException();
816 
817         int nOldState = m_nObjectState;
818 
819         if ( nOldState == nNewState )
820         {
821             if ( nOldState == com.sun.star.embed.EmbedStates.ACTIVE )
822             {
823                 if ( m_aEditorFrame == null )
824                     throw new com.sun.star.uno.RuntimeException();
825                 m_aEditorFrame.toFront();
826             }
827 
828             return;
829         }
830 
831         if ( nNewState != com.sun.star.embed.EmbedStates.LOADED
832           && nNewState != com.sun.star.embed.EmbedStates.RUNNING
833           && nNewState != com.sun.star.embed.EmbedStates.ACTIVE )
834             throw new com.sun.star.embed.UnreachableStateException();
835 
836         StateChangeNotification( true, nOldState, nNewState );
837 
838         try
839         {
840             if ( nOldState == com.sun.star.embed.EmbedStates.LOADED )
841             {
842                 // switch to the RUNNING state first
843                 String aText = ReadStringFromStream( m_xOwnStorage, "content.txt" );
844 
845                 EditorFrame aEditorFrame = new EditorFrame( m_aEntryName, this, 5, 20 );
846                 aEditorFrame.setText( aText );
847 
848                 ReadSizeFromOwnStorage();
849 
850                 m_aEditorFrame = aEditorFrame;
851                 m_nObjectState = com.sun.star.embed.EmbedStates.RUNNING;
852 
853                 if ( nNewState == com.sun.star.embed.EmbedStates.ACTIVE )
854                 {
855                     if ( m_xClient == null )
856                         throw new com.sun.star.embed.WrongStateException();
857 
858                     m_aEditorFrame.show();
859                     m_aEditorFrame.toFront();
860 
861                     if ( m_aObjSize != null )
862                         aEditorFrame.setAppSize( m_aObjSize );
863 
864                     m_xClient.visibilityChanged( true );
865                     m_nObjectState = com.sun.star.embed.EmbedStates.ACTIVE;
866                 }
867             }
868             else if ( nOldState == com.sun.star.embed.EmbedStates.RUNNING )
869             {
870                 if ( nNewState == com.sun.star.embed.EmbedStates.LOADED )
871                 {
872                     EditorFrame aFrame = m_aEditorFrame;
873                     m_aEditorFrame = null;
874                     aFrame.dispose();
875                     m_nObjectState = nNewState;
876                 }
877                 else // nNewState == ACTIVE
878                 {
879                     if ( m_aEditorFrame == null )
880                         throw new com.sun.star.uno.RuntimeException();
881 
882                     if ( m_xClient == null )
883                         throw new com.sun.star.embed.WrongStateException();
884 
885                     m_aEditorFrame.show();
886                     m_aEditorFrame.toFront();
887 
888                     if ( m_aObjSize != null )
889                         m_aEditorFrame.setAppSize( m_aObjSize );
890 
891                     m_xClient.visibilityChanged( true );
892 
893                     m_nObjectState = nNewState;
894                 }
895             }
896             else // nOldState == ACTIVE
897             {
898                 UpdateSizeAndGetFromActive();
899                 if ( nNewState == com.sun.star.embed.EmbedStates.RUNNING )
900                 {
901                     m_aEditorFrame.hide();
902                     m_nObjectState = nNewState;
903                 }
904                 else // nNewState == LOADED
905                 {
906                     EditorFrame aFrame = m_aEditorFrame;
907                     m_aEditorFrame = null;
908                     aFrame.dispose();
909                     m_nObjectState = nNewState;
910                 }
911             }
912         }
913         catch( com.sun.star.uno.Exception e )
914         {
915             if ( nOldState != m_nObjectState )
916                 StateChangeNotification( false, nOldState, m_nObjectState );
917             throw e;
918         }
919 
920         StateChangeNotification( true, nOldState, nNewState );
921     }
922 
923 
getReachableStates()924     public int[] getReachableStates() throws com.sun.star.embed.NeedsRunningStateException, com.sun.star.embed.WrongStateException
925     {
926         if ( m_bDisposed )
927             throw new com.sun.star.lang.DisposedException();
928 
929         if ( m_nObjectState == -1 )
930             throw new com.sun.star.embed.WrongStateException();
931 
932         int[] pStates = new int[3];
933         pStates[0] = com.sun.star.embed.EmbedStates.LOADED;
934         pStates[1] = com.sun.star.embed.EmbedStates.RUNNING;
935         pStates[2] = com.sun.star.embed.EmbedStates.ACTIVE;
936 
937         return pStates;
938     }
939 
940 
getCurrentState()941     public int getCurrentState() throws com.sun.star.embed.WrongStateException
942     {
943         if ( m_bDisposed )
944             throw new com.sun.star.lang.DisposedException();
945 
946         if ( m_nObjectState == -1 )
947             throw new com.sun.star.embed.WrongStateException();
948 
949         return m_nObjectState;
950     }
951 
952 
doVerb(int nVerbID)953     public void doVerb(int nVerbID) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.embed.WrongStateException, com.sun.star.embed.UnreachableStateException, com.sun.star.uno.Exception
954     {
955         if ( m_bDisposed )
956             throw new com.sun.star.lang.DisposedException();
957 
958         if ( m_nObjectState == -1 )
959             throw new com.sun.star.embed.WrongStateException();
960 
961         if ( nVerbID == com.sun.star.embed.EmbedVerbs.MS_OLEVERB_PRIMARY
962           || nVerbID == com.sun.star.embed.EmbedVerbs.MS_OLEVERB_SHOW
963           || nVerbID == com.sun.star.embed.EmbedVerbs.MS_OLEVERB_OPEN )
964             changeState( com.sun.star.embed.EmbedStates.ACTIVE );
965         else if ( nVerbID == com.sun.star.embed.EmbedVerbs.MS_OLEVERB_HIDE )
966             changeState( com.sun.star.embed.EmbedStates.RUNNING );
967     }
968 
969 
getSupportedVerbs()970     public com.sun.star.embed.VerbDescriptor[] getSupportedVerbs() throws com.sun.star.embed.NeedsRunningStateException, com.sun.star.embed.WrongStateException
971     {
972         if ( m_bDisposed )
973             throw new com.sun.star.lang.DisposedException();
974 
975         if ( m_nObjectState == -1 )
976             throw new com.sun.star.embed.WrongStateException();
977 
978         if ( m_pOwnVerbs == null )
979         {
980             try
981             {
982                 XMultiServiceFactory xConfProvider = theDefaultProvider.get(m_xContext);
983                 if ( xConfProvider == null )
984                     throw new com.sun.star.uno.RuntimeException();
985 
986                 Object[] aArgs = new Object[1];
987                 aArgs[0] = "/org.openoffice.Office.Embedding/Objects";
988                 Object oSettings = xConfProvider.createInstanceWithArguments( "com.sun.star.configuration.ConfigurationAccess", aArgs );
989                 XNameAccess xObjConfNA = UnoRuntime.queryInterface( XNameAccess.class, oSettings );
990                 if ( xObjConfNA == null )
991                     throw new com.sun.star.uno.RuntimeException();
992 
993                 Object oEmbObj = xObjConfNA.getByName( "69474366-FD6F-4806-8374-8EDD1B6E771D" );
994                 XNameAccess xEmbObjNA = UnoRuntime.queryInterface( XNameAccess.class, oEmbObj );
995                 if ( xEmbObjNA == null )
996                     throw new com.sun.star.uno.RuntimeException();
997 
998                 String[] pVerbShortcuts = (String[]) AnyConverter.toArray( xEmbObjNA.getByName( "ObjectVerbs" ) );
999                 if ( pVerbShortcuts != null && pVerbShortcuts.length != 0 )
1000                 {
1001                     com.sun.star.embed.VerbDescriptor[] pVerbs = new com.sun.star.embed.VerbDescriptor[pVerbShortcuts.length];
1002                        aArgs[0] = "/org.openoffice.Office.Embedding/Verbs";
1003                        Object oVerbs = xConfProvider.createInstanceWithArguments( "com.sun.star.configuration.ConfigurationAccess", aArgs );
1004                        XNameAccess xVerbsConfNA = UnoRuntime.queryInterface( XNameAccess.class, oVerbs );
1005                        if ( xVerbsConfNA == null )
1006                         throw new com.sun.star.uno.RuntimeException();
1007 
1008                     for ( int nInd = 0; nInd < pVerbShortcuts.length; nInd++ )
1009                     {
1010                         try
1011                         {
1012                             XNameAccess xVerbNA = UnoRuntime.queryInterface(
1013                                                                 XNameAccess.class,
1014                                                                 xVerbsConfNA.getByName( pVerbShortcuts[nInd] ) );
1015                             if ( xVerbNA != null )
1016                             {
1017                                 com.sun.star.embed.VerbDescriptor aVerb = new com.sun.star.embed.VerbDescriptor();
1018                                 aVerb.VerbID = AnyConverter.toInt( xVerbNA.getByName( "VerbID" ) );
1019                                 aVerb.VerbName = AnyConverter.toString( xVerbNA.getByName( "VerbUIName" ) );
1020                                 aVerb.VerbFlags = AnyConverter.toInt( xVerbNA.getByName( "VerbFlags" ) );
1021                                 aVerb.VerbAttributes = AnyConverter.toInt( xVerbNA.getByName( "VerbAttributes" ) );
1022                                 pVerbs[nInd] = aVerb;
1023                             }
1024                         }
1025                         catch( java.lang.Exception e )
1026                         {
1027                         }
1028 
1029                         if ( pVerbs[nInd] == null )
1030                         {
1031                             // let the error be visible
1032                             pVerbs[nInd] = new com.sun.star.embed.VerbDescriptor();
1033                             pVerbs[nInd].VerbID = com.sun.star.embed.EmbedVerbs.MS_OLEVERB_PRIMARY;
1034                             pVerbs[nInd].VerbName = "ERROR!";
1035                             pVerbs[nInd].VerbFlags = 0;
1036                             pVerbs[nInd].VerbAttributes = com.sun.star.embed.VerbAttributes.MS_VERBATTR_ONCONTAINERMENU;
1037                         }
1038                     }
1039 
1040                     m_pOwnVerbs = pVerbs;
1041                 }
1042             }
1043             catch( com.sun.star.uno.Exception e )
1044             {}
1045         }
1046 
1047         if ( m_pOwnVerbs != null )
1048             return m_pOwnVerbs;
1049 
1050         return new com.sun.star.embed.VerbDescriptor[0];
1051     }
1052 
1053 
setClientSite(com.sun.star.embed.XEmbeddedClient xClient)1054     public void setClientSite(com.sun.star.embed.XEmbeddedClient xClient) throws com.sun.star.embed.WrongStateException
1055     {
1056         if ( m_bDisposed )
1057             throw new com.sun.star.lang.DisposedException();
1058 
1059         if ( m_nObjectState == -1 )
1060             throw new com.sun.star.embed.WrongStateException();
1061 
1062         m_xClient = xClient;
1063     }
1064 
1065 
getClientSite()1066     public com.sun.star.embed.XEmbeddedClient getClientSite() throws com.sun.star.embed.WrongStateException
1067     {
1068         if ( m_bDisposed )
1069             throw new com.sun.star.lang.DisposedException();
1070 
1071         if ( m_nObjectState == -1 )
1072             throw new com.sun.star.embed.WrongStateException();
1073 
1074         return m_xClient;
1075     }
1076 
1077 
update()1078     public void update() throws com.sun.star.embed.WrongStateException, com.sun.star.uno.Exception
1079     {
1080         if ( m_bDisposed )
1081             throw new com.sun.star.lang.DisposedException();
1082 
1083         if ( m_nObjectState == -1 )
1084             throw new com.sun.star.embed.WrongStateException();
1085 
1086         // not implemented
1087     }
1088 
1089 
setUpdateMode(int nMode)1090     public void setUpdateMode(int nMode) throws com.sun.star.embed.WrongStateException
1091     {
1092         if ( m_bDisposed )
1093             throw new com.sun.star.lang.DisposedException();
1094 
1095         if ( m_nObjectState == -1 )
1096             throw new com.sun.star.embed.WrongStateException();
1097 
1098         // not implemented
1099     }
1100 
1101 
getStatus(long nAspect)1102     public long getStatus(long nAspect) throws com.sun.star.embed.WrongStateException
1103     {
1104         if ( m_bDisposed )
1105             throw new com.sun.star.lang.DisposedException();
1106 
1107         if ( m_nObjectState == -1 )
1108             throw new com.sun.star.embed.WrongStateException();
1109 
1110         return 0;
1111     }
1112 
1113 
setContainerName(String sName)1114     public void setContainerName(String sName)
1115     {
1116         if ( m_bDisposed )
1117             throw new com.sun.star.lang.DisposedException();
1118 
1119         // not implemented
1120     }
1121 }
1122 
1123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
1124