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