Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / embeddedobj / test / Container1 / EmbedContApp.java
blob9743a4f4cadc32f9c273cf9ac4394973ac40c505
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package embeddedobj.test;
21 import java.awt.*;
22 import java.applet.*;
23 import java.awt.event.*;
24 import java.net.*;
25 import java.io.*;
26 import java.util.Vector;
28 import javax.swing.JOptionPane;
29 import javax.swing.Timer;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.lang.XSingleServiceFactory;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.uno.XInterface;
36 import com.sun.star.uno.AnyConverter;
37 import com.sun.star.uno.Type;
38 import com.sun.star.uno.Any;
40 import com.sun.star.lang.XComponent;
42 import com.sun.star.util.XCloseable;
43 import com.sun.star.util.XURLTransformer;
44 import com.sun.star.util.URL;
46 import com.sun.star.beans.PropertyValue;
47 import com.sun.star.beans.NamedValue;
49 import com.sun.star.datatransfer.DataFlavor;
50 import com.sun.star.datatransfer.XTransferable;
52 import com.sun.star.container.XNameAccess;
54 import com.sun.star.io.XStream;
55 import com.sun.star.io.XInputStream;
56 import com.sun.star.io.XOutputStream;
57 import com.sun.star.io.XTruncate;
59 import com.sun.star.awt.XWindow;
60 import com.sun.star.awt.XBitmap;
62 import com.sun.star.task.XJob;
64 import com.sun.star.embed.*;
67 class ActionObject
69 public byte m_nID;
70 public String m_sParam;
72 public ActionObject()
74 m_nID = 0;
75 m_sParam = null;
78 public ActionObject( byte nID )
80 m_nID = nID;
81 m_sParam = null;
84 public ActionObject( byte nID, String sParam )
86 m_nID = nID;
87 m_sParam = sParam;
90 public ActionObject( ActionObject aObject )
92 m_nID = aObject.m_nID;
93 m_sParam = aObject.m_sParam;
97 public class EmbedContApp extends Applet
98 implements MouseListener, XEmbeddedClient, ActionListener, XJob, XInplaceClient, XWindowSupplier
100 private XMultiServiceFactory m_xServiceFactory;
102 private final boolean m_bStoreVisRepl = false;
104 private XJob m_xMainThreadExecutor;
105 private NamedValue[] m_pValuesForExecutor;
107 private XEmbeddedObject m_xEmbedObj;
108 private XStorage m_xStorage;
109 private float m_nXScaling;
110 private float m_nYScaling;
111 private float m_nXPixelSize;
112 private float m_nYPixelSize;
114 private Frame m_aFrame;
115 private Menu m_aFileMenu;
116 private Menu m_aObjectMenu;
117 private Toolkit m_aToolkit;
119 private Image m_aImage;
120 private Object m_oImageLock;
122 private boolean m_bOwnFile = false;
124 private boolean m_bLinkObj = false;
125 private String m_aLinkURI;
127 private Object m_oActionsNumberLock;
129 private Timer m_aTimer;
130 private boolean m_bDestroyed = false;
132 private Object m_oInHandlerLock;
133 private boolean m_bInHandler = false;
135 private XURLTransformer m_xTransformer;
137 private NativeView m_aNativeView;
138 private XWindow m_xVCLWindow;
140 private XBitmap m_xBitmap;
141 private BitmapPainter m_aBitmapPainter;
143 // Constants
144 private final byte DESTROY = 1;
145 private final byte ACTIVATE_OUTPLACE = 2;
146 private final byte NEW_DOCUMENT = 3;
147 private final byte SAVE_AS = 4;
148 private final byte OPEN_FILE = 5;
149 private final byte SAVE = 6;
150 private final byte NEW_OBJECT = 7;
151 private final byte OBJECT_FROM_FILE = 8;
152 private final byte LINK_FROM_FILE = 9;
153 private final byte CONVERT_LINK_TO_OBJECT = 10;
154 private final byte ACTIVATE_INPLACE = 11;
155 private final byte DEACTIVATE = 12;
157 // Methods
158 public EmbedContApp( Frame aFrame, XMultiServiceFactory xServiceFactory )
160 m_aFrame = aFrame;
161 m_xServiceFactory = xServiceFactory;
164 public void init()
166 resize( 800, 600 );
167 setBackground( Color.gray );
169 m_aToolkit = Toolkit.getDefaultToolkit();
171 try {
172 Object oTransformer = m_xServiceFactory.createInstance( "com.sun.star.util.URLTransformer" );
173 m_xTransformer = (XURLTransformer)UnoRuntime.queryInterface( XURLTransformer.class, oTransformer );
174 } catch( Exception e ) { System.exit( 0 ); }
176 m_oActionsNumberLock = new Object();
177 m_aActionsList = new ArrayList();
179 m_oInHandlerLock = new Object();
180 m_oImageLock = new Object();
182 try {
183 Object oJob = m_xServiceFactory.createInstance( "com.sun.star.comp.thread.MainThreadExecutor" );
184 m_xMainThreadExecutor = (XJob)UnoRuntime.queryInterface( XJob.class, oJob );
185 } catch( Exception e ) {}
187 if ( m_xMainThreadExecutor == null )
189 System.out.println( "Can't create MainThreadExecutor! The application is unusable!" );
190 System.exit( 0 );
193 m_nXScaling = 1;
194 m_nYScaling = 1;
195 m_nXPixelSize = 1;
196 m_nYPixelSize = 1;
198 m_pValuesForExecutor = new NamedValue[1];
199 m_pValuesForExecutor[0] = new NamedValue( "JobToExecute", (Object)this );
201 m_aTimer = new Timer( 100, this );
202 m_aTimer.start();
204 // Get a menu bar.
205 MenuBar aMenuBar = m_aFrame.getMenuBar();
206 if( aMenuBar == null )
208 aMenuBar = new MenuBar();
209 m_aFrame.setMenuBar( aMenuBar );
212 // Create menus for the menu bar.
214 // File menu
215 m_aFileMenu = new Menu( "File", true );
216 aMenuBar.add( m_aFileMenu );
218 MenuItem aItem = new NewMenuItem();
219 m_aFileMenu.add( aItem );
221 aItem = new OpenFileMenuItem();
222 m_aFileMenu.add( aItem );
224 aItem = new SaveMenuItem();
225 m_aFileMenu.add( aItem );
227 aItem = new SaveAsMenuItem();
228 m_aFileMenu.add( aItem );
230 // Object menu
231 m_aObjectMenu = new Menu( "Object", true );
232 aMenuBar.add( m_aObjectMenu );
234 aItem = new NewObjectMenuItem();
235 m_aObjectMenu.add( aItem );
237 aItem = new LoadObjectMenuItem();
238 m_aObjectMenu.add( aItem );
240 aItem = new LinkObjectMenuItem();
241 m_aObjectMenu.add( aItem );
243 aItem = new ConvertLinkToEmbedMenuItem();
244 m_aObjectMenu.add( aItem );
246 // Activation menu
247 m_aObjectMenu = new Menu( "Activation", true );
248 aMenuBar.add( m_aObjectMenu );
250 aItem = new ActivateOutplaceMenuItem();
251 m_aObjectMenu.add( aItem );
253 aItem = new ActivateInplaceMenuItem();
254 m_aObjectMenu.add( aItem );
256 aItem = new DeactivateMenuItem();
257 m_aObjectMenu.add( aItem );
259 m_aNativeView = new NativeView();
260 m_aNativeView.resize( 800, 600 );
261 this.add( m_aNativeView );
264 public void actionPerformed( ActionEvent evt )
266 synchronized( m_oInHandlerLock )
268 if ( m_bInHandler )
269 return;
270 m_bInHandler = true;
273 synchronized( m_oActionsNumberLock )
275 if ( m_aActionsList.size() > 0 )
277 try {
278 m_xMainThreadExecutor.execute( m_pValuesForExecutor );
280 catch( Exception e )
282 System.out.println( "Exception in actionPerformed() : " + e );
285 else
287 synchronized( m_oInHandlerLock )
289 m_bInHandler = false;
295 // XWindowSupplier
296 public XWindow getWindow()
298 return m_xVCLWindow;
301 // XEmbeddedClient
302 public void saveObject()
303 throws com.sun.star.uno.Exception
305 if ( m_xEmbedObj != null )
307 try {
308 XEmbedPersist xPersist = (XEmbedPersist)UnoRuntime.queryInterface( XEmbedPersist.class, m_xEmbedObj );
309 if ( xPersist != null )
311 xPersist.storeOwn();
312 generateNewImage();
314 else
315 JOptionPane.showMessageDialog( m_aFrame, "No XEmbedPersist!", "Error:", JOptionPane.ERROR_MESSAGE );
317 catch( Exception e )
319 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in saveObject:", JOptionPane.ERROR_MESSAGE );
323 generateNewImage();
324 repaint();
327 public void onShowWindow( boolean bVisible )
329 // for now nothing to do
332 // XInplaceClient
333 public boolean canInplaceActivate()
335 return true;
338 public void onInplaceActivate()
340 // TODO
341 // prepare for inplace activation
343 // REMOVE
344 // workaround for CLIPCHILDREN problem
345 if ( m_aBitmapPainter != null )
346 m_aBitmapPainter.stopPainting();
349 public void onUIActivate()
351 // TODO
352 // prepare for UI activate
355 public void onInplaceDeactivate()
357 // TODO
358 // inplace deactivation is done
360 // REMOVE
361 // workaround for CLIPCHILDREN problem
362 if ( m_aBitmapPainter != null )
363 m_aBitmapPainter.startPainting();
366 public void onUIDeactivate()
368 // TODO
369 // prepare for UI deactivate
372 public XIPMainContainerWindow getTopmostWindow()
374 // TODO
375 // return an implementation of XIPMainContainerWindow
376 // mainly required for ui activation
377 // dummy implementation is enough for inplace activation
379 return null;
382 public XInplaceUIWindow getDocumentWindow()
384 // TODO
385 // return implementation of XInplaceUIWindow
386 // mainly required for ui activation
387 // dummy implementation is enough for inplace activation
389 return null;
392 public com.sun.star.awt.Rectangle getPosRect()
394 // provide position rectangle to the object
395 try {
396 // here object bitmap and scaling factor hold the size
397 com.sun.star.awt.Size aBitmapSize = m_xBitmap.getSize();
398 com.sun.star.awt.Size aVisSize = new com.sun.star.awt.Size(
399 (int)( aBitmapSize.Width * m_nXScaling ),
400 (int)( aBitmapSize.Height * m_nYScaling ) );
401 return new com.sun.star.awt.Rectangle( 10, 10, aVisSize.Width, aVisSize.Height );
403 catch( Exception e )
405 System.out.println( "Position rectangle generation failed!" );
408 return new com.sun.star.awt.Rectangle( 10, 10, 110, 110 );
411 public com.sun.star.awt.Rectangle getClipRect()
413 // provide clip rectangle to the object
414 // in this application position and clip rectangles are the same
416 try {
417 // here object bitmap and scaling factor hold the size
418 com.sun.star.awt.Size aBitmapSize = m_xBitmap.getSize();
419 com.sun.star.awt.Size aVisSize = new com.sun.star.awt.Size(
420 (int)( aBitmapSize.Width * m_nXScaling ),
421 (int)( aBitmapSize.Height * m_nYScaling ) );
422 return new com.sun.star.awt.Rectangle( 10, 10, aVisSize.Width, aVisSize.Height );
424 catch( Exception e )
426 System.out.println( "Clip rectangle generation failed!" );
429 return new com.sun.star.awt.Rectangle( 10, 10, 110, 110 );
432 public void translateAccelerators( com.sun.star.awt.KeyEvent[] aKeys )
434 // TODO
435 // an accelerator table for object
436 // ui activation related
439 public void scrollObj( com.sun.star.awt.Size aOffset )
441 // TODO
442 // scrolls the object to a specified offset
443 // not mandatory for the testing application :)
446 public void onPosRectChange( com.sun.star.awt.Rectangle aPosRect )
448 // object asks to change the position
449 if ( m_xEmbedObj != null )
451 try {
452 int nState = m_xEmbedObj.getCurrentState();
453 // such a position change make sense only when object is
454 // either inplace or ui active
455 if ( nState == EmbedStates.EMBED_INPLACE_ACTIVE
456 || nState == EmbedStates.EMBED_UI_ACTIVE )
458 XInplaceObject xInplObj = (XInplaceObject)UnoRuntime.queryInterface( XInplaceObject.class, m_xEmbedObj );
459 if ( xInplObj != null )
461 xInplObj.setObjectRects( aPosRect, aPosRect ); // show the whole object
462 if ( m_aBitmapPainter != null )
463 m_aBitmapPainter.setRect( aPosRect );
465 else
466 System.out.println( "Why object that does not support inplace activation behave like inplace object?!" );
468 else
469 System.out.println( "The object is not active but asks to change visual area!" );
470 } catch( Exception e )
472 System.out.println( "Exception is thrown in onPosRectChange: " + e );
475 else
476 System.out.println( "Who asks to change visual area?!!" );
479 // XJob
480 public Object execute( NamedValue[] pValues )
482 for( int nInd = 0; nInd < m_aActionsList.size(); nInd++ )
484 ActionObject aAction = m_aActionsList.get( nInd );
485 if ( aAction != null )
487 if ( aAction.m_nID == DESTROY )
489 // free all resources
490 clearObjectAndStorage();
491 m_bDestroyed = true;
493 else if ( aAction.m_nID == ACTIVATE_OUTPLACE )
495 // activate object if exists and not active
496 if ( m_xEmbedObj != null )
498 try {
499 m_xEmbedObj.changeState( EmbedStates.EMBED_ACTIVE );
501 catch( Exception ex )
503 System.out.println( "Exception on mouse click" + ex );
507 else if ( aAction.m_nID == NEW_DOCUMENT )
509 // clear everything
510 clearObjectAndStorage();
512 repaint();
514 else if ( aAction.m_nID == SAVE_AS )
516 // open SaveAs dialog and store
518 if ( m_xStorage != null && m_xEmbedObj != null )
520 try {
522 if ( m_bLinkObj )
523 storeLinkAsFileURI( aFileURI );
524 else
526 saveObjectAsFileURI( aAction.m_sParam );
528 catch( Exception ex )
530 System.out.println( "Exception in SaveAsMenuItem: " + ex );
534 else if ( aAction.m_nID == OPEN_FILE )
536 // clear everything
537 clearObjectAndStorage();
539 // load from specified file
540 loadFileURI( aAction.m_sParam );
542 if ( m_xEmbedObj != null )
544 try {
545 m_xEmbedObj.setClientSite( this );
547 catch( Exception ex )
549 System.out.println( "Exception in OpenFileMenuItem: " + ex );
553 generateNewImage();
554 repaint();
556 else if ( aAction.m_nID == SAVE )
558 if ( m_xStorage != null && m_xEmbedObj != null )
560 // if has persistence store there
561 // if not it is and error, SaveAs had to be used
563 if ( m_bOwnFile )
565 if ( m_xStorage != null )
567 try {
568 saveObject();
570 if ( m_bLinkObj )
571 storeLinkToStorage();
573 XTransactedObject xTransact = (XTransactedObject)UnoRuntime.queryInterface( XTransactedObject.class,
574 m_xStorage );
575 if ( xTransact != null )
576 xTransact.commit();
578 catch( Exception ex )
580 System.out.println( "Exception during save operation in SaveMenuItem:" + ex );
583 else
585 System.out.println( "No storage for owned file!" );
588 else
590 System.out.println( "No owned file!" );
594 else if ( aAction.m_nID == NEW_OBJECT )
596 // remove current object an init a new one
597 clearObjectAndStorage();
599 if ( aAction.m_sParam != null )
601 m_xStorage = createTempStorage();
603 if ( m_xStorage != null )
604 m_xEmbedObj = createEmbedObject( aAction.m_sParam );
605 else
606 System.out.println( "Can't create temporary storage!" );
608 if ( m_xEmbedObj != null )
610 try {
611 m_xEmbedObj.setClientSite( this );
613 catch( Exception ex )
615 System.out.println( "Exception in NewObjectMenuItem:" + ex );
620 generateNewImage();
621 repaint();
623 else if ( aAction.m_nID == OBJECT_FROM_FILE )
625 // first remove current object
626 clearObjectAndStorage();
628 // create object from specified file
629 m_xStorage = createTempStorage();
631 if ( m_xStorage != null )
632 m_xEmbedObj = loadEmbedObject( aAction.m_sParam );
634 if ( m_xEmbedObj != null )
636 try {
637 m_xEmbedObj.setClientSite( this );
639 catch( Exception ex )
641 System.out.println( "Exception in LoadObjectMenuItem: " + ex );
645 generateNewImage();
646 repaint();
648 else if ( aAction.m_nID == LINK_FROM_FILE )
650 // first remove current object
651 clearObjectAndStorage();
653 m_xStorage = createTempStorage();
655 // create object from specified file
656 m_xEmbedObj = createLinkObject( aAction.m_sParam );
658 if ( m_xEmbedObj != null )
660 m_aLinkURI = aAction.m_sParam;
661 m_bLinkObj = true;
663 try {
664 m_xEmbedObj.setClientSite( this );
666 catch( Exception ex )
668 System.out.println( "Exception in LinkObjectMenuItem:" + ex );
672 generateNewImage();
673 repaint();
675 else if ( aAction.m_nID == CONVERT_LINK_TO_OBJECT )
677 if ( !m_bLinkObj )
679 System.out.println( "The object is not a link!" );
680 continue;
683 if ( m_xEmbedObj != null )
685 if ( m_xStorage != null )
687 try {
688 XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface( XNameAccess.class,
689 m_xStorage );
690 if ( xNameAccess != null && xNameAccess.hasByName( "LinkName" ) )
691 m_xStorage.removeElement( "LinkName" );
693 XLinkageSupport xLinkage = (XLinkageSupport)UnoRuntime.queryInterface( XLinkageSupport.class,
694 m_xEmbedObj );
695 if ( xLinkage != null )
697 xLinkage.breakLink( m_xStorage, "EmbedSub" );
698 m_bLinkObj = false;
699 m_aLinkURI = null;
701 else
702 System.out.println( "No XLinkageSupport in ConvertLink... !" );
704 catch( Exception e1 )
706 System.out.println( "Exception in ConvertLinkToEmbed:try 1 :" + e1 );
711 else if ( aAction.m_nID == ACTIVATE_INPLACE )
713 // activate object
714 if ( m_xEmbedObj != null )
716 // in general it is better to check acceptable states
717 try {
718 m_xEmbedObj.changeState( EmbedStates.EMBED_INPLACE_ACTIVE );
720 catch( Exception ex )
722 System.out.println( "Exception on inplace activation " + ex );
726 else if ( aAction.m_nID == DEACTIVATE )
728 // activate object
730 if ( m_xEmbedObj != null )
732 int nOldState = -1;
733 try {
734 nOldState = m_xEmbedObj.getCurrentState();
735 } catch( Exception e )
738 if ( nOldState == EmbedStates.EMBED_ACTIVE
739 || nOldState == EmbedStates.EMBED_INPLACE_ACTIVE
740 || nOldState == EmbedStates.EMBED_UI_ACTIVE )
742 try {
743 m_xEmbedObj.changeState( EmbedStates.EMBED_RUNNING );
745 catch( Exception ex )
747 System.out.println( "Exception on inplace activation " + ex );
750 else
752 System.out.println( "Deactivation of nonactive object!" );
756 else
758 System.out.println( "Unknown action is requested: " + aAction.m_nID + "\n" );
763 m_aActionsList.clear();
765 synchronized( m_oInHandlerLock )
767 m_bInHandler = false;
770 return Any.VOID;
773 public void actionRegister( byte nActionID, String sParam )
775 synchronized( m_oActionsNumberLock )
777 int nSize = m_aActionsList.size();
778 if ( nSize < 199 )
780 if ( nSize == 0 )
781 m_aActionsList.add( new ActionObject( nActionID, sParam ) );
782 else
784 ActionObject aAction = m_aActionsList.get( nSize - 1 );
785 if ( aAction != null && aAction.m_nID != DESTROY )
786 m_aActionsList.add( new ActionObject( nActionID, sParam ) );
792 public void SaveAsOperation()
794 if ( m_xStorage != null && m_xEmbedObj != null )
796 FileDialog aFileDialog = new FileDialog( m_aFrame, "SaveAs", FileDialog.SAVE );
797 aFileDialog.show();
798 if ( aFileDialog.getFile() != null )
800 String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
801 File aFile = new File( aFileName );
802 if ( aFile != null )
804 // create object from specified file
805 String aFileURI = getValidURL( aFile.toURI().toASCIIString() );
806 actionRegister( SAVE_AS, aFileURI );
810 else
811 JOptionPane.showMessageDialog( m_aFrame, "No document is embedded!", "Error:", JOptionPane.ERROR_MESSAGE );
815 public void destroy()
817 // redirect the call through the timer and call super.destroy();
818 actionRegister( DESTROY, null );
820 for ( int i = 0; i < 3 && !m_bDestroyed; i++ )
822 try {
823 Thread.sleep( 200 );
824 } catch ( Exception e )
828 if ( !m_bDestroyed )
829 System.out.println( "The object application can not exit correctly!" );
831 m_aTimer.stop();
833 super.destroy();
836 public void update( Graphics g )
838 paint( g );
841 public void paint( Graphics g )
843 super.paint( g );
845 createVclWindow();
848 public void createVclWindow()
850 synchronized( m_oImageLock )
852 if ( m_xVCLWindow == null && m_xServiceFactory != null && m_xEmbedObj != null && m_xBitmap != null )
854 java.awt.Rectangle aBounds = getBounds();
855 m_xVCLWindow = WindowHelper.createWindow( m_xServiceFactory, m_aNativeView, aBounds );
856 m_xVCLWindow.setVisible( true );
858 com.sun.star.awt.Size aBitmapSize = new com.sun.star.awt.Size( 200, 100 );
860 XVisualObject xVisObj = (XVisualObject)UnoRuntime.queryInterface( XVisualObject.class, m_xEmbedObj );
861 try {
862 com.sun.star.awt.Size aVisSize = xVisObj.getVisAreaSize( Aspects.MSASPECT_CONTENT );
863 m_nXPixelSize = aVisSize.Width / aBitmapSize.Width;
864 m_nYPixelSize = aVisSize.Height / aBitmapSize.Height;
866 catch( Exception e )
870 if ( m_xBitmap != null )
871 aBitmapSize = m_xBitmap.getSize();
873 System.out.println( "The visual area is Width = " + aBitmapSize.Width + "; Height = " + aBitmapSize.Height );
875 com.sun.star.awt.Rectangle aRect = new com.sun.star.awt.Rectangle(
878 Math.min( (int)aBounds.getWidth() - 20, aBitmapSize.Width ),
879 Math.min( (int)aBounds.getHeight() - 20, aBitmapSize.Height ) );
881 m_aBitmapPainter = new BitmapPainter( m_xMainThreadExecutor, m_xVCLWindow, m_xBitmap, aRect );
886 public void generateNewImage()
888 if ( m_xEmbedObj != null )
890 try {
891 int nOldState = m_xEmbedObj.getCurrentState();
892 int nState = nOldState;
893 if ( nOldState == EmbedStates.EMBED_LOADED )
895 m_xEmbedObj.changeState( EmbedStates.EMBED_RUNNING );
896 nState = EmbedStates.EMBED_RUNNING;
899 if ( nState == EmbedStates.EMBED_UI_ACTIVE || nState == EmbedStates.EMBED_INPLACE_ACTIVE
900 || nState == EmbedStates.EMBED_ACTIVE || nState == EmbedStates.EMBED_RUNNING )
902 XComponentSupplier xCompProv = (XComponentSupplier)UnoRuntime.queryInterface(
903 XComponentSupplier.class,
904 m_xEmbedObj );
905 if ( xCompProv != null )
907 XCloseable xCloseable = xCompProv.getComponent();
908 XTransferable xTransfer = (XTransferable)UnoRuntime.queryInterface(
909 XTransferable.class,
910 xCloseable );
911 if ( xTransfer != null )
913 DataFlavor aFlavor = new DataFlavor();
914 aFlavor.MimeType = "application/x-openoffice;windows_formatname=\"Bitmap\"";
915 aFlavor.HumanPresentableName = "Bitmap";
916 aFlavor.DataType = new Type( byte[].class );
918 Object aAny = xTransfer.getTransferData( aFlavor );
919 if ( aAny != null && AnyConverter.isArray( aAny ) )
921 synchronized( m_oImageLock )
923 m_xBitmap = WindowHelper.getVCLBitmapFromBytes( m_xServiceFactory, aAny );
924 if ( m_aBitmapPainter != null )
926 m_aBitmapPainter.setBitmap( m_xBitmap );
928 if ( m_xBitmap != null )
930 try {
931 com.sun.star.awt.Size aBitmapSize = m_xBitmap.getSize();
932 com.sun.star.awt.Size aVisSize = new com.sun.star.awt.Size(
933 (int)( aBitmapSize.Width * m_nXScaling ),
934 (int)( aBitmapSize.Height * m_nYScaling ) );
935 m_aBitmapPainter.setSize( aVisSize );
937 catch( Exception e )
945 else
946 System.out.println( "paint() : can not get XTransferable for the component!\n" );
948 else
949 System.out.println( "paint() : XComponentSupplier is not implemented!\n" );
952 catch( com.sun.star.uno.Exception e )
954 // dialogs should not be used in paint()
955 System.out.println( "Exception in paint(): " + e );
960 public void mouseClicked( MouseEvent e )
962 if( e.getModifiers() == InputEvent.BUTTON1_MASK )
964 actionRegister( ACTIVATE_OUTPLACE, null );
968 public void mousePressed( MouseEvent e ){};
969 public void mouseEntered( MouseEvent e ){};
970 public void mouseExited( MouseEvent e ){};
971 public void mouseReleased( MouseEvent e ){};
973 class NewMenuItem extends MenuItem implements ActionListener // Menu New
975 public NewMenuItem()
977 super( "New", new MenuShortcut( KeyEvent.VK_A ));
978 addActionListener( this );
981 public void actionPerformed( ActionEvent e )
983 actionRegister( NEW_DOCUMENT, null );
987 class SaveAsMenuItem extends MenuItem implements ActionListener // Menu SaveAs...
989 public SaveAsMenuItem()
991 super( "SaveAs..." );
992 addActionListener( this );
995 public void actionPerformed( ActionEvent e )
997 // open SaveAs dialog and store
999 SaveAsOperation();
1003 class OpenFileMenuItem extends MenuItem implements ActionListener // Menu Open
1005 public OpenFileMenuItem()
1007 super( "Open", new MenuShortcut( KeyEvent.VK_C ));
1008 addActionListener( this );
1011 public void actionPerformed( ActionEvent e )
1013 // open OpenFile dialog and load doc
1014 FileDialog aFileDialog = new FileDialog( m_aFrame, "Open" );
1015 aFileDialog.show();
1016 if ( aFileDialog.getFile() != null )
1018 String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
1019 File aFile = new File( aFileName );
1020 if ( aFile != null )
1022 // create object from specified file
1023 String aFileURI = getValidURL( aFile.toURI().toASCIIString() );
1024 actionRegister( OPEN_FILE, aFileURI );
1030 class SaveMenuItem extends MenuItem implements ActionListener // Menu Save
1032 public SaveMenuItem()
1034 super( "Save", new MenuShortcut( KeyEvent.VK_D ));
1035 addActionListener( this );
1038 public void actionPerformed( ActionEvent e )
1040 // if has persistence store there
1041 // if not open SaveAs dialog and store
1042 if ( m_xStorage != null && m_xEmbedObj != null )
1044 if ( m_bOwnFile )
1046 if ( m_xStorage == null )
1048 JOptionPane.showMessageDialog( m_aFrame,
1049 "No storage for oned file!",
1050 "Error:",
1051 JOptionPane.ERROR_MESSAGE );
1053 return;
1056 actionRegister( SAVE, null );
1058 else
1060 SaveAsOperation();
1063 else
1064 JOptionPane.showMessageDialog( m_aFrame, "No document is embedded!", "Error:", JOptionPane.ERROR_MESSAGE );
1068 class NewObjectMenuItem extends MenuItem implements ActionListener // Menu NewObject
1070 public NewObjectMenuItem()
1072 super( "Create", new MenuShortcut( KeyEvent.VK_N ));
1073 addActionListener( this );
1076 public void actionPerformed( ActionEvent e )
1078 Object[] possibleValues = { "com.sun.star.comp.Writer.TextDocument",
1079 "com.sun.star.comp.Writer.GlobalDocument",
1080 "com.sun.star.comp.Writer.WebDocument",
1081 "com.sun.star.comp.Calc.SpreadsheetDocument",
1082 "com.sun.star.comp.Draw.PresentationDocument",
1083 "com.sun.star.comp.Draw.DrawingDocument",
1084 "com.sun.star.comp.Math.FormulaDocument",
1085 "BitmapImage" };
1087 String selectedValue = (String)JOptionPane.showInputDialog( null, "DocumentType", "Select",
1088 JOptionPane.INFORMATION_MESSAGE, null,
1089 possibleValues, possibleValues[0] );
1091 actionRegister( NEW_OBJECT, selectedValue );
1095 class LoadObjectMenuItem extends MenuItem implements ActionListener // Menu LoadObject
1097 public LoadObjectMenuItem()
1099 super( "Load from file", new MenuShortcut( KeyEvent.VK_L ));
1100 addActionListener( this );
1103 public void actionPerformed( ActionEvent e )
1105 // open OpenFile dialog and load doc
1106 FileDialog aFileDialog = new FileDialog( m_aFrame, "Select sources to use for object init" );
1107 aFileDialog.show();
1108 if ( aFileDialog.getFile() != null )
1110 String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
1111 File aFile = new File( aFileName );
1112 if ( aFile != null )
1114 // create object from specified file
1115 String aFileURI = getValidURL( aFile.toURI().toASCIIString() );
1116 actionRegister( OBJECT_FROM_FILE, aFileURI );
1122 class LinkObjectMenuItem extends MenuItem implements ActionListener // Menu LinkObject
1124 public LinkObjectMenuItem()
1126 super( "Create link", new MenuShortcut( KeyEvent.VK_M ));
1127 addActionListener( this );
1130 public void actionPerformed( ActionEvent e )
1132 // open OpenFile dialog and load doc
1133 FileDialog aFileDialog = new FileDialog( m_aFrame, "Select sources to use for object init" );
1134 aFileDialog.show();
1135 if ( aFileDialog.getFile() != null )
1137 String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
1138 File aFile = new File( aFileName );
1139 if ( aFile != null )
1141 // create object from specified file
1142 String aFileURI = getValidURL( aFile.toURI().toASCIIString() );
1143 actionRegister( LINK_FROM_FILE, aFileURI );
1149 class ConvertLinkToEmbedMenuItem extends MenuItem implements ActionListener // Menu LinkObject
1151 public ConvertLinkToEmbedMenuItem()
1153 super( "Convert link to embed", new MenuShortcut( KeyEvent.VK_M ));
1154 addActionListener( this );
1157 public void actionPerformed( ActionEvent e )
1159 actionRegister( CONVERT_LINK_TO_OBJECT, null );
1163 class ActivateOutplaceMenuItem extends MenuItem implements ActionListener // Menu ActiveteOutplace
1165 public ActivateOutplaceMenuItem()
1167 super( "Activate outplace", new MenuShortcut( KeyEvent.VK_A ));
1168 addActionListener( this );
1171 public void actionPerformed( ActionEvent e )
1173 actionRegister( ACTIVATE_OUTPLACE, null );
1177 class ActivateInplaceMenuItem extends MenuItem implements ActionListener // Menu ActivateInplace
1179 public ActivateInplaceMenuItem()
1181 super( "Activate inplace", new MenuShortcut( KeyEvent.VK_I ));
1182 addActionListener( this );
1185 public void actionPerformed( ActionEvent e )
1187 actionRegister( ACTIVATE_INPLACE, null );
1191 class DeactivateMenuItem extends MenuItem implements ActionListener // Menu Deactivate
1193 public DeactivateMenuItem()
1195 super( "Deactivate", new MenuShortcut( KeyEvent.VK_D ));
1196 addActionListener( this );
1199 public void actionPerformed( ActionEvent e )
1201 actionRegister( DEACTIVATE, null );
1205 // Helper methods
1206 public XEmbeddedObject createEmbedObject( String aServiceName )
1208 XEmbeddedObject xEmbObj = null;
1209 byte[] pClassID = new byte[16];
1211 if ( aServiceName.equals( "com.sun.star.comp.Writer.TextDocument" ) )
1213 int[] pTempClassID = { 0x8B, 0xC6, 0xB1, 0x65, 0xB1, 0xB2, 0x4E, 0xDD,
1214 0xAA, 0x47, 0xDA, 0xE2, 0xEE, 0x68, 0x9D, 0xD6 };
1215 for ( int ind = 0; ind < 16; ind++ )
1216 pClassID[ind] = (byte)pTempClassID[ind];
1218 else if ( aServiceName.equals( "com.sun.star.comp.Writer.GlobalDocument" ) )
1220 int[] pTempClassID = { 0xB2, 0x1A, 0x0A, 0x7C, 0xE4, 0x03, 0x41, 0xFE,
1221 0x95, 0x62, 0xBD, 0x13, 0xEA, 0x6F, 0x15, 0xA0 };
1222 for ( int ind = 0; ind < 16; ind++ )
1223 pClassID[ind] = (byte)pTempClassID[ind];
1225 else if ( aServiceName.equals( "com.sun.star.comp.Writer.WebDocument" ) )
1227 int[] pTempClassID = { 0xA8, 0xBB, 0xA6, 0x0C, 0x7C, 0x60, 0x45, 0x50,
1228 0x91, 0xCE, 0x39, 0xC3, 0x90, 0x3F, 0xAC, 0x5E };
1229 for ( int ind = 0; ind < 16; ind++ )
1230 pClassID[ind] = (byte)pTempClassID[ind];
1232 else if ( aServiceName.equals( "com.sun.star.comp.Calc.SpreadsheetDocument" ) )
1234 int[] pTempClassID = { 0x47, 0xBB, 0xB4, 0xCB, 0xCE, 0x4C, 0x4E, 0x80,
1235 0xA5, 0x91, 0x42, 0xD9, 0xAE, 0x74, 0x95, 0x0F };
1236 for ( int ind = 0; ind < 16; ind++ )
1237 pClassID[ind] = (byte)pTempClassID[ind];
1239 else if ( aServiceName.equals( "com.sun.star.comp.Draw.PresentationDocument" ) )
1241 int[] pTempClassID = { 0x91, 0x76, 0xE4, 0x8A, 0x63, 0x7A, 0x4D, 0x1F,
1242 0x80, 0x3B, 0x99, 0xD9, 0xBF, 0xAC, 0x10, 0x47 };
1243 for ( int ind = 0; ind < 16; ind++ )
1244 pClassID[ind] = (byte)pTempClassID[ind];
1246 else if ( aServiceName.equals( "com.sun.star.comp.Draw.DrawingDocument" ) )
1248 int[] pTempClassID = { 0x4B, 0xAB, 0x89, 0x70, 0x8A, 0x3B, 0x45, 0xB3,
1249 0x99, 0x1C, 0xCB, 0xEE, 0xAC, 0x6B, 0xD5, 0xE3 };
1250 for ( int ind = 0; ind < 16; ind++ )
1251 pClassID[ind] = (byte)pTempClassID[ind];
1253 else if ( aServiceName.equals( "com.sun.star.comp.Math.FormulaDocument" ) )
1255 int[] pTempClassID = { 0x07, 0x8B, 0x7A, 0xBA, 0x54, 0xFC, 0x45, 0x7F,
1256 0x85, 0x51, 0x61, 0x47, 0xE7, 0x76, 0xA9, 0x97 };
1257 for ( int ind = 0; ind < 16; ind++ )
1258 pClassID[ind] = (byte)pTempClassID[ind];
1260 else if ( aServiceName.equals( "BitmapImage" ) )
1262 int[] pTempClassID = { 0xD3, 0xE3, 0x4B, 0x21, 0x9D, 0x75, 0x10, 0x1A,
1263 0x8C, 0x3D, 0x00, 0xAA, 0x00, 0x1A, 0x16, 0x52 };
1264 for ( int ind = 0; ind < 16; ind++ )
1265 pClassID[ind] = (byte)pTempClassID[ind];
1268 if ( pClassID != null )
1270 // create embedded object based on the class ID
1271 try {
1272 Object oEmbedCreator = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectCreator" );
1273 XEmbedObjectCreator xEmbedCreator = (XEmbedObjectCreator)UnoRuntime.queryInterface(
1274 XEmbedObjectCreator.class,
1275 oEmbedCreator );
1276 if ( xEmbedCreator != null )
1278 Object oEmbObj = xEmbedCreator.createInstanceInitNew( pClassID,
1279 "Dummy name",
1280 m_xStorage,
1281 "EmbedSub",
1282 new PropertyValue[0] );
1283 xEmbObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
1285 else
1286 JOptionPane.showMessageDialog( m_aFrame,
1287 "Can't create EmbedCreator!",
1288 "Error:",
1289 JOptionPane.ERROR_MESSAGE );
1291 catch( Exception e )
1293 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in createInstanceInitNew():", JOptionPane.ERROR_MESSAGE );
1296 else
1297 JOptionPane.showMessageDialog( m_aFrame, "Can't retrieve class ID!", "Error:", JOptionPane.ERROR_MESSAGE );
1299 return xEmbObj;
1302 public XEmbeddedObject createLinkObject( String aLinkURL )
1304 XEmbeddedObject xEmbObj = null;
1306 try {
1307 Object oLinkCreator = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectCreator" );
1308 XLinkCreator xLinkCreator = (XLinkCreator)UnoRuntime.queryInterface(
1309 XLinkCreator.class,
1310 oLinkCreator );
1311 if ( xLinkCreator != null )
1313 PropertyValue[] aMedDescr = { new PropertyValue(), new PropertyValue() };
1314 aMedDescr[0].Name = "URL";
1315 aMedDescr[0].Value = (Object) aLinkURL;
1316 aMedDescr[1].Name = "ReadOnly";
1317 aMedDescr[1].Value = (Object) Boolean.FALSE;
1318 Object oEmbObj = xLinkCreator.createInstanceLink( m_xStorage, "EmbedSub", aMedDescr, new PropertyValue[0] );
1319 xEmbObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
1321 else
1322 JOptionPane.showMessageDialog( m_aFrame,
1323 "Can't create LinkCreator!",
1324 "Error:",
1325 JOptionPane.ERROR_MESSAGE );
1327 catch( Exception e )
1329 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in createLinkObject():", JOptionPane.ERROR_MESSAGE );
1333 return xEmbObj;
1337 public XEmbeddedObject loadEmbedObject( String aFileURI )
1339 XEmbeddedObject xEmbObj = null;
1340 try {
1341 Object oEmbedCreator = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectCreator" );
1342 XEmbedObjectCreator xEmbedCreator = (XEmbedObjectCreator)UnoRuntime.queryInterface(
1343 XEmbedObjectCreator.class,
1344 oEmbedCreator );
1345 if ( xEmbedCreator != null )
1347 PropertyValue[] aMedDescr = { new PropertyValue(), new PropertyValue() };
1348 aMedDescr[0].Name = "URL";
1349 aMedDescr[0].Value = (Object) aFileURI;
1350 aMedDescr[1].Name = "ReadOnly";
1351 aMedDescr[1].Value = (Object) Boolean.FALSE;
1352 Object oEmbObj = xEmbedCreator.createInstanceInitFromMediaDescriptor( m_xStorage,
1353 "EmbedSub",
1354 aMedDescr,
1355 new PropertyValue[0] );
1356 xEmbObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
1358 else
1359 JOptionPane.showMessageDialog( m_aFrame,
1360 "Can't create EmbedFactory!",
1361 "Error:",
1362 JOptionPane.ERROR_MESSAGE );
1364 catch( Exception e )
1366 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in loadEmbedObject():", JOptionPane.ERROR_MESSAGE );
1369 return xEmbObj;
1372 public void clearObjectAndStorage()
1374 synchronized( m_oImageLock )
1376 m_aImage = null;
1379 m_nXScaling = 1;
1380 m_nYScaling = 1;
1381 m_nXPixelSize = 1;
1382 m_nYPixelSize = 1;
1384 m_bOwnFile = false;
1386 m_aLinkURI = null;
1387 m_bLinkObj = false;
1389 if ( m_xEmbedObj != null )
1391 try {
1392 XCloseable xClose = (XCloseable)UnoRuntime.queryInterface( XCloseable.class, m_xEmbedObj );
1393 if ( xClose != null )
1394 xClose.close( true );
1396 catch ( Exception ex )
1398 m_xEmbedObj = null;
1401 if ( m_xStorage != null )
1403 try {
1404 XComponent xComponent = (XComponent)UnoRuntime.queryInterface( XComponent.class, m_xStorage );
1405 if ( xComponent != null )
1406 xComponent.dispose();
1408 catch ( Exception ex )
1410 m_xStorage = null;
1414 public XStorage createTempStorage()
1416 XStorage xTempStorage = null;
1418 try {
1419 Object oStorageFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.StorageFactory" );
1420 XSingleServiceFactory xStorageFactory = (XSingleServiceFactory)UnoRuntime.queryInterface(
1421 XSingleServiceFactory.class,
1422 oStorageFactory );
1423 if ( xStorageFactory != null )
1425 Object oStorage = xStorageFactory.createInstance();
1426 xTempStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
1428 else
1429 JOptionPane.showMessageDialog( m_aFrame,
1430 "Can't create StorageFactory!",
1431 "Error:",
1432 JOptionPane.ERROR_MESSAGE );
1434 catch( Exception e )
1436 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in createTempStorage():", JOptionPane.ERROR_MESSAGE );
1439 return xTempStorage;
1442 public void saveObjectAsFileURI( String aFileURI )
1444 try {
1445 Object oStorageFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.StorageFactory" );
1446 XSingleServiceFactory xStorageFactory = (XSingleServiceFactory)UnoRuntime.queryInterface(
1447 XSingleServiceFactory.class,
1448 oStorageFactory );
1449 if ( xStorageFactory != null )
1451 XEmbedPersist xPersist = (XEmbedPersist)UnoRuntime.queryInterface( XEmbedPersist.class, m_xEmbedObj );
1452 if ( xPersist != null )
1454 Object aArgs[] = new Object[2];
1455 aArgs[0] = aFileURI;
1456 aArgs[1] = Integer.valueOf( ElementModes.ELEMENT_READWRITE );
1458 Object oStorage = xStorageFactory.createInstanceWithArguments( aArgs );
1459 XStorage xTargetStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
1461 PropertyValue aProps[] = { new PropertyValue() };
1462 aProps[0].Name = "StoreVisualReplacement";
1463 aProps[0].Value = Boolean.valueOf( m_bStoreVisRepl );
1465 xPersist.storeAsEntry( xTargetStorage, "EmbedSub", new PropertyValue[0], aProps );
1466 xPersist.saveCompleted( true );
1468 // the object must be already based on new storage
1469 XComponent xComponent = (XComponent)UnoRuntime.queryInterface( XComponent.class, m_xStorage );
1470 xComponent.dispose();
1472 m_xStorage = xTargetStorage;
1473 m_bOwnFile = true;
1475 XTransactedObject xTransact = (XTransactedObject)UnoRuntime.queryInterface( XTransactedObject.class,
1476 m_xStorage );
1477 if ( xTransact != null )
1478 xTransact.commit();
1480 else
1481 JOptionPane.showMessageDialog( m_aFrame, "No XEmbedPersist!", "Error:", JOptionPane.ERROR_MESSAGE );
1483 else
1484 JOptionPane.showMessageDialog( m_aFrame,
1485 "Can't create StorageFactory!",
1486 "Error:",
1487 JOptionPane.ERROR_MESSAGE );
1489 catch( Exception e )
1491 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in saveStorageToFileURI():", JOptionPane.ERROR_MESSAGE );
1496 public void loadFileURI( String aFileURI )
1500 Object oStorageFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.StorageFactory" );
1501 XSingleServiceFactory xStorageFactory = (XSingleServiceFactory)UnoRuntime.queryInterface(
1502 XSingleServiceFactory.class,
1503 oStorageFactory );
1504 Object aArgs[] = new Object[2];
1505 aArgs[0] = aFileURI;
1506 aArgs[1] = Integer.valueOf( ElementModes.ELEMENT_READWRITE );
1508 Object oStorage = xStorageFactory.createInstanceWithArguments( aArgs );
1509 XStorage xTargetStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
1511 Object oEmbedCreator = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectCreator" );
1512 XEmbedObjectCreator xEmbedCreator = (XEmbedObjectCreator)UnoRuntime.queryInterface(
1513 XEmbedObjectCreator.class,
1514 oEmbedCreator );
1516 XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface( XNameAccess.class,
1517 xTargetStorage );
1518 if ( xNameAccess == null )
1520 JOptionPane.showMessageDialog( m_aFrame, "No XNameAccess!", "Error:", JOptionPane.ERROR_MESSAGE );
1521 return;
1524 Object oEmbObj = null;
1525 if ( xNameAccess.hasByName( "LinkName" ) && xTargetStorage.isStreamElement( "LinkName" ) )
1528 else
1529 oEmbObj = xEmbedCreator.createInstanceInitFromEntry( xTargetStorage,
1530 "EmbedSub",
1531 false,
1532 new PropertyValue[0] );
1534 m_xEmbedObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
1536 if ( m_xEmbedObj != null )
1538 m_xStorage = xTargetStorage;
1539 m_bOwnFile = true;
1541 else
1542 JOptionPane.showMessageDialog( m_aFrame,
1543 "Can't create EmbedObject from storage!",
1544 "Error:",
1545 JOptionPane.ERROR_MESSAGE );
1547 catch( Exception e )
1549 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in loadFileURI():", JOptionPane.ERROR_MESSAGE );
1553 public void storeLinkToStorage()
1555 if ( m_xStorage != null && m_bLinkObj )
1557 try {
1558 XStream xLinkStream = m_xStorage.openStreamElement( "LinkName", ElementModes.ELEMENT_WRITE );
1560 if ( xLinkStream != null )
1562 XOutputStream xLinkOutStream = xLinkStream.getOutputStream();
1563 XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface( XTruncate.class,
1564 xLinkOutStream );
1565 if ( xLinkOutStream != null && xTruncate != null )
1567 xTruncate.truncate();
1569 char[] aLinkChar = m_aLinkURI.toCharArray();
1570 byte[] aLinkBytes = new byte[ aLinkChar.length ];
1571 for ( int ind = 0; ind < aLinkChar.length; ind++ )
1572 aLinkBytes[ind] = (byte)aLinkChar[ind];
1574 xLinkOutStream.writeBytes( aLinkBytes );
1575 xLinkOutStream.closeOutput();
1577 XComponent xComponent = (XComponent) UnoRuntime.queryInterface( XComponent.class,
1578 xLinkStream );
1579 if ( xComponent != null )
1580 xComponent.dispose();
1582 else
1583 JOptionPane.showMessageDialog( m_aFrame,
1584 "The substream can not be truncated or written!",
1585 "Error:",
1586 JOptionPane.ERROR_MESSAGE );
1589 else
1590 JOptionPane.showMessageDialog( m_aFrame,
1591 "Can't create/open substream!",
1592 "Error:",
1593 JOptionPane.ERROR_MESSAGE );
1595 catch( Exception e )
1597 JOptionPane.showMessageDialog( m_aFrame,
1599 "Exception in storeLinkToStorage:",
1600 JOptionPane.ERROR_MESSAGE );
1606 public void storeLinkAsFileURI( String aFileURI )
1608 try {
1609 Object oStorageFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.StorageFactory" );
1610 XSingleServiceFactory xStorageFactory = (XSingleServiceFactory)UnoRuntime.queryInterface(
1611 XSingleServiceFactory.class,
1612 oStorageFactory );
1613 if ( xStorageFactory != null )
1615 Object aArgs[] = new Object[2];
1616 aArgs[0] = aFileURI;
1617 aArgs[1] = Integer.valueOf( ElementModes.ELEMENT_READWRITE );
1619 Object oStorage = xStorageFactory.createInstanceWithArguments( aArgs );
1620 XStorage xTargetStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
1622 XComponent xComponent = (XComponent)UnoRuntime.queryInterface( XComponent.class, m_xStorage );
1623 xComponent.dispose();
1625 m_xStorage = xTargetStorage;
1626 m_bOwnFile = true;
1628 storeLinkToStorage();
1630 XTransactedObject xTransact = (XTransactedObject)UnoRuntime.queryInterface( XTransactedObject.class,
1631 m_xStorage );
1632 if ( xTransact != null )
1633 xTransact.commit();
1635 else
1636 JOptionPane.showMessageDialog( m_aFrame,
1637 "Can't create StorageFactory!",
1638 "Error:",
1639 JOptionPane.ERROR_MESSAGE );
1641 catch( Exception e )
1643 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in saveStorageToFileURI():", JOptionPane.ERROR_MESSAGE );
1647 public String getValidURL( String sFileURL )
1649 // m_xTransformer must be set!
1650 URL[] aURLs = { new URL() };
1651 aURLs[0].Complete = sFileURL;
1653 try {
1654 if ( !m_xTransformer.parseSmart( aURLs, "" ) )
1655 throw new Exception();
1657 catch( Exception e )
1659 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in getValidURL():", JOptionPane.ERROR_MESSAGE );
1662 return aURLs[0].Complete;
1665 public void disposeObject()
1667 // TODO:
1668 // usage of object, storage and bitmap painter should be locked
1669 // but since possibility of race condition is very low
1670 // it is not really required for testing application
1672 clearObjectAndStorage();
1674 if ( m_aBitmapPainter != null )
1676 m_aBitmapPainter.disconnectListener();
1677 m_aBitmapPainter = null;