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