1*113d1ee9SAndrew Rist /**************************************************************
2*113d1ee9SAndrew Rist  *
3*113d1ee9SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*113d1ee9SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*113d1ee9SAndrew Rist  * distributed with this work for additional information
6*113d1ee9SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*113d1ee9SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*113d1ee9SAndrew Rist  * "License"); you may not use this file except in compliance
9*113d1ee9SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*113d1ee9SAndrew Rist  *
11*113d1ee9SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*113d1ee9SAndrew Rist  *
13*113d1ee9SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*113d1ee9SAndrew Rist  * software distributed under the License is distributed on an
15*113d1ee9SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*113d1ee9SAndrew Rist  * KIND, either express or implied.  See the License for the
17*113d1ee9SAndrew Rist  * specific language governing permissions and limitations
18*113d1ee9SAndrew Rist  * under the License.
19*113d1ee9SAndrew Rist  *
20*113d1ee9SAndrew Rist  *************************************************************/
21*113d1ee9SAndrew Rist 
22cdf0e10cSrcweir package embeddedobj.test;
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import java.awt.*;
25cdf0e10cSrcweir import java.applet.*;
26cdf0e10cSrcweir import java.awt.event.*;
27cdf0e10cSrcweir import java.net.*;
28cdf0e10cSrcweir import java.io.*;
29cdf0e10cSrcweir import java.util.Vector;
30cdf0e10cSrcweir 
31cdf0e10cSrcweir import javax.swing.JOptionPane;
32cdf0e10cSrcweir import javax.swing.Timer;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
35cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
38cdf0e10cSrcweir import com.sun.star.uno.XInterface;
39cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
40cdf0e10cSrcweir import com.sun.star.uno.Type;
41cdf0e10cSrcweir import com.sun.star.uno.Any;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir import com.sun.star.lang.XComponent;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir import com.sun.star.util.XCloseable;
46cdf0e10cSrcweir import com.sun.star.util.XURLTransformer;
47cdf0e10cSrcweir import com.sun.star.util.URL;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
50cdf0e10cSrcweir import com.sun.star.beans.NamedValue;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir import com.sun.star.datatransfer.DataFlavor;
53cdf0e10cSrcweir import com.sun.star.datatransfer.XTransferable;
54cdf0e10cSrcweir 
55cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir import com.sun.star.io.XStream;
58cdf0e10cSrcweir import com.sun.star.io.XInputStream;
59cdf0e10cSrcweir import com.sun.star.io.XOutputStream;
60cdf0e10cSrcweir import com.sun.star.io.XTruncate;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir import com.sun.star.awt.XWindow;
63cdf0e10cSrcweir import com.sun.star.awt.XBitmap;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir import com.sun.star.task.XJob;
66cdf0e10cSrcweir 
67cdf0e10cSrcweir import com.sun.star.embed.*;
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 
70cdf0e10cSrcweir class ActionObject
71cdf0e10cSrcweir {
72cdf0e10cSrcweir 	public byte m_nID;
73cdf0e10cSrcweir 	public String m_sParam;
74cdf0e10cSrcweir 
ActionObject()75cdf0e10cSrcweir 	public ActionObject()
76cdf0e10cSrcweir 	{
77cdf0e10cSrcweir 		m_nID = 0;
78cdf0e10cSrcweir 		m_sParam = null;
79cdf0e10cSrcweir 	}
80cdf0e10cSrcweir 
ActionObject( byte nID )81cdf0e10cSrcweir 	public ActionObject( byte nID )
82cdf0e10cSrcweir 	{
83cdf0e10cSrcweir 		m_nID = nID;
84cdf0e10cSrcweir 		m_sParam = null;
85cdf0e10cSrcweir 	}
86cdf0e10cSrcweir 
ActionObject( byte nID, String sParam )87cdf0e10cSrcweir 	public ActionObject( byte nID, String sParam )
88cdf0e10cSrcweir 	{
89cdf0e10cSrcweir 		m_nID = nID;
90cdf0e10cSrcweir 		m_sParam = sParam;
91cdf0e10cSrcweir 	}
92cdf0e10cSrcweir 
ActionObject( ActionObject aObject )93cdf0e10cSrcweir 	public ActionObject( ActionObject aObject )
94cdf0e10cSrcweir 	{
95cdf0e10cSrcweir 		m_nID = aObject.m_nID;
96cdf0e10cSrcweir 		m_sParam = aObject.m_sParam;
97cdf0e10cSrcweir 	}
98cdf0e10cSrcweir };
99cdf0e10cSrcweir 
100cdf0e10cSrcweir public class EmbedContApp extends Applet
101cdf0e10cSrcweir 	implements MouseListener, XEmbeddedClient, ActionListener, XJob, XInplaceClient, XWindowSupplier
102cdf0e10cSrcweir {
103cdf0e10cSrcweir 	private XMultiServiceFactory m_xServiceFactory;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 	private final boolean m_bStoreVisRepl = false;
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 	private XJob m_xMainThreadExecutor;
108cdf0e10cSrcweir 	private NamedValue[] m_pValuesForExecutor;
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 	private XEmbeddedObject m_xEmbedObj;
111cdf0e10cSrcweir 	private XStorage m_xStorage;
112cdf0e10cSrcweir 	private float	 m_nXScaling;
113cdf0e10cSrcweir 	private float	 m_nYScaling;
114cdf0e10cSrcweir 	private float	 m_nXPixelSize;
115cdf0e10cSrcweir 	private float	 m_nYPixelSize;
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 	private Frame m_aFrame;
118cdf0e10cSrcweir 	private Menu m_aFileMenu;
119cdf0e10cSrcweir 	private Menu m_aObjectMenu;
120cdf0e10cSrcweir 	private Toolkit m_aToolkit;
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 	private Image m_aImage;
123cdf0e10cSrcweir 	private Object m_oImageLock;
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 	private boolean m_bOwnFile = false;
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 	private boolean m_bLinkObj = false;
128cdf0e10cSrcweir 	private String m_aLinkURI;
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 	private Object m_oActionsNumberLock;
131cdf0e10cSrcweir 	private Vector m_aActionsList;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 	private Timer m_aTimer;
134cdf0e10cSrcweir 	private boolean m_bDestroyed = false;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 	private Object m_oInHandlerLock;
137cdf0e10cSrcweir 	private boolean m_bInHandler = false;
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 	private XURLTransformer m_xTransformer;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 	private NativeView m_aNativeView;
142cdf0e10cSrcweir 	private XWindow m_xVCLWindow;
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 	private XBitmap m_xBitmap;
145cdf0e10cSrcweir 	private BitmapPainter m_aBitmapPainter;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir // Constants
148cdf0e10cSrcweir 	private final byte DESTROY					=  1;
149cdf0e10cSrcweir 	private final byte ACTIVATE_OUTPLACE		=  2;
150cdf0e10cSrcweir 	private final byte NEW_DOCUMENT				=  3;
151cdf0e10cSrcweir 	private final byte SAVE_AS					=  4;
152cdf0e10cSrcweir 	private final byte OPEN_FILE				=  5;
153cdf0e10cSrcweir 	private final byte SAVE						=  6;
154cdf0e10cSrcweir 	private final byte NEW_OBJECT				=  7;
155cdf0e10cSrcweir 	private final byte OBJECT_FROM_FILE			=  8;
156cdf0e10cSrcweir 	private final byte LINK_FROM_FILE			=  9;
157cdf0e10cSrcweir 	private final byte CONVERT_LINK_TO_OBJECT	= 10;
158cdf0e10cSrcweir 	private final byte ACTIVATE_INPLACE			= 11;
159cdf0e10cSrcweir 	private final byte DEACTIVATE				= 12;
160cdf0e10cSrcweir 
161cdf0e10cSrcweir // Methods
EmbedContApp( Frame aFrame, XMultiServiceFactory xServiceFactory )162cdf0e10cSrcweir 	public EmbedContApp( Frame aFrame, XMultiServiceFactory xServiceFactory )
163cdf0e10cSrcweir 	{
164cdf0e10cSrcweir 		m_aFrame = aFrame;
165cdf0e10cSrcweir 		m_xServiceFactory = xServiceFactory;
166cdf0e10cSrcweir 	}
167cdf0e10cSrcweir 
init()168cdf0e10cSrcweir 	public void init()
169cdf0e10cSrcweir 	{
170cdf0e10cSrcweir 		resize( 800, 600 );
171cdf0e10cSrcweir 		setBackground( Color.gray );
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 		m_aToolkit = Toolkit.getDefaultToolkit();
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 		try {
176cdf0e10cSrcweir 			Object oTransformer = m_xServiceFactory.createInstance( "com.sun.star.util.URLTransformer" );
177cdf0e10cSrcweir 			m_xTransformer = (XURLTransformer)UnoRuntime.queryInterface( XURLTransformer.class, oTransformer );
178cdf0e10cSrcweir 		} catch( Exception e ) { System.exit( 0 ); }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 		m_oActionsNumberLock = new Object();
181cdf0e10cSrcweir 		m_aActionsList = new Vector();
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 		m_oInHandlerLock = new Object();
184cdf0e10cSrcweir 		m_oImageLock = new Object();
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 		try {
187cdf0e10cSrcweir 			Object oJob = m_xServiceFactory.createInstance( "com.sun.star.comp.thread.MainThreadExecutor" );
188cdf0e10cSrcweir 			m_xMainThreadExecutor = (XJob)UnoRuntime.queryInterface( XJob.class, oJob );
189cdf0e10cSrcweir 		} catch( Exception e ) {}
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 		if ( m_xMainThreadExecutor == null )
192cdf0e10cSrcweir 		{
193cdf0e10cSrcweir 			System.out.println( "Can't create MainThreadExecutor! The application is unusable!" );
194cdf0e10cSrcweir 			System.exit( 0 );
195cdf0e10cSrcweir 		}
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 		m_nXScaling = 1;
198cdf0e10cSrcweir 		m_nYScaling = 1;
199cdf0e10cSrcweir 		m_nXPixelSize = 1;
200cdf0e10cSrcweir 		m_nYPixelSize = 1;
201cdf0e10cSrcweir 
202cdf0e10cSrcweir 		m_pValuesForExecutor = new NamedValue[1];
203cdf0e10cSrcweir 		m_pValuesForExecutor[0] = new NamedValue( "JobToExecute", (Object)this );
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 		m_aTimer = new Timer( 100, this );
206cdf0e10cSrcweir 		m_aTimer.start();
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 		// Get a menu bar.
209cdf0e10cSrcweir 		MenuBar aMenuBar = m_aFrame.getMenuBar();
210cdf0e10cSrcweir 		if( aMenuBar == null )
211cdf0e10cSrcweir 		{
212cdf0e10cSrcweir 			aMenuBar = new MenuBar();
213cdf0e10cSrcweir 			m_aFrame.setMenuBar( aMenuBar );
214cdf0e10cSrcweir 		}
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 		// Create menus for the menu bar.
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 		// File menu
219cdf0e10cSrcweir 		m_aFileMenu = new Menu( "File", true );
220cdf0e10cSrcweir 		aMenuBar.add( m_aFileMenu );
221cdf0e10cSrcweir 
222cdf0e10cSrcweir 		MenuItem aItem = new NewMenuItem();
223cdf0e10cSrcweir 		m_aFileMenu.add( aItem );
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 		aItem = new OpenFileMenuItem();
226cdf0e10cSrcweir 		m_aFileMenu.add( aItem );
227cdf0e10cSrcweir 
228cdf0e10cSrcweir 		aItem = new SaveMenuItem();
229cdf0e10cSrcweir 		m_aFileMenu.add( aItem );
230cdf0e10cSrcweir 
231cdf0e10cSrcweir 		aItem = new SaveAsMenuItem();
232cdf0e10cSrcweir 		m_aFileMenu.add( aItem );
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 		// Object menu
235cdf0e10cSrcweir 		m_aObjectMenu = new Menu( "Object", true );
236cdf0e10cSrcweir 		aMenuBar.add( m_aObjectMenu );
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 		aItem = new NewObjectMenuItem();
239cdf0e10cSrcweir 		m_aObjectMenu.add( aItem );
240cdf0e10cSrcweir 
241cdf0e10cSrcweir 		aItem = new LoadObjectMenuItem();
242cdf0e10cSrcweir 		m_aObjectMenu.add( aItem );
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 		aItem = new LinkObjectMenuItem();
245cdf0e10cSrcweir 		m_aObjectMenu.add( aItem );
246cdf0e10cSrcweir 
247cdf0e10cSrcweir 		aItem = new ConvertLinkToEmbedMenuItem();
248cdf0e10cSrcweir 		m_aObjectMenu.add( aItem );
249cdf0e10cSrcweir 
250cdf0e10cSrcweir 		// Activation menu
251cdf0e10cSrcweir 		m_aObjectMenu = new Menu( "Activation", true );
252cdf0e10cSrcweir 		aMenuBar.add( m_aObjectMenu );
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 		aItem = new ActivateOutplaceMenuItem();
255cdf0e10cSrcweir 		m_aObjectMenu.add( aItem );
256cdf0e10cSrcweir 
257cdf0e10cSrcweir 		aItem = new ActivateInplaceMenuItem();
258cdf0e10cSrcweir 		m_aObjectMenu.add( aItem );
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 		aItem = new DeactivateMenuItem();
261cdf0e10cSrcweir 		m_aObjectMenu.add( aItem );
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 		m_aNativeView = new NativeView();
264cdf0e10cSrcweir 		m_aNativeView.resize( 800, 600 );
265cdf0e10cSrcweir 		this.add( m_aNativeView );
266cdf0e10cSrcweir 
267cdf0e10cSrcweir 		// Handle mouse clicks in our window.
268cdf0e10cSrcweir //		addMouseListener( this );
269cdf0e10cSrcweir 	}
270cdf0e10cSrcweir 
actionPerformed( ActionEvent evt )271cdf0e10cSrcweir 	public void actionPerformed( ActionEvent evt )
272cdf0e10cSrcweir 	{
273cdf0e10cSrcweir 		synchronized( m_oInHandlerLock )
274cdf0e10cSrcweir 		{
275cdf0e10cSrcweir 			if ( m_bInHandler )
276cdf0e10cSrcweir 				return;
277cdf0e10cSrcweir 			m_bInHandler = true;
278cdf0e10cSrcweir 		}
279cdf0e10cSrcweir 
280cdf0e10cSrcweir 		synchronized( m_oActionsNumberLock )
281cdf0e10cSrcweir 		{
282cdf0e10cSrcweir 			if ( m_aActionsList.size() > 0 )
283cdf0e10cSrcweir 			{
284cdf0e10cSrcweir 				try {
285cdf0e10cSrcweir 					m_xMainThreadExecutor.execute( m_pValuesForExecutor );
286cdf0e10cSrcweir 				}
287cdf0e10cSrcweir 				catch( Exception e )
288cdf0e10cSrcweir 				{
289cdf0e10cSrcweir 					System.out.println( "Exception in actionPerformed() : " + e );
290cdf0e10cSrcweir 				}
291cdf0e10cSrcweir 			}
292cdf0e10cSrcweir 			else
293cdf0e10cSrcweir 			{
294cdf0e10cSrcweir 				synchronized( m_oInHandlerLock )
295cdf0e10cSrcweir 				{
296cdf0e10cSrcweir 					m_bInHandler = false;
297cdf0e10cSrcweir 				}
298cdf0e10cSrcweir 			}
299cdf0e10cSrcweir 		}
300cdf0e10cSrcweir 	}
301cdf0e10cSrcweir 
302cdf0e10cSrcweir 	// XWindowSupplier
getWindow()303cdf0e10cSrcweir 	public XWindow getWindow()
304cdf0e10cSrcweir 	{
305cdf0e10cSrcweir 		return m_xVCLWindow;
306cdf0e10cSrcweir 	}
307cdf0e10cSrcweir 
308cdf0e10cSrcweir 	// XEmbeddedClient
saveObject()309cdf0e10cSrcweir 	public void saveObject()
310cdf0e10cSrcweir 		throws com.sun.star.uno.Exception
311cdf0e10cSrcweir 	{
312cdf0e10cSrcweir 		if ( m_xEmbedObj != null )
313cdf0e10cSrcweir 		{
314cdf0e10cSrcweir 			try {
315cdf0e10cSrcweir 				XEmbedPersist xPersist = (XEmbedPersist)UnoRuntime.queryInterface( XEmbedPersist.class, m_xEmbedObj );
316cdf0e10cSrcweir 				if ( xPersist != null )
317cdf0e10cSrcweir 				{
318cdf0e10cSrcweir 					xPersist.storeOwn();
319cdf0e10cSrcweir 					generateNewImage();
320cdf0e10cSrcweir 				}
321cdf0e10cSrcweir 				else
322cdf0e10cSrcweir 					JOptionPane.showMessageDialog( m_aFrame, "No XEmbedPersist!", "Error:", JOptionPane.ERROR_MESSAGE );
323cdf0e10cSrcweir 			}
324cdf0e10cSrcweir 			catch( Exception e )
325cdf0e10cSrcweir 			{
326cdf0e10cSrcweir 				JOptionPane.showMessageDialog( m_aFrame, e, "Exception in saveObject:", JOptionPane.ERROR_MESSAGE );
327cdf0e10cSrcweir 			}
328cdf0e10cSrcweir 		}
329cdf0e10cSrcweir 
330cdf0e10cSrcweir 		generateNewImage();
331cdf0e10cSrcweir 		repaint();
332cdf0e10cSrcweir 	}
333cdf0e10cSrcweir 
onShowWindow( boolean bVisible )334cdf0e10cSrcweir 	public void onShowWindow( boolean bVisible )
335cdf0e10cSrcweir 	{
336cdf0e10cSrcweir 		// for now nothing to do
337cdf0e10cSrcweir 	}
338cdf0e10cSrcweir 
339cdf0e10cSrcweir 	// XInplaceClient
canInplaceActivate()340cdf0e10cSrcweir 	public boolean canInplaceActivate()
341cdf0e10cSrcweir 	{
342cdf0e10cSrcweir 		return true;
343cdf0e10cSrcweir 	}
344cdf0e10cSrcweir 
onInplaceActivate()345cdf0e10cSrcweir 	public void onInplaceActivate()
346cdf0e10cSrcweir 	{
347cdf0e10cSrcweir 		// TODO
348cdf0e10cSrcweir 		// prepare for inplace activation
349cdf0e10cSrcweir 
350cdf0e10cSrcweir 		// REMOVE
351cdf0e10cSrcweir 		// workaround for CLIPCHILDREN problem
352cdf0e10cSrcweir 		if ( m_aBitmapPainter != null )
353cdf0e10cSrcweir 			m_aBitmapPainter.stopPainting();
354cdf0e10cSrcweir 	}
355cdf0e10cSrcweir 
onUIActivate()356cdf0e10cSrcweir 	public void onUIActivate()
357cdf0e10cSrcweir 	{
358cdf0e10cSrcweir 		// TODO
359cdf0e10cSrcweir 		// prepare for UI activate
360cdf0e10cSrcweir 	}
361cdf0e10cSrcweir 
onInplaceDeactivate()362cdf0e10cSrcweir 	public void onInplaceDeactivate()
363cdf0e10cSrcweir 	{
364cdf0e10cSrcweir 		// TODO
365cdf0e10cSrcweir 		// inplace deactivation is done
366cdf0e10cSrcweir 
367cdf0e10cSrcweir 		// REMOVE
368cdf0e10cSrcweir 		// workaround for CLIPCHILDREN problem
369cdf0e10cSrcweir 		if ( m_aBitmapPainter != null )
370cdf0e10cSrcweir 			m_aBitmapPainter.startPainting();
371cdf0e10cSrcweir 	}
372cdf0e10cSrcweir 
onUIDeactivate()373cdf0e10cSrcweir 	public void onUIDeactivate()
374cdf0e10cSrcweir 	{
375cdf0e10cSrcweir 		// TODO
376cdf0e10cSrcweir 		// prepare for UI deactivate
377cdf0e10cSrcweir 	}
378cdf0e10cSrcweir 
getTopmostWindow()379cdf0e10cSrcweir 	public XIPMainContainerWindow getTopmostWindow()
380cdf0e10cSrcweir 	{
381cdf0e10cSrcweir 		// TODO
382cdf0e10cSrcweir 		// return an implementation of XIPMainContainerWindow
383cdf0e10cSrcweir 		// mainly required for ui activation
384cdf0e10cSrcweir 		// dummy implementation is enough for inplace activation
385cdf0e10cSrcweir 
386cdf0e10cSrcweir 		return null;
387cdf0e10cSrcweir 	}
388cdf0e10cSrcweir 
getDocumentWindow()389cdf0e10cSrcweir 	public XInplaceUIWindow getDocumentWindow()
390cdf0e10cSrcweir 	{
391cdf0e10cSrcweir 		// TODO
392cdf0e10cSrcweir 		// return implementation of XInplaceUIWindow
393cdf0e10cSrcweir 		// mainly required for ui activation
394cdf0e10cSrcweir 		// dummy implementation is enough for inplace activation
395cdf0e10cSrcweir 
396cdf0e10cSrcweir 		return null;
397cdf0e10cSrcweir 	}
398cdf0e10cSrcweir 
getPosRect()399cdf0e10cSrcweir 	public com.sun.star.awt.Rectangle getPosRect()
400cdf0e10cSrcweir 	{
401cdf0e10cSrcweir 		// provide position rectangle to the object
402cdf0e10cSrcweir 		try {
403cdf0e10cSrcweir 			// here object bitmap and scaling factor hold the size
404cdf0e10cSrcweir 			com.sun.star.awt.Size aBitmapSize = m_xBitmap.getSize();
405cdf0e10cSrcweir 			com.sun.star.awt.Size aVisSize = new com.sun.star.awt.Size(
406cdf0e10cSrcweir 											(int)( aBitmapSize.Width * m_nXScaling ),
407cdf0e10cSrcweir 											(int)( aBitmapSize.Height * m_nYScaling ) );
408cdf0e10cSrcweir 			return new com.sun.star.awt.Rectangle( 10, 10, aVisSize.Width, aVisSize.Height );
409cdf0e10cSrcweir 		}
410cdf0e10cSrcweir 		catch( Exception e )
411cdf0e10cSrcweir 		{
412cdf0e10cSrcweir 			System.out.println( "Position rectangle generation failed!" );
413cdf0e10cSrcweir 		}
414cdf0e10cSrcweir 
415cdf0e10cSrcweir 		return new com.sun.star.awt.Rectangle( 10, 10, 110, 110 );
416cdf0e10cSrcweir 	}
417cdf0e10cSrcweir 
getClipRect()418cdf0e10cSrcweir 	public com.sun.star.awt.Rectangle getClipRect()
419cdf0e10cSrcweir 	{
420cdf0e10cSrcweir 		// provide clip rectangle to the object
421cdf0e10cSrcweir 		// in this application position and clip rectangles are the same
422cdf0e10cSrcweir 
423cdf0e10cSrcweir 		try {
424cdf0e10cSrcweir 			// here object bitmap and scaling factor hold the size
425cdf0e10cSrcweir 			com.sun.star.awt.Size aBitmapSize = m_xBitmap.getSize();
426cdf0e10cSrcweir 			com.sun.star.awt.Size aVisSize = new com.sun.star.awt.Size(
427cdf0e10cSrcweir 											(int)( aBitmapSize.Width * m_nXScaling ),
428cdf0e10cSrcweir 											(int)( aBitmapSize.Height * m_nYScaling ) );
429cdf0e10cSrcweir 			return new com.sun.star.awt.Rectangle( 10, 10, aVisSize.Width, aVisSize.Height );
430cdf0e10cSrcweir 		}
431cdf0e10cSrcweir 		catch( Exception e )
432cdf0e10cSrcweir 		{
433cdf0e10cSrcweir 			System.out.println( "Clip rectangle generation failed!" );
434cdf0e10cSrcweir 		}
435cdf0e10cSrcweir 
436cdf0e10cSrcweir 		return new com.sun.star.awt.Rectangle( 10, 10, 110, 110 );
437cdf0e10cSrcweir 	}
438cdf0e10cSrcweir 
translateAccelerators( com.sun.star.awt.KeyEvent[] aKeys )439cdf0e10cSrcweir 	public void translateAccelerators( com.sun.star.awt.KeyEvent[] aKeys )
440cdf0e10cSrcweir 	{
441cdf0e10cSrcweir 		// TODO
442cdf0e10cSrcweir 		// an accelerator table for object
443cdf0e10cSrcweir 		// ui activation related
444cdf0e10cSrcweir 	}
445cdf0e10cSrcweir 
scrollObj( com.sun.star.awt.Size aOffset )446cdf0e10cSrcweir 	public void scrollObj( com.sun.star.awt.Size aOffset )
447cdf0e10cSrcweir 	{
448cdf0e10cSrcweir 		// TODO
449cdf0e10cSrcweir 		// scrolls the object to a specified offset
450cdf0e10cSrcweir 		// not mandatory for the testing application :)
451cdf0e10cSrcweir 	}
452cdf0e10cSrcweir 
onPosRectChange( com.sun.star.awt.Rectangle aPosRect )453cdf0e10cSrcweir 	public void onPosRectChange( com.sun.star.awt.Rectangle aPosRect )
454cdf0e10cSrcweir 	{
455cdf0e10cSrcweir 		// object asks to change the position
456cdf0e10cSrcweir 		if ( m_xEmbedObj != null )
457cdf0e10cSrcweir 		{
458cdf0e10cSrcweir 			try {
459cdf0e10cSrcweir 				int nState = m_xEmbedObj.getCurrentState();
460cdf0e10cSrcweir 				// such a position change make sence only when object is
461cdf0e10cSrcweir 				// either inplace or ui active
462cdf0e10cSrcweir 				if ( nState == EmbedStates.EMBED_INPLACE_ACTIVE
463cdf0e10cSrcweir 			  	|| nState == EmbedStates.EMBED_UI_ACTIVE )
464cdf0e10cSrcweir 			 	{
465cdf0e10cSrcweir 					XInplaceObject xInplObj = (XInplaceObject)UnoRuntime.queryInterface( XInplaceObject.class, m_xEmbedObj );
466cdf0e10cSrcweir 			 		if ( xInplObj != null )
467cdf0e10cSrcweir 					{
468cdf0e10cSrcweir 						xInplObj.setObjectRects( aPosRect, aPosRect ); // show the whole object
469cdf0e10cSrcweir 						if ( m_aBitmapPainter != null )
470cdf0e10cSrcweir 							m_aBitmapPainter.setRect( aPosRect );
471cdf0e10cSrcweir 					}
472cdf0e10cSrcweir 					else
473cdf0e10cSrcweir 			 			System.out.println( "Why object that does not support inplace activation behave like inplace object?!" );
474cdf0e10cSrcweir 			 	}
475cdf0e10cSrcweir 			 	else
476cdf0e10cSrcweir 			 		System.out.println( "The object is not active but asks to change visual area!" );
477cdf0e10cSrcweir 			} catch( Exception e )
478cdf0e10cSrcweir 			{
479cdf0e10cSrcweir 			 	System.out.println( "Exception is thrown in onPosRectChange: " + e );
480cdf0e10cSrcweir 			}
481cdf0e10cSrcweir 		}
482cdf0e10cSrcweir 		else
483cdf0e10cSrcweir 		 	System.out.println( "Who asks to change visual area?!!" );
484cdf0e10cSrcweir 	}
485cdf0e10cSrcweir 
486cdf0e10cSrcweir 	// XJob
execute( NamedValue[] pValues )487cdf0e10cSrcweir 	public Object execute( NamedValue[] pValues )
488cdf0e10cSrcweir 	{
489cdf0e10cSrcweir 		for( int nInd = 0; nInd < m_aActionsList.size(); nInd++ )
490cdf0e10cSrcweir 		{
491cdf0e10cSrcweir 			ActionObject aAction = ( ActionObject ) m_aActionsList.get( nInd );
492cdf0e10cSrcweir 			if ( aAction != null )
493cdf0e10cSrcweir 			{
494cdf0e10cSrcweir 				if ( aAction.m_nID == DESTROY )
495cdf0e10cSrcweir 				{
496cdf0e10cSrcweir 					// free all resources
497cdf0e10cSrcweir 					clearObjectAndStorage();
498cdf0e10cSrcweir 					m_bDestroyed = true;
499cdf0e10cSrcweir 				}
500cdf0e10cSrcweir 				else if ( aAction.m_nID == ACTIVATE_OUTPLACE )
501cdf0e10cSrcweir 				{
502cdf0e10cSrcweir 					// activate object if exists and not active
503cdf0e10cSrcweir 					if ( m_xEmbedObj != null )
504cdf0e10cSrcweir 					{
505cdf0e10cSrcweir 						try {
506cdf0e10cSrcweir 							m_xEmbedObj.changeState( EmbedStates.EMBED_ACTIVE );
507cdf0e10cSrcweir 						}
508cdf0e10cSrcweir 						catch( Exception ex )
509cdf0e10cSrcweir 						{
510cdf0e10cSrcweir 							System.out.println( "Exception on mouse click" + ex );
511cdf0e10cSrcweir 						}
512cdf0e10cSrcweir 					}
513cdf0e10cSrcweir 				}
514cdf0e10cSrcweir 				else if ( aAction.m_nID == NEW_DOCUMENT )
515cdf0e10cSrcweir 				{
516cdf0e10cSrcweir 					// clear everything
517cdf0e10cSrcweir 					clearObjectAndStorage();
518cdf0e10cSrcweir 
519cdf0e10cSrcweir 					repaint();
520cdf0e10cSrcweir 				}
521cdf0e10cSrcweir 				else if ( aAction.m_nID == SAVE_AS )
522cdf0e10cSrcweir 				{
523cdf0e10cSrcweir 					// open SaveAs dialog and store
524cdf0e10cSrcweir 
525cdf0e10cSrcweir 					if ( m_xStorage != null && m_xEmbedObj != null )
526cdf0e10cSrcweir 					{
527cdf0e10cSrcweir 						try {
528cdf0e10cSrcweir 							/*
529cdf0e10cSrcweir 								if ( m_bLinkObj )
530cdf0e10cSrcweir 									storeLinkAsFileURI( aFileURI );
531cdf0e10cSrcweir 								else
532cdf0e10cSrcweir 							*/
533cdf0e10cSrcweir 							saveObjectAsFileURI( aAction.m_sParam );
534cdf0e10cSrcweir 						}
535cdf0e10cSrcweir 						catch( Exception ex )
536cdf0e10cSrcweir 						{
537cdf0e10cSrcweir 							System.out.println( "Exception in SaveAsMenuItem: " + ex );
538cdf0e10cSrcweir 						}
539cdf0e10cSrcweir 					}
540cdf0e10cSrcweir 				}
541cdf0e10cSrcweir 				else if ( aAction.m_nID == OPEN_FILE )
542cdf0e10cSrcweir 				{
543cdf0e10cSrcweir 					// clear everything
544cdf0e10cSrcweir 					clearObjectAndStorage();
545cdf0e10cSrcweir 
546cdf0e10cSrcweir 					// load from specified file
547cdf0e10cSrcweir 					loadFileURI( aAction.m_sParam );
548cdf0e10cSrcweir 
549cdf0e10cSrcweir 					if ( m_xEmbedObj != null )
550cdf0e10cSrcweir 					{
551cdf0e10cSrcweir 						try {
552cdf0e10cSrcweir 							m_xEmbedObj.setClientSite( this );
553cdf0e10cSrcweir 						}
554cdf0e10cSrcweir 						catch( Exception ex )
555cdf0e10cSrcweir 						{
556cdf0e10cSrcweir 							System.out.println( "Exception in OpenFileMenuItem: " + ex );
557cdf0e10cSrcweir 						}
558cdf0e10cSrcweir 					}
559cdf0e10cSrcweir 
560cdf0e10cSrcweir 					generateNewImage();
561cdf0e10cSrcweir 					repaint();
562cdf0e10cSrcweir 				}
563cdf0e10cSrcweir 				else if ( aAction.m_nID == SAVE )
564cdf0e10cSrcweir 				{
565cdf0e10cSrcweir 					if ( m_xStorage != null && m_xEmbedObj != null )
566cdf0e10cSrcweir 					{
567cdf0e10cSrcweir 						// if has persistance store there
568cdf0e10cSrcweir 						// if not it is and error, SaveAs had to be used
569cdf0e10cSrcweir 
570cdf0e10cSrcweir 						if ( m_bOwnFile )
571cdf0e10cSrcweir 						{
572cdf0e10cSrcweir 							if ( m_xStorage != null )
573cdf0e10cSrcweir 							{
574cdf0e10cSrcweir 								try {
575cdf0e10cSrcweir 									saveObject();
576cdf0e10cSrcweir 
577cdf0e10cSrcweir 									if ( m_bLinkObj )
578cdf0e10cSrcweir 										storeLinkToStorage();
579cdf0e10cSrcweir 
580cdf0e10cSrcweir 									XTransactedObject xTransact = (XTransactedObject)UnoRuntime.queryInterface( XTransactedObject.class,
581cdf0e10cSrcweir 																												m_xStorage );
582cdf0e10cSrcweir 									if ( xTransact != null )
583cdf0e10cSrcweir 										xTransact.commit();
584cdf0e10cSrcweir 								}
585cdf0e10cSrcweir 								catch( Exception ex )
586cdf0e10cSrcweir 								{
587cdf0e10cSrcweir 									System.out.println( "Exception during save operation in SaveMenuItem:" + ex );
588cdf0e10cSrcweir 								}
589cdf0e10cSrcweir 							}
590cdf0e10cSrcweir 							else
591cdf0e10cSrcweir 							{
592cdf0e10cSrcweir 								System.out.println( "No storage for owned file!" );
593cdf0e10cSrcweir 							}
594cdf0e10cSrcweir 						}
595cdf0e10cSrcweir 						else
596cdf0e10cSrcweir 						{
597cdf0e10cSrcweir 							System.out.println( "No owned file!" );
598cdf0e10cSrcweir 						}
599cdf0e10cSrcweir 					}
600cdf0e10cSrcweir 				}
601cdf0e10cSrcweir 				else if ( aAction.m_nID == NEW_OBJECT )
602cdf0e10cSrcweir 				{
603cdf0e10cSrcweir 					// remove current object an init a new one
604cdf0e10cSrcweir 					clearObjectAndStorage();
605cdf0e10cSrcweir 
606cdf0e10cSrcweir 					if ( aAction.m_sParam != null )
607cdf0e10cSrcweir 					{
608cdf0e10cSrcweir 						m_xStorage = createTempStorage();
609cdf0e10cSrcweir 
610cdf0e10cSrcweir 						if ( m_xStorage != null )
611cdf0e10cSrcweir 							m_xEmbedObj = createEmbedObject( aAction.m_sParam );
612cdf0e10cSrcweir 						else
613cdf0e10cSrcweir 							System.out.println( "Can't create temporary storage!" );
614cdf0e10cSrcweir 
615cdf0e10cSrcweir 						if ( m_xEmbedObj != null )
616cdf0e10cSrcweir 						{
617cdf0e10cSrcweir 							try {
618cdf0e10cSrcweir 								m_xEmbedObj.setClientSite( this );
619cdf0e10cSrcweir 							}
620cdf0e10cSrcweir 							catch( Exception ex )
621cdf0e10cSrcweir 							{
622cdf0e10cSrcweir 								System.out.println( "Exception in NewObjectMenuItem:" + ex );
623cdf0e10cSrcweir 							}
624cdf0e10cSrcweir 						}
625cdf0e10cSrcweir 					}
626cdf0e10cSrcweir 
627cdf0e10cSrcweir 					generateNewImage();
628cdf0e10cSrcweir 					repaint();
629cdf0e10cSrcweir 				}
630cdf0e10cSrcweir 				else if ( aAction.m_nID == OBJECT_FROM_FILE )
631cdf0e10cSrcweir 				{
632cdf0e10cSrcweir 					// first remove current object
633cdf0e10cSrcweir 					clearObjectAndStorage();
634cdf0e10cSrcweir 
635cdf0e10cSrcweir 					// create object from specified file
636cdf0e10cSrcweir 					m_xStorage = createTempStorage();
637cdf0e10cSrcweir 
638cdf0e10cSrcweir 					if ( m_xStorage != null )
639cdf0e10cSrcweir 						m_xEmbedObj = loadEmbedObject( aAction.m_sParam );
640cdf0e10cSrcweir 
641cdf0e10cSrcweir 					if ( m_xEmbedObj != null )
642cdf0e10cSrcweir 					{
643cdf0e10cSrcweir 						try {
644cdf0e10cSrcweir 							m_xEmbedObj.setClientSite( this );
645cdf0e10cSrcweir 						}
646cdf0e10cSrcweir 						catch( Exception ex )
647cdf0e10cSrcweir 						{
648cdf0e10cSrcweir 							System.out.println( "Exception in LoadObjectMenuItem: " + ex );
649cdf0e10cSrcweir 						}
650cdf0e10cSrcweir 					}
651cdf0e10cSrcweir 
652cdf0e10cSrcweir 					generateNewImage();
653cdf0e10cSrcweir 					repaint();
654cdf0e10cSrcweir 				}
655cdf0e10cSrcweir 				else if ( aAction.m_nID == LINK_FROM_FILE )
656cdf0e10cSrcweir 				{
657cdf0e10cSrcweir 					// first remove current object
658cdf0e10cSrcweir 					clearObjectAndStorage();
659cdf0e10cSrcweir 
660cdf0e10cSrcweir 					m_xStorage = createTempStorage();
661cdf0e10cSrcweir 
662cdf0e10cSrcweir 					// create object from specified file
663cdf0e10cSrcweir 					m_xEmbedObj = createLinkObject( aAction.m_sParam );
664cdf0e10cSrcweir 
665cdf0e10cSrcweir 					if ( m_xEmbedObj != null )
666cdf0e10cSrcweir 					{
667cdf0e10cSrcweir 						m_aLinkURI = aAction.m_sParam;
668cdf0e10cSrcweir 						m_bLinkObj = true;
669cdf0e10cSrcweir 
670cdf0e10cSrcweir 						try {
671cdf0e10cSrcweir 							m_xEmbedObj.setClientSite( this );
672cdf0e10cSrcweir 						}
673cdf0e10cSrcweir 						catch( Exception ex )
674cdf0e10cSrcweir 						{
675cdf0e10cSrcweir 							System.out.println( "Exception in LinkObjectMenuItem:" + ex );
676cdf0e10cSrcweir 						}
677cdf0e10cSrcweir 					}
678cdf0e10cSrcweir 
679cdf0e10cSrcweir 					generateNewImage();
680cdf0e10cSrcweir 					repaint();
681cdf0e10cSrcweir 				}
682cdf0e10cSrcweir 				else if ( aAction.m_nID == CONVERT_LINK_TO_OBJECT )
683cdf0e10cSrcweir 				{
684cdf0e10cSrcweir 					if ( !m_bLinkObj )
685cdf0e10cSrcweir 					{
686cdf0e10cSrcweir 						System.out.println( "The object is not a link!" );
687cdf0e10cSrcweir 						continue;
688cdf0e10cSrcweir 					}
689cdf0e10cSrcweir 
690cdf0e10cSrcweir 					if ( m_xEmbedObj != null )
691cdf0e10cSrcweir 					{
692cdf0e10cSrcweir 						if ( m_xStorage != null )
693cdf0e10cSrcweir 						{
694cdf0e10cSrcweir 							try {
695cdf0e10cSrcweir 								XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface( XNameAccess.class,
696cdf0e10cSrcweir 																								m_xStorage );
697cdf0e10cSrcweir 								if ( xNameAccess != null && xNameAccess.hasByName( "LinkName" ) )
698cdf0e10cSrcweir 									m_xStorage.removeElement( "LinkName" );
699cdf0e10cSrcweir 
700cdf0e10cSrcweir 								XLinkageSupport xLinkage = (XLinkageSupport)UnoRuntime.queryInterface( XLinkageSupport.class,
701cdf0e10cSrcweir 																										m_xEmbedObj );
702cdf0e10cSrcweir 								if ( xLinkage != null )
703cdf0e10cSrcweir 								{
704cdf0e10cSrcweir 									xLinkage.breakLink( m_xStorage, "EmbedSub" );
705cdf0e10cSrcweir 									m_bLinkObj = false;
706cdf0e10cSrcweir 									m_aLinkURI = null;
707cdf0e10cSrcweir 								}
708cdf0e10cSrcweir 								else
709cdf0e10cSrcweir 									System.out.println( "No XLinkageSupport in ConvertLink... !" );
710cdf0e10cSrcweir 							}
711cdf0e10cSrcweir 							catch( Exception e1 )
712cdf0e10cSrcweir 							{
713cdf0e10cSrcweir 								System.out.println( "Exception in ConvertLinkToEmbed:try 1 :" + e1 );
714cdf0e10cSrcweir 							}
715cdf0e10cSrcweir 						}
716cdf0e10cSrcweir 					}
717cdf0e10cSrcweir 				}
718cdf0e10cSrcweir 				else if ( aAction.m_nID == ACTIVATE_INPLACE )
719cdf0e10cSrcweir 				{
720cdf0e10cSrcweir 					// activate object
721cdf0e10cSrcweir 					if ( m_xEmbedObj != null )
722cdf0e10cSrcweir 					{
723cdf0e10cSrcweir 						// in general it is better to check acceptable states
724cdf0e10cSrcweir 						try {
725cdf0e10cSrcweir 							m_xEmbedObj.changeState( EmbedStates.EMBED_INPLACE_ACTIVE );
726cdf0e10cSrcweir 						}
727cdf0e10cSrcweir 						catch( Exception ex )
728cdf0e10cSrcweir 						{
729cdf0e10cSrcweir 							System.out.println( "Exception on inplace activation " + ex );
730cdf0e10cSrcweir 						}
731cdf0e10cSrcweir 					}
732cdf0e10cSrcweir 				}
733cdf0e10cSrcweir 				else if ( aAction.m_nID == DEACTIVATE )
734cdf0e10cSrcweir 				{
735cdf0e10cSrcweir 					// activate object
736cdf0e10cSrcweir 
737cdf0e10cSrcweir 					if ( m_xEmbedObj != null )
738cdf0e10cSrcweir 					{
739cdf0e10cSrcweir 						int nOldState = -1;
740cdf0e10cSrcweir 						try {
741cdf0e10cSrcweir 							nOldState = m_xEmbedObj.getCurrentState();
742cdf0e10cSrcweir 						} catch( Exception e )
743cdf0e10cSrcweir 						{}
744cdf0e10cSrcweir 
745cdf0e10cSrcweir 						if ( nOldState == EmbedStates.EMBED_ACTIVE
746cdf0e10cSrcweir 						  || nOldState == EmbedStates.EMBED_INPLACE_ACTIVE
747cdf0e10cSrcweir 						  || nOldState == EmbedStates.EMBED_UI_ACTIVE )
748cdf0e10cSrcweir 						{
749cdf0e10cSrcweir 							try {
750cdf0e10cSrcweir 								m_xEmbedObj.changeState( EmbedStates.EMBED_RUNNING );
751cdf0e10cSrcweir 							}
752cdf0e10cSrcweir 							catch( Exception ex )
753cdf0e10cSrcweir 							{
754cdf0e10cSrcweir 								System.out.println( "Exception on inplace activation " + ex );
755cdf0e10cSrcweir 							}
756cdf0e10cSrcweir 						}
757cdf0e10cSrcweir 						else
758cdf0e10cSrcweir 						{
759cdf0e10cSrcweir 							System.out.println( "Deactivation of nonactive object!" );
760cdf0e10cSrcweir 						}
761cdf0e10cSrcweir 					}
762cdf0e10cSrcweir 				}
763cdf0e10cSrcweir 				else
764cdf0e10cSrcweir 				{
765cdf0e10cSrcweir 					System.out.println( "Unknoun action is requested: " + aAction.m_nID + "\n" );
766cdf0e10cSrcweir 				}
767cdf0e10cSrcweir 			}
768cdf0e10cSrcweir 		}
769cdf0e10cSrcweir 
770cdf0e10cSrcweir 		m_aActionsList.clear();
771cdf0e10cSrcweir 
772cdf0e10cSrcweir 		synchronized( m_oInHandlerLock )
773cdf0e10cSrcweir 		{
774cdf0e10cSrcweir 			m_bInHandler = false;
775cdf0e10cSrcweir 		}
776cdf0e10cSrcweir 
777cdf0e10cSrcweir 		return Any.VOID;
778cdf0e10cSrcweir 	}
779cdf0e10cSrcweir 
actionRegister( byte nActionID, String sParam )780cdf0e10cSrcweir 	public void actionRegister( byte nActionID, String sParam )
781cdf0e10cSrcweir 	{
782cdf0e10cSrcweir 		synchronized( m_oActionsNumberLock )
783cdf0e10cSrcweir 		{
784cdf0e10cSrcweir 			int nSize = m_aActionsList.size();
785cdf0e10cSrcweir 			if ( nSize < 199 )
786cdf0e10cSrcweir 			{
787cdf0e10cSrcweir 				if ( nSize == 0 )
788cdf0e10cSrcweir 					m_aActionsList.add( new ActionObject( nActionID, sParam ) );
789cdf0e10cSrcweir 				else
790cdf0e10cSrcweir 				{
791cdf0e10cSrcweir 					ActionObject aAction = ( ActionObject ) m_aActionsList.get( nSize - 1 );
792cdf0e10cSrcweir 					if ( aAction != null && aAction.m_nID != DESTROY )
793cdf0e10cSrcweir 						m_aActionsList.add( new ActionObject( nActionID, sParam ) );
794cdf0e10cSrcweir 				}
795cdf0e10cSrcweir 			}
796cdf0e10cSrcweir 		}
797cdf0e10cSrcweir 	}
798cdf0e10cSrcweir 
SaveAsOperation()799cdf0e10cSrcweir 	public void SaveAsOperation()
800cdf0e10cSrcweir 	{
801cdf0e10cSrcweir 		if ( m_xStorage != null && m_xEmbedObj != null )
802cdf0e10cSrcweir 		{
803cdf0e10cSrcweir 			FileDialog aFileDialog = new FileDialog( m_aFrame, "SaveAs", FileDialog.SAVE );
804cdf0e10cSrcweir 			aFileDialog.show();
805cdf0e10cSrcweir 			if ( aFileDialog.getFile() != null )
806cdf0e10cSrcweir 			{
807cdf0e10cSrcweir 				String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
808cdf0e10cSrcweir 				File aFile = new File( aFileName );
809cdf0e10cSrcweir 				if ( aFile != null )
810cdf0e10cSrcweir 				{
811cdf0e10cSrcweir 					// create object from specified file
812cdf0e10cSrcweir 					String aFileURI = getValidURL( aFile.toURI().toASCIIString() );
813cdf0e10cSrcweir 					actionRegister( SAVE_AS, aFileURI );
814cdf0e10cSrcweir 				}
815cdf0e10cSrcweir 			}
816cdf0e10cSrcweir 		}
817cdf0e10cSrcweir 		else
818cdf0e10cSrcweir 			JOptionPane.showMessageDialog( m_aFrame, "No document is embedded!", "Error:", JOptionPane.ERROR_MESSAGE );
819cdf0e10cSrcweir 
820cdf0e10cSrcweir 	}
821cdf0e10cSrcweir 
destroy()822cdf0e10cSrcweir 	public void destroy()
823cdf0e10cSrcweir 	{
824cdf0e10cSrcweir 		// redirect the call through the timer and call super.destroy();
825cdf0e10cSrcweir 		actionRegister( DESTROY, null );
826cdf0e10cSrcweir 
827cdf0e10cSrcweir 		for ( int i = 0; i < 3 && !m_bDestroyed; i++ )
828cdf0e10cSrcweir 		{
829cdf0e10cSrcweir 			try {
830cdf0e10cSrcweir 				Thread.sleep( 200 );
831cdf0e10cSrcweir 			} catch ( Exception e )
832cdf0e10cSrcweir 			{}
833cdf0e10cSrcweir 		}
834cdf0e10cSrcweir 
835cdf0e10cSrcweir 		if ( !m_bDestroyed )
836cdf0e10cSrcweir 			System.out.println( "The object application can not exit correctly!" );
837cdf0e10cSrcweir 
838cdf0e10cSrcweir 		m_aTimer.stop();
839cdf0e10cSrcweir 
840cdf0e10cSrcweir 		super.destroy();
841cdf0e10cSrcweir 	}
842cdf0e10cSrcweir 
update( Graphics g )843cdf0e10cSrcweir 	public void update( Graphics g )
844cdf0e10cSrcweir 	{
845cdf0e10cSrcweir 		paint( g );
846cdf0e10cSrcweir 	}
847cdf0e10cSrcweir 
paint( Graphics g )848cdf0e10cSrcweir 	public void paint( Graphics g )
849cdf0e10cSrcweir 	{
850cdf0e10cSrcweir 		super.paint( g );
851cdf0e10cSrcweir 
852cdf0e10cSrcweir 		// m_aNativeView.paint( g );
853cdf0e10cSrcweir 
854cdf0e10cSrcweir 		createVclWindow();
855cdf0e10cSrcweir 	}
856cdf0e10cSrcweir 
createVclWindow()857cdf0e10cSrcweir 	public void createVclWindow()
858cdf0e10cSrcweir 	{
859cdf0e10cSrcweir 		synchronized( m_oImageLock )
860cdf0e10cSrcweir 		{
861cdf0e10cSrcweir 			if ( m_xVCLWindow == null && m_xServiceFactory != null && m_xEmbedObj != null && m_xBitmap != null )
862cdf0e10cSrcweir 			{
863cdf0e10cSrcweir 				java.awt.Rectangle aBounds = getBounds();
864cdf0e10cSrcweir 				m_xVCLWindow = WindowHelper.createWindow( m_xServiceFactory, m_aNativeView, aBounds );
865cdf0e10cSrcweir 				m_xVCLWindow.setVisible( true );
866cdf0e10cSrcweir 
867cdf0e10cSrcweir 				com.sun.star.awt.Size aBitmapSize = new com.sun.star.awt.Size( 200, 100 );
868cdf0e10cSrcweir 
869cdf0e10cSrcweir 				XVisualObject xVisObj = (XVisualObject)UnoRuntime.queryInterface( XVisualObject.class, m_xEmbedObj );
870cdf0e10cSrcweir 				try {
871cdf0e10cSrcweir 					com.sun.star.awt.Size aVisSize = xVisObj.getVisAreaSize( Aspects.MSASPECT_CONTENT );
872cdf0e10cSrcweir 					m_nXPixelSize = aVisSize.Width / aBitmapSize.Width;
873cdf0e10cSrcweir 					m_nYPixelSize = aVisSize.Height / aBitmapSize.Height;
874cdf0e10cSrcweir 				}
875cdf0e10cSrcweir 				catch( Exception e )
876cdf0e10cSrcweir 				{
877cdf0e10cSrcweir 				}
878cdf0e10cSrcweir 
879cdf0e10cSrcweir 				if ( m_xBitmap != null )
880cdf0e10cSrcweir 				 	aBitmapSize = m_xBitmap.getSize();
881cdf0e10cSrcweir 
882cdf0e10cSrcweir 				System.out.println( "The visual area is Width = " + aBitmapSize.Width + "; Height = " + aBitmapSize.Height );
883cdf0e10cSrcweir 
884cdf0e10cSrcweir 				com.sun.star.awt.Rectangle aRect = new com.sun.star.awt.Rectangle(
885cdf0e10cSrcweir 														10,
886cdf0e10cSrcweir 														10,
887cdf0e10cSrcweir 														Math.min( (int)aBounds.getWidth() - 20, aBitmapSize.Width ),
888cdf0e10cSrcweir 														Math.min( (int)aBounds.getHeight() - 20, aBitmapSize.Height ) );
889cdf0e10cSrcweir 
890cdf0e10cSrcweir 				m_aBitmapPainter = new BitmapPainter( m_xMainThreadExecutor, m_xVCLWindow, m_xBitmap, aRect );
891cdf0e10cSrcweir 			}
892cdf0e10cSrcweir 		}
893cdf0e10cSrcweir 	}
894cdf0e10cSrcweir 
generateNewImage()895cdf0e10cSrcweir 	public void generateNewImage()
896cdf0e10cSrcweir 	{
897cdf0e10cSrcweir 		if ( m_xEmbedObj != null )
898cdf0e10cSrcweir 		{
899cdf0e10cSrcweir 			try {
900cdf0e10cSrcweir 				int nOldState = m_xEmbedObj.getCurrentState();
901cdf0e10cSrcweir 				int nState = nOldState;
902cdf0e10cSrcweir 				if ( nOldState == EmbedStates.EMBED_LOADED )
903cdf0e10cSrcweir 				{
904cdf0e10cSrcweir 					m_xEmbedObj.changeState( EmbedStates.EMBED_RUNNING );
905cdf0e10cSrcweir 					nState = EmbedStates.EMBED_RUNNING;
906cdf0e10cSrcweir 				}
907cdf0e10cSrcweir 
908cdf0e10cSrcweir 				if ( nState == EmbedStates.EMBED_UI_ACTIVE || nState == EmbedStates.EMBED_INPLACE_ACTIVE
909cdf0e10cSrcweir 				  || nState == EmbedStates.EMBED_ACTIVE || nState == EmbedStates.EMBED_RUNNING )
910cdf0e10cSrcweir 				{
911cdf0e10cSrcweir 					XComponentSupplier xCompProv = (XComponentSupplier)UnoRuntime.queryInterface(
912cdf0e10cSrcweir 																					XComponentSupplier.class,
913cdf0e10cSrcweir 																					m_xEmbedObj );
914cdf0e10cSrcweir 					if ( xCompProv != null )
915cdf0e10cSrcweir 					{
916cdf0e10cSrcweir 						XCloseable xCloseable = xCompProv.getComponent();
917cdf0e10cSrcweir 						XTransferable xTransfer = (XTransferable)UnoRuntime.queryInterface(
918cdf0e10cSrcweir 																					XTransferable.class,
919cdf0e10cSrcweir 																					xCloseable );
920cdf0e10cSrcweir 						if ( xTransfer != null )
921cdf0e10cSrcweir 						{
922cdf0e10cSrcweir 							DataFlavor aFlavor = new DataFlavor();
923cdf0e10cSrcweir 							aFlavor.MimeType = "application/x-openoffice;windows_formatname=\"Bitmap\"";
924cdf0e10cSrcweir 							aFlavor.HumanPresentableName = "Bitmap";
925cdf0e10cSrcweir 							aFlavor.DataType = new Type( byte[].class );
926cdf0e10cSrcweir 
927cdf0e10cSrcweir 							Object aAny = xTransfer.getTransferData( aFlavor );
928cdf0e10cSrcweir 							if ( aAny != null && AnyConverter.isArray( aAny ) )
929cdf0e10cSrcweir 							{
930cdf0e10cSrcweir 								synchronized( m_oImageLock )
931cdf0e10cSrcweir 								{
932cdf0e10cSrcweir 									m_xBitmap = WindowHelper.getVCLBitmapFromBytes( m_xServiceFactory, aAny );
933cdf0e10cSrcweir 									if ( m_aBitmapPainter != null )
934cdf0e10cSrcweir 									{
935cdf0e10cSrcweir 										m_aBitmapPainter.setBitmap( m_xBitmap );
936cdf0e10cSrcweir 
937cdf0e10cSrcweir 										if ( m_xBitmap != null )
938cdf0e10cSrcweir 										{
939cdf0e10cSrcweir 											try {
940cdf0e10cSrcweir 												com.sun.star.awt.Size aBitmapSize = m_xBitmap.getSize();
941cdf0e10cSrcweir 												com.sun.star.awt.Size aVisSize = new com.sun.star.awt.Size(
942cdf0e10cSrcweir 																				(int)( aBitmapSize.Width * m_nXScaling ),
943cdf0e10cSrcweir 																				(int)( aBitmapSize.Height * m_nYScaling ) );
944cdf0e10cSrcweir 												m_aBitmapPainter.setSize( aVisSize );
945cdf0e10cSrcweir 											}
946cdf0e10cSrcweir 											catch( Exception e )
947cdf0e10cSrcweir 											{
948cdf0e10cSrcweir 											}
949cdf0e10cSrcweir 										}
950cdf0e10cSrcweir 									}
951cdf0e10cSrcweir 								}
952cdf0e10cSrcweir 							}
953cdf0e10cSrcweir 						}
954cdf0e10cSrcweir 						else
955cdf0e10cSrcweir 							System.out.println( "paint() : can not get XTransferable for the component!\n" );
956cdf0e10cSrcweir 					}
957cdf0e10cSrcweir 					else
958cdf0e10cSrcweir 						System.out.println( "paint() : XComponentSupplier is not implemented!\n" );
959cdf0e10cSrcweir 				}
960cdf0e10cSrcweir 			}
961cdf0e10cSrcweir 			catch( com.sun.star.uno.Exception e )
962cdf0e10cSrcweir 			{
963cdf0e10cSrcweir 				// dialogs should not be used in paint()
964cdf0e10cSrcweir 				System.out.println( "Exception in paint(): " + e );
965cdf0e10cSrcweir 			}
966cdf0e10cSrcweir 		}
967cdf0e10cSrcweir 	}
968cdf0e10cSrcweir 
mouseClicked( MouseEvent e )969cdf0e10cSrcweir 	public void mouseClicked( MouseEvent e )
970cdf0e10cSrcweir 	{
971cdf0e10cSrcweir 		if( e.getModifiers() == InputEvent.BUTTON1_MASK )
972cdf0e10cSrcweir 		{
973cdf0e10cSrcweir 			actionRegister( ACTIVATE_OUTPLACE, null );
974cdf0e10cSrcweir 		}
975cdf0e10cSrcweir 	}
976cdf0e10cSrcweir 
mousePressed( MouseEvent e )977cdf0e10cSrcweir 	public void mousePressed( MouseEvent e ){};
mouseEntered( MouseEvent e )978cdf0e10cSrcweir 	public void mouseEntered( MouseEvent e ){};
mouseExited( MouseEvent e )979cdf0e10cSrcweir 	public void mouseExited( MouseEvent e ){};
mouseReleased( MouseEvent e )980cdf0e10cSrcweir 	public void mouseReleased( MouseEvent e ){};
981cdf0e10cSrcweir 
982cdf0e10cSrcweir 	// classes
983cdf0e10cSrcweir 	class NewMenuItem extends MenuItem implements ActionListener // Menu New
984cdf0e10cSrcweir 	{
NewMenuItem()985cdf0e10cSrcweir 		public NewMenuItem()
986cdf0e10cSrcweir 		{
987cdf0e10cSrcweir 			super( "New", new MenuShortcut( KeyEvent.VK_A ));
988cdf0e10cSrcweir 			addActionListener( this );
989cdf0e10cSrcweir 		}
990cdf0e10cSrcweir 
actionPerformed( ActionEvent e )991cdf0e10cSrcweir 		public void actionPerformed( ActionEvent e )
992cdf0e10cSrcweir 		{
993cdf0e10cSrcweir 			actionRegister( NEW_DOCUMENT, null );
994cdf0e10cSrcweir 		}
995cdf0e10cSrcweir 	}
996cdf0e10cSrcweir 
997cdf0e10cSrcweir 	class SaveAsMenuItem extends MenuItem implements ActionListener // Menu SaveAs...
998cdf0e10cSrcweir 	{
SaveAsMenuItem()999cdf0e10cSrcweir 		public SaveAsMenuItem()
1000cdf0e10cSrcweir 		{
1001cdf0e10cSrcweir 			super( "SaveAs..." );
1002cdf0e10cSrcweir 			addActionListener( this );
1003cdf0e10cSrcweir 		}
1004cdf0e10cSrcweir 
actionPerformed( ActionEvent e )1005cdf0e10cSrcweir 		public void actionPerformed( ActionEvent e )
1006cdf0e10cSrcweir 		{
1007cdf0e10cSrcweir 			// open SaveAs dialog and store
1008cdf0e10cSrcweir 
1009cdf0e10cSrcweir 			SaveAsOperation();
1010cdf0e10cSrcweir 		}
1011cdf0e10cSrcweir 	}
1012cdf0e10cSrcweir 
1013cdf0e10cSrcweir 	class OpenFileMenuItem extends MenuItem implements ActionListener // Menu Open
1014cdf0e10cSrcweir 	{
OpenFileMenuItem()1015cdf0e10cSrcweir 		public OpenFileMenuItem()
1016cdf0e10cSrcweir 		{
1017cdf0e10cSrcweir 			super( "Open", new MenuShortcut( KeyEvent.VK_C ));
1018cdf0e10cSrcweir 			addActionListener( this );
1019cdf0e10cSrcweir 		}
1020cdf0e10cSrcweir 
actionPerformed( ActionEvent e )1021cdf0e10cSrcweir 		public void actionPerformed( ActionEvent e )
1022cdf0e10cSrcweir 		{
1023cdf0e10cSrcweir 			// open OpenFile dialog and load doc
1024cdf0e10cSrcweir 			FileDialog aFileDialog = new FileDialog( m_aFrame, "Open" );
1025cdf0e10cSrcweir 			aFileDialog.show();
1026cdf0e10cSrcweir 			if ( aFileDialog.getFile() != null )
1027cdf0e10cSrcweir 			{
1028cdf0e10cSrcweir 				String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
1029cdf0e10cSrcweir 				File aFile = new File( aFileName );
1030cdf0e10cSrcweir 				if ( aFile != null )
1031cdf0e10cSrcweir 				{
1032cdf0e10cSrcweir 					// create object from specified file
1033cdf0e10cSrcweir 					String aFileURI = getValidURL( aFile.toURI().toASCIIString() );
1034cdf0e10cSrcweir 					actionRegister( OPEN_FILE, aFileURI );
1035cdf0e10cSrcweir 				}
1036cdf0e10cSrcweir 			}
1037cdf0e10cSrcweir 		}
1038cdf0e10cSrcweir 	}
1039cdf0e10cSrcweir 
1040cdf0e10cSrcweir 	class SaveMenuItem extends MenuItem implements ActionListener // Menu Save
1041cdf0e10cSrcweir 	{
SaveMenuItem()1042cdf0e10cSrcweir 		public SaveMenuItem()
1043cdf0e10cSrcweir 		{
1044cdf0e10cSrcweir 			super( "Save", new MenuShortcut( KeyEvent.VK_D ));
1045cdf0e10cSrcweir 			addActionListener( this );
1046cdf0e10cSrcweir 		}
1047cdf0e10cSrcweir 
actionPerformed( ActionEvent e )1048cdf0e10cSrcweir 		public void actionPerformed( ActionEvent e )
1049cdf0e10cSrcweir 		{
1050cdf0e10cSrcweir 			// if has persistance store there
1051cdf0e10cSrcweir 			// if not open SaveAs dialog and store
1052cdf0e10cSrcweir 			if ( m_xStorage != null && m_xEmbedObj != null )
1053cdf0e10cSrcweir 			{
1054cdf0e10cSrcweir 				if ( m_bOwnFile )
1055cdf0e10cSrcweir 				{
1056cdf0e10cSrcweir 					if ( m_xStorage == null )
1057cdf0e10cSrcweir 					{
1058cdf0e10cSrcweir 						JOptionPane.showMessageDialog( m_aFrame,
1059cdf0e10cSrcweir 														"No storage for oned file!",
1060cdf0e10cSrcweir 														"Error:",
1061cdf0e10cSrcweir 														JOptionPane.ERROR_MESSAGE );
1062cdf0e10cSrcweir 
1063cdf0e10cSrcweir 						return;
1064cdf0e10cSrcweir 					}
1065cdf0e10cSrcweir 
1066cdf0e10cSrcweir 					actionRegister( SAVE, null );
1067cdf0e10cSrcweir 				}
1068cdf0e10cSrcweir 				else
1069cdf0e10cSrcweir 				{
1070cdf0e10cSrcweir 					SaveAsOperation();
1071cdf0e10cSrcweir 				}
1072cdf0e10cSrcweir 			}
1073cdf0e10cSrcweir 			else
1074cdf0e10cSrcweir 				JOptionPane.showMessageDialog( m_aFrame, "No document is embedded!", "Error:", JOptionPane.ERROR_MESSAGE );
1075cdf0e10cSrcweir 		}
1076cdf0e10cSrcweir 	}
1077cdf0e10cSrcweir 
1078cdf0e10cSrcweir 	class NewObjectMenuItem extends MenuItem implements ActionListener // Menu NewObject
1079cdf0e10cSrcweir 	{
NewObjectMenuItem()1080cdf0e10cSrcweir 		public NewObjectMenuItem()
1081cdf0e10cSrcweir 		{
1082cdf0e10cSrcweir 			super( "Create", new MenuShortcut( KeyEvent.VK_N ));
1083cdf0e10cSrcweir 			addActionListener( this );
1084cdf0e10cSrcweir 		}
1085cdf0e10cSrcweir 
actionPerformed( ActionEvent e )1086cdf0e10cSrcweir 		public void actionPerformed( ActionEvent e )
1087cdf0e10cSrcweir 		{
1088cdf0e10cSrcweir 			Object[] possibleValues = { "com.sun.star.comp.Writer.TextDocument",
1089cdf0e10cSrcweir 										"com.sun.star.comp.Writer.GlobalDocument",
1090cdf0e10cSrcweir 										"com.sun.star.comp.Writer.WebDocument",
1091cdf0e10cSrcweir 										"com.sun.star.comp.Calc.SpreadsheetDocument",
1092cdf0e10cSrcweir 										"com.sun.star.comp.Draw.PresentationDocument",
1093cdf0e10cSrcweir 										"com.sun.star.comp.Draw.DrawingDocument",
1094cdf0e10cSrcweir 										"com.sun.star.comp.Math.FormulaDocument",
1095cdf0e10cSrcweir 										"BitmapImage" };
1096cdf0e10cSrcweir 
1097cdf0e10cSrcweir 			String selectedValue = (String)JOptionPane.showInputDialog( null, "DocumentType", "Select",
1098cdf0e10cSrcweir        																	JOptionPane.INFORMATION_MESSAGE, null,
1099cdf0e10cSrcweir        																	possibleValues, possibleValues[0] );
1100cdf0e10cSrcweir 
1101cdf0e10cSrcweir 			actionRegister( NEW_OBJECT, selectedValue );
1102cdf0e10cSrcweir 		}
1103cdf0e10cSrcweir 	}
1104cdf0e10cSrcweir 
1105cdf0e10cSrcweir 	class LoadObjectMenuItem extends MenuItem implements ActionListener // Menu LoadObject
1106cdf0e10cSrcweir 	{
LoadObjectMenuItem()1107cdf0e10cSrcweir 		public LoadObjectMenuItem()
1108cdf0e10cSrcweir 		{
1109cdf0e10cSrcweir 			super( "Load from file", new MenuShortcut( KeyEvent.VK_L ));
1110cdf0e10cSrcweir 			addActionListener( this );
1111cdf0e10cSrcweir 		}
1112cdf0e10cSrcweir 
actionPerformed( ActionEvent e )1113cdf0e10cSrcweir 		public void actionPerformed( ActionEvent e )
1114cdf0e10cSrcweir 		{
1115cdf0e10cSrcweir 			// open OpenFile dialog and load doc
1116cdf0e10cSrcweir 			FileDialog aFileDialog = new FileDialog( m_aFrame, "Select sources to use for object init" );
1117cdf0e10cSrcweir 			aFileDialog.show();
1118cdf0e10cSrcweir 			if ( aFileDialog.getFile() != null )
1119cdf0e10cSrcweir 			{
1120cdf0e10cSrcweir 				String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
1121cdf0e10cSrcweir 				File aFile = new File( aFileName );
1122cdf0e10cSrcweir 				if ( aFile != null )
1123cdf0e10cSrcweir 				{
1124cdf0e10cSrcweir 					// create object from specified file
1125cdf0e10cSrcweir 					String aFileURI = getValidURL( aFile.toURI().toASCIIString() );
1126cdf0e10cSrcweir 					actionRegister( OBJECT_FROM_FILE, aFileURI );
1127cdf0e10cSrcweir 				}
1128cdf0e10cSrcweir 			}
1129cdf0e10cSrcweir 		}
1130cdf0e10cSrcweir 	}
1131cdf0e10cSrcweir 
1132cdf0e10cSrcweir 	class LinkObjectMenuItem extends MenuItem implements ActionListener // Menu LinkObject
1133cdf0e10cSrcweir 	{
LinkObjectMenuItem()1134cdf0e10cSrcweir 		public LinkObjectMenuItem()
1135cdf0e10cSrcweir 		{
1136cdf0e10cSrcweir 			super( "Create link", new MenuShortcut( KeyEvent.VK_M ));
1137cdf0e10cSrcweir 			addActionListener( this );
1138cdf0e10cSrcweir 		}
1139cdf0e10cSrcweir 
actionPerformed( ActionEvent e )1140cdf0e10cSrcweir 		public void actionPerformed( ActionEvent e )
1141cdf0e10cSrcweir 		{
1142cdf0e10cSrcweir 			// open OpenFile dialog and load doc
1143cdf0e10cSrcweir 			FileDialog aFileDialog = new FileDialog( m_aFrame, "Select sources to use for object init" );
1144cdf0e10cSrcweir 			aFileDialog.show();
1145cdf0e10cSrcweir 			if ( aFileDialog.getFile() != null )
1146cdf0e10cSrcweir 			{
1147cdf0e10cSrcweir 				String aFileName = aFileDialog.getDirectory() + aFileDialog.getFile();
1148cdf0e10cSrcweir 				File aFile = new File( aFileName );
1149cdf0e10cSrcweir 				if ( aFile != null )
1150cdf0e10cSrcweir 				{
1151cdf0e10cSrcweir 					// create object from specified file
1152cdf0e10cSrcweir 					String aFileURI = getValidURL( aFile.toURI().toASCIIString() );
1153cdf0e10cSrcweir 					actionRegister( LINK_FROM_FILE, aFileURI );
1154cdf0e10cSrcweir 				}
1155cdf0e10cSrcweir 			}
1156cdf0e10cSrcweir 		}
1157cdf0e10cSrcweir 	}
1158cdf0e10cSrcweir 
1159cdf0e10cSrcweir 	class ConvertLinkToEmbedMenuItem extends MenuItem implements ActionListener // Menu LinkObject
1160cdf0e10cSrcweir 	{
ConvertLinkToEmbedMenuItem()1161cdf0e10cSrcweir 		public ConvertLinkToEmbedMenuItem()
1162cdf0e10cSrcweir 		{
1163cdf0e10cSrcweir 			super( "Convert link to embed", new MenuShortcut( KeyEvent.VK_M ));
1164cdf0e10cSrcweir 			addActionListener( this );
1165cdf0e10cSrcweir 		}
1166cdf0e10cSrcweir 
actionPerformed( ActionEvent e )1167cdf0e10cSrcweir 		public void actionPerformed( ActionEvent e )
1168cdf0e10cSrcweir 		{
1169cdf0e10cSrcweir 			actionRegister( CONVERT_LINK_TO_OBJECT, null );
1170cdf0e10cSrcweir 		}
1171cdf0e10cSrcweir 	}
1172cdf0e10cSrcweir 
1173cdf0e10cSrcweir 	class ActivateOutplaceMenuItem extends MenuItem implements ActionListener // Menu ActiveteOutplace
1174cdf0e10cSrcweir 	{
ActivateOutplaceMenuItem()1175cdf0e10cSrcweir 		public ActivateOutplaceMenuItem()
1176cdf0e10cSrcweir 		{
1177cdf0e10cSrcweir 			super( "Activate outplace", new MenuShortcut( KeyEvent.VK_A ));
1178cdf0e10cSrcweir 			addActionListener( this );
1179cdf0e10cSrcweir 		}
1180cdf0e10cSrcweir 
actionPerformed( ActionEvent e )1181cdf0e10cSrcweir 		public void actionPerformed( ActionEvent e )
1182cdf0e10cSrcweir 		{
1183cdf0e10cSrcweir 			actionRegister( ACTIVATE_OUTPLACE, null );
1184cdf0e10cSrcweir 		}
1185cdf0e10cSrcweir 	}
1186cdf0e10cSrcweir 
1187cdf0e10cSrcweir 	class ActivateInplaceMenuItem extends MenuItem implements ActionListener // Menu ActivateInplace
1188cdf0e10cSrcweir 	{
ActivateInplaceMenuItem()1189cdf0e10cSrcweir 		public ActivateInplaceMenuItem()
1190cdf0e10cSrcweir 		{
1191cdf0e10cSrcweir 			super( "Activate inplace", new MenuShortcut( KeyEvent.VK_I ));
1192cdf0e10cSrcweir 			addActionListener( this );
1193cdf0e10cSrcweir 		}
1194cdf0e10cSrcweir 
actionPerformed( ActionEvent e )1195cdf0e10cSrcweir 		public void actionPerformed( ActionEvent e )
1196cdf0e10cSrcweir 		{
1197cdf0e10cSrcweir 			actionRegister( ACTIVATE_INPLACE, null );
1198cdf0e10cSrcweir 		}
1199cdf0e10cSrcweir 	}
1200cdf0e10cSrcweir 
1201cdf0e10cSrcweir 	class DeactivateMenuItem extends MenuItem implements ActionListener // Menu Deactivate
1202cdf0e10cSrcweir 	{
DeactivateMenuItem()1203cdf0e10cSrcweir 		public DeactivateMenuItem()
1204cdf0e10cSrcweir 		{
1205cdf0e10cSrcweir 			super( "Deactivate", new MenuShortcut( KeyEvent.VK_D ));
1206cdf0e10cSrcweir 			addActionListener( this );
1207cdf0e10cSrcweir 		}
1208cdf0e10cSrcweir 
actionPerformed( ActionEvent e )1209cdf0e10cSrcweir 		public void actionPerformed( ActionEvent e )
1210cdf0e10cSrcweir 		{
1211cdf0e10cSrcweir 			actionRegister( DEACTIVATE, null );
1212cdf0e10cSrcweir 		}
1213cdf0e10cSrcweir 	}
1214cdf0e10cSrcweir 
1215cdf0e10cSrcweir 	// Helper methods
createEmbedObject( String aServiceName )1216cdf0e10cSrcweir 	public XEmbeddedObject createEmbedObject( String aServiceName )
1217cdf0e10cSrcweir 	{
1218cdf0e10cSrcweir 		XEmbeddedObject xEmbObj = null;
1219cdf0e10cSrcweir 		byte[] pClassID = new byte[16];
1220cdf0e10cSrcweir 
1221cdf0e10cSrcweir 		if ( aServiceName.equals( "com.sun.star.comp.Writer.TextDocument" ) )
1222cdf0e10cSrcweir 		{
1223cdf0e10cSrcweir 			int[] pTempClassID = { 0x8B, 0xC6, 0xB1, 0x65, 0xB1, 0xB2, 0x4E, 0xDD,
1224cdf0e10cSrcweir 									0xAA, 0x47, 0xDA, 0xE2, 0xEE, 0x68, 0x9D, 0xD6 };
1225cdf0e10cSrcweir 			for ( int ind = 0; ind < 16; ind++ )
1226cdf0e10cSrcweir 				pClassID[ind] = (byte)pTempClassID[ind];
1227cdf0e10cSrcweir 		}
1228cdf0e10cSrcweir 		else if ( aServiceName.equals( "com.sun.star.comp.Writer.GlobalDocument" ) )
1229cdf0e10cSrcweir 		{
1230cdf0e10cSrcweir 			int[] pTempClassID = { 0xB2, 0x1A, 0x0A, 0x7C, 0xE4, 0x03, 0x41, 0xFE,
1231cdf0e10cSrcweir 									0x95, 0x62, 0xBD, 0x13, 0xEA, 0x6F, 0x15, 0xA0 };
1232cdf0e10cSrcweir 			for ( int ind = 0; ind < 16; ind++ )
1233cdf0e10cSrcweir 				pClassID[ind] = (byte)pTempClassID[ind];
1234cdf0e10cSrcweir 		}
1235cdf0e10cSrcweir 		else if ( aServiceName.equals( "com.sun.star.comp.Writer.WebDocument" ) )
1236cdf0e10cSrcweir 		{
1237cdf0e10cSrcweir 			int[] pTempClassID = { 0xA8, 0xBB, 0xA6, 0x0C, 0x7C, 0x60, 0x45, 0x50,
1238cdf0e10cSrcweir 									0x91, 0xCE, 0x39, 0xC3, 0x90, 0x3F, 0xAC, 0x5E };
1239cdf0e10cSrcweir 			for ( int ind = 0; ind < 16; ind++ )
1240cdf0e10cSrcweir 				pClassID[ind] = (byte)pTempClassID[ind];
1241cdf0e10cSrcweir 		}
1242cdf0e10cSrcweir 		else if ( aServiceName.equals( "com.sun.star.comp.Calc.SpreadsheetDocument" ) )
1243cdf0e10cSrcweir 		{
1244cdf0e10cSrcweir 			int[] pTempClassID = { 0x47, 0xBB, 0xB4, 0xCB, 0xCE, 0x4C, 0x4E, 0x80,
1245cdf0e10cSrcweir 									0xA5, 0x91, 0x42, 0xD9, 0xAE, 0x74, 0x95, 0x0F };
1246cdf0e10cSrcweir 			for ( int ind = 0; ind < 16; ind++ )
1247cdf0e10cSrcweir 				pClassID[ind] = (byte)pTempClassID[ind];
1248cdf0e10cSrcweir 		}
1249cdf0e10cSrcweir 		else if ( aServiceName.equals( "com.sun.star.comp.Draw.PresentationDocument" ) )
1250cdf0e10cSrcweir 		{
1251cdf0e10cSrcweir 			int[] pTempClassID = { 0x91, 0x76, 0xE4, 0x8A, 0x63, 0x7A, 0x4D, 0x1F,
1252cdf0e10cSrcweir 									0x80, 0x3B, 0x99, 0xD9, 0xBF, 0xAC, 0x10, 0x47 };
1253cdf0e10cSrcweir 			for ( int ind = 0; ind < 16; ind++ )
1254cdf0e10cSrcweir 				pClassID[ind] = (byte)pTempClassID[ind];
1255cdf0e10cSrcweir 		}
1256cdf0e10cSrcweir 		else if ( aServiceName.equals( "com.sun.star.comp.Draw.DrawingDocument" ) )
1257cdf0e10cSrcweir 		{
1258cdf0e10cSrcweir 			int[] pTempClassID = { 0x4B, 0xAB, 0x89, 0x70, 0x8A, 0x3B, 0x45, 0xB3,
1259cdf0e10cSrcweir 									0x99, 0x1C, 0xCB, 0xEE, 0xAC, 0x6B, 0xD5, 0xE3 };
1260cdf0e10cSrcweir 			for ( int ind = 0; ind < 16; ind++ )
1261cdf0e10cSrcweir 				pClassID[ind] = (byte)pTempClassID[ind];
1262cdf0e10cSrcweir 		}
1263cdf0e10cSrcweir 		else if ( aServiceName.equals( "com.sun.star.comp.Math.FormulaDocument" ) )
1264cdf0e10cSrcweir 		{
1265cdf0e10cSrcweir 			int[] pTempClassID = { 0x07, 0x8B, 0x7A, 0xBA, 0x54, 0xFC, 0x45, 0x7F,
1266cdf0e10cSrcweir 									0x85, 0x51, 0x61, 0x47, 0xE7, 0x76, 0xA9, 0x97 };
1267cdf0e10cSrcweir 			for ( int ind = 0; ind < 16; ind++ )
1268cdf0e10cSrcweir 				pClassID[ind] = (byte)pTempClassID[ind];
1269cdf0e10cSrcweir 		}
1270cdf0e10cSrcweir 		else if ( aServiceName.equals( "BitmapImage" ) )
1271cdf0e10cSrcweir 		{
1272cdf0e10cSrcweir 			int[] pTempClassID = { 0xD3, 0xE3, 0x4B, 0x21, 0x9D, 0x75, 0x10, 0x1A,
1273cdf0e10cSrcweir 									0x8C, 0x3D, 0x00, 0xAA, 0x00, 0x1A, 0x16, 0x52 };
1274cdf0e10cSrcweir 			for ( int ind = 0; ind < 16; ind++ )
1275cdf0e10cSrcweir 				pClassID[ind] = (byte)pTempClassID[ind];
1276cdf0e10cSrcweir 		}
1277cdf0e10cSrcweir 
1278cdf0e10cSrcweir 		if ( pClassID != null )
1279cdf0e10cSrcweir 		{
1280cdf0e10cSrcweir 			// create embedded object based on the class ID
1281cdf0e10cSrcweir 			try {
1282cdf0e10cSrcweir 				Object oEmbedCreator = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectCreator" );
1283cdf0e10cSrcweir 				XEmbedObjectCreator xEmbedCreator = (XEmbedObjectCreator)UnoRuntime.queryInterface(
1284cdf0e10cSrcweir 																						XEmbedObjectCreator.class,
1285cdf0e10cSrcweir 																						oEmbedCreator );
1286cdf0e10cSrcweir 				if ( xEmbedCreator != null )
1287cdf0e10cSrcweir 				{
1288cdf0e10cSrcweir 					Object oEmbObj = xEmbedCreator.createInstanceInitNew( pClassID,
1289cdf0e10cSrcweir 																		"Dummy name",
1290cdf0e10cSrcweir 																		m_xStorage,
1291cdf0e10cSrcweir 																		"EmbedSub",
1292cdf0e10cSrcweir 																		new PropertyValue[0] );
1293cdf0e10cSrcweir 					xEmbObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
1294cdf0e10cSrcweir 				}
1295cdf0e10cSrcweir 				else
1296cdf0e10cSrcweir 					JOptionPane.showMessageDialog( m_aFrame,
1297cdf0e10cSrcweir 												   "Can't create EmbedCreator!",
1298cdf0e10cSrcweir 												   "Error:",
1299cdf0e10cSrcweir 												   JOptionPane.ERROR_MESSAGE );
1300cdf0e10cSrcweir 			}
1301cdf0e10cSrcweir 			catch( Exception e )
1302cdf0e10cSrcweir 			{
1303cdf0e10cSrcweir 				JOptionPane.showMessageDialog( m_aFrame, e, "Exception in createInstanceInitNew():", JOptionPane.ERROR_MESSAGE );
1304cdf0e10cSrcweir 			}
1305cdf0e10cSrcweir 		}
1306cdf0e10cSrcweir 		else
1307cdf0e10cSrcweir 			JOptionPane.showMessageDialog( m_aFrame, "Can't retrieve class ID!", "Error:", JOptionPane.ERROR_MESSAGE );
1308cdf0e10cSrcweir 
1309cdf0e10cSrcweir 		return xEmbObj;
1310cdf0e10cSrcweir 	}
1311cdf0e10cSrcweir 
createLinkObject( String aLinkURL )1312cdf0e10cSrcweir 	public XEmbeddedObject createLinkObject( String aLinkURL )
1313cdf0e10cSrcweir 	{
1314cdf0e10cSrcweir 		XEmbeddedObject xEmbObj = null;
1315cdf0e10cSrcweir 
1316cdf0e10cSrcweir 		try {
1317cdf0e10cSrcweir 			Object oLinkCreator = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectCreator" );
1318cdf0e10cSrcweir 			XLinkCreator xLinkCreator = (XLinkCreator)UnoRuntime.queryInterface(
1319cdf0e10cSrcweir 																					XLinkCreator.class,
1320cdf0e10cSrcweir 																					oLinkCreator );
1321cdf0e10cSrcweir 			if ( xLinkCreator != null )
1322cdf0e10cSrcweir 			{
1323cdf0e10cSrcweir 				PropertyValue[] aMedDescr = { new PropertyValue(), new PropertyValue() };
1324cdf0e10cSrcweir 				aMedDescr[0].Name = "URL";
1325cdf0e10cSrcweir 				aMedDescr[0].Value = (Object) aLinkURL;
1326cdf0e10cSrcweir 				aMedDescr[1].Name = "ReadOnly";
1327cdf0e10cSrcweir 				aMedDescr[1].Value = (Object) new Boolean( false );
1328cdf0e10cSrcweir 				Object oEmbObj = xLinkCreator.createInstanceLink( m_xStorage, "EmbedSub", aMedDescr, new PropertyValue[0] );
1329cdf0e10cSrcweir 				xEmbObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
1330cdf0e10cSrcweir 			}
1331cdf0e10cSrcweir 			else
1332cdf0e10cSrcweir 				JOptionPane.showMessageDialog( m_aFrame,
1333cdf0e10cSrcweir 											   "Can't create LinkCreator!",
1334cdf0e10cSrcweir 											   "Error:",
1335cdf0e10cSrcweir 											   JOptionPane.ERROR_MESSAGE );
1336cdf0e10cSrcweir 		}
1337cdf0e10cSrcweir 		catch( Exception e )
1338cdf0e10cSrcweir 		{
1339cdf0e10cSrcweir 			JOptionPane.showMessageDialog( m_aFrame, e, "Exception in createLinkObject():", JOptionPane.ERROR_MESSAGE );
1340cdf0e10cSrcweir 		}
1341cdf0e10cSrcweir 
1342cdf0e10cSrcweir 
1343cdf0e10cSrcweir 		return xEmbObj;
1344cdf0e10cSrcweir 	}
1345cdf0e10cSrcweir 
1346cdf0e10cSrcweir 
loadEmbedObject( String aFileURI )1347cdf0e10cSrcweir 	public XEmbeddedObject loadEmbedObject( String aFileURI )
1348cdf0e10cSrcweir 	{
1349cdf0e10cSrcweir 		XEmbeddedObject xEmbObj = null;
1350cdf0e10cSrcweir 		try {
1351cdf0e10cSrcweir 			Object oEmbedCreator = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectCreator" );
1352cdf0e10cSrcweir 			XEmbedObjectCreator xEmbedCreator = (XEmbedObjectCreator)UnoRuntime.queryInterface(
1353cdf0e10cSrcweir 																					XEmbedObjectCreator.class,
1354cdf0e10cSrcweir 																					oEmbedCreator );
1355cdf0e10cSrcweir 			if ( xEmbedCreator != null )
1356cdf0e10cSrcweir 			{
1357cdf0e10cSrcweir 				PropertyValue[] aMedDescr = { new PropertyValue(), new PropertyValue() };
1358cdf0e10cSrcweir 				aMedDescr[0].Name = "URL";
1359cdf0e10cSrcweir 				aMedDescr[0].Value = (Object) aFileURI;
1360cdf0e10cSrcweir 				aMedDescr[1].Name = "ReadOnly";
1361cdf0e10cSrcweir 				aMedDescr[1].Value = (Object) new Boolean( false );
1362cdf0e10cSrcweir 				Object oEmbObj = xEmbedCreator.createInstanceInitFromMediaDescriptor( m_xStorage,
1363cdf0e10cSrcweir 																					"EmbedSub",
1364cdf0e10cSrcweir 																					aMedDescr,
1365cdf0e10cSrcweir 																					new PropertyValue[0] );
1366cdf0e10cSrcweir 				xEmbObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
1367cdf0e10cSrcweir 			}
1368cdf0e10cSrcweir 			else
1369cdf0e10cSrcweir 				JOptionPane.showMessageDialog( m_aFrame,
1370cdf0e10cSrcweir 											   "Can't create EmbedFactory!",
1371cdf0e10cSrcweir 											   "Error:",
1372cdf0e10cSrcweir 											   JOptionPane.ERROR_MESSAGE );
1373cdf0e10cSrcweir 		}
1374cdf0e10cSrcweir 		catch( Exception e )
1375cdf0e10cSrcweir 		{
1376cdf0e10cSrcweir 			JOptionPane.showMessageDialog( m_aFrame, e, "Exception in loadEmbedObject():", JOptionPane.ERROR_MESSAGE );
1377cdf0e10cSrcweir 		}
1378cdf0e10cSrcweir 
1379cdf0e10cSrcweir 		return xEmbObj;
1380cdf0e10cSrcweir 	}
1381cdf0e10cSrcweir 
clearObjectAndStorage()1382cdf0e10cSrcweir 	public void clearObjectAndStorage()
1383cdf0e10cSrcweir 	{
1384cdf0e10cSrcweir 		synchronized( m_oImageLock )
1385cdf0e10cSrcweir 		{
1386cdf0e10cSrcweir 			m_aImage = null;
1387cdf0e10cSrcweir 		}
1388cdf0e10cSrcweir 
1389cdf0e10cSrcweir 		m_nXScaling = 1;
1390cdf0e10cSrcweir 		m_nYScaling = 1;
1391cdf0e10cSrcweir 		m_nXPixelSize = 1;
1392cdf0e10cSrcweir 		m_nYPixelSize = 1;
1393cdf0e10cSrcweir 
1394cdf0e10cSrcweir 		m_bOwnFile = false;
1395cdf0e10cSrcweir 
1396cdf0e10cSrcweir 		m_aLinkURI = null;
1397cdf0e10cSrcweir 		m_bLinkObj = false;
1398cdf0e10cSrcweir 
1399cdf0e10cSrcweir 		if ( m_xEmbedObj != null )
1400cdf0e10cSrcweir 		{
1401cdf0e10cSrcweir 			try {
1402cdf0e10cSrcweir 				XCloseable xClose = (XCloseable)UnoRuntime.queryInterface( XCloseable.class, m_xEmbedObj );
1403cdf0e10cSrcweir 				if ( xClose != null )
1404cdf0e10cSrcweir 					xClose.close( true );
1405cdf0e10cSrcweir 			}
1406cdf0e10cSrcweir 			catch ( Exception ex )
1407cdf0e10cSrcweir 			{}
1408cdf0e10cSrcweir 			m_xEmbedObj = null;
1409cdf0e10cSrcweir 		}
1410cdf0e10cSrcweir 
1411cdf0e10cSrcweir 		if ( m_xStorage != null )
1412cdf0e10cSrcweir 		{
1413cdf0e10cSrcweir 			try {
1414cdf0e10cSrcweir 				XComponent xComponent = (XComponent)UnoRuntime.queryInterface( XComponent.class, m_xStorage );
1415cdf0e10cSrcweir 				if ( xComponent != null )
1416cdf0e10cSrcweir 					xComponent.dispose();
1417cdf0e10cSrcweir 			}
1418cdf0e10cSrcweir 			catch ( Exception ex )
1419cdf0e10cSrcweir 			{}
1420cdf0e10cSrcweir 			m_xStorage = null;
1421cdf0e10cSrcweir 		}
1422cdf0e10cSrcweir 	}
1423cdf0e10cSrcweir 
createTempStorage()1424cdf0e10cSrcweir 	public XStorage createTempStorage()
1425cdf0e10cSrcweir 	{
1426cdf0e10cSrcweir 		XStorage xTempStorage = null;
1427cdf0e10cSrcweir 
1428cdf0e10cSrcweir 		try {
1429cdf0e10cSrcweir 			Object oStorageFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.StorageFactory" );
1430cdf0e10cSrcweir 			XSingleServiceFactory xStorageFactory = (XSingleServiceFactory)UnoRuntime.queryInterface(
1431cdf0e10cSrcweir 																						XSingleServiceFactory.class,
1432cdf0e10cSrcweir 																						oStorageFactory );
1433cdf0e10cSrcweir 			if ( xStorageFactory != null )
1434cdf0e10cSrcweir 			{
1435cdf0e10cSrcweir 				Object oStorage = xStorageFactory.createInstance();
1436cdf0e10cSrcweir 				xTempStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
1437cdf0e10cSrcweir 			}
1438cdf0e10cSrcweir 			else
1439cdf0e10cSrcweir 				JOptionPane.showMessageDialog( m_aFrame,
1440cdf0e10cSrcweir 												"Can't create StorageFactory!",
1441cdf0e10cSrcweir 												"Error:",
1442cdf0e10cSrcweir 												JOptionPane.ERROR_MESSAGE );
1443cdf0e10cSrcweir 		}
1444cdf0e10cSrcweir 		catch( Exception e )
1445cdf0e10cSrcweir 		{
1446cdf0e10cSrcweir 			JOptionPane.showMessageDialog( m_aFrame, e, "Exception in createTempStorage():", JOptionPane.ERROR_MESSAGE );
1447cdf0e10cSrcweir 		}
1448cdf0e10cSrcweir 
1449cdf0e10cSrcweir 		return xTempStorage;
1450cdf0e10cSrcweir 	}
1451cdf0e10cSrcweir 
saveObjectAsFileURI( String aFileURI )1452cdf0e10cSrcweir 	public void saveObjectAsFileURI( String aFileURI )
1453cdf0e10cSrcweir 	{
1454cdf0e10cSrcweir 		try {
1455cdf0e10cSrcweir 			Object oStorageFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.StorageFactory" );
1456cdf0e10cSrcweir 			XSingleServiceFactory xStorageFactory = (XSingleServiceFactory)UnoRuntime.queryInterface(
1457cdf0e10cSrcweir 																						XSingleServiceFactory.class,
1458cdf0e10cSrcweir 																						oStorageFactory );
1459cdf0e10cSrcweir 			if ( xStorageFactory != null )
1460cdf0e10cSrcweir 			{
1461cdf0e10cSrcweir 				XEmbedPersist xPersist = (XEmbedPersist)UnoRuntime.queryInterface( XEmbedPersist.class, m_xEmbedObj );
1462cdf0e10cSrcweir 				if ( xPersist != null )
1463cdf0e10cSrcweir 				{
1464cdf0e10cSrcweir 					Object aArgs[] = new Object[2];
1465cdf0e10cSrcweir 					aArgs[0] = aFileURI;
1466cdf0e10cSrcweir 					aArgs[1] = new Integer( ElementModes.ELEMENT_READWRITE );
1467cdf0e10cSrcweir 
1468cdf0e10cSrcweir 					Object oStorage = xStorageFactory.createInstanceWithArguments( aArgs );
1469cdf0e10cSrcweir 					XStorage xTargetStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
1470cdf0e10cSrcweir 
1471cdf0e10cSrcweir 					PropertyValue aProps[] = { new PropertyValue() };
1472cdf0e10cSrcweir 					aProps[0].Name = "StoreVisualReplacement";
1473cdf0e10cSrcweir 					aProps[0].Value = new Boolean( m_bStoreVisRepl );
1474cdf0e10cSrcweir 
1475cdf0e10cSrcweir 					xPersist.storeAsEntry( xTargetStorage, "EmbedSub", new PropertyValue[0], aProps );
1476cdf0e10cSrcweir 					xPersist.saveCompleted( true );
1477cdf0e10cSrcweir 
1478cdf0e10cSrcweir 					// the object must be already based on new storage
1479cdf0e10cSrcweir 					XComponent xComponent = (XComponent)UnoRuntime.queryInterface( XComponent.class, m_xStorage );
1480cdf0e10cSrcweir 					xComponent.dispose();
1481cdf0e10cSrcweir 
1482cdf0e10cSrcweir 					m_xStorage = xTargetStorage;
1483cdf0e10cSrcweir 					m_bOwnFile = true;
1484cdf0e10cSrcweir 
1485cdf0e10cSrcweir 					XTransactedObject xTransact = (XTransactedObject)UnoRuntime.queryInterface( XTransactedObject.class,
1486cdf0e10cSrcweir 																							m_xStorage );
1487cdf0e10cSrcweir 					if ( xTransact != null )
1488cdf0e10cSrcweir 						xTransact.commit();
1489cdf0e10cSrcweir 				}
1490cdf0e10cSrcweir 				else
1491cdf0e10cSrcweir 					JOptionPane.showMessageDialog( m_aFrame, "No XEmbedPersist!", "Error:", JOptionPane.ERROR_MESSAGE );
1492cdf0e10cSrcweir 			}
1493cdf0e10cSrcweir 			else
1494cdf0e10cSrcweir 				JOptionPane.showMessageDialog( m_aFrame,
1495cdf0e10cSrcweir 												"Can't create StorageFactory!",
1496cdf0e10cSrcweir 												"Error:",
1497cdf0e10cSrcweir 												JOptionPane.ERROR_MESSAGE );
1498cdf0e10cSrcweir 		}
1499cdf0e10cSrcweir 		catch( Exception e )
1500cdf0e10cSrcweir 		{
1501cdf0e10cSrcweir 			JOptionPane.showMessageDialog( m_aFrame, e, "Exception in saveStorageToFileURI():", JOptionPane.ERROR_MESSAGE );
1502cdf0e10cSrcweir 		}
1503cdf0e10cSrcweir 
1504cdf0e10cSrcweir 	}
1505cdf0e10cSrcweir 
loadFileURI( String aFileURI )1506cdf0e10cSrcweir 	public void loadFileURI( String aFileURI )
1507cdf0e10cSrcweir 	{
1508cdf0e10cSrcweir 		try
1509cdf0e10cSrcweir 		{
1510cdf0e10cSrcweir 			Object oStorageFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.StorageFactory" );
1511cdf0e10cSrcweir 			XSingleServiceFactory xStorageFactory = (XSingleServiceFactory)UnoRuntime.queryInterface(
1512cdf0e10cSrcweir 																						XSingleServiceFactory.class,
1513cdf0e10cSrcweir 																						oStorageFactory );
1514cdf0e10cSrcweir 			Object aArgs[] = new Object[2];
1515cdf0e10cSrcweir 			aArgs[0] = aFileURI;
1516cdf0e10cSrcweir 			aArgs[1] = new Integer( ElementModes.ELEMENT_READWRITE );
1517cdf0e10cSrcweir 
1518cdf0e10cSrcweir 			Object oStorage = xStorageFactory.createInstanceWithArguments( aArgs );
1519cdf0e10cSrcweir 			XStorage xTargetStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
1520cdf0e10cSrcweir 
1521cdf0e10cSrcweir 			Object oEmbedCreator = m_xServiceFactory.createInstance( "com.sun.star.embed.EmbeddedObjectCreator" );
1522cdf0e10cSrcweir 			XEmbedObjectCreator xEmbedCreator = (XEmbedObjectCreator)UnoRuntime.queryInterface(
1523cdf0e10cSrcweir 																					XEmbedObjectCreator.class,
1524cdf0e10cSrcweir 																					oEmbedCreator );
1525cdf0e10cSrcweir 
1526cdf0e10cSrcweir 			XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface( XNameAccess.class,
1527cdf0e10cSrcweir 																			xTargetStorage );
1528cdf0e10cSrcweir 			if ( xNameAccess == null )
1529cdf0e10cSrcweir 			{
1530cdf0e10cSrcweir 				JOptionPane.showMessageDialog( m_aFrame, "No XNameAccess!", "Error:", JOptionPane.ERROR_MESSAGE );
1531cdf0e10cSrcweir 				return;
1532cdf0e10cSrcweir 			}
1533cdf0e10cSrcweir 
1534cdf0e10cSrcweir 			Object oEmbObj = null;
1535cdf0e10cSrcweir 			if ( xNameAccess.hasByName( "LinkName" ) && xTargetStorage.isStreamElement( "LinkName" ) )
1536cdf0e10cSrcweir 			{
1537cdf0e10cSrcweir 			/*
1538cdf0e10cSrcweir 				// OOo links will not be tested until they have correct persistence
1539cdf0e10cSrcweir 				XStream xLinkStream = xTargetStorage.openStreamElement( "LinkName", ElementModes.ELEMENT_READ );
1540cdf0e10cSrcweir 				if ( xLinkStream != null )
1541cdf0e10cSrcweir 				{
1542cdf0e10cSrcweir 					XInputStream xInStream = xLinkStream.getInputStream();
1543cdf0e10cSrcweir 					if ( xInStream != null )
1544cdf0e10cSrcweir 					{
1545cdf0e10cSrcweir 						byte[][] pBuff = new byte[1][0];
1546cdf0e10cSrcweir 						int nRead = xInStream.readBytes( pBuff, 1000 );
1547cdf0e10cSrcweir 						m_aLinkURI = new String( pBuff[0] );
1548cdf0e10cSrcweir 						xInStream.closeInput();
1549cdf0e10cSrcweir 						oEmbObj = xEmbedCreator.createInstanceLink( m_aLinkURI );
1550cdf0e10cSrcweir 						m_bLinkObj = true;
1551cdf0e10cSrcweir 					}
1552cdf0e10cSrcweir 				}
1553cdf0e10cSrcweir 			*/
1554cdf0e10cSrcweir 			}
1555cdf0e10cSrcweir 			else
1556cdf0e10cSrcweir 				oEmbObj = xEmbedCreator.createInstanceInitFromEntry( xTargetStorage,
1557cdf0e10cSrcweir 																	"EmbedSub",
1558cdf0e10cSrcweir 																	false,
1559cdf0e10cSrcweir 																	new PropertyValue[0] );
1560cdf0e10cSrcweir 
1561cdf0e10cSrcweir 			m_xEmbedObj = (XEmbeddedObject)UnoRuntime.queryInterface( XEmbeddedObject.class, oEmbObj );
1562cdf0e10cSrcweir 
1563cdf0e10cSrcweir 			if ( m_xEmbedObj != null )
1564cdf0e10cSrcweir 			{
1565cdf0e10cSrcweir 				m_xStorage = xTargetStorage;
1566cdf0e10cSrcweir 				m_bOwnFile = true;
1567cdf0e10cSrcweir 			}
1568cdf0e10cSrcweir 			else
1569cdf0e10cSrcweir 				JOptionPane.showMessageDialog( m_aFrame,
1570cdf0e10cSrcweir 											   "Can't create EmbedObject from storage!",
1571cdf0e10cSrcweir 											   "Error:",
1572cdf0e10cSrcweir 											   JOptionPane.ERROR_MESSAGE );
1573cdf0e10cSrcweir 		}
1574cdf0e10cSrcweir 		catch( Exception e )
1575cdf0e10cSrcweir 		{
1576cdf0e10cSrcweir 			JOptionPane.showMessageDialog( m_aFrame, e, "Exception in loadFileURI():", JOptionPane.ERROR_MESSAGE );
1577cdf0e10cSrcweir 		}
1578cdf0e10cSrcweir 	}
1579cdf0e10cSrcweir 
storeLinkToStorage()1580cdf0e10cSrcweir 	public void storeLinkToStorage()
1581cdf0e10cSrcweir 	{
1582cdf0e10cSrcweir 		if ( m_xStorage != null && m_bLinkObj )
1583cdf0e10cSrcweir 		{
1584cdf0e10cSrcweir 			try {
1585cdf0e10cSrcweir 				XStream xLinkStream = m_xStorage.openStreamElement( "LinkName", ElementModes.ELEMENT_WRITE );
1586cdf0e10cSrcweir 
1587cdf0e10cSrcweir 				if ( xLinkStream != null )
1588cdf0e10cSrcweir 				{
1589cdf0e10cSrcweir 					XOutputStream xLinkOutStream = xLinkStream.getOutputStream();
1590cdf0e10cSrcweir 					XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface( XTruncate.class,
1591cdf0e10cSrcweir 																			 	xLinkOutStream );
1592cdf0e10cSrcweir 					if ( xLinkOutStream != null && xTruncate != null )
1593cdf0e10cSrcweir 					{
1594cdf0e10cSrcweir 						xTruncate.truncate();
1595cdf0e10cSrcweir 
1596cdf0e10cSrcweir 						char[] aLinkChar = m_aLinkURI.toCharArray();
1597cdf0e10cSrcweir 						byte[] aLinkBytes = new byte[ aLinkChar.length ];
1598cdf0e10cSrcweir 						for ( int ind = 0; ind < aLinkChar.length; ind++ )
1599cdf0e10cSrcweir 							aLinkBytes[ind] = (byte)aLinkChar[ind];
1600cdf0e10cSrcweir 
1601cdf0e10cSrcweir 						xLinkOutStream.writeBytes( aLinkBytes );
1602cdf0e10cSrcweir 						xLinkOutStream.closeOutput();
1603cdf0e10cSrcweir 
1604cdf0e10cSrcweir 						XComponent xComponent = (XComponent) UnoRuntime.queryInterface( XComponent.class,
1605cdf0e10cSrcweir 																						xLinkStream );
1606cdf0e10cSrcweir 						if ( xComponent != null )
1607cdf0e10cSrcweir 							xComponent.dispose();
1608cdf0e10cSrcweir 					}
1609cdf0e10cSrcweir 					else
1610cdf0e10cSrcweir 						JOptionPane.showMessageDialog( m_aFrame,
1611cdf0e10cSrcweir 														"The substream can not be truncated or written!",
1612cdf0e10cSrcweir 														"Error:",
1613cdf0e10cSrcweir 														JOptionPane.ERROR_MESSAGE );
1614cdf0e10cSrcweir 
1615cdf0e10cSrcweir 				}
1616cdf0e10cSrcweir 				else
1617cdf0e10cSrcweir 					JOptionPane.showMessageDialog( m_aFrame,
1618cdf0e10cSrcweir 													"Can't create/open substream!",
1619cdf0e10cSrcweir 													"Error:",
1620cdf0e10cSrcweir 													JOptionPane.ERROR_MESSAGE );
1621cdf0e10cSrcweir 			}
1622cdf0e10cSrcweir 			catch( Exception e )
1623cdf0e10cSrcweir 			{
1624cdf0e10cSrcweir 				JOptionPane.showMessageDialog( m_aFrame,
1625cdf0e10cSrcweir 											e,
1626cdf0e10cSrcweir 											"Exception in storeLinkToStorage:",
1627cdf0e10cSrcweir 											JOptionPane.ERROR_MESSAGE );
1628cdf0e10cSrcweir 
1629cdf0e10cSrcweir 			}
1630cdf0e10cSrcweir 		}
1631cdf0e10cSrcweir 	}
1632cdf0e10cSrcweir 
storeLinkAsFileURI( String aFileURI )1633cdf0e10cSrcweir 	public void storeLinkAsFileURI( String aFileURI )
1634cdf0e10cSrcweir 	{
1635cdf0e10cSrcweir 		try {
1636cdf0e10cSrcweir 			Object oStorageFactory = m_xServiceFactory.createInstance( "com.sun.star.embed.StorageFactory" );
1637cdf0e10cSrcweir 			XSingleServiceFactory xStorageFactory = (XSingleServiceFactory)UnoRuntime.queryInterface(
1638cdf0e10cSrcweir 																						XSingleServiceFactory.class,
1639cdf0e10cSrcweir 																						oStorageFactory );
1640cdf0e10cSrcweir 			if ( xStorageFactory != null )
1641cdf0e10cSrcweir 			{
1642cdf0e10cSrcweir 				Object aArgs[] = new Object[2];
1643cdf0e10cSrcweir 				aArgs[0] = aFileURI;
1644cdf0e10cSrcweir 				aArgs[1] = new Integer( ElementModes.ELEMENT_READWRITE );
1645cdf0e10cSrcweir 
1646cdf0e10cSrcweir 				Object oStorage = xStorageFactory.createInstanceWithArguments( aArgs );
1647cdf0e10cSrcweir 				XStorage xTargetStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oStorage );
1648cdf0e10cSrcweir 
1649cdf0e10cSrcweir 				XComponent xComponent = (XComponent)UnoRuntime.queryInterface( XComponent.class, m_xStorage );
1650cdf0e10cSrcweir 				xComponent.dispose();
1651cdf0e10cSrcweir 
1652cdf0e10cSrcweir 				m_xStorage = xTargetStorage;
1653cdf0e10cSrcweir 				m_bOwnFile = true;
1654cdf0e10cSrcweir 
1655cdf0e10cSrcweir 				storeLinkToStorage();
1656cdf0e10cSrcweir 
1657cdf0e10cSrcweir 				XTransactedObject xTransact = (XTransactedObject)UnoRuntime.queryInterface( XTransactedObject.class,
1658cdf0e10cSrcweir 																							m_xStorage );
1659cdf0e10cSrcweir 				if ( xTransact != null )
1660cdf0e10cSrcweir 					xTransact.commit();
1661cdf0e10cSrcweir 			}
1662cdf0e10cSrcweir 			else
1663cdf0e10cSrcweir 				JOptionPane.showMessageDialog( m_aFrame,
1664cdf0e10cSrcweir 												"Can't create StorageFactory!",
1665cdf0e10cSrcweir 												"Error:",
1666cdf0e10cSrcweir 												JOptionPane.ERROR_MESSAGE );
1667cdf0e10cSrcweir 		}
1668cdf0e10cSrcweir 		catch( Exception e )
1669cdf0e10cSrcweir 		{
1670cdf0e10cSrcweir 			JOptionPane.showMessageDialog( m_aFrame, e, "Exception in saveStorageToFileURI():", JOptionPane.ERROR_MESSAGE );
1671cdf0e10cSrcweir 		}
1672cdf0e10cSrcweir 	}
1673cdf0e10cSrcweir 
getValidURL( String sFileURL )1674cdf0e10cSrcweir 	public String getValidURL( String sFileURL )
1675cdf0e10cSrcweir 	{
1676cdf0e10cSrcweir 		// m_xTransformer must be set!
1677cdf0e10cSrcweir 		URL[] aURLs = { new URL() };
1678cdf0e10cSrcweir 		aURLs[0].Complete = sFileURL;
1679cdf0e10cSrcweir 
1680cdf0e10cSrcweir 		try {
1681cdf0e10cSrcweir 			if ( !m_xTransformer.parseSmart( aURLs, "" ) )
1682cdf0e10cSrcweir 				throw new Exception();
1683cdf0e10cSrcweir 		}
1684cdf0e10cSrcweir 		catch( Exception e )
1685cdf0e10cSrcweir 		{
1686cdf0e10cSrcweir 			JOptionPane.showMessageDialog( m_aFrame, e, "Exception in getValidURL():", JOptionPane.ERROR_MESSAGE );
1687cdf0e10cSrcweir 		}
1688cdf0e10cSrcweir 
1689cdf0e10cSrcweir 		return aURLs[0].Complete;
1690cdf0e10cSrcweir 	}
1691cdf0e10cSrcweir 
disposeObject()1692cdf0e10cSrcweir 	public void disposeObject()
1693cdf0e10cSrcweir 	{
1694cdf0e10cSrcweir 		// TODO:
1695cdf0e10cSrcweir 		// usage of object, storage and bitmap painter should be locked
1696cdf0e10cSrcweir 		// but since possibility of rasecondition is very low
1697cdf0e10cSrcweir 		// it is not really required for testing application
1698cdf0e10cSrcweir 
1699cdf0e10cSrcweir 		clearObjectAndStorage();
1700cdf0e10cSrcweir 
1701cdf0e10cSrcweir 		if ( m_aBitmapPainter != null )
1702cdf0e10cSrcweir 		{
1703cdf0e10cSrcweir 			m_aBitmapPainter.disconnectListener();
1704cdf0e10cSrcweir 			m_aBitmapPainter = null;
1705cdf0e10cSrcweir 		}
1706cdf0e10cSrcweir 	}
1707cdf0e10cSrcweir }
1708cdf0e10cSrcweir 
1709