bump product version to 7.2.5.1
[LibreOffice.git] / odk / examples / DevelopersGuide / OfficeBean / OOoBeanViewer.java
blobf87eb36786eafc8bf5eec96849874bcc1e355cc3
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 package org.libreoffice.sdk.examples.developers_guide.office_bean;
38 import com.sun.star.comp.beans.OOoBean;
39 import javax.swing.filechooser.*;
40 import javax.swing.*;
41 import java.io.*;
43 /* A simple Applet that contains the SimpleBean.
45 * This applet is a sample implementation of the
46 * OpenOffice.org bean.
47 * When initially loaded the applet has two buttons
48 * one for opening an existent file and one to open
49 * a blank document of a given type supported by
50 * OpenOffice.org eg. Writer, Calc, Impress, ...
54 public class OOoBeanViewer extends java.applet.Applet
57 /**
58 * Private variables declaration - GUI components
60 private java.awt.Panel rightPanel;
61 private java.awt.Panel bottomPanel;
62 private javax.swing.JButton closeButton;
63 private javax.swing.JButton terminateButton;
64 private javax.swing.JButton newDocumentButton;
65 private javax.swing.JPopupMenu documentTypePopUp;
66 private javax.swing.JCheckBox menuBarButton;
67 private javax.swing.JCheckBox mainBarButton;
68 private javax.swing.JCheckBox toolBarButton;
69 private javax.swing.JCheckBox statusBarButton;
70 private javax.swing.JButton storeDocumentButton;
71 private javax.swing.JButton loadDocumentButton;
72 private javax.swing.JButton syswinButton;
73 private JTextField documentURLTextField;
74 private JMenuItem item;
75 private JFileChooser fileChooser;
76 private byte buffer[];
78 /**
79 * Private variables declaration - SimpleBean variables
81 private OOoBean aBean;
83 /**
84 * Initialize the Applet
86 public void init()
88 //The aBean needs to be initialized to add it to the applet
89 aBean = new OOoBean();
91 //Initialize GUI components
92 rightPanel = new java.awt.Panel();
93 bottomPanel = new java.awt.Panel();
94 closeButton = new javax.swing.JButton("close");
95 terminateButton = new javax.swing.JButton("terminate");
96 newDocumentButton = new javax.swing.JButton("new document...");
97 documentTypePopUp = new javax.swing.JPopupMenu();
98 storeDocumentButton = new javax.swing.JButton("store to buffer");
99 loadDocumentButton = new javax.swing.JButton("load from buffer");
100 syswinButton = new javax.swing.JButton("release/acquire");
102 menuBarButton = new javax.swing.JCheckBox("MenuBar");
103 menuBarButton.setSelected( aBean.isMenuBarVisible() );
105 mainBarButton = new javax.swing.JCheckBox("MainBar");
106 mainBarButton.setSelected( aBean.isStandardBarVisible() );
108 toolBarButton = new javax.swing.JCheckBox("ToolBar");
109 toolBarButton.setSelected( aBean.isToolBarVisible() );
111 statusBarButton = new javax.swing.JCheckBox("StatusBar");
112 statusBarButton.setSelected( aBean.isStatusBarVisible() );
114 documentURLTextField = new javax.swing.JTextField();
116 //Set up the Popup Menu to create a blank document
117 documentTypePopUp.setToolTipText("Create an empty document");
119 item = documentTypePopUp.add("Text Document");
120 item.addActionListener(new java.awt.event.ActionListener()
122 public void actionPerformed(java.awt.event.ActionEvent evt)
124 createBlankDoc("private:factory/swriter",
125 "New text document");
129 item = documentTypePopUp.add("Presentation Document");
130 item.addActionListener(new java.awt.event.ActionListener()
132 public void actionPerformed(java.awt.event.ActionEvent evt)
134 createBlankDoc("private:factory/simpress",
135 "New presentation document");
139 item = documentTypePopUp.add("Drawing Document");
140 item.addActionListener(new java.awt.event.ActionListener()
142 public void actionPerformed(java.awt.event.ActionEvent evt)
144 createBlankDoc("private:factory/sdraw",
145 "New drawing document");
149 item = documentTypePopUp.add("Formula Document");
150 item.addActionListener(new java.awt.event.ActionListener()
152 public void actionPerformed(java.awt.event.ActionEvent evt)
154 createBlankDoc("private:factory/smath",
155 "New formula document");
159 item = documentTypePopUp.add("Spreadsheet Document");
160 item.addActionListener(new java.awt.event.ActionListener()
162 public void actionPerformed(java.awt.event.ActionEvent evt)
164 createBlankDoc("private:factory/scalc",
165 "New spreadsheet document");
169 syswinButton.addActionListener(
170 new java.awt.event.ActionListener()
172 public void actionPerformed(java.awt.event.ActionEvent evt)
176 aBean.releaseSystemWindow();
177 aBean.aquireSystemWindow();
179 catch ( com.sun.star.comp.beans.NoConnectionException aExc )
181 catch ( com.sun.star.comp.beans.SystemWindowException aExc )
186 storeDocumentButton.addActionListener(
187 new java.awt.event.ActionListener()
189 public void actionPerformed(java.awt.event.ActionEvent evt)
193 buffer = aBean.storeToByteArray( null, null );
195 catch ( Throwable aExc )
197 System.err.println( "storeToBuffer failed: " + aExc );
198 aExc.printStackTrace( System.err );
203 loadDocumentButton.addActionListener(
204 new java.awt.event.ActionListener()
206 public void actionPerformed(java.awt.event.ActionEvent evt)
210 aBean.loadFromByteArray( buffer, null );
212 catch ( Throwable aExc )
214 System.err.println( "loadFromBuffer failed: " + aExc );
215 aExc.printStackTrace( System.err );
220 closeButton.addActionListener(new java.awt.event.ActionListener()
222 public void actionPerformed(java.awt.event.ActionEvent evt)
224 close();
228 terminateButton.addActionListener(new java.awt.event.ActionListener()
230 public void actionPerformed(java.awt.event.ActionEvent evt)
232 terminate();
236 newDocumentButton.addActionListener(new java.awt.event.ActionListener()
238 public void actionPerformed(java.awt.event.ActionEvent evt)
240 documentTypePopUp.show((java.awt.Component)evt.getSource(), 0,0);
244 menuBarButton.addActionListener(new java.awt.event.ActionListener()
246 public void actionPerformed(java.awt.event.ActionEvent evt)
248 aBean.setMenuBarVisible( !aBean.isMenuBarVisible() );
252 mainBarButton.addActionListener(new java.awt.event.ActionListener()
254 public void actionPerformed(java.awt.event.ActionEvent evt)
256 aBean.setStandardBarVisible( !aBean.isStandardBarVisible() );
260 toolBarButton.addActionListener(new java.awt.event.ActionListener()
262 public void actionPerformed(java.awt.event.ActionEvent evt)
264 aBean.setToolBarVisible( !aBean.isToolBarVisible() );
268 statusBarButton.addActionListener(new java.awt.event.ActionListener()
270 public void actionPerformed(java.awt.event.ActionEvent evt)
272 aBean.setStatusBarVisible( !aBean.isStatusBarVisible() );
276 documentURLTextField.setEditable(false);
277 documentURLTextField.setPreferredSize(new java.awt.Dimension(200, 30));
279 rightPanel.setLayout( new java.awt.GridLayout(10,1) );
280 rightPanel.add(closeButton);
281 rightPanel.add(terminateButton);
282 rightPanel.add(newDocumentButton);
283 rightPanel.add(storeDocumentButton);
284 rightPanel.add(loadDocumentButton);
285 rightPanel.add(syswinButton);
286 rightPanel.add(menuBarButton);
287 rightPanel.add(mainBarButton);
288 rightPanel.add(toolBarButton);
289 rightPanel.add(statusBarButton);
291 //bottomPanel.setLayout( new java.awt.GridLayout(1,1) );
292 bottomPanel.setLayout( new java.awt.BorderLayout() );
293 bottomPanel.add(documentURLTextField);
295 setLayout(new java.awt.BorderLayout());
297 add(aBean, java.awt.BorderLayout.CENTER);
298 add(rightPanel, java.awt.BorderLayout.EAST);
299 add(bottomPanel, java.awt.BorderLayout.SOUTH);
303 * Create a blank document of type <code>desc</code>
305 * @param url The private internal URL of the OpenOffice.org
306 * document describing the document
307 * @param desc A description of the document to be created
309 private void createBlankDoc(String url, String desc)
311 //Create a blank document
314 documentURLTextField.setText(desc);
315 //Get the office process to load the URL
316 aBean.loadFromURL( url, null );
318 aBean.aquireSystemWindow();
320 catch ( com.sun.star.comp.beans.SystemWindowException aExc )
322 System.err.println( "OOoBeanViewer.1:" );
323 aExc.printStackTrace();
325 catch ( com.sun.star.comp.beans.NoConnectionException aExc )
327 System.err.println( "OOoBeanViewer.2:" );
328 aExc.printStackTrace();
330 catch ( Exception aExc )
332 System.err.println( "OOoBeanViewer.3:" );
333 aExc.printStackTrace();
334 //return;
338 /** closes the bean viewer, leaves OOo running.
340 private void close()
342 setVisible(false);
343 aBean.stopOOoConnection();
344 stop();
345 System.exit(0);
348 /** closes the bean viewer and tries to terminate OOo.
350 private void terminate()
352 setVisible(false);
353 com.sun.star.frame.XDesktop xDesktop = null;
354 try {
355 xDesktop = aBean.getOOoDesktop();
357 catch ( com.sun.star.comp.beans.NoConnectionException aExc ) {} // ignore
358 aBean.stopOOoConnection();
359 stop();
360 if ( xDesktop != null )
361 xDesktop.terminate();
362 System.exit(0);
366 * An ExitListener listening for windowClosing events
368 private class ExitListener extends java.awt.event.WindowAdapter
371 * windowClosed
373 * @param e A WindowEvent for a closed Window event
375 public void windowClosed( java.awt.event.WindowEvent e)
377 close();
381 * windowClosing for a closing window event
383 * @param e A WindowEvent for a closing window event
385 public void windowClosing( java.awt.event.WindowEvent e)
387 ((java.awt.Window)e.getSource()).dispose();
391 public static void main(String args[])
393 java.awt.Frame frame = new java.awt.Frame("OpenOffice.org Demo");
394 OOoBeanViewer aViewer = new OOoBeanViewer();
396 frame.setLayout(new java.awt.BorderLayout());
398 frame.addWindowListener( aViewer.new ExitListener() );
400 aViewer.init();
401 aViewer.start();
403 frame.add(aViewer);
404 frame.setLocation( 200, 200 );
405 frame.setSize( 800, 480 );
406 frame.show();
410 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */