nss: upgrade to release 3.73
[LibreOffice.git] / odk / examples / java / EmbedDocument / Container1 / EmbedContApp.java
bloba275c27eaf7f2ae593c1c426419a98d83e1f0424
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import java.awt.*;
21 import java.applet.*;
22 import java.awt.event.*;
23 import java.net.*;
24 import java.io.*;
26 import javax.swing.JOptionPane;
28 import com.sun.star.lang.XMultiServiceFactory;
29 import com.sun.star.lang.XSingleServiceFactory;
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.uno.XInterface;
33 import com.sun.star.uno.AnyConverter;
34 import com.sun.star.uno.Type;
36 import com.sun.star.lang.XComponent;
38 import com.sun.star.beans.PropertyValue;
40 import com.sun.star.datatransfer.DataFlavor;
41 import com.sun.star.datatransfer.XTransferable;
43 import com.sun.star.container.XNameAccess;
45 import com.sun.star.io.XStream;
46 import com.sun.star.io.XInputStream;
47 import com.sun.star.io.XOutputStream;
48 import com.sun.star.io.XTruncate;
50 import com.sun.star.util.XCloseable;
52 import com.sun.star.embed.*;
54 public class EmbedContApp extends Applet implements MouseListener, XEmbeddedClient
56 private XMultiServiceFactory m_xServiceFactory;
58 private XEmbeddedObject m_xEmbedObj;
59 private XStorage m_xStorage;
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;
67 private boolean m_bOwnFile = false;
69 private boolean m_bLinkObj = false;
70 private String m_aLinkURI;
72 public EmbedContApp( Frame aFrame, XMultiServiceFactory xServiceFactory )
74 m_aFrame = aFrame;
75 m_xServiceFactory = xServiceFactory;
78 public void init()
80 resize( 640, 480 );
81 setBackground( Color.gray );
83 m_aToolkit = Toolkit.getDefaultToolkit();
85 // Get a menu bar.
86 MenuBar aMenuBar = m_aFrame.getMenuBar();
87 if( aMenuBar == null )
89 aMenuBar = new MenuBar();
90 m_aFrame.setMenuBar( aMenuBar );
93 // Create menus for the menu bar.
95 // File menu
96 m_aFileMenu = new Menu( "File", true );
97 aMenuBar.add( m_aFileMenu );
99 MenuItem aItem = new NewMenuItem();
100 m_aFileMenu.add( aItem );
102 aItem = new OpenFileMenuItem();
103 m_aFileMenu.add( aItem );
105 aItem = new SaveMenuItem();
106 m_aFileMenu.add( aItem );
108 aItem = new SaveAsMenuItem();
109 m_aFileMenu.add( aItem );
111 // Object menu
112 m_aObjectMenu = new Menu( "Object", true );
113 aMenuBar.add( m_aObjectMenu );
115 aItem = new NewObjectMenuItem();
116 m_aObjectMenu.add( aItem );
118 aItem = new LoadObjectMenuItem();
119 m_aObjectMenu.add( aItem );
121 aItem = new LinkObjectMenuItem();
122 m_aObjectMenu.add( aItem );
124 aItem = new ConvertLinkToEmbedMenuItem();
125 m_aObjectMenu.add( aItem );
127 // Handle mouse clicks in our window.
128 addMouseListener( this );
131 public void update( Graphics g )
133 paint( g );
136 public void paint( Graphics g )
138 super.paint( g );
140 if ( m_xEmbedObj != null )
142 synchronized( this )
144 if ( m_aImage != null )
145 g.drawImage( m_aImage, 0, 0, EmbedContApp.this );
150 public void generateNewImage()
152 if ( m_xEmbedObj != null )
154 try {
155 int nOldState = m_xEmbedObj.getCurrentState();
156 int nState = nOldState;
157 if ( nOldState == EmbedStates.LOADED )
159 m_xEmbedObj.changeState( EmbedStates.RUNNING );
160 nState = EmbedStates.RUNNING;
163 if ( nState == EmbedStates.ACTIVE || nState == EmbedStates.RUNNING )
165 XComponentSupplier xCompProv = (XComponentSupplier)UnoRuntime.queryInterface(
166 XComponentSupplier.class,
167 m_xEmbedObj );
168 if ( xCompProv != null )
170 XCloseable xComp = xCompProv.getComponent();
171 XTransferable xTransfer = (XTransferable)UnoRuntime.queryInterface(
172 XTransferable.class,
173 xComp );
174 if ( xTransfer != null )
176 DataFlavor aFlavor = new DataFlavor();
177 aFlavor.MimeType = "image/png";
178 aFlavor.HumanPresentableName = "Portable Network Graphics";
179 aFlavor.DataType = new Type( byte[].class );
181 byte[] aPNGData = (byte[])AnyConverter.toArray( xTransfer.getTransferData( aFlavor ) );
182 if ( aPNGData != null && aPNGData.length != 0 )
184 synchronized( this )
186 m_aImage = m_aToolkit.createImage( aPNGData );
190 else
191 System.out.println( "paint() : can not get XTransferable for the component!\n" );
193 else
194 System.out.println( "paint() : XComponentSupplier is not implemented!\n" );
197 catch( com.sun.star.uno.Exception e )
199 // dialogs should not be used in paint()
200 System.out.println( "Exception in paint(): " + e );
205 public void mouseClicked( MouseEvent e )
207 if( e.getModifiers() == InputEvent.BUTTON1_MASK )
209 // activate object if exists and not active
210 if ( m_xEmbedObj != null )
212 try {
213 m_xEmbedObj.changeState( EmbedStates.ACTIVE );
215 catch( Exception ex )
217 JOptionPane.showMessageDialog( m_aFrame, ex, "Exception on mouse click", JOptionPane.ERROR_MESSAGE );
223 public void mousePressed( MouseEvent e ){};
224 public void mouseEntered( MouseEvent e ){};
225 public void mouseExited( MouseEvent e ){};
226 public void mouseReleased( MouseEvent e ){};
228 // XEmbeddedClient
229 public void saveObject()
230 throws com.sun.star.uno.Exception
232 if ( m_xEmbedObj != null )
234 try {
235 XEmbedPersist xPersist = (XEmbedPersist)UnoRuntime.queryInterface( XEmbedPersist.class, m_xEmbedObj );
236 if ( xPersist != null )
238 xPersist.storeOwn();
239 generateNewImage();
241 else
242 JOptionPane.showMessageDialog( m_aFrame, "No XEmbedPersist!", "Error:", JOptionPane.ERROR_MESSAGE );
244 catch( Exception e )
246 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in saveObject:", JOptionPane.ERROR_MESSAGE );
250 generateNewImage();
251 repaint();
254 public void onShowWindow( boolean bVisible )
256 // for now nothing to do
259 class NewMenuItem extends MenuItem implements ActionListener // Menu New
261 public NewMenuItem()
263 super( "New", new MenuShortcut( KeyEvent.VK_A ));
264 addActionListener( this );
267 public void actionPerformed( ActionEvent e )
269 // clear everything
270 clearObjectAndStorage();
272 repaint();
276 class SaveAsMenuItem extends MenuItem implements ActionListener // Menu SaveAs...
278 public SaveAsMenuItem()
280 super( "SaveAs..." );
281 addActionListener( this );
284 public void actionPerformed( ActionEvent e )
286 // open SaveAs dialog and store
288 if ( m_xStorage != null && m_xEmbedObj != null )
290 FileDialog aFileDialog = new FileDialog( m_aFrame, "SaveAs", FileDialog.SAVE );
291 aFileDialog.show();
292 if ( aFileDialog.getFile() != null )
294 String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
295 File aFile = new File( aFileName );
296 if ( aFile != null )
298 // create object from specified file
299 String aFileURI = aFile.toURI().toASCIIString();
300 try {
301 saveObject();
303 if ( m_bLinkObj )
304 storeLinkToStorage();
306 saveStorageAsFileURI( aFileURI );
308 catch( Exception ex )
310 JOptionPane.showMessageDialog( m_aFrame,
312 "Exception in SaveAsMenuItem:",
313 JOptionPane.ERROR_MESSAGE );
318 else
319 JOptionPane.showMessageDialog( m_aFrame, "No document is embedded!", "Error:", JOptionPane.ERROR_MESSAGE );
323 class OpenFileMenuItem extends MenuItem implements ActionListener // Menu Open
325 public OpenFileMenuItem()
327 super( "Open", new MenuShortcut( KeyEvent.VK_C ));
328 addActionListener( this );
331 public void actionPerformed( ActionEvent e )
333 // clear everything
334 clearObjectAndStorage();
336 // open OpenFile dialog and load doc
337 FileDialog aFileDialog = new FileDialog( m_aFrame, "Open" );
338 aFileDialog.show();
339 if ( aFileDialog.getFile() != null )
341 String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
342 File aFile = new File( aFileName );
343 if ( aFile != null )
345 // create object from specified file
346 String aFileURI = aFile.toURI().toASCIIString();
348 // load from specified file
349 loadFileURI( aFileURI );
351 if ( m_xEmbedObj != null )
353 try {
354 m_xEmbedObj.setClientSite( EmbedContApp.this );
356 catch( Exception ex )
358 JOptionPane.showMessageDialog( m_aFrame,
360 "Exception in OpenFileMenuItem:",
361 JOptionPane.ERROR_MESSAGE );
367 generateNewImage();
368 repaint();
372 class SaveMenuItem extends MenuItem implements ActionListener // Menu Save
374 public SaveMenuItem()
376 super( "Save", new MenuShortcut( KeyEvent.VK_D ));
377 addActionListener( this );
380 public void actionPerformed( ActionEvent e )
382 // if has persistence store there
383 // if not open SaveAs dialog and store
384 if ( m_xStorage != null && m_xEmbedObj != null )
386 if ( m_bOwnFile )
388 if ( m_xStorage == null )
390 JOptionPane.showMessageDialog( m_aFrame,
391 "No storage for owned file!",
392 "Error:",
393 JOptionPane.ERROR_MESSAGE );
394 return;
397 try {
398 saveObject();
400 if ( m_bLinkObj )
401 storeLinkToStorage();
403 XTransactedObject xTransact = (XTransactedObject)UnoRuntime.queryInterface( XTransactedObject.class,
404 m_xStorage );
405 if ( xTransact != null )
406 xTransact.commit();
408 catch( Exception ex )
410 JOptionPane.showMessageDialog( m_aFrame,
412 "Exception during save operation in SaveMenuItem:",
413 JOptionPane.ERROR_MESSAGE );
416 else
418 FileDialog aFileDialog = new FileDialog( m_aFrame, "SaveAs", FileDialog.SAVE );
419 aFileDialog.show();
420 if ( aFileDialog.getFile() != null )
422 String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
423 File aFile = new File( aFileName );
424 if ( aFile != null )
426 // create object from specified file
427 String aFileURI = aFile.toURI().toASCIIString();
428 try {
429 saveObject();
431 if ( m_bLinkObj )
432 storeLinkToStorage();
434 saveStorageAsFileURI( aFileURI );
436 catch( Exception ex )
438 JOptionPane.showMessageDialog( m_aFrame,
440 "Exception during 'save as' operation in SaveMenuItem:",
441 JOptionPane.ERROR_MESSAGE );
447 else
448 JOptionPane.showMessageDialog( m_aFrame, "No document is embedded!", "Error:", JOptionPane.ERROR_MESSAGE );
452 class NewObjectMenuItem extends MenuItem implements ActionListener // Menu NewObject
454 public NewObjectMenuItem()
456 super( "Create", new MenuShortcut( KeyEvent.VK_N ));
457 addActionListener( this );
460 public void actionPerformed( ActionEvent e )
462 // remove current object an init a new one
463 clearObjectAndStorage();
465 Object[] possibleValues = { "com.sun.star.comp.Writer.TextDocument",
466 "com.sun.star.comp.Writer.GlobalDocument",
467 "com.sun.star.comp.Writer.WebDocument",
468 "com.sun.star.comp.Calc.SpreadsheetDocument",
469 "com.sun.star.comp.Draw.PresentationDocument",
470 "com.sun.star.comp.Draw.DrawingDocument",
471 "com.sun.star.comp.Math.FormulaDocument" };
473 String selectedValue = (String)JOptionPane.showInputDialog( null, "DocumentType", "Select",
474 JOptionPane.INFORMATION_MESSAGE, null,
475 possibleValues, possibleValues[0] );
477 if ( selectedValue != null )
479 m_xStorage = createTempStorage();
481 if ( m_xStorage != null )
482 m_xEmbedObj = createEmbedObject( selectedValue );
483 else
484 JOptionPane.showMessageDialog( m_aFrame,
485 "Can't create temporary storage!",
486 "Error:",
487 JOptionPane.ERROR_MESSAGE );
490 if ( m_xEmbedObj != null )
492 try {
493 m_xEmbedObj.setClientSite( EmbedContApp.this );
495 catch( Exception ex )
497 JOptionPane.showMessageDialog( m_aFrame,
499 "Exception in NewObjectMenuItem:",
500 JOptionPane.ERROR_MESSAGE );
505 generateNewImage();
506 repaint();
510 class LoadObjectMenuItem extends MenuItem implements ActionListener // Menu LoadObject
512 public LoadObjectMenuItem()
514 super( "Load from file", new MenuShortcut( KeyEvent.VK_L ));
515 addActionListener( this );
518 public void actionPerformed( ActionEvent e )
520 // first remove current object
521 clearObjectAndStorage();
523 // open OpenFile dialog and load doc
524 FileDialog aFileDialog = new FileDialog( m_aFrame, "Select sources to use for object init" );
525 aFileDialog.show();
526 if ( aFileDialog.getFile() != null )
528 String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
529 File aFile = new File( aFileName );
530 if ( aFile != null )
532 // create object from specified file
533 String aFileURI = aFile.toURI().toASCIIString();
534 m_xStorage = createTempStorage();
536 if ( m_xStorage != null )
537 m_xEmbedObj = loadEmbedObject( aFileURI );
539 if ( m_xEmbedObj != null )
541 try {
542 m_xEmbedObj.setClientSite( EmbedContApp.this );
544 catch( Exception ex )
546 JOptionPane.showMessageDialog( m_aFrame,
548 "Exception in LoadObjectMenuItem:",
549 JOptionPane.ERROR_MESSAGE );
555 generateNewImage();
556 repaint();
560 class LinkObjectMenuItem extends MenuItem implements ActionListener // Menu LinkObject
562 public LinkObjectMenuItem()
564 super( "Create link", new MenuShortcut( KeyEvent.VK_M ));
565 addActionListener( this );
568 public void actionPerformed( ActionEvent e )
570 // first remove current object
571 clearObjectAndStorage();
573 // open OpenFile dialog and load doc
574 FileDialog aFileDialog = new FileDialog( m_aFrame, "Select sources to use for object init" );
575 aFileDialog.show();
576 if ( aFileDialog.getFile() != null )
578 m_xStorage = createTempStorage();
580 String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
581 File aFile = new File( aFileName );
582 if ( aFile != null )
584 // create object from specified file
585 String aFileURI = aFile.toURI().toASCIIString();
587 m_xEmbedObj = createLinkObject( aFileURI );
589 if ( m_xEmbedObj != null )
591 m_aLinkURI = aFileURI;
592 m_bLinkObj = true;
594 try {
595 m_xEmbedObj.setClientSite( EmbedContApp.this );
597 catch( Exception ex )
599 JOptionPane.showMessageDialog( m_aFrame,
601 "Exception in LinkObjectMenuItem:",
602 JOptionPane.ERROR_MESSAGE );
608 generateNewImage();
609 repaint();
613 class ConvertLinkToEmbedMenuItem extends MenuItem implements ActionListener // Menu LinkObject
615 public ConvertLinkToEmbedMenuItem()
617 super( "Convert link to embed", new MenuShortcut( KeyEvent.VK_M ));
618 addActionListener( this );
621 public void actionPerformed( ActionEvent e )
623 if ( !m_bLinkObj )
625 JOptionPane.showMessageDialog( m_aFrame, "The object is not a link!", "Error:", JOptionPane.ERROR_MESSAGE );
626 return;
629 if ( m_xEmbedObj != null )
631 if ( m_xStorage != null )
633 try {
634 XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface( XNameAccess.class,
635 m_xStorage );
636 if ( xNameAccess != null && xNameAccess.hasByName( "LinkName" ) )
637 m_xStorage.removeElement( "LinkName" );
639 XEmbedPersist xPersist = (XEmbedPersist)UnoRuntime.queryInterface( XEmbedPersist.class,
640 m_xEmbedObj );
641 if ( xPersist != null )
643 PropertyValue[] pEmp = new PropertyValue[0];
644 xPersist.setPersistentEntry( m_xStorage, "EmbedSub", EntryInitModes.NO_INIT, pEmp );
645 m_bLinkObj = false;
646 m_aLinkURI = null;
648 else
649 JOptionPane.showMessageDialog( m_aFrame,
650 "No XEmbedPersist in ConvertLink... !",
651 "Error:",
652 JOptionPane.ERROR_MESSAGE );
654 catch( Exception e1 )
656 JOptionPane.showMessageDialog( m_aFrame,
658 "Exception in ConvertLinkToEmbed:try 1 :",
659 JOptionPane.ERROR_MESSAGE );
666 // Helper methods
667 public XEmbeddedObject createEmbedObject( String aServiceName )
669 XEmbeddedObject xEmbObj = null;
670 byte[] pClassID = new byte[16];
672 if ( aServiceName.equals( "com.sun.star.comp.Writer.TextDocument" ) )
674 int[] pTempClassID = { 0x8B, 0xC6, 0xB1, 0x65, 0xB1, 0xB2, 0x4E, 0xDD,
675 0xAA, 0x47, 0xDA, 0xE2, 0xEE, 0x68, 0x9D, 0xD6 };
676 for ( int ind = 0; ind < 16; ind++ )
677 pClassID[ind] = (byte)pTempClassID[ind];
679 else if ( aServiceName.equals( "com.sun.star.comp.Writer.GlobalDocument" ) )
681 int[] pTempClassID = { 0xB2, 0x1A, 0x0A, 0x7C, 0xE4, 0x03, 0x41, 0xFE,
682 0x95, 0x62, 0xBD, 0x13, 0xEA, 0x6F, 0x15, 0xA0 };
683 for ( int ind = 0; ind < 16; ind++ )
684 pClassID[ind] = (byte)pTempClassID[ind];
686 else if ( aServiceName.equals( "com.sun.star.comp.Writer.WebDocument" ) )
688 int[] pTempClassID = { 0xA8, 0xBB, 0xA6, 0x0C, 0x7C, 0x60, 0x45, 0x50,
689 0x91, 0xCE, 0x39, 0xC3, 0x90, 0x3F, 0xAC, 0x5E };
690 for ( int ind = 0; ind < 16; ind++ )
691 pClassID[ind] = (byte)pTempClassID[ind];
693 else if ( aServiceName.equals( "com.sun.star.comp.Calc.SpreadsheetDocument" ) )
695 int[] pTempClassID = { 0x47, 0xBB, 0xB4, 0xCB, 0xCE, 0x4C, 0x4E, 0x80,
696 0xA5, 0x91, 0x42, 0xD9, 0xAE, 0x74, 0x95, 0x0F };
697 for ( int ind = 0; ind < 16; ind++ )
698 pClassID[ind] = (byte)pTempClassID[ind];
700 else if ( aServiceName.equals( "com.sun.star.comp.Draw.PresentationDocument" ) )
702 int[] pTempClassID = { 0x91, 0x76, 0xE4, 0x8A, 0x63, 0x7A, 0x4D, 0x1F,
703 0x80, 0x3B, 0x99, 0xD9, 0xBF, 0xAC, 0x10, 0x47 };
704 for ( int ind = 0; ind < 16; ind++ )
705 pClassID[ind] = (byte)pTempClassID[ind];
707 else if ( aServiceName.equals( "com.sun.star.comp.Draw.DrawingDocument" ) )
709 int[] pTempClassID = { 0x4B, 0xAB, 0x89, 0x70, 0x8A, 0x3B, 0x45, 0xB3,
710 0x99, 0x1C, 0xCB, 0xEE, 0xAC, 0x6B, 0xD5, 0xE3 };
711 for ( int ind = 0; ind < 16; ind++ )
712 pClassID[ind] = (byte)pTempClassID[ind];
714 else if ( aServiceName.equals( "com.sun.star.comp.Math.FormulaDocument" ) )
716 int[] pTempClassID = { 0x07, 0x8B, 0x7A, 0xBA, 0x54, 0xFC, 0x45, 0x7F,
717 0x85, 0x51, 0x61, 0x47, 0xE7, 0x76, 0xA9, 0x97 };
718 for ( int ind = 0; ind < 16; ind++ )
719 pClassID[ind] = (byte)pTempClassID[ind];
722 if ( pClassID != null )
724 // create embedded object based on the class ID
725 try {
726 Object oEmbedFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectFactory" );
727 XEmbedObjectFactory xEmbedFactory = (XEmbedObjectFactory)UnoRuntime.queryInterface(
728 XEmbedObjectFactory.class,
729 oEmbedFactory );
730 if ( xEmbedFactory != null )
732 Object oEmbObj = xEmbedFactory.createInstanceInitNew( pClassID,
733 "Dummy name",
734 m_xStorage,
735 "EmbedSub" );
736 xEmbObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
738 else
739 JOptionPane.showMessageDialog( m_aFrame,
740 "Can't create EmbedFactory!",
741 "Error:",
742 JOptionPane.ERROR_MESSAGE );
744 catch( Exception e )
746 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in createInstanceInitNew():", JOptionPane.ERROR_MESSAGE );
749 else
750 JOptionPane.showMessageDialog( m_aFrame, "Can't retrieve class ID!", "Error:", JOptionPane.ERROR_MESSAGE );
752 return xEmbObj;
755 public XEmbeddedObject createLinkObject( String aLinkURL )
757 XEmbeddedObject xEmbObj = null;
759 try {
760 Object oEmbedFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectFactory" );
761 XEmbedObjectFactory xEmbedFactory = (XEmbedObjectFactory)UnoRuntime.queryInterface(
762 XEmbedObjectFactory.class,
763 oEmbedFactory );
764 if ( xEmbedFactory != null )
766 Object oEmbObj = xEmbedFactory.createInstanceLink( aLinkURL );
767 xEmbObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
769 else
770 JOptionPane.showMessageDialog( m_aFrame,
771 "Can't create EmbedFactory!",
772 "Error:",
773 JOptionPane.ERROR_MESSAGE );
775 catch( Exception e )
777 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in createLinkObject():", JOptionPane.ERROR_MESSAGE );
781 return xEmbObj;
785 public XEmbeddedObject loadEmbedObject( String aFileURI )
787 XEmbeddedObject xEmbObj = null;
788 try {
789 Object oEmbedFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectFactory" );
790 XEmbedObjectFactory xEmbedFactory = (XEmbedObjectFactory)UnoRuntime.queryInterface(
791 XEmbedObjectFactory.class,
792 oEmbedFactory );
793 if ( xEmbedFactory != null )
795 PropertyValue[] aMedDescr = { new PropertyValue(), new PropertyValue() };
796 aMedDescr[0].Name = "URL";
797 aMedDescr[0].Value = (Object) aFileURI;
798 aMedDescr[1].Name = "ReadOnly";
799 aMedDescr[1].Value = (Object) Boolean.FALSE;
800 Object oEmbObj = xEmbedFactory.createInstanceInitFromMediaDescriptor( m_xStorage,
801 "EmbedSub",
802 aMedDescr );
803 xEmbObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
805 else
806 JOptionPane.showMessageDialog( m_aFrame,
807 "Can't create EmbedFactory!",
808 "Error:",
809 JOptionPane.ERROR_MESSAGE );
811 catch( Exception e )
813 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in loadEmbedObject():", JOptionPane.ERROR_MESSAGE );
816 return xEmbObj;
819 public void clearObjectAndStorage()
821 synchronized( this )
823 m_aImage = null;
826 m_bOwnFile = false;
828 m_aLinkURI = null;
829 m_bLinkObj = false;
831 if ( m_xEmbedObj != null )
833 try {
834 XComponent xComponent = (XComponent)UnoRuntime.queryInterface( XComponent.class, m_xEmbedObj );
835 if ( xComponent != null )
836 xComponent.dispose();
838 catch ( Exception ex )
840 m_xEmbedObj = null;
843 if ( m_xStorage != null )
845 try {
846 XComponent xComponent = (XComponent)UnoRuntime.queryInterface( XComponent.class, m_xStorage );
847 if ( xComponent != null )
848 xComponent.dispose();
850 catch ( Exception ex )
852 m_xStorage = null;
856 public XStorage createTempStorage()
858 XStorage xTempStorage = null;
860 try {
861 Object oStorageFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.StorageFactory" );
862 XSingleServiceFactory xStorageFactory = (XSingleServiceFactory)UnoRuntime.queryInterface(
863 XSingleServiceFactory.class,
864 oStorageFactory );
865 if ( xStorageFactory != null )
867 Object oStorage = xStorageFactory.createInstance();
868 xTempStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
870 else
871 JOptionPane.showMessageDialog( m_aFrame,
872 "Can't create StorageFactory!",
873 "Error:",
874 JOptionPane.ERROR_MESSAGE );
876 catch( Exception e )
878 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in createTempStorage():", JOptionPane.ERROR_MESSAGE );
881 return xTempStorage;
884 public void saveStorageAsFileURI( String aFileURI )
886 try {
887 Object oStorageFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.StorageFactory" );
888 XSingleServiceFactory xStorageFactory = (XSingleServiceFactory)UnoRuntime.queryInterface(
889 XSingleServiceFactory.class,
890 oStorageFactory );
891 if ( xStorageFactory != null )
893 Object aArgs[] = new Object[2];
894 aArgs[0] = aFileURI;
895 aArgs[1] = Integer.valueOf( ElementModes.READWRITE );
897 Object oStorage = xStorageFactory.createInstanceWithArguments( aArgs );
898 XStorage xTargetStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
899 m_xStorage.copyToStorage( xTargetStorage );
901 XComponent xComponent = (XComponent)UnoRuntime.queryInterface( XComponent.class, m_xStorage );
902 xComponent.dispose();
904 m_xStorage = xTargetStorage;
905 m_bOwnFile = true;
907 else
908 JOptionPane.showMessageDialog( m_aFrame,
909 "Can't create StorageFactory!",
910 "Error:",
911 JOptionPane.ERROR_MESSAGE );
913 catch( Exception e )
915 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in saveStorageToFileURI():", JOptionPane.ERROR_MESSAGE );
920 public void loadFileURI( String aFileURI )
924 Object oStorageFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.StorageFactory" );
925 XSingleServiceFactory xStorageFactory = (XSingleServiceFactory)UnoRuntime.queryInterface(
926 XSingleServiceFactory.class,
927 oStorageFactory );
928 Object aArgs[] = new Object[2];
929 aArgs[0] = aFileURI;
930 aArgs[1] = Integer.valueOf( ElementModes.READWRITE );
932 Object oStorage = xStorageFactory.createInstanceWithArguments( aArgs );
933 XStorage xTargetStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
935 Object oEmbedFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectFactory" );
936 XEmbedObjectFactory xEmbedFactory = (XEmbedObjectFactory)UnoRuntime.queryInterface(
937 XEmbedObjectFactory.class,
938 oEmbedFactory );
940 XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface( XNameAccess.class,
941 xTargetStorage );
942 if ( xNameAccess == null )
944 JOptionPane.showMessageDialog( m_aFrame, "No XNameAccess!", "Error:", JOptionPane.ERROR_MESSAGE );
945 return;
948 Object oEmbObj = null;
949 if ( xNameAccess.hasByName( "LinkName" ) && xTargetStorage.isStreamElement( "LinkName" ) )
951 XStream xLinkStream = xTargetStorage.openStreamElement( "LinkName", ElementModes.READ );
952 if ( xLinkStream != null )
954 XInputStream xInStream = xLinkStream.getInputStream();
955 if ( xInStream != null )
957 byte[][] pBuff = new byte[1][0];
958 int nRead = xInStream.readBytes( pBuff, 1000 );
959 m_aLinkURI = new String( pBuff[0] );
960 xInStream.closeInput();
961 oEmbObj = xEmbedFactory.createInstanceLink( m_aLinkURI );
962 m_bLinkObj = true;
966 else
967 oEmbObj = xEmbedFactory.createInstanceInitFromEntry( xTargetStorage,
968 "EmbedSub",
969 false );
971 m_xEmbedObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
973 if ( m_xEmbedObj != null )
975 m_xStorage = xTargetStorage;
976 m_bOwnFile = true;
978 else
979 JOptionPane.showMessageDialog( m_aFrame,
980 "Can't create EmbedObject from storage!",
981 "Error:",
982 JOptionPane.ERROR_MESSAGE );
984 catch( Exception e )
986 JOptionPane.showMessageDialog( m_aFrame, e, "Exception in loadFileURI():", JOptionPane.ERROR_MESSAGE );
990 public void storeLinkToStorage()
992 if ( m_xStorage != null && m_bLinkObj )
994 try {
995 XStream xLinkStream = m_xStorage.openStreamElement( "LinkName", ElementModes.WRITE );
997 if ( xLinkStream != null )
999 XOutputStream xLinkOutStream = xLinkStream.getOutputStream();
1000 XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface( XTruncate.class,
1001 xLinkOutStream );
1002 if ( xLinkOutStream != null && xTruncate != null )
1004 xTruncate.truncate();
1006 char[] aLinkChar = m_aLinkURI.toCharArray();
1007 byte[] aLinkBytes = new byte[ aLinkChar.length ];
1008 for ( int ind = 0; ind < aLinkChar.length; ind++ )
1009 aLinkBytes[ind] = (byte)aLinkChar[ind];
1011 xLinkOutStream.writeBytes( aLinkBytes );
1012 xLinkOutStream.closeOutput();
1014 XComponent xComponent = (XComponent) UnoRuntime.queryInterface( XComponent.class,
1015 xLinkStream );
1016 if ( xComponent != null )
1017 xComponent.dispose();
1019 else
1020 JOptionPane.showMessageDialog( m_aFrame,
1021 "The substream can not be truncated or written!",
1022 "Error:",
1023 JOptionPane.ERROR_MESSAGE );
1026 else
1027 JOptionPane.showMessageDialog( m_aFrame,
1028 "Can't create/open substream!",
1029 "Error:",
1030 JOptionPane.ERROR_MESSAGE );
1032 catch( Exception e )
1034 JOptionPane.showMessageDialog( m_aFrame,
1036 "Exception in storeLinkToStorage:",
1037 JOptionPane.ERROR_MESSAGE );
1044 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */