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