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