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 // __________ Imports __________
24 
25 import com.sun.star.uno.UnoRuntime;
26 
27 import java.awt.*;
28 import javax.swing.*;
29 import java.lang.String;
30 import java.awt.event.*;
31 import java.awt.*;
32 
33 // __________ Implementation __________
34 
35 /**
36  * Makes it possible to change some states of currently loaded
37  * document (e.g. enable/disable menubar, toolbar, objectbar)
38  *
39  * @author     Andreas Schlüns
40  * @created    20.06.2002 09:28
41  */
42 public class CustomizeView extends    JPanel
43                            implements IShutdownListener
44 {
45     // ____________________
46     // const
47 
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.
51      */
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"         ;
55 
56     private static final String FEATUREPROP_MENUBAR     = "MenuBarVisible"    ;
57     private static final String FEATUREPROP_TOOLBAR     = "ToolBarVisible"    ;
58     private static final String FEATUREPROP_OBJECTBAR   = "ObjectBarVisible"  ;
59 
60     private static final String ACTION_MENUBAR          = "toogle_menu"       ;
61     private static final String ACTION_TOOLBAR          = "toogle_toolbar"    ;
62     private static final String ACTION_OBJECTBAR        = "toogle_objectbar"  ;
63 
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"      ;
67 
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"     ;
71 
72     // ____________________
73     // member
74 
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
79      *
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
83      */
84     private JCheckBox           m_cbMenuBar         ;
85     private JCheckBox           m_cbToolBar         ;
86     private JCheckBox           m_cbObjectBar       ;
87 
88     private StatusListener      m_aMenuBarListener  ;
89     private StatusListener      m_aToolBarListener  ;
90     private StatusListener      m_aObjectBarListener;
91 
92     // ____________________
93 
94     /**
95      * ctor
96      * Create view controls on startup and initialize it.
97      * We don't start listening here. see setFrame()!
98      */
99     CustomizeView()
100     {
101         this.setLayout(new GridLayout(3,0));
102 
103         m_cbMenuBar   = new JCheckBox(MENUBAR_OFF  , false);
104         m_cbToolBar   = new JCheckBox(TOOLBAR_OFF  , false);
105         m_cbObjectBar = new JCheckBox(OBJECTBAR_OFF, false);
106 
107         m_cbMenuBar.setEnabled  (false);
108         m_cbToolBar.setEnabled  (false);
109         m_cbObjectBar.setEnabled(false);
110 
111         m_cbMenuBar.setActionCommand  (ACTION_MENUBAR  );
112         m_cbToolBar.setActionCommand  (ACTION_TOOLBAR  );
113         m_cbObjectBar.setActionCommand(ACTION_OBJECTBAR);
114 
115         this.add(m_cbMenuBar  );
116         this.add(m_cbToolBar  );
117         this.add(m_cbObjectBar);
118     }
119 
120     // ____________________
121 
122     /**
123      * set new frame for this view
124      * We start listening for frame action/status and click events instandly.
125      * If an event occure we use it to synchronize our controls
126      * with states of a (my be) new document view of this frame.
127      *
128      * @param xFrame
129      *          the reference to the frame, which provides the
130      *          possibility to get the required status informations
131      *
132      *          Attention: We don't accept new frames here.
133      *          We get one after startup and work with him.
134      *          That's it!
135      */
136     public void setFrame(com.sun.star.frame.XFrame xFrame)
137     {
138         if (xFrame==null)
139             return;
140 
141         // be listener for click events
142         // They will toogle 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);
146 
147         m_cbMenuBar.addActionListener  (aMenuBarHandler  );
148         m_cbToolBar.addActionListener  (aToolBarHandler  );
149         m_cbObjectBar.addActionListener(aObjectBarHandler);
150 
151         // be frame action listener
152         // The callback will update listener connections
153         // for status updates automaticly!
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);
157 
158         m_aMenuBarListener.startListening();
159         m_aToolBarListener.startListening();
160         m_aObjectBarListener.startListening();
161     }
162 
163     // ____________________
164 
165     /**
166      * react for click events of the used check boxes
167      * We use our internal set dispatch objects to
168      * call it. This calls toogle the menu/object- or toolbar.
169      * Note: Because we are listener status events too - hopefully
170      * we get a notification, if toogling 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. Otherwhise
173      * we can produce a never ending recursion!
174      *
175      * @param aEvent
176      *          describes the used check box and his current state
177      *          we can use to dispatch the right URL to the office
178      */
179     class ClickListener implements ActionListener,
180                                    com.sun.star.lang.XEventListener
181     {
182         /// URL, to toogle the requested UI item
183         String m_sURL;
184         /// name of the property which must be used in combination with the URL
185         String m_sProp;
186         /// we must use this frame to dispatch a request
187         com.sun.star.frame.XFrame m_xFrame;
188 
189         //_____________________
190 
191         /**
192          * ctor
193          * It initialize an instance of this clas only.
194          */
195         ClickListener( String                    sURL   ,
196                        String                    sProp  ,
197                        com.sun.star.frame.XFrame xFrame )
198         {
199             m_sURL   = sURL  ;
200             m_sProp  = sProp ;
201             m_xFrame = xFrame;
202         }
203 
204         //_____________________
205 
206         /**
207          * callback for action events
208          * Such events occure if somehwere click the
209          * JCheckBox control on which we are registered.
210          * Such events doesn't occure if we set it programmaticly
211          * (e.g. if we get status events to -> see class StatusListener too)
212          *
213          * @param aEvent
214          *          describes the check box and his state
215          *          we can use to toogle the requested office
216          *          ressource.
217          */
218         public void actionPerformed(ActionEvent aEvent)
219         {
220             synchronized(this)
221             {
222                 if (m_xFrame==null)
223                     return;
224             }
225 
226             // define parameters for following dispatch
227             boolean bState = ((JCheckBox)aEvent.getSource()).isSelected();
228 
229             // prepare the dispatch
230             com.sun.star.util.URL aURL = FunctionHelper.parseURL(m_sURL);
231             if (aURL==null)
232                 return;
233 
234             com.sun.star.beans.PropertyValue[] lProperties = new com.sun.star.beans.PropertyValue[1];
235             lProperties[0]       = new com.sun.star.beans.PropertyValue();
236             lProperties[0].Name  = m_sProp;
237             lProperties[0].Value = new Boolean(bState);
238 
239             // execute (dispatch) it into the frame
240             if (m_xFrame==null)
241                 return;
242             FunctionHelper.execute(m_xFrame,aURL,lProperties,null);
243         }
244 
245         // ____________________
246 
247         /**
248          * callback for disposing events
249          * Internaly we save a reference to an office frame.
250          * Of course he can die and inform us then. We should react
251          * and forget his reference.
252          *
253          * @param aEvent
254          *          describes the source which fire this event
255          *          Must be our internal saved frame. Otherwhise
256          *          somewhere know us without a registration ...
257          */
258         public void disposing(com.sun.star.lang.EventObject aEvent)
259         {
260             synchronized(this)
261             {
262                 m_xFrame = null;
263             }
264         }
265     }
266 
267     // ____________________
268 
269     /**
270      * If this java application shutdown - we must cancel all current existing
271      * listener connections. Otherwhise the office will run into some
272      * DisposedExceptions if it tries to use these forgotten listener references.
273      * And of course it can die doing that.
274      * We are registered at a central object to be informed if the VM will exit.
275      * So we can react.
276      */
277     public void shutdown()
278     {
279         m_aMenuBarListener.shutdown();
280         m_aToolBarListener.shutdown();
281         m_aObjectBarListener.shutdown();
282 
283         m_aMenuBarListener   = null;
284         m_aToolBarListener   = null;
285         m_aObjectBarListener = null;
286     }
287 }
288