1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *************************************************************************/
35 package org
.libreoffice
.sdk
.examples
.developers_guide
.office_bean
;
37 import com
.sun
.star
.comp
.beans
.OOoBean
;
38 import javax
.swing
.filechooser
.*;
42 /** A simple Applet that contains the SimpleBean.
44 * This applet is a sample implementation of the
45 * OpenOffice.org bean.
46 * When initially loaded the applet has two buttons
47 * one for opening an existent file and one to open
48 * a blank document of a given type supported by
49 * OpenOffice.org eg. Writer, Calc, Impress, .....
53 public class OOoBeanViewer
extends java
.applet
.Applet
57 * Private variables declaration - GUI components
59 private java
.awt
.Panel rightPanel
;
60 private java
.awt
.Panel bottomPanel
;
61 private javax
.swing
.JButton closeButton
;
62 private javax
.swing
.JButton terminateButton
;
63 private javax
.swing
.JButton newDocumentButton
;
64 private javax
.swing
.JPopupMenu documentTypePopUp
;
65 private javax
.swing
.JCheckBox menuBarButton
;
66 private javax
.swing
.JCheckBox mainBarButton
;
67 private javax
.swing
.JCheckBox toolBarButton
;
68 private javax
.swing
.JCheckBox statusBarButton
;
69 private javax
.swing
.JButton storeDocumentButton
;
70 private javax
.swing
.JButton loadDocumentButton
;
71 private javax
.swing
.JButton syswinButton
;
72 private JTextField documentURLTextField
;
73 private JMenuItem item
;
74 private JFileChooser fileChooser
;
75 private byte buffer
[];
78 * Private variables declaration - SimpleBean variables
80 private OOoBean aBean
;
83 * Initialize the Appplet
87 //The aBean needs to be initialized to add it to the applet
88 aBean
= new OOoBean();
90 //Initialize GUI components
91 rightPanel
= new java
.awt
.Panel();
92 bottomPanel
= new java
.awt
.Panel();
93 closeButton
= new javax
.swing
.JButton("close");
94 terminateButton
= new javax
.swing
.JButton("terminate");
95 newDocumentButton
= new javax
.swing
.JButton("new document...");
96 documentTypePopUp
= new javax
.swing
.JPopupMenu();
97 storeDocumentButton
= new javax
.swing
.JButton("store to buffer");
98 loadDocumentButton
= new javax
.swing
.JButton("load from buffer");
99 syswinButton
= new javax
.swing
.JButton("release/acquire");
101 menuBarButton
= new javax
.swing
.JCheckBox("MenuBar");
102 menuBarButton
.setSelected( aBean
.isMenuBarVisible() );
104 mainBarButton
= new javax
.swing
.JCheckBox("MainBar");
105 mainBarButton
.setSelected( aBean
.isStandardBarVisible() );
107 toolBarButton
= new javax
.swing
.JCheckBox("ToolBar");
108 toolBarButton
.setSelected( aBean
.isToolBarVisible() );
110 statusBarButton
= new javax
.swing
.JCheckBox("StatusBar");
111 statusBarButton
.setSelected( aBean
.isStatusBarVisible() );
113 documentURLTextField
= new javax
.swing
.JTextField();
115 //Set up the Popup Menu to create a blank document
116 documentTypePopUp
.setToolTipText("Create an empty document");
118 item
= documentTypePopUp
.add("Text Document");
119 item
.addActionListener(new java
.awt
.event
.ActionListener()
121 public void actionPerformed(java
.awt
.event
.ActionEvent evt
)
123 createBlankDoc("private:factory/swriter",
124 "New text document");
128 item
= documentTypePopUp
.add("Presentation Document");
129 item
.addActionListener(new java
.awt
.event
.ActionListener()
131 public void actionPerformed(java
.awt
.event
.ActionEvent evt
)
133 createBlankDoc("private:factory/simpress",
134 "New presentation document");
138 item
= documentTypePopUp
.add("Drawing Document");
139 item
.addActionListener(new java
.awt
.event
.ActionListener()
141 public void actionPerformed(java
.awt
.event
.ActionEvent evt
)
143 createBlankDoc("private:factory/sdraw",
144 "New drawing document");
148 item
= documentTypePopUp
.add("Formula Document");
149 item
.addActionListener(new java
.awt
.event
.ActionListener()
151 public void actionPerformed(java
.awt
.event
.ActionEvent evt
)
153 createBlankDoc("private:factory/smath",
154 "New formula document");
158 item
= documentTypePopUp
.add("Spreadsheet Document");
159 item
.addActionListener(new java
.awt
.event
.ActionListener()
161 public void actionPerformed(java
.awt
.event
.ActionEvent evt
)
163 createBlankDoc("private:factory/scalc",
164 "New spreadsheet document");
168 syswinButton
.addActionListener(
169 new java
.awt
.event
.ActionListener()
171 public void actionPerformed(java
.awt
.event
.ActionEvent evt
)
175 aBean
.releaseSystemWindow();
176 aBean
.aquireSystemWindow();
178 catch ( com
.sun
.star
.comp
.beans
.NoConnectionException aExc
)
180 catch ( com
.sun
.star
.comp
.beans
.SystemWindowException aExc
)
185 storeDocumentButton
.addActionListener(
186 new java
.awt
.event
.ActionListener()
188 public void actionPerformed(java
.awt
.event
.ActionEvent evt
)
192 buffer
= aBean
.storeToByteArray( null, null );
194 catch ( Throwable aExc
)
196 System
.err
.println( "storeToBuffer failed: " + aExc
);
197 aExc
.printStackTrace( System
.err
);
202 loadDocumentButton
.addActionListener(
203 new java
.awt
.event
.ActionListener()
205 public void actionPerformed(java
.awt
.event
.ActionEvent evt
)
209 aBean
.loadFromByteArray( buffer
, null );
211 catch ( Throwable aExc
)
213 System
.err
.println( "loadFromBuffer failed: " + aExc
);
214 aExc
.printStackTrace( System
.err
);
219 closeButton
.addActionListener(new java
.awt
.event
.ActionListener()
221 public void actionPerformed(java
.awt
.event
.ActionEvent evt
)
227 terminateButton
.addActionListener(new java
.awt
.event
.ActionListener()
229 public void actionPerformed(java
.awt
.event
.ActionEvent evt
)
235 newDocumentButton
.addActionListener(new java
.awt
.event
.ActionListener()
237 public void actionPerformed(java
.awt
.event
.ActionEvent evt
)
239 documentTypePopUp
.show((java
.awt
.Component
)evt
.getSource(), 0,0);
243 menuBarButton
.addActionListener(new java
.awt
.event
.ActionListener()
245 public void actionPerformed(java
.awt
.event
.ActionEvent evt
)
247 aBean
.setMenuBarVisible( !aBean
.isMenuBarVisible() );
251 mainBarButton
.addActionListener(new java
.awt
.event
.ActionListener()
253 public void actionPerformed(java
.awt
.event
.ActionEvent evt
)
255 aBean
.setStandardBarVisible( !aBean
.isStandardBarVisible() );
259 toolBarButton
.addActionListener(new java
.awt
.event
.ActionListener()
261 public void actionPerformed(java
.awt
.event
.ActionEvent evt
)
263 aBean
.setToolBarVisible( !aBean
.isToolBarVisible() );
267 statusBarButton
.addActionListener(new java
.awt
.event
.ActionListener()
269 public void actionPerformed(java
.awt
.event
.ActionEvent evt
)
271 aBean
.setStatusBarVisible( !aBean
.isStatusBarVisible() );
275 documentURLTextField
.setEditable(false);
276 documentURLTextField
.setPreferredSize(new java
.awt
.Dimension(200, 30));
278 rightPanel
.setLayout( new java
.awt
.GridLayout(10,1) );
279 rightPanel
.add(closeButton
);
280 rightPanel
.add(terminateButton
);
281 rightPanel
.add(newDocumentButton
);
282 rightPanel
.add(storeDocumentButton
);
283 rightPanel
.add(loadDocumentButton
);
284 rightPanel
.add(syswinButton
);
285 rightPanel
.add(menuBarButton
);
286 rightPanel
.add(mainBarButton
);
287 rightPanel
.add(toolBarButton
);
288 rightPanel
.add(statusBarButton
);
290 //bottomPanel.setLayout( new java.awt.GridLayout(1,1) );
291 bottomPanel
.setLayout( new java
.awt
.BorderLayout() );
292 bottomPanel
.add(documentURLTextField
);
294 setLayout(new java
.awt
.BorderLayout());
296 add(aBean
, java
.awt
.BorderLayout
.CENTER
);
297 add(rightPanel
, java
.awt
.BorderLayout
.EAST
);
298 add(bottomPanel
, java
.awt
.BorderLayout
.SOUTH
);
302 * Create a blank document of type <code>desc</code>
304 * @param url The private internal URL of the OpenOffice.org
305 * document describing the document
306 * @param desc A description of the document to be created
308 private void createBlankDoc(String url
, String desc
)
310 //Create a blank document
313 documentURLTextField
.setText(desc
);
314 //Get the office process to load the URL
315 aBean
.loadFromURL( url
, null );
317 aBean
.aquireSystemWindow();
319 catch ( com
.sun
.star
.comp
.beans
.SystemWindowException aExc
)
321 System
.err
.println( "OOoBeanViewer.1:" );
322 aExc
.printStackTrace();
324 catch ( com
.sun
.star
.comp
.beans
.NoConnectionException aExc
)
326 System
.err
.println( "OOoBeanViewer.2:" );
327 aExc
.printStackTrace();
329 catch ( Exception aExc
)
331 System
.err
.println( "OOoBeanViewer.3:" );
332 aExc
.printStackTrace();
337 /** closes the bean viewer, leaves OOo running.
342 aBean
.stopOOoConnection();
347 /** closes the bean viewer and tries to terminate OOo.
349 private void terminate()
352 com
.sun
.star
.frame
.XDesktop xDesktop
= null;
354 xDesktop
= aBean
.getOOoDesktop();
356 catch ( com
.sun
.star
.comp
.beans
.NoConnectionException aExc
) {} // ignore
357 aBean
.stopOOoConnection();
359 if ( xDesktop
!= null )
360 xDesktop
.terminate();
365 * An ExitListener listening for windowClosing events
367 private class ExitListener
extends java
.awt
.event
.WindowAdapter
372 * @param e A WindowEvent for a closed Window event
374 public void windowClosed( java
.awt
.event
.WindowEvent e
)
380 * windowClosing for a closing window event
382 * @param e A WindowEvent for a closing window event
384 public void windowClosing( java
.awt
.event
.WindowEvent e
)
386 ((java
.awt
.Window
)e
.getSource()).dispose();
390 public static void main(String args
[])
392 java
.awt
.Frame frame
= new java
.awt
.Frame("OpenOffice.org Demo");
393 OOoBeanViewer aViewer
= new OOoBeanViewer();
395 frame
.setLayout(new java
.awt
.BorderLayout());
397 frame
.addWindowListener( aViewer
.new ExitListener() );
403 frame
.setLocation( 200, 200 );
404 frame
.setSize( 800, 480 );