Get the style color and number just once
[LibreOffice.git] / odk / examples / DevelopersGuide / OfficeDev / DesktopEnvironment / CustomizeView.java
blobf1942958547200a358f70c632ff8cda8856e1358
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 import java.awt.*;
37 import javax.swing.*;
38 import java.awt.event.*;
40 /**
41 * Makes it possible to change some states of currently loaded
42 * document (e.g. enable/disable menubar, toolbar, objectbar)
45 public class CustomizeView extends JPanel
46 implements IShutdownListener
48 /**
49 * These const URL's describe feature for toggling some properties of loaded document.
50 * Dispatch it with the corresponding parameter to the frame.
52 private static final String FEATUREURL_MENUBAR = "slot:6661" ;
53 private static final String FEATUREURL_TOOLBAR = "slot:5909" ;
54 private static final String FEATUREURL_OBJECTBAR = "slot:5905" ;
56 private static final String FEATUREPROP_MENUBAR = "MenuBarVisible" ;
57 private static final String FEATUREPROP_TOOLBAR = "ToolBarVisible" ;
58 private static final String FEATUREPROP_OBJECTBAR = "ObjectBarVisible" ;
60 private static final String ACTION_MENUBAR = "toggle_menu" ;
61 private static final String ACTION_TOOLBAR = "toggle_toolbar" ;
62 private static final String ACTION_OBJECTBAR = "toggle_objectbar" ;
64 private static final String MENUBAR_ON = "menubar on" ;
65 private static final String TOOLBAR_ON = "toolbar on" ;
66 private static final String OBJECTBAR_ON = "objectbar on" ;
68 private static final String MENUBAR_OFF = "menubar off" ;
69 private static final String TOOLBAR_OFF = "toolbar off" ;
70 private static final String OBJECTBAR_OFF = "objectbar off" ;
73 // member
75 /**
76 * @member m_cbMenuBar reference to checkbox for toggling menubar
77 * @member m_cbToolBar reference to checkbox for toggling toolbar
78 * @member m_cbObjectBar reference to checkbox for toggling objectbar
80 * @member m_aMenuBarListener listener for status events of the menu bar
81 * @member m_aToolBarListener listener for status events of the tool bar
82 * @member m_aObjectBarListener listener for status events of the object bar
84 private final JCheckBox m_cbMenuBar ;
85 private final JCheckBox m_cbToolBar ;
86 private final JCheckBox m_cbObjectBar ;
88 private StatusListener m_aMenuBarListener ;
89 private StatusListener m_aToolBarListener ;
90 private StatusListener m_aObjectBarListener;
94 /**
95 * ctor
96 * Create view controls on startup and initialize it.
97 * We don't start listening here. see setFrame()!
99 CustomizeView()
101 this.setLayout(new GridLayout(3,0));
103 m_cbMenuBar = new JCheckBox(MENUBAR_OFF , false);
104 m_cbToolBar = new JCheckBox(TOOLBAR_OFF , false);
105 m_cbObjectBar = new JCheckBox(OBJECTBAR_OFF, false);
107 m_cbMenuBar.setEnabled (false);
108 m_cbToolBar.setEnabled (false);
109 m_cbObjectBar.setEnabled(false);
111 m_cbMenuBar.setActionCommand (ACTION_MENUBAR );
112 m_cbToolBar.setActionCommand (ACTION_TOOLBAR );
113 m_cbObjectBar.setActionCommand(ACTION_OBJECTBAR);
115 this.add(m_cbMenuBar );
116 this.add(m_cbToolBar );
117 this.add(m_cbObjectBar);
123 * set new frame for this view
124 * We start listening for frame action/status and click events instandly.
125 * If an event occurs, we use it to synchronize our controls
126 * with states of a (maybe) new document view of this frame.
128 * @param xFrame
129 * the reference to the frame, which provides the
130 * possibility to get the required status information
132 * Attention: We don't accept new frames here.
133 * We get one after startup and work with it.
134 * That's it!
136 public void setFrame(com.sun.star.frame.XFrame xFrame)
138 if (xFrame==null)
139 return;
141 // be listener for click events
142 // They will toggle the UI controls.
143 ClickListener aMenuBarHandler = new ClickListener(FEATUREURL_MENUBAR ,FEATUREPROP_MENUBAR ,xFrame);
144 ClickListener aToolBarHandler = new ClickListener(FEATUREURL_TOOLBAR ,FEATUREPROP_TOOLBAR ,xFrame);
145 ClickListener aObjectBarHandler = new ClickListener(FEATUREURL_OBJECTBAR,FEATUREPROP_OBJECTBAR,xFrame);
147 m_cbMenuBar.addActionListener (aMenuBarHandler );
148 m_cbToolBar.addActionListener (aToolBarHandler );
149 m_cbObjectBar.addActionListener(aObjectBarHandler);
151 // be frame action listener
152 // The callback will update listener connections
153 // for status updates automatically!
154 m_aMenuBarListener = new StatusListener(m_cbMenuBar ,MENUBAR_ON ,MENUBAR_OFF ,xFrame, FEATUREURL_MENUBAR );
155 m_aToolBarListener = new StatusListener(m_cbToolBar ,TOOLBAR_ON ,TOOLBAR_OFF ,xFrame, FEATUREURL_TOOLBAR );
156 m_aObjectBarListener = new StatusListener(m_cbObjectBar,OBJECTBAR_ON,OBJECTBAR_OFF,xFrame, FEATUREURL_OBJECTBAR);
158 m_aMenuBarListener.startListening();
159 m_aToolBarListener.startListening();
160 m_aObjectBarListener.startListening();
166 * react for click events of the used check boxes
167 * We use our internal set dispatch objects to
168 * call it. This calls toggle the menu/object- or toolbar.
169 * Note: Because we are listener status events too - hopefully
170 * we get a notification, if toggling was successfully or not.
171 * We use this information to update our check boxes again.
172 * But such update doesn't force (hopefully) an action event. Otherwise
173 * we can produce a never ending recursion!
175 private class ClickListener implements ActionListener,
176 com.sun.star.lang.XEventListener
178 /// URL, to toggle the requested UI item
179 private final String m_sURL;
180 /// name of the property which must be used in combination with the URL
181 private final String m_sProp;
182 /// we must use this frame to dispatch a request
183 private com.sun.star.frame.XFrame m_xFrame;
188 * ctor
189 * It initialize an instance of this class only.
191 private ClickListener( String sURL ,
192 String sProp ,
193 com.sun.star.frame.XFrame xFrame )
195 m_sURL = sURL ;
196 m_sProp = sProp ;
197 m_xFrame = xFrame;
203 * callback for action events
204 * Such events occur, if someone clicked the
205 * JCheckBox control, on which we are registered.
206 * Such events do not occur, if we set it programmatically
207 * (e.g. if we get status events to -> see class StatusListener too)
209 * @param aEvent
210 * describes the check box and its state
211 * we can use to toggle the requested office
212 * resource.
214 public void actionPerformed(ActionEvent aEvent)
216 synchronized(this)
218 if (m_xFrame==null)
219 return;
222 // define parameters for following dispatch
223 boolean bState = ((JCheckBox)aEvent.getSource()).isSelected();
225 // prepare the dispatch
226 com.sun.star.util.URL aURL = FunctionHelper.parseURL(m_sURL);
227 if (aURL==null)
228 return;
230 com.sun.star.beans.PropertyValue[] lProperties = new com.sun.star.beans.PropertyValue[1];
231 lProperties[0] = new com.sun.star.beans.PropertyValue();
232 lProperties[0].Name = m_sProp;
233 lProperties[0].Value = Boolean.valueOf(bState);
235 // execute (dispatch) it into the frame
236 if (m_xFrame==null)
237 return;
238 FunctionHelper.execute(m_xFrame,aURL,lProperties,null);
244 * callback for disposing events
245 * Internally we save a reference to an office frame.
246 * Of course he can die and inform us then. We should react
247 * and forget his reference.
249 * @param aEvent
250 * describes the source which fire this event
251 * Must be our internal saved frame. Otherwise
252 * somewhere know us without a registration ...
254 public void disposing(com.sun.star.lang.EventObject aEvent)
256 synchronized(this)
258 m_xFrame = null;
266 * If this java application shutdown - we must cancel all current existing
267 * listener connections. Otherwise the office will run into some
268 * DisposedExceptions if it tries to use these forgotten listener references.
269 * And of course it can die doing that.
270 * We are registered at a central object to be informed if the VM will exit.
271 * So we can react.
273 public void shutdown()
275 m_aMenuBarListener.shutdown();
276 m_aToolBarListener.shutdown();
277 m_aObjectBarListener.shutdown();
279 m_aMenuBarListener = null;
280 m_aToolBarListener = null;
281 m_aObjectBarListener = null;
285 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */