/* * OptionButton.java - Option button controls. * * Copyright (c) 1996 Chuck McManis, All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. * * CHUCK MCMANIS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. CHUCK MCMANIS * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT * OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ import java.applet.Applet; import java.awt.*; import java.util.Random; import java.util.Vector; import util.comm.*; /** * This class implements a simple exclusive choice control. It uses * the data channel to determine if it should be off or on. */ public class OptionButton extends VJApplet implements Runnable { Thread updater = null; DataChannel rr = null; Vector dc = new Vector(); Font labelFont = new Font("helvetica", Font.BOLD, 14); String myLabel = null; Dimension mySize = new Dimension(0, 0); int myID; String myGroup; boolean currentState = false; public Dimension preferredSize() { return mySize; } /** * Parse a option message to send off on select * Format is "DataChannelName:" */ OptionMessage parseOption(String op) { String chan = op.substring(0, op.indexOf(':')); String msg = op.substring(op.indexOf(':')+1); System.out.println("Message parsed was '"+chan+"' and '"+msg+"'"); return new OptionMessage(chan, msg); } public void init() { String x; super.init(); System.out.println("OptionButton:: init."); myLabel = getParameter("label"); x = getParameter("id"); if (x == null) { System.out.println("MISSING ID, which is required."); myID = -1; } else { try { myID = Integer.parseInt(x); } catch (NumberFormatException ne) { System.out.println("Non-integer ID."); myID = -2; } } myGroup = getParameter("group"); if (myGroup == null) myGroup = "default"; for (int i=0; true; i++) { String ts = getParameter("msg"+i); if (ts == null) break; OptionMessage om = parseOption(ts); if (x != null) dc.addElement(om); } } void drawX(Graphics g) { g.setColor(Color.black); g.drawLine(3, 3, 9, 9); g.drawLine(3, 4, 8, 9); g.drawLine(4, 3, 9, 8); g.drawLine(3, 9, 9, 3); g.drawLine(4, 9, 9, 4); g.drawLine(3, 8, 8, 3); } public void paint(Graphics g) { g.setColor(bgColor); g.fillRect(0, 0, myWidth, myHeight); g.setColor(dkColor); g.drawArc(0, 0, 12, 12, 45, 180); g.drawArc(1, 1, 11, 11, 45, 180); g.setColor(brColor); g.drawArc(0, 0, 12, 12, 45, -180); g.drawArc(1, 1, 11, 11, 45, -180); if (currentState) drawX(offscreen); g.setColor(Color.black); if (myLabel != null) g.drawString(myLabel, 15, 10); } public void start() { updater = new Thread(this); updater.setName("OptionButton Thread"); rr = DataChannel.getChannel(myGroup, updater, 1); updater.start(); } public void stop() { updater = null; } public void run() { while (Thread.currentThread() == updater) { repaint(); try { int press; press = ((Integer) rr.getValue()).intValue(); currentState = (press == myID); } catch (DataChannelOverrun e) { System.out.println("OptionButton : OVERRUN!"); } catch (DataChannelException ee) { System.out.println("We're dead."); ee.printStackTrace(); } if (currentState && (dc.size() > 0)) { for (int i = 0; i < dc.size(); i++) { ((OptionMessage)(dc.elementAt(i))).send(); } } } } public boolean mouseDown(Event ev, int x, int y) { currentState = true; rr.putValue(new Integer(myID)); return true; } } class OptionMessage { String msg; DataChannel dc; public OptionMessage(String c, String m) { dc = DataChannel.getChannel(c); msg = m; } void send() { dc.putValue(msg); } }