|
| 1 | +package com.bbn.openmap.layer.test; |
| 2 | + |
| 3 | +import com.bbn.openmap.layer.OMGraphicHandlerLayer; |
| 4 | +import com.bbn.openmap.omGraphics.OMAction; |
| 5 | +import com.bbn.openmap.omGraphics.OMColor; |
| 6 | +import com.bbn.openmap.omGraphics.OMGraphic; |
| 7 | +import com.bbn.openmap.omGraphics.OMGraphicList; |
| 8 | +import com.bbn.openmap.omGraphics.OMRect; |
| 9 | +import com.bbn.openmap.tools.drawing.DrawingToolRequestor; |
| 10 | +import com.bbn.openmap.tools.drawing.OMDrawingTool; |
| 11 | +import com.bbn.openmap.util.DataBounds; |
| 12 | + |
| 13 | +/** |
| 14 | + * A little layer to test out the DataBounds intersections. You draw OMRects |
| 15 | + * using the drawing tool, and any rectangles that overlap fill up with red. |
| 16 | + * |
| 17 | + * @author dietrick |
| 18 | + */ |
| 19 | +public class BoundsTestLayer extends OMGraphicHandlerLayer implements DrawingToolRequestor { |
| 20 | + |
| 21 | + OMGraphicList holder = new OMGraphicList(); |
| 22 | + OMDrawingTool drawingTool = null; |
| 23 | + String BOUNDS = "bounds"; |
| 24 | + String HIT = "hit"; |
| 25 | + |
| 26 | + public BoundsTestLayer() { |
| 27 | + setMouseModeIDsForEvents(new String[] { "Gestures" }); |
| 28 | + } |
| 29 | + |
| 30 | + public OMGraphicList prepare() { |
| 31 | + |
| 32 | + OMGraphicList list = new OMGraphicList(); |
| 33 | + list.addAll(holder); |
| 34 | + |
| 35 | + for (OMGraphic omg : holder) { |
| 36 | + omg.removeAttribute(HIT); |
| 37 | + omg.setFillPaint(OMColor.clear); |
| 38 | + } |
| 39 | + |
| 40 | + for (OMGraphic omg : holder) { |
| 41 | + if (omg instanceof OMRect) { |
| 42 | + DataBounds bnds = (DataBounds) omg.getAttribute(BOUNDS); |
| 43 | + |
| 44 | + for (OMGraphic omg2 : list) { |
| 45 | + if (omg2 instanceof OMRect && !omg.equals(omg2)) { |
| 46 | + DataBounds bnds2 = (DataBounds) omg2.getAttribute(BOUNDS); |
| 47 | + |
| 48 | + if (bnds.intersects(bnds2)) { |
| 49 | + omg.putAttribute(HIT, true); |
| 50 | + omg2.putAttribute(HIT, true); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + for (OMGraphic omg : holder) { |
| 58 | + if (omg.getAttribute(HIT) != null) { |
| 59 | + omg.setFillPaint(OMColor.red); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + list.generate(getProjection()); |
| 64 | + return list; |
| 65 | + } |
| 66 | + |
| 67 | + public void findAndInit(Object obj) { |
| 68 | + super.findAndInit(obj); |
| 69 | + |
| 70 | + if (obj instanceof OMDrawingTool) { |
| 71 | + drawingTool = (OMDrawingTool) obj; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public boolean isSelectable(OMGraphic omg) { |
| 76 | + return (drawingTool != null && drawingTool.canEdit(omg.getClass())); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Called if isSelectable(OMGraphic) was true, so the list has the |
| 81 | + * OMGraphic. A list is used in case underlying code is written to handle |
| 82 | + * more than one OMGraphic being selected at a time. |
| 83 | + */ |
| 84 | + public void select(OMGraphicList list) { |
| 85 | + if (list != null && !list.isEmpty()) { |
| 86 | + OMGraphic omg = list.getOMGraphicAt(0); |
| 87 | + |
| 88 | + if (drawingTool != null && drawingTool.canEdit(omg.getClass())) { |
| 89 | + drawingTool.setBehaviorMask(OMDrawingTool.QUICK_CHANGE_BEHAVIOR_MASK); |
| 90 | + if (drawingTool.edit(omg, this) == null) { |
| 91 | + // Shouldn't see this because we checked, but ... |
| 92 | + fireRequestInfoLine("Can't figure out how to modify this object."); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /* |
| 99 | + * (non-Javadoc) |
| 100 | + * |
| 101 | + * @see |
| 102 | + * com.bbn.openmap.tools.drawing.DrawingToolRequestor#drawingComplete(com |
| 103 | + * .bbn.openmap.omGraphics.OMGraphic, com.bbn.openmap.omGraphics.OMAction) |
| 104 | + */ |
| 105 | + public void drawingComplete(OMGraphic omg, OMAction action) { |
| 106 | + if (omg instanceof OMRect) { |
| 107 | + OMRect rect = (OMRect) omg; |
| 108 | + DataBounds bounds = new DataBounds(rect.getWestLon(), rect.getNorthLat(), rect.getEastLon(), rect.getSouthLat()); |
| 109 | + rect.putAttribute(BOUNDS, bounds); |
| 110 | + } |
| 111 | + holder.doAction(omg, action); |
| 112 | + doPrepare(); |
| 113 | + } |
| 114 | +} |
0 commit comments