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