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 
35*cdf0e10cSrcweir // __________ Imports __________
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir import java.awt.*;
40*cdf0e10cSrcweir import javax.swing.*;
41*cdf0e10cSrcweir import java.lang.String;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir // __________ Implementation __________
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir /**
46*cdf0e10cSrcweir  * Implement a view to show status informations
47*cdf0e10cSrcweir  * of currently loaded document of a document view.
48*cdf0e10cSrcweir  * It use seperate listener threads to get this informations
49*cdf0e10cSrcweir  * and actualize it automaticly if frame broadcast changes of
50*cdf0e10cSrcweir  * his contained document.
51*cdf0e10cSrcweir  * Threads are neccessary to prevent this view against deadlocks.
52*cdf0e10cSrcweir  * These deadlocks can occure if a listener will be notified
53*cdf0e10cSrcweir  * by the office in an "oneway" method and try to call back
54*cdf0e10cSrcweir  * to the office by using a synchronous method.
55*cdf0e10cSrcweir  * UNO must guarantee order of all these calls ... and if
56*cdf0e10cSrcweir  * the source of arrived event holds a mutex and our synchronous
57*cdf0e10cSrcweir  * call needs this mutex too => a deadlock occure.
58*cdf0e10cSrcweir  * Why? UNO had created a new thread for our synchronous call
59*cdf0e10cSrcweir  * inside the office process and so exist different threads
60*cdf0e10cSrcweir  * for this constallation.
61*cdf0e10cSrcweir  *
62*cdf0e10cSrcweir  * @author     Andreas Schlüns
63*cdf0e10cSrcweir  * @created    20.06.2002 15:08
64*cdf0e10cSrcweir  */
65*cdf0e10cSrcweir public class StatusView extends    JPanel
66*cdf0e10cSrcweir                         implements IShutdownListener
67*cdf0e10cSrcweir {
68*cdf0e10cSrcweir     // ____________________
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir     /**
71*cdf0e10cSrcweir      * const
72*cdf0e10cSrcweir      * These URL's describe available feature states.
73*cdf0e10cSrcweir      */
74*cdf0e10cSrcweir     public static final String FEATUREURL_FONT      = "slot:10007";
75*cdf0e10cSrcweir     public static final String FEATUREURL_SIZE      = "slot:10015";
76*cdf0e10cSrcweir     public static final String FEATUREURL_BOLD      = "slot:10009";
77*cdf0e10cSrcweir     public static final String FEATUREURL_ITALIC    = "slot:10008";
78*cdf0e10cSrcweir     public static final String FEATUREURL_UNDERLINE = "slot:10014";
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir     // ____________________
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     /**
83*cdf0e10cSrcweir      * const
84*cdf0e10cSrcweir      * These values are used to show current state of showed feature.
85*cdf0e10cSrcweir      */
86*cdf0e10cSrcweir     public static final String FONT_OFF         = "unknown" ;
87*cdf0e10cSrcweir     public static final String SIZE_OFF         = "0.0"     ;
88*cdf0e10cSrcweir     public static final String BOLD_OFF         = "-"       ;
89*cdf0e10cSrcweir     public static final String ITALIC_OFF       = "-"       ;
90*cdf0e10cSrcweir     public static final String UNDERLINE_OFF    = "-"       ;
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     public static final String FONT_ON          = ""        ;
93*cdf0e10cSrcweir     public static final String SIZE_ON          = ""        ;
94*cdf0e10cSrcweir     public static final String BOLD_ON          = "X"       ;
95*cdf0e10cSrcweir     public static final String ITALIC_ON        = "X"       ;
96*cdf0e10cSrcweir     public static final String UNDERLINE_ON     = "X"       ;
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     // ____________________
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir     /**
101*cdf0e10cSrcweir      * @member  mlaFontValue            shows status of font name
102*cdf0e10cSrcweir      * @member  mlaSizeValue            shows status of font size
103*cdf0e10cSrcweir      * @member  mlaBoldValue            shows status of font style bold
104*cdf0e10cSrcweir      * @member  mlaUnderlineValue       shows status of font style underline
105*cdf0e10cSrcweir      * @member  mlaItalicValue          shows status of font style italic
106*cdf0e10cSrcweir      *
107*cdf0e10cSrcweir      * @member  maFontListener          threadsafe(!) helper to listen for status event which describe font name
108*cdf0e10cSrcweir      * @member  maSizeListener          threadsafe(!) helper to listen for status event which describe font size
109*cdf0e10cSrcweir      * @member  maBoldListener          threadsafe(!) helper to listen for status event which describe font style bold
110*cdf0e10cSrcweir      * @member  maUnderlineListener     threadsafe(!) helper to listen for status event which describe font style underline
111*cdf0e10cSrcweir      * @member  maItalicListener        threadsafe(!) helper to listen for status event which describe font style italic
112*cdf0e10cSrcweir      */
113*cdf0e10cSrcweir     private JLabel              m_laFontValue            ;
114*cdf0e10cSrcweir     private JLabel              m_laSizeValue            ;
115*cdf0e10cSrcweir     private JLabel              m_laBoldValue            ;
116*cdf0e10cSrcweir     private JLabel              m_laUnderlineValue       ;
117*cdf0e10cSrcweir     private JLabel              m_laItalicValue          ;
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir     private StatusListener      m_aFontListener          ;
120*cdf0e10cSrcweir     private StatusListener      m_aSizeListener          ;
121*cdf0e10cSrcweir     private StatusListener      m_aBoldListener          ;
122*cdf0e10cSrcweir     private StatusListener      m_aUnderlineListener     ;
123*cdf0e10cSrcweir     private StatusListener      m_aItalicListener        ;
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     // ____________________
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir     /**
128*cdf0e10cSrcweir      * ctor
129*cdf0e10cSrcweir      * Create view controls on startup and initialize it with default values.
130*cdf0e10cSrcweir      * Filling of view items can be done by special set-methods.
131*cdf0e10cSrcweir      * We don't start listening here! see setFrame() for that ...
132*cdf0e10cSrcweir      */
133*cdf0e10cSrcweir     StatusView()
134*cdf0e10cSrcweir     {
135*cdf0e10cSrcweir         this.setLayout(new GridBagLayout());
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir         GridBagConstraints aConstraint = new GridBagConstraints();
138*cdf0e10cSrcweir         aConstraint.anchor = GridBagConstraints.NORTHWEST;
139*cdf0e10cSrcweir         aConstraint.insets = new Insets(2,2,2,2);
140*cdf0e10cSrcweir         aConstraint.gridy  = 0;
141*cdf0e10cSrcweir         aConstraint.gridx  = 0;
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir         JLabel laFont      = new JLabel("Font"     );
144*cdf0e10cSrcweir         JLabel laSize      = new JLabel("Size"     );
145*cdf0e10cSrcweir         JLabel laBold      = new JLabel("Bold"     );
146*cdf0e10cSrcweir         JLabel laUnderline = new JLabel("Underline");
147*cdf0e10cSrcweir         JLabel laItalic    = new JLabel("Italic"   );
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir         m_laFontValue       = new JLabel();
150*cdf0e10cSrcweir         m_laSizeValue       = new JLabel();
151*cdf0e10cSrcweir         m_laBoldValue       = new JLabel();
152*cdf0e10cSrcweir         m_laUnderlineValue  = new JLabel();
153*cdf0e10cSrcweir         m_laItalicValue     = new JLabel();
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir         aConstraint.gridx = 0;
156*cdf0e10cSrcweir         this.add( laFont, aConstraint );
157*cdf0e10cSrcweir         aConstraint.gridx = 1;
158*cdf0e10cSrcweir         this.add( m_laFontValue, aConstraint );
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir         ++aConstraint.gridy;
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir         aConstraint.gridx = 0;
163*cdf0e10cSrcweir         this.add( laSize, aConstraint );
164*cdf0e10cSrcweir         aConstraint.gridx = 1;
165*cdf0e10cSrcweir         this.add( m_laSizeValue, aConstraint );
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir         ++aConstraint.gridy;
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir         aConstraint.gridx = 0;
170*cdf0e10cSrcweir         this.add( laSize, aConstraint );
171*cdf0e10cSrcweir         aConstraint.gridx = 1;
172*cdf0e10cSrcweir         this.add( m_laSizeValue, aConstraint );
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir         ++aConstraint.gridy;
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir         aConstraint.gridx = 0;
177*cdf0e10cSrcweir         this.add( laBold, aConstraint );
178*cdf0e10cSrcweir         aConstraint.gridx = 1;
179*cdf0e10cSrcweir         this.add( m_laBoldValue, aConstraint );
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir         ++aConstraint.gridy;
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir         aConstraint.gridx = 0;
184*cdf0e10cSrcweir         this.add( laUnderline, aConstraint );
185*cdf0e10cSrcweir         aConstraint.gridx = 1;
186*cdf0e10cSrcweir         this.add( m_laUnderlineValue, aConstraint );
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir         ++aConstraint.gridy;
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir         aConstraint.gridx = 0;
191*cdf0e10cSrcweir         this.add( laItalic, aConstraint );
192*cdf0e10cSrcweir         aConstraint.gridx = 1;
193*cdf0e10cSrcweir         this.add( m_laItalicValue, aConstraint );
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir         m_laFontValue.setEnabled     (false);
196*cdf0e10cSrcweir         m_laSizeValue.setEnabled     (false);
197*cdf0e10cSrcweir         m_laBoldValue.setEnabled     (false);
198*cdf0e10cSrcweir         m_laItalicValue.setEnabled   (false);
199*cdf0e10cSrcweir         m_laUnderlineValue.setEnabled(false);
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir         m_laFontValue.setText     (FONT_OFF     );
202*cdf0e10cSrcweir         m_laSizeValue.setText     (SIZE_OFF     );
203*cdf0e10cSrcweir         m_laBoldValue.setText     (BOLD_OFF     );
204*cdf0e10cSrcweir         m_laItalicValue.setText   (ITALIC_OFF   );
205*cdf0e10cSrcweir         m_laUnderlineValue.setText(UNDERLINE_OFF);
206*cdf0e10cSrcweir     }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir     // ____________________
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir     /**
211*cdf0e10cSrcweir      * Set new frame for this view and start listening for events imedatly.
212*cdf0e10cSrcweir      * We create one status listener for every control we whish to update.
213*cdf0e10cSrcweir      * And because the environment of the frame can be changed - these
214*cdf0e10cSrcweir      * listener refresh himself internaly for frame action events too.
215*cdf0e10cSrcweir      * So we register it as such frame action listener only here.
216*cdf0e10cSrcweir      * Rest is done automaticly ...
217*cdf0e10cSrcweir      *
218*cdf0e10cSrcweir      * @param xFrame
219*cdf0e10cSrcweir      *          will be used as source of possible status events
220*cdf0e10cSrcweir      */
221*cdf0e10cSrcweir     public void setFrame(com.sun.star.frame.XFrame xFrame)
222*cdf0e10cSrcweir     {
223*cdf0e10cSrcweir         if (xFrame==null)
224*cdf0e10cSrcweir             return;
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir         // create some listener on given frame for available status events
227*cdf0e10cSrcweir         // Created listener instances will register herself on this frame and
228*cdf0e10cSrcweir         // show her received informations automaticly on setted UI controls.
229*cdf0e10cSrcweir         m_aFontListener      = new StatusListener(m_laFontValue     ,FONT_ON     ,FONT_OFF     ,xFrame, FEATUREURL_FONT     );
230*cdf0e10cSrcweir         m_aSizeListener      = new StatusListener(m_laSizeValue     ,SIZE_ON     ,SIZE_OFF     ,xFrame, FEATUREURL_SIZE     );
231*cdf0e10cSrcweir         m_aBoldListener      = new StatusListener(m_laBoldValue     ,BOLD_ON     ,BOLD_OFF     ,xFrame, FEATUREURL_BOLD     );
232*cdf0e10cSrcweir         m_aItalicListener    = new StatusListener(m_laItalicValue   ,ITALIC_ON   ,ITALIC_OFF   ,xFrame, FEATUREURL_ITALIC   );
233*cdf0e10cSrcweir         m_aUnderlineListener = new StatusListener(m_laUnderlineValue,UNDERLINE_ON,UNDERLINE_OFF,xFrame, FEATUREURL_UNDERLINE);
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir         m_aFontListener.startListening();
236*cdf0e10cSrcweir         m_aSizeListener.startListening();
237*cdf0e10cSrcweir         m_aBoldListener.startListening();
238*cdf0e10cSrcweir         m_aItalicListener.startListening();
239*cdf0e10cSrcweir         m_aUnderlineListener.startListening();
240*cdf0e10cSrcweir     }
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir     // ____________________
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir     /**
245*cdf0e10cSrcweir      * If this java application shutdown - we must cancel all current existing
246*cdf0e10cSrcweir      * listener connections. Otherwhise the office will run into some
247*cdf0e10cSrcweir      * DisposedExceptions if it tries to use these forgotten listener references.
248*cdf0e10cSrcweir      * And of course it can die doing that.
249*cdf0e10cSrcweir      * We are registered at a central object to be informed if the VM will exit.
250*cdf0e10cSrcweir      * So we can react.
251*cdf0e10cSrcweir      */
252*cdf0e10cSrcweir     public void shutdown()
253*cdf0e10cSrcweir     {
254*cdf0e10cSrcweir         m_aFontListener.shutdown();
255*cdf0e10cSrcweir         m_aSizeListener.shutdown();
256*cdf0e10cSrcweir         m_aBoldListener.shutdown();
257*cdf0e10cSrcweir         m_aItalicListener.shutdown();
258*cdf0e10cSrcweir         m_aUnderlineListener.shutdown();
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir         m_aFontListener      = null;
261*cdf0e10cSrcweir         m_aSizeListener      = null;
262*cdf0e10cSrcweir         m_aBoldListener      = null;
263*cdf0e10cSrcweir         m_aItalicListener    = null;
264*cdf0e10cSrcweir         m_aUnderlineListener = null;
265*cdf0e10cSrcweir     }
266*cdf0e10cSrcweir }
267