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