merged tag ooo/OOO330_m14
[LibreOffice.git] / embeddedobj / test / Container1 / EmbedContApp.java
blob5fdc9f14cf63a7aa15faa8c42833a461550209d9
1 package embeddedobj.test;
3 import java.awt.*;
4 import java.applet.*;
5 import java.awt.event.*;
6 import java.net.*;
7 import java.io.*;
8 import java.util.Vector;
10 import javax.swing.JOptionPane;
11 import javax.swing.Timer;
13 import com.sun.star.lang.XMultiServiceFactory;
14 import com.sun.star.lang.XSingleServiceFactory;
16 import com.sun.star.uno.UnoRuntime;
17 import com.sun.star.uno.XInterface;
18 import com.sun.star.uno.AnyConverter;
19 import com.sun.star.uno.Type;
20 import com.sun.star.uno.Any;
22 import com.sun.star.lang.XComponent;
24 import com.sun.star.util.XCloseable;
25 import com.sun.star.util.XURLTransformer;
26 import com.sun.star.util.URL;
28 import com.sun.star.beans.PropertyValue;
29 import com.sun.star.beans.NamedValue;
31 import com.sun.star.datatransfer.DataFlavor;
32 import com.sun.star.datatransfer.XTransferable;
34 import com.sun.star.container.XNameAccess;
36 import com.sun.star.io.XStream;
37 import com.sun.star.io.XInputStream;
38 import com.sun.star.io.XOutputStream;
39 import com.sun.star.io.XTruncate;
41 import com.sun.star.awt.XWindow;
42 import com.sun.star.awt.XBitmap;
44 import com.sun.star.task.XJob;
46 import com.sun.star.embed.*;
49 class ActionObject
51 public byte m_nID;
52 public String m_sParam;
54 public ActionObject()
56 m_nID = 0;
57 m_sParam = null;
60 public ActionObject( byte nID )
62 m_nID = nID;
63 m_sParam = null;
66 public ActionObject( byte nID, String sParam )
68 m_nID = nID;
69 m_sParam = sParam;
72 public ActionObject( ActionObject aObject )
74 m_nID = aObject.m_nID;
75 m_sParam = aObject.m_sParam;
79 public class EmbedContApp extends Applet
80 implements MouseListener, XEmbeddedClient, ActionListener, XJob, XInplaceClient, XWindowSupplier
82 private XMultiServiceFactory m_xServiceFactory;
84 private final boolean m_bStoreVisRepl = false;
86 private XJob m_xMainThreadExecutor;
87 private NamedValue[] m_pValuesForExecutor;
89 private XEmbeddedObject m_xEmbedObj;
90 private XStorage m_xStorage;
91 private float m_nXScaling;
92 private float m_nYScaling;
93 private float m_nXPixelSize;
94 private float m_nYPixelSize;
96 private Frame m_aFrame;
97 private Menu m_aFileMenu;
98 private Menu m_aObjectMenu;
99 private Toolkit m_aToolkit;
101 private Image m_aImage;
102 private Object m_oImageLock;
104 private boolean m_bOwnFile = false;
106 private boolean m_bLinkObj = false;
107 private String m_aLinkURI;
109 private Object m_oActionsNumberLock;
110 private Vector m_aActionsList;
112 private Timer m_aTimer;
113 private boolean m_bDestroyed = false;
115 private Object m_oInHandlerLock;
116 private boolean m_bInHandler = false;
118 private XURLTransformer m_xTransformer;
120 private NativeView m_aNativeView;
121 private XWindow m_xVCLWindow;
123 private XBitmap m_xBitmap;
124 private BitmapPainter m_aBitmapPainter;
126 // Constants
127 private final byte DESTROY = 1;
128 private final byte ACTIVATE_OUTPLACE = 2;
129 private final byte NEW_DOCUMENT = 3;
130 private final byte SAVE_AS = 4;
131 private final byte OPEN_FILE = 5;
132 private final byte SAVE = 6;
133 private final byte NEW_OBJECT = 7;
134 private final byte OBJECT_FROM_FILE = 8;
135 private final byte LINK_FROM_FILE = 9;
136 private final byte CONVERT_LINK_TO_OBJECT = 10;
137 private final byte ACTIVATE_INPLACE = 11;
138 private final byte DEACTIVATE = 12;
140 // Methods
141 public EmbedContApp( Frame aFrame, XMultiServiceFactory xServiceFactory )
143 m_aFrame = aFrame;
144 m_xServiceFactory = xServiceFactory;
147 public void init()
149 resize( 800, 600 );
150 setBackground( Color.gray );
152 m_aToolkit = Toolkit.getDefaultToolkit();
154 try {
155 Object oTransformer = m_xServiceFactory.createInstance( "com.sun.star.util.URLTransformer" );
156 m_xTransformer = (XURLTransformer)UnoRuntime.queryInterface( XURLTransformer.class, oTransformer );
157 } catch( Exception e ) { System.exit( 0 ); }
159 m_oActionsNumberLock = new Object();
160 m_aActionsList = new Vector();
162 m_oInHandlerLock = new Object();
163 m_oImageLock = new Object();
165 try {
166 Object oJob = m_xServiceFactory.createInstance( "com.sun.star.comp.thread.MainThreadExecutor" );
167 m_xMainThreadExecutor = (XJob)UnoRuntime.queryInterface( XJob.class, oJob );
168 } catch( Exception e ) {}
170 if ( m_xMainThreadExecutor == null )
172 System.out.println( "Can't create MainThreadExecutor! The application is unusable!" );
173 System.exit( 0 );
176 m_nXScaling = 1;
177 m_nYScaling = 1;
178 m_nXPixelSize = 1;
179 m_nYPixelSize = 1;
181 m_pValuesForExecutor = new NamedValue[1];
182 m_pValuesForExecutor[0] = new NamedValue( "JobToExecute", (Object)this );
184 m_aTimer = new Timer( 100, this );
185 m_aTimer.start();
187 // Get a menu bar.
188 MenuBar aMenuBar = m_aFrame.getMenuBar();
189 if( aMenuBar == null )
191 aMenuBar = new MenuBar();
192 m_aFrame.setMenuBar( aMenuBar );
195 // Create menus for the menu bar.
197 // File menu
198 m_aFileMenu = new Menu( "File", true );
199 aMenuBar.add( m_aFileMenu );
201 MenuItem aItem = new NewMenuItem();
202 m_aFileMenu.add( aItem );
204 aItem = new OpenFileMenuItem();
205 m_aFileMenu.add( aItem );
207 aItem = new SaveMenuItem();
208 m_aFileMenu.add( aItem );
210 aItem = new SaveAsMenuItem();
211 m_aFileMenu.add( aItem );
213 // Object menu
214 m_aObjectMenu = new Menu( "Object", true );
215 aMenuBar.add( m_aObjectMenu );
217 aItem = new NewObjectMenuItem();
218 m_aObjectMenu.add( aItem );
220 aItem = new LoadObjectMenuItem();
221 m_aObjectMenu.add( aItem );
223 aItem = new LinkObjectMenuItem();
224 m_aObjectMenu.add( aItem );
226 aItem = new ConvertLinkToEmbedMenuItem();
227 m_aObjectMenu.add( aItem );
229 // Activation menu
230 m_aObjectMenu = new Menu( "Activation", true );
231 aMenuBar.add( m_aObjectMenu );
233 aItem = new ActivateOutplaceMenuItem();
234 m_aObjectMenu.add( aItem );
236 aItem = new ActivateInplaceMenuItem();
237 m_aObjectMenu.add( aItem );
239 aItem = new DeactivateMenuItem();
240 m_aObjectMenu.add( aItem );
242 m_aNativeView = new NativeView();
243 m_aNativeView.resize( 800, 600 );
244 this.add( m_aNativeView );
246 // Handle mouse clicks in our window.
247 // addMouseListener( this );
250 public void actionPerformed( ActionEvent evt )
252 synchronized( m_oInHandlerLock )
254 if ( m_bInHandler )
255 return;
256 m_bInHandler = true;
259 synchronized( m_oActionsNumberLock )
261 if ( m_aActionsList.size() > 0 )
263 try {
264 m_xMainThreadExecutor.execute( m_pValuesForExecutor );
266 catch( Exception e )
268 System.out.println( "Exception in actionPerformed() : " + e );
271 else
273 synchronized( m_oInHandlerLock )
275 m_bInHandler = false;
281 // XWindowSupplier
282 public XWindow getWindow()
284 return m_xVCLWindow;
287 // XEmbeddedClient
288 public void saveObject()
289 throws com.sun.star.uno.Exception
291 if ( m_xEmbedObj != null )
293 try {
294 XEmbedPersist xPersist = (XEmbedPersist)UnoRuntime.queryInterface( XEmbedPersist.class, m_xEmbedObj );
295 if ( xPersist != null )
297 xPersist.storeOwn();
298 generateNewImage();
300 else
301 JOptionPane.showMessageDialog( m_aFrame, "No XEmbedPersist!", "Error:", JOptionPane.ERROR_MESSAGE );
303 catch( Exception e )
305 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in saveObject:", JOptionPane.ERROR_MESSAGE );
309 generateNewImage();
310 repaint();
313 public void onShowWindow( boolean bVisible )
315 // for now nothing to do
318 // XInplaceClient
319 public boolean canInplaceActivate()
321 return true;
324 public void onInplaceActivate()
326 // TODO
327 // prepare for inplace activation
329 // REMOVE
330 // workaround for CLIPCHILDREN problem
331 if ( m_aBitmapPainter != null )
332 m_aBitmapPainter.stopPainting();
335 public void onUIActivate()
337 // TODO
338 // prepare for UI activate
341 public void onInplaceDeactivate()
343 // TODO
344 // inplace deactivation is done
346 // REMOVE
347 // workaround for CLIPCHILDREN problem
348 if ( m_aBitmapPainter != null )
349 m_aBitmapPainter.startPainting();
352 public void onUIDeactivate()
354 // TODO
355 // prepare for UI deactivate
358 public XIPMainContainerWindow getTopmostWindow()
360 // TODO
361 // return an implementation of XIPMainContainerWindow
362 // mainly required for ui activation
363 // dummy implementation is enough for inplace activation
365 return null;
368 public XInplaceUIWindow getDocumentWindow()
370 // TODO
371 // return implementation of XInplaceUIWindow
372 // mainly required for ui activation
373 // dummy implementation is enough for inplace activation
375 return null;
378 public com.sun.star.awt.Rectangle getPosRect()
380 // provide position rectangle to the object
381 try {
382 // here object bitmap and scaling factor hold the size
383 com.sun.star.awt.Size aBitmapSize = m_xBitmap.getSize();
384 com.sun.star.awt.Size aVisSize = new com.sun.star.awt.Size(
385 (int)( aBitmapSize.Width * m_nXScaling ),
386 (int)( aBitmapSize.Height * m_nYScaling ) );
387 return new com.sun.star.awt.Rectangle( 10, 10, aVisSize.Width, aVisSize.Height );
389 catch( Exception e )
391 System.out.println( "Position rectangle generation failed!" );
394 return new com.sun.star.awt.Rectangle( 10, 10, 110, 110 );
397 public com.sun.star.awt.Rectangle getClipRect()
399 // provide clip rectangle to the object
400 // in this application position and clip rectangles are the same
402 try {
403 // here object bitmap and scaling factor hold the size
404 com.sun.star.awt.Size aBitmapSize = m_xBitmap.getSize();
405 com.sun.star.awt.Size aVisSize = new com.sun.star.awt.Size(
406 (int)( aBitmapSize.Width * m_nXScaling ),
407 (int)( aBitmapSize.Height * m_nYScaling ) );
408 return new com.sun.star.awt.Rectangle( 10, 10, aVisSize.Width, aVisSize.Height );
410 catch( Exception e )
412 System.out.println( "Clip rectangle generation failed!" );
415 return new com.sun.star.awt.Rectangle( 10, 10, 110, 110 );
418 public void translateAccelerators( com.sun.star.awt.KeyEvent[] aKeys )
420 // TODO
421 // an accelerator table for object
422 // ui activation related
425 public void scrollObj( com.sun.star.awt.Size aOffset )
427 // TODO
428 // scrolls the object to a specified offset
429 // not mandatory for the testing application :)
432 public void onPosRectChange( com.sun.star.awt.Rectangle aPosRect )
434 // object asks to change the position
435 if ( m_xEmbedObj != null )
437 try {
438 int nState = m_xEmbedObj.getCurrentState();
439 // such a position change make sence only when object is
440 // either inplace or ui active
441 if ( nState == EmbedStates.EMBED_INPLACE_ACTIVE
442 || nState == EmbedStates.EMBED_UI_ACTIVE )
444 XInplaceObject xInplObj = (XInplaceObject)UnoRuntime.queryInterface( XInplaceObject.class, m_xEmbedObj );
445 if ( xInplObj != null )
447 xInplObj.setObjectRects( aPosRect, aPosRect ); // show the whole object
448 if ( m_aBitmapPainter != null )
449 m_aBitmapPainter.setRect( aPosRect );
451 else
452 System.out.println( "Why object that does not support inplace activation behave like inplace object?!" );
454 else
455 System.out.println( "The object is not active but asks to change visual area!" );
456 } catch( Exception e )
458 System.out.println( "Exception is thrown in onPosRectChange: " + e );
461 else
462 System.out.println( "Who asks to change visual area?!!" );
465 // XJob
466 public Object execute( NamedValue[] pValues )
468 for( int nInd = 0; nInd < m_aActionsList.size(); nInd++ )
470 ActionObject aAction = ( ActionObject ) m_aActionsList.get( nInd );
471 if ( aAction != null )
473 if ( aAction.m_nID == DESTROY )
475 // free all resources
476 clearObjectAndStorage();
477 m_bDestroyed = true;
479 else if ( aAction.m_nID == ACTIVATE_OUTPLACE )
481 // activate object if exists and not active
482 if ( m_xEmbedObj != null )
484 try {
485 m_xEmbedObj.changeState( EmbedStates.EMBED_ACTIVE );
487 catch( Exception ex )
489 System.out.println( "Exception on mouse click" + ex );
493 else if ( aAction.m_nID == NEW_DOCUMENT )
495 // clear everything
496 clearObjectAndStorage();
498 repaint();
500 else if ( aAction.m_nID == SAVE_AS )
502 // open SaveAs dialog and store
504 if ( m_xStorage != null && m_xEmbedObj != null )
506 try {
508 if ( m_bLinkObj )
509 storeLinkAsFileURI( aFileURI );
510 else
512 saveObjectAsFileURI( aAction.m_sParam );
514 catch( Exception ex )
516 System.out.println( "Exception in SaveAsMenuItem: " + ex );
520 else if ( aAction.m_nID == OPEN_FILE )
522 // clear everything
523 clearObjectAndStorage();
525 // load from specified file
526 loadFileURI( aAction.m_sParam );
528 if ( m_xEmbedObj != null )
530 try {
531 m_xEmbedObj.setClientSite( this );
533 catch( Exception ex )
535 System.out.println( "Exception in OpenFileMenuItem: " + ex );
539 generateNewImage();
540 repaint();
542 else if ( aAction.m_nID == SAVE )
544 if ( m_xStorage != null && m_xEmbedObj != null )
546 // if has persistance store there
547 // if not it is and error, SaveAs had to be used
549 if ( m_bOwnFile )
551 if ( m_xStorage != null )
553 try {
554 saveObject();
556 if ( m_bLinkObj )
557 storeLinkToStorage();
559 XTransactedObject xTransact = (XTransactedObject)UnoRuntime.queryInterface( XTransactedObject.class,
560 m_xStorage );
561 if ( xTransact != null )
562 xTransact.commit();
564 catch( Exception ex )
566 System.out.println( "Exception during save operation in SaveMenuItem:" + ex );
569 else
571 System.out.println( "No storage for owned file!" );
574 else
576 System.out.println( "No owned file!" );
580 else if ( aAction.m_nID == NEW_OBJECT )
582 // remove current object an init a new one
583 clearObjectAndStorage();
585 if ( aAction.m_sParam != null )
587 m_xStorage = createTempStorage();
589 if ( m_xStorage != null )
590 m_xEmbedObj = createEmbedObject( aAction.m_sParam );
591 else
592 System.out.println( "Can't create temporary storage!" );
594 if ( m_xEmbedObj != null )
596 try {
597 m_xEmbedObj.setClientSite( this );
599 catch( Exception ex )
601 System.out.println( "Exception in NewObjectMenuItem:" + ex );
606 generateNewImage();
607 repaint();
609 else if ( aAction.m_nID == OBJECT_FROM_FILE )
611 // first remove current object
612 clearObjectAndStorage();
614 // create object from specified file
615 m_xStorage = createTempStorage();
617 if ( m_xStorage != null )
618 m_xEmbedObj = loadEmbedObject( aAction.m_sParam );
620 if ( m_xEmbedObj != null )
622 try {
623 m_xEmbedObj.setClientSite( this );
625 catch( Exception ex )
627 System.out.println( "Exception in LoadObjectMenuItem: " + ex );
631 generateNewImage();
632 repaint();
634 else if ( aAction.m_nID == LINK_FROM_FILE )
636 // first remove current object
637 clearObjectAndStorage();
639 m_xStorage = createTempStorage();
641 // create object from specified file
642 m_xEmbedObj = createLinkObject( aAction.m_sParam );
644 if ( m_xEmbedObj != null )
646 m_aLinkURI = aAction.m_sParam;
647 m_bLinkObj = true;
649 try {
650 m_xEmbedObj.setClientSite( this );
652 catch( Exception ex )
654 System.out.println( "Exception in LinkObjectMenuItem:" + ex );
658 generateNewImage();
659 repaint();
661 else if ( aAction.m_nID == CONVERT_LINK_TO_OBJECT )
663 if ( !m_bLinkObj )
665 System.out.println( "The object is not a link!" );
666 continue;
669 if ( m_xEmbedObj != null )
671 if ( m_xStorage != null )
673 try {
674 XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface( XNameAccess.class,
675 m_xStorage );
676 if ( xNameAccess != null && xNameAccess.hasByName( "LinkName" ) )
677 m_xStorage.removeElement( "LinkName" );
679 XLinkageSupport xLinkage = (XLinkageSupport)UnoRuntime.queryInterface( XLinkageSupport.class,
680 m_xEmbedObj );
681 if ( xLinkage != null )
683 xLinkage.breakLink( m_xStorage, "EmbedSub" );
684 m_bLinkObj = false;
685 m_aLinkURI = null;
687 else
688 System.out.println( "No XLinkageSupport in ConvertLink... !" );
690 catch( Exception e1 )
692 System.out.println( "Exception in ConvertLinkToEmbed:try 1 :" + e1 );
697 else if ( aAction.m_nID == ACTIVATE_INPLACE )
699 // activate object
700 if ( m_xEmbedObj != null )
702 // in general it is better to check acceptable states
703 try {
704 m_xEmbedObj.changeState( EmbedStates.EMBED_INPLACE_ACTIVE );
706 catch( Exception ex )
708 System.out.println( "Exception on inplace activation " + ex );
712 else if ( aAction.m_nID == DEACTIVATE )
714 // activate object
716 if ( m_xEmbedObj != null )
718 int nOldState = -1;
719 try {
720 nOldState = m_xEmbedObj.getCurrentState();
721 } catch( Exception e )
724 if ( nOldState == EmbedStates.EMBED_ACTIVE
725 || nOldState == EmbedStates.EMBED_INPLACE_ACTIVE
726 || nOldState == EmbedStates.EMBED_UI_ACTIVE )
728 try {
729 m_xEmbedObj.changeState( EmbedStates.EMBED_RUNNING );
731 catch( Exception ex )
733 System.out.println( "Exception on inplace activation " + ex );
736 else
738 System.out.println( "Deactivation of nonactive object!" );
742 else
744 System.out.println( "Unknoun action is requested: " + aAction.m_nID + "\n" );
749 m_aActionsList.clear();
751 synchronized( m_oInHandlerLock )
753 m_bInHandler = false;
756 return Any.VOID;
759 public void actionRegister( byte nActionID, String sParam )
761 synchronized( m_oActionsNumberLock )
763 int nSize = m_aActionsList.size();
764 if ( nSize < 199 )
766 if ( nSize == 0 )
767 m_aActionsList.add( new ActionObject( nActionID, sParam ) );
768 else
770 ActionObject aAction = ( ActionObject ) m_aActionsList.get( nSize - 1 );
771 if ( aAction != null && aAction.m_nID != DESTROY )
772 m_aActionsList.add( new ActionObject( nActionID, sParam ) );
778 public void SaveAsOperation()
780 if ( m_xStorage != null && m_xEmbedObj != null )
782 FileDialog aFileDialog = new FileDialog( m_aFrame, "SaveAs", FileDialog.SAVE );
783 aFileDialog.show();
784 if ( aFileDialog.getFile() != null )
786 String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
787 File aFile = new File( aFileName );
788 if ( aFile != null )
790 // create object from specified file
791 String aFileURI = getValidURL( aFile.toURI().toASCIIString() );
792 actionRegister( SAVE_AS, aFileURI );
796 else
797 JOptionPane.showMessageDialog( m_aFrame, "No document is embedded!", "Error:", JOptionPane.ERROR_MESSAGE );
801 public void destroy()
803 // redirect the call through the timer and call super.destroy();
804 actionRegister( DESTROY, null );
806 for ( int i = 0; i < 3 && !m_bDestroyed; i++ )
808 try {
809 Thread.sleep( 200 );
810 } catch ( Exception e )
814 if ( !m_bDestroyed )
815 System.out.println( "The object application can not exit correctly!" );
817 m_aTimer.stop();
819 super.destroy();
822 public void update( Graphics g )
824 paint( g );
827 public void paint( Graphics g )
829 super.paint( g );
831 // m_aNativeView.paint( g );
833 createVclWindow();
836 public void createVclWindow()
838 synchronized( m_oImageLock )
840 if ( m_xVCLWindow == null && m_xServiceFactory != null && m_xEmbedObj != null && m_xBitmap != null )
842 java.awt.Rectangle aBounds = getBounds();
843 m_xVCLWindow = WindowHelper.createWindow( m_xServiceFactory, m_aNativeView, aBounds );
844 m_xVCLWindow.setVisible( true );
846 com.sun.star.awt.Size aBitmapSize = new com.sun.star.awt.Size( 200, 100 );
848 XVisualObject xVisObj = (XVisualObject)UnoRuntime.queryInterface( XVisualObject.class, m_xEmbedObj );
849 try {
850 com.sun.star.awt.Size aVisSize = xVisObj.getVisAreaSize( Aspects.MSASPECT_CONTENT );
851 m_nXPixelSize = aVisSize.Width / aBitmapSize.Width;
852 m_nYPixelSize = aVisSize.Height / aBitmapSize.Height;
854 catch( Exception e )
858 if ( m_xBitmap != null )
859 aBitmapSize = m_xBitmap.getSize();
861 System.out.println( "The visual area is Width = " + aBitmapSize.Width + "; Height = " + aBitmapSize.Height );
863 com.sun.star.awt.Rectangle aRect = new com.sun.star.awt.Rectangle(
866 Math.min( (int)aBounds.getWidth() - 20, aBitmapSize.Width ),
867 Math.min( (int)aBounds.getHeight() - 20, aBitmapSize.Height ) );
869 m_aBitmapPainter = new BitmapPainter( m_xMainThreadExecutor, m_xVCLWindow, m_xBitmap, aRect );
874 public void generateNewImage()
876 if ( m_xEmbedObj != null )
878 try {
879 int nOldState = m_xEmbedObj.getCurrentState();
880 int nState = nOldState;
881 if ( nOldState == EmbedStates.EMBED_LOADED )
883 m_xEmbedObj.changeState( EmbedStates.EMBED_RUNNING );
884 nState = EmbedStates.EMBED_RUNNING;
887 if ( nState == EmbedStates.EMBED_UI_ACTIVE || nState == EmbedStates.EMBED_INPLACE_ACTIVE
888 || nState == EmbedStates.EMBED_ACTIVE || nState == EmbedStates.EMBED_RUNNING )
890 XComponentSupplier xCompProv = (XComponentSupplier)UnoRuntime.queryInterface(
891 XComponentSupplier.class,
892 m_xEmbedObj );
893 if ( xCompProv != null )
895 XCloseable xCloseable = xCompProv.getComponent();
896 XTransferable xTransfer = (XTransferable)UnoRuntime.queryInterface(
897 XTransferable.class,
898 xCloseable );
899 if ( xTransfer != null )
901 DataFlavor aFlavor = new DataFlavor();
902 aFlavor.MimeType = "application/x-openoffice;windows_formatname=\"Bitmap\"";
903 aFlavor.HumanPresentableName = "Bitmap";
904 aFlavor.DataType = new Type( byte[].class );
906 Object aAny = xTransfer.getTransferData( aFlavor );
907 if ( aAny != null && AnyConverter.isArray( aAny ) )
909 synchronized( m_oImageLock )
911 m_xBitmap = WindowHelper.getVCLBitmapFromBytes( m_xServiceFactory, aAny );
912 if ( m_aBitmapPainter != null )
914 m_aBitmapPainter.setBitmap( m_xBitmap );
916 if ( m_xBitmap != null )
918 try {
919 com.sun.star.awt.Size aBitmapSize = m_xBitmap.getSize();
920 com.sun.star.awt.Size aVisSize = new com.sun.star.awt.Size(
921 (int)( aBitmapSize.Width * m_nXScaling ),
922 (int)( aBitmapSize.Height * m_nYScaling ) );
923 m_aBitmapPainter.setSize( aVisSize );
925 catch( Exception e )
933 else
934 System.out.println( "paint() : can not get XTransferable for the component!\n" );
936 else
937 System.out.println( "paint() : XComponentSupplier is not implemented!\n" );
940 catch( com.sun.star.uno.Exception e )
942 // dialogs should not be used in paint()
943 System.out.println( "Exception in paint(): " + e );
948 public void mouseClicked( MouseEvent e )
950 if( e.getModifiers() == InputEvent.BUTTON1_MASK )
952 actionRegister( ACTIVATE_OUTPLACE, null );
956 public void mousePressed( MouseEvent e ){};
957 public void mouseEntered( MouseEvent e ){};
958 public void mouseExited( MouseEvent e ){};
959 public void mouseReleased( MouseEvent e ){};
961 // classes
962 class NewMenuItem extends MenuItem implements ActionListener // Menu New
964 public NewMenuItem()
966 super( "New", new MenuShortcut( KeyEvent.VK_A ));
967 addActionListener( this );
970 public void actionPerformed( ActionEvent e )
972 actionRegister( NEW_DOCUMENT, null );
976 class SaveAsMenuItem extends MenuItem implements ActionListener // Menu SaveAs...
978 public SaveAsMenuItem()
980 super( "SaveAs..." );
981 addActionListener( this );
984 public void actionPerformed( ActionEvent e )
986 // open SaveAs dialog and store
988 SaveAsOperation();
992 class OpenFileMenuItem extends MenuItem implements ActionListener // Menu Open
994 public OpenFileMenuItem()
996 super( "Open", new MenuShortcut( KeyEvent.VK_C ));
997 addActionListener( this );
1000 public void actionPerformed( ActionEvent e )
1002 // open OpenFile dialog and load doc
1003 FileDialog aFileDialog = new FileDialog( m_aFrame, "Open" );
1004 aFileDialog.show();
1005 if ( aFileDialog.getFile() != null )
1007 String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
1008 File aFile = new File( aFileName );
1009 if ( aFile != null )
1011 // create object from specified file
1012 String aFileURI = getValidURL( aFile.toURI().toASCIIString() );
1013 actionRegister( OPEN_FILE, aFileURI );
1019 class SaveMenuItem extends MenuItem implements ActionListener // Menu Save
1021 public SaveMenuItem()
1023 super( "Save", new MenuShortcut( KeyEvent.VK_D ));
1024 addActionListener( this );
1027 public void actionPerformed( ActionEvent e )
1029 // if has persistance store there
1030 // if not open SaveAs dialog and store
1031 if ( m_xStorage != null && m_xEmbedObj != null )
1033 if ( m_bOwnFile )
1035 if ( m_xStorage == null )
1037 JOptionPane.showMessageDialog( m_aFrame,
1038 "No storage for oned file!",
1039 "Error:",
1040 JOptionPane.ERROR_MESSAGE );
1042 return;
1045 actionRegister( SAVE, null );
1047 else
1049 SaveAsOperation();
1052 else
1053 JOptionPane.showMessageDialog( m_aFrame, "No document is embedded!", "Error:", JOptionPane.ERROR_MESSAGE );
1057 class NewObjectMenuItem extends MenuItem implements ActionListener // Menu NewObject
1059 public NewObjectMenuItem()
1061 super( "Create", new MenuShortcut( KeyEvent.VK_N ));
1062 addActionListener( this );
1065 public void actionPerformed( ActionEvent e )
1067 Object[] possibleValues = { "com.sun.star.comp.Writer.TextDocument",
1068 "com.sun.star.comp.Writer.GlobalDocument",
1069 "com.sun.star.comp.Writer.WebDocument",
1070 "com.sun.star.comp.Calc.SpreadsheetDocument",
1071 "com.sun.star.comp.Draw.PresentationDocument",
1072 "com.sun.star.comp.Draw.DrawingDocument",
1073 "com.sun.star.comp.Math.FormulaDocument",
1074 "BitmapImage" };
1076 String selectedValue = (String)JOptionPane.showInputDialog( null, "DocumentType", "Select",
1077 JOptionPane.INFORMATION_MESSAGE, null,
1078 possibleValues, possibleValues[0] );
1080 actionRegister( NEW_OBJECT, selectedValue );
1084 class LoadObjectMenuItem extends MenuItem implements ActionListener // Menu LoadObject
1086 public LoadObjectMenuItem()
1088 super( "Load from file", new MenuShortcut( KeyEvent.VK_L ));
1089 addActionListener( this );
1092 public void actionPerformed( ActionEvent e )
1094 // open OpenFile dialog and load doc
1095 FileDialog aFileDialog = new FileDialog( m_aFrame, "Select sources to use for object init" );
1096 aFileDialog.show();
1097 if ( aFileDialog.getFile() != null )
1099 String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
1100 File aFile = new File( aFileName );
1101 if ( aFile != null )
1103 // create object from specified file
1104 String aFileURI = getValidURL( aFile.toURI().toASCIIString() );
1105 actionRegister( OBJECT_FROM_FILE, aFileURI );
1111 class LinkObjectMenuItem extends MenuItem implements ActionListener // Menu LinkObject
1113 public LinkObjectMenuItem()
1115 super( "Create link", new MenuShortcut( KeyEvent.VK_M ));
1116 addActionListener( this );
1119 public void actionPerformed( ActionEvent e )
1121 // open OpenFile dialog and load doc
1122 FileDialog aFileDialog = new FileDialog( m_aFrame, "Select sources to use for object init" );
1123 aFileDialog.show();
1124 if ( aFileDialog.getFile() != null )
1126 String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
1127 File aFile = new File( aFileName );
1128 if ( aFile != null )
1130 // create object from specified file
1131 String aFileURI = getValidURL( aFile.toURI().toASCIIString() );
1132 actionRegister( LINK_FROM_FILE, aFileURI );
1138 class ConvertLinkToEmbedMenuItem extends MenuItem implements ActionListener // Menu LinkObject
1140 public ConvertLinkToEmbedMenuItem()
1142 super( "Convert link to embed", new MenuShortcut( KeyEvent.VK_M ));
1143 addActionListener( this );
1146 public void actionPerformed( ActionEvent e )
1148 actionRegister( CONVERT_LINK_TO_OBJECT, null );
1152 class ActivateOutplaceMenuItem extends MenuItem implements ActionListener // Menu ActiveteOutplace
1154 public ActivateOutplaceMenuItem()
1156 super( "Activate outplace", new MenuShortcut( KeyEvent.VK_A ));
1157 addActionListener( this );
1160 public void actionPerformed( ActionEvent e )
1162 actionRegister( ACTIVATE_OUTPLACE, null );
1166 class ActivateInplaceMenuItem extends MenuItem implements ActionListener // Menu ActivateInplace
1168 public ActivateInplaceMenuItem()
1170 super( "Activate inplace", new MenuShortcut( KeyEvent.VK_I ));
1171 addActionListener( this );
1174 public void actionPerformed( ActionEvent e )
1176 actionRegister( ACTIVATE_INPLACE, null );
1180 class DeactivateMenuItem extends MenuItem implements ActionListener // Menu Deactivate
1182 public DeactivateMenuItem()
1184 super( "Deactivate", new MenuShortcut( KeyEvent.VK_D ));
1185 addActionListener( this );
1188 public void actionPerformed( ActionEvent e )
1190 actionRegister( DEACTIVATE, null );
1194 // Helper methods
1195 public XEmbeddedObject createEmbedObject( String aServiceName )
1197 XEmbeddedObject xEmbObj = null;
1198 byte[] pClassID = new byte[16];
1200 if ( aServiceName.equals( "com.sun.star.comp.Writer.TextDocument" ) )
1202 int[] pTempClassID = { 0x8B, 0xC6, 0xB1, 0x65, 0xB1, 0xB2, 0x4E, 0xDD,
1203 0xAA, 0x47, 0xDA, 0xE2, 0xEE, 0x68, 0x9D, 0xD6 };
1204 for ( int ind = 0; ind < 16; ind++ )
1205 pClassID[ind] = (byte)pTempClassID[ind];
1207 else if ( aServiceName.equals( "com.sun.star.comp.Writer.GlobalDocument" ) )
1209 int[] pTempClassID = { 0xB2, 0x1A, 0x0A, 0x7C, 0xE4, 0x03, 0x41, 0xFE,
1210 0x95, 0x62, 0xBD, 0x13, 0xEA, 0x6F, 0x15, 0xA0 };
1211 for ( int ind = 0; ind < 16; ind++ )
1212 pClassID[ind] = (byte)pTempClassID[ind];
1214 else if ( aServiceName.equals( "com.sun.star.comp.Writer.WebDocument" ) )
1216 int[] pTempClassID = { 0xA8, 0xBB, 0xA6, 0x0C, 0x7C, 0x60, 0x45, 0x50,
1217 0x91, 0xCE, 0x39, 0xC3, 0x90, 0x3F, 0xAC, 0x5E };
1218 for ( int ind = 0; ind < 16; ind++ )
1219 pClassID[ind] = (byte)pTempClassID[ind];
1221 else if ( aServiceName.equals( "com.sun.star.comp.Calc.SpreadsheetDocument" ) )
1223 int[] pTempClassID = { 0x47, 0xBB, 0xB4, 0xCB, 0xCE, 0x4C, 0x4E, 0x80,
1224 0xA5, 0x91, 0x42, 0xD9, 0xAE, 0x74, 0x95, 0x0F };
1225 for ( int ind = 0; ind < 16; ind++ )
1226 pClassID[ind] = (byte)pTempClassID[ind];
1228 else if ( aServiceName.equals( "com.sun.star.comp.Draw.PresentationDocument" ) )
1230 int[] pTempClassID = { 0x91, 0x76, 0xE4, 0x8A, 0x63, 0x7A, 0x4D, 0x1F,
1231 0x80, 0x3B, 0x99, 0xD9, 0xBF, 0xAC, 0x10, 0x47 };
1232 for ( int ind = 0; ind < 16; ind++ )
1233 pClassID[ind] = (byte)pTempClassID[ind];
1235 else if ( aServiceName.equals( "com.sun.star.comp.Draw.DrawingDocument" ) )
1237 int[] pTempClassID = { 0x4B, 0xAB, 0x89, 0x70, 0x8A, 0x3B, 0x45, 0xB3,
1238 0x99, 0x1C, 0xCB, 0xEE, 0xAC, 0x6B, 0xD5, 0xE3 };
1239 for ( int ind = 0; ind < 16; ind++ )
1240 pClassID[ind] = (byte)pTempClassID[ind];
1242 else if ( aServiceName.equals( "com.sun.star.comp.Math.FormulaDocument" ) )
1244 int[] pTempClassID = { 0x07, 0x8B, 0x7A, 0xBA, 0x54, 0xFC, 0x45, 0x7F,
1245 0x85, 0x51, 0x61, 0x47, 0xE7, 0x76, 0xA9, 0x97 };
1246 for ( int ind = 0; ind < 16; ind++ )
1247 pClassID[ind] = (byte)pTempClassID[ind];
1249 else if ( aServiceName.equals( "BitmapImage" ) )
1251 int[] pTempClassID = { 0xD3, 0xE3, 0x4B, 0x21, 0x9D, 0x75, 0x10, 0x1A,
1252 0x8C, 0x3D, 0x00, 0xAA, 0x00, 0x1A, 0x16, 0x52 };
1253 for ( int ind = 0; ind < 16; ind++ )
1254 pClassID[ind] = (byte)pTempClassID[ind];
1257 if ( pClassID != null )
1259 // create embedded object based on the class ID
1260 try {
1261 Object oEmbedCreator = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectCreator" );
1262 XEmbedObjectCreator xEmbedCreator = (XEmbedObjectCreator)UnoRuntime.queryInterface(
1263 XEmbedObjectCreator.class,
1264 oEmbedCreator );
1265 if ( xEmbedCreator != null )
1267 Object oEmbObj = xEmbedCreator.createInstanceInitNew( pClassID,
1268 "Dummy name",
1269 m_xStorage,
1270 "EmbedSub",
1271 new PropertyValue[0] );
1272 xEmbObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
1274 else
1275 JOptionPane.showMessageDialog( m_aFrame,
1276 "Can't create EmbedCreator!",
1277 "Error:",
1278 JOptionPane.ERROR_MESSAGE );
1280 catch( Exception e )
1282 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in createInstanceInitNew():", JOptionPane.ERROR_MESSAGE );
1285 else
1286 JOptionPane.showMessageDialog( m_aFrame, "Can't retrieve class ID!", "Error:", JOptionPane.ERROR_MESSAGE );
1288 return xEmbObj;
1291 public XEmbeddedObject createLinkObject( String aLinkURL )
1293 XEmbeddedObject xEmbObj = null;
1295 try {
1296 Object oLinkCreator = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectCreator" );
1297 XLinkCreator xLinkCreator = (XLinkCreator)UnoRuntime.queryInterface(
1298 XLinkCreator.class,
1299 oLinkCreator );
1300 if ( xLinkCreator != null )
1302 PropertyValue[] aMedDescr = { new PropertyValue(), new PropertyValue() };
1303 aMedDescr[0].Name = "URL";
1304 aMedDescr[0].Value = (Object) aLinkURL;
1305 aMedDescr[1].Name = "ReadOnly";
1306 aMedDescr[1].Value = (Object) new Boolean( false );
1307 Object oEmbObj = xLinkCreator.createInstanceLink( m_xStorage, "EmbedSub", aMedDescr, new PropertyValue[0] );
1308 xEmbObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
1310 else
1311 JOptionPane.showMessageDialog( m_aFrame,
1312 "Can't create LinkCreator!",
1313 "Error:",
1314 JOptionPane.ERROR_MESSAGE );
1316 catch( Exception e )
1318 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in createLinkObject():", JOptionPane.ERROR_MESSAGE );
1322 return xEmbObj;
1326 public XEmbeddedObject loadEmbedObject( String aFileURI )
1328 XEmbeddedObject xEmbObj = null;
1329 try {
1330 Object oEmbedCreator = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectCreator" );
1331 XEmbedObjectCreator xEmbedCreator = (XEmbedObjectCreator)UnoRuntime.queryInterface(
1332 XEmbedObjectCreator.class,
1333 oEmbedCreator );
1334 if ( xEmbedCreator != null )
1336 PropertyValue[] aMedDescr = { new PropertyValue(), new PropertyValue() };
1337 aMedDescr[0].Name = "URL";
1338 aMedDescr[0].Value = (Object) aFileURI;
1339 aMedDescr[1].Name = "ReadOnly";
1340 aMedDescr[1].Value = (Object) new Boolean( false );
1341 Object oEmbObj = xEmbedCreator.createInstanceInitFromMediaDescriptor( m_xStorage,
1342 "EmbedSub",
1343 aMedDescr,
1344 new PropertyValue[0] );
1345 xEmbObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
1347 else
1348 JOptionPane.showMessageDialog( m_aFrame,
1349 "Can't create EmbedFactory!",
1350 "Error:",
1351 JOptionPane.ERROR_MESSAGE );
1353 catch( Exception e )
1355 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in loadEmbedObject():", JOptionPane.ERROR_MESSAGE );
1358 return xEmbObj;
1361 public void clearObjectAndStorage()
1363 synchronized( m_oImageLock )
1365 m_aImage = null;
1368 m_nXScaling = 1;
1369 m_nYScaling = 1;
1370 m_nXPixelSize = 1;
1371 m_nYPixelSize = 1;
1373 m_bOwnFile = false;
1375 m_aLinkURI = null;
1376 m_bLinkObj = false;
1378 if ( m_xEmbedObj != null )
1380 try {
1381 XCloseable xClose = (XCloseable)UnoRuntime.queryInterface( XCloseable.class, m_xEmbedObj );
1382 if ( xClose != null )
1383 xClose.close( true );
1385 catch ( Exception ex )
1387 m_xEmbedObj = null;
1390 if ( m_xStorage != null )
1392 try {
1393 XComponent xComponent = (XComponent)UnoRuntime.queryInterface( XComponent.class, m_xStorage );
1394 if ( xComponent != null )
1395 xComponent.dispose();
1397 catch ( Exception ex )
1399 m_xStorage = null;
1403 public XStorage createTempStorage()
1405 XStorage xTempStorage = null;
1407 try {
1408 Object oStorageFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.StorageFactory" );
1409 XSingleServiceFactory xStorageFactory = (XSingleServiceFactory)UnoRuntime.queryInterface(
1410 XSingleServiceFactory.class,
1411 oStorageFactory );
1412 if ( xStorageFactory != null )
1414 Object oStorage = xStorageFactory.createInstance();
1415 xTempStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
1417 else
1418 JOptionPane.showMessageDialog( m_aFrame,
1419 "Can't create StorageFactory!",
1420 "Error:",
1421 JOptionPane.ERROR_MESSAGE );
1423 catch( Exception e )
1425 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in createTempStorage():", JOptionPane.ERROR_MESSAGE );
1428 return xTempStorage;
1431 public void saveObjectAsFileURI( String aFileURI )
1433 try {
1434 Object oStorageFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.StorageFactory" );
1435 XSingleServiceFactory xStorageFactory = (XSingleServiceFactory)UnoRuntime.queryInterface(
1436 XSingleServiceFactory.class,
1437 oStorageFactory );
1438 if ( xStorageFactory != null )
1440 XEmbedPersist xPersist = (XEmbedPersist)UnoRuntime.queryInterface( XEmbedPersist.class, m_xEmbedObj );
1441 if ( xPersist != null )
1443 Object aArgs[] = new Object[2];
1444 aArgs[0] = aFileURI;
1445 aArgs[1] = new Integer( ElementModes.ELEMENT_READWRITE );
1447 Object oStorage = xStorageFactory.createInstanceWithArguments( aArgs );
1448 XStorage xTargetStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
1450 PropertyValue aProps[] = { new PropertyValue() };
1451 aProps[0].Name = "StoreVisualReplacement";
1452 aProps[0].Value = new Boolean( m_bStoreVisRepl );
1454 xPersist.storeAsEntry( xTargetStorage, "EmbedSub", new PropertyValue[0], aProps );
1455 xPersist.saveCompleted( true );
1457 // the object must be already based on new storage
1458 XComponent xComponent = (XComponent)UnoRuntime.queryInterface( XComponent.class, m_xStorage );
1459 xComponent.dispose();
1461 m_xStorage = xTargetStorage;
1462 m_bOwnFile = true;
1464 XTransactedObject xTransact = (XTransactedObject)UnoRuntime.queryInterface( XTransactedObject.class,
1465 m_xStorage );
1466 if ( xTransact != null )
1467 xTransact.commit();
1469 else
1470 JOptionPane.showMessageDialog( m_aFrame, "No XEmbedPersist!", "Error:", JOptionPane.ERROR_MESSAGE );
1472 else
1473 JOptionPane.showMessageDialog( m_aFrame,
1474 "Can't create StorageFactory!",
1475 "Error:",
1476 JOptionPane.ERROR_MESSAGE );
1478 catch( Exception e )
1480 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in saveStorageToFileURI():", JOptionPane.ERROR_MESSAGE );
1485 public void loadFileURI( String aFileURI )
1489 Object oStorageFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.StorageFactory" );
1490 XSingleServiceFactory xStorageFactory = (XSingleServiceFactory)UnoRuntime.queryInterface(
1491 XSingleServiceFactory.class,
1492 oStorageFactory );
1493 Object aArgs[] = new Object[2];
1494 aArgs[0] = aFileURI;
1495 aArgs[1] = new Integer( ElementModes.ELEMENT_READWRITE );
1497 Object oStorage = xStorageFactory.createInstanceWithArguments( aArgs );
1498 XStorage xTargetStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
1500 Object oEmbedCreator = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectCreator" );
1501 XEmbedObjectCreator xEmbedCreator = (XEmbedObjectCreator)UnoRuntime.queryInterface(
1502 XEmbedObjectCreator.class,
1503 oEmbedCreator );
1505 XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface( XNameAccess.class,
1506 xTargetStorage );
1507 if ( xNameAccess == null )
1509 JOptionPane.showMessageDialog( m_aFrame, "No XNameAccess!", "Error:", JOptionPane.ERROR_MESSAGE );
1510 return;
1513 Object oEmbObj = null;
1514 if ( xNameAccess.hasByName( "LinkName" ) && xTargetStorage.isStreamElement( "LinkName" ) )
1517 // OOo links will not be tested until they have correct persistence
1518 XStream xLinkStream = xTargetStorage.openStreamElement( "LinkName", ElementModes.ELEMENT_READ );
1519 if ( xLinkStream != null )
1521 XInputStream xInStream = xLinkStream.getInputStream();
1522 if ( xInStream != null )
1524 byte[][] pBuff = new byte[1][0];
1525 int nRead = xInStream.readBytes( pBuff, 1000 );
1526 m_aLinkURI = new String( pBuff[0] );
1527 xInStream.closeInput();
1528 oEmbObj = xEmbedCreator.createInstanceLink( m_aLinkURI );
1529 m_bLinkObj = true;
1534 else
1535 oEmbObj = xEmbedCreator.createInstanceInitFromEntry( xTargetStorage,
1536 "EmbedSub",
1537 false,
1538 new PropertyValue[0] );
1540 m_xEmbedObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
1542 if ( m_xEmbedObj != null )
1544 m_xStorage = xTargetStorage;
1545 m_bOwnFile = true;
1547 else
1548 JOptionPane.showMessageDialog( m_aFrame,
1549 "Can't create EmbedObject from storage!",
1550 "Error:",
1551 JOptionPane.ERROR_MESSAGE );
1553 catch( Exception e )
1555 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in loadFileURI():", JOptionPane.ERROR_MESSAGE );
1559 public void storeLinkToStorage()
1561 if ( m_xStorage != null && m_bLinkObj )
1563 try {
1564 XStream xLinkStream = m_xStorage.openStreamElement( "LinkName", ElementModes.ELEMENT_WRITE );
1566 if ( xLinkStream != null )
1568 XOutputStream xLinkOutStream = xLinkStream.getOutputStream();
1569 XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface( XTruncate.class,
1570 xLinkOutStream );
1571 if ( xLinkOutStream != null && xTruncate != null )
1573 xTruncate.truncate();
1575 char[] aLinkChar = m_aLinkURI.toCharArray();
1576 byte[] aLinkBytes = new byte[ aLinkChar.length ];
1577 for ( int ind = 0; ind < aLinkChar.length; ind++ )
1578 aLinkBytes[ind] = (byte)aLinkChar[ind];
1580 xLinkOutStream.writeBytes( aLinkBytes );
1581 xLinkOutStream.closeOutput();
1583 XComponent xComponent = (XComponent) UnoRuntime.queryInterface( XComponent.class,
1584 xLinkStream );
1585 if ( xComponent != null )
1586 xComponent.dispose();
1588 else
1589 JOptionPane.showMessageDialog( m_aFrame,
1590 "The substream can not be truncated or written!",
1591 "Error:",
1592 JOptionPane.ERROR_MESSAGE );
1595 else
1596 JOptionPane.showMessageDialog( m_aFrame,
1597 "Can't create/open substream!",
1598 "Error:",
1599 JOptionPane.ERROR_MESSAGE );
1601 catch( Exception e )
1603 JOptionPane.showMessageDialog( m_aFrame,
1605 "Exception in storeLinkToStorage:",
1606 JOptionPane.ERROR_MESSAGE );
1612 public void storeLinkAsFileURI( String aFileURI )
1614 try {
1615 Object oStorageFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.StorageFactory" );
1616 XSingleServiceFactory xStorageFactory = (XSingleServiceFactory)UnoRuntime.queryInterface(
1617 XSingleServiceFactory.class,
1618 oStorageFactory );
1619 if ( xStorageFactory != null )
1621 Object aArgs[] = new Object[2];
1622 aArgs[0] = aFileURI;
1623 aArgs[1] = new Integer( ElementModes.ELEMENT_READWRITE );
1625 Object oStorage = xStorageFactory.createInstanceWithArguments( aArgs );
1626 XStorage xTargetStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
1628 XComponent xComponent = (XComponent)UnoRuntime.queryInterface( XComponent.class, m_xStorage );
1629 xComponent.dispose();
1631 m_xStorage = xTargetStorage;
1632 m_bOwnFile = true;
1634 storeLinkToStorage();
1636 XTransactedObject xTransact = (XTransactedObject)UnoRuntime.queryInterface( XTransactedObject.class,
1637 m_xStorage );
1638 if ( xTransact != null )
1639 xTransact.commit();
1641 else
1642 JOptionPane.showMessageDialog( m_aFrame,
1643 "Can't create StorageFactory!",
1644 "Error:",
1645 JOptionPane.ERROR_MESSAGE );
1647 catch( Exception e )
1649 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in saveStorageToFileURI():", JOptionPane.ERROR_MESSAGE );
1653 public String getValidURL( String sFileURL )
1655 // m_xTransformer must be set!
1656 URL[] aURLs = { new URL() };
1657 aURLs[0].Complete = sFileURL;
1659 try {
1660 if ( !m_xTransformer.parseSmart( aURLs, "" ) )
1661 throw new Exception();
1663 catch( Exception e )
1665 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in getValidURL():", JOptionPane.ERROR_MESSAGE );
1668 return aURLs[0].Complete;
1671 public void disposeObject()
1673 // TODO:
1674 // usage of object, storage and bitmap painter should be locked
1675 // but since possibility of rasecondition is very low
1676 // it is not really required for testing application
1678 clearObjectAndStorage();
1680 if ( m_aBitmapPainter != null )
1682 m_aBitmapPainter.disconnectListener();
1683 m_aBitmapPainter = null;