1 package embeddedobj.test;
2 
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;
9 
10 import javax.swing.JOptionPane;
11 import javax.swing.Timer;
12 
13 import com.sun.star.lang.XMultiServiceFactory;
14 import com.sun.star.lang.XSingleServiceFactory;
15 
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;
21 
22 import com.sun.star.lang.XComponent;
23 
24 import com.sun.star.util.XCloseable;
25 import com.sun.star.util.XURLTransformer;
26 import com.sun.star.util.URL;
27 
28 import com.sun.star.beans.PropertyValue;
29 import com.sun.star.beans.NamedValue;
30 
31 import com.sun.star.datatransfer.DataFlavor;
32 import com.sun.star.datatransfer.XTransferable;
33 
34 import com.sun.star.container.XNameAccess;
35 
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;
40 
41 import com.sun.star.awt.XWindow;
42 import com.sun.star.awt.XBitmap;
43 
44 import com.sun.star.task.XJob;
45 
46 import com.sun.star.embed.*;
47 
48 
49 class ActionObject
50 {
51 	public byte m_nID;
52 	public String m_sParam;
53 
54 	public ActionObject()
55 	{
56 		m_nID = 0;
57 		m_sParam = null;
58 	}
59 
60 	public ActionObject( byte nID )
61 	{
62 		m_nID = nID;
63 		m_sParam = null;
64 	}
65 
66 	public ActionObject( byte nID, String sParam )
67 	{
68 		m_nID = nID;
69 		m_sParam = sParam;
70 	}
71 
72 	public ActionObject( ActionObject aObject )
73 	{
74 		m_nID = aObject.m_nID;
75 		m_sParam = aObject.m_sParam;
76 	}
77 };
78 
79 public class EmbedContApp extends Applet
80 	implements MouseListener, XEmbeddedClient, ActionListener, XJob, XInplaceClient, XWindowSupplier
81 {
82 	private XMultiServiceFactory m_xServiceFactory;
83 
84 	private final boolean m_bStoreVisRepl = false;
85 
86 	private XJob m_xMainThreadExecutor;
87 	private NamedValue[] m_pValuesForExecutor;
88 
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;
95 
96 	private Frame m_aFrame;
97 	private Menu m_aFileMenu;
98 	private Menu m_aObjectMenu;
99 	private Toolkit m_aToolkit;
100 
101 	private Image m_aImage;
102 	private Object m_oImageLock;
103 
104 	private boolean m_bOwnFile = false;
105 
106 	private boolean m_bLinkObj = false;
107 	private String m_aLinkURI;
108 
109 	private Object m_oActionsNumberLock;
110 	private Vector m_aActionsList;
111 
112 	private Timer m_aTimer;
113 	private boolean m_bDestroyed = false;
114 
115 	private Object m_oInHandlerLock;
116 	private boolean m_bInHandler = false;
117 
118 	private XURLTransformer m_xTransformer;
119 
120 	private NativeView m_aNativeView;
121 	private XWindow m_xVCLWindow;
122 
123 	private XBitmap m_xBitmap;
124 	private BitmapPainter m_aBitmapPainter;
125 
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;
139 
140 // Methods
141 	public EmbedContApp( Frame aFrame, XMultiServiceFactory xServiceFactory )
142 	{
143 		m_aFrame = aFrame;
144 		m_xServiceFactory = xServiceFactory;
145 	}
146 
147 	public void init()
148 	{
149 		resize( 800, 600 );
150 		setBackground( Color.gray );
151 
152 		m_aToolkit = Toolkit.getDefaultToolkit();
153 
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 ); }
158 
159 		m_oActionsNumberLock = new Object();
160 		m_aActionsList = new Vector();
161 
162 		m_oInHandlerLock = new Object();
163 		m_oImageLock = new Object();
164 
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 ) {}
169 
170 		if ( m_xMainThreadExecutor == null )
171 		{
172 			System.out.println( "Can't create MainThreadExecutor! The application is unusable!" );
173 			System.exit( 0 );
174 		}
175 
176 		m_nXScaling = 1;
177 		m_nYScaling = 1;
178 		m_nXPixelSize = 1;
179 		m_nYPixelSize = 1;
180 
181 		m_pValuesForExecutor = new NamedValue[1];
182 		m_pValuesForExecutor[0] = new NamedValue( "JobToExecute", (Object)this );
183 
184 		m_aTimer = new Timer( 100, this );
185 		m_aTimer.start();
186 
187 		// Get a menu bar.
188 		MenuBar aMenuBar = m_aFrame.getMenuBar();
189 		if( aMenuBar == null )
190 		{
191 			aMenuBar = new MenuBar();
192 			m_aFrame.setMenuBar( aMenuBar );
193 		}
194 
195 		// Create menus for the menu bar.
196 
197 		// File menu
198 		m_aFileMenu = new Menu( "File", true );
199 		aMenuBar.add( m_aFileMenu );
200 
201 		MenuItem aItem = new NewMenuItem();
202 		m_aFileMenu.add( aItem );
203 
204 		aItem = new OpenFileMenuItem();
205 		m_aFileMenu.add( aItem );
206 
207 		aItem = new SaveMenuItem();
208 		m_aFileMenu.add( aItem );
209 
210 		aItem = new SaveAsMenuItem();
211 		m_aFileMenu.add( aItem );
212 
213 		// Object menu
214 		m_aObjectMenu = new Menu( "Object", true );
215 		aMenuBar.add( m_aObjectMenu );
216 
217 		aItem = new NewObjectMenuItem();
218 		m_aObjectMenu.add( aItem );
219 
220 		aItem = new LoadObjectMenuItem();
221 		m_aObjectMenu.add( aItem );
222 
223 		aItem = new LinkObjectMenuItem();
224 		m_aObjectMenu.add( aItem );
225 
226 		aItem = new ConvertLinkToEmbedMenuItem();
227 		m_aObjectMenu.add( aItem );
228 
229 		// Activation menu
230 		m_aObjectMenu = new Menu( "Activation", true );
231 		aMenuBar.add( m_aObjectMenu );
232 
233 		aItem = new ActivateOutplaceMenuItem();
234 		m_aObjectMenu.add( aItem );
235 
236 		aItem = new ActivateInplaceMenuItem();
237 		m_aObjectMenu.add( aItem );
238 
239 		aItem = new DeactivateMenuItem();
240 		m_aObjectMenu.add( aItem );
241 
242 		m_aNativeView = new NativeView();
243 		m_aNativeView.resize( 800, 600 );
244 		this.add( m_aNativeView );
245 
246 		// Handle mouse clicks in our window.
247 //		addMouseListener( this );
248 	}
249 
250 	public void actionPerformed( ActionEvent evt )
251 	{
252 		synchronized( m_oInHandlerLock )
253 		{
254 			if ( m_bInHandler )
255 				return;
256 			m_bInHandler = true;
257 		}
258 
259 		synchronized( m_oActionsNumberLock )
260 		{
261 			if ( m_aActionsList.size() > 0 )
262 			{
263 				try {
264 					m_xMainThreadExecutor.execute( m_pValuesForExecutor );
265 				}
266 				catch( Exception e )
267 				{
268 					System.out.println( "Exception in actionPerformed() : " + e );
269 				}
270 			}
271 			else
272 			{
273 				synchronized( m_oInHandlerLock )
274 				{
275 					m_bInHandler = false;
276 				}
277 			}
278 		}
279 	}
280 
281 	// XWindowSupplier
282 	public XWindow getWindow()
283 	{
284 		return m_xVCLWindow;
285 	}
286 
287 	// XEmbeddedClient
288 	public void saveObject()
289 		throws com.sun.star.uno.Exception
290 	{
291 		if ( m_xEmbedObj != null )
292 		{
293 			try {
294 				XEmbedPersist xPersist = (XEmbedPersist)UnoRuntime.queryInterface( XEmbedPersist.class, m_xEmbedObj );
295 				if ( xPersist != null )
296 				{
297 					xPersist.storeOwn();
298 					generateNewImage();
299 				}
300 				else
301 					JOptionPane.showMessageDialog( m_aFrame, "No XEmbedPersist!", "Error:", JOptionPane.ERROR_MESSAGE );
302 			}
303 			catch( Exception e )
304 			{
305 				JOptionPane.showMessageDialog( m_aFrame, e, "Exception in saveObject:", JOptionPane.ERROR_MESSAGE );
306 			}
307 		}
308 
309 		generateNewImage();
310 		repaint();
311 	}
312 
313 	public void onShowWindow( boolean bVisible )
314 	{
315 		// for now nothing to do
316 	}
317 
318 	// XInplaceClient
319 	public boolean canInplaceActivate()
320 	{
321 		return true;
322 	}
323 
324 	public void onInplaceActivate()
325 	{
326 		// TODO
327 		// prepare for inplace activation
328 
329 		// REMOVE
330 		// workaround for CLIPCHILDREN problem
331 		if ( m_aBitmapPainter != null )
332 			m_aBitmapPainter.stopPainting();
333 	}
334 
335 	public void onUIActivate()
336 	{
337 		// TODO
338 		// prepare for UI activate
339 	}
340 
341 	public void onInplaceDeactivate()
342 	{
343 		// TODO
344 		// inplace deactivation is done
345 
346 		// REMOVE
347 		// workaround for CLIPCHILDREN problem
348 		if ( m_aBitmapPainter != null )
349 			m_aBitmapPainter.startPainting();
350 	}
351 
352 	public void onUIDeactivate()
353 	{
354 		// TODO
355 		// prepare for UI deactivate
356 	}
357 
358 	public XIPMainContainerWindow getTopmostWindow()
359 	{
360 		// TODO
361 		// return an implementation of XIPMainContainerWindow
362 		// mainly required for ui activation
363 		// dummy implementation is enough for inplace activation
364 
365 		return null;
366 	}
367 
368 	public XInplaceUIWindow getDocumentWindow()
369 	{
370 		// TODO
371 		// return implementation of XInplaceUIWindow
372 		// mainly required for ui activation
373 		// dummy implementation is enough for inplace activation
374 
375 		return null;
376 	}
377 
378 	public com.sun.star.awt.Rectangle getPosRect()
379 	{
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 );
388 		}
389 		catch( Exception e )
390 		{
391 			System.out.println( "Position rectangle generation failed!" );
392 		}
393 
394 		return new com.sun.star.awt.Rectangle( 10, 10, 110, 110 );
395 	}
396 
397 	public com.sun.star.awt.Rectangle getClipRect()
398 	{
399 		// provide clip rectangle to the object
400 		// in this application position and clip rectangles are the same
401 
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 );
409 		}
410 		catch( Exception e )
411 		{
412 			System.out.println( "Clip rectangle generation failed!" );
413 		}
414 
415 		return new com.sun.star.awt.Rectangle( 10, 10, 110, 110 );
416 	}
417 
418 	public void translateAccelerators( com.sun.star.awt.KeyEvent[] aKeys )
419 	{
420 		// TODO
421 		// an accelerator table for object
422 		// ui activation related
423 	}
424 
425 	public void scrollObj( com.sun.star.awt.Size aOffset )
426 	{
427 		// TODO
428 		// scrolls the object to a specified offset
429 		// not mandatory for the testing application :)
430 	}
431 
432 	public void onPosRectChange( com.sun.star.awt.Rectangle aPosRect )
433 	{
434 		// object asks to change the position
435 		if ( m_xEmbedObj != null )
436 		{
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 )
443 			 	{
444 					XInplaceObject xInplObj = (XInplaceObject)UnoRuntime.queryInterface( XInplaceObject.class, m_xEmbedObj );
445 			 		if ( xInplObj != null )
446 					{
447 						xInplObj.setObjectRects( aPosRect, aPosRect ); // show the whole object
448 						if ( m_aBitmapPainter != null )
449 							m_aBitmapPainter.setRect( aPosRect );
450 					}
451 					else
452 			 			System.out.println( "Why object that does not support inplace activation behave like inplace object?!" );
453 			 	}
454 			 	else
455 			 		System.out.println( "The object is not active but asks to change visual area!" );
456 			} catch( Exception e )
457 			{
458 			 	System.out.println( "Exception is thrown in onPosRectChange: " + e );
459 			}
460 		}
461 		else
462 		 	System.out.println( "Who asks to change visual area?!!" );
463 	}
464 
465 	// XJob
466 	public Object execute( NamedValue[] pValues )
467 	{
468 		for( int nInd = 0; nInd < m_aActionsList.size(); nInd++ )
469 		{
470 			ActionObject aAction = ( ActionObject ) m_aActionsList.get( nInd );
471 			if ( aAction != null )
472 			{
473 				if ( aAction.m_nID == DESTROY )
474 				{
475 					// free all resources
476 					clearObjectAndStorage();
477 					m_bDestroyed = true;
478 				}
479 				else if ( aAction.m_nID == ACTIVATE_OUTPLACE )
480 				{
481 					// activate object if exists and not active
482 					if ( m_xEmbedObj != null )
483 					{
484 						try {
485 							m_xEmbedObj.changeState( EmbedStates.EMBED_ACTIVE );
486 						}
487 						catch( Exception ex )
488 						{
489 							System.out.println( "Exception on mouse click" + ex );
490 						}
491 					}
492 				}
493 				else if ( aAction.m_nID == NEW_DOCUMENT )
494 				{
495 					// clear everything
496 					clearObjectAndStorage();
497 
498 					repaint();
499 				}
500 				else if ( aAction.m_nID == SAVE_AS )
501 				{
502 					// open SaveAs dialog and store
503 
504 					if ( m_xStorage != null && m_xEmbedObj != null )
505 					{
506 						try {
507 							/*
508 								if ( m_bLinkObj )
509 									storeLinkAsFileURI( aFileURI );
510 								else
511 							*/
512 							saveObjectAsFileURI( aAction.m_sParam );
513 						}
514 						catch( Exception ex )
515 						{
516 							System.out.println( "Exception in SaveAsMenuItem: " + ex );
517 						}
518 					}
519 				}
520 				else if ( aAction.m_nID == OPEN_FILE )
521 				{
522 					// clear everything
523 					clearObjectAndStorage();
524 
525 					// load from specified file
526 					loadFileURI( aAction.m_sParam );
527 
528 					if ( m_xEmbedObj != null )
529 					{
530 						try {
531 							m_xEmbedObj.setClientSite( this );
532 						}
533 						catch( Exception ex )
534 						{
535 							System.out.println( "Exception in OpenFileMenuItem: " + ex );
536 						}
537 					}
538 
539 					generateNewImage();
540 					repaint();
541 				}
542 				else if ( aAction.m_nID == SAVE )
543 				{
544 					if ( m_xStorage != null && m_xEmbedObj != null )
545 					{
546 						// if has persistance store there
547 						// if not it is and error, SaveAs had to be used
548 
549 						if ( m_bOwnFile )
550 						{
551 							if ( m_xStorage != null )
552 							{
553 								try {
554 									saveObject();
555 
556 									if ( m_bLinkObj )
557 										storeLinkToStorage();
558 
559 									XTransactedObject xTransact = (XTransactedObject)UnoRuntime.queryInterface( XTransactedObject.class,
560 																												m_xStorage );
561 									if ( xTransact != null )
562 										xTransact.commit();
563 								}
564 								catch( Exception ex )
565 								{
566 									System.out.println( "Exception during save operation in SaveMenuItem:" + ex );
567 								}
568 							}
569 							else
570 							{
571 								System.out.println( "No storage for owned file!" );
572 							}
573 						}
574 						else
575 						{
576 							System.out.println( "No owned file!" );
577 						}
578 					}
579 				}
580 				else if ( aAction.m_nID == NEW_OBJECT )
581 				{
582 					// remove current object an init a new one
583 					clearObjectAndStorage();
584 
585 					if ( aAction.m_sParam != null )
586 					{
587 						m_xStorage = createTempStorage();
588 
589 						if ( m_xStorage != null )
590 							m_xEmbedObj = createEmbedObject( aAction.m_sParam );
591 						else
592 							System.out.println( "Can't create temporary storage!" );
593 
594 						if ( m_xEmbedObj != null )
595 						{
596 							try {
597 								m_xEmbedObj.setClientSite( this );
598 							}
599 							catch( Exception ex )
600 							{
601 								System.out.println( "Exception in NewObjectMenuItem:" + ex );
602 							}
603 						}
604 					}
605 
606 					generateNewImage();
607 					repaint();
608 				}
609 				else if ( aAction.m_nID == OBJECT_FROM_FILE )
610 				{
611 					// first remove current object
612 					clearObjectAndStorage();
613 
614 					// create object from specified file
615 					m_xStorage = createTempStorage();
616 
617 					if ( m_xStorage != null )
618 						m_xEmbedObj = loadEmbedObject( aAction.m_sParam );
619 
620 					if ( m_xEmbedObj != null )
621 					{
622 						try {
623 							m_xEmbedObj.setClientSite( this );
624 						}
625 						catch( Exception ex )
626 						{
627 							System.out.println( "Exception in LoadObjectMenuItem: " + ex );
628 						}
629 					}
630 
631 					generateNewImage();
632 					repaint();
633 				}
634 				else if ( aAction.m_nID == LINK_FROM_FILE )
635 				{
636 					// first remove current object
637 					clearObjectAndStorage();
638 
639 					m_xStorage = createTempStorage();
640 
641 					// create object from specified file
642 					m_xEmbedObj = createLinkObject( aAction.m_sParam );
643 
644 					if ( m_xEmbedObj != null )
645 					{
646 						m_aLinkURI = aAction.m_sParam;
647 						m_bLinkObj = true;
648 
649 						try {
650 							m_xEmbedObj.setClientSite( this );
651 						}
652 						catch( Exception ex )
653 						{
654 							System.out.println( "Exception in LinkObjectMenuItem:" + ex );
655 						}
656 					}
657 
658 					generateNewImage();
659 					repaint();
660 				}
661 				else if ( aAction.m_nID == CONVERT_LINK_TO_OBJECT )
662 				{
663 					if ( !m_bLinkObj )
664 					{
665 						System.out.println( "The object is not a link!" );
666 						continue;
667 					}
668 
669 					if ( m_xEmbedObj != null )
670 					{
671 						if ( m_xStorage != null )
672 						{
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" );
678 
679 								XLinkageSupport xLinkage = (XLinkageSupport)UnoRuntime.queryInterface( XLinkageSupport.class,
680 																										m_xEmbedObj );
681 								if ( xLinkage != null )
682 								{
683 									xLinkage.breakLink( m_xStorage, "EmbedSub" );
684 									m_bLinkObj = false;
685 									m_aLinkURI = null;
686 								}
687 								else
688 									System.out.println( "No XLinkageSupport in ConvertLink... !" );
689 							}
690 							catch( Exception e1 )
691 							{
692 								System.out.println( "Exception in ConvertLinkToEmbed:try 1 :" + e1 );
693 							}
694 						}
695 					}
696 				}
697 				else if ( aAction.m_nID == ACTIVATE_INPLACE )
698 				{
699 					// activate object
700 					if ( m_xEmbedObj != null )
701 					{
702 						// in general it is better to check acceptable states
703 						try {
704 							m_xEmbedObj.changeState( EmbedStates.EMBED_INPLACE_ACTIVE );
705 						}
706 						catch( Exception ex )
707 						{
708 							System.out.println( "Exception on inplace activation " + ex );
709 						}
710 					}
711 				}
712 				else if ( aAction.m_nID == DEACTIVATE )
713 				{
714 					// activate object
715 
716 					if ( m_xEmbedObj != null )
717 					{
718 						int nOldState = -1;
719 						try {
720 							nOldState = m_xEmbedObj.getCurrentState();
721 						} catch( Exception e )
722 						{}
723 
724 						if ( nOldState == EmbedStates.EMBED_ACTIVE
725 						  || nOldState == EmbedStates.EMBED_INPLACE_ACTIVE
726 						  || nOldState == EmbedStates.EMBED_UI_ACTIVE )
727 						{
728 							try {
729 								m_xEmbedObj.changeState( EmbedStates.EMBED_RUNNING );
730 							}
731 							catch( Exception ex )
732 							{
733 								System.out.println( "Exception on inplace activation " + ex );
734 							}
735 						}
736 						else
737 						{
738 							System.out.println( "Deactivation of nonactive object!" );
739 						}
740 					}
741 				}
742 				else
743 				{
744 					System.out.println( "Unknoun action is requested: " + aAction.m_nID + "\n" );
745 				}
746 			}
747 		}
748 
749 		m_aActionsList.clear();
750 
751 		synchronized( m_oInHandlerLock )
752 		{
753 			m_bInHandler = false;
754 		}
755 
756 		return Any.VOID;
757 	}
758 
759 	public void actionRegister( byte nActionID, String sParam )
760 	{
761 		synchronized( m_oActionsNumberLock )
762 		{
763 			int nSize = m_aActionsList.size();
764 			if ( nSize < 199 )
765 			{
766 				if ( nSize == 0 )
767 					m_aActionsList.add( new ActionObject( nActionID, sParam ) );
768 				else
769 				{
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 ) );
773 				}
774 			}
775 		}
776 	}
777 
778 	public void SaveAsOperation()
779 	{
780 		if ( m_xStorage != null && m_xEmbedObj != null )
781 		{
782 			FileDialog aFileDialog = new FileDialog( m_aFrame, "SaveAs", FileDialog.SAVE );
783 			aFileDialog.show();
784 			if ( aFileDialog.getFile() != null )
785 			{
786 				String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
787 				File aFile = new File( aFileName );
788 				if ( aFile != null )
789 				{
790 					// create object from specified file
791 					String aFileURI = getValidURL( aFile.toURI().toASCIIString() );
792 					actionRegister( SAVE_AS, aFileURI );
793 				}
794 			}
795 		}
796 		else
797 			JOptionPane.showMessageDialog( m_aFrame, "No document is embedded!", "Error:", JOptionPane.ERROR_MESSAGE );
798 
799 	}
800 
801 	public void destroy()
802 	{
803 		// redirect the call through the timer and call super.destroy();
804 		actionRegister( DESTROY, null );
805 
806 		for ( int i = 0; i < 3 && !m_bDestroyed; i++ )
807 		{
808 			try {
809 				Thread.sleep( 200 );
810 			} catch ( Exception e )
811 			{}
812 		}
813 
814 		if ( !m_bDestroyed )
815 			System.out.println( "The object application can not exit correctly!" );
816 
817 		m_aTimer.stop();
818 
819 		super.destroy();
820 	}
821 
822 	public void update( Graphics g )
823 	{
824 		paint( g );
825 	}
826 
827 	public void paint( Graphics g )
828 	{
829 		super.paint( g );
830 
831 		// m_aNativeView.paint( g );
832 
833 		createVclWindow();
834 	}
835 
836 	public void createVclWindow()
837 	{
838 		synchronized( m_oImageLock )
839 		{
840 			if ( m_xVCLWindow == null && m_xServiceFactory != null && m_xEmbedObj != null && m_xBitmap != null )
841 			{
842 				java.awt.Rectangle aBounds = getBounds();
843 				m_xVCLWindow = WindowHelper.createWindow( m_xServiceFactory, m_aNativeView, aBounds );
844 				m_xVCLWindow.setVisible( true );
845 
846 				com.sun.star.awt.Size aBitmapSize = new com.sun.star.awt.Size( 200, 100 );
847 
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;
853 				}
854 				catch( Exception e )
855 				{
856 				}
857 
858 				if ( m_xBitmap != null )
859 				 	aBitmapSize = m_xBitmap.getSize();
860 
861 				System.out.println( "The visual area is Width = " + aBitmapSize.Width + "; Height = " + aBitmapSize.Height );
862 
863 				com.sun.star.awt.Rectangle aRect = new com.sun.star.awt.Rectangle(
864 														10,
865 														10,
866 														Math.min( (int)aBounds.getWidth() - 20, aBitmapSize.Width ),
867 														Math.min( (int)aBounds.getHeight() - 20, aBitmapSize.Height ) );
868 
869 				m_aBitmapPainter = new BitmapPainter( m_xMainThreadExecutor, m_xVCLWindow, m_xBitmap, aRect );
870 			}
871 		}
872 	}
873 
874 	public void generateNewImage()
875 	{
876 		if ( m_xEmbedObj != null )
877 		{
878 			try {
879 				int nOldState = m_xEmbedObj.getCurrentState();
880 				int nState = nOldState;
881 				if ( nOldState == EmbedStates.EMBED_LOADED )
882 				{
883 					m_xEmbedObj.changeState( EmbedStates.EMBED_RUNNING );
884 					nState = EmbedStates.EMBED_RUNNING;
885 				}
886 
887 				if ( nState == EmbedStates.EMBED_UI_ACTIVE || nState == EmbedStates.EMBED_INPLACE_ACTIVE
888 				  || nState == EmbedStates.EMBED_ACTIVE || nState == EmbedStates.EMBED_RUNNING )
889 				{
890 					XComponentSupplier xCompProv = (XComponentSupplier)UnoRuntime.queryInterface(
891 																					XComponentSupplier.class,
892 																					m_xEmbedObj );
893 					if ( xCompProv != null )
894 					{
895 						XCloseable xCloseable = xCompProv.getComponent();
896 						XTransferable xTransfer = (XTransferable)UnoRuntime.queryInterface(
897 																					XTransferable.class,
898 																					xCloseable );
899 						if ( xTransfer != null )
900 						{
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 );
905 
906 							Object aAny = xTransfer.getTransferData( aFlavor );
907 							if ( aAny != null && AnyConverter.isArray( aAny ) )
908 							{
909 								synchronized( m_oImageLock )
910 								{
911 									m_xBitmap = WindowHelper.getVCLBitmapFromBytes( m_xServiceFactory, aAny );
912 									if ( m_aBitmapPainter != null )
913 									{
914 										m_aBitmapPainter.setBitmap( m_xBitmap );
915 
916 										if ( m_xBitmap != null )
917 										{
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 );
924 											}
925 											catch( Exception e )
926 											{
927 											}
928 										}
929 									}
930 								}
931 							}
932 						}
933 						else
934 							System.out.println( "paint() : can not get XTransferable for the component!\n" );
935 					}
936 					else
937 						System.out.println( "paint() : XComponentSupplier is not implemented!\n" );
938 				}
939 			}
940 			catch( com.sun.star.uno.Exception e )
941 			{
942 				// dialogs should not be used in paint()
943 				System.out.println( "Exception in paint(): " + e );
944 			}
945 		}
946 	}
947 
948 	public void mouseClicked( MouseEvent e )
949 	{
950 		if( e.getModifiers() == InputEvent.BUTTON1_MASK )
951 		{
952 			actionRegister( ACTIVATE_OUTPLACE, null );
953 		}
954 	}
955 
956 	public void mousePressed( MouseEvent e ){};
957 	public void mouseEntered( MouseEvent e ){};
958 	public void mouseExited( MouseEvent e ){};
959 	public void mouseReleased( MouseEvent e ){};
960 
961 	// classes
962 	class NewMenuItem extends MenuItem implements ActionListener // Menu New
963 	{
964 		public NewMenuItem()
965 		{
966 			super( "New", new MenuShortcut( KeyEvent.VK_A ));
967 			addActionListener( this );
968 		}
969 
970 		public void actionPerformed( ActionEvent e )
971 		{
972 			actionRegister( NEW_DOCUMENT, null );
973 		}
974 	}
975 
976 	class SaveAsMenuItem extends MenuItem implements ActionListener // Menu SaveAs...
977 	{
978 		public SaveAsMenuItem()
979 		{
980 			super( "SaveAs..." );
981 			addActionListener( this );
982 		}
983 
984 		public void actionPerformed( ActionEvent e )
985 		{
986 			// open SaveAs dialog and store
987 
988 			SaveAsOperation();
989 		}
990 	}
991 
992 	class OpenFileMenuItem extends MenuItem implements ActionListener // Menu Open
993 	{
994 		public OpenFileMenuItem()
995 		{
996 			super( "Open", new MenuShortcut( KeyEvent.VK_C ));
997 			addActionListener( this );
998 		}
999 
1000 		public void actionPerformed( ActionEvent e )
1001 		{
1002 			// open OpenFile dialog and load doc
1003 			FileDialog aFileDialog = new FileDialog( m_aFrame, "Open" );
1004 			aFileDialog.show();
1005 			if ( aFileDialog.getFile() != null )
1006 			{
1007 				String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
1008 				File aFile = new File( aFileName );
1009 				if ( aFile != null )
1010 				{
1011 					// create object from specified file
1012 					String aFileURI = getValidURL( aFile.toURI().toASCIIString() );
1013 					actionRegister( OPEN_FILE, aFileURI );
1014 				}
1015 			}
1016 		}
1017 	}
1018 
1019 	class SaveMenuItem extends MenuItem implements ActionListener // Menu Save
1020 	{
1021 		public SaveMenuItem()
1022 		{
1023 			super( "Save", new MenuShortcut( KeyEvent.VK_D ));
1024 			addActionListener( this );
1025 		}
1026 
1027 		public void actionPerformed( ActionEvent e )
1028 		{
1029 			// if has persistance store there
1030 			// if not open SaveAs dialog and store
1031 			if ( m_xStorage != null && m_xEmbedObj != null )
1032 			{
1033 				if ( m_bOwnFile )
1034 				{
1035 					if ( m_xStorage == null )
1036 					{
1037 						JOptionPane.showMessageDialog( m_aFrame,
1038 														"No storage for oned file!",
1039 														"Error:",
1040 														JOptionPane.ERROR_MESSAGE );
1041 
1042 						return;
1043 					}
1044 
1045 					actionRegister( SAVE, null );
1046 				}
1047 				else
1048 				{
1049 					SaveAsOperation();
1050 				}
1051 			}
1052 			else
1053 				JOptionPane.showMessageDialog( m_aFrame, "No document is embedded!", "Error:", JOptionPane.ERROR_MESSAGE );
1054 		}
1055 	}
1056 
1057 	class NewObjectMenuItem extends MenuItem implements ActionListener // Menu NewObject
1058 	{
1059 		public NewObjectMenuItem()
1060 		{
1061 			super( "Create", new MenuShortcut( KeyEvent.VK_N ));
1062 			addActionListener( this );
1063 		}
1064 
1065 		public void actionPerformed( ActionEvent e )
1066 		{
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" };
1075 
1076 			String selectedValue = (String)JOptionPane.showInputDialog( null, "DocumentType", "Select",
1077        																	JOptionPane.INFORMATION_MESSAGE, null,
1078        																	possibleValues, possibleValues[0] );
1079 
1080 			actionRegister( NEW_OBJECT, selectedValue );
1081 		}
1082 	}
1083 
1084 	class LoadObjectMenuItem extends MenuItem implements ActionListener // Menu LoadObject
1085 	{
1086 		public LoadObjectMenuItem()
1087 		{
1088 			super( "Load from file", new MenuShortcut( KeyEvent.VK_L ));
1089 			addActionListener( this );
1090 		}
1091 
1092 		public void actionPerformed( ActionEvent e )
1093 		{
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 )
1098 			{
1099 				String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
1100 				File aFile = new File( aFileName );
1101 				if ( aFile != null )
1102 				{
1103 					// create object from specified file
1104 					String aFileURI = getValidURL( aFile.toURI().toASCIIString() );
1105 					actionRegister( OBJECT_FROM_FILE, aFileURI );
1106 				}
1107 			}
1108 		}
1109 	}
1110 
1111 	class LinkObjectMenuItem extends MenuItem implements ActionListener // Menu LinkObject
1112 	{
1113 		public LinkObjectMenuItem()
1114 		{
1115 			super( "Create link", new MenuShortcut( KeyEvent.VK_M ));
1116 			addActionListener( this );
1117 		}
1118 
1119 		public void actionPerformed( ActionEvent e )
1120 		{
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 )
1125 			{
1126 				String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
1127 				File aFile = new File( aFileName );
1128 				if ( aFile != null )
1129 				{
1130 					// create object from specified file
1131 					String aFileURI = getValidURL( aFile.toURI().toASCIIString() );
1132 					actionRegister( LINK_FROM_FILE, aFileURI );
1133 				}
1134 			}
1135 		}
1136 	}
1137 
1138 	class ConvertLinkToEmbedMenuItem extends MenuItem implements ActionListener // Menu LinkObject
1139 	{
1140 		public ConvertLinkToEmbedMenuItem()
1141 		{
1142 			super( "Convert link to embed", new MenuShortcut( KeyEvent.VK_M ));
1143 			addActionListener( this );
1144 		}
1145 
1146 		public void actionPerformed( ActionEvent e )
1147 		{
1148 			actionRegister( CONVERT_LINK_TO_OBJECT, null );
1149 		}
1150 	}
1151 
1152 	class ActivateOutplaceMenuItem extends MenuItem implements ActionListener // Menu ActiveteOutplace
1153 	{
1154 		public ActivateOutplaceMenuItem()
1155 		{
1156 			super( "Activate outplace", new MenuShortcut( KeyEvent.VK_A ));
1157 			addActionListener( this );
1158 		}
1159 
1160 		public void actionPerformed( ActionEvent e )
1161 		{
1162 			actionRegister( ACTIVATE_OUTPLACE, null );
1163 		}
1164 	}
1165 
1166 	class ActivateInplaceMenuItem extends MenuItem implements ActionListener // Menu ActivateInplace
1167 	{
1168 		public ActivateInplaceMenuItem()
1169 		{
1170 			super( "Activate inplace", new MenuShortcut( KeyEvent.VK_I ));
1171 			addActionListener( this );
1172 		}
1173 
1174 		public void actionPerformed( ActionEvent e )
1175 		{
1176 			actionRegister( ACTIVATE_INPLACE, null );
1177 		}
1178 	}
1179 
1180 	class DeactivateMenuItem extends MenuItem implements ActionListener // Menu Deactivate
1181 	{
1182 		public DeactivateMenuItem()
1183 		{
1184 			super( "Deactivate", new MenuShortcut( KeyEvent.VK_D ));
1185 			addActionListener( this );
1186 		}
1187 
1188 		public void actionPerformed( ActionEvent e )
1189 		{
1190 			actionRegister( DEACTIVATE, null );
1191 		}
1192 	}
1193 
1194 	// Helper methods
1195 	public XEmbeddedObject createEmbedObject( String aServiceName )
1196 	{
1197 		XEmbeddedObject xEmbObj = null;
1198 		byte[] pClassID = new byte[16];
1199 
1200 		if ( aServiceName.equals( "com.sun.star.comp.Writer.TextDocument" ) )
1201 		{
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];
1206 		}
1207 		else if ( aServiceName.equals( "com.sun.star.comp.Writer.GlobalDocument" ) )
1208 		{
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];
1213 		}
1214 		else if ( aServiceName.equals( "com.sun.star.comp.Writer.WebDocument" ) )
1215 		{
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];
1220 		}
1221 		else if ( aServiceName.equals( "com.sun.star.comp.Calc.SpreadsheetDocument" ) )
1222 		{
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];
1227 		}
1228 		else if ( aServiceName.equals( "com.sun.star.comp.Draw.PresentationDocument" ) )
1229 		{
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];
1234 		}
1235 		else if ( aServiceName.equals( "com.sun.star.comp.Draw.DrawingDocument" ) )
1236 		{
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];
1241 		}
1242 		else if ( aServiceName.equals( "com.sun.star.comp.Math.FormulaDocument" ) )
1243 		{
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];
1248 		}
1249 		else if ( aServiceName.equals( "BitmapImage" ) )
1250 		{
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];
1255 		}
1256 
1257 		if ( pClassID != null )
1258 		{
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 )
1266 				{
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 );
1273 				}
1274 				else
1275 					JOptionPane.showMessageDialog( m_aFrame,
1276 												   "Can't create EmbedCreator!",
1277 												   "Error:",
1278 												   JOptionPane.ERROR_MESSAGE );
1279 			}
1280 			catch( Exception e )
1281 			{
1282 				JOptionPane.showMessageDialog( m_aFrame, e, "Exception in createInstanceInitNew():", JOptionPane.ERROR_MESSAGE );
1283 			}
1284 		}
1285 		else
1286 			JOptionPane.showMessageDialog( m_aFrame, "Can't retrieve class ID!", "Error:", JOptionPane.ERROR_MESSAGE );
1287 
1288 		return xEmbObj;
1289 	}
1290 
1291 	public XEmbeddedObject createLinkObject( String aLinkURL )
1292 	{
1293 		XEmbeddedObject xEmbObj = null;
1294 
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 )
1301 			{
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 );
1309 			}
1310 			else
1311 				JOptionPane.showMessageDialog( m_aFrame,
1312 											   "Can't create LinkCreator!",
1313 											   "Error:",
1314 											   JOptionPane.ERROR_MESSAGE );
1315 		}
1316 		catch( Exception e )
1317 		{
1318 			JOptionPane.showMessageDialog( m_aFrame, e, "Exception in createLinkObject():", JOptionPane.ERROR_MESSAGE );
1319 		}
1320 
1321 
1322 		return xEmbObj;
1323 	}
1324 
1325 
1326 	public XEmbeddedObject loadEmbedObject( String aFileURI )
1327 	{
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 )
1335 			{
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 );
1346 			}
1347 			else
1348 				JOptionPane.showMessageDialog( m_aFrame,
1349 											   "Can't create EmbedFactory!",
1350 											   "Error:",
1351 											   JOptionPane.ERROR_MESSAGE );
1352 		}
1353 		catch( Exception e )
1354 		{
1355 			JOptionPane.showMessageDialog( m_aFrame, e, "Exception in loadEmbedObject():", JOptionPane.ERROR_MESSAGE );
1356 		}
1357 
1358 		return xEmbObj;
1359 	}
1360 
1361 	public void clearObjectAndStorage()
1362 	{
1363 		synchronized( m_oImageLock )
1364 		{
1365 			m_aImage = null;
1366 		}
1367 
1368 		m_nXScaling = 1;
1369 		m_nYScaling = 1;
1370 		m_nXPixelSize = 1;
1371 		m_nYPixelSize = 1;
1372 
1373 		m_bOwnFile = false;
1374 
1375 		m_aLinkURI = null;
1376 		m_bLinkObj = false;
1377 
1378 		if ( m_xEmbedObj != null )
1379 		{
1380 			try {
1381 				XCloseable xClose = (XCloseable)UnoRuntime.queryInterface( XCloseable.class, m_xEmbedObj );
1382 				if ( xClose != null )
1383 					xClose.close( true );
1384 			}
1385 			catch ( Exception ex )
1386 			{}
1387 			m_xEmbedObj = null;
1388 		}
1389 
1390 		if ( m_xStorage != null )
1391 		{
1392 			try {
1393 				XComponent xComponent = (XComponent)UnoRuntime.queryInterface( XComponent.class, m_xStorage );
1394 				if ( xComponent != null )
1395 					xComponent.dispose();
1396 			}
1397 			catch ( Exception ex )
1398 			{}
1399 			m_xStorage = null;
1400 		}
1401 	}
1402 
1403 	public XStorage createTempStorage()
1404 	{
1405 		XStorage xTempStorage = null;
1406 
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 )
1413 			{
1414 				Object oStorage = xStorageFactory.createInstance();
1415 				xTempStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
1416 			}
1417 			else
1418 				JOptionPane.showMessageDialog( m_aFrame,
1419 												"Can't create StorageFactory!",
1420 												"Error:",
1421 												JOptionPane.ERROR_MESSAGE );
1422 		}
1423 		catch( Exception e )
1424 		{
1425 			JOptionPane.showMessageDialog( m_aFrame, e, "Exception in createTempStorage():", JOptionPane.ERROR_MESSAGE );
1426 		}
1427 
1428 		return xTempStorage;
1429 	}
1430 
1431 	public void saveObjectAsFileURI( String aFileURI )
1432 	{
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 )
1439 			{
1440 				XEmbedPersist xPersist = (XEmbedPersist)UnoRuntime.queryInterface( XEmbedPersist.class, m_xEmbedObj );
1441 				if ( xPersist != null )
1442 				{
1443 					Object aArgs[] = new Object[2];
1444 					aArgs[0] = aFileURI;
1445 					aArgs[1] = new Integer( ElementModes.ELEMENT_READWRITE );
1446 
1447 					Object oStorage = xStorageFactory.createInstanceWithArguments( aArgs );
1448 					XStorage xTargetStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
1449 
1450 					PropertyValue aProps[] = { new PropertyValue() };
1451 					aProps[0].Name = "StoreVisualReplacement";
1452 					aProps[0].Value = new Boolean( m_bStoreVisRepl );
1453 
1454 					xPersist.storeAsEntry( xTargetStorage, "EmbedSub", new PropertyValue[0], aProps );
1455 					xPersist.saveCompleted( true );
1456 
1457 					// the object must be already based on new storage
1458 					XComponent xComponent = (XComponent)UnoRuntime.queryInterface( XComponent.class, m_xStorage );
1459 					xComponent.dispose();
1460 
1461 					m_xStorage = xTargetStorage;
1462 					m_bOwnFile = true;
1463 
1464 					XTransactedObject xTransact = (XTransactedObject)UnoRuntime.queryInterface( XTransactedObject.class,
1465 																							m_xStorage );
1466 					if ( xTransact != null )
1467 						xTransact.commit();
1468 				}
1469 				else
1470 					JOptionPane.showMessageDialog( m_aFrame, "No XEmbedPersist!", "Error:", JOptionPane.ERROR_MESSAGE );
1471 			}
1472 			else
1473 				JOptionPane.showMessageDialog( m_aFrame,
1474 												"Can't create StorageFactory!",
1475 												"Error:",
1476 												JOptionPane.ERROR_MESSAGE );
1477 		}
1478 		catch( Exception e )
1479 		{
1480 			JOptionPane.showMessageDialog( m_aFrame, e, "Exception in saveStorageToFileURI():", JOptionPane.ERROR_MESSAGE );
1481 		}
1482 
1483 	}
1484 
1485 	public void loadFileURI( String aFileURI )
1486 	{
1487 		try
1488 		{
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 );
1496 
1497 			Object oStorage = xStorageFactory.createInstanceWithArguments( aArgs );
1498 			XStorage xTargetStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
1499 
1500 			Object oEmbedCreator = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectCreator" );
1501 			XEmbedObjectCreator xEmbedCreator = (XEmbedObjectCreator)UnoRuntime.queryInterface(
1502 																					XEmbedObjectCreator.class,
1503 																					oEmbedCreator );
1504 
1505 			XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface( XNameAccess.class,
1506 																			xTargetStorage );
1507 			if ( xNameAccess == null )
1508 			{
1509 				JOptionPane.showMessageDialog( m_aFrame, "No XNameAccess!", "Error:", JOptionPane.ERROR_MESSAGE );
1510 				return;
1511 			}
1512 
1513 			Object oEmbObj = null;
1514 			if ( xNameAccess.hasByName( "LinkName" ) && xTargetStorage.isStreamElement( "LinkName" ) )
1515 			{
1516 			/*
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 )
1520 				{
1521 					XInputStream xInStream = xLinkStream.getInputStream();
1522 					if ( xInStream != null )
1523 					{
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;
1530 					}
1531 				}
1532 			*/
1533 			}
1534 			else
1535 				oEmbObj = xEmbedCreator.createInstanceInitFromEntry( xTargetStorage,
1536 																	"EmbedSub",
1537 																	false,
1538 																	new PropertyValue[0] );
1539 
1540 			m_xEmbedObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
1541 
1542 			if ( m_xEmbedObj != null )
1543 			{
1544 				m_xStorage = xTargetStorage;
1545 				m_bOwnFile = true;
1546 			}
1547 			else
1548 				JOptionPane.showMessageDialog( m_aFrame,
1549 											   "Can't create EmbedObject from storage!",
1550 											   "Error:",
1551 											   JOptionPane.ERROR_MESSAGE );
1552 		}
1553 		catch( Exception e )
1554 		{
1555 			JOptionPane.showMessageDialog( m_aFrame, e, "Exception in loadFileURI():", JOptionPane.ERROR_MESSAGE );
1556 		}
1557 	}
1558 
1559 	public void storeLinkToStorage()
1560 	{
1561 		if ( m_xStorage != null && m_bLinkObj )
1562 		{
1563 			try {
1564 				XStream xLinkStream = m_xStorage.openStreamElement( "LinkName", ElementModes.ELEMENT_WRITE );
1565 
1566 				if ( xLinkStream != null )
1567 				{
1568 					XOutputStream xLinkOutStream = xLinkStream.getOutputStream();
1569 					XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface( XTruncate.class,
1570 																			 	xLinkOutStream );
1571 					if ( xLinkOutStream != null && xTruncate != null )
1572 					{
1573 						xTruncate.truncate();
1574 
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];
1579 
1580 						xLinkOutStream.writeBytes( aLinkBytes );
1581 						xLinkOutStream.closeOutput();
1582 
1583 						XComponent xComponent = (XComponent) UnoRuntime.queryInterface( XComponent.class,
1584 																						xLinkStream );
1585 						if ( xComponent != null )
1586 							xComponent.dispose();
1587 					}
1588 					else
1589 						JOptionPane.showMessageDialog( m_aFrame,
1590 														"The substream can not be truncated or written!",
1591 														"Error:",
1592 														JOptionPane.ERROR_MESSAGE );
1593 
1594 				}
1595 				else
1596 					JOptionPane.showMessageDialog( m_aFrame,
1597 													"Can't create/open substream!",
1598 													"Error:",
1599 													JOptionPane.ERROR_MESSAGE );
1600 			}
1601 			catch( Exception e )
1602 			{
1603 				JOptionPane.showMessageDialog( m_aFrame,
1604 											e,
1605 											"Exception in storeLinkToStorage:",
1606 											JOptionPane.ERROR_MESSAGE );
1607 
1608 			}
1609 		}
1610 	}
1611 
1612 	public void storeLinkAsFileURI( String aFileURI )
1613 	{
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 )
1620 			{
1621 				Object aArgs[] = new Object[2];
1622 				aArgs[0] = aFileURI;
1623 				aArgs[1] = new Integer( ElementModes.ELEMENT_READWRITE );
1624 
1625 				Object oStorage = xStorageFactory.createInstanceWithArguments( aArgs );
1626 				XStorage xTargetStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
1627 
1628 				XComponent xComponent = (XComponent)UnoRuntime.queryInterface( XComponent.class, m_xStorage );
1629 				xComponent.dispose();
1630 
1631 				m_xStorage = xTargetStorage;
1632 				m_bOwnFile = true;
1633 
1634 				storeLinkToStorage();
1635 
1636 				XTransactedObject xTransact = (XTransactedObject)UnoRuntime.queryInterface( XTransactedObject.class,
1637 																							m_xStorage );
1638 				if ( xTransact != null )
1639 					xTransact.commit();
1640 			}
1641 			else
1642 				JOptionPane.showMessageDialog( m_aFrame,
1643 												"Can't create StorageFactory!",
1644 												"Error:",
1645 												JOptionPane.ERROR_MESSAGE );
1646 		}
1647 		catch( Exception e )
1648 		{
1649 			JOptionPane.showMessageDialog( m_aFrame, e, "Exception in saveStorageToFileURI():", JOptionPane.ERROR_MESSAGE );
1650 		}
1651 	}
1652 
1653 	public String getValidURL( String sFileURL )
1654 	{
1655 		// m_xTransformer must be set!
1656 		URL[] aURLs = { new URL() };
1657 		aURLs[0].Complete = sFileURL;
1658 
1659 		try {
1660 			if ( !m_xTransformer.parseSmart( aURLs, "" ) )
1661 				throw new Exception();
1662 		}
1663 		catch( Exception e )
1664 		{
1665 			JOptionPane.showMessageDialog( m_aFrame, e, "Exception in getValidURL():", JOptionPane.ERROR_MESSAGE );
1666 		}
1667 
1668 		return aURLs[0].Complete;
1669 	}
1670 
1671 	public void disposeObject()
1672 	{
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
1677 
1678 		clearObjectAndStorage();
1679 
1680 		if ( m_aBitmapPainter != null )
1681 		{
1682 			m_aBitmapPainter.disconnectListener();
1683 			m_aBitmapPainter = null;
1684 		}
1685 	}
1686 }
1687 
1688