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 import java.awt.*;
23 import java.applet.*;
24 import java.awt.event.*;
25 import java.net.*;
26 import java.io.*;
27 
28 import javax.swing.JOptionPane;
29 
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.lang.XSingleServiceFactory;
32 
33 import com.sun.star.uno.UnoRuntime;
34 import com.sun.star.uno.XInterface;
35 import com.sun.star.uno.AnyConverter;
36 import com.sun.star.uno.Type;
37 
38 import com.sun.star.lang.XComponent;
39 
40 import com.sun.star.beans.PropertyValue;
41 
42 import com.sun.star.datatransfer.DataFlavor;
43 import com.sun.star.datatransfer.XTransferable;
44 
45 import com.sun.star.container.XNameAccess;
46 
47 import com.sun.star.io.XStream;
48 import com.sun.star.io.XInputStream;
49 import com.sun.star.io.XOutputStream;
50 import com.sun.star.io.XTruncate;
51 
52 import com.sun.star.embed.*;
53 
54 public class EmbedContApp extends Applet implements MouseListener, XEmbeddedClient
55 {
56 	private XMultiServiceFactory m_xServiceFactory;
57 
58 	private XEmbeddedObject m_xEmbedObj;
59 	private XStorage m_xStorage;
60 
61 	private Frame m_aFrame;
62 	private Menu m_aFileMenu;
63 	private Menu m_aObjectMenu;
64 	private Toolkit m_aToolkit;
65 	private Image m_aImage;
66 
67 	private boolean m_bOwnFile = false;
68 
69 	private boolean m_bLinkObj = false;
70 	private String m_aLinkURI;
71 
EmbedContApp( Frame aFrame, XMultiServiceFactory xServiceFactory )72 	public EmbedContApp( Frame aFrame, XMultiServiceFactory xServiceFactory )
73 	{
74 		m_aFrame = aFrame;
75 		m_xServiceFactory = xServiceFactory;
76 	}
77 
init()78 	public void init()
79 	{
80 		resize( 640, 480 );
81 		setBackground( Color.gray );
82 
83 		m_aToolkit = Toolkit.getDefaultToolkit();
84 
85 		// Get a menu bar.
86 		MenuBar aMenuBar = m_aFrame.getMenuBar();
87 		if( aMenuBar == null )
88 		{
89 			aMenuBar = new MenuBar();
90 			m_aFrame.setMenuBar( aMenuBar );
91 		}
92 
93 		// Create menus for the menu bar.
94 
95 		// File menu
96 		m_aFileMenu = new Menu( "File", true );
97 		aMenuBar.add( m_aFileMenu );
98 
99 		MenuItem aItem = new NewMenuItem();
100 		m_aFileMenu.add( aItem );
101 
102 		aItem = new OpenFileMenuItem();
103 		m_aFileMenu.add( aItem );
104 
105 		aItem = new SaveMenuItem();
106 		m_aFileMenu.add( aItem );
107 
108 		aItem = new SaveAsMenuItem();
109 		m_aFileMenu.add( aItem );
110 
111 		// Object menu
112 		m_aObjectMenu = new Menu( "Object", true );
113 		aMenuBar.add( m_aObjectMenu );
114 
115 		aItem = new NewObjectMenuItem();
116 		m_aObjectMenu.add( aItem );
117 
118 		aItem = new LoadObjectMenuItem();
119 		m_aObjectMenu.add( aItem );
120 
121 		aItem = new LinkObjectMenuItem();
122 		m_aObjectMenu.add( aItem );
123 
124 		aItem = new ConvertLinkToEmbedMenuItem();
125 		m_aObjectMenu.add( aItem );
126 
127 		// Handle mouse clicks in our window.
128 //		addMouseListener( new MouseWatcher() );
129 		addMouseListener( this );
130 	}
131 
update( Graphics g )132 	public void update( Graphics g )
133 	{
134 		paint( g );
135 	}
136 
paint( Graphics g )137 	public void paint( Graphics g )
138 	{
139 		super.paint( g );
140 
141 		if ( m_xEmbedObj != null )
142 		{
143 			synchronized( this )
144 			{
145 				if ( m_aImage != null )
146 					g.drawImage( m_aImage, 0, 0, EmbedContApp.this );
147 			}
148 		}
149 	}
150 
generateNewImage()151 	public void generateNewImage()
152 	{
153 		if ( m_xEmbedObj != null )
154 		{
155 			try {
156 				int nOldState = m_xEmbedObj.getCurrentState();
157 				int nState = nOldState;
158 				if ( nOldState == EmbedStates.EMBED_LOADED )
159 				{
160 					m_xEmbedObj.changeState( EmbedStates.EMBED_RUNNING );
161 					nState = EmbedStates.EMBED_RUNNING;
162 				}
163 
164 				if ( nState == EmbedStates.EMBED_ACTIVE || nState == EmbedStates.EMBED_RUNNING )
165 				{
166 					XComponentSupplier xCompProv = (XComponentSupplier)UnoRuntime.queryInterface(
167 																					XComponentSupplier.class,
168 																					m_xEmbedObj );
169 					if ( xCompProv != null )
170 					{
171 						XComponent xComp = xCompProv.getComponent();
172 						XTransferable xTransfer = (XTransferable)UnoRuntime.queryInterface(
173 																					XTransferable.class,
174 																					xComp );
175 						if ( xTransfer != null )
176 						{
177 							DataFlavor aFlavor = new DataFlavor();
178 							aFlavor.MimeType = "image/png";
179 							aFlavor.HumanPresentableName = "Portable Network Graphics";
180 							aFlavor.DataType = new Type( byte[].class );
181 
182 							byte[] aPNGData = (byte[])AnyConverter.toArray( xTransfer.getTransferData( aFlavor ) );
183 							if ( aPNGData != null && aPNGData.length != 0 )
184 							{
185 								synchronized( this )
186 								{
187 									m_aImage = m_aToolkit.createImage( aPNGData );
188 								}
189 							}
190 						}
191 						else
192 							System.out.println( "paint() : can not get XTransferable for the component!\n" );
193 					}
194 					else
195 						System.out.println( "paint() : XComponentSupplier is not implemented!\n" );
196 				}
197 			}
198 			catch( com.sun.star.uno.Exception e )
199 			{
200 				// dialogs should not be used in paint()
201 				System.out.println( "Exception in paint(): " + e );
202 			}
203 		}
204 	}
205 
mouseClicked( MouseEvent e )206 	public void mouseClicked( MouseEvent e )
207 	{
208 		if( e.getModifiers() == InputEvent.BUTTON1_MASK )
209 		{
210 			// activate object if exists and not active
211 			if ( m_xEmbedObj != null )
212 			{
213 				try {
214 					m_xEmbedObj.changeState( EmbedStates.EMBED_ACTIVE );
215 				}
216 				catch( Exception ex )
217 				{
218 					JOptionPane.showMessageDialog( m_aFrame, ex, "Exception on mouse click", JOptionPane.ERROR_MESSAGE );
219 				}
220 			}
221 		}
222 	}
223 
mousePressed( MouseEvent e )224 	public void mousePressed( MouseEvent e ){};
mouseEntered( MouseEvent e )225 	public void mouseEntered( MouseEvent e ){};
mouseExited( MouseEvent e )226 	public void mouseExited( MouseEvent e ){};
mouseReleased( MouseEvent e )227 	public void mouseReleased( MouseEvent e ){};
228 
229 	// XEmbeddedClient
saveObject()230 	public void saveObject()
231 		throws com.sun.star.uno.Exception
232 	{
233 		if ( m_xEmbedObj != null )
234 		{
235 			try {
236 				XEmbedPersist xPersist = (XEmbedPersist)UnoRuntime.queryInterface( XEmbedPersist.class, m_xEmbedObj );
237 				if ( xPersist != null )
238 				{
239 					xPersist.storeOwn();
240 					generateNewImage();
241 				}
242 				else
243 					JOptionPane.showMessageDialog( m_aFrame, "No XEmbedPersist!", "Error:", JOptionPane.ERROR_MESSAGE );
244 			}
245 			catch( Exception e )
246 			{
247 				JOptionPane.showMessageDialog( m_aFrame, e, "Exception in saveObject:", JOptionPane.ERROR_MESSAGE );
248 			}
249 		}
250 
251 		generateNewImage();
252 		repaint();
253 	}
254 
onShowWindow( boolean bVisible )255 	public void onShowWindow( boolean bVisible )
256 	{
257 		// for now nothing to do
258 	}
259 
260 	// classes
261 	class NewMenuItem extends MenuItem implements ActionListener // Menu New
262 	{
NewMenuItem()263 		public NewMenuItem()
264 		{
265 			super( "New", new MenuShortcut( KeyEvent.VK_A ));
266 			addActionListener( this );
267 		}
268 
actionPerformed( ActionEvent e )269 		public void actionPerformed( ActionEvent e )
270 		{
271 			// clear everything
272 			clearObjectAndStorage();
273 
274 			repaint();
275 		}
276 	}
277 
278 	class SaveAsMenuItem extends MenuItem implements ActionListener // Menu SaveAs...
279 	{
SaveAsMenuItem()280 		public SaveAsMenuItem()
281 		{
282 			super( "SaveAs..." );
283 			addActionListener( this );
284 		}
285 
actionPerformed( ActionEvent e )286 		public void actionPerformed( ActionEvent e )
287 		{
288 			// open SaveAs dialog and store
289 
290 			if ( m_xStorage != null && m_xEmbedObj != null )
291 			{
292 				FileDialog aFileDialog = new FileDialog( m_aFrame, "SaveAs", FileDialog.SAVE );
293 				aFileDialog.show();
294 				if ( aFileDialog.getFile() != null )
295 				{
296 					String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
297 					File aFile = new File( aFileName );
298 					if ( aFile != null )
299 					{
300 						// create object from specified file
301 						String aFileURI = aFile.toURI().toASCIIString();
302 						try {
303 							saveObject();
304 
305 							if ( m_bLinkObj )
306 								storeLinkToStorage();
307 
308 							saveStorageAsFileURI( aFileURI );
309 						}
310 						catch( Exception ex )
311 						{
312 							JOptionPane.showMessageDialog( m_aFrame,
313 															ex,
314 															"Exception in SaveAsMenuItem:",
315 															JOptionPane.ERROR_MESSAGE );
316 						}
317 					}
318 				}
319 			}
320 			else
321 				JOptionPane.showMessageDialog( m_aFrame, "No document is embedded!", "Error:", JOptionPane.ERROR_MESSAGE );
322 		}
323 	}
324 
325 	class OpenFileMenuItem extends MenuItem implements ActionListener // Menu Open
326 	{
OpenFileMenuItem()327 		public OpenFileMenuItem()
328 		{
329 			super( "Open", new MenuShortcut( KeyEvent.VK_C ));
330 			addActionListener( this );
331 		}
332 
actionPerformed( ActionEvent e )333 		public void actionPerformed( ActionEvent e )
334 		{
335 			// clear everything
336 			clearObjectAndStorage();
337 
338 			// open OpenFile dialog and load doc
339 			FileDialog aFileDialog = new FileDialog( m_aFrame, "Open" );
340 			aFileDialog.show();
341 			if ( aFileDialog.getFile() != null )
342 			{
343 				String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
344 				File aFile = new File( aFileName );
345 				if ( aFile != null )
346 				{
347 					// create object from specified file
348 					String aFileURI = aFile.toURI().toASCIIString();
349 
350 					// load from specified file
351 					loadFileURI( aFileURI );
352 
353 					if ( m_xEmbedObj != null )
354 					{
355 						try {
356 							m_xEmbedObj.setClientSite( EmbedContApp.this );
357 						}
358 						catch( Exception ex )
359 						{
360 							JOptionPane.showMessageDialog( m_aFrame,
361 															ex,
362 															"Exception in OpenFileMenuItem:",
363 															JOptionPane.ERROR_MESSAGE );
364 						}
365 					}
366 				}
367 			}
368 
369 			generateNewImage();
370 			repaint();
371 		}
372 	}
373 
374 	class SaveMenuItem extends MenuItem implements ActionListener // Menu Save
375 	{
SaveMenuItem()376 		public SaveMenuItem()
377 		{
378 			super( "Save", new MenuShortcut( KeyEvent.VK_D ));
379 			addActionListener( this );
380 		}
381 
actionPerformed( ActionEvent e )382 		public void actionPerformed( ActionEvent e )
383 		{
384 			// if has persistence store there
385 			// if not open SaveAs dialog and store
386 			if ( m_xStorage != null && m_xEmbedObj != null )
387 			{
388 				if ( m_bOwnFile )
389 				{
390 					if ( m_xStorage == null )
391 					{
392 						JOptionPane.showMessageDialog( m_aFrame,
393 														"No storage for oned file!",
394 														"Error:",
395 														JOptionPane.ERROR_MESSAGE );
396 						return;
397 					}
398 
399 					try {
400 						saveObject();
401 
402 						if ( m_bLinkObj )
403 							storeLinkToStorage();
404 
405 						XTransactedObject xTransact = (XTransactedObject)UnoRuntime.queryInterface( XTransactedObject.class,
406 																									m_xStorage );
407 						if ( xTransact != null )
408 							xTransact.commit();
409 					}
410 					catch( Exception ex )
411 					{
412 						JOptionPane.showMessageDialog( m_aFrame,
413 														ex,
414 														"Exception during save operation in SaveMenuItem:",
415 														JOptionPane.ERROR_MESSAGE );
416 					}
417 				}
418 				else
419 				{
420 					FileDialog aFileDialog = new FileDialog( m_aFrame, "SaveAs", FileDialog.SAVE );
421 					aFileDialog.show();
422 					if ( aFileDialog.getFile() != null )
423 					{
424 						String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
425 						File aFile = new File( aFileName );
426 						if ( aFile != null )
427 						{
428 							// create object from specified file
429 							String aFileURI = aFile.toURI().toASCIIString();
430 							try {
431 								saveObject();
432 
433 								if ( m_bLinkObj )
434 									storeLinkToStorage();
435 
436 								saveStorageAsFileURI( aFileURI );
437 							}
438 							catch( Exception ex )
439 							{
440 								JOptionPane.showMessageDialog( m_aFrame,
441 																ex,
442 																"Exception during 'save as' operation in SaveMenuItem:",
443 																JOptionPane.ERROR_MESSAGE );
444 							}
445 						}
446 					}
447 				}
448 			}
449 			else
450 				JOptionPane.showMessageDialog( m_aFrame, "No document is embedded!", "Error:", JOptionPane.ERROR_MESSAGE );
451 		}
452 	}
453 
454 	class NewObjectMenuItem extends MenuItem implements ActionListener // Menu NewObject
455 	{
NewObjectMenuItem()456 		public NewObjectMenuItem()
457 		{
458 			super( "Create", new MenuShortcut( KeyEvent.VK_N ));
459 			addActionListener( this );
460 		}
461 
actionPerformed( ActionEvent e )462 		public void actionPerformed( ActionEvent e )
463 		{
464 			// remove current object an init a new one
465 			clearObjectAndStorage();
466 
467 			Object[] possibleValues = { "com.sun.star.comp.Writer.TextDocument",
468 										"com.sun.star.comp.Writer.GlobalDocument",
469 										"com.sun.star.comp.Writer.WebDocument",
470 										"com.sun.star.comp.Calc.SpreadsheetDocument",
471 										"com.sun.star.comp.Draw.PresentationDocument",
472 										"com.sun.star.comp.Draw.DrawingDocument",
473 										"com.sun.star.comp.Math.FormulaDocument" };
474 
475 			String selectedValue = (String)JOptionPane.showInputDialog( null, "DocumentType", "Select",
476             															JOptionPane.INFORMATION_MESSAGE, null,
477             															possibleValues, possibleValues[0] );
478 
479 			if ( selectedValue != null )
480 			{
481 				m_xStorage = createTempStorage();
482 
483 				if ( m_xStorage != null )
484 					m_xEmbedObj = createEmbedObject( selectedValue );
485 				else
486 					JOptionPane.showMessageDialog( m_aFrame,
487 													"Can't create temporary storage!",
488 													"Error:",
489 													JOptionPane.ERROR_MESSAGE );
490 
491 
492 				if ( m_xEmbedObj != null )
493 				{
494 					try {
495 						m_xEmbedObj.setClientSite( EmbedContApp.this );
496 					}
497 					catch( Exception ex )
498 					{
499 						JOptionPane.showMessageDialog( m_aFrame,
500 														ex,
501 														"Exception in NewObjectMenuItem:",
502 														JOptionPane.ERROR_MESSAGE );
503 					}
504 				}
505 			}
506 
507 			generateNewImage();
508 			repaint();
509 		}
510 	}
511 
512 	class LoadObjectMenuItem extends MenuItem implements ActionListener // Menu LoadObject
513 	{
LoadObjectMenuItem()514 		public LoadObjectMenuItem()
515 		{
516 			super( "Load from file", new MenuShortcut( KeyEvent.VK_L ));
517 			addActionListener( this );
518 		}
519 
actionPerformed( ActionEvent e )520 		public void actionPerformed( ActionEvent e )
521 		{
522 			// first remove current object
523 			clearObjectAndStorage();
524 
525 			// open OpenFile dialog and load doc
526 			FileDialog aFileDialog = new FileDialog( m_aFrame, "Select sources to use for object init" );
527 			aFileDialog.show();
528 			if ( aFileDialog.getFile() != null )
529 			{
530 				String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
531 				File aFile = new File( aFileName );
532 				if ( aFile != null )
533 				{
534 					// create object from specified file
535 					String aFileURI = aFile.toURI().toASCIIString();
536 					m_xStorage = createTempStorage();
537 
538 					if ( m_xStorage != null )
539 						m_xEmbedObj = loadEmbedObject( aFileURI );
540 
541 					if ( m_xEmbedObj != null )
542 					{
543 						try {
544 							m_xEmbedObj.setClientSite( EmbedContApp.this );
545 						}
546 						catch( Exception ex )
547 						{
548 							JOptionPane.showMessageDialog( m_aFrame,
549 															ex,
550 															"Exception in LoadObjectMenuItem:",
551 															JOptionPane.ERROR_MESSAGE );
552 						}
553 					}
554 				}
555 			}
556 
557 			generateNewImage();
558 			repaint();
559 		}
560 	}
561 
562 	class LinkObjectMenuItem extends MenuItem implements ActionListener // Menu LinkObject
563 	{
LinkObjectMenuItem()564 		public LinkObjectMenuItem()
565 		{
566 			super( "Create link", new MenuShortcut( KeyEvent.VK_M ));
567 			addActionListener( this );
568 		}
569 
actionPerformed( ActionEvent e )570 		public void actionPerformed( ActionEvent e )
571 		{
572 			// first remove current object
573 			clearObjectAndStorage();
574 
575 			// open OpenFile dialog and load doc
576 			FileDialog aFileDialog = new FileDialog( m_aFrame, "Select sources to use for object init" );
577 			aFileDialog.show();
578 			if ( aFileDialog.getFile() != null )
579 			{
580 				m_xStorage = createTempStorage();
581 
582 				String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
583 				File aFile = new File( aFileName );
584 				if ( aFile != null )
585 				{
586 					// create object from specified file
587 					String aFileURI = aFile.toURI().toASCIIString();
588 
589 					m_xEmbedObj = createLinkObject( aFileURI );
590 
591 					if ( m_xEmbedObj != null )
592 					{
593 						m_aLinkURI = aFileURI;
594 						m_bLinkObj = true;
595 
596 						try {
597 							m_xEmbedObj.setClientSite( EmbedContApp.this );
598 						}
599 						catch( Exception ex )
600 						{
601 							JOptionPane.showMessageDialog( m_aFrame,
602 															ex,
603 															"Exception in LinkObjectMenuItem:",
604 															JOptionPane.ERROR_MESSAGE );
605 						}
606 					}
607 				}
608 			}
609 
610 			generateNewImage();
611 			repaint();
612 		}
613 	}
614 
615 	class ConvertLinkToEmbedMenuItem extends MenuItem implements ActionListener // Menu LinkObject
616 	{
ConvertLinkToEmbedMenuItem()617 		public ConvertLinkToEmbedMenuItem()
618 		{
619 			super( "Convert link to embed", new MenuShortcut( KeyEvent.VK_M ));
620 			addActionListener( this );
621 		}
622 
actionPerformed( ActionEvent e )623 		public void actionPerformed( ActionEvent e )
624 		{
625 			if ( !m_bLinkObj )
626 			{
627 				JOptionPane.showMessageDialog( m_aFrame, "The object is not a link!", "Error:", JOptionPane.ERROR_MESSAGE );
628 				return;
629 			}
630 
631 			if ( m_xEmbedObj != null )
632 			{
633 				if ( m_xStorage != null )
634 				{
635 					try {
636 						XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface( XNameAccess.class,
637 																						m_xStorage );
638 						if ( xNameAccess != null && xNameAccess.hasByName( "LinkName" ) )
639 							m_xStorage.removeElement( "LinkName" );
640 
641 						XEmbedPersist xPersist = (XEmbedPersist)UnoRuntime.queryInterface( XEmbedPersist.class,
642 																						m_xEmbedObj );
643 						if ( xPersist != null )
644 						{
645 							PropertyValue[] pEmp = new PropertyValue[0];
646 							xPersist.setPersistentEntry( m_xStorage, "EmbedSub", EntryInitModes.ENTRY_NO_INIT, pEmp );
647 							m_bLinkObj = false;
648 							m_aLinkURI = null;
649 						}
650 						else
651 							JOptionPane.showMessageDialog( m_aFrame,
652 															"No XEmbedPersist in ConvertLink... !",
653 															"Error:",
654 															JOptionPane.ERROR_MESSAGE );
655 					}
656 					catch( Exception e1 )
657 					{
658 						JOptionPane.showMessageDialog( m_aFrame,
659 														e1,
660 														"Exception in ConvertLinkToEmbed:try 1 :",
661 														JOptionPane.ERROR_MESSAGE );
662 					}
663 				}
664 			}
665 		}
666 	}
667 
668 	// Helper methods
createEmbedObject( String aServiceName )669 	public XEmbeddedObject createEmbedObject( String aServiceName )
670 	{
671 		XEmbeddedObject xEmbObj = null;
672 		byte[] pClassID = new byte[16];
673 
674 		if ( aServiceName.equals( "com.sun.star.comp.Writer.TextDocument" ) )
675 		{
676 			int[] pTempClassID = { 0x8B, 0xC6, 0xB1, 0x65, 0xB1, 0xB2, 0x4E, 0xDD,
677 									0xAA, 0x47, 0xDA, 0xE2, 0xEE, 0x68, 0x9D, 0xD6 };
678 			for ( int ind = 0; ind < 16; ind++ )
679 				pClassID[ind] = (byte)pTempClassID[ind];
680 		}
681 		else if ( aServiceName.equals( "com.sun.star.comp.Writer.GlobalDocument" ) )
682 		{
683 			int[] pTempClassID = { 0xB2, 0x1A, 0x0A, 0x7C, 0xE4, 0x03, 0x41, 0xFE,
684 									0x95, 0x62, 0xBD, 0x13, 0xEA, 0x6F, 0x15, 0xA0 };
685 			for ( int ind = 0; ind < 16; ind++ )
686 				pClassID[ind] = (byte)pTempClassID[ind];
687 		}
688 		else if ( aServiceName.equals( "com.sun.star.comp.Writer.WebDocument" ) )
689 		{
690 			int[] pTempClassID = { 0xA8, 0xBB, 0xA6, 0x0C, 0x7C, 0x60, 0x45, 0x50,
691 									0x91, 0xCE, 0x39, 0xC3, 0x90, 0x3F, 0xAC, 0x5E };
692 			for ( int ind = 0; ind < 16; ind++ )
693 				pClassID[ind] = (byte)pTempClassID[ind];
694 		}
695 		else if ( aServiceName.equals( "com.sun.star.comp.Calc.SpreadsheetDocument" ) )
696 		{
697 			int[] pTempClassID = { 0x47, 0xBB, 0xB4, 0xCB, 0xCE, 0x4C, 0x4E, 0x80,
698 									0xA5, 0x91, 0x42, 0xD9, 0xAE, 0x74, 0x95, 0x0F };
699 			for ( int ind = 0; ind < 16; ind++ )
700 				pClassID[ind] = (byte)pTempClassID[ind];
701 		}
702 		else if ( aServiceName.equals( "com.sun.star.comp.Draw.PresentationDocument" ) )
703 		{
704 			int[] pTempClassID = { 0x91, 0x76, 0xE4, 0x8A, 0x63, 0x7A, 0x4D, 0x1F,
705 									0x80, 0x3B, 0x99, 0xD9, 0xBF, 0xAC, 0x10, 0x47 };
706 			for ( int ind = 0; ind < 16; ind++ )
707 				pClassID[ind] = (byte)pTempClassID[ind];
708 		}
709 		else if ( aServiceName.equals( "com.sun.star.comp.Draw.DrawingDocument" ) )
710 		{
711 			int[] pTempClassID = { 0x4B, 0xAB, 0x89, 0x70, 0x8A, 0x3B, 0x45, 0xB3,
712 									0x99, 0x1C, 0xCB, 0xEE, 0xAC, 0x6B, 0xD5, 0xE3 };
713 			for ( int ind = 0; ind < 16; ind++ )
714 				pClassID[ind] = (byte)pTempClassID[ind];
715 		}
716 		else if ( aServiceName.equals( "com.sun.star.comp.Math.FormulaDocument" ) )
717 		{
718 			int[] pTempClassID = { 0x07, 0x8B, 0x7A, 0xBA, 0x54, 0xFC, 0x45, 0x7F,
719 									0x85, 0x51, 0x61, 0x47, 0xE7, 0x76, 0xA9, 0x97 };
720 			for ( int ind = 0; ind < 16; ind++ )
721 				pClassID[ind] = (byte)pTempClassID[ind];
722 		}
723 
724 		if ( pClassID != null )
725 		{
726 			// create embedded object based on the class ID
727 			try {
728 				Object oEmbedFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectFactory" );
729 				XEmbedObjectFactory xEmbedFactory = (XEmbedObjectFactory)UnoRuntime.queryInterface(
730 																						XEmbedObjectFactory.class,
731 																						oEmbedFactory );
732 				if ( xEmbedFactory != null )
733 				{
734 					Object oEmbObj = xEmbedFactory.createInstanceInitNew( pClassID,
735 																		"Dummy name",
736 																		m_xStorage,
737 																		"EmbedSub" );
738 					xEmbObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
739 				}
740 				else
741 					JOptionPane.showMessageDialog( m_aFrame,
742 												   "Can't create EmbedFactory!",
743 												   "Error:",
744 												   JOptionPane.ERROR_MESSAGE );
745 			}
746 			catch( Exception e )
747 			{
748 				JOptionPane.showMessageDialog( m_aFrame, e, "Exception in createInstanceInitNew():", JOptionPane.ERROR_MESSAGE );
749 			}
750 		}
751 		else
752 			JOptionPane.showMessageDialog( m_aFrame, "Can't retrieve class ID!", "Error:", JOptionPane.ERROR_MESSAGE );
753 
754 		return xEmbObj;
755 	}
756 
createLinkObject( String aLinkURL )757 	public XEmbeddedObject createLinkObject( String aLinkURL )
758 	{
759 		XEmbeddedObject xEmbObj = null;
760 
761 		try {
762 			Object oEmbedFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectFactory" );
763 			XEmbedObjectFactory xEmbedFactory = (XEmbedObjectFactory)UnoRuntime.queryInterface(
764 																					XEmbedObjectFactory.class,
765 																					oEmbedFactory );
766 			if ( xEmbedFactory != null )
767 			{
768 				Object oEmbObj = xEmbedFactory.createInstanceLink( aLinkURL );
769 				xEmbObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
770 			}
771 			else
772 				JOptionPane.showMessageDialog( m_aFrame,
773 											   "Can't create EmbedFactory!",
774 											   "Error:",
775 											   JOptionPane.ERROR_MESSAGE );
776 		}
777 		catch( Exception e )
778 		{
779 			JOptionPane.showMessageDialog( m_aFrame, e, "Exception in createLinkObject():", JOptionPane.ERROR_MESSAGE );
780 		}
781 
782 
783 		return xEmbObj;
784 	}
785 
786 
loadEmbedObject( String aFileURI )787 	public XEmbeddedObject loadEmbedObject( String aFileURI )
788 	{
789 		XEmbeddedObject xEmbObj = null;
790 		try {
791 			Object oEmbedFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectFactory" );
792 			XEmbedObjectFactory xEmbedFactory = (XEmbedObjectFactory)UnoRuntime.queryInterface(
793 																					XEmbedObjectFactory.class,
794 																					oEmbedFactory );
795 			if ( xEmbedFactory != null )
796 			{
797 				PropertyValue[] aMedDescr = { new PropertyValue(), new PropertyValue() };
798 				aMedDescr[0].Name = "URL";
799 				aMedDescr[0].Value = (Object) aFileURI;
800 				aMedDescr[1].Name = "ReadOnly";
801 				aMedDescr[1].Value = (Object) new Boolean( false );
802 				Object oEmbObj = xEmbedFactory.createInstanceInitFromMediaDescriptor( m_xStorage,
803 																					"EmbedSub",
804 																					aMedDescr );
805 				xEmbObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
806 			}
807 			else
808 				JOptionPane.showMessageDialog( m_aFrame,
809 											   "Can't create EmbedFactory!",
810 											   "Error:",
811 											   JOptionPane.ERROR_MESSAGE );
812 		}
813 		catch( Exception e )
814 		{
815 			JOptionPane.showMessageDialog( m_aFrame, e, "Exception in loadEmbedObject():", JOptionPane.ERROR_MESSAGE );
816 		}
817 
818 		return xEmbObj;
819 	}
820 
clearObjectAndStorage()821 	public void clearObjectAndStorage()
822 	{
823 		synchronized( this )
824 		{
825 			m_aImage = null;
826 		}
827 
828 		m_bOwnFile = false;
829 
830 		m_aLinkURI = null;
831 		m_bLinkObj = false;
832 
833 		if ( m_xEmbedObj != null )
834 		{
835 			try {
836 				XComponent xComponent = (XComponent)UnoRuntime.queryInterface( XComponent.class, m_xEmbedObj );
837 				if ( xComponent != null )
838 					xComponent.dispose();
839 			}
840 			catch ( Exception ex )
841 			{}
842 			m_xEmbedObj = null;
843 		}
844 
845 		if ( m_xStorage != null )
846 		{
847 			try {
848 				XComponent xComponent = (XComponent)UnoRuntime.queryInterface( XComponent.class, m_xStorage );
849 				if ( xComponent != null )
850 					xComponent.dispose();
851 			}
852 			catch ( Exception ex )
853 			{}
854 			m_xStorage = null;
855 		}
856 	}
857 
createTempStorage()858 	public XStorage createTempStorage()
859 	{
860 		XStorage xTempStorage = null;
861 
862 		try {
863 			Object oStorageFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.StorageFactory" );
864 			XSingleServiceFactory xStorageFactory = (XSingleServiceFactory)UnoRuntime.queryInterface(
865 																						XSingleServiceFactory.class,
866 																						oStorageFactory );
867 			if ( xStorageFactory != null )
868 			{
869 				Object oStorage = xStorageFactory.createInstance();
870 				xTempStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
871 			}
872 			else
873 				JOptionPane.showMessageDialog( m_aFrame,
874 												"Can't create StorageFactory!",
875 												"Error:",
876 												JOptionPane.ERROR_MESSAGE );
877 		}
878 		catch( Exception e )
879 		{
880 			JOptionPane.showMessageDialog( m_aFrame, e, "Exception in createTempStorage():", JOptionPane.ERROR_MESSAGE );
881 		}
882 
883 		return xTempStorage;
884 	}
885 
saveStorageAsFileURI( String aFileURI )886 	public void saveStorageAsFileURI( String aFileURI )
887 	{
888 		try {
889 			Object oStorageFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.StorageFactory" );
890 			XSingleServiceFactory xStorageFactory = (XSingleServiceFactory)UnoRuntime.queryInterface(
891 																						XSingleServiceFactory.class,
892 																						oStorageFactory );
893 			if ( xStorageFactory != null )
894 			{
895 				Object aArgs[] = new Object[2];
896 				aArgs[0] = aFileURI;
897 				aArgs[1] = new Integer( ElementModes.ELEMENT_READWRITE );
898 
899 				Object oStorage = xStorageFactory.createInstanceWithArguments( aArgs );
900 				XStorage xTargetStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
901 				m_xStorage.copyToStorage( xTargetStorage );
902 
903 				XComponent xComponent = (XComponent)UnoRuntime.queryInterface( XComponent.class, m_xStorage );
904 				xComponent.dispose();
905 
906 				m_xStorage = xTargetStorage;
907 				m_bOwnFile = true;
908 			}
909 			else
910 				JOptionPane.showMessageDialog( m_aFrame,
911 												"Can't create StorageFactory!",
912 												"Error:",
913 												JOptionPane.ERROR_MESSAGE );
914 		}
915 		catch( Exception e )
916 		{
917 			JOptionPane.showMessageDialog( m_aFrame, e, "Exception in saveStorageToFileURI():", JOptionPane.ERROR_MESSAGE );
918 		}
919 
920 	}
921 
loadFileURI( String aFileURI )922 	public void loadFileURI( String aFileURI )
923 	{
924 		try
925 		{
926 			Object oStorageFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.StorageFactory" );
927 			XSingleServiceFactory xStorageFactory = (XSingleServiceFactory)UnoRuntime.queryInterface(
928 																						XSingleServiceFactory.class,
929 																						oStorageFactory );
930 			Object aArgs[] = new Object[2];
931 			aArgs[0] = aFileURI;
932 			aArgs[1] = new Integer( ElementModes.ELEMENT_READWRITE );
933 
934 			Object oStorage = xStorageFactory.createInstanceWithArguments( aArgs );
935 			XStorage xTargetStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
936 
937 			Object oEmbedFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectFactory" );
938 			XEmbedObjectFactory xEmbedFactory = (XEmbedObjectFactory)UnoRuntime.queryInterface(
939 																					XEmbedObjectFactory.class,
940 																					oEmbedFactory );
941 
942 			XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface( XNameAccess.class,
943 																			xTargetStorage );
944 			if ( xNameAccess == null )
945 			{
946 				JOptionPane.showMessageDialog( m_aFrame, "No XNameAccess!", "Error:", JOptionPane.ERROR_MESSAGE );
947 				return;
948 			}
949 
950 			Object oEmbObj = null;
951 			if ( xNameAccess.hasByName( "LinkName" ) && xTargetStorage.isStreamElement( "LinkName" ) )
952 			{
953 				XStream xLinkStream = xTargetStorage.openStreamElement( "LinkName", ElementModes.ELEMENT_READ );
954 				if ( xLinkStream != null )
955 				{
956 					XInputStream xInStream = xLinkStream.getInputStream();
957 					if ( xInStream != null )
958 					{
959 						byte[][] pBuff = new byte[1][0];
960 						int nRead = xInStream.readBytes( pBuff, 1000 );
961 						m_aLinkURI = new String( pBuff[0] );
962 						xInStream.closeInput();
963 						oEmbObj = xEmbedFactory.createInstanceLink( m_aLinkURI );
964 						m_bLinkObj = true;
965 					}
966 				}
967 			}
968 			else
969 				oEmbObj = xEmbedFactory.createInstanceInitFromEntry( xTargetStorage,
970 																	"EmbedSub",
971 																	false );
972 
973 			m_xEmbedObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
974 
975 			if ( m_xEmbedObj != null )
976 			{
977 				m_xStorage = xTargetStorage;
978 				m_bOwnFile = true;
979 			}
980 			else
981 				JOptionPane.showMessageDialog( m_aFrame,
982 											   "Can't create EmbedObject from storage!",
983 											   "Error:",
984 											   JOptionPane.ERROR_MESSAGE );
985 		}
986 		catch( Exception e )
987 		{
988 			JOptionPane.showMessageDialog( m_aFrame, e, "Exception in loadFileURI():", JOptionPane.ERROR_MESSAGE );
989 		}
990 	}
991 
storeLinkToStorage()992 	public void storeLinkToStorage()
993 	{
994 		if ( m_xStorage != null && m_bLinkObj )
995 		{
996 			try {
997 				XStream xLinkStream = m_xStorage.openStreamElement( "LinkName", ElementModes.ELEMENT_WRITE );
998 
999 				if ( xLinkStream != null )
1000 				{
1001 					XOutputStream xLinkOutStream = xLinkStream.getOutputStream();
1002 					XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface( XTruncate.class,
1003 																			 	xLinkOutStream );
1004 					if ( xLinkOutStream != null && xTruncate != null )
1005 					{
1006 						xTruncate.truncate();
1007 
1008 						char[] aLinkChar = m_aLinkURI.toCharArray();
1009 						byte[] aLinkBytes = new byte[ aLinkChar.length ];
1010 						for ( int ind = 0; ind < aLinkChar.length; ind++ )
1011 							aLinkBytes[ind] = (byte)aLinkChar[ind];
1012 
1013 						xLinkOutStream.writeBytes( aLinkBytes );
1014 						xLinkOutStream.closeOutput();
1015 
1016 						XComponent xComponent = (XComponent) UnoRuntime.queryInterface( XComponent.class,
1017 																						xLinkStream );
1018 						if ( xComponent != null )
1019 							xComponent.dispose();
1020 					}
1021 					else
1022 						JOptionPane.showMessageDialog( m_aFrame,
1023 														"The substream can not be truncated or written!",
1024 														"Error:",
1025 														JOptionPane.ERROR_MESSAGE );
1026 
1027 				}
1028 				else
1029 					JOptionPane.showMessageDialog( m_aFrame,
1030 													"Can't create/open substream!",
1031 													"Error:",
1032 													JOptionPane.ERROR_MESSAGE );
1033 			}
1034 			catch( Exception e )
1035 			{
1036 				JOptionPane.showMessageDialog( m_aFrame,
1037 											e,
1038 											"Exception in storeLinkToStorage:",
1039 											JOptionPane.ERROR_MESSAGE );
1040 
1041 			}
1042 		}
1043 	}
1044 }
1045 
1046