home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 November
/
Chip_1998-11_cd.bin
/
tema
/
Cafe
/
jfc.bin
/
BasicProgressBarUI.java
< prev
next >
Wrap
Text File
|
1998-02-26
|
8KB
|
288 lines
/*
* @(#)BasicProgressBarUI.java 1.32 98/02/02
*
* Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
* SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*
*/
package com.sun.java.swing.plaf.basic;
import java.awt.*;
import com.sun.java.swing.*;
import com.sun.java.swing.event.*;
import com.sun.java.swing.plaf.*;
import java.io.Serializable;
/**
* A Basic L&F implementation of ProgressBarUI. This implementation
* is both the view and the controller.
* <p>
* Warning: serialized objects of this class will not be compatible with
* future swing releases. The current serialization support is appropriate
* for short term storage or RMI between Swing1.0 applications. It will
* not be possible to load serialized Swing1.0 objects with future releases
* of Swing. The JDK1.2 release of Swing will be the compatibility
* baseline for the serialized form of Swing objects.
*
* @version 1.32 02/02/98
* @author Rob Davis
*/
public class BasicProgressBarUI extends ProgressBarUI
implements ChangeListener, Serializable
{
int cachedPercent;
static final Dimension PREFERRED_INNER_HORIZONTAL = new Dimension(146, 12);
static final Dimension PREFERRED_INNER_VERTICAL = new Dimension(12, 146);
public static ComponentUI createUI(JComponent x) {
return new BasicProgressBarUI();
}
public void installUI(JComponent c) {
installDefaults(c);
installListeners(c);
}
public void uninstallUI(JComponent c) {
uninstallListeners(c);
}
protected void installDefaults(JComponent c) {
c.setOpaque(true);
LookAndFeel.installColorsAndFont(c, "ProgressBar.background",
"ProgressBar.foreground",
"ProgressBar.font");
}
protected void installListeners(JComponent c) {
((JProgressBar)c).addChangeListener(this);
}
protected void uninstallListeners(JComponent c) {
((JProgressBar)c).removeChangeListener(this);
}
protected void uninstallDefaults(JComponent c) {
}
public Dimension getPreferredInnerHorizontal() {
return PREFERRED_INNER_HORIZONTAL;
}
public Dimension getPreferredInnerVertical() {
return PREFERRED_INNER_VERTICAL;
}
public int getCachedPercent() {
return cachedPercent;
}
public void setCachedPercent(int cachedPercent) {
this.cachedPercent = cachedPercent;
}
public int getBorderBuffer() {
return 0;
}
public int getCellLength() {
return 1;
}
public int getCellSpacing() {
return 0;
}
public void paint(Graphics g, JComponent c) {
Dimension size = c.getSize();
Dimension innerSize;
int x, y;
JProgressBar progressBar = (JProgressBar)c;
BoundedRangeModel model = progressBar.getModel();
Insets b = getBorderInsets(c);
x = b.left;
y = b.top;
innerSize = new Dimension(size.width - (b.left + b.right),
size.height - (b.top + b.bottom));
g.setColor(c.getForeground());
if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
long span = model.getMaximum() - model.getMinimum();
int length = 0;
if (span != 0) {
double bigWidth = innerSize.width;
double bigValue = model.getValue();
double fractionComplete = bigValue / span;
length = (int)(bigWidth * fractionComplete);
}
int max = (x + length) - getCellLength();
int current;
int increment = getCellLength() + getCellSpacing();
for (current = x; current < max; current += increment) {
g.fillRect(current, y, getCellLength(), innerSize.height);
}
if (length == innerSize.width && current < (innerSize.width + x)) {
g.fillRect(current, y, (innerSize.width + x) - current,
innerSize.height);
}
} else {
int span = model.getMaximum() - model.getMinimum();
int length = 0;
if (span != 0) {
double bigHeight = innerSize.height;
double bigValue = model.getValue();
double fractionComplete = bigValue / span;
length = (int)(bigHeight * fractionComplete);
}
int min = ((innerSize.height - 1) + y) - length;
int current;
int increment = getCellLength() + getCellSpacing();
for (current = (innerSize.height - 1) + (y - getCellLength());
current > min; current -= increment)
{
g.fillRect(x, current, innerSize.width, getCellLength());
}
if (length == innerSize.height) {
g.fillRect(x, y, innerSize.width, (current + getCellLength()) - y);
}
}
if (progressBar.isBorderPainted()) {
BasicGraphicsUtils.drawEtchedRect(g, 0,0, size.width, size.height);
}
}
public Dimension getPreferredSize(JComponent c) {
Dimension size;
JProgressBar progressBar = (JProgressBar)c;
Insets border = getBorderInsets(c);
if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
size = new Dimension(getPreferredInnerHorizontal());
} else {
size = new Dimension(getPreferredInnerVertical());
}
size.width += border.left + border.right;
size.height += border.top + border.bottom;
return size;
}
public Dimension getMinimumSize(JComponent c) {
Dimension pref = getPreferredSize(c);
JProgressBar progressBar = (JProgressBar)c;
if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
pref.width = 0;
} else {
pref.height = 0;
}
return pref;
}
public Dimension getMaximumSize(JComponent c) {
Dimension pref = getPreferredSize(c);
JProgressBar progressBar = (JProgressBar)c;
if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
pref.width = Short.MAX_VALUE;
} else {
pref.height = Short.MAX_VALUE;
}
return pref;
}
public Insets getInsets(JComponent c) {
return getBorderInsets(c);
}
public Insets getBorderInsets(JComponent c) {
JProgressBar bar = (JProgressBar)c;
if (bar.isBorderPainted()) {
Insets retval;
retval = (Insets)BasicGraphicsUtils.getEtchedInsets().clone();
retval.left += getBorderBuffer();
retval.right += getBorderBuffer();
retval.top += getBorderBuffer();
retval.bottom += getBorderBuffer();
return retval;
} else {
return new Insets(0, 0, 0, 0);
}
}
//
// Change Events
//
public void stateChanged(ChangeEvent e) {
JProgressBar bar = (JProgressBar)e.getSource();
BoundedRangeModel model = bar.getModel();
int newRange = model.getMaximum() - model.getMinimum();
int newPercent = (newRange > 0) ? ((100 * model.getValue()) / newRange) : 0;
if (newPercent != getCachedPercent()) {
int minPercent = Math.min(newPercent, getCachedPercent());
int maxPercent = Math.max(newPercent, getCachedPercent());
Insets border = getBorderInsets(bar);
Dimension size = bar.getSize();
if (bar.getOrientation() == JProgressBar.HORIZONTAL) {
int length = (size.width - (border.right + border.left));
int x1 = (minPercent * length) / 100;
int x2 = (maxPercent * length) / 100;
int cell = getCellLength() + getCellSpacing();
x1 = ((x1 / cell) * cell) + border.left;
x2 = (((x2 / cell) + 1) * cell) + border.left;
// Pending(arnaud). Jeff, something is wrong with
// that code. (x2 - x1) might be too small.
// calling repaint() for now
bar.repaint();
//bar.repaint(x1-1, border.top, (x2 - x1) + 2,
//size.height - (border.top + border.bottom));
} else {
int length = size.height - (border.bottom + border.top);
int y1 = (maxPercent * length) / 100;
int y2 = (minPercent * length) / 100;
int cell = getCellLength() + getCellSpacing();
y1 = (length - (((y1 / cell) + 1) * cell)) + border.top;
y2 = (length - ((y2 / cell) * cell)) + border.top;
bar.repaint(border.left, y1-1,
size.width - (border.left + border.right),
(y2 - y1)+2);
}
setCachedPercent(newPercent);
}
}
}