1 package prefuse.util.ui; 2 3 import java.awt.Color ; 4 import java.awt.Component ; 5 import java.awt.ComponentOrientation ; 6 import java.awt.Container ; 7 import java.awt.Font ; 8 import java.awt.event.InputEvent ; 9 10 import javax.swing.Box ; 11 import javax.swing.BoxLayout ; 12 import javax.swing.JComponent ; 13 import javax.swing.UIManager ; 14 15 20 public class UILib { 21 22 24 27 private UILib() { 28 } 30 31 43 50 public static boolean isButtonPressed(InputEvent e, int button) { 51 return (e.getModifiers() & button) == button; 52 } 53 54 59 public static final void setPlatformLookAndFeel() { 60 try { 61 String laf = UIManager.getSystemLookAndFeelClassName(); 62 UIManager.setLookAndFeel(laf); 63 } catch ( Exception e ) {} 64 } 65 66 77 public static Box getBox(Component [] c, boolean horiz, 78 int margin, int spacing) 79 { 80 return getBox(c, horiz, margin, margin, spacing); 81 } 82 83 95 public static Box getBox(Component [] c, boolean horiz, 96 int margin1, int margin2, int spacing) 97 { 98 Box b = new Box (horiz ? BoxLayout.X_AXIS : BoxLayout.Y_AXIS); 99 addStrut(b, horiz, margin1); 100 for ( int i=0; i<c.length; ++i ) { 101 if ( i > 0 ) { 102 addStrut(b, horiz, spacing); 103 addGlue(b, horiz); 104 } 105 b.add(c[i]); 106 } 107 addStrut(b, horiz, margin2); 108 return b; 109 } 110 111 119 public static void addStrut(JComponent b, boolean horiz, int size) { 120 if ( size < 1 ) return; 121 b.add(horiz ? Box.createHorizontalStrut(size) 122 : Box.createVerticalStrut(size) ); 123 } 124 125 132 public static void addGlue(JComponent b, boolean horiz) { 133 b.add(horiz ? Box.createHorizontalGlue() 134 : Box.createVerticalGlue()); 135 } 136 137 148 public static void addStrut(JComponent b, int layout, int size) { 149 if ( size < 1 ) return; 150 b.add( getAxis(b, layout) == BoxLayout.X_AXIS 151 ? Box.createHorizontalStrut(size) 152 : Box.createVerticalStrut(size) ); 153 } 154 155 165 public static void addGlue(JComponent b, int layout) { 166 b.add( getAxis(b, layout) == BoxLayout.X_AXIS 167 ? Box.createHorizontalGlue() 168 : Box.createVerticalGlue()); 169 } 170 171 183 public static int getAxis(JComponent c, int layout) { 184 ComponentOrientation o = c.getComponentOrientation(); 185 switch ( layout ) { 186 case BoxLayout.LINE_AXIS: 187 return o.isHorizontal() ? BoxLayout.X_AXIS : BoxLayout.Y_AXIS; 188 case BoxLayout.PAGE_AXIS: 189 return o.isHorizontal() ? BoxLayout.Y_AXIS : BoxLayout.X_AXIS; 190 default: 191 return layout; 192 } 193 } 194 195 202 public static void setColor(Component c, Color back, Color fore) { 203 c.setBackground(back); 204 c.setForeground(fore); 205 if ( c instanceof Container ) { 206 Container con = (Container )c; 207 for ( int i=0; i<con.getComponentCount(); ++i ) 208 setColor(con.getComponent(i), back, fore); 209 } 210 } 211 212 218 public static void setFont(Component c, Font font) { 219 c.setFont(font); 220 if ( c instanceof Container ) { 221 Container con = (Container )c; 222 for ( int i=0; i<con.getComponentCount(); ++i ) 223 setFont(con.getComponent(i), font); 224 } 225 } 226 227 } | Popular Tags |