public static Point getCenterOf(final Component aContainer, final JComponent content) {
    final JComponent component = getTargetComponent(aContainer);

    Point containerScreenPoint = component.getVisibleRect().getLocation();
    SwingUtilities.convertPointToScreen(containerScreenPoint, aContainer);

    return UIUtil.getCenterPoint(
        new Rectangle(containerScreenPoint, component.getVisibleRect().getSize()),
        content.getPreferredSize());
  }
  public static void scroll(
      JComponent c, Rectangle r, ScrollBias horizontalBias, ScrollBias verticalBias) {
    Rectangle visible = c.getVisibleRect();
    Rectangle dest = new Rectangle(r);

    if (dest.width > visible.width) {
      if (horizontalBias == ScrollBias.VIEWPORT) {
        // leave as is
      } else if (horizontalBias == ScrollBias.UNCHANGED) {
        if (dest.x <= visible.x && dest.x + dest.width >= visible.x + visible.width) {
          dest.width = visible.width;
        }
      } else {
        if (horizontalBias == ScrollBias.CENTER) {
          dest.x += (dest.width - visible.width) / 2;
        } else if (horizontalBias == ScrollBias.LAST) dest.x += dest.width - visible.width;

        dest.width = visible.width;
      }
    }

    if (dest.height > visible.height) {
      // same code as above in the other direction
    }

    if (!visible.contains(dest)) c.scrollRectToVisible(dest);
  }
  public static void center(JComponent c, Rectangle r, boolean withInsets) {
    Rectangle visible = c.getVisibleRect();

    visible.x = r.x - (visible.width - r.width) / 2;
    visible.y = r.y - (visible.height - r.height) / 2;

    Rectangle bounds = c.getBounds();
    Insets i = withInsets ? new Insets(0, 0, 0, 0) : c.getInsets();
    bounds.x = i.left;
    bounds.y = i.top;
    bounds.width -= i.left + i.right;
    bounds.height -= i.top + i.bottom;

    if (visible.x < bounds.x) visible.x = bounds.x;

    if (visible.x + visible.width > bounds.x + bounds.width)
      visible.x = bounds.x + bounds.width - visible.width;

    if (visible.y < bounds.y) visible.y = bounds.y;

    if (visible.y + visible.height > bounds.y + bounds.height)
      visible.y = bounds.y + bounds.height - visible.height;

    c.scrollRectToVisible(visible);
  }
Exemple #4
0
  private void moveVisibleRect(Point center) {
    JComponent component = scene.getView();
    if (component == null) {
      return;
    }
    double zoomFactor = scene.getZoomFactor();
    Rectangle bounds = scene.getBounds();
    Dimension size = getSize();

    double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0;
    double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0;
    double scale = Math.min(sx, sy);

    int vw = (int) (scale * bounds.width);
    int vh = (int) (scale * bounds.height);
    int vx = (size.width - vw) / 2;
    int vy = (size.height - vh) / 2;

    int cx = (int) ((double) (center.x - vx) / scale * zoomFactor);
    int cy = (int) ((double) (center.y - vy) / scale * zoomFactor);

    Rectangle visibleRect = component.getVisibleRect();
    visibleRect.x = cx - visibleRect.width / 2;
    visibleRect.y = cy - visibleRect.height / 2;
    component.scrollRectToVisible(visibleRect);

    this.repaint();
  }
  public void updateLocation(JComponent container) {
    final Rectangle rec = container.getVisibleRect();

    final Dimension iconSize = getPreferredSize();

    final Rectangle newBounds =
        new Rectangle(rec.x + rec.width - iconSize.width, rec.y, iconSize.width, iconSize.height);
    if (!newBounds.equals(getBounds())) {
      setBounds(newBounds);
      container.repaint();
    }
  }
  private void attach(Panner panner, boolean nextCorner) {

    Rectangle visible = target.getVisibleRect();
    Dimension sz = size;
    if (sz == null) {
      sz = new Dimension(visible.width, visible.height);
      sz.width = sz.width * percent / 100;
      sz.height = sz.height * percent / 100;
    }
    panner.setSize(sz);
    if (corner != CENTER && nextCorner) {
      if (System.currentTimeMillis() - lastHidden < MOVE_TIMEOUT) {
        corner = (corner + 1) & 0x3;
      } else {
        corner = 0;
      }
    }
    int x = visible.x;
    int y = visible.y;
    switch (corner) {
      case UL:
        x += offset.x;
        y += offset.y;
        break;
      case UR:
        x += visible.width - offset.x - panner.getPreferredSize().width;
        y += offset.y;
        break;
      case LR:
        x += visible.width - offset.x - panner.getPreferredSize().width;
        y += visible.height - offset.y - panner.getPreferredSize().height;
        break;
      case LL:
        x += offset.x;
        y += visible.height - offset.y - panner.getPreferredSize().height;
        break;
      case CENTER:
      default:
        x += (visible.width - panner.getPreferredSize().width) / 2;
        y += (visible.height - panner.getPreferredSize().height) / 2;
        break;
    }
    panner.attach(x, y);
    panner.revalidate();
    panner.repaint();
  }
