package causation.lab.plaf.basic; // packages import javax.swing.*; import javax.swing.plaf.*; import java.awt.*; import java.net.URL; import java.net.MalformedURLException; // custom classes import causation.lab.WBVariable; import causation.lab.plaf.WBVariableUI; /** *

* The L&F for WBVariables. * Copyright 1999 by David Danks. All rights reserved. *

* * @see WBVariable * @see WBVariableUI * @version 0.9 Aug 30, 1999 * @author David Danks */ public class BasicWBVariableUI extends WBVariableUI { /////////////////// Static Class Variables /////////////////// private static final Color BACKGROUND = new Color(153, 204, 153); private static final Color HIGHLIGHT_BACKGROUND = new Color(255, 204, 102); private static final Color BORDER = new Color(102, 102, 102, 102); private static final Color ENABLED_TEXT = Color.black; private static final Color DISABLED_TEXT = new Color(102, 102, 102); private static final int BORDER_WIDTH = 1; private static final int INTERIOR_LINE_WIDTH = 1; private static final int VALUE_LOCK_GAP = 5; // this margin is the margin _inside_ the border private static final Insets margin = new Insets(3, 3, 3, 3); private static final String ICON_DIR = "icons/"; private static final String ENABLED_LOCK_TRANSPARENT = "locktrans.gif"; private static final String DISABLED_LOCK_TRANSPARENT = "locktransoff.gif"; private static final int DEFAULT_LOCK_SIZE = 16; /////////////////// Instance Variables /////////////////// private URL codebase = null; private Icon nameIcon = null; private ImageIcon enabledLockIcon = null; private ImageIcon disabledLockIcon = null; /////////////////// Constructor /////////////////// /** * Currently does not do any initialization */ public BasicWBVariableUI() { ; } /////////////////// Class Methods /////////////////// /** * @param c The JComponent the BasicWBVariableUI is being created for * @return A new instance of BasicWBVariableUI */ public static ComponentUI createUI(JComponent c) { return new BasicWBVariableUI(); } /////////////////// Instance Methods /////////////////// /** * Currently does not do anything * @param c The JComponent that will use the BasicWBVariableUI */ public void installUI(JComponent c) { ; } /** * Currently does not do anything * @param c The JComponent that is no longer using the BasicWBVariableUI */ public void uninstallUI(JComponent c) { ; } /** * @return The "name" icon */ public Icon getNameIcon() { return nameIcon; } /** * @param The new "name" icon */ public void setNameIcon(Icon newIcon) { if (newIcon == nameIcon) return; nameIcon = newIcon; } /** * @return The disabled lock icon */ public Icon getDisabledLockIcon() { return disabledLockIcon; } /** * @return The codebase used to load lock icons */ public URL getCodebase() { return codebase; } /** * @param newBase The new codebase from which to load lock icons */ public void setCodebase(URL newBase) { if (newBase == codebase) return; codebase = newBase; // now update the lock icons enabledLockIcon = null; disabledLockIcon = null; if (codebase == null) return; try { enabledLockIcon = new ImageIcon(new URL(codebase+ICON_DIR+ ENABLED_LOCK_TRANSPARENT)); disabledLockIcon = new ImageIcon(new URL(codebase+ICON_DIR+ DISABLED_LOCK_TRANSPARENT)); } catch (MalformedURLException m) { System.out.println("Bad lock icon URL: "+m.toString()); return; } // now, check to see if the Icons loaded... if (enabledLockIcon.getIconWidth() < 0) enabledLockIcon = null; if (disabledLockIcon.getIconWidth() < 0) disabledLockIcon = null; } /** * @return The preferred size of the given WBVariable */ public Dimension getPreferredSize(WBVariable v) { FontMetrics fm = v.getFontMetrics(v.getFont()); int height = fm.getHeight()+margin.top+margin.bottom+2*BORDER_WIDTH+ INTERIOR_LINE_WIDTH; if (nameIcon == null) height += fm.getHeight(); else height += nameIcon.getIconHeight(); int width = margin.left+margin.right+2*BORDER_WIDTH; if (nameIcon == null) width += fm.stringWidth(v.getName()); else width += nameIcon.getIconWidth(); String value = (v.getActiveValue() == null) ? "?" : v.getActiveValue(); int lowWidth = margin.left+margin.right+2*BORDER_WIDTH+ fm.stringWidth(value); if (v.isLocked()) { lowWidth += VALUE_LOCK_GAP; if (v.isExperimenting() && (enabledLockIcon != null)) { lowWidth += enabledLockIcon.getIconWidth(); if (fm.getHeight() < enabledLockIcon.getIconHeight()) height += enabledLockIcon.getIconHeight()-fm.getHeight(); } else if (v.isExperimenting()) { // so icon == null lowWidth += DEFAULT_LOCK_SIZE; if (fm.getHeight() < DEFAULT_LOCK_SIZE) height += DEFAULT_LOCK_SIZE-fm.getHeight(); } else if (!(v.isExperimenting()) && (disabledLockIcon != null)) { lowWidth += disabledLockIcon.getIconWidth(); if (fm.getHeight() < disabledLockIcon.getIconHeight()) height += disabledLockIcon.getIconHeight()-fm.getHeight(); } else if (!(v.isExperimenting())) { // so icon == null lowWidth += DEFAULT_LOCK_SIZE; if (fm.getHeight() < DEFAULT_LOCK_SIZE) height += DEFAULT_LOCK_SIZE-fm.getHeight(); } } width = Math.max(width, lowWidth); return new Dimension(width, height); } /** * @return The minimum size of the given WBVariable */ public Dimension getMinimumSize(WBVariable v) { return getPreferredSize(v); } /** * Does all of the painting for the WBVariable. Draws an icon (if there * is one) instead of the variable name. Note: The border gets drawn * last since this makes it easier to ensure that nothing overwrites it. * @param g The Graphics context in which to paint the WBVariable * @param c The WBVariable that is being painted */ public void paint(Graphics g, JComponent c) { if (!(c instanceof WBVariable)) return; WBVariable variable = (WBVariable)c; // draw the background if (variable.isHighlight()) g.setColor(HIGHLIGHT_BACKGROUND); else g.setColor(BACKGROUND); g.fillRect(BORDER_WIDTH, BORDER_WIDTH, c.getWidth()-2*BORDER_WIDTH-1, c.getHeight()-2*BORDER_WIDTH-1); FontMetrics fm = variable.getFontMetrics(variable.getFont()); // draw the name/icon g.setColor(ENABLED_TEXT); int yOffset = margin.top+BORDER_WIDTH; if (nameIcon == null) { int inset = c.getWidth()-2*BORDER_WIDTH-margin.left-margin.right - fm.stringWidth(variable.getName()); inset /= 2; g.drawString(variable.getName(), margin.left+BORDER_WIDTH+inset, fm.getAscent()+yOffset); } else { int w = nameIcon.getIconWidth(); int inset = c.getWidth()-2*BORDER_WIDTH-margin.left-margin.right- nameIcon.getIconWidth(); inset /= 2; nameIcon.paintIcon(c, g, margin.left+BORDER_WIDTH+inset, yOffset); } // use the same increase so all the lines are the same yOffset += fm.getHeight(); // draw the separating line for (int i=0; i fm.getHeight())) yAdjust = (lock.getIconHeight()-fm.getHeight()) / 2; else if ((lock == null) && (DEFAULT_LOCK_SIZE > fm.getHeight())) yAdjust = (DEFAULT_LOCK_SIZE-fm.getHeight()) / 2; } g.drawString(value, margin.left+BORDER_WIDTH+inset, yOffset+yAdjust+fm.getAscent()); // draw the lock, if necessary if (variable.isLocked()) { int x = margin.left+BORDER_WIDTH+inset+fm.stringWidth(value)+ VALUE_LOCK_GAP; int y = yOffset; if (yAdjust == 0) { // need to vertically adjust the lock if (lock != null) y += (fm.getHeight()-lock.getIconHeight())/2; else y += (fm.getHeight()-DEFAULT_LOCK_SIZE)/2; } if (lock != null) lock.paintIcon(variable, g, x, y); else { // draw the default lock at (x,y)=upper-left corner g.drawRect(x, y+DEFAULT_LOCK_SIZE/2, DEFAULT_LOCK_SIZE, DEFAULT_LOCK_SIZE/2); g.drawArc(x+DEFAULT_LOCK_SIZE/4, y, DEFAULT_LOCK_SIZE/2, DEFAULT_LOCK_SIZE, 0, 180); } } // draw the border g.setColor(BORDER); for (int i=0; i