1 18 package org.apache.batik.apps.svgbrowser; 19 20 import java.awt.BorderLayout ; 21 import java.awt.Dimension ; 22 import java.util.Locale ; 23 import java.util.ResourceBundle ; 24 25 import javax.swing.JLabel ; 26 import javax.swing.JPanel ; 27 import javax.swing.border.BevelBorder ; 28 29 import org.apache.batik.util.gui.resource.ResourceManager; 30 31 37 public class StatusBar extends JPanel { 38 39 42 protected final static String RESOURCES = 43 "org.apache.batik.apps.svgbrowser.resources.StatusBarMessages"; 44 45 48 protected static ResourceBundle bundle; 49 50 53 protected static ResourceManager rManager; 54 static { 55 bundle = ResourceBundle.getBundle(RESOURCES, Locale.getDefault()); 56 rManager = new ResourceManager(bundle); 57 } 58 59 62 protected JLabel xPosition; 63 64 67 protected JLabel yPosition; 68 69 72 protected JLabel zoom; 73 74 77 protected JLabel message; 78 79 82 protected String mainMessage; 83 84 87 protected String temporaryMessage; 88 89 92 protected DisplayThread displayThread; 93 94 97 public StatusBar() { 98 super(new BorderLayout (5, 5)); 99 100 JPanel p = new JPanel (new BorderLayout (0, 0)); 101 add("West", p); 102 103 xPosition = new JLabel (); 104 BevelBorder bb; 105 bb = new BevelBorder (BevelBorder.LOWERED, 106 getBackground().brighter().brighter(), 107 getBackground(), 108 getBackground().darker().darker(), 109 getBackground()); 110 xPosition.setBorder(bb); 111 xPosition.setPreferredSize(new Dimension (110, 16)); 112 p.add("West", xPosition); 113 114 yPosition = new JLabel (); 115 yPosition.setBorder(bb); 116 yPosition.setPreferredSize(new Dimension (110, 16)); 117 p.add("Center", yPosition); 118 119 zoom = new JLabel (); 120 zoom.setBorder(bb); 121 zoom.setPreferredSize(new Dimension (70, 16)); 122 p.add("East", zoom); 123 124 p = new JPanel (new BorderLayout (0, 0)); 125 message = new JLabel (); 126 message.setBorder(bb); 127 p.add(message); 128 add(p); 129 setMainMessage(rManager.getString("Panel.default_message")); 130 } 131 132 135 public void setXPosition(float x) { 136 xPosition.setText("x: " + x); 137 } 138 139 142 public void setWidth(float w) { 143 xPosition.setText(rManager.getString("Position.width_letters") + 144 " " + w); 145 } 146 147 150 public void setYPosition(float y) { 151 yPosition.setText("y: " + y); 152 } 153 154 157 public void setHeight(float h) { 158 yPosition.setText(rManager.getString("Position.height_letters") + 159 " " + h); 160 } 161 162 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 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 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 188 public void setMessage(String s) { 189 setPreferredSize(new Dimension (0, getPreferredSize().height)); 190 if (displayThread != null) { 191 displayThread.finish(); 192 } 193 temporaryMessage = s; 194 Thread old = displayThread; 195 displayThread = new DisplayThread(old); 196 displayThread.start(); 197 } 198 199 203 public void setMainMessage(String s) { 204 mainMessage = s; 205 message.setText(mainMessage = s); 206 if (displayThread != null) { 207 displayThread.finish(); 208 displayThread = null; 209 } 210 setPreferredSize(new Dimension (0, getPreferredSize().height)); 211 } 212 213 216 protected class DisplayThread extends Thread { 217 static final long DEFAULT_DURATION = 5000; 218 long duration; 219 Thread toJoin; 220 public DisplayThread() { 221 this(DEFAULT_DURATION, null); 222 } 223 public DisplayThread(long duration) { 224 this(duration, null); 225 } 226 public DisplayThread(Thread toJoin) { 227 this(DEFAULT_DURATION, toJoin); 228 } 229 public DisplayThread(long duration, Thread 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 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 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 |