/**
   * @todo Refactor this. Shouldn't mix localisation logic and relation mutation logic in one method
   * @todo-javadoc Write javadocs for method parameter
   * @todo-javadoc Write javadocs for method parameter
   * @todo-javadoc Write javadocs for return value
   * @todo-javadoc Write javadocs for method parameter
   * @param evt Describe what the parameter does
   * @return Describe the return value
   */
  public boolean selectMaybe(MouseEvent evt) {
    boolean multiplicity = evt.isControlDown();
    boolean cardinality = evt.isShiftDown();
    Point point = evt.getPoint();
    setSelected(_mainLine.intersects(point.getX() - 5, point.getY() - 5, 10, 10));
    if (isSelected()) {
      double distFromP1 =
          Point2D.distance(_mainLine.getX1(), _mainLine.getY1(), point.getX(), point.getY());

      boolean nearP1 = distFromP1 < _mainLength / 2;
      boolean leftChosen = (_leftIsWest && nearP1) || (!_leftIsWest && !nearP1);
      RelationshipRole role = leftChosen ? _rightRole : _leftRole;
      if (multiplicity) {
        // toggle multiplicity. Let's prevent the user from doing something stupid. Some
        // cardinalities
        // can't be changed. we know best etc.
        if (role.getRelation().isMany2Many()) {
          // don't allow to change cardinality of m:n relationships
          JOptionPane.showMessageDialog(
              null,
              "Can't change the cardinality of a many-to-many relationship",
              "Cardinality",
              JOptionPane.INFORMATION_MESSAGE);
          return false;
        }
        if (role.isFkPk()) {
          // don't allow to change cardinality of 1:1 relationships that are 1:1 because fk is also
          // pk
          JOptionPane.showMessageDialog(
              null,
              "Can't change the cardinality of a one-to-one relationship where the foreign key is also a primary key",
              "Cardinality",
              JOptionPane.INFORMATION_MESSAGE);
          return false;
        }
        if (role.isTargetPrimaryKey()) {
          JOptionPane.showMessageDialog(
              null,
              "Can't change the cardinality of a the one-side of a relationship that corresponds to a primary key",
              "Cardinality",
              JOptionPane.INFORMATION_MESSAGE);
          return false;
        }
        // All checks passed. Do the toggle
        role.setTargetMany(!role.isTargetMany());
      }
      if (cardinality) {
        // toggle between uni/bidirectional
        role.setEnabled(!role.isEnabled());
      }
      return true;
    } else {
      return false;
    }
  }
