1 18 19 package org.objectweb.jac.aspects.gui.swing; 20 21 import java.awt.Component ; 22 import java.awt.Dimension ; 23 import java.awt.FontMetrics ; 24 import javax.swing.JComponent ; 25 import org.objectweb.jac.aspects.gui.Length; 26 import org.objectweb.jac.aspects.gui.Unit; 27 28 public class SwingUtils { 29 public static int getPixelLength(Length l, Component c) { 30 FontMetrics metrics = c.getFontMetrics(c.getFont()); 31 if (l.unit == Unit.EM) { 32 return (int)(l.value * metrics.getHeight()); 33 } else if (l.unit == Unit.EX) { 34 return (int)(l.value * metrics.charWidth('x')); 35 } else if (l.unit == Unit.PX) { 36 return (int)l.value; 37 } else { 38 throw new RuntimeException ("Unhandled unit: "+l.unit); 39 } 40 } 41 42 public static void setSize(JComponent c, Length width, Length height) { 43 if (width!=null || height!=null) { 44 Dimension dim = c.getPreferredSize(); 45 if (width!=null) 46 dim.width = SwingUtils.getPixelLength(width,c); 47 if (height!=null) 48 dim.height = SwingUtils.getPixelLength(height,c); 49 c.setPreferredSize(dim); 50 } 51 } 52 53 public static void setColumns(javax.swing.JTextField textField, Length width) { 54 if (width != null && width.unit == Unit.EX) 55 textField.setColumns((int)width.value); 56 } 57 } 58 | Popular Tags |