Exemple #7
0
  public void paint(Graphics g) {
    Graphics2D gr = (Graphics2D) g;
    super.paint(g);
    Rectangle bounds = scene.getBounds();
    Dimension size = getSize();

    double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0;
    double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0;
    double scale = Math.min(sx, sy);

    int vw = (int) (scale * bounds.width);
    int vh = (int) (scale * bounds.height);
    int vx = (size.width - vw) / 2;
    int vy = (size.height - vh) / 2;

    if (image == null || vw != imageWidth || vh != imageHeight) {

      imageWidth = vw;
      imageHeight = vh;
      image = this.createImage(imageWidth, imageHeight);
      Graphics2D ig = (Graphics2D) image.getGraphics();
      ig.scale(scale, scale);
      scene.setRealZoomFactor(scale);
      scene.paint(ig);
      scene.setRealZoomFactor(0.0);
    }

    gr.drawImage(image, vx, vy, this);

    JComponent component = scene.getView();
    double zoomFactor = scene.getZoomFactor();
    Rectangle viewRectangle = component != null ? component.getVisibleRect() : null;
    if (viewRectangle != null) {
      Rectangle window =
          new Rectangle(
              (int) ((double) viewRectangle.x * scale / zoomFactor),
              (int) ((double) viewRectangle.y * scale / zoomFactor),
              (int) ((double) viewRectangle.width * scale / zoomFactor),
              (int) ((double) viewRectangle.height * scale / zoomFactor));
      window.translate(vx, vy);
      gr.setColor(new Color(200, 200, 200, 128));
      gr.fill(window);
      gr.setColor(Color.BLACK);
      gr.drawRect(window.x, window.y, window.width - 1, window.height - 1);
    }
  }
  /**
   * @param aEvent
   * @param aStartPoint
   */
  protected void handleZoomRegion(final MouseEvent aEvent, final Point aStartPoint) {
    // For now, disabled by default as it isn't 100% working yet...
    if (Boolean.FALSE.equals(Boolean.valueOf(System.getProperty("zoomregionenabled", "false")))) {
      return;
    }

    final JComponent source = (JComponent) aEvent.getComponent();
    final boolean dragging = (aEvent.getID() == MouseEvent.MOUSE_DRAGGED);

    final GhostGlassPane glassPane =
        (GhostGlassPane) SwingUtilities.getRootPane(source).getGlassPane();

    Rectangle viewRect;
    final JScrollPane scrollPane =
        SwingComponentUtils.getAncestorOfClass(JScrollPane.class, source);
    if (scrollPane != null) {
      final JViewport viewport = scrollPane.getViewport();
      viewRect = SwingUtilities.convertRectangle(viewport, viewport.getVisibleRect(), glassPane);
    } else {
      viewRect = SwingUtilities.convertRectangle(source, source.getVisibleRect(), glassPane);
    }

    final Point start = SwingUtilities.convertPoint(source, aStartPoint, glassPane);
    final Point current = SwingUtilities.convertPoint(source, aEvent.getPoint(), glassPane);

    if (dragging) {
      if (!glassPane.isVisible()) {
        glassPane.setVisible(true);
        glassPane.setRenderer(new RubberBandRenderer(), start, current, viewRect);
      } else {
        glassPane.updateRenderer(start, current, viewRect);
      }

      glassPane.repaintPartially();
    } else
    /* if ( !dragging ) */
    {
      // Fire off a signal to the zoom controller to do its job...
      this.controller.getZoomController().zoomRegion(aStartPoint, aEvent.getPoint());

      glassPane.setVisible(false);
    }
  }
Exemple #9
0
 public static void paintImmediately(JComponent component) {
   component.paintImmediately(component.getVisibleRect());
 }
Exemple #10
0
  /**
   * Must be overwritten because Swing uses this method to tell if 2 elements are overlapping It's
   * also used to determine which element gets selected if there are overlapping elements (the
   * smallest one) IMPORTANT: on overlapping elements, contains is called for all elements until the
   * first one returns true, then the others contain methods are not called
   *
   * <p>In future this logic should be the same as in
   * BaseletGWT.DrawPanel.getGridElementOnPosition() which is much simpler because its externally
   * calculated (this migration is only possible if there are not GridElement Listeners, but instead
   * only one Diagram Listener which delegates the event to the specific GridElement like in GWT)
   */
  public static boolean checkForOverlap(GridElement gridElement, Point p) {
    JComponent component = (JComponent) gridElement.getComponent();
    java.awt.Rectangle rectangle = component.getVisibleRect();
    Point absolute =
        new Point(
            gridElement.getRectangle().getX() + p.getX(),
            gridElement.getRectangle().getY() + p.getY());
    if (!rectangle.contains(p.x, p.y)) {
      return false;
    }

    DrawPanel drawPanel = HandlerElementMap.getHandlerForElement(gridElement).getDrawPanel();
    List<GridElement> elements = drawPanel.getGridElements();
    Selector selector = drawPanel.getSelector();
    for (GridElement other : elements) {
      if (other == gridElement
          || other.getLayer() < gridElement.getLayer()
          || !other.isSelectableOn(absolute)) {
        continue; // ignore this element, elements with a lower layer and elements which are not
                  // selectable on the point
      }

      // issue #260: if a relation is the only selected element its selection should be retained
      if (gridElement instanceof Relation && other instanceof Relation) {
        if (selector.isSelectedOnly(gridElement)) {
          return true;
        }
        if (selector.isSelectedOnly(other)) {
          return false;
        }
      }

      JComponent otherComponent = (JComponent) other.getComponent();
      if (other.getLayer()
          > gridElement
              .getLayer()) { // elements with higher layer can "overwrite" contains-value of this
        // move point to coordinate system of other entity
        Point other_p =
            new Point(
                p.x + gridElement.getRectangle().x - other.getRectangle().x,
                p.y + gridElement.getRectangle().y - other.getRectangle().y);
        if (otherComponent.contains(Converter.convert(other_p))) {
          return false;
        }
      }

      java.awt.Rectangle other_rectangle = otherComponent.getVisibleRect();
      // move bounds to coordinate system of this component
      other_rectangle.x += other.getRectangle().x - gridElement.getRectangle().x;
      other_rectangle.y += other.getRectangle().y - gridElement.getRectangle().y;
      // when elements intersect, select the smaller element except if it is an old relation
      // (because they have a larger rectangle than they use). NOTE: Old Relations are not checked
      // because they do not properly implement isSelectableOn
      if (!(other instanceof com.baselet.element.old.element.Relation)
          && rectangle.intersects(other_rectangle)
          && firstSmallerThanSecond(other_rectangle, rectangle)) {
        return false;
      }
    }
    return true;
  }
 /**
  * Returns a point at the center of the visible rectangle of the given {@code JComponent}.
  *
  * <p><b>Note:</b> This method is accessed in the current executing thread. Such thread may or may
  * not be the event dispatch thread (EDT.) Client code must call this method from the EDT.
  *
  * @param c the given {@code JComponent}.
  * @return a point at the center of the visible rectangle of the given {@code JComponent}.
  */
 @RunsInCurrentThread
 public static @Nonnull Point centerOfVisibleRect(@Nonnull JComponent c) {
   Rectangle r = c.getVisibleRect();
   return centerOf(checkNotNull(r));
 }
  @NotNull
  @Override
  public RelativePoint guessBestPopupLocation(@NotNull final JComponent component) {
    Point popupMenuPoint = null;
    final Rectangle visibleRect = component.getVisibleRect();
    if (component instanceof JList) { // JList
      JList list = (JList) component;
      int firstVisibleIndex = list.getFirstVisibleIndex();
      int lastVisibleIndex = list.getLastVisibleIndex();
      int[] selectedIndices = list.getSelectedIndices();
      for (int index : selectedIndices) {
        if (firstVisibleIndex <= index && index <= lastVisibleIndex) {
          Rectangle cellBounds = list.getCellBounds(index, index);
          popupMenuPoint =
              new Point(visibleRect.x + visibleRect.width / 4, cellBounds.y + cellBounds.height);
          break;
        }
      }
    } else if (component instanceof JTree) { // JTree
      JTree tree = (JTree) component;
      int[] selectionRows = tree.getSelectionRows();
      if (selectionRows != null) {
        Arrays.sort(selectionRows);
        for (int i = 0; i < selectionRows.length; i++) {
          int row = selectionRows[i];
          Rectangle rowBounds = tree.getRowBounds(row);
          if (visibleRect.contains(rowBounds)) {
            popupMenuPoint = new Point(rowBounds.x + 2, rowBounds.y + rowBounds.height - 1);
            break;
          }
        }
        if (popupMenuPoint == null) { // All selected rows are out of visible rect
          Point visibleCenter =
              new Point(
                  visibleRect.x + visibleRect.width / 2, visibleRect.y + visibleRect.height / 2);
          double minDistance = Double.POSITIVE_INFINITY;
          int bestRow = -1;
          Point rowCenter;
          double distance;
          for (int i = 0; i < selectionRows.length; i++) {
            int row = selectionRows[i];
            Rectangle rowBounds = tree.getRowBounds(row);
            rowCenter =
                new Point(rowBounds.x + rowBounds.width / 2, rowBounds.y + rowBounds.height / 2);
            distance = visibleCenter.distance(rowCenter);
            if (minDistance > distance) {
              minDistance = distance;
              bestRow = row;
            }
          }

          if (bestRow != -1) {
            Rectangle rowBounds = tree.getRowBounds(bestRow);
            tree.scrollRectToVisible(
                new Rectangle(
                    rowBounds.x,
                    rowBounds.y,
                    Math.min(visibleRect.width, rowBounds.width),
                    rowBounds.height));
            popupMenuPoint = new Point(rowBounds.x + 2, rowBounds.y + rowBounds.height - 1);
          }
        }
      }
    } else if (component instanceof JTable) {
      JTable table = (JTable) component;
      int column = table.getColumnModel().getSelectionModel().getLeadSelectionIndex();
      int row =
          Math.max(
              table.getSelectionModel().getLeadSelectionIndex(),
              table.getSelectionModel().getAnchorSelectionIndex());
      Rectangle rect = table.getCellRect(row, column, false);
      if (!visibleRect.intersects(rect)) {
        table.scrollRectToVisible(rect);
      }
      popupMenuPoint = new Point(rect.x, rect.y + rect.height);
    } else if (component instanceof PopupOwner) {
      popupMenuPoint = ((PopupOwner) component).getBestPopupPosition();
    }
    if (popupMenuPoint == null) {
      popupMenuPoint =
          new Point(visibleRect.x + visibleRect.width / 2, visibleRect.y + visibleRect.height / 2);
    }

    return new RelativePoint(component, popupMenuPoint);
  }