xref: /trunk/main/odk/examples/DevelopersGuide/OfficeBean/OOoBeanViewer.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
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.
20  *
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.
32  *
33  *************************************************************************/
34 
35 package com.sun.star.comp.beans;
36 
37 import javax.swing.filechooser.*;
38 import javax.swing.*;
39 import java.io.*;
40 
41 /** A simple Applet that contains the SimpleBean.
42  *
43  * This applet is a sample implementation of the
44  * OpenOffice.org bean.
45  * When initally loaded the applet has two buttons
46  * one for opening an existant file and one to open
47  * a blank document of a given type supported by
48  * OpenOffice.org eg. Writer, Calc, Impress, .....
49  *
50  */
51 
52 public class OOoBeanViewer extends java.applet.Applet
53 {
54 
55    /**
56     * Private variables declaration - GUI components
57     */
58    private java.awt.Panel rightPanel;
59    private java.awt.Panel bottomPanel;
60    private javax.swing.JButton closeButton;
61    private javax.swing.JButton terminateButton;
62    private javax.swing.JButton newDocumentButton;
63    private javax.swing.JPopupMenu documentTypePopUp;
64    private javax.swing.JCheckBox menuBarButton;
65    private javax.swing.JCheckBox mainBarButton;
66    private javax.swing.JCheckBox toolBarButton;
67    private javax.swing.JCheckBox statusBarButton;
68    private javax.swing.JButton storeDocumentButton;
69    private javax.swing.JButton loadDocumentButton;
70    private javax.swing.JButton syswinButton;
71    private JTextField documentURLTextField;
72    private JMenuItem item;
73    private JFileChooser fileChooser;
74    private byte buffer[];
75 
76    /**
77     * Private variables declaration - SimpleBean variables
78     */
79    private OOoBean aBean;
80 
81    /**
82     * Initialize the Appplet
83     */
84    public void init()
85    {
86         //The aBean needs to be initialized to add it to the applet
87         aBean = new OOoBean();
88 
89         //Initialize GUI components
90         rightPanel = new java.awt.Panel();
91         bottomPanel = new java.awt.Panel();
92         closeButton = new javax.swing.JButton("close");
93         terminateButton = new javax.swing.JButton("terminate");
94         newDocumentButton = new javax.swing.JButton("new document...");
95         documentTypePopUp = new javax.swing.JPopupMenu();
96         storeDocumentButton = new javax.swing.JButton("store to buffer");
97         loadDocumentButton = new javax.swing.JButton("load from buffer");
98         syswinButton = new javax.swing.JButton("release/aquire");
99 
100         menuBarButton = new javax.swing.JCheckBox("MenuBar");
101         menuBarButton.setSelected( aBean.isMenuBarVisible() );
102 
103         mainBarButton = new javax.swing.JCheckBox("MainBar");
104         mainBarButton.setSelected( aBean.isStandardBarVisible() );
105 
106         toolBarButton = new javax.swing.JCheckBox("ToolBar");
107         toolBarButton.setSelected( aBean.isToolBarVisible() );
108 
109         statusBarButton = new javax.swing.JCheckBox("StatusBar");
110         statusBarButton.setSelected( aBean.isStatusBarVisible() );
111 
112         documentURLTextField = new javax.swing.JTextField();
113 
114         //Set up the Popup Menu to create a blank document
115         documentTypePopUp.setToolTipText("Create an empty document");
116 
117         item = documentTypePopUp.add("Text Document");
118         item.addActionListener(new java.awt.event.ActionListener()
119         {
120             public void actionPerformed(java.awt.event.ActionEvent evt)
121             {
122                 createBlankDoc("private:factory/swriter",
123                     "New text document");
124             }
125         });
126 
127         item = documentTypePopUp.add("Presentation Document");
128         item.addActionListener(new java.awt.event.ActionListener()
129         {
130             public void actionPerformed(java.awt.event.ActionEvent evt)
131             {
132                 createBlankDoc("private:factory/simpress",
133                     "New presentation document");
134             }
135         });
136 
137         item = documentTypePopUp.add("Drawing Document");
138         item.addActionListener(new java.awt.event.ActionListener()
139         {
140             public void actionPerformed(java.awt.event.ActionEvent evt)
141             {
142                 createBlankDoc("private:factory/sdraw",
143                    "New drawing document");
144             }
145         });
146 
147         item = documentTypePopUp.add("Formula Document");
148         item.addActionListener(new java.awt.event.ActionListener()
149         {
150             public void actionPerformed(java.awt.event.ActionEvent evt)
151             {
152                 createBlankDoc("private:factory/smath",
153                     "New formula document");
154             }
155         });
156 
157         item = documentTypePopUp.add("Spreadsheet Document");
158         item.addActionListener(new java.awt.event.ActionListener()
159         {
160             public void actionPerformed(java.awt.event.ActionEvent evt)
161             {
162                 createBlankDoc("private:factory/scalc",
163                     "New spreadsheet document");
164             }
165         });
166 
167         syswinButton.addActionListener(
168                 new java.awt.event.ActionListener()
169         {
170             public void actionPerformed(java.awt.event.ActionEvent evt)
171             {
172                 try
173                 {
174                     aBean.releaseSystemWindow();
175                     aBean.aquireSystemWindow();
176                 }
177                 catch ( com.sun.star.comp.beans.NoConnectionException aExc )
178                 {}
179                 catch ( com.sun.star.comp.beans.SystemWindowException aExc )
180                 {}
181             }
182        });
183 
184         storeDocumentButton.addActionListener(
185                 new java.awt.event.ActionListener()
186         {
187             public void actionPerformed(java.awt.event.ActionEvent evt)
188             {
189                 try
190                 {
191                     buffer = aBean.storeToByteArray( null, null );
192                 }
193                 catch ( Throwable aExc )
194                 {
195                     System.err.println( "storeToBuffer failed: " + aExc );
196                     aExc.printStackTrace( System.err );
197                 }
198             }
199        });
200 
201         loadDocumentButton.addActionListener(
202                 new java.awt.event.ActionListener()
203         {
204             public void actionPerformed(java.awt.event.ActionEvent evt)
205             {
206                 try
207                 {
208                     aBean.loadFromByteArray( buffer, null );
209                 }
210                 catch ( Throwable aExc )
211                 {
212                     System.err.println( "loadFromBuffer failed: " + aExc );
213                     aExc.printStackTrace( System.err );
214                 }
215             }
216        });
217 
218        closeButton.addActionListener(new java.awt.event.ActionListener()
219        {
220             public void actionPerformed(java.awt.event.ActionEvent evt)
221             {
222                 close();
223             }
224        });
225 
226        terminateButton.addActionListener(new java.awt.event.ActionListener()
227        {
228             public void actionPerformed(java.awt.event.ActionEvent evt)
229             {
230                 terminate();
231             }
232        });
233 
234        newDocumentButton.addActionListener(new java.awt.event.ActionListener()
235        {
236             public void actionPerformed(java.awt.event.ActionEvent evt)
237             {
238                 documentTypePopUp.show((java.awt.Component)evt.getSource(), 0,0);
239             }
240        });
241 
242        menuBarButton.addActionListener(new java.awt.event.ActionListener()
243        {
244             public void actionPerformed(java.awt.event.ActionEvent evt)
245             {
246                 aBean.setMenuBarVisible( !aBean.isMenuBarVisible() );
247             }
248        });
249 
250        mainBarButton.addActionListener(new java.awt.event.ActionListener()
251        {
252             public void actionPerformed(java.awt.event.ActionEvent evt)
253             {
254                 aBean.setStandardBarVisible( !aBean.isStandardBarVisible() );
255             }
256        });
257 
258        toolBarButton.addActionListener(new java.awt.event.ActionListener()
259        {
260             public void actionPerformed(java.awt.event.ActionEvent evt)
261             {
262                 aBean.setToolBarVisible( !aBean.isToolBarVisible() );
263             }
264        });
265 
266        statusBarButton.addActionListener(new java.awt.event.ActionListener()
267        {
268             public void actionPerformed(java.awt.event.ActionEvent evt)
269             {
270                 aBean.setStatusBarVisible( !aBean.isStatusBarVisible() );
271             }
272        });
273 
274        documentURLTextField.setEditable(false);
275        documentURLTextField.setPreferredSize(new java.awt.Dimension(200, 30));
276 
277        rightPanel.setLayout( new java.awt.GridLayout(10,1) );
278        rightPanel.add(closeButton);
279        rightPanel.add(terminateButton);
280        rightPanel.add(newDocumentButton);
281        rightPanel.add(storeDocumentButton);
282        rightPanel.add(loadDocumentButton);
283        rightPanel.add(syswinButton);
284        rightPanel.add(menuBarButton);
285        rightPanel.add(mainBarButton);
286        rightPanel.add(toolBarButton);
287        rightPanel.add(statusBarButton);
288 
289        //bottomPanel.setLayout( new java.awt.GridLayout(1,1) );
290        bottomPanel.setLayout( new java.awt.BorderLayout() );
291        bottomPanel.add(documentURLTextField);
292 
293        setLayout(new java.awt.BorderLayout());
294 
295        add(aBean, java.awt.BorderLayout.CENTER);
296        add(rightPanel, java.awt.BorderLayout.EAST);
297        add(bottomPanel, java.awt.BorderLayout.SOUTH);
298    }
299 
300    /**
301     * Create a blank document of type <code>desc</code>
302     *
303     * @param url The private internal URL of the OpenOffice.org
304     *            document describing the document
305     * @param desc A description of the document to be created
306     */
307    private void createBlankDoc(String url, String desc)
308    {
309         //Create a blank document
310         try
311         {
312             documentURLTextField.setText(desc);
313             //Get the office process to load the URL
314             aBean.loadFromURL( url, null );
315 
316             aBean.aquireSystemWindow();
317         }
318         catch ( com.sun.star.comp.beans.SystemWindowException aExc )
319         {
320             System.err.println( "OOoBeanViewer.1:" );
321             aExc.printStackTrace();
322         }
323         catch ( com.sun.star.comp.beans.NoConnectionException aExc )
324         {
325             System.err.println( "OOoBeanViewer.2:" );
326             aExc.printStackTrace();
327         }
328         catch ( Exception aExc )
329         {
330             System.err.println( "OOoBeanViewer.3:" );
331             aExc.printStackTrace();
332             //return;
333         }
334     }
335 
336     /** closes the bean viewer, leaves OOo running.
337      */
338    private void close()
339    {
340             setVisible(false);
341             aBean.stopOOoConnection();
342             stop();
343             System.exit(0);
344    }
345 
346     /** closes the bean viewer and tries to terminate OOo.
347      */
348    private void terminate()
349    {
350             setVisible(false);
351             com.sun.star.frame.XDesktop xDesktop = null;
352             try {
353                 xDesktop = aBean.getOOoDesktop();
354             }
355             catch ( com.sun.star.comp.beans.NoConnectionException aExc ) {} // ignore
356             aBean.stopOOoConnection();
357             stop();
358             if ( xDesktop != null )
359                 xDesktop.terminate();
360             System.exit(0);
361    }
362 
363    /**
364     * An ExitListener listening for windowClosing events
365     */
366    private class ExitListener extends java.awt.event.WindowAdapter
367    {
368         /**
369          * windowClosed
370          *
371          * @param e A WindowEvent for a closed Window event
372          */
373         public void windowClosed( java.awt.event.WindowEvent e)
374         {
375             close();
376         }
377 
378         /**
379          * windowClosing for a closing window event
380          *
381          * @param e A WindowEvent for a closing window event
382          */
383         public void windowClosing( java.awt.event.WindowEvent e)
384         {
385             ((java.awt.Window)e.getSource()).dispose();
386         }
387    }
388 
389    public static void main(String args[])
390    {
391        java.awt.Frame frame = new java.awt.Frame("OpenOffice.org Demo");
392        OOoBeanViewer aViewer = new OOoBeanViewer();
393 
394        frame.setLayout(new java.awt.BorderLayout());
395 
396        frame.addWindowListener( aViewer.new ExitListener() );
397 
398        aViewer.init();
399        aViewer.start();
400 
401        frame.add(aViewer);
402        frame.setLocation( 200, 200 );
403        frame.setSize( 800, 480 );
404        frame.show();
405    }
406 }
407 
408