1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir // __________ Imports __________
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir import java.awt.*;
39*cdf0e10cSrcweir import javax.swing.*;
40*cdf0e10cSrcweir import java.lang.String;
41*cdf0e10cSrcweir import java.awt.event.*;
42*cdf0e10cSrcweir import java.awt.*;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir // __________ Implementation __________
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir /**
47*cdf0e10cSrcweir  * Makes it possible to change some states of currently loaded
48*cdf0e10cSrcweir  * document (e.g. enable/disable menubar, toolbar, objectbar)
49*cdf0e10cSrcweir  *
50*cdf0e10cSrcweir  * @author     Andreas Schlüns
51*cdf0e10cSrcweir  * @created    20.06.2002 09:28
52*cdf0e10cSrcweir  */
53*cdf0e10cSrcweir public class CustomizeView extends    JPanel
54*cdf0e10cSrcweir                            implements IShutdownListener
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir     // ____________________
57*cdf0e10cSrcweir     // const
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     /**
60*cdf0e10cSrcweir      * These const URL's describe feature for toggling some properties of loaded document.
61*cdf0e10cSrcweir      * Dispatch it with the corresponding parameter to the frame.
62*cdf0e10cSrcweir      */
63*cdf0e10cSrcweir     private static final String FEATUREURL_MENUBAR      = "slot:6661"         ;
64*cdf0e10cSrcweir     private static final String FEATUREURL_TOOLBAR      = "slot:5909"         ;
65*cdf0e10cSrcweir     private static final String FEATUREURL_OBJECTBAR    = "slot:5905"         ;
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir     private static final String FEATUREPROP_MENUBAR     = "MenuBarVisible"    ;
68*cdf0e10cSrcweir     private static final String FEATUREPROP_TOOLBAR     = "ToolBarVisible"    ;
69*cdf0e10cSrcweir     private static final String FEATUREPROP_OBJECTBAR   = "ObjectBarVisible"  ;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir     private static final String ACTION_MENUBAR          = "toogle_menu"       ;
72*cdf0e10cSrcweir     private static final String ACTION_TOOLBAR          = "toogle_toolbar"    ;
73*cdf0e10cSrcweir     private static final String ACTION_OBJECTBAR        = "toogle_objectbar"  ;
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir     private static final String MENUBAR_ON              = "menubar on"        ;
76*cdf0e10cSrcweir     private static final String TOOLBAR_ON              = "toolbar on"        ;
77*cdf0e10cSrcweir     private static final String OBJECTBAR_ON            = "objectbar on"      ;
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir     private static final String MENUBAR_OFF             = "menubar off"       ;
80*cdf0e10cSrcweir     private static final String TOOLBAR_OFF             = "toolbar off"       ;
81*cdf0e10cSrcweir     private static final String OBJECTBAR_OFF           = "objectbar off"     ;
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir     // ____________________
84*cdf0e10cSrcweir     // member
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir     /**
87*cdf0e10cSrcweir      * @member  m_cbMenuBar             reference to checkbox for toggling menubar
88*cdf0e10cSrcweir      * @member  m_cbToolBar             reference to checkbox for toggling toolbar
89*cdf0e10cSrcweir      * @member  m_cbObjectBar           reference to checkbox for toggling objectbar
90*cdf0e10cSrcweir      *
91*cdf0e10cSrcweir      * @member  m_aMenuBarListener      listener for status events of the menu bar
92*cdf0e10cSrcweir      * @member  m_aToolBarListener      listener for status events of the tool bar
93*cdf0e10cSrcweir      * @member  m_aObjectBarListener    listener for status events of the object bar
94*cdf0e10cSrcweir      */
95*cdf0e10cSrcweir     private JCheckBox           m_cbMenuBar         ;
96*cdf0e10cSrcweir     private JCheckBox           m_cbToolBar         ;
97*cdf0e10cSrcweir     private JCheckBox           m_cbObjectBar       ;
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir     private StatusListener      m_aMenuBarListener  ;
100*cdf0e10cSrcweir     private StatusListener      m_aToolBarListener  ;
101*cdf0e10cSrcweir     private StatusListener      m_aObjectBarListener;
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir     // ____________________
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     /**
106*cdf0e10cSrcweir      * ctor
107*cdf0e10cSrcweir      * Create view controls on startup and initialize it.
108*cdf0e10cSrcweir      * We don't start listening here. see setFrame()!
109*cdf0e10cSrcweir      */
110*cdf0e10cSrcweir     CustomizeView()
111*cdf0e10cSrcweir     {
112*cdf0e10cSrcweir         this.setLayout(new GridLayout(3,0));
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir         m_cbMenuBar   = new JCheckBox(MENUBAR_OFF  , false);
115*cdf0e10cSrcweir         m_cbToolBar   = new JCheckBox(TOOLBAR_OFF  , false);
116*cdf0e10cSrcweir         m_cbObjectBar = new JCheckBox(OBJECTBAR_OFF, false);
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir         m_cbMenuBar.setEnabled  (false);
119*cdf0e10cSrcweir         m_cbToolBar.setEnabled  (false);
120*cdf0e10cSrcweir         m_cbObjectBar.setEnabled(false);
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir         m_cbMenuBar.setActionCommand  (ACTION_MENUBAR  );
123*cdf0e10cSrcweir         m_cbToolBar.setActionCommand  (ACTION_TOOLBAR  );
124*cdf0e10cSrcweir         m_cbObjectBar.setActionCommand(ACTION_OBJECTBAR);
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir         this.add(m_cbMenuBar  );
127*cdf0e10cSrcweir         this.add(m_cbToolBar  );
128*cdf0e10cSrcweir         this.add(m_cbObjectBar);
129*cdf0e10cSrcweir     }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir     // ____________________
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir     /**
134*cdf0e10cSrcweir      * set new frame for this view
135*cdf0e10cSrcweir      * We start listening for frame action/status and click events instandly.
136*cdf0e10cSrcweir      * If an event occure we use it to synchronize our controls
137*cdf0e10cSrcweir      * with states of a (my be) new document view of this frame.
138*cdf0e10cSrcweir      *
139*cdf0e10cSrcweir      * @param xFrame
140*cdf0e10cSrcweir      *          the reference to the frame, which provides the
141*cdf0e10cSrcweir      *          possibility to get the required status informations
142*cdf0e10cSrcweir      *
143*cdf0e10cSrcweir      *          Attention: We don't accept new frames here.
144*cdf0e10cSrcweir      *          We get one after startup and work with him.
145*cdf0e10cSrcweir      *          That's it!
146*cdf0e10cSrcweir      */
147*cdf0e10cSrcweir     public void setFrame(com.sun.star.frame.XFrame xFrame)
148*cdf0e10cSrcweir     {
149*cdf0e10cSrcweir         if (xFrame==null)
150*cdf0e10cSrcweir             return;
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir         // be listener for click events
153*cdf0e10cSrcweir         // They will toogle the UI controls.
154*cdf0e10cSrcweir         ClickListener aMenuBarHandler   = new ClickListener(FEATUREURL_MENUBAR  ,FEATUREPROP_MENUBAR  ,xFrame);
155*cdf0e10cSrcweir         ClickListener aToolBarHandler   = new ClickListener(FEATUREURL_TOOLBAR  ,FEATUREPROP_TOOLBAR  ,xFrame);
156*cdf0e10cSrcweir         ClickListener aObjectBarHandler = new ClickListener(FEATUREURL_OBJECTBAR,FEATUREPROP_OBJECTBAR,xFrame);
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir         m_cbMenuBar.addActionListener  (aMenuBarHandler  );
159*cdf0e10cSrcweir         m_cbToolBar.addActionListener  (aToolBarHandler  );
160*cdf0e10cSrcweir         m_cbObjectBar.addActionListener(aObjectBarHandler);
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir         // be frame action listener
163*cdf0e10cSrcweir         // The callback will update listener connections
164*cdf0e10cSrcweir         // for status updates automaticly!
165*cdf0e10cSrcweir         m_aMenuBarListener   = new StatusListener(m_cbMenuBar  ,MENUBAR_ON  ,MENUBAR_OFF  ,xFrame, FEATUREURL_MENUBAR  );
166*cdf0e10cSrcweir         m_aToolBarListener   = new StatusListener(m_cbToolBar  ,TOOLBAR_ON  ,TOOLBAR_OFF  ,xFrame, FEATUREURL_TOOLBAR  );
167*cdf0e10cSrcweir         m_aObjectBarListener = new StatusListener(m_cbObjectBar,OBJECTBAR_ON,OBJECTBAR_OFF,xFrame, FEATUREURL_OBJECTBAR);
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir         m_aMenuBarListener.startListening();
170*cdf0e10cSrcweir         m_aToolBarListener.startListening();
171*cdf0e10cSrcweir         m_aObjectBarListener.startListening();
172*cdf0e10cSrcweir     }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir     // ____________________
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     /**
177*cdf0e10cSrcweir      * react for click events of the used check boxes
178*cdf0e10cSrcweir      * We use our internal set dispatch objects to
179*cdf0e10cSrcweir      * call it. This calls toogle the menu/object- or toolbar.
180*cdf0e10cSrcweir      * Note: Because we are listener status events too - hopefully
181*cdf0e10cSrcweir      * we get a notification, if toogling was successfully or not.
182*cdf0e10cSrcweir      * We use this information to update our check boxes again.
183*cdf0e10cSrcweir      * But such update doesn't force (hopefully) an action event. Otherwhise
184*cdf0e10cSrcweir      * we can produce a never ending recursion!
185*cdf0e10cSrcweir      *
186*cdf0e10cSrcweir      * @param aEvent
187*cdf0e10cSrcweir      *          describes the used check box and his current state
188*cdf0e10cSrcweir      *          we can use to dispatch the right URL to the office
189*cdf0e10cSrcweir      */
190*cdf0e10cSrcweir     class ClickListener implements ActionListener,
191*cdf0e10cSrcweir                                    com.sun.star.lang.XEventListener
192*cdf0e10cSrcweir     {
193*cdf0e10cSrcweir         /// URL, to toogle the requested UI item
194*cdf0e10cSrcweir         String m_sURL;
195*cdf0e10cSrcweir         /// name of the property which must be used in combination with the URL
196*cdf0e10cSrcweir         String m_sProp;
197*cdf0e10cSrcweir         /// we must use this frame to dispatch a request
198*cdf0e10cSrcweir         com.sun.star.frame.XFrame m_xFrame;
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir         //_____________________
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir         /**
203*cdf0e10cSrcweir          * ctor
204*cdf0e10cSrcweir          * It initialize an instance of this clas only.
205*cdf0e10cSrcweir          */
206*cdf0e10cSrcweir         ClickListener( String                    sURL   ,
207*cdf0e10cSrcweir                        String                    sProp  ,
208*cdf0e10cSrcweir                        com.sun.star.frame.XFrame xFrame )
209*cdf0e10cSrcweir         {
210*cdf0e10cSrcweir             m_sURL   = sURL  ;
211*cdf0e10cSrcweir             m_sProp  = sProp ;
212*cdf0e10cSrcweir             m_xFrame = xFrame;
213*cdf0e10cSrcweir         }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir         //_____________________
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir         /**
218*cdf0e10cSrcweir          * callback for action events
219*cdf0e10cSrcweir          * Such events occure if somehwere click the
220*cdf0e10cSrcweir          * JCheckBox control on which we are registered.
221*cdf0e10cSrcweir          * Such events doesn't occure if we set it programmaticly
222*cdf0e10cSrcweir          * (e.g. if we get status events to -> see class StatusListener too)
223*cdf0e10cSrcweir          *
224*cdf0e10cSrcweir          * @param aEvent
225*cdf0e10cSrcweir          *          describes the check box and his state
226*cdf0e10cSrcweir          *          we can use to toogle the requested office
227*cdf0e10cSrcweir          *          ressource.
228*cdf0e10cSrcweir          */
229*cdf0e10cSrcweir         public void actionPerformed(ActionEvent aEvent)
230*cdf0e10cSrcweir         {
231*cdf0e10cSrcweir             synchronized(this)
232*cdf0e10cSrcweir             {
233*cdf0e10cSrcweir                 if (m_xFrame==null)
234*cdf0e10cSrcweir                     return;
235*cdf0e10cSrcweir             }
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir             // define parameters for following dispatch
238*cdf0e10cSrcweir             boolean bState = ((JCheckBox)aEvent.getSource()).isSelected();
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir             // prepare the dispatch
241*cdf0e10cSrcweir             com.sun.star.util.URL aURL = FunctionHelper.parseURL(m_sURL);
242*cdf0e10cSrcweir             if (aURL==null)
243*cdf0e10cSrcweir                 return;
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir             com.sun.star.beans.PropertyValue[] lProperties = new com.sun.star.beans.PropertyValue[1];
246*cdf0e10cSrcweir             lProperties[0]       = new com.sun.star.beans.PropertyValue();
247*cdf0e10cSrcweir             lProperties[0].Name  = m_sProp;
248*cdf0e10cSrcweir             lProperties[0].Value = new Boolean(bState);
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir             // execute (dispatch) it into the frame
251*cdf0e10cSrcweir             if (m_xFrame==null)
252*cdf0e10cSrcweir                 return;
253*cdf0e10cSrcweir             FunctionHelper.execute(m_xFrame,aURL,lProperties,null);
254*cdf0e10cSrcweir         }
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir         // ____________________
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir         /**
259*cdf0e10cSrcweir          * callback for disposing events
260*cdf0e10cSrcweir          * Internaly we save a reference to an office frame.
261*cdf0e10cSrcweir          * Of course he can die and inform us then. We should react
262*cdf0e10cSrcweir          * and forget his reference.
263*cdf0e10cSrcweir          *
264*cdf0e10cSrcweir          * @param aEvent
265*cdf0e10cSrcweir          *          describes the source which fire this event
266*cdf0e10cSrcweir          *          Must be our internal saved frame. Otherwhise
267*cdf0e10cSrcweir          *          somewhere know us without a registration ...
268*cdf0e10cSrcweir          */
269*cdf0e10cSrcweir         public void disposing(com.sun.star.lang.EventObject aEvent)
270*cdf0e10cSrcweir         {
271*cdf0e10cSrcweir             synchronized(this)
272*cdf0e10cSrcweir             {
273*cdf0e10cSrcweir                 m_xFrame = null;
274*cdf0e10cSrcweir             }
275*cdf0e10cSrcweir         }
276*cdf0e10cSrcweir     }
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir     // ____________________
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir     /**
281*cdf0e10cSrcweir      * If this java application shutdown - we must cancel all current existing
282*cdf0e10cSrcweir      * listener connections. Otherwhise the office will run into some
283*cdf0e10cSrcweir      * DisposedExceptions if it tries to use these forgotten listener references.
284*cdf0e10cSrcweir      * And of course it can die doing that.
285*cdf0e10cSrcweir      * We are registered at a central object to be informed if the VM will exit.
286*cdf0e10cSrcweir      * So we can react.
287*cdf0e10cSrcweir      */
288*cdf0e10cSrcweir     public void shutdown()
289*cdf0e10cSrcweir     {
290*cdf0e10cSrcweir         m_aMenuBarListener.shutdown();
291*cdf0e10cSrcweir         m_aToolBarListener.shutdown();
292*cdf0e10cSrcweir         m_aObjectBarListener.shutdown();
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir         m_aMenuBarListener   = null;
295*cdf0e10cSrcweir         m_aToolBarListener   = null;
296*cdf0e10cSrcweir         m_aObjectBarListener = null;
297*cdf0e10cSrcweir     }
298*cdf0e10cSrcweir }
299