Example #2
0
  /**
   * ******************************************************************************************
   * Draws the chart
   *
   * @param axisChart
   * @param iAxisChartDataSet
   * @throws PropertyException
   *     *******************************************************************************************
   */
  static void render(AxisChart axisChart, IAxisChartDataSet iAxisChartDataSet)
      throws PropertyException {
    Graphics2D g2d = axisChart.getGraphics2D();

    LineChartProperties lineChartProperties =
        (LineChartProperties) iAxisChartDataSet.getChartTypeProperties();
    lineChartProperties.validate(iAxisChartDataSet);

    // DataAxisProperties dataAxisProperties= (DataAxisProperties)
    // axisChart.getAxisProperties().getYAxisProperties();
    IDataSeries iDataSeries = (IDataSeries) axisChart.getIAxisDataSeries();

    // ---cache the computed values
    float[][] yAxisCoordinates =
        new float[iAxisChartDataSet.getNumberOfDataSets()]
            [iAxisChartDataSet.getNumberOfDataItems()];

    // ---need this for image map calculation
    float xMapCoordinate = axisChart.getXAxis().getTickStart();

    // LOOP
    for (int j = 0; j < iAxisChartDataSet.getNumberOfDataItems(); j++) {
      // LOOP
      for (int i = 0; i < yAxisCoordinates.length; i++) {
        if (iAxisChartDataSet.getValue(i, j) != Double.NaN) {
          yAxisCoordinates[i][j] =
              axisChart
                  .getYAxis()
                  .computeAxisCoordinate(
                      axisChart.getYAxis().getOrigin(),
                      iAxisChartDataSet.getValue(i, j),
                      axisChart.getYAxis().getScaleCalculator().getMinValue());

          // ---if we are generating an ImageMap, store the image coordinates
          if (axisChart.getGenerateImageMapFlag()) {
            String label;
            if (axisChart.getXAxis().getAxisLabelsGroup() != null) {
              label = axisChart.getXAxis().getAxisLabelsGroup().getTextTag(j).getText();
            } else {
              label = null;
            }

            axisChart
                .getImageMap()
                .addImageMapArea(
                    new CircleMapArea(
                        xMapCoordinate,
                        yAxisCoordinates[i][j],
                        iAxisChartDataSet.getValue(i, j),
                        label,
                        iAxisChartDataSet.getLegendLabel(i)));
          }
        } else {
          yAxisCoordinates[i][j] = Float.NaN;
        }
      }

      xMapCoordinate += axisChart.getXAxis().getScalePixelWidth();
    }

    AffineTransform originalTransform = null;
    double[] cornerXOffset = null;
    double[] cornerYOffset = null;

    // ---check if there are any points to display
    if (lineChartProperties.getShapes() != null) {
      // ---when centering the shapes on the points, need x and y offset to do this
      cornerXOffset = new double[iAxisChartDataSet.getNumberOfDataSets()];
      cornerYOffset = new double[iAxisChartDataSet.getNumberOfDataSets()];

      // ---get the original transform so can reset it.
      originalTransform = g2d.getTransform();

      Rectangle2D rectangle;

      // LOOP
      // ---pre-compute the dimensions of each Shape so do not do it in loop.
      for (int i = 0; i < iAxisChartDataSet.getNumberOfDataSets(); i++) {
        if (lineChartProperties.getShapes()[i] != null) {
          rectangle = lineChartProperties.getShapes()[i].getBounds2D();
          cornerXOffset[i] = rectangle.getWidth() / 2;
          cornerYOffset[i] = rectangle.getHeight() / 2;
        }
      }
    }

    // ---init for first segment
    Line2D.Float line =
        new Line2D.Float(
            axisChart.getXAxis().getTickStart(),
            yAxisCoordinates[0][0],
            axisChart.getXAxis().getTickStart(),
            yAxisCoordinates[0][0]);
    // ---make sure not plotting a chart with only one data point.
    if (yAxisCoordinates[0].length > 1) {
      line.y2 = yAxisCoordinates[0][1];
    }

    // LOOP
    // ---draw each line to the image
    for (int i = 0; i < yAxisCoordinates.length; i++) {
      line.x1 = axisChart.getXAxis().getTickStart();
      line.y1 = yAxisCoordinates[i][0];
      line.x2 = line.x1;

      // LOOP
      for (int j = 1; j < yAxisCoordinates[0].length; j++) {
        // ---if current point on line should be drawn
        if (!Float.isNaN(yAxisCoordinates[i][j])) {
          // ---if the previous point was not drawn, no line
          if (Float.isNaN(yAxisCoordinates[i][j - 1])) {
            line.x2 += axisChart.getXAxis().getScalePixelWidth();
            line.x1 = line.x2;
            line.y1 = yAxisCoordinates[i][j];
            line.y2 = yAxisCoordinates[i][j];

            continue;
          }

          line.x2 += axisChart.getXAxis().getScalePixelWidth();
          line.y2 = yAxisCoordinates[i][j];

          g2d.setPaint(iAxisChartDataSet.getPaint(i));
          g2d.setStroke(lineChartProperties.getLineStrokes()[i]);
          g2d.draw(line);

          // ---plot the Point
          if (lineChartProperties.getShapes()[i] != null) {
            // ---translate the Shape into position.
            g2d.translate(line.x1 - cornerXOffset[i], line.y1 - cornerYOffset[i]);

            g2d.setPaint(iAxisChartDataSet.getPaint(i));
            g2d.fill(lineChartProperties.getShapes()[i]);

            // ---translate back to the original position
            g2d.setTransform(originalTransform);
          }

          line.x1 = line.x2;
          line.y1 = line.y2;
        } else {
          if ((!Float.isNaN(yAxisCoordinates[i][j - 1]))
              && (lineChartProperties.getShapes()[i] != null)) {
            // ---translate the Shape into position.
            g2d.translate(line.x1 - cornerXOffset[i], line.y1 - cornerYOffset[i]);

            g2d.setPaint(iAxisChartDataSet.getPaint(i));
            g2d.fill(lineChartProperties.getShapes()[i]);

            // ---translate back to the original position
            g2d.setTransform(originalTransform);
          }

          line.x2 += axisChart.getXAxis().getScalePixelWidth();
          line.x1 = line.x2;
        }
      }

      // ---put the last shape on the line
      if ((!Float.isNaN(yAxisCoordinates[i][yAxisCoordinates[i].length - 1]))
          && (lineChartProperties.getShapes()[i] != null)) {
        // ---translate the Shape into position.
        g2d.translate(line.x2 - cornerXOffset[i], line.y2 - cornerYOffset[i]);

        g2d.setPaint(iAxisChartDataSet.getPaint(i));
        g2d.fill(lineChartProperties.getShapes()[i]);

        // ---translate back to the original position
        g2d.setTransform(originalTransform);
      }
    }
  }
  /**
   * Sets the CardinalityPoints attribute of the RelationLine object
   *
   * @todo refactor this duplicate code!!!
   */
  private void setCardinalityPoints() {
    // compute vector of length 1
    _mainLength =
        Point2D.distance(
            _mainLine.getX1(), _mainLine.getY1(), _mainLine.getX2(), _mainLine.getY2());
    double vx = (_mainLine.getX2() - _mainLine.getX1()) / _mainLength;
    double vy = (_mainLine.getY2() - _mainLine.getY1()) / _mainLength;
    double cardx = 20.0 * vx;
    double cardy = 20.0 * vy;
    double arrowx = 30.0 * vx;
    double arrowy = 30.0 * vy;
    if (_leftIsWest) {
      _leftArrowPoint.setLocation(_mainLine.getX1() + cardx, _mainLine.getY1() + cardy);
      _rightArrowPoint.setLocation(_mainLine.getX2() - cardx, _mainLine.getY2() - cardy);

      // The left-side arrow head
      _temp.setLocation(_mainLine.getX1() + arrowx, _mainLine.getY1() + arrowy);

      _arrowTransform.setToRotation(Math.PI / 6, _leftArrowPoint.getX(), _leftArrowPoint.getY());
      _arrowTransform.transform(_temp, _leftArrowPoint1);
      _leftArrowLine1.setLine(
          _leftArrowPoint.getX(),
          _leftArrowPoint.getY(),
          _leftArrowPoint1.getX(),
          _leftArrowPoint1.getY());

      _arrowTransform.setToRotation(-Math.PI / 6, _leftArrowPoint.getX(), _leftArrowPoint.getY());
      _arrowTransform.transform(_temp, _leftArrowPoint2);
      _leftArrowLine2.setLine(
          _leftArrowPoint.getX(),
          _leftArrowPoint.getY(),
          _leftArrowPoint2.getX(),
          _leftArrowPoint2.getY());

      _arrowTransform.setToRotation(Math.PI / 2, _leftArrowPoint.getX(), _leftArrowPoint.getY());
      _arrowTransform.transform(_temp, _leftCardinalityPoint);

      _arrowTransform.setToRotation(-Math.PI / 2, _leftArrowPoint.getX(), _leftArrowPoint.getY());
      _arrowTransform.transform(_temp, _leftFkPoint);

      // The right-side arrow head
      _temp.setLocation(_mainLine.getX2() - arrowx, _mainLine.getY2() - arrowy);

      _arrowTransform.setToRotation(Math.PI / 6, _rightArrowPoint.getX(), _rightArrowPoint.getY());
      _arrowTransform.transform(_temp, _rightArrowPoint1);
      _rightArrowLine1.setLine(
          _rightArrowPoint.getX(),
          _rightArrowPoint.getY(),
          _rightArrowPoint1.getX(),
          _rightArrowPoint1.getY());

      _arrowTransform.setToRotation(-Math.PI / 6, _rightArrowPoint.getX(), _rightArrowPoint.getY());
      _arrowTransform.transform(_temp, _rightArrowPoint2);
      _rightArrowLine2.setLine(
          _rightArrowPoint.getX(),
          _rightArrowPoint.getY(),
          _rightArrowPoint2.getX(),
          _rightArrowPoint2.getY());

      _arrowTransform.setToRotation(-Math.PI / 2, _rightArrowPoint.getX(), _rightArrowPoint.getY());
      _arrowTransform.transform(_temp, _rightCardinalityPoint);

      _arrowTransform.setToRotation(Math.PI / 2, _rightArrowPoint.getX(), _rightArrowPoint.getY());
      _arrowTransform.transform(_temp, _rightFkPoint);
    } else {
      _leftArrowPoint.setLocation(_mainLine.getX2() - cardx, _mainLine.getY2() - cardy);
      _rightArrowPoint.setLocation(_mainLine.getX1() + cardx, _mainLine.getY1() + cardy);

      // The left-side arrow head
      _temp.setLocation(_mainLine.getX2() - arrowx, _mainLine.getY2() - arrowy);

      _arrowTransform.setToRotation(Math.PI / 6, _leftArrowPoint.getX(), _leftArrowPoint.getY());
      _arrowTransform.transform(_temp, _leftArrowPoint1);
      _leftArrowLine1.setLine(
          _leftArrowPoint.getX(),
          _leftArrowPoint.getY(),
          _leftArrowPoint1.getX(),
          _leftArrowPoint1.getY());

      _arrowTransform.setToRotation(-Math.PI / 6, _leftArrowPoint.getX(), _leftArrowPoint.getY());
      _arrowTransform.transform(_temp, _leftArrowPoint2);
      _leftArrowLine2.setLine(
          _leftArrowPoint.getX(),
          _leftArrowPoint.getY(),
          _leftArrowPoint2.getX(),
          _leftArrowPoint2.getY());

      _arrowTransform.setToRotation(Math.PI / 2, _leftArrowPoint.getX(), _leftArrowPoint.getY());
      _arrowTransform.transform(_temp, _leftCardinalityPoint);

      _arrowTransform.setToRotation(-Math.PI / 2, _leftArrowPoint.getX(), _leftArrowPoint.getY());
      _arrowTransform.transform(_temp, _leftFkPoint);

      // The right-side arrow head
      _temp.setLocation(_mainLine.getX1() + arrowx, _mainLine.getY1() + arrowy);

      _arrowTransform.setToRotation(Math.PI / 6, _rightArrowPoint.getX(), _rightArrowPoint.getY());
      _arrowTransform.transform(_temp, _rightArrowPoint1);
      _rightArrowLine1.setLine(
          _rightArrowPoint.getX(),
          _rightArrowPoint.getY(),
          _rightArrowPoint1.getX(),
          _rightArrowPoint1.getY());

      _arrowTransform.setToRotation(-Math.PI / 6, _rightArrowPoint.getX(), _rightArrowPoint.getY());
      _arrowTransform.transform(_temp, _rightArrowPoint2);
      _rightArrowLine2.setLine(
          _rightArrowPoint.getX(),
          _rightArrowPoint.getY(),
          _rightArrowPoint2.getX(),
          _rightArrowPoint2.getY());

      _arrowTransform.setToRotation(-Math.PI / 2, _rightArrowPoint.getX(), _rightArrowPoint.getY());
      _arrowTransform.transform(_temp, _rightCardinalityPoint);

      _arrowTransform.setToRotation(Math.PI / 2, _rightArrowPoint.getX(), _rightArrowPoint.getY());
      _arrowTransform.transform(_temp, _rightFkPoint);
    }
  }
  /**
   * Updates all coordinates
   *
   * @todo Optimise. Some info can be cached
   * @todo Handle m:n (leftY/rightY etc)
   */
  public void update() {
    /*
     *  int leftY = _leftTable.getY() + _leftTable.getColumnY(_leftRole.getColumnMap().getPrimaryKey());
     *  int rightY;
     *  if (!_leftRole.getRelation().isMany2Many()) {
     *  rightY = _rightTable.getY() + _rightTable.getColumnY(_leftRole.getColumnMap().getForeignKey());
     *  }
     *  else {
     *  rightY = _rightTable.getY() + _rightTable.getColumnY(_rightRole.getColumnMap().getPrimaryKey());
     *  }
     */
    int leftY = _leftTable.getY() + _leftColumnY;
    int rightY = _rightTable.getY() + _rightColumnY;

    // find out which table is farthest west (and east)
    JTablePanel westTable;
    JTablePanel eastTable;
    int westY;
    int eastY;
    Line2D.Float[] westLines;
    Line2D.Float[] eastLines;
    int[] westEdgeY;
    int[] eastEdgeY;

    _leftIsWest = _leftTable.getX() < _rightTable.getX();

    if (_leftIsWest) {
      westTable = _leftTable;
      eastTable = _rightTable;
      westY = leftY;
      eastY = rightY;
      westLines = _leftLines;
      eastLines = _rightLines;
      westEdgeY = _leftEdgeY;
      eastEdgeY = _rightEdgeY;
    } else {
      westTable = _rightTable;
      eastTable = _leftTable;
      westY = rightY;
      eastY = leftY;
      westLines = _rightLines;
      eastLines = _leftLines;
      westEdgeY = _rightEdgeY;
      eastEdgeY = _leftEdgeY;
    }

    // find out whether the tables are more or less vertically aligned
    boolean aligned = (eastTable.getX() - westTable.getX()) < eastTable.getWidth() / 2;
    // TODO: handle different widths

    int westX;
    if (aligned) {
      westX = westTable.getX() - 10;
      for (int i = 0; i < westEdgeY.length; i++) {
        westLines[i].setLine(westX, westY, westTable.getX(), westEdgeY[i] + westTable.getY());
      }
    } else {
      westX = westTable.getX() + westTable.getWidth() + 10;
      for (int i = 0; i < westEdgeY.length; i++) {
        westLines[i].setLine(
            westX, westY, westTable.getX() + westTable.getWidth(), westEdgeY[i] + westTable.getY());
      }
    }
    int eastX = eastTable.getX() - 10;

    for (int i = 0; i < eastEdgeY.length; i++) {
      eastLines[i].setLine(eastX, eastY, eastTable.getX(), eastEdgeY[i] + eastTable.getY());
    }

    _mainLine.setLine(westX, westY, eastX, eastY);

    setCardinalityPoints();
  }
 public void line(float x1, float y1, float x2, float y2) {
   line.setLine(x1, y1, x2, y2);
   strokeShape(line);
 }