merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / DevelopersGuide / OfficeDev / DesktopEnvironment / CustomizeView.java
blob03cff85dcfd54c1fa4cc3836842a5675a45d92d7
1 /*************************************************************************
3 * $RCSfile: CustomizeView.java,v $
5 * $Revision: 1.4 $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:37:25 $
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 *************************************************************************/
40 // __________ Imports __________
42 import com.sun.star.uno.UnoRuntime;
44 import java.awt.*;
45 import javax.swing.*;
46 import java.lang.String;
47 import java.awt.event.*;
48 import java.awt.*;
50 // __________ Implementation __________
52 /**
53 * Makes it possible to change some states of currently loaded
54 * document (e.g. enable/disable menubar, toolbar, objectbar)
56 * @author Andreas Schlüns
57 * @created 20.06.2002 09:28
59 public class CustomizeView extends JPanel
60 implements IShutdownListener
62 // ____________________
63 // const
65 /**
66 * These const URL's describe feature for toggling some properties of loaded document.
67 * Dispatch it with the corresponding parameter to the frame.
69 private static final String FEATUREURL_MENUBAR = "slot:6661" ;
70 private static final String FEATUREURL_TOOLBAR = "slot:5909" ;
71 private static final String FEATUREURL_OBJECTBAR = "slot:5905" ;
73 private static final String FEATUREPROP_MENUBAR = "MenuBarVisible" ;
74 private static final String FEATUREPROP_TOOLBAR = "ToolBarVisible" ;
75 private static final String FEATUREPROP_OBJECTBAR = "ObjectBarVisible" ;
77 private static final String ACTION_MENUBAR = "toogle_menu" ;
78 private static final String ACTION_TOOLBAR = "toogle_toolbar" ;
79 private static final String ACTION_OBJECTBAR = "toogle_objectbar" ;
81 private static final String MENUBAR_ON = "menubar on" ;
82 private static final String TOOLBAR_ON = "toolbar on" ;
83 private static final String OBJECTBAR_ON = "objectbar on" ;
85 private static final String MENUBAR_OFF = "menubar off" ;
86 private static final String TOOLBAR_OFF = "toolbar off" ;
87 private static final String OBJECTBAR_OFF = "objectbar off" ;
89 // ____________________
90 // member
92 /**
93 * @member m_cbMenuBar reference to checkbox for toggling menubar
94 * @member m_cbToolBar reference to checkbox for toggling toolbar
95 * @member m_cbObjectBar reference to checkbox for toggling objectbar
97 * @member m_aMenuBarListener listener for status events of the menu bar
98 * @member m_aToolBarListener listener for status events of the tool bar
99 * @member m_aObjectBarListener listener for status events of the object bar
101 private JCheckBox m_cbMenuBar ;
102 private JCheckBox m_cbToolBar ;
103 private JCheckBox m_cbObjectBar ;
105 private StatusListener m_aMenuBarListener ;
106 private StatusListener m_aToolBarListener ;
107 private StatusListener m_aObjectBarListener;
109 // ____________________
112 * ctor
113 * Create view controls on startup and initialize it.
114 * We don't start listening here. see setFrame()!
116 CustomizeView()
118 this.setLayout(new GridLayout(3,0));
120 m_cbMenuBar = new JCheckBox(MENUBAR_OFF , false);
121 m_cbToolBar = new JCheckBox(TOOLBAR_OFF , false);
122 m_cbObjectBar = new JCheckBox(OBJECTBAR_OFF, false);
124 m_cbMenuBar.setEnabled (false);
125 m_cbToolBar.setEnabled (false);
126 m_cbObjectBar.setEnabled(false);
128 m_cbMenuBar.setActionCommand (ACTION_MENUBAR );
129 m_cbToolBar.setActionCommand (ACTION_TOOLBAR );
130 m_cbObjectBar.setActionCommand(ACTION_OBJECTBAR);
132 this.add(m_cbMenuBar );
133 this.add(m_cbToolBar );
134 this.add(m_cbObjectBar);
137 // ____________________
140 * set new frame for this view
141 * We start listening for frame action/status and click events instandly.
142 * If an event occure we use it to synchronize our controls
143 * with states of a (my be) new document view of this frame.
145 * @param xFrame
146 * the reference to the frame, which provides the
147 * possibility to get the required status informations
149 * Attention: We don't accept new frames here.
150 * We get one after startup and work with him.
151 * That's it!
153 public void setFrame(com.sun.star.frame.XFrame xFrame)
155 if (xFrame==null)
156 return;
158 // be listener for click events
159 // They will toogle the UI controls.
160 ClickListener aMenuBarHandler = new ClickListener(FEATUREURL_MENUBAR ,FEATUREPROP_MENUBAR ,xFrame);
161 ClickListener aToolBarHandler = new ClickListener(FEATUREURL_TOOLBAR ,FEATUREPROP_TOOLBAR ,xFrame);
162 ClickListener aObjectBarHandler = new ClickListener(FEATUREURL_OBJECTBAR,FEATUREPROP_OBJECTBAR,xFrame);
164 m_cbMenuBar.addActionListener (aMenuBarHandler );
165 m_cbToolBar.addActionListener (aToolBarHandler );
166 m_cbObjectBar.addActionListener(aObjectBarHandler);
168 // be frame action listener
169 // The callback will update listener connections
170 // for status updates automaticly!
171 m_aMenuBarListener = new StatusListener(m_cbMenuBar ,MENUBAR_ON ,MENUBAR_OFF ,xFrame, FEATUREURL_MENUBAR );
172 m_aToolBarListener = new StatusListener(m_cbToolBar ,TOOLBAR_ON ,TOOLBAR_OFF ,xFrame, FEATUREURL_TOOLBAR );
173 m_aObjectBarListener = new StatusListener(m_cbObjectBar,OBJECTBAR_ON,OBJECTBAR_OFF,xFrame, FEATUREURL_OBJECTBAR);
175 m_aMenuBarListener.startListening();
176 m_aToolBarListener.startListening();
177 m_aObjectBarListener.startListening();
180 // ____________________
183 * react for click events of the used check boxes
184 * We use our internal set dispatch objects to
185 * call it. This calls toogle the menu/object- or toolbar.
186 * Note: Because we are listener status events too - hopefully
187 * we get a notification, if toogling was successfully or not.
188 * We use this information to update our check boxes again.
189 * But such update doesn't force (hopefully) an action event. Otherwhise
190 * we can produce a never ending recursion!
192 * @param aEvent
193 * describes the used check box and his current state
194 * we can use to dispatch the right URL to the office
196 class ClickListener implements ActionListener,
197 com.sun.star.lang.XEventListener
199 /// URL, to toogle the requested UI item
200 String m_sURL;
201 /// name of the property which must be used in combination with the URL
202 String m_sProp;
203 /// we must use this frame to dispatch a request
204 com.sun.star.frame.XFrame m_xFrame;
206 //_____________________
209 * ctor
210 * It initialize an instance of this clas only.
212 ClickListener( String sURL ,
213 String sProp ,
214 com.sun.star.frame.XFrame xFrame )
216 m_sURL = sURL ;
217 m_sProp = sProp ;
218 m_xFrame = xFrame;
221 //_____________________
224 * callback for action events
225 * Such events occure if somehwere click the
226 * JCheckBox control on which we are registered.
227 * Such events doesn't occure if we set it programmaticly
228 * (e.g. if we get status events to -> see class StatusListener too)
230 * @param aEvent
231 * describes the check box and his state
232 * we can use to toogle the requested office
233 * ressource.
235 public void actionPerformed(ActionEvent aEvent)
237 synchronized(this)
239 if (m_xFrame==null)
240 return;
243 // define parameters for following dispatch
244 boolean bState = ((JCheckBox)aEvent.getSource()).isSelected();
246 // prepare the dispatch
247 com.sun.star.util.URL aURL = FunctionHelper.parseURL(m_sURL);
248 if (aURL==null)
249 return;
251 com.sun.star.beans.PropertyValue[] lProperties = new com.sun.star.beans.PropertyValue[1];
252 lProperties[0] = new com.sun.star.beans.PropertyValue();
253 lProperties[0].Name = m_sProp;
254 lProperties[0].Value = new Boolean(bState);
256 // execute (dispatch) it into the frame
257 if (m_xFrame==null)
258 return;
259 FunctionHelper.execute(m_xFrame,aURL,lProperties,null);
262 // ____________________
265 * callback for disposing events
266 * Internaly we save a reference to an office frame.
267 * Of course he can die and inform us then. We should react
268 * and forget his reference.
270 * @param aEvent
271 * describes the source which fire this event
272 * Must be our internal saved frame. Otherwhise
273 * somewhere know us without a registration ...
275 public void disposing(com.sun.star.lang.EventObject aEvent)
277 synchronized(this)
279 m_xFrame = null;
284 // ____________________
287 * If this java application shutdown - we must cancel all current existing
288 * listener connections. Otherwhise the office will run into some
289 * DisposedExceptions if it tries to use these forgotten listener references.
290 * And of course it can die doing that.
291 * We are registered at a central object to be informed if the VM will exit.
292 * So we can react.
294 public void shutdown()
296 m_aMenuBarListener.shutdown();
297 m_aToolBarListener.shutdown();
298 m_aObjectBarListener.shutdown();
300 m_aMenuBarListener = null;
301 m_aToolBarListener = null;
302 m_aObjectBarListener = null;