1 package org.openoffice.examples.embedding; 2 3 import com.sun.star.uno.UnoRuntime; 4 import com.sun.star.uno.XComponentContext; 5 import com.sun.star.lang.XMultiComponentFactory; 6 import com.sun.star.lang.XMultiServiceFactory; 7 import com.sun.star.uno.AnyConverter; 8 import com.sun.star.lib.uno.helper.WeakBase; 9 import com.sun.star.io.XStream; 10 import com.sun.star.io.XOutputStream; 11 import com.sun.star.io.XInputStream; 12 import com.sun.star.io.XTruncate; 13 import com.sun.star.beans.XPropertySet; 14 import com.sun.star.lang.XComponent; 15 import com.sun.star.document.EventObject; 16 import com.sun.star.embed.VisualRepresentation; 17 import com.sun.star.container.XNameAccess; 18 19 20 import com.sun.star.embed.*; 21 22 import java.util.Vector; 23 import java.awt.Dimension; 24 import java.lang.Integer; 25 import org.omg.CORBA.COMM_FAILURE; 26 27 import org.openoffice.examples.embedding.EditorFrame; 28 29 public final class OwnEmbeddedObject extends WeakBase 30 implements com.sun.star.embed.XEmbedPersist, 31 com.sun.star.embed.XEmbeddedObject 32 { 33 protected final XComponentContext m_xContext; 34 protected final byte[] m_aClassID; 35 36 protected boolean m_bDisposed = false; 37 protected int m_nObjectState = -1; 38 39 protected com.sun.star.embed.XStorage m_xParentStorage; 40 protected com.sun.star.embed.XStorage m_xOwnStorage; 41 protected String m_aEntryName; 42 43 protected com.sun.star.embed.XStorage m_xNewParentStorage; 44 protected com.sun.star.embed.XStorage m_xNewOwnStorage; 45 protected String m_aNewEntryName; 46 protected boolean m_bWaitSaveCompleted = false; 47 48 protected EditorFrame m_aEditorFrame; 49 50 protected Vector m_aListeners; 51 52 com.sun.star.embed.VerbDescriptor[] m_pOwnVerbs; 53 54 com.sun.star.embed.XEmbeddedClient m_xClient; 55 56 Dimension m_aObjSize; 57 58 // ------------------------------------------------------------- 59 protected Vector GetListeners() 60 { 61 if ( m_aListeners == null ) 62 m_aListeners = new Vector<Object>( 10, 10 ); 63 64 return m_aListeners; 65 } 66 67 // ------------------------------------------------------------- 68 protected Dimension UpdateSizeAndGetFromActive() 69 { 70 if ( m_nObjectState == com.sun.star.embed.EmbedStates.ACTIVE ) 71 m_aObjSize = m_aEditorFrame.getAppSize(); 72 73 if ( m_aObjSize != null ) 74 return m_aObjSize; 75 else 76 return new Dimension(); 77 } 78 79 // ------------------------------------------------------------- 80 protected void SwitchOwnPersistence( XStorage xParentStorage, XStorage xOwnStorage, String aEntryName ) 81 { 82 if ( xOwnStorage != m_xOwnStorage ) 83 { 84 if ( m_xOwnStorage != null ) 85 m_xOwnStorage.dispose(); 86 m_xParentStorage = xParentStorage; 87 m_xOwnStorage = xOwnStorage; 88 m_aEntryName = aEntryName; 89 } 90 } 91 92 // ------------------------------------------------------------- 93 protected void SwitchOwnPersistence( XStorage xParentStorage, String aEntryName ) throws com.sun.star.io.IOException 94 { 95 if ( xParentStorage != m_xParentStorage || !aEntryName.equals( m_aEntryName ) ) 96 { 97 try 98 { 99 XStorage xOwnStorage = xParentStorage.openStorageElement( aEntryName, com.sun.star.embed.ElementModes.READWRITE ); 100 SwitchOwnPersistence( xParentStorage, xOwnStorage, aEntryName ); 101 } 102 catch( com.sun.star.uno.RuntimeException e ) 103 { 104 throw e; 105 } 106 catch( com.sun.star.io.IOException e ) 107 { 108 throw e; 109 } 110 catch( com.sun.star.uno.Exception e ) 111 { 112 throw new com.sun.star.io.IOException( "Error while switching object storage!" ); 113 } 114 } 115 } 116 117 // ------------------------------------------------------------- 118 protected static void SaveDataToStorage( XStorage xStorage, String aString, Dimension aDimension ) throws com.sun.star.io.IOException 119 { 120 try 121 { 122 // save the text 123 XStream xStream = xStorage.openStreamElement( "content.txt", com.sun.star.embed.ElementModes.READWRITE ); 124 XComponent xStreamComp = ( XComponent ) UnoRuntime.queryInterface( XComponent.class, xStream ); 125 if ( xStreamComp == null ) 126 throw new com.sun.star.uno.RuntimeException(); 127 128 XOutputStream xOutStream = xStream.getOutputStream(); 129 XTruncate xTruncate = ( XTruncate ) UnoRuntime.queryInterface( XTruncate.class, xOutStream ); 130 if ( xTruncate == null ) 131 throw new com.sun.star.io.IOException(); 132 133 xTruncate.truncate(); 134 xOutStream.writeBytes( aString.getBytes() ); 135 136 // save the size 137 xStream = xStorage.openStreamElement( "properties.txt", com.sun.star.embed.ElementModes.READWRITE ); 138 xStreamComp = ( XComponent ) UnoRuntime.queryInterface( XComponent.class, xStream ); 139 if ( xStreamComp == null ) 140 throw new com.sun.star.uno.RuntimeException(); 141 142 xOutStream = xStream.getOutputStream(); 143 xTruncate = ( XTruncate ) UnoRuntime.queryInterface( XTruncate.class, xOutStream ); 144 if ( xTruncate == null ) 145 throw new com.sun.star.io.IOException(); 146 147 xTruncate.truncate(); 148 String aProps = Integer.toString( (int)aDimension.getWidth() ) + "|" + Integer.toString( (int)aDimension.getHeight() ); 149 xOutStream.writeBytes( aProps.getBytes() ); 150 151 // set the media type 152 XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, xStorage ); 153 if ( xPropSet == null ) 154 throw new com.sun.star.uno.RuntimeException(); 155 xPropSet.setPropertyValue( "MediaType", "application/x-openoffice-embedded-69474366-FD6F-4806-8374-8EDD1B6E771D" ); 156 157 XTransactedObject xTransact = ( XTransactedObject ) UnoRuntime.queryInterface( XTransactedObject.class, xStorage ); 158 if ( xTransact != null ) 159 xTransact.commit(); 160 161 xStreamComp.dispose(); 162 } 163 catch( com.sun.star.uno.RuntimeException e ) 164 { 165 throw e; 166 } 167 catch( com.sun.star.io.IOException e ) 168 { 169 throw e; 170 } 171 catch( com.sun.star.uno.Exception e ) 172 { 173 throw new com.sun.star.io.IOException( "Error while switching object storage!" ); 174 } 175 } 176 177 // ------------------------------------------------------------- 178 protected void PostEvent( String aEvEntryName ) 179 { 180 if ( m_aListeners != null ) 181 { 182 com.sun.star.document.EventObject aEventObject = new com.sun.star.document.EventObject( this, aEvEntryName ); 183 for ( int nInd = 0; nInd < m_aListeners.size(); nInd++ ) 184 { 185 try 186 { 187 com.sun.star.document.XEventListener xListener = ( com.sun.star.document.XEventListener ) 188 UnoRuntime.queryInterface( com.sun.star.document.XEventListener.class, m_aListeners.get( nInd ) ); 189 190 if ( xListener != null ) 191 xListener.notifyEvent( aEventObject ); 192 } 193 catch( java.lang.Exception e ) 194 { 195 m_aListeners.remove( nInd ); 196 } 197 } 198 } 199 } 200 201 // ------------------------------------------------------------- 202 protected void StateChangeNotification( boolean bBeforeChange, int nOldState, int nNewState ) 203 { 204 if ( m_aListeners != null ) 205 { 206 com.sun.star.lang.EventObject aEventObject = new com.sun.star.lang.EventObject( this ); 207 for ( int nInd = 0; nInd < m_aListeners.size(); nInd++ ) 208 { 209 try 210 { 211 com.sun.star.embed.XStateChangeListener xListener = ( com.sun.star.embed.XStateChangeListener ) 212 UnoRuntime.queryInterface( com.sun.star.embed.XStateChangeListener.class, m_aListeners.get( nInd ) ); 213 214 if ( xListener != null ) 215 { 216 if ( bBeforeChange ) 217 xListener.changingState( aEventObject, nOldState, nNewState ); 218 else 219 xListener.stateChanged( aEventObject, nOldState, nNewState ); 220 } 221 } 222 catch( java.lang.Exception e ) 223 { 224 m_aListeners.remove( nInd ); 225 } 226 } 227 } 228 } 229 230 // ------------------------------------------------------------- 231 protected String ReadStringFromStream( XStorage xStorage, String aStreamName ) throws com.sun.star.io.IOException 232 { 233 if ( xStorage == null ) 234 throw new com.sun.star.uno.RuntimeException(); 235 236 try 237 { 238 XStream xStream = xStorage.openStreamElement( aStreamName, com.sun.star.embed.ElementModes.READWRITE ); 239 XComponent xStreamComp = ( XComponent ) UnoRuntime.queryInterface( XComponent.class, xStream ); 240 if ( xStreamComp == null ) 241 throw new com.sun.star.uno.RuntimeException(); 242 243 XInputStream xInStream = xStream.getInputStream(); 244 byte[][] aData = new byte[1][]; 245 aData[0] = new byte[0]; 246 String aResult = new String(); 247 248 int nLen = 0; 249 do 250 { 251 nLen = xInStream.readBytes( aData, 10 ); 252 if ( aData.length == 0 || aData[0] == null ) 253 throw new com.sun.star.io.IOException(); 254 aResult += new String( aData[0] ); 255 } while( nLen > 0 ); 256 257 xStreamComp.dispose(); 258 259 return aResult; 260 } 261 catch( com.sun.star.uno.RuntimeException e ) 262 { 263 throw e; 264 } 265 catch( com.sun.star.io.IOException e ) 266 { 267 throw e; 268 } 269 catch( com.sun.star.uno.Exception e ) 270 { 271 throw new com.sun.star.io.IOException( "Error while reading one of object streams!" ); 272 } 273 } 274 275 // ------------------------------------------------------------- 276 protected void ReadSizeFromOwnStorage() throws com.sun.star.io.IOException 277 { 278 String aSize = ReadStringFromStream( m_xOwnStorage, "properties.txt" ); 279 280 int nSeparator = aSize.indexOf( '|' ); 281 if ( nSeparator > 0 && nSeparator < aSize.length() - 1 ) 282 { 283 int nWidth = Integer.parseInt( aSize.substring( 0, nSeparator ) ); 284 int nHeight = Integer.parseInt( aSize.substring( nSeparator + 1, aSize.length() ) ); 285 m_aObjSize = new Dimension( nWidth, nHeight ); 286 } 287 } 288 289 // ------------------------------------------------------------- 290 public OwnEmbeddedObject( XComponentContext context, byte[] aClassID ) 291 { 292 m_xContext = context; 293 m_aClassID = aClassID; 294 }; 295 296 // ------------------------------------------------------------- 297 public void CloseFrameRequest() 298 { 299 com.sun.star.embed.XEmbeddedClient xClient = m_xClient; 300 if ( xClient == null ) 301 return; 302 303 UpdateSizeAndGetFromActive(); 304 StateChangeNotification( true, com.sun.star.embed.EmbedStates.ACTIVE, com.sun.star.embed.EmbedStates.RUNNING ); 305 306 try{ 307 xClient.visibilityChanged( false ); 308 } catch( com.sun.star.uno.Exception e ){} 309 310 try{ 311 xClient.saveObject(); 312 } catch( com.sun.star.uno.Exception e ){} 313 314 m_nObjectState = com.sun.star.embed.EmbedStates.RUNNING; 315 StateChangeNotification( false, com.sun.star.embed.EmbedStates.ACTIVE, m_nObjectState ); 316 317 PostEvent( "OnVisAreaChanged" ); 318 } 319 320 // com.sun.star.embed.XCommonEmbedPersist: 321 // ------------------------------------------------------------- 322 public void storeOwn() throws com.sun.star.embed.WrongStateException, com.sun.star.io.IOException, com.sun.star.uno.Exception 323 { 324 if ( m_bDisposed ) 325 throw new com.sun.star.lang.DisposedException(); 326 327 if ( m_nObjectState == -1 || m_bWaitSaveCompleted ) 328 throw new com.sun.star.embed.WrongStateException(); 329 330 // nothing to do, if the object is in loaded state 331 if ( m_nObjectState == com.sun.star.embed.EmbedStates.LOADED ) 332 return; 333 334 if ( m_xOwnStorage == null ) 335 throw new com.sun.star.io.IOException(); 336 337 PostEvent( "OnSave" ); 338 339 if ( m_aEditorFrame == null ) 340 throw new com.sun.star.uno.RuntimeException(); 341 342 SaveDataToStorage( m_xOwnStorage, m_aEditorFrame.getText(), UpdateSizeAndGetFromActive() ); 343 344 PostEvent( "OnSaveDone" ); 345 } 346 347 // ------------------------------------------------------------- 348 public boolean isReadonly() throws com.sun.star.embed.WrongStateException 349 { 350 return false; 351 } 352 353 // ------------------------------------------------------------- 354 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 355 { 356 // not implemented currently 357 return; 358 } 359 360 // com.sun.star.embed.XEmbedPersist: 361 // ------------------------------------------------------------- 362 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 363 { 364 if ( m_bDisposed ) 365 throw new com.sun.star.lang.DisposedException(); 366 367 if ( xStorage == null || aEntryName.length() == 0 ) 368 throw new com.sun.star.lang.IllegalArgumentException(); 369 370 if ( ( m_nObjectState != -1 || nEntryConnectionMode == com.sun.star.embed.EntryInitModes.NO_INIT ) 371 && ( m_nObjectState == -1 || nEntryConnectionMode != com.sun.star.embed.EntryInitModes.NO_INIT ) ) 372 { 373 // if the object is not loaded 374 // it can not get persistant representation without initialization 375 376 // if the object is loaded 377 // it can switch persistant representation only without initialization 378 379 throw new com.sun.star.embed.WrongStateException(); 380 } 381 382 if ( m_bWaitSaveCompleted ) 383 { 384 if ( nEntryConnectionMode == com.sun.star.embed.EntryInitModes.NO_INIT ) 385 { 386 if ( m_xParentStorage == xStorage && m_aEntryName.equals( aEntryName ) ) 387 saveCompleted( false ); 388 else if ( m_xNewParentStorage == xStorage && m_aNewEntryName.equals( aEntryName ) ) 389 saveCompleted( true ); 390 else 391 throw new com.sun.star.embed.WrongStateException(); 392 } 393 else 394 throw new com.sun.star.embed.WrongStateException(); 395 396 return; 397 } 398 399 boolean bElExists = xStorage.hasByName( aEntryName ); 400 401 if ( nEntryConnectionMode == com.sun.star.embed.EntryInitModes.DEFAULT_INIT ) 402 { 403 SwitchOwnPersistence( xStorage, aEntryName ); 404 if ( bElExists ) 405 { 406 XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, m_xOwnStorage ); 407 if ( xPropSet == null ) 408 throw new com.sun.star.uno.RuntimeException(); 409 410 String aMediaType = AnyConverter.toString( xPropSet.getPropertyValue( "MediaType" ) ); 411 if ( !aMediaType.equals( "application/x-openoffice-embedded-69474366-FD6F-4806-8374-8EDD1B6E771D" ) ) 412 throw new com.sun.star.lang.IllegalArgumentException(); 413 414 m_nObjectState = com.sun.star.embed.EmbedStates.LOADED; 415 } 416 else 417 { 418 m_aEditorFrame = new EditorFrame( m_aEntryName, this, 5, 20 ); 419 m_nObjectState = com.sun.star.embed.EmbedStates.RUNNING; 420 m_aObjSize = m_aEditorFrame.getAppSize(); 421 } 422 } 423 else if ( nEntryConnectionMode == com.sun.star.embed.EntryInitModes.TRUNCATE_INIT ) 424 { 425 SwitchOwnPersistence( xStorage, aEntryName ); 426 m_aEditorFrame = new EditorFrame( m_aEntryName, this, 5, 20 ); 427 m_nObjectState = com.sun.star.embed.EmbedStates.RUNNING; 428 m_aObjSize = m_aEditorFrame.getAppSize(); 429 } 430 else 431 throw new com.sun.star.lang.IllegalArgumentException(); 432 } 433 434 // ------------------------------------------------------------- 435 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 436 { 437 if ( m_bDisposed ) 438 throw new com.sun.star.lang.DisposedException(); 439 440 if ( m_nObjectState == -1 || m_bWaitSaveCompleted ) 441 throw new com.sun.star.embed.WrongStateException(); 442 443 if ( m_nObjectState == com.sun.star.embed.EmbedStates.LOADED ) 444 { 445 m_xParentStorage.copyElementTo( m_aEntryName, xStorage, aEntryName ); 446 } 447 else 448 { 449 com.sun.star.embed.XStorage xSubStorage = 450 xStorage.openStorageElement( aEntryName, 451 com.sun.star.embed.ElementModes.READWRITE | com.sun.star.embed.ElementModes.TRUNCATE ); 452 453 String aContents = m_aEditorFrame.getText(); 454 455 SaveDataToStorage( xSubStorage, m_aEditorFrame.getText(), UpdateSizeAndGetFromActive() ); 456 } 457 } 458 459 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 653 public String getClassName() 654 { 655 if ( m_bDisposed ) 656 throw new com.sun.star.lang.DisposedException(); 657 658 return new String(); 659 } 660 661 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 // ------------------------------------------------------------- 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 XMultiComponentFactory xFactory = m_xContext.getServiceManager(); 983 Object obj = xFactory.createInstanceWithContext( "com.sun.star.configuration.ConfigurationProvider", m_xContext ); 984 XMultiServiceFactory xConfProvider = (XMultiServiceFactory) UnoRuntime.queryInterface( XMultiServiceFactory.class, obj ); 985 if ( xConfProvider == null ) 986 throw new com.sun.star.uno.RuntimeException(); 987 988 Object[] aArgs = new Object[1]; 989 aArgs[0] = "/org.openoffice.Office.Embedding/Objects"; 990 Object oSettings = xConfProvider.createInstanceWithArguments( "com.sun.star.configuration.ConfigurationAccess", aArgs ); 991 XNameAccess xObjConfNA = ( XNameAccess ) UnoRuntime.queryInterface( XNameAccess.class, oSettings ); 992 if ( xObjConfNA == null ) 993 throw new com.sun.star.uno.RuntimeException(); 994 995 Object oEmbObj = xObjConfNA.getByName( "69474366-FD6F-4806-8374-8EDD1B6E771D" ); 996 XNameAccess xEmbObjNA = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, oEmbObj ); 997 if ( xEmbObjNA == null ) 998 throw new com.sun.star.uno.RuntimeException(); 999 1000 String[] pVerbShortcuts = (String[]) AnyConverter.toArray( xEmbObjNA.getByName( "ObjectVerbs" ) ); 1001 if ( pVerbShortcuts != null && pVerbShortcuts.length != 0 ) 1002 { 1003 com.sun.star.embed.VerbDescriptor[] pVerbs = new com.sun.star.embed.VerbDescriptor[pVerbShortcuts.length]; 1004 aArgs[0] = "/org.openoffice.Office.Embedding/Verbs"; 1005 Object oVerbs = xConfProvider.createInstanceWithArguments( "com.sun.star.configuration.ConfigurationAccess", aArgs ); 1006 XNameAccess xVerbsConfNA = ( XNameAccess ) UnoRuntime.queryInterface( XNameAccess.class, oVerbs ); 1007 if ( xVerbsConfNA == null ) 1008 throw new com.sun.star.uno.RuntimeException(); 1009 1010 for ( int nInd = 0; nInd < pVerbShortcuts.length; nInd++ ) 1011 { 1012 try 1013 { 1014 XNameAccess xVerbNA = (XNameAccess) UnoRuntime.queryInterface( 1015 XNameAccess.class, 1016 xVerbsConfNA.getByName( pVerbShortcuts[nInd] ) ); 1017 if ( xVerbNA != null ) 1018 { 1019 com.sun.star.embed.VerbDescriptor aVerb = new com.sun.star.embed.VerbDescriptor(); 1020 aVerb.VerbID = AnyConverter.toInt( xVerbNA.getByName( "VerbID" ) ); 1021 aVerb.VerbName = AnyConverter.toString( xVerbNA.getByName( "VerbUIName" ) ); 1022 aVerb.VerbFlags = AnyConverter.toInt( xVerbNA.getByName( "VerbFlags" ) ); 1023 aVerb.VerbAttributes = AnyConverter.toInt( xVerbNA.getByName( "VerbAttributes" ) ); 1024 pVerbs[nInd] = aVerb; 1025 } 1026 } 1027 catch( java.lang.Exception e ) 1028 { 1029 } 1030 1031 if ( pVerbs[nInd] == null ) 1032 { 1033 // let the error be visible 1034 pVerbs[nInd] = new com.sun.star.embed.VerbDescriptor(); 1035 pVerbs[nInd].VerbID = com.sun.star.embed.EmbedVerbs.MS_OLEVERB_PRIMARY; 1036 pVerbs[nInd].VerbName = "ERROR!"; 1037 pVerbs[nInd].VerbFlags = 0; 1038 pVerbs[nInd].VerbAttributes = com.sun.star.embed.VerbAttributes.MS_VERBATTR_ONCONTAINERMENU; 1039 } 1040 } 1041 1042 m_pOwnVerbs = pVerbs; 1043 } 1044 } 1045 catch( com.sun.star.uno.Exception e ) 1046 {} 1047 } 1048 1049 if ( m_pOwnVerbs != null ) 1050 return m_pOwnVerbs; 1051 1052 return new com.sun.star.embed.VerbDescriptor[0]; 1053 } 1054 1055 // ------------------------------------------------------------- 1056 public void setClientSite(com.sun.star.embed.XEmbeddedClient xClient) throws com.sun.star.embed.WrongStateException 1057 { 1058 if ( m_bDisposed ) 1059 throw new com.sun.star.lang.DisposedException(); 1060 1061 if ( m_nObjectState == -1 ) 1062 throw new com.sun.star.embed.WrongStateException(); 1063 1064 m_xClient = xClient; 1065 } 1066 1067 // ------------------------------------------------------------- 1068 public com.sun.star.embed.XEmbeddedClient getClientSite() throws com.sun.star.embed.WrongStateException 1069 { 1070 if ( m_bDisposed ) 1071 throw new com.sun.star.lang.DisposedException(); 1072 1073 if ( m_nObjectState == -1 ) 1074 throw new com.sun.star.embed.WrongStateException(); 1075 1076 return m_xClient; 1077 } 1078 1079 // ------------------------------------------------------------- 1080 public void update() throws com.sun.star.embed.WrongStateException, com.sun.star.uno.Exception 1081 { 1082 if ( m_bDisposed ) 1083 throw new com.sun.star.lang.DisposedException(); 1084 1085 if ( m_nObjectState == -1 ) 1086 throw new com.sun.star.embed.WrongStateException(); 1087 1088 // not implemented 1089 } 1090 1091 // ------------------------------------------------------------- 1092 public void setUpdateMode(int nMode) throws com.sun.star.embed.WrongStateException 1093 { 1094 if ( m_bDisposed ) 1095 throw new com.sun.star.lang.DisposedException(); 1096 1097 if ( m_nObjectState == -1 ) 1098 throw new com.sun.star.embed.WrongStateException(); 1099 1100 // not implemented 1101 } 1102 1103 // ------------------------------------------------------------- 1104 public long getStatus(long nAspect) throws com.sun.star.embed.WrongStateException 1105 { 1106 if ( m_bDisposed ) 1107 throw new com.sun.star.lang.DisposedException(); 1108 1109 if ( m_nObjectState == -1 ) 1110 throw new com.sun.star.embed.WrongStateException(); 1111 1112 return 0; 1113 } 1114 1115 // ------------------------------------------------------------- 1116 public void setContainerName(String sName) 1117 { 1118 if ( m_bDisposed ) 1119 throw new com.sun.star.lang.DisposedException(); 1120 1121 // not implemented 1122 } 1123 } 1124 1125