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