Get the style color and number just once
[LibreOffice.git] / odk / examples / DevelopersGuide / OfficeDev / DesktopEnvironment / StatusView.java
blob076669014d83b5c52349d6e590e3d18acac37636
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 // __________ Imports __________
38 import java.awt.*;
39 import javax.swing.*;
41 // __________ Implementation __________
43 /**
44 * Implement a view to show status information
45 * of currently loaded document of a document view.
46 * It uses separate listener threads to get this information
47 * and actualize it automatically, if frame broadcast changes of
48 * his contained document.
49 * Threads are necessary to prevent this view against deadlocks.
50 * These deadlocks can occur if a listener will be notified
51 * by the office in an "oneway" method and try to call back
52 * to the office by using a synchronous method.
53 * UNO must guarantee order of all these calls ... and if
54 * the source of arrived event holds a mutex and our synchronous
55 * call needs this mutex too => a deadlock occurs.
56 * Why? UNO had created a new thread for our synchronous call
57 * inside the office process and so exist different threads
58 * for this constellation.
61 public class StatusView extends JPanel
62 implements IShutdownListener
66 /**
67 * const
68 * These URL's describe available feature states.
70 private static final String FEATUREURL_FONT = ".uno:CharFontName";
71 private static final String FEATUREURL_SIZE = ".uno:FontHeight";
72 private static final String FEATUREURL_BOLD = ".uno:Bold";
73 private static final String FEATUREURL_ITALIC = ".uno:Italic";
74 private static final String FEATUREURL_UNDERLINE = ".uno:Underline";
78 /**
79 * const
80 * These values are used to show current state of showed feature.
82 private static final String FONT_OFF = "unknown" ;
83 private static final String SIZE_OFF = "0.0" ;
84 private static final String BOLD_OFF = "-" ;
85 private static final String ITALIC_OFF = "-" ;
86 private static final String UNDERLINE_OFF = "-" ;
88 private static final String FONT_ON = "" ;
89 private static final String SIZE_ON = "" ;
90 private static final String BOLD_ON = "X" ;
91 private static final String ITALIC_ON = "X" ;
92 private static final String UNDERLINE_ON = "X" ;
96 /**
97 * @member mlaFontValue shows status of font name
98 * @member mlaSizeValue shows status of font size
99 * @member mlaBoldValue shows status of font style bold
100 * @member mlaUnderlineValue shows status of font style underline
101 * @member mlaItalicValue shows status of font style italic
103 * @member maFontListener threadsafe(!) helper to listen for status event which describe font name
104 * @member maSizeListener threadsafe(!) helper to listen for status event which describe font size
105 * @member maBoldListener threadsafe(!) helper to listen for status event which describe font style bold
106 * @member maUnderlineListener threadsafe(!) helper to listen for status event which describe font style underline
107 * @member maItalicListener threadsafe(!) helper to listen for status event which describe font style italic
109 private final JLabel m_laFontValue ;
110 private final JLabel m_laSizeValue ;
111 private final JLabel m_laBoldValue ;
112 private final JLabel m_laUnderlineValue ;
113 private final JLabel m_laItalicValue ;
115 private StatusListener m_aFontListener ;
116 private StatusListener m_aSizeListener ;
117 private StatusListener m_aBoldListener ;
118 private StatusListener m_aUnderlineListener ;
119 private StatusListener m_aItalicListener ;
124 * ctor
125 * Create view controls on startup and initialize it with default values.
126 * Filling of view items can be done by special set-methods.
127 * We don't start listening here! see setFrame() for that ...
129 StatusView()
131 this.setLayout(new GridBagLayout());
133 GridBagConstraints aConstraint = new GridBagConstraints();
134 aConstraint.anchor = GridBagConstraints.NORTHWEST;
135 aConstraint.insets = new Insets(2,2,2,2);
136 aConstraint.gridy = 0;
137 aConstraint.gridx = 0;
139 JLabel laFont = new JLabel("Font" );
140 JLabel laSize = new JLabel("Size" );
141 JLabel laBold = new JLabel("Bold" );
142 JLabel laUnderline = new JLabel("Underline");
143 JLabel laItalic = new JLabel("Italic" );
145 m_laFontValue = new JLabel();
146 m_laSizeValue = new JLabel();
147 m_laBoldValue = new JLabel();
148 m_laUnderlineValue = new JLabel();
149 m_laItalicValue = new JLabel();
151 aConstraint.gridx = 0;
152 this.add( laFont, aConstraint );
153 aConstraint.gridx = 1;
154 this.add( m_laFontValue, aConstraint );
156 ++aConstraint.gridy;
158 aConstraint.gridx = 0;
159 this.add( laSize, aConstraint );
160 aConstraint.gridx = 1;
161 this.add( m_laSizeValue, aConstraint );
163 ++aConstraint.gridy;
165 aConstraint.gridx = 0;
166 this.add( laSize, aConstraint );
167 aConstraint.gridx = 1;
168 this.add( m_laSizeValue, aConstraint );
170 ++aConstraint.gridy;
172 aConstraint.gridx = 0;
173 this.add( laBold, aConstraint );
174 aConstraint.gridx = 1;
175 this.add( m_laBoldValue, aConstraint );
177 ++aConstraint.gridy;
179 aConstraint.gridx = 0;
180 this.add( laUnderline, aConstraint );
181 aConstraint.gridx = 1;
182 this.add( m_laUnderlineValue, aConstraint );
184 ++aConstraint.gridy;
186 aConstraint.gridx = 0;
187 this.add( laItalic, aConstraint );
188 aConstraint.gridx = 1;
189 this.add( m_laItalicValue, aConstraint );
191 m_laFontValue.setEnabled (false);
192 m_laSizeValue.setEnabled (false);
193 m_laBoldValue.setEnabled (false);
194 m_laItalicValue.setEnabled (false);
195 m_laUnderlineValue.setEnabled(false);
197 m_laFontValue.setText (FONT_OFF );
198 m_laSizeValue.setText (SIZE_OFF );
199 m_laBoldValue.setText (BOLD_OFF );
200 m_laItalicValue.setText (ITALIC_OFF );
201 m_laUnderlineValue.setText(UNDERLINE_OFF);
207 * Set new frame for this view and start listening for events immediately.
208 * We create one status listener for every control we wish to update.
209 * And because the environment of the frame can be changed - these
210 * listener refresh himself internally for frame action events too.
211 * So we register it as such frame action listener only here.
212 * Rest is done automatically...
214 * @param xFrame
215 * will be used as source of possible status events
217 public void setFrame(com.sun.star.frame.XFrame xFrame)
219 if (xFrame==null)
220 return;
222 // create some listener on given frame for available status events
223 // Created listener instances will register themselves on this frame and
224 // show it received information automatically on set UI controls.
225 m_aFontListener = new StatusListener(m_laFontValue ,FONT_ON ,FONT_OFF ,xFrame, FEATUREURL_FONT );
226 m_aSizeListener = new StatusListener(m_laSizeValue ,SIZE_ON ,SIZE_OFF ,xFrame, FEATUREURL_SIZE );
227 m_aBoldListener = new StatusListener(m_laBoldValue ,BOLD_ON ,BOLD_OFF ,xFrame, FEATUREURL_BOLD );
228 m_aItalicListener = new StatusListener(m_laItalicValue ,ITALIC_ON ,ITALIC_OFF ,xFrame, FEATUREURL_ITALIC );
229 m_aUnderlineListener = new StatusListener(m_laUnderlineValue,UNDERLINE_ON,UNDERLINE_OFF,xFrame, FEATUREURL_UNDERLINE);
231 m_aFontListener.startListening();
232 m_aSizeListener.startListening();
233 m_aBoldListener.startListening();
234 m_aItalicListener.startListening();
235 m_aUnderlineListener.startListening();
241 * If this java application shutdown - we must cancel all current existing
242 * listener connections. Otherwise the office will run into some
243 * DisposedExceptions if it tries to use these forgotten listener references.
244 * And of course it can die doing that.
245 * We are registered at a central object to be informed if the VM will exit.
246 * So we can react.
248 public void shutdown()
250 m_aFontListener.shutdown();
251 m_aSizeListener.shutdown();
252 m_aBoldListener.shutdown();
253 m_aItalicListener.shutdown();
254 m_aUnderlineListener.shutdown();
256 m_aFontListener = null;
257 m_aSizeListener = null;
258 m_aBoldListener = null;
259 m_aItalicListener = null;
260 m_aUnderlineListener = null;
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */