merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / DevelopersGuide / OfficeDev / DesktopEnvironment / DocumentView.java
blob4de809385ba5dad7ebbace60854b43ed7c6ae363
1 /*************************************************************************
3 * $RCSfile: DocumentView.java,v $
5 * $Revision: 1.4 $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:37:50 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 // __________ Imports __________
43 import com.sun.star.uno.UnoRuntime;
45 import java.awt.*;
46 import java.awt.event.*;
47 import java.lang.*;
48 import java.net.*;
49 import javax.swing.*;
50 import javax.swing.border.*;
51 import java.awt.AWTEvent;
52 import java.awt.event.WindowEvent;
54 // __________ Implementation __________
56 /**
57 * This implement a java frame wich contains
58 * an office document, shows some status informations
59 * about that, provides simple functionality on it
60 * (e.g. toggle menubar, save document) and
61 * react for different situations independent
62 * (e.g. closing the document from outside).
63 * Every instance of this class will be a member
64 * inside the global "ViewContainer" of this java
65 * demo application which holds all opened views alive.
67 * @author Andreas Schlüns
68 * @created 06.03.2002 09:38
70 public class DocumentView extends JFrame
71 implements com.sun.star.lang.XEventListener, // react for Frame::disposing()
72 IShutdownListener // react for System.exit()
74 // ____________________
76 /**
77 * const
78 * These command strings are used to identify a received action
79 * of buttons on which we listen for action events.
81 public static final String COMMAND_OPEN = "open" ;
82 public static final String COMMAND_SAVE = "save" ;
83 public static final String COMMAND_EXPORT = "export" ;
84 public static final String COMMAND_EXIT = "exit" ;
86 // ____________________
88 /**
89 * @member mxFrame office frame which contains the document of this view
91 * @member maStatusView special panel wich show available status informations of currently loaded document
92 * @member maDocumentView use JNI mechanism to plug an office window into our own java UI container (used for inplace mode only!)
93 * @member maCustomizeView special panel makes it possible to toggle menubar/toolbar or objectbar of loaded document
94 * @member maInterceptor interceptor thread which intercept "new" menu of office frame to open new frames inside this java application
96 * @member msName unique name of this view (returned by the global ViewContainer during registration)
98 * @member mbOpen button to open documents
99 * @member mbSave button to save currently loaded document
100 * @member mbExport button to save currently loaded document in HTML format (if it is possible!)
101 * @member mbExit button to exit this demo
103 * @member maInterception we try to intercept the file->new menu to open new document inside this java application
105 private com.sun.star.frame.XFrame mxFrame ;
107 private StatusView maStatusView ;
108 private NativeView maDocumentView ;
109 private CustomizeView maCustomizeView ;
110 private Interceptor maInterceptor ;
112 private String msName ;
114 private JButton mbtOpen ;
115 private JButton mbtSave ;
116 private JButton mbtExport ;
117 private JButton mbtExit ;
119 private boolean mbDead ;
121 // ____________________
124 * ctor
125 * Create view controls on startup and initialize it with default values.
127 DocumentView()
129 this.setSize( new Dimension(800,600) );
131 JPanel paMainPanel = (JPanel)this.getContentPane();
133 // create and add command buttons to a panel
134 // it will be a sub panel of later layouted UI
135 mbtOpen = new JButton("Open ..." );
136 mbtSave = new JButton("Save" );
137 mbtExport = new JButton("Save as HTML ...");
138 mbtExit = new JButton("Exit" );
140 mbtOpen.setEnabled (true );
141 mbtSave.setEnabled (false);
142 mbtExport.setEnabled(false);
143 mbtExit.setEnabled (true );
145 mbtOpen.setActionCommand (COMMAND_OPEN );
146 mbtSave.setActionCommand (COMMAND_SAVE );
147 mbtExport.setActionCommand(COMMAND_EXPORT);
148 mbtExit.setActionCommand (COMMAND_EXIT );
150 Reactor aListener = new Reactor();
151 mbtOpen.addActionListener (aListener);
152 mbtSave.addActionListener (aListener);
153 mbtExport.addActionListener(aListener);
154 mbtExit.addActionListener (aListener);
156 JPanel paCommands = new JPanel( new GridLayout(4,0) );
157 paCommands.add(mbtOpen);
158 paCommands.add(mbtSave);
159 paCommands.add(mbtExport);
160 paCommands.add(mbtExit);
162 // create view to show status informations of opened file
163 maStatusView = new StatusView();
165 // create view for toggle different bar's of document
166 maCustomizeView = new CustomizeView();
168 paCommands.setBorder ( new TitledBorder(BorderFactory.createEtchedBorder(),"Commands") );
169 maStatusView.setBorder ( new TitledBorder(BorderFactory.createEtchedBorder(),"Status Informations") );
170 maCustomizeView.setBorder( new TitledBorder(BorderFactory.createEtchedBorder(),"Customize Document View") );
172 // layout the whole UI
173 JPanel paTest = new JPanel(new GridLayout(3,0));
174 paTest.add(paCommands );
175 paTest.add(maStatusView );
176 paTest.add(maCustomizeView);
177 JScrollPane paScroll = new JScrollPane();
178 paScroll.getViewport().add(paTest,null);
180 if(ViewContainer.mbInplace==true)
182 // create view to show opened documents
183 // This special view is neccessary for inplace mode only!
184 maDocumentView = new NativeView();
186 JSplitPane paSplit = new JSplitPane();
187 paSplit.setOneTouchExpandable( true );
189 paSplit.setLeftComponent (maDocumentView);
190 paSplit.setRightComponent(paScroll );
192 paMainPanel.add(paSplit);
194 else
196 paMainPanel.add(paScroll);
199 // Register this new view on our global view container.
200 msName = FunctionHelper.getUniqueFrameName();
201 this.setTitle(msName);
202 ViewContainer.getGlobalContainer().addView(this);
203 ViewContainer.getGlobalContainer().addListener(this);
204 // be listener for closing the application
205 this.enableEvents(AWTEvent.WINDOW_EVENT_MASK);
208 // ____________________
211 * Create the view frame for showing the office documents on demand.
212 * Dependend from given command line parameter we create
213 * an office XFrame and initialize it with a window. This
214 * window can be a pure toolkit window (means toolkit of office!)
215 * or a plugged java canvas - office window combination.
217 public void createFrame()
219 // create view frame (as a XFrame!) here
220 // Look for right view mode setted by user command line parameter.
221 // First try to get a new unambigous frame name from our global ViewContainer.
222 if(ViewContainer.mbInplace==true)
224 // inplace document view can't be initialized without a visible parent window hierarchy!
225 // So make shure that we are visible in every case!
226 this.setVisible(true);
227 mxFrame = FunctionHelper.createViewFrame(msName,maDocumentView);
229 else
230 mxFrame = FunctionHelper.createViewFrame(msName,null);
232 if(mxFrame!=null)
234 // start interception
235 maInterceptor = new Interceptor(mxFrame);
236 maInterceptor.startListening();
238 // start listening for status events and actualization
239 // of our status view
240 // (of course for our CustomizeView too)
241 maStatusView.setFrame (mxFrame);
242 maCustomizeView.setFrame(mxFrame);
244 // be listener for closing the remote target view frame
245 com.sun.star.lang.XComponent xBroadcaster = (com.sun.star.lang.XComponent)UnoRuntime.queryInterface(
246 com.sun.star.lang.XComponent.class,
247 mxFrame);
249 if(xBroadcaster!=null)
250 xBroadcaster.addEventListener(this);
254 // ____________________
257 * Different ways to load any URL from outside (may be by the command line)
258 * into this document view or to save it.
260 public void load(String sURL)
262 load(sURL,new com.sun.star.beans.PropertyValue[0]);
265 // ____________________
267 public void load(String sURL, com.sun.star.beans.PropertyValue[] lArguments)
269 com.sun.star.lang.XComponent xDocument = FunctionHelper.loadDocument(mxFrame,sURL,lArguments);
270 if(xDocument!=null)
272 mbtSave.setEnabled (true);
273 mbtExport.setEnabled(true);
275 else
277 mbtSave.setEnabled (false);
278 mbtExport.setEnabled(false);
282 // ____________________
284 public void save()
286 com.sun.star.frame.XController xController = mxFrame.getController();
287 if (xController==null)
288 return;
289 com.sun.star.frame.XModel xDocument = xController.getModel();
290 if (xDocument==null)
291 return;
292 FunctionHelper.saveDocument(xDocument);
295 // ____________________
297 public void exportHTML(String sURL)
299 com.sun.star.frame.XController xController = mxFrame.getController();
300 if (xController==null)
301 return;
302 com.sun.star.frame.XModel xDocument = xController.getModel();
303 if (xDocument==null)
304 return;
305 FunctionHelper.saveAsHTML(xDocument,sURL);
308 // ____________________
311 * Overridden so we can react for window closing of this view.
313 protected void processWindowEvent(WindowEvent aEvent)
315 if (aEvent.getID()!=WindowEvent.WINDOW_CLOSING)
317 super.processWindowEvent(aEvent);
319 else
320 if (FunctionHelper.closeFrame(mxFrame))
322 mxFrame = null;
323 shutdown();
324 super.processWindowEvent(aEvent);
328 // ____________________
331 * Here we can react for System.exit() normaly.
332 * But we use it for disposing() or windowClosing() too.
334 public void shutdown()
336 if (mbDead)
337 return;
338 mbDead=true;
340 // force these sub view to release her remote
341 // refrences too!
342 maStatusView.shutdown();
343 maCustomizeView.shutdown();
345 maStatusView = null;
346 maCustomizeView = null;
348 // disable all interceptions
349 maInterceptor.shutdown();
350 maInterceptor = null;
352 // close the frame and his document
353 // Relaesing of our listener connections for disposing()
354 // will be forced automaticly then. Because the frame
355 // will call us back ...
356 if (mxFrame!=null)
357 FunctionHelper.closeFrame(mxFrame);
359 // deregister this view in the global container
360 // Normaly we should die afterwards by garbage collection ...
361 // In cease this was the last view - it force a system.exit().
362 // But then we are no longer a member of the global container
363 // of possible shutdown listener ... and this method should be
364 // called again.
365 ViewContainer.getGlobalContainer().removeView(this);
368 // ____________________
371 * callback from our internal saved frame
372 * which wish to die. Its not neccessary to remove listener connections
373 * here. Because the broadcaster do it automaticly.
374 * We have to release all references to him only.
376 * @param aSource
377 * describe the broadcaster of this event
378 * Must be our internal saved frame.
380 public void disposing(com.sun.star.lang.EventObject aSource)
382 mxFrame = null;
385 // ____________________
388 * This inner class is used to react for events of our own UI controls.
389 * So we can start different actions then.
391 private class Reactor implements ActionListener
393 // ____________________
396 * This method react for pressed buttons or selected check boxes.
398 public void actionPerformed(ActionEvent aEvent)
400 String sCommand = aEvent.getActionCommand();
401 //-----------------------------
402 // open any file from disk
403 if( sCommand.compareTo(COMMAND_OPEN) == 0 )
405 String sURL = FunctionHelper.askUserForFileURL(DocumentView.this,true);
406 if(sURL!=null)
407 DocumentView.this.load(sURL);
409 else
410 //-----------------------------
411 // save current document
412 if( sCommand.compareTo(COMMAND_SAVE) == 0 )
414 DocumentView.this.save();
416 else
417 //-----------------------------
418 // export current document to html
419 if( sCommand.compareTo(COMMAND_EXPORT) == 0 )
421 String sURL = FunctionHelper.askUserForFileURL(DocumentView.this,false);
422 if(sURL!=null)
423 DocumentView.this.exportHTML(sURL);
425 else
426 //-----------------------------
427 // exit application
428 if( sCommand.compareTo(COMMAND_EXIT) == 0 )
430 // This will force deleting of this and
431 // all other currently opened views automaticly!
432 System.exit(0);