@Override
    public void onEndPage(PdfWriter writer, Document document) {
      try {
        Rectangle page = document.getPageSize();

        getHeaderFooter(document);
        // Add page header
        header.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
        header.writeSelectedRows(
            0, -1, document.leftMargin(), page.getHeight() - 10, writer.getDirectContent());

        // Add page footer
        footer.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
        footer.writeSelectedRows(
            0, -1, document.leftMargin(), document.bottomMargin() - 5, writer.getDirectContent());

        // Add page border

        int bmargin = 8; // border margin
        PdfContentByte cb = writer.getDirectContent();
        cb.rectangle(
            bmargin, bmargin, page.getWidth() - bmargin * 2, page.getHeight() - bmargin * 2);
        cb.setColorStroke(Color.LIGHT_GRAY);
        cb.stroke();

      } catch (JSONException e) {
        Logger.getLogger(ExportProjectReportServlet.class.getName()).log(Level.SEVERE, null, e);
        throw new ExceptionConverter(e);
      }
    }
  private static boolean testRectangle() {
    boolean pass = true;
    int test = 1;
    Rectangle rect;

    System.out.println("Rectangle tests...");

    rect = new Rectangle(4.3, 5.6, new Point(-99, 66), Color.cyan, false);
    pass &= test(approx(rect.getArea(), 4.3 * 5.6, 0.000001), test++);
    pass &= test(rect.getColor().equals(Color.cyan), test++);

    rect.setColor(Color.black);
    pass &= test(rect.getColor().equals(Color.black), test++);
    pass &= test(!rect.getFilled(), test++);

    rect.setFilled(true);
    pass &= test(rect.getFilled(), test++);
    pass &= test(rect.getWidth() == 4.3, test++);
    pass &= test(rect.getHeight() == 5.6, test++);

    rect.setWidth(4.321);
    pass &= test(rect.getWidth() == 4.321, test++);

    rect.setHeight(89.21);
    pass &= test(rect.getHeight() == 89.21, test++);
    pass &= test(approx(rect.getArea(), 4.321 * 89.21, 0.000001), test++);
    pass &= test(rect.getPosition().equals(new Point(-99, 66)), test++);

    rect.move(new Point(-5, -7));
    pass &= test(rect.getPosition().equals(new Point(-104, 59)), test++);

    Rectangle rect2 = new Rectangle(4.321, 89.21, new Point(-104, 59), Color.black, true);
    pass &= test(rect.equals(rect2), test++);

    Rectangle rect3 = new Rectangle(4.3219, 89.21, new Point(-104, 59), Color.black, false);
    pass &= test(!rect2.equals(rect3), test++);

    rect3 = new Rectangle(4.321, 89.219, new Point(-104, 59), Color.black, false);
    pass &= test(!rect2.equals(rect3), test++);

    rect3 = new Rectangle(4.321, 89.21, new Point(-104, 59), Color.red, false);
    pass &= test(!rect2.equals(rect3), test++);

    rect3 = new Rectangle(4.321, 89.21, new Point(104, 59), Color.black, false);
    pass &= test(!rect2.equals(rect3), test++);

    pass &= test(!rect2.equals(null), test++);
    pass &= test(!rect2.equals(new String("Whatever")), test++);

    return pass;
  }
예제 #3
0
  private Group createButtonArea(final Scene scene) {
    // create button area
    final Group buttonGroup = new Group();
    // rounded rect
    Rectangle buttonArea =
        RectangleBuilder.create()
            .arcWidth(15)
            .arcHeight(20)
            .fill(new Color(0, 0, 0, .55))
            .x(0)
            .y(0)
            .width(60)
            .height(30)
            .stroke(Color.rgb(255, 255, 255, .70))
            .build();
    buttonGroup.getChildren().add(buttonArea);

    // move button group when scene is resized
    buttonGroup
        .translateXProperty()
        .bind(scene.widthProperty().subtract(buttonArea.getWidth() + 6));
    buttonGroup
        .translateYProperty()
        .bind(scene.heightProperty().subtract(buttonArea.getHeight() + 6));
    return buttonGroup;
  }
예제 #4
0
 protected Point randomPointIn(Rectangle r) {
   double x = r.getMinX() + randomDouble() * r.getWidth();
   double y = r.getMinY() + randomDouble() * r.getHeight();
   x = normX(x);
   y = normY(y);
   Point p = ctx.makePoint(x, y);
   assertEquals(CONTAINS, r.relate(p));
   return p;
 }
예제 #5
0
  @Override
  public boolean equals(Object obj) {
    if (obj == null || this.getClass() != obj.getClass()) {
      return false;
    }

    Rectangle other = (Rectangle) obj;
    if (getHeight() == other.getHeight() && getWidth() == other.getWidth()) {
      return true;
    }
    return false;
  }
  /** @param args the command line arguments */
  public static void main(String[] args) {
    Rectangle rect = new Rectangle();
    System.out.println(rect.getWidth());

    Rectangle square = new Rectangle(4, 4);
    System.out.println(square.getWidth());
    System.out.println(square.getLength());

    Area a1 = new Area(square);
    a1.calcArea();
    System.out.println(a1.getArea());
    square.scale(-2);
    System.out.println(square.getWidth());
    System.out.println(square.getLength());
    a1.calcArea();
    System.out.println(a1.getArea());

    Rectangle anotherRect = new Rectangle(8, 10);
    square = anotherRect;
    a1.calcArea();
    System.out.println(a1.getArea());
  }
