package com.rle.monitor; /* * RLE Surface Crack Detector Graphing Software * * Copyright (C) 2002 RLE Technologies * For more information visit http://www.rletech.com/ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, * USA. */ /** * @author Andy Goth * @author Eric Peterson * @author Matt Lane * @version 0.1 */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.plaf.*; import javax.swing.plaf.basic.*; import javax.swing.plaf.metal.*; /** UI for my custom checkbox. */ public class MyCheckBoxUI extends javax.swing.plaf.metal.MetalCheckBoxUI { static private Icon icon = new ImageIcon("images/check.png"); public MyCheckBoxUI() {} public MyCheckBoxUI(MyCheckBox b) {} public static ComponentUI createUI(JComponent b) { return new MyCheckBoxUI((MyCheckBox)b); } public void paint(Graphics g, JComponent c) { if (g == null) return; AbstractButton b = (AbstractButton)c; ButtonModel model = b.getModel(); Dimension size = c.getSize(); int w = size.width; int h = size.height; Font f = c.getFont(); g.setFont(f); FontMetrics fm = g.getFontMetrics(); Rectangle viewRect = new Rectangle(size); Rectangle iconRect = new Rectangle(1, 1, icon.getIconWidth() + 4, h - 2); Rectangle textBox = new Rectangle(icon.getIconWidth() + 5, 1, w - icon.getIconWidth() - 6, h - 2); Rectangle textRect = new Rectangle((int)(textBox.getX() + 3), (int)(textBox.getY() + (textBox.getHeight() - fm.getHeight()) / 2 + 1), fm.stringWidth(b.getText()), fm.getHeight()); /* Draw! */ Color textColor = null; if (!model.isEnabled()) { g.setColor(MetalLookAndFeel.getControlShadow()); g.drawRect(0, 0, w, h); textColor = MetalLookAndFeel.getControlShadow(); if (!model.isSelected()) { /* Disabled, unselected */ } else { /* Disabled, selected */ } } else { MyUtils.drawSunkenBorder(g, viewRect); MyUtils.drawRaisedBorder(g, textBox); textColor = b.getForeground(); if (model.isPressed() && model.isArmed()) { /* Enabled, held down */ MyUtils.drawPressedBorder(g, iconRect); g.setColor(MetalLookAndFeel.getControlShadow()); g.fillRect(2, 2, iconRect.width - 2, iconRect.height - 2); } else { /* Enabled, not held down */ MyUtils.drawRaisedBorder(g, iconRect); } if (model.isSelected()) { /* Enabled, selected */ icon.paintIcon(c, g, 3, (h - icon.getIconHeight()) / 2); } else { /* Enabled, unselected */ } } g.setColor(textColor); BasicGraphicsUtils.drawString(g, b.getText(), model.getMnemonic(), textRect.x, textRect.y + fm.getAscent()); if (b.hasFocus() && b.isFocusPainted()) { /* Focused */ g.setColor(getFocusColor()); g.drawRect(textRect.x - 1, textRect.y - 1, textRect.width + 1, textRect.height + 1); } } public Dimension getPreferredSize(JComponent c) { FontMetrics fm = c.getFontMetrics(c.getFont()); int width = 12 + icon.getIconWidth() + fm.stringWidth(((MyCheckBox)c).getText()); int height = 8 + fm.getHeight(); if ((height & 1) == 1) height++; return new Dimension(width, height); } }