/* * @(#)CLegendUI.java 0.0.1 99/07/29 * * Copyright (c) 1999 by Willie Wheeler. All rights reserved. */ package stats.plaf.concrete; import java.awt.*; import javax.swing.*; import javax.swing.plaf.*; import stats.*; import stats.plaf.*; /** * @version 0.0.1 07/29/99 * @author Willie Wheeler */ public class CLegendUI extends LegendUI { protected static final int XPAD = 5, YPAD = 5; protected static ComponentUI ui = new CLegendUI(); public static ComponentUI createUI(JComponent c) { return ui; } public void installUI(JComponent c) { c.setOpaque(false); } public void uninstallUI(JComponent c) { } public void paint(Graphics g, JComponent c) { Legend legend = (Legend)c; int legendWidth = legend.getSize().width; int legendHeight = legend.getSize().height; int numCats = legend.getNumStates(); FontMetrics fm = legend.getFontMetrics(legend.getFont()); int lineHeight = (int)(1.2 * fm.getHeight()); int boxSide = fm.getHeight() - fm.getLeading(); // Center legend vertically. int allLinesHeight = (numCats+1) * lineHeight; int yOff = Math.max(YPAD, (legendHeight - allLinesHeight) / 2); g.translate(0, yOff); // Paint variable name, centered horizontally. String varName = legend.getIndependentsAsString(); int xOff = Math.max(XPAD, (legendWidth - fm.stringWidth(varName)) / 2); g.setColor(legend.getTextColor()); g.drawString(varName, xOff, boxSide - 3); // Center boxes and categories horizontally, and drop down one line. int maxCatWidth = 0; for (int i = 0; i < numCats; i++) { int catWidth = fm.stringWidth(legend.getStateName(i)); maxCatWidth = Math.max(catWidth, maxCatWidth); } int boxPlusCatsWidth = boxSide + XPAD + maxCatWidth; xOff = Math.max(XPAD, (legendWidth - boxPlusCatsWidth) / 2); g.translate(xOff, lineHeight); // Paint boxes and categories. for (int i = 0; i < numCats; i++) { int y = i * lineHeight; g.setColor(legend.getStateColor(i)); g.fillRect(0, y, boxSide, boxSide); g.setColor(legend.getTextColor()); g.drawRect(0, y, boxSide, boxSide); g.drawString(legend.getStateName(i), boxSide + XPAD, y + boxSide - 3); } } public Dimension getPreferredSize(JComponent c) { Legend legend = (Legend)c; int numCats = legend.getNumStates(); FontMetrics fm = legend.getFontMetrics(legend.getFont()); // Compute preferred width = max(var name width, category widths). int varNameWidth = fm.stringWidth(legend.getIndependentsAsString()); int maxCatWidth = 0; for (int i = 0; i < numCats; i++) { int catWidth = fm.stringWidth(legend.getStateName(i)); maxCatWidth = Math.max(catWidth, maxCatWidth); } int boxSide = fm.getHeight() - fm.getLeading(); int prefWidth = Math.max(varNameWidth, boxSide + XPAD + maxCatWidth) + 2*XPAD; // Compute preferred height. int lineHeight = (int)(1.2 * fm.getHeight()); int prefHeight = (numCats+1)*lineHeight + 2*YPAD; // Return preferred size. return new Dimension(prefWidth, prefHeight); } public Dimension getMinimumSize(JComponent c) { return getPreferredSize(c); } public Dimension getMaximumSize(JComponent c) { return getPreferredSize(c); } }