예제 #7
0
 public boolean collidesWith(Rectangle r) {
   if (r.getX() + r.getWidth() < getX()) {
     return false;
   }
   if (r.getY() + r.getHeight() < getY()) {
     return false;
   }
   if (r.getX() > getX() + getWidth()) {
     return false;
   }
   if (r.getY() > getY() + getHeight()) {
     return false;
   }
   return true;
 }
예제 #8
0
파일: Shape.java 프로젝트: shahidminhas/abc
 public int area() {
   int width = fBounds.getWidth();
   int height = fBounds.getHeight();
   return width * height;
 }
예제 #9
0
파일: Shape.java 프로젝트: shahidminhas/abc
 public int filledArea() {
   return area() - fInnerBounds.getWidth() * fInnerBounds.getHeight();
 }
  /** tests the canvas. This one as XmlElementRefs, XmlElements, and an attachment... */
  public void testCanvas() throws Exception {
    Canvas canvas = new Canvas();
    Bus bus = new Bus();
    bus.setId("busId");
    Rectangle busFrame = new Rectangle();
    busFrame.setWidth(100);
    bus.setFrame(busFrame);
    Cat cat = new Cat();
    cat.setId("catId");
    Circle catFace = new Circle();
    catFace.setRadius(30);
    cat.setFace(catFace);
    House house = new House();
    house.setId("houseId");
    Rectangle houseBase = new Rectangle();
    houseBase.setWidth(76);
    house.setBase(houseBase);
    canvas.setFigures(Arrays.asList(bus, cat, house));
    Rectangle rectangle = new Rectangle();
    rectangle.setHeight(50);
    rectangle.setId("rectId");
    Circle circle = new Circle();
    circle.setRadius(10);
    circle.setId("circleId");
    Triangle triangle = new Triangle();
    triangle.setBase(80);
    triangle.setId("triId");
    canvas.setShapes(Arrays.asList(rectangle, circle, triangle));
    byte[] swaRefBytes =
        "This is a bunch of random bytes that are to be used as an SWA ref attachment.".getBytes();
    byte[] explicitBase64Bytes =
        "This is some more random bytes that are to be used as a base 64 encoded attachment."
            .getBytes();
    byte[] attachment1Bytes =
        "This is some more random bytes that are to be used as the first MTOM attachment."
            .getBytes();
    byte[] attachment2Bytes =
        "This is some more random bytes that are to be used as the second MTOM attachment."
            .getBytes();
    byte[] attachment3Bytes =
        "This is some more random bytes that are to be used as the third MTOM attachment."
            .getBytes();
    CanvasAttachment attachment1 = new CanvasAttachment();
    attachment1.setValue(attachment1Bytes);
    CanvasAttachment attachment2 = new CanvasAttachment();
    attachment2.setValue(attachment2Bytes);
    CanvasAttachment attachment3 = new CanvasAttachment();
    attachment3.setValue(attachment3Bytes);
    ByteArrayDataSource dataSource =
        new ByteArrayDataSource(swaRefBytes, "application/octet-stream");
    dataSource.setName("somename");
    // todo: uncomment when JAXB bug is fixed
    //    canvas.setBackgroundImage(new DataHandler(dataSource));
    canvas.setExplicitBase64Attachment(explicitBase64Bytes);
    canvas.setOtherAttachments(Arrays.asList(attachment1, attachment2, attachment3));

    JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
    ObjectMapper canvasMapper =
        provider.locateMapper(Canvas.class, MediaType.APPLICATION_JSON_TYPE);
    ObjectMapper clientMapper = new ObjectMapper();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    canvasMapper.writeValue(out, canvas);
    // set up the attachments that were written

    shapes.json.draw.Canvas clientCanvas =
        clientMapper.readValue(
            new ByteArrayInputStream(out.toByteArray()), shapes.json.draw.Canvas.class);
    Collection<ObjectNode> clientShapes = clientCanvas.getShapes();
    assertEquals(3, clientShapes.size());
    for (ObjectNode shape : clientShapes) {
      assertEquals(1, shape.size());
      String shapeKey = shape.getFields().next().getKey();
      shape = (ObjectNode) shape.get(shapeKey);
      if ("circle".equals(shapeKey)) {
        assertEquals("circleId", shape.get("id").getTextValue());
        assertEquals(10, shape.get("radius").getIntValue());
      } else if ("triangle".equals(shapeKey)) {
        assertEquals("triId", shape.get("id").getTextValue());
        assertEquals(80, shape.get("base").getIntValue());
      } else if ("rectangle".equals(shapeKey)) {
        assertEquals("rectId", shape.get("id").getTextValue());
        assertEquals(50, shape.get("height").getIntValue());
      } else {
        fail("Unknown shape: " + shapeKey);
      }
    }

    Collection<ObjectNode> clientFigures = clientCanvas.getFigures();
    assertEquals(3, clientFigures.size());
    for (ObjectNode figure : clientFigures) {
      assertEquals(1, figure.size());
      String figureKey = figure.getFields().next().getKey();
      figure = (ObjectNode) figure.get(figureKey);
      if ("bus".equals(figureKey)) {
        assertEquals("busId", figure.get("id").getTextValue());
        assertTrue(figure.get("frame") != null);
        assertEquals(100, figure.get("frame").get("width").getIntValue());
      } else if ("cat".equals(figureKey)) {
        assertEquals("catId", figure.get("id").getTextValue());
        assertTrue(figure.get("circle") != null);
        assertEquals(30, figure.get("circle").get("radius").getIntValue());
      } else if ("house".equals(figureKey)) {
        assertEquals("houseId", figure.get("id").getTextValue());
        assertTrue(figure.get("base") != null);
        assertEquals(76, figure.get("base").get("width").getIntValue());
      } else {
        fail("Unknown figure: " + figure);
      }
    }

    //    DataHandler backgroundImage = clientCanvas.getBackgroundImage();
    //    InputStream attachmentStream = backgroundImage.getInputStream();
    //    ByteArrayOutputStream bgImageIn = new ByteArrayOutputStream();
    //    int byteIn = attachmentStream.read();
    //    while (byteIn > 0) {
    //      bgImageIn.write(byteIn);
    //      byteIn = attachmentStream.read();
    //    }
    //    assertTrue(Arrays.equals(swaRefBytes, bgImageIn.toByteArray()));

    byte[] base64Attachment = clientCanvas.getExplicitBase64Attachment();
    assertNotNull(base64Attachment);
    assertTrue(Arrays.equals(explicitBase64Bytes, base64Attachment));

    Collection otherAttachments = clientCanvas.getOtherAttachments();
    assertEquals(3, otherAttachments.size());
    Iterator attachmentsIt = otherAttachments.iterator();
    int attachmentCount = 0;
    while (attachmentsIt.hasNext()) {
      shapes.json.draw.CanvasAttachment otherAttachment =
          (shapes.json.draw.CanvasAttachment) attachmentsIt.next();
      byte[] otherAttachmentBytes = otherAttachment.getValue();
      if (Arrays.equals(attachment1Bytes, otherAttachmentBytes)) {
        attachmentCount++;
      } else if (Arrays.equals(attachment2Bytes, otherAttachmentBytes)) {
        attachmentCount++;
      } else if (Arrays.equals(attachment3Bytes, otherAttachmentBytes)) {
        attachmentCount++;
      } else {
        fail("Unknown attachment.");
      }
    }
    assertEquals(3, attachmentCount);

    clientCanvas.setShapes(null); // @XmlElementRefs can't be (de)serialized.
    out = new ByteArrayOutputStream();
    clientMapper.writeValue(out, clientCanvas);
    canvas = canvasMapper.readValue(new ByteArrayInputStream(out.toByteArray()), Canvas.class);

    Collection shapes = canvas.getShapes();
    assertNull(shapes);
    //    assertEquals(3, shapes.size());
    //    for (Object Shape : shapes) {
    //      if (Shape instanceof Circle) {
    //        assertEquals("circleId", ((Circle) Shape).getId());
    //        assertEquals(10, ((Circle) Shape).getRadius());
    //      }
    //      else if (Shape instanceof Rectangle) {
    //        assertEquals("rectId", ((Rectangle) Shape).getId());
    //        assertEquals(50, ((Rectangle) Shape).getHeight());
    //      }
    //      else if (Shape instanceof Triangle) {
    //        assertEquals("triId", ((Triangle) Shape).getId());
    //        assertEquals(80, ((Triangle) Shape).getBase());
    //      }
    //      else {
    //        fail("Unknown shape: " + Shape);
    //      }
    //    }

    Collection figures = canvas.getFigures();
    assertEquals(3, figures.size());
    for (Object Figure : figures) {
      if (Figure instanceof Bus) {
        bus = (Bus) Figure;
        assertEquals("busId", bus.getId());
        Rectangle BusFrame = bus.getFrame();
        assertNotNull(BusFrame);
        assertEquals(100, busFrame.getWidth());
      } else if (Figure instanceof Cat) {
        cat = (Cat) Figure;
        assertEquals("catId", cat.getId());
        Circle CatFace = cat.getFace();
        assertNotNull(CatFace);
        assertEquals(30, CatFace.getRadius());
      } else if (Figure instanceof House) {
        house = (House) Figure;
        assertEquals("houseId", house.getId());
        Rectangle HouseBase = house.getBase();
        assertNotNull(HouseBase);
        assertEquals(76, HouseBase.getWidth());
      } else {
        fail("Unknown figure: " + Figure);
      }
    }

    //    backgroundImage = canvas.getBackgroundImage();
    //    attachmentStream = backgroundImage.getInputStream();
    //    bgImageIn = new ByteArrayOutputStream();
    //    byteIn = attachmentStream.read();
    //    while (byteIn > 0) {
    //      bgImageIn.write(byteIn);
    //      byteIn = attachmentStream.read();
    //    }
    //
    //    assertTrue(Arrays.equals(swaRefBytes, bgImageIn.toByteArray()));

    base64Attachment = canvas.getExplicitBase64Attachment();
    assertNotNull(base64Attachment);
    assertTrue(Arrays.equals(explicitBase64Bytes, base64Attachment));

    otherAttachments = canvas.getOtherAttachments();
    assertEquals(3, otherAttachments.size());
    attachmentsIt = otherAttachments.iterator();
    attachmentCount = 0;
    while (attachmentsIt.hasNext()) {
      CanvasAttachment otherAttachment = (CanvasAttachment) attachmentsIt.next();
      byte[] otherAttachmentBytes = otherAttachment.getValue();
      if (Arrays.equals(attachment1Bytes, otherAttachmentBytes)) {
        attachmentCount++;
      } else if (Arrays.equals(attachment2Bytes, otherAttachmentBytes)) {
        attachmentCount++;
      } else if (Arrays.equals(attachment3Bytes, otherAttachmentBytes)) {
        attachmentCount++;
      } else {
        fail("Unknown attachment.");
      }
    }
    assertEquals(3, attachmentCount);

    // todo: test element ref to an attachment element
    // todo: test element refs of attachment elements.
  }
  /** tests the basic shapes. */
  public void testBasicShapes() throws Exception {
    Circle circle = new Circle();
    circle.setColor(Color.BLUE);
    circle.setId("someid");
    circle.setLineStyle(LineStyle.solid);
    circle.setPositionX(8);
    circle.setPositionY(9);
    circle.setRadius(10);

    JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
    ObjectMapper circleMapper =
        provider.locateMapper(Circle.class, MediaType.APPLICATION_JSON_TYPE);
    ObjectMapper clientMapper = new ObjectMapper();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    circleMapper.writeValue(out, circle);
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());

    shapes.json.Circle clientCircle = clientMapper.readValue(in, shapes.json.Circle.class);
    assertSame(shapes.json.Color.BLUE, clientCircle.getColor());
    assertEquals("someid", clientCircle.getId());
    assertEquals(shapes.json.LineStyle.solid, clientCircle.getLineStyle());
    assertEquals(8, clientCircle.getPositionX());
    assertEquals(9, clientCircle.getPositionY());
    assertEquals(10, clientCircle.getRadius());

    out = new ByteArrayOutputStream();
    clientMapper.writeValue(out, clientCircle);
    in = new ByteArrayInputStream(out.toByteArray());

    circle = circleMapper.readValue(in, Circle.class);
    assertSame(Color.BLUE, circle.getColor());
    assertEquals("someid", circle.getId());
    assertEquals(LineStyle.solid, circle.getLineStyle());
    assertEquals(8, circle.getPositionX());
    assertEquals(9, circle.getPositionY());
    assertEquals(10, circle.getRadius());

    ObjectMapper rectangleMapper =
        provider.locateMapper(Rectangle.class, MediaType.APPLICATION_JSON_TYPE);

    Rectangle rectangle = new Rectangle();
    rectangle.setColor(Color.GREEN);
    rectangle.setId("rectid");
    rectangle.setHeight(500);
    rectangle.setWidth(1000);
    rectangle.setLineStyle(LineStyle.dotted);
    rectangle.setPositionX(-100);
    rectangle.setPositionY(-300);

    out = new ByteArrayOutputStream();
    rectangleMapper.writeValue(out, rectangle);
    in = new ByteArrayInputStream(out.toByteArray());

    shapes.json.Rectangle clientRect = clientMapper.readValue(in, shapes.json.Rectangle.class);
    assertSame(shapes.json.Color.GREEN, clientRect.getColor());
    assertEquals("rectid", clientRect.getId());
    assertEquals(shapes.json.LineStyle.dotted, clientRect.getLineStyle());
    assertEquals(500, clientRect.getHeight());
    assertEquals(1000, clientRect.getWidth());
    assertEquals(-100, clientRect.getPositionX());
    assertEquals(-300, clientRect.getPositionY());

    out = new ByteArrayOutputStream();
    clientMapper.writeValue(out, clientRect);
    in = new ByteArrayInputStream(out.toByteArray());

    rectangle = rectangleMapper.readValue(in, Rectangle.class);
    assertSame(Color.GREEN, rectangle.getColor());
    assertEquals("rectid", rectangle.getId());
    assertEquals(LineStyle.dotted, rectangle.getLineStyle());
    assertEquals(500, rectangle.getHeight());
    assertEquals(1000, rectangle.getWidth());
    assertEquals(-100, rectangle.getPositionX());
    assertEquals(-300, rectangle.getPositionY());

    ObjectMapper triangleMapper =
        provider.locateMapper(Triangle.class, MediaType.APPLICATION_JSON_TYPE);

    Triangle triangle = new Triangle();
    triangle.setBase(90);
    triangle.setColor(Color.RED);
    triangle.setHeight(100);
    triangle.setId("triangleId");
    triangle.setLineStyle(LineStyle.dashed);
    triangle.setPositionX(0);
    triangle.setPositionY(-10);

    out = new ByteArrayOutputStream();
    triangleMapper.writeValue(out, triangle);
    in = new ByteArrayInputStream(out.toByteArray());

    shapes.json.Triangle clientTri = clientMapper.readValue(in, shapes.json.Triangle.class);
    assertSame(shapes.json.Color.RED, clientTri.getColor());
    assertEquals("triangleId", clientTri.getId());
    assertEquals(shapes.json.LineStyle.dashed, clientTri.getLineStyle());
    assertEquals(90, clientTri.getBase());
    assertEquals(100, clientTri.getHeight());
    assertEquals(0, clientTri.getPositionX());
    assertEquals(-10, clientTri.getPositionY());

    out = new ByteArrayOutputStream();
    clientMapper.writeValue(out, clientTri);
    in = new ByteArrayInputStream(out.toByteArray());

    triangle = triangleMapper.readValue(in, Triangle.class);
    assertSame(Color.RED, triangle.getColor());
    assertEquals(90, triangle.getBase());
    assertEquals(100, triangle.getHeight());
    assertEquals("triangleId", triangle.getId());
    assertSame(LineStyle.dashed, triangle.getLineStyle());
    assertEquals(0, triangle.getPositionX());
    assertEquals(-10, triangle.getPositionY());
  }
  /** tests house. This one has things like nillable and required properties. */
  public void testHouse() throws Exception {
    House house = new House();
    Rectangle base = new Rectangle();
    base.setColor(Color.BLUE);
    base.setHeight(80);
    base.setWidth(80);
    base.setLineStyle(LineStyle.solid);
    base.setId("baseid");
    house.setBase(base);
    Rectangle door = new Rectangle();
    door.setColor(Color.YELLOW);
    door.setHeight(35);
    door.setWidth(20);
    door.setLineStyle(LineStyle.solid);
    door.setId("doorId");
    house.setDoor(door);
    Circle knob = new Circle();
    knob.setColor(Color.RED);
    knob.setId("knobId");
    knob.setLineStyle(LineStyle.dashed);
    knob.setRadius(2);
    house.setDoorKnob(knob);
    Label label1 = new Label();
    label1.setValue("bachelor-pad");
    Label label2 = new Label();
    label2.setValue("single-family-dwelling");
    house.setLabels(Arrays.asList(label1, label2));
    Triangle roof = new Triangle();
    roof.setBase(84);
    roof.setHeight(20);
    roof.setColor(Color.YELLOW);
    roof.setLineStyle(LineStyle.solid);
    house.setRoof(roof);
    Rectangle window = new Rectangle();
    window.setColor(Color.YELLOW);
    window.setHeight(10);
    window.setWidth(10);
    window.setLineStyle(LineStyle.solid);
    house.setWindows(Arrays.asList(window));
    house.setConstructedDate(new DateTime(3L));
    house.setType(XmlQNameEnumUtil.toQName(HouseType.brick));
    house.setStyle(XmlQNameEnumUtil.toQName(HouseStyle.latin));
    house.setColor(XmlQNameEnumUtil.toURI(HouseColor.blue));

    JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
    ObjectMapper houseMapper = provider.locateMapper(House.class, MediaType.APPLICATION_JSON_TYPE);
    ObjectMapper clientMapper = new ObjectMapper();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    houseMapper.writeValue(out, house);
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    shapes.json.structures.House clientHouse =
        clientMapper.readValue(in, shapes.json.structures.House.class);

    shapes.json.Rectangle clientBase = clientHouse.getBase();
    assertSame(shapes.json.Color.BLUE, clientBase.getColor());
    assertSame(shapes.json.LineStyle.solid, clientBase.getLineStyle());
    assertEquals(80, clientBase.getHeight());
    assertEquals(80, clientBase.getWidth());
    assertEquals("baseid", clientBase.getId());
    shapes.json.Rectangle clientDoor = clientHouse.getDoor();
    assertSame(shapes.json.Color.YELLOW, clientDoor.getColor());
    assertSame(shapes.json.LineStyle.solid, clientDoor.getLineStyle());
    assertEquals(35, clientDoor.getHeight());
    assertEquals(20, clientDoor.getWidth());
    assertEquals("doorId", clientDoor.getId());
    shapes.json.Circle clientKnob = clientHouse.getDoorKnob();
    assertSame(shapes.json.Color.RED, clientKnob.getColor());
    assertSame(shapes.json.LineStyle.dashed, clientKnob.getLineStyle());
    assertEquals(2, clientKnob.getRadius());
    assertEquals("knobId", clientKnob.getId());
    List<String> labels = Arrays.asList("bachelor-pad", "single-family-dwelling");
    clientHouse.getLabels().size();
    for (Object l : clientHouse.getLabels()) {
      shapes.json.Label label = (shapes.json.Label) l;
      assertTrue(labels.contains(label.getValue()));
    }
    shapes.json.Triangle clientRoof = clientHouse.getRoof();
    assertSame(shapes.json.Color.YELLOW, clientRoof.getColor());
    assertSame(shapes.json.LineStyle.solid, clientRoof.getLineStyle());
    assertNull(clientRoof.getId());
    assertEquals(84, clientRoof.getBase());
    assertEquals(20, clientRoof.getHeight());
    assertEquals(1, clientHouse.getWindows().size());
    shapes.json.Rectangle clientWindow = (shapes.json.Rectangle) clientHouse.getWindows().get(0);
    assertSame(shapes.json.Color.YELLOW, clientWindow.getColor());
    assertSame(shapes.json.LineStyle.solid, clientWindow.getLineStyle());
    assertEquals(10, clientWindow.getHeight());
    assertEquals(10, clientWindow.getWidth());
    assertNull(clientWindow.getId());
    assertEquals(new Date(3L), clientHouse.getConstructedDate());

    out = new ByteArrayOutputStream();
    clientMapper.writeValue(out, clientHouse);
    house = houseMapper.readValue(new ByteArrayInputStream(out.toByteArray()), House.class);
    base = house.getBase();
    assertSame(Color.BLUE, base.getColor());
    assertSame(LineStyle.solid, base.getLineStyle());
    assertEquals(80, base.getHeight());
    assertEquals(80, base.getWidth());
    assertEquals("baseid", base.getId());
    door = house.getDoor();
    assertSame(Color.YELLOW, door.getColor());
    assertSame(LineStyle.solid, door.getLineStyle());
    assertEquals(35, door.getHeight());
    assertEquals(20, door.getWidth());
    assertEquals("doorId", door.getId());
    knob = house.getDoorKnob();
    assertSame(Color.RED, knob.getColor());
    assertSame(LineStyle.dashed, knob.getLineStyle());
    assertEquals(2, knob.getRadius());
    assertEquals("knobId", knob.getId());
    labels = Arrays.asList("bachelor-pad", "single-family-dwelling");
    house.getLabels().size();
    for (Object l : house.getLabels()) {
      Label label = (Label) l;
      assertTrue(labels.contains(label.getValue()));
    }
    roof = house.getRoof();
    assertSame(Color.YELLOW, roof.getColor());
    assertSame(LineStyle.solid, roof.getLineStyle());
    assertNull(roof.getId());
    assertEquals(84, roof.getBase());
    assertEquals(20, roof.getHeight());
    assertEquals(1, house.getWindows().size());
    window = house.getWindows().get(0);
    assertSame(Color.YELLOW, window.getColor());
    assertSame(LineStyle.solid, window.getLineStyle());
    assertEquals(10, window.getHeight());
    assertEquals(10, window.getWidth());
    assertNull(window.getId());
    assertEquals(new DateTime(3L), house.getConstructedDate());
    assertEquals(XmlQNameEnumUtil.toQName(HouseType.brick), house.getType());
    assertEquals(XmlQNameEnumUtil.toQName(HouseStyle.latin), house.getStyle());
    assertEquals(XmlQNameEnumUtil.toURI(HouseColor.blue), house.getColor());

    // now let's check the nillable and required stuff:

    // roof is required, but nillable.
    clientHouse.setRoof(null);
    out = new ByteArrayOutputStream();
    clientMapper.writeValue(out, clientHouse);
    house = houseMapper.readValue(new ByteArrayInputStream(out.toByteArray()), House.class);
    base = house.getBase();
    assertSame(Color.BLUE, base.getColor());
    assertSame(LineStyle.solid, base.getLineStyle());
    assertEquals(80, base.getHeight());
    assertEquals(80, base.getWidth());
    assertEquals("baseid", base.getId());
    door = house.getDoor();
    assertSame(Color.YELLOW, door.getColor());
    assertSame(LineStyle.solid, door.getLineStyle());
    assertEquals(35, door.getHeight());
    assertEquals(20, door.getWidth());
    assertEquals("doorId", door.getId());
    knob = house.getDoorKnob();
    assertSame(Color.RED, knob.getColor());
    assertSame(LineStyle.dashed, knob.getLineStyle());
    assertEquals(2, knob.getRadius());
    assertEquals("knobId", knob.getId());
    labels = Arrays.asList("bachelor-pad", "single-family-dwelling");
    house.getLabels().size();
    for (Object l : house.getLabels()) {
      Label label = (Label) l;
      assertTrue(labels.contains(label.getValue()));
    }
    roof = house.getRoof();
    assertNull(roof);
    assertEquals(1, house.getWindows().size());
    window = house.getWindows().get(0);
    assertSame(Color.YELLOW, window.getColor());
    assertSame(LineStyle.solid, window.getLineStyle());
    assertEquals(10, window.getHeight());
    assertEquals(10, window.getWidth());
    assertNull(window.getId());

    // windows are nillable...
    clientHouse.setWindows(null);
    out = new ByteArrayOutputStream();
    clientMapper.writeValue(out, clientHouse);
    house = houseMapper.readValue(new ByteArrayInputStream(out.toByteArray()), House.class);
    base = house.getBase();
    assertSame(Color.BLUE, base.getColor());
    assertSame(LineStyle.solid, base.getLineStyle());
    assertEquals(80, base.getHeight());
    assertEquals(80, base.getWidth());
    assertEquals("baseid", base.getId());
    door = house.getDoor();
    assertSame(Color.YELLOW, door.getColor());
    assertSame(LineStyle.solid, door.getLineStyle());
    assertEquals(35, door.getHeight());
    assertEquals(20, door.getWidth());
    assertEquals("doorId", door.getId());
    knob = house.getDoorKnob();
    assertSame(Color.RED, knob.getColor());
    assertSame(LineStyle.dashed, knob.getLineStyle());
    assertEquals(2, knob.getRadius());
    assertEquals("knobId", knob.getId());
    labels = Arrays.asList("bachelor-pad", "single-family-dwelling");
    house.getLabels().size();
    for (Object l : house.getLabels()) {
      Label label = (Label) l;
      assertTrue(labels.contains(label.getValue()));
    }
    roof = house.getRoof();
    assertNull(roof);
    assertNull(house.getWindows());
  }
  /** tests bus. This one has an element wrapper. */
  public void testBus() throws Exception {
    Bus bus = new Bus();
    bus.setId("some bus");
    Label cityBus = new Label();
    cityBus.setValue("city");
    Label countryBus = new Label();
    countryBus.setValue("country");
    Label longDistanceBus = new Label();
    longDistanceBus.setValue("long-distance");

    bus.setId("bus id");
    bus.setLabels(Arrays.asList(cityBus, countryBus, longDistanceBus));
    Rectangle door = new Rectangle();
    door.setColor(Color.BLUE);
    door.setWidth(2);
    door.setHeight(4);
    door.setLineStyle(LineStyle.solid);
    bus.setDoor(door);
    Rectangle frame = new Rectangle();
    frame.setHeight(10);
    frame.setWidth(50);
    frame.setColor(Color.YELLOW);
    frame.setLineStyle(LineStyle.solid);
    bus.setFrame(frame);
    Circle front = new Circle();
    front.setColor(Color.BLUE);
    front.setLineStyle(LineStyle.dotted);
    front.setRadius(6);
    Circle back = new Circle();
    back.setColor(Color.BLUE);
    back.setLineStyle(LineStyle.dotted);
    back.setRadius(7);
    bus.setWheels(new Circle[] {front, back});
    Rectangle window1 = new Rectangle();
    window1.setColor(Color.BLUE);
    window1.setWidth(2);
    window1.setHeight(2);
    window1.setLineStyle(LineStyle.solid);
    Rectangle window2 = new Rectangle();
    window2.setColor(Color.BLUE);
    window2.setWidth(2);
    window2.setHeight(2);
    window2.setLineStyle(LineStyle.solid);
    Rectangle window3 = new Rectangle();
    window3.setColor(Color.BLUE);
    window3.setWidth(2);
    window3.setHeight(2);
    window3.setLineStyle(LineStyle.solid);
    bus.setWindows(Arrays.asList(window1, window2, window3));
    Map<Integer, Circle> riders = new HashMap<Integer, Circle>();
    Circle rider3 = new Circle();
    rider3.setRadius(3);
    riders.put(3, rider3);
    Circle rider4 = new Circle();
    rider4.setRadius(4);
    riders.put(4, rider4);
    bus.setType(XmlQNameEnumUtil.toQName(BusType.charter));

    JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
    ObjectMapper busMapper = provider.locateMapper(Bus.class, MediaType.APPLICATION_JSON_TYPE);
    ObjectMapper clientMapper = new ObjectMapper();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    busMapper.writeValue(out, bus);
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    shapes.json.vehicles.Bus clientBus = clientMapper.readValue(in, shapes.json.vehicles.Bus.class);
    assertEquals("bus id", clientBus.getId());
    ArrayList<String> labels =
        new ArrayList<String>(Arrays.asList("city", "country", "long-distance"));
    for (Object l : clientBus.getLabels()) {
      shapes.json.Label label = (shapes.json.Label) l;
      assertTrue(labels.remove(label.getValue()));
    }
    shapes.json.Rectangle clientDoor = clientBus.getDoor();
    assertSame(shapes.json.Color.BLUE, clientDoor.getColor());
    assertEquals(2, clientDoor.getWidth());
    assertEquals(4, clientDoor.getHeight());
    assertSame(shapes.json.LineStyle.solid, clientDoor.getLineStyle());
    shapes.json.Rectangle clientFrame = clientBus.getFrame();
    assertEquals(10, clientFrame.getHeight());
    assertEquals(50, clientFrame.getWidth());
    assertSame(shapes.json.Color.YELLOW, clientFrame.getColor());
    assertSame(shapes.json.LineStyle.solid, clientFrame.getLineStyle());
    shapes.json.Circle[] clientWheels = clientBus.getWheels();
    assertEquals(2, clientWheels.length);
    assertEquals(6, clientWheels[0].getRadius());
    assertSame(shapes.json.Color.BLUE, clientWheels[0].getColor());
    assertSame(shapes.json.LineStyle.dotted, clientWheels[0].getLineStyle());
    assertEquals(7, clientWheels[1].getRadius());
    assertSame(shapes.json.Color.BLUE, clientWheels[1].getColor());
    assertSame(shapes.json.LineStyle.dotted, clientWheels[1].getLineStyle());
    shapes.json.Rectangle[] clientWindows =
        (shapes.json.Rectangle[]) clientBus.getWindows().toArray(new shapes.json.Rectangle[3]);
    assertEquals(2, clientWindows[0].getWidth());
    assertEquals(2, clientWindows[0].getHeight());
    assertEquals(shapes.json.Color.BLUE, clientWindows[0].getColor());
    assertEquals(shapes.json.LineStyle.solid, clientWindows[0].getLineStyle());
    assertEquals(2, clientWindows[1].getWidth());
    assertEquals(2, clientWindows[1].getHeight());
    assertEquals(shapes.json.Color.BLUE, clientWindows[1].getColor());
    assertEquals(shapes.json.LineStyle.solid, clientWindows[1].getLineStyle());
    assertEquals(2, clientWindows[2].getWidth());
    assertEquals(2, clientWindows[2].getHeight());
    assertEquals(shapes.json.Color.BLUE, clientWindows[2].getColor());
    assertEquals(shapes.json.LineStyle.solid, clientWindows[2].getLineStyle());

    out = new ByteArrayOutputStream();
    clientMapper.writeValue(out, clientBus);
    bus = busMapper.readValue(new ByteArrayInputStream(out.toByteArray()), Bus.class);
    door = bus.getDoor();
    assertSame(Color.BLUE, door.getColor());
    assertEquals(2, door.getWidth());
    assertEquals(4, door.getHeight());
    assertSame(LineStyle.solid, door.getLineStyle());
    frame = bus.getFrame();
    assertEquals(10, frame.getHeight());
    assertEquals(50, frame.getWidth());
    assertSame(Color.YELLOW, frame.getColor());
    assertSame(LineStyle.solid, frame.getLineStyle());
    Circle[] wheels = bus.getWheels();
    assertEquals(2, wheels.length);
    assertEquals(6, wheels[0].getRadius());
    assertSame(Color.BLUE, wheels[0].getColor());
    assertSame(LineStyle.dotted, wheels[0].getLineStyle());
    assertEquals(7, wheels[1].getRadius());
    assertSame(Color.BLUE, wheels[1].getColor());
    assertSame(LineStyle.dotted, wheels[1].getLineStyle());
    Rectangle[] windows = bus.getWindows().toArray(new Rectangle[3]);
    assertEquals(2, windows[0].getWidth());
    assertEquals(2, windows[0].getHeight());
    assertEquals(Color.BLUE, windows[0].getColor());
    assertEquals(LineStyle.solid, windows[0].getLineStyle());
    assertEquals(2, windows[1].getWidth());
    assertEquals(2, windows[1].getHeight());
    assertEquals(Color.BLUE, windows[1].getColor());
    assertEquals(LineStyle.solid, windows[1].getLineStyle());
    assertEquals(2, windows[2].getWidth());
    assertEquals(2, windows[2].getHeight());
    assertEquals(Color.BLUE, windows[2].getColor());
    assertEquals(LineStyle.solid, windows[2].getLineStyle());
    assertEquals(BusType.charter, XmlQNameEnumUtil.fromQName(bus.getType(), BusType.class));

    // todo: test an element wrapper around elementRefs
  }
