KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > apps > svgbrowser > StatusBar


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.apps.svgbrowser;
19
20 import java.awt.BorderLayout JavaDoc;
21 import java.awt.Dimension JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.ResourceBundle JavaDoc;
24
25 import javax.swing.JLabel JavaDoc;
26 import javax.swing.JPanel JavaDoc;
27 import javax.swing.border.BevelBorder JavaDoc;
28
29 import org.apache.batik.util.gui.resource.ResourceManager;
30
31 /**
32  * This class represents a viewer status bar.
33  *
34  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
35  * @version $Id: StatusBar.java,v 1.8 2005/03/27 08:58:30 cam Exp $
36  */

37 public class StatusBar extends JPanel JavaDoc {
38
39     /**
40      * The gui resources file name
41      */

42     protected final static String JavaDoc RESOURCES =
43         "org.apache.batik.apps.svgbrowser.resources.StatusBarMessages";
44
45     /**
46      * The resource bundle
47      */

48     protected static ResourceBundle JavaDoc bundle;
49
50     /**
51      * The resource manager
52      */

53     protected static ResourceManager rManager;
54     static {
55         bundle = ResourceBundle.getBundle(RESOURCES, Locale.getDefault());
56         rManager = new ResourceManager(bundle);
57     }
58
59     /**
60      * The x position/width label.
61      */

62     protected JLabel JavaDoc xPosition;
63
64     /**
65      * The y position/height label.
66      */

67     protected JLabel JavaDoc yPosition;
68
69     /**
70      * The zoom label.
71      */

72     protected JLabel JavaDoc zoom;
73
74     /**
75      * The message label
76      */

77     protected JLabel JavaDoc message;
78
79     /**
80      * The main message
81      */

82     protected String JavaDoc mainMessage;
83
84     /**
85      * The temporary message
86      */

87     protected String JavaDoc temporaryMessage;
88
89     /**
90      * The current display thread.
91      */

92     protected DisplayThread displayThread;
93
94     /**
95      * Creates a new status bar.
96      */

97     public StatusBar() {
98         super(new BorderLayout JavaDoc(5, 5));
99
100         JPanel JavaDoc p = new JPanel JavaDoc(new BorderLayout JavaDoc(0, 0));
101         add("West", p);
102
103         xPosition = new JLabel JavaDoc();
104         BevelBorder JavaDoc bb;
105         bb = new BevelBorder JavaDoc(BevelBorder.LOWERED,
106                              getBackground().brighter().brighter(),
107                              getBackground(),
108                              getBackground().darker().darker(),
109                              getBackground());
110         xPosition.setBorder(bb);
111         xPosition.setPreferredSize(new Dimension JavaDoc(110, 16));
112         p.add("West", xPosition);
113
114         yPosition = new JLabel JavaDoc();
115         yPosition.setBorder(bb);
116         yPosition.setPreferredSize(new Dimension JavaDoc(110, 16));
117         p.add("Center", yPosition);
118
119         zoom = new JLabel JavaDoc();
120         zoom.setBorder(bb);
121         zoom.setPreferredSize(new Dimension JavaDoc(70, 16));
122         p.add("East", zoom);
123
124         p = new JPanel JavaDoc(new BorderLayout JavaDoc(0, 0));
125         message = new JLabel JavaDoc();
126         message.setBorder(bb);
127         p.add(message);
128         add(p);
129         setMainMessage(rManager.getString("Panel.default_message"));
130     }
131
132     /**
133      * Sets the x position.
134      */

135     public void setXPosition(float x) {
136         xPosition.setText("x: " + x);
137     }
138
139     /**
140      * Sets the width.
141      */

142     public void setWidth(float w) {
143         xPosition.setText(rManager.getString("Position.width_letters") +
144                           " " + w);
145     }
146
147     /**
148      * Sets the y position.
149      */

150     public void setYPosition(float y) {
151         yPosition.setText("y: " + y);
152     }
153
154     /**
155      * Sets the height.
156      */

157     public void setHeight(float h) {
158         yPosition.setText(rManager.getString("Position.height_letters") +
159                           " " + h);
160     }
161
162     /**
163      * Sets the zoom factor.
164      */

165     public void setZoom(float f) {
166         f = (f > 0) ? f : -f;
167         if (f == 1) {
168             zoom.setText("1:1");
169         } else if (f >= 1) {
170             String JavaDoc s = Float.toString(f);
171             if (s.length() > 6) {
172                 s = s.substring(0, 6);
173             }
174             zoom.setText("1:" + s);
175         } else {
176             String JavaDoc s = Float.toString(1 / f);
177             if (s.length() > 6) {
178                 s = s.substring(0, 6);
179             }
180             zoom.setText(s + ":1");
181         }
182     }
183
184     /**
185      * Sets a temporary message
186      * @param s the message
187      */

188     public void setMessage(String JavaDoc s) {
189         setPreferredSize(new Dimension JavaDoc(0, getPreferredSize().height));
190         if (displayThread != null) {
191             displayThread.finish();
192         }
193         temporaryMessage = s;
194         Thread JavaDoc old = displayThread;
195         displayThread = new DisplayThread(old);
196         displayThread.start();
197     }
198
199     /**
200      * Sets the main message
201      * @param s the message
202      */

203     public void setMainMessage(String JavaDoc s) {
204         mainMessage = s;
205         message.setText(mainMessage = s);
206         if (displayThread != null) {
207             displayThread.finish();
208             displayThread = null;
209         }
210         setPreferredSize(new Dimension JavaDoc(0, getPreferredSize().height));
211     }
212
213     /**
214      * To display the main message
215      */

216     protected class DisplayThread extends Thread JavaDoc {
217         static final long DEFAULT_DURATION = 5000;
218         long duration;
219         Thread JavaDoc toJoin;
220         public DisplayThread() {
221             this(DEFAULT_DURATION, null);
222         }
223         public DisplayThread(long duration) {
224             this(duration, null);
225         }
226         public DisplayThread(Thread JavaDoc toJoin) {
227             this(DEFAULT_DURATION, toJoin);
228         }
229         public DisplayThread(long duration, Thread JavaDoc toJoin) {
230             this.duration = duration;
231             this.toJoin = toJoin;
232             setPriority(Thread.MIN_PRIORITY);
233         }
234
235         public synchronized void finish() {
236             this.duration = 0;
237             this.notifyAll();
238         }
239
240         public void run() {
241             synchronized (this) {
242                 if (toJoin != null) {
243                     while (toJoin.isAlive()) {
244                         try { toJoin.join(); }
245                         catch (InterruptedException JavaDoc ie) { }
246                     }
247                     toJoin = null;
248                 }
249
250                 message.setText(temporaryMessage);
251
252                 long lTime = System.currentTimeMillis();
253
254                 while (duration > 0) {
255                     try {
256                         wait(duration);
257                     } catch(InterruptedException JavaDoc e) { }
258                     long cTime = System.currentTimeMillis();
259                     duration -= (cTime-lTime);
260                     lTime = cTime;
261                 }
262                 message.setText(mainMessage);
263             }
264         }
265     }
266 }
267
Popular Tags