/***************************************************************************** SelectorTest An application to test the ObjectList class, with different mouse sensitive areas within each item. bruce.miller@nist.gov Contribution of the National Institute of Standards and Technology, not subject to copyright. ******************************************************************************/ package test; import java.applet.*; import java.awt.*; import java.util.*; import gui.*; import util.*; public class SelectorTest extends Applet { SelectionList selector; int nSelections = 20; public void init() { setLayout(new BorderLayout()); Label label = new Label("ObjectList Demo & Tester"); label.setFont(new Font("Helvetica",Font.BOLD,14)); add("North",label); add("Center",selector = new SelectionList(this,50,15)); for(int i=0; i< nSelections; i++) selector.addItem(new Selection(Format.US(i))); } } class Selection { String name; int state; static final int IGNORE = 0; static final int REQUIRE = 1; static final int FORBID = 2; static final String descriptor[] = {"ignore","require","forbid"}; Selection(String name) { this.name = name; } void toggleRequire() { state = (state == REQUIRE ? IGNORE : REQUIRE); } void toggleForbid() { state = (state == FORBID ? IGNORE : FORBID); } void toggle(int type) { switch (type) { case REQUIRE : toggleRequire(); break; case FORBID : toggleForbid(); break; }} } class SelectionList extends ObjectList { Applet applet; SelectionList(Applet applet, int width, int height) { super(width,height); this.applet = applet; } public String getString(Object item) { return ((Selection) item).name; } void drawFigure(Graphics g, int corners[][], int x, int y, boolean filled) { g.translate(x,y); if (filled) { g.setColor(Color.red); g.fillPolygon(corners[0],corners[1],corners[0].length); } g.setColor(Color.black); g.drawPolygon(corners[0],corners[1],corners[0].length); g.translate(-x,-y); } int figureWidth = 14, figureHeight = 14; int cross[][] = {{2,7,12,14,9,14,12,7,2,0,5,0,2}, {0,5,0,2,7,12,14,9,14,12,7,2,0}}; int check[][] = {{4,14,4,2,0,4}, {14,0,10,6,10,14}}; int x0 = 0, // where the Require widget goes x1 = x0+2+figureWidth, // where the Forbit widget goes x2 = x1 +2 + figureWidth; // where the string goes. int mouseZone(Event e) { int x = e.x-itemBounds(e.arg).x; return ((x0 < x) && (x < x1) ? Selection.REQUIRE : (x1 < x) && (x < x2) ? Selection.FORBID : Selection.IGNORE); } public Dimension paintItem(Graphics g, Object item) { Selection s = (Selection) item; String name = s.name; int state = s.state; FontMetrics m = getFontMetrics(getFont()); int h = Math.max(figureHeight,m.getHeight()); int y = Math.max(figureHeight,h-m.getDescent()); if (g != null) { drawFigure(g,check,x0,0,state==Selection.REQUIRE); drawFigure(g,cross,x1,0,state==Selection.FORBID); g.drawString(name,x2,y); } return new Dimension(x2 + m.stringWidth(name),h+2); } void highlightZone(Selection s, int zone) { Rectangle p = itemBounds(s); switch (zone) { case Selection.REQUIRE : highlightRect(p.x+x0,p.y,figureHeight,figureWidth); break; case Selection.FORBID : highlightRect(p.x+x1,p.y,figureHeight,figureWidth); break; }} int prevZone = 0; String opString(Selection s, int zone) { return (s.state == zone ? "un" : "") + Selection.descriptor[zone] + " feature " + s.name; } public boolean handleEvent(Event e) { if ((e.id == Event.LIST_SELECT) || (e.id == Event.ACTION_EVENT)) { deselect(e.arg); ((Selection) e.arg).toggle(mouseZone(e)); repaint(); } else if (e.id == ObjectList.MOUSE_EXIT_ITEM) { applet.showStatus(""); highlightZone((Selection) e.arg, prevZone); } else if (e.id == ObjectList.MOUSE_ENTER_ITEM) { Selection s = (Selection) e.arg; int zone = mouseZone(e); highlightZone(s, prevZone = zone); applet.showStatus(opString(s,zone)); } else if (e.id == ObjectList.MOUSE_MOVE_ITEM) { Selection s = (Selection) e.arg; int zone = mouseZone(e); if (zone != prevZone) { highlightZone(s,prevZone); highlightZone(s,prevZone = zone); applet.showStatus(opString(s,zone)); }} else return super.handleEvent(e); return true; } }