예제 #14
0
  public int geometryTest(Point2D worldCoord, int tolerance) {
    shapeList = model.getShapes();
    if (selectedShape != null) {
      if (doHandleCheck(worldCoord, selectedShape)) {
        return selectedIndex;
      }
    }

    Point2D.Double objCoord = new Point2D.Double();
    List<Shape> reversed = model.getShapesReversed();

    for (int i = 0; i < reversed.size(); i++) {
      Shape s = reversed.get(i);
      AffineTransform worldToObj =
          new AffineTransform(
              Math.cos(s.getRotation()),
              -Math.sin(s.getRotation()),
              Math.sin(s.getRotation()),
              Math.cos(s.getRotation()),
              0,
              0);
      worldToObj.concatenate(
          new AffineTransform(1, 0, 0, 1, -s.getCenter().getX(), -s.getCenter().getY()));
      worldToObj.transform(worldCoord, objCoord);
      if (s instanceof Line) {
        Line l = (Line) s;
        Point2D.Double d = new Point2D.Double();
        double x1 = l.getEnd().getX() - l.getCenter().getX();
        double y1 = l.getEnd().getY() - l.getCenter().getY();
        double lineLength = Math.sqrt((x1) * (x1) + (y1) * (y1));
        d.setLocation((x1) / lineLength, (y1) / lineLength);
        double t = (objCoord.getX()) * d.getX() + (objCoord.getY()) * d.getY();
        Point2D.Double q = new Point2D.Double();
        q.setLocation(t * d.getX(), t * d.getY());
        double qdist =
            Math.sqrt(
                (objCoord.getX() - q.getX()) * (objCoord.getX() - q.getX())
                    + (objCoord.getY() - q.getY()) * (objCoord.getY() - q.getY()));
        tolerance = (int) (tolerance * this.getScale());
        if (qdist <= tolerance && t >= -tolerance && t <= lineLength + tolerance) {
          selectedShape = l;
          selectedIndex = shapeList.size() - i - 1;
          setChanged();
          notifyObservers();
          return selectedIndex;
        }
      } else if (s instanceof Square) {
        Square sq = (Square) s;
        if (Math.abs(objCoord.getX()) < sq.getSize() / 2
            && Math.abs(objCoord.getY()) < sq.getSize() / 2) {
          selectedShape = sq;
          selectedIndex = shapeList.size() - i - 1;
          setChanged();
          notifyObservers();
          return selectedIndex;
        }
      } else if (s instanceof Rectangle) {
        Rectangle r = (Rectangle) s;
        if (Math.abs(objCoord.getX()) < r.getWidth() / 2
            && Math.abs(objCoord.getY()) < r.getHeight() / 2) {
          selectedShape = r;
          selectedIndex = shapeList.size() - i - 1;
          setChanged();
          notifyObservers();
          return selectedIndex;
        }
      } else if (s instanceof Circle) {
        Circle c = (Circle) s;
        if (objCoord.getX() * objCoord.getX() + objCoord.getY() * objCoord.getY()
            < (c.getRadius() * c.getRadius())) {
          selectedShape = c;
          selectedIndex = shapeList.size() - i - 1;
          setChanged();
          notifyObservers();
          return selectedIndex;
        }
      } else if (s instanceof Ellipse) {
        Ellipse el = (Ellipse) s;
        double a = el.getWidth() / 2;
        double b = el.getHeight() / 2;
        if ((objCoord.getX() * objCoord.getX()) / (a * a)
                + (objCoord.getY() * objCoord.getY()) / (b * b)
            <= 1) {
          selectedShape = el;
          selectedIndex = shapeList.size() - i - 1;
          setChanged();
          notifyObservers();
          return selectedIndex;
        }
      } else if (s instanceof Triangle) {
        Triangle t = (Triangle) s;
        Point2D.Double a =
            new Point2D.Double(
                t.getA().getX() - t.getCenter().getX(), t.getA().getY() - t.getCenter().getY());
        Point2D.Double b =
            new Point2D.Double(
                t.getB().getX() - t.getCenter().getX(), t.getB().getY() - t.getCenter().getY());
        Point2D.Double c =
            new Point2D.Double(
                t.getC().getX() - t.getCenter().getX(), t.getC().getY() - t.getCenter().getY());

        double triArea = calcArea(a, b, c);
        double a1 = calcArea(objCoord, b, c);
        double a2 = calcArea(objCoord, a, c);
        double a3 = calcArea(objCoord, a, b);

        if (a1 + a2 + a3 <= triArea) {
          selectedShape = t;
          selectedIndex = shapeList.size() - i - 1;
          setChanged();
          notifyObservers();
          return selectedIndex;
        }
      } else {
        selectedShape = null;
        selectedIndex = -1;
        setChanged();
        notifyObservers();
      }
    }
    selectedShape = null;
    selectedIndex = -1;
    setChanged();
    notifyObservers();
    return -1;
  }