merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / DevelopersGuide / OfficeBean / OOoBeanViewer.java
blobf57ad6a8df9cc1aa1633f21c30bfbbeacf4ec348
1 /*************************************************************************
3 * $RCSfile: OOoBeanViewer.java,v $
5 * $Revision: 1.3 $
7 * last change: $Author: vg $ $Date: 2005-02-16 16:22:52 $
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 package com.sun.star.comp.beans;
43 import javax.swing.filechooser.*;
44 import javax.swing.*;
45 import java.io.*;
47 /** A simple Applet that contains the SimpleBean.
49 * This applet is a sample implementation of the
50 * OpenOffice.org bean.
51 * When initally loaded the applet has two buttons
52 * one for opening an existant file and one to open
53 * a blank document of a given type supported by
54 * OpenOffice.org eg. Writer, Calc, Impress, .....
58 public class OOoBeanViewer extends java.applet.Applet
61 /**
62 * Private variables declaration - GUI components
64 private java.awt.Panel rightPanel;
65 private java.awt.Panel bottomPanel;
66 private javax.swing.JButton closeButton;
67 private javax.swing.JButton terminateButton;
68 private javax.swing.JButton newDocumentButton;
69 private javax.swing.JPopupMenu documentTypePopUp;
70 private javax.swing.JCheckBox menuBarButton;
71 private javax.swing.JCheckBox mainBarButton;
72 private javax.swing.JCheckBox toolBarButton;
73 private javax.swing.JCheckBox statusBarButton;
74 private javax.swing.JButton storeDocumentButton;
75 private javax.swing.JButton loadDocumentButton;
76 private javax.swing.JButton syswinButton;
77 private JTextField documentURLTextField;
78 private JMenuItem item;
79 private JFileChooser fileChooser;
80 private byte buffer[];
82 /**
83 * Private variables declaration - SimpleBean variables
85 private OOoBean aBean;
87 /**
88 * Initialize the Appplet
90 public void init()
92 //The aBean needs to be initialized to add it to the applet
93 aBean = new OOoBean();
95 //Initialize GUI components
96 rightPanel = new java.awt.Panel();
97 bottomPanel = new java.awt.Panel();
98 closeButton = new javax.swing.JButton("close");
99 terminateButton = new javax.swing.JButton("terminate");
100 newDocumentButton = new javax.swing.JButton("new document...");
101 documentTypePopUp = new javax.swing.JPopupMenu();
102 storeDocumentButton = new javax.swing.JButton("store to buffer");
103 loadDocumentButton = new javax.swing.JButton("load from buffer");
104 syswinButton = new javax.swing.JButton("release/aquire");
106 menuBarButton = new javax.swing.JCheckBox("MenuBar");
107 menuBarButton.setSelected( aBean.isMenuBarVisible() );
109 mainBarButton = new javax.swing.JCheckBox("MainBar");
110 mainBarButton.setSelected( aBean.isStandardBarVisible() );
112 toolBarButton = new javax.swing.JCheckBox("ToolBar");
113 toolBarButton.setSelected( aBean.isToolBarVisible() );
115 statusBarButton = new javax.swing.JCheckBox("StatusBar");
116 statusBarButton.setSelected( aBean.isStatusBarVisible() );
118 documentURLTextField = new javax.swing.JTextField();
120 //Set up the Popup Menu to create a blank document
121 documentTypePopUp.setToolTipText("Create an empty document");
123 item = documentTypePopUp.add("Text Document");
124 item.addActionListener(new java.awt.event.ActionListener()
126 public void actionPerformed(java.awt.event.ActionEvent evt)
128 createBlankDoc("private:factory/swriter",
129 "New text document");
133 item = documentTypePopUp.add("Presentation Document");
134 item.addActionListener(new java.awt.event.ActionListener()
136 public void actionPerformed(java.awt.event.ActionEvent evt)
138 createBlankDoc("private:factory/simpress",
139 "New presentation document");
143 item = documentTypePopUp.add("Drawing Document");
144 item.addActionListener(new java.awt.event.ActionListener()
146 public void actionPerformed(java.awt.event.ActionEvent evt)
148 createBlankDoc("private:factory/sdraw",
149 "New drawing document");
153 item = documentTypePopUp.add("Formula Document");
154 item.addActionListener(new java.awt.event.ActionListener()
156 public void actionPerformed(java.awt.event.ActionEvent evt)
158 createBlankDoc("private:factory/smath",
159 "New formula document");
163 item = documentTypePopUp.add("Spreadsheet Document");
164 item.addActionListener(new java.awt.event.ActionListener()
166 public void actionPerformed(java.awt.event.ActionEvent evt)
168 createBlankDoc("private:factory/scalc",
169 "New spreadsheet document");
173 syswinButton.addActionListener(
174 new java.awt.event.ActionListener()
176 public void actionPerformed(java.awt.event.ActionEvent evt)
178 try
180 aBean.releaseSystemWindow();
181 aBean.aquireSystemWindow();
183 catch ( com.sun.star.comp.beans.NoConnectionException aExc )
185 catch ( com.sun.star.comp.beans.SystemWindowException aExc )
190 storeDocumentButton.addActionListener(
191 new java.awt.event.ActionListener()
193 public void actionPerformed(java.awt.event.ActionEvent evt)
195 try
197 buffer = aBean.storeToByteArray( null, null );
199 catch ( Throwable aExc )
201 System.err.println( "storeToBuffer failed: " + aExc );
202 aExc.printStackTrace( System.err );
207 loadDocumentButton.addActionListener(
208 new java.awt.event.ActionListener()
210 public void actionPerformed(java.awt.event.ActionEvent evt)
212 try
214 aBean.loadFromByteArray( buffer, null );
216 catch ( Throwable aExc )
218 System.err.println( "loadFromBuffer failed: " + aExc );
219 aExc.printStackTrace( System.err );
224 closeButton.addActionListener(new java.awt.event.ActionListener()
226 public void actionPerformed(java.awt.event.ActionEvent evt)
228 close();
232 terminateButton.addActionListener(new java.awt.event.ActionListener()
234 public void actionPerformed(java.awt.event.ActionEvent evt)
236 terminate();
240 newDocumentButton.addActionListener(new java.awt.event.ActionListener()
242 public void actionPerformed(java.awt.event.ActionEvent evt)
244 documentTypePopUp.show((java.awt.Component)evt.getSource(), 0,0);
248 menuBarButton.addActionListener(new java.awt.event.ActionListener()
250 public void actionPerformed(java.awt.event.ActionEvent evt)
252 aBean.setMenuBarVisible( !aBean.isMenuBarVisible() );
256 mainBarButton.addActionListener(new java.awt.event.ActionListener()
258 public void actionPerformed(java.awt.event.ActionEvent evt)
260 aBean.setStandardBarVisible( !aBean.isStandardBarVisible() );
264 toolBarButton.addActionListener(new java.awt.event.ActionListener()
266 public void actionPerformed(java.awt.event.ActionEvent evt)
268 aBean.setToolBarVisible( !aBean.isToolBarVisible() );
272 statusBarButton.addActionListener(new java.awt.event.ActionListener()
274 public void actionPerformed(java.awt.event.ActionEvent evt)
276 aBean.setStatusBarVisible( !aBean.isStatusBarVisible() );
280 documentURLTextField.setEditable(false);
281 documentURLTextField.setPreferredSize(new java.awt.Dimension(200, 30));
283 rightPanel.setLayout( new java.awt.GridLayout(10,1) );
284 rightPanel.add(closeButton);
285 rightPanel.add(terminateButton);
286 rightPanel.add(newDocumentButton);
287 rightPanel.add(storeDocumentButton);
288 rightPanel.add(loadDocumentButton);
289 rightPanel.add(syswinButton);
290 rightPanel.add(menuBarButton);
291 rightPanel.add(mainBarButton);
292 rightPanel.add(toolBarButton);
293 rightPanel.add(statusBarButton);
295 //bottomPanel.setLayout( new java.awt.GridLayout(1,1) );
296 bottomPanel.setLayout( new java.awt.BorderLayout() );
297 bottomPanel.add(documentURLTextField);
299 setLayout(new java.awt.BorderLayout());
301 add(aBean, java.awt.BorderLayout.CENTER);
302 add(rightPanel, java.awt.BorderLayout.EAST);
303 add(bottomPanel, java.awt.BorderLayout.SOUTH);
307 * Create a blank document of type <code>desc</code>
309 * @param url The private internal URL of the OpenOffice.org
310 * document describing the document
311 * @param desc A description of the document to be created
313 private void createBlankDoc(String url, String desc)
315 //Create a blank document
318 documentURLTextField.setText(desc);
319 //Get the office process to load the URL
320 aBean.loadFromURL( url, null );
322 aBean.aquireSystemWindow();
324 catch ( com.sun.star.comp.beans.SystemWindowException aExc )
326 System.err.println( "OOoBeanViewer.1:" );
327 aExc.printStackTrace();
329 catch ( com.sun.star.comp.beans.NoConnectionException aExc )
331 System.err.println( "OOoBeanViewer.2:" );
332 aExc.printStackTrace();
334 catch ( Exception aExc )
336 System.err.println( "OOoBeanViewer.3:" );
337 aExc.printStackTrace();
338 //return;
342 /** closes the bean viewer, leaves OOo running.
344 private void close()
346 setVisible(false);
347 aBean.stopOOoConnection();
348 stop();
349 System.exit(0);
352 /** closes the bean viewer and tries to terminate OOo.
354 private void terminate()
356 setVisible(false);
357 com.sun.star.frame.XDesktop xDesktop = null;
358 try {
359 xDesktop = aBean.getOOoDesktop();
361 catch ( com.sun.star.comp.beans.NoConnectionException aExc ) {} // ignore
362 aBean.stopOOoConnection();
363 stop();
364 if ( xDesktop != null )
365 xDesktop.terminate();
366 System.exit(0);
370 * An ExitListener listening for windowClosing events
372 private class ExitListener extends java.awt.event.WindowAdapter
375 * windowClosed
377 * @param e A WindowEvent for a closed Window event
379 public void windowClosed( java.awt.event.WindowEvent e)
381 close();
385 * windowClosing for a closing window event
387 * @param e A WindowEvent for a closing window event
389 public void windowClosing( java.awt.event.WindowEvent e)
391 ((java.awt.Window)e.getSource()).dispose();
395 public static void main(String args[])
397 java.awt.Frame frame = new java.awt.Frame("OpenOffice.org Demo");
398 OOoBeanViewer aViewer = new OOoBeanViewer();
400 frame.setLayout(new java.awt.BorderLayout());
402 frame.addWindowListener( aViewer.new ExitListener() );
404 aViewer.init();
405 aViewer.start();
407 frame.add(aViewer);
408 frame.setLocation( 200, 200 );
409 frame.setSize( 800, 480 );
410 frame.show();