/* * ColorSetting.java * * Created on May 12, 2006, 1:40 PM */ package Current.popups.Settings.Items; import java.awt.*; import java.awt.event.*; import java.awt.geom.Rectangle2D; import java.awt.geom.Line2D; import java.lang.reflect.*; import org.pat.graphics.boxed.ListenString; import Current.manage.*; /** * * @author pat */ public class BooleanSetting extends Canvas implements MouseListener { public SettingsObject sobj; public Field F; public Manager M; public static final String[] truefalse={"true","false"}; private ListenString LS[]; private static int padding=4; /** Creates a new instance of ColorSetting * @param M the Manager * @param C the containing Canvas * @param title the text to display to refer to this setting * @param key the name of the key used by McbSettings * @deafult_color fall-back color in case the key is not in McbSettings */ public BooleanSetting(Manager M, SettingsObject sobj, Field F) { this.M=M; this.sobj=sobj; this.F=F; LS=new ListenString[2]; String name=null; try { name=(String) (sobj.getType().getDeclaredField(F.getName()+"_name").get(sobj.getObject())); } catch (Throwable T) { name=F.getName(); } LS=new ListenString[2]; setFont(new Font("sanserif", Font.PLAIN, 12)); LS[0]=new ListenString(name,this); LS[1]=new ListenString("",truefalse,this); LS[0].setColors(Color.black,Color.white); LS[1].setColors(Color.black,Color.orange); LS[0].setLeft(padding); LS[0].setTop(padding); LS[1].setLeft(LS[0].getRight()+padding); LS[1].setTop(padding); setSize((int)(LS[1].getRight()+padding), (int)(LS[1].getBottom()+padding)); setBackground(new Color(0,0,70)); this.addMouseListener(this); } public Dimension getPreferredSize() { return new Dimension((int)(LS[1].getRight()+padding), (int)(LS[1].getBottom()+padding)); } public void setBoolean(boolean b) { try { F.setBoolean(sobj.set_obj, b); } catch (Throwable T) { T.printStackTrace(); System.exit(0); } M.mcbSend(sobj.getObject()); } public void toggleBoolean() { try { setBoolean(! F.getBoolean(sobj.set_obj)); } catch (Throwable T) { T.printStackTrace(); System.exit(0); } } public void paint(Graphics gfx) { Graphics2D g=(Graphics2D) gfx; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(new Color(0,127,255)); g.draw(new Line2D.Float((float)0,(float)0, (float)(getWidth()-1),(float)0)); g.draw(new Line2D.Float((float)0,(float)(getHeight()-1), (float)(getWidth()-1),(float)(getHeight()-1))); try { if (F.getBoolean(sobj.set_obj)) { LS[1].str=truefalse[0]; } else { LS[1].str=truefalse[1]; } } catch (Throwable T) { T.printStackTrace(); } for (int i=0; i<2; i++) { LS[i].render(g); } } public void mouseClicked(MouseEvent e) { if (LS[1].contains(e)) { toggleBoolean(); repaint(); } } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } }