|
From: JP <j-...@us...> - 2007-05-05 06:19:24
|
Update of /cvsroot/swixat/swixat/src/main/java/org/swixat/widget In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv22588/src/main/java/org/swixat/widget Modified Files: Tag: B0_6_0 MemoryStatus.java Added Files: Tag: B0_6_0 FadeLabel.java Log Message: - Enhanced and simplified the MemoryStatus widget. - Added FadeLabel widget - a JLabel whose text fades away after a specified delay. Suitable for status bars and other such temporary displays. Index: MemoryStatus.java =================================================================== RCS file: /cvsroot/swixat/swixat/src/main/java/org/swixat/widget/MemoryStatus.java,v retrieving revision 1.2 retrieving revision 1.2.2.1 diff -C2 -d -r1.2 -r1.2.2.1 *** MemoryStatus.java 2 Oct 2006 21:48:36 -0000 1.2 --- MemoryStatus.java 5 May 2007 06:19:18 -0000 1.2.2.1 *************** *** 30,97 **** package org.swixat.widget; ! import java.awt.Color; ! import java.awt.Dimension; ! import java.awt.Font; ! import java.awt.Graphics; ! import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; - import java.awt.font.FontRenderContext; - import java.awt.font.LineMetrics; - import java.awt.geom.Rectangle2D; - - import javax.swing.JComponent; - import javax.swing.JLabel; - import javax.swing.Timer; /** ! * ! * Display memory free and used in small panel. ! * ! * Code based on JEdit widget ! * ! * Copyright: Slava Pestov <sl...@je...> ! * ! * You are free to distribute this software under the terms of ! * the GNU General Public License. ! * ! * @author poussin ! * */ ! public class MemoryStatus extends JComponent implements ActionListener { ! ! private LineMetrics lm; - private final static String memoryTestStr = "99999/99999Mb"; - private Color progressBackground; - private Color progressForeground; private Timer timer; - public MemoryStatus() { ! Font font = (new JLabel()).getFont(); ! setFont(font); ! FontRenderContext frc = new FontRenderContext(null, false, false); ! Rectangle2D bounds = font.getStringBounds("99999/99999Mb", frc); ! Dimension dim = new Dimension((int)bounds.getWidth(), (int)bounds.getHeight()); ! setMinimumSize(dim); ! setPreferredSize(dim); ! lm = font.getLineMetrics(memoryTestStr, frc); ! setForeground(Color.BLACK); ! setBackground(Color.WHITE); ! progressForeground = Color.decode("#cccccc"); ! progressBackground = Color.decode("#666699a"); } - /** - * called by timer - */ public void actionPerformed(ActionEvent evt) { ! repaint(); } - /** Adds a feature to the Notify attribute of the MemoryStatus object */ public void addNotify() { super.addNotify(); --- 30,61 ---- package org.swixat.widget; ! import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** ! * Progress bar-like display of the VM's used vs. total memory. */ ! public class MemoryStatus extends JProgressBar implements ActionListener { private Timer timer; public MemoryStatus() { ! super(); ! setStringPainted(true); } public void actionPerformed(ActionEvent evt) { ! Runtime runtime = Runtime.getRuntime(); ! int freeMemory = (int) (runtime.freeMemory() / 1024L / 1024L); ! int totalMemory = (int) (runtime.totalMemory() / 1024L / 1024L); ! int usedMemory = totalMemory - freeMemory; ! setMaximum(totalMemory); ! setValue(usedMemory); ! setString(usedMemory + "M of " + totalMemory + "M"); } public void addNotify() { super.addNotify(); *************** *** 105,141 **** } - public void paintComponent(Graphics g) { - Insets insets = new Insets(0, 0, 0, 0); - Runtime runtime = Runtime.getRuntime(); - int freeMemory = (int)(runtime.freeMemory() / 1024L); - int totalMemory = (int)(runtime.totalMemory() / 1024L); - int usedMemory = totalMemory - freeMemory; - int width = getWidth() - insets.left - insets.right; - int height = getHeight() - insets.top - insets.bottom - 1; - float fraction = (float)usedMemory / (float)totalMemory; - g.setColor(progressBackground); - g.fillRect(insets.left, insets.top, (int)((float)width * fraction), height); - String str = usedMemory / 1024 + "/" + totalMemory / 1024 + "Mb"; - FontRenderContext frc = new FontRenderContext(null, false, false); - Rectangle2D bounds = g.getFont().getStringBounds(str, frc); - Graphics g2 = g.create(); - g2.setClip(insets.left, insets.top, (int)((float)width * fraction), height); - g2.setColor(progressForeground); - g2.drawString(str, - insets.left + (int)((double)width - bounds.getWidth()) / 2, - (int)((float)insets.top + lm.getAscent() + ((double)height - bounds.getHeight()) / 2.0 )); - g2.dispose(); - g2 = g.create(); - g2.setClip(insets.left + (int)((float)width * fraction), - insets.top, getWidth() - insets.left - (int)((float)width * fraction), - height); - g2.setColor(getForeground()); - g2.drawString(str, - insets.left + (int)((double)width - bounds.getWidth()) / 2, - (int)((float)insets.top + lm.getAscent() + ((double)height - bounds.getHeight()) / 2.0)); - g2.dispose(); - } - } --- 69,74 ---- } } + --- NEW FILE: FadeLabel.java --- /* *##% * Copyright (C) 2006 * Paolo Marrone, Benjamin Poussin and the SwiXAT team * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * *##%*/ /* * * FadeLabel.java * * Created: 30 mars 2006 02:31:47 * * @author JP * @version $Revision: 1.1.2.1 $ * * Last update: $Date: 2007/05/05 06:19:20 $ * by : $Author: j--p $ */ package org.swixat.widget; import org.swixat.framework.MessageFadeTimer; import javax.swing.*; import java.awt.*; /** * A JLabel whose text fades away after a specified delay. * Suitable for status bars and other such temporary displays. */ public class FadeLabel extends JLabel { private int delay = 3000; private MessageFadeTimer messageFader = null; private Color initialForeground; public FadeLabel() { super(); initialForeground = getForeground(); } public void setText(String messageText) { super.setText(messageText); if (initialForeground != null) { // cancel fader if already running if (messageFader != null) { messageFader.cancel(); } setForeground(initialForeground); // fadeout message after a few seconds messageFader = new MessageFadeTimer(this, delay); messageFader.start(); } } public Color getInitialForeground() { return initialForeground; } public void setInitialForeground(Color initialForeground) { this.initialForeground = initialForeground; } public int getDelay() { return delay; } public void setDelay(int delay) { if (delay > 0) { this.delay = delay; } } } |