1/* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22/* 23 * ident "%Z%%M% %I% %E% SMI" 24 * 25 * Copyright (c) 2000 by Sun Microsystems, Inc. 26 * All rights reserved. 27 */ 28 29/* 30 * Copyright (C) 1996 Active Software, Inc. 31 * All rights reserved. 32 * 33 * @(#) StatusBar.java 1.15 - last change made 07/25/97 34 */ 35 36package sunsoft.jws.visual.rt.awt; 37 38import sunsoft.jws.visual.rt.base.Global; 39import sunsoft.jws.visual.rt.base.Util; 40import java.awt.*; 41 42/** 43 * A label that shows a single line of text for while. After a set 44 * time period the text is blanked. It's useful for a status bar at 45 * the bottom of a frame. 46 * 47 * @version 1.15, 07/25/97 48 */ 49public class StatusBar extends Canvas implements Runnable { 50 private String text; 51 private long wakeupTime; 52 private long timeout; 53 54 public StatusBar(String text) { 55 wakeupTime = 0; 56 timeout = 7000; 57 setFont(new Font(/* NOI18N */"Sansserif", Font.BOLD, 14)); 58 59 Thread thread = new Thread(this); 60 thread.setDaemon(true); 61 thread.start(); 62 } 63 64 public StatusBar() { 65 this(/* NOI18N */""); 66 } 67 68 public synchronized void setTimeout(long millis) { 69 timeout = millis; 70 resetTimer(true); 71 } 72 73 public void setText(String text) { 74 setText(text, true); 75 } 76 77 public void setText(String text, boolean shouldTimeout) { 78 if (text != this.text && (this.text == null || 79 !this.text.equals(text))) { 80 this.text = text; 81 repaint(); 82 } 83 84 resetTimer(shouldTimeout); 85 } 86 87 public String getText() { 88 return text; 89 } 90 91 public void paint(Graphics g) { 92 if (Global.isWindows()) 93 g = getGraphics(); 94 Dimension d = size(); 95 96 g.setColor(getBackground()); 97 if (Global.isWindows()) 98 g.fillRect(0, 0, d.width, d.height); 99 Global.util.draw3DRect(g, 0, 0, d.width-1, d.height-1, 100 Util.WIN95_SUNKEN, 1); 101 102 if (text != null) { 103 g.setColor(getForeground()); 104 g.setFont(getFont()); 105 FontMetrics fm = g.getFontMetrics(); 106 g.drawString(text, 5, fm.getAscent() + 3); 107 } 108 } 109 110 public Dimension minimumSize() { 111 Graphics g = getGraphics(); 112 int w = 10; 113 int h = 6; 114 115 if (g != null) { 116 FontMetrics fm = g.getFontMetrics(); 117 if (text != null) 118 w += fm.stringWidth(text); 119 h += fm.getHeight(); 120 } 121 122 return new Dimension(w, h); 123 } 124 125 public Dimension preferredSize() { 126 return minimumSize(); 127 } 128 129 public synchronized void run() { 130 long currentTime = System.currentTimeMillis(); 131 132 while (true) { 133 try { 134 if (wakeupTime == 0) 135 wait(); 136 else 137 wait(wakeupTime - currentTime); 138 } 139 catch (java.lang.InterruptedException ex) { 140 } 141 142 currentTime = System.currentTimeMillis(); 143 if (wakeupTime != 0 && wakeupTime < currentTime) { 144 text = null; 145 repaint(); 146 wakeupTime = 0; 147 } 148 } 149 } 150 151 private synchronized void resetTimer(boolean shouldTimeout) { 152 if (timeout > 0 && shouldTimeout && text != null && 153 !text.equals(/* NOI18N */"")) { 154 wakeupTime = System.currentTimeMillis() + timeout; 155 notify(); 156 } else { 157 wakeupTime = 0; 158 } 159 } 160} 161