/** * @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; } }
/** * 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); } }