To implement the ability to display pin names, a new class called ActivePin is introduced.
01: public class ActivePin extends ViewerImpl { 02: private String name_; 03: 04: public ActivePin (ActivePinCallback callback, String name, float size) { 05: super(null, callback); 06: name_ = name; 07: row_ = row; 08: col_ = column; 09: 10: FigureStyle style = new FigureStyle(); 11: style.foreground = Color.yellow; 12: style.brushSize = 1; 13: 14: Glyph ring = LayoutKit.align(FigureKit.circle(FigureMode.Fill, style, 0, 0, size), 0.5F, 0.5F); 15: 16: style.foreground = Color.black; 17: Glyph pin = LayoutKit.align(FigureKit.circle(FigureMode.Fill, style, 0, 0, size/2.5F), 0.5F, 0.5F); 18: 19: Glyph pinAndRing = LayoutKit.overlay(); 20: pinAndRing.append(ring); 21: pinAndRing.append(pin); 22: 23: Glyph completePin = LayoutKit.align(LayoutKit.fixed(pinAndRing, size, size), 0F, 0F); 24: 25: setBody(completePin); 26: } 27: 28: public String getName () { 29: return name_; 30: } 31: }
ActivePin keeps its name internally. The pin name can be accessed by the getName() method, on line 28.
When the mouse enters/exits the area of the pin, a MOUSE_ENTER/MOUSE_EXIT event is generated. So that events can be handled, ActivePin inherits from ViewerImpl, which handles them by dispatching them to a handler. In this case, the events are dispatched to an ActivePinCallback object passed to ActivePin in the constructor on line 4.
01: public class ActivePinCallback implements ViewerCallback { 02: private TextField pinNameField_ = null; 03: 04: public ActivePinCallback(TextField pinNameField) { 05: pinNameField_ = pinNameField; 06: } 07: 08: public boolean mouseDown(Viewer v, GraphicsEvent evt, float X, float Y) { 09: return false; 10: } 11: 12: public boolean mouseDrag(Viewer v, GraphicsEvent evt, float X, float Y) { 13: return false; 14: } 15: 16: public boolean mouseUp(Viewer v, GraphicsEvent evt, float X, float Y) { 17: return false; 18: } 19: 20: public boolean mouseMove(Viewer v, GraphicsEvent evt, float X, float Y) { 21: return false; 22: } 23: 24: public boolean mouseEnter (Viewer v, GraphicsEvent event, float x, float y) { 25: ActivePin pin = (ActivePin) v; 26: pinNameField_.setText(pin.getName()); 27: return true; 28: } 29: 30: public boolean mouseExit (Viewer v, GraphicsEvent event, float x, float y) { 31: pinNameField_.setText(""); 32: return true; 33: } 34: 35: public boolean keyDown(Viewer v, GraphicsEvent evt, int key) { 36: return false; 37: } 38: 39: public boolean keyUp(Viewer v, GraphicsEvent evt, int key) { 40: return false; 41: } 42: 43: public boolean action(Viewer v, GraphicsEvent evt, Object what) { 44: return false; 45: } 46: 47: public boolean keyActionDown(Viewer v, GraphicsEvent evt, int key) { 48: return false; 49: } 50: 51: public boolean keyActionUp(Viewer v, GraphicsEvent evt, int key) { 52: return false; 53: } 54: }
ActivePinCallback receives calls from its viewer whenever an event is handled. In this example, ActivePin is its viewer. The mouseEnter(Viewer v, GraphicsEvent evt, float x, float y) method on lines 24-28 first casts the viewer to ActivePin. Then, on line 26, it asks the pin for its name and sets the text field, pinNameField_, to the name. As will be evident in the description below, this text field is the text field displayed underneath the package on the display.
Now for the applet code.
01: public class TutorialExample6 extends Applet { 02: private TextField pinNameField_ = new TextField(20); 03 private ActivePinCallback callback_ = new ActivePinCallback(pinNameField_); 03: 04: public TutorialExample6() { 05: setLayout(new BorderLayout()); 06: setBackground(Color.white); 07: 08: FigureStyle style = new FigureStyle(); 09: style.foreground = Color.yellow; 10: style.brushSize = 1; 11: 12: Glyph verticalBox = LayoutKit.vbox(); 13: 14: for (int row = 0; row < 8; row++) { 15: Glyph horizontalRow = LayoutKit.hbox(); 16: for (int col = 0; col < 8; col++) 17: if (row > 1 && row < 6 && col > 1 && col < 6) { 18: horizontalRow.append(LayoutKit.hspace(32)); 19: } 20: else { 21: String pinName = "Pin " + String.valueOf(row) + "X" + String.valueOf(col); 22: horizontalRow.append( 23: LayoutKit.margin( 24: new ActivePin(callback_, pinName, 20F), 25: 6 26: ) 27: ); 28: } 29: verticalBox.append(horizontalRow); 30: } 31: 32: Glyph part = new Background(verticalBox, Color.gray, true, true); 33: 34: Glyph columnBox = LayoutKit.hbox(); 35: style = new FigureStyle(); 36: style.foreground = Color.black; 37: style.font = new Font("Courier", Font.PLAIN, 16); 38: for (int col = 0; col < 8; col++) { 39: columnBox.append(LayoutKit.hfil()); 39: columnBox.append(LayoutKit.align(FigureKit.label(style, String.valueOf(col)), 0F, 0F)); 40: columnBox.append(LayoutKit.hfil()); 41: } 42: 43: Glyph rowBox = LayoutKit.vbox(); 44: Toolkit kit = Toolkit.getDefaultToolkit(); 45: FontMetrics metrics = kit.getFontMetrics(style.font); 46: Sgraphics s = new Sgraphics(); 47: float columnBoxHeight = s.convertVertical(metrics.getHeight()); 48: rowBox.append(LayoutKit.vspace(columnBoxHeight)); 49: for (int row = 0; row < 8; row++) { 50: rowBox.append(LayoutKit.vfil()); 51: rowBox.append(LayoutKit.align(FigureKit.label(style, String.valueOf((char) (row + 65))), 0F, 0F)); 52: rowBox.append(LayoutKit.vfil()); 53: } 54: 55: Glyph v = LayoutKit.vbox(); 56: v.append(LayoutKit.align(columnBox, 0, 0)); 57: v.append(part); 58: 59: Glyph h = LayoutKit.hbox(); 60: h.append(LayoutKit.vfixed(rowBox, columnBoxHeight + 256F)); 61: h.append(LayoutKit.hspace(4F)); 62: h.append(v); 63: 64: Glyph device = LayoutKit.margin(h, 60); 65: 66: SgraphicsAdapter adapter = new SgraphicsAdapter(device); 67: 68: add("Center", adapter); 69: 70: pinNameField_.setEditable(false); 71: add("South", pinNameField_); 72: } 73: }
The applet creates the pins on line 24 as in the previous example, but the pins are ActivePins. (The name used is just a fabricated name for this example. Line 2 creates a text field and line 71 adds it to the bottom of the applet. Line 3 passes this text field to the constructor of ActivePinCallback. Finally, line 24 creates each pin with the callback. Notice that all pins use the same callback object. Because the mouse can not be in two places at one time, one callback object can handle all the mouse enter/exit events.
Now when the mouse enters a pin, ActivePin sends the event to ActivePinCallback, which then writes the name of the pin in the text field.
For comments or questions contact Mike Jones (Mike.Jones@mass.com)