Google Tag Manager

2021/04/30

Resize the tab area of the JTabbedPane by dragging with the mouse cursor

Code

class TabAreaResizeLayer extends LayerUI<ClippedTitleTabbedPane> {
  private int offset;
  private boolean resizing;

  @Override public void installUI(JComponent c) {
    super.installUI(c);
    if (c instanceof JLayer) {
      ((JLayer<?>) c).setLayerEventMask(
          AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
    }
  }

  @Override public void uninstallUI(JComponent c) {
    if (c instanceof JLayer) {
      ((JLayer<?>) c).setLayerEventMask(0);
    }
    super.uninstallUI(c);
  }

  @Override protected void processMouseEvent(
        MouseEvent e, JLayer<? extends ClippedTitleTabbedPane> l) {
    ClippedTitleTabbedPane tabbedPane = l.getView();
    if (e.getID() == MouseEvent.MOUSE_PRESSED) {
      Rectangle rect = getDividerBounds(tabbedPane);
      Point pt = e.getPoint();
      SwingUtilities.convertPoint(e.getComponent(), pt, tabbedPane);
      if (rect.contains(pt)) {
        offset = pt.x - tabbedPane.getTabAreaWidth();
        tabbedPane.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
        resizing = true;
        e.consume();
      }
    } else if (e.getID() == MouseEvent.MOUSE_RELEASED) {
      tabbedPane.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
      resizing = false;
    }
  }

  @Override protected void processMouseMotionEvent(
        MouseEvent e, JLayer<? extends ClippedTitleTabbedPane> l) {
    ClippedTitleTabbedPane tabbedPane = l.getView();
    Point pt = e.getPoint();
    SwingUtilities.convertPoint(e.getComponent(), pt, tabbedPane);
    if (e.getID() == MouseEvent.MOUSE_MOVED) {
      Rectangle r = getDividerBounds(tabbedPane);
      Cursor c = Cursor.getPredefinedCursor(
          r.contains(pt) ? Cursor.W_RESIZE_CURSOR : Cursor.DEFAULT_CURSOR);
      tabbedPane.setCursor(c);
    } else if (e.getID() == MouseEvent.MOUSE_DRAGGED && resizing) {
      tabbedPane.setTabAreaWidth(pt.x - offset);
      e.consume();
    }
  }

  private static Rectangle getDividerBounds(ClippedTitleTabbedPane tabbedPane) {
    Dimension dividerSize = new Dimension(4, 4);
    Rectangle bounds = tabbedPane.getBounds();
    Rectangle compRect = Optional.ofNullable(tabbedPane.getSelectedComponent())
        .map(Component::getBounds).orElseGet(Rectangle::new);
    switch (tabbedPane.getTabPlacement()) {
      case SwingConstants.LEFT:
        bounds.x = compRect.x - dividerSize.width;
        bounds.width = dividerSize.width * 2;
        break;
      case SwingConstants.RIGHT:
        bounds.x += compRect.x + compRect.width - dividerSize.width;
        bounds.width = dividerSize.width * 2;
        break;
      case SwingConstants.BOTTOM:
        bounds.y += compRect.y + compRect.height - dividerSize.height;
        bounds.height = dividerSize.height * 2;
        break;
      default: // case SwingConstants.TOP:
        bounds.y = compRect.y - dividerSize.height;
        bounds.height = dividerSize.height * 2;
        break;
    }
    return bounds;
  }
}

References