private TabLabel findMostOverlapping(Axis measurer, TabLabel... labels) { double freeSpace; if (measurer.getMinValue(myDragRec) < measurer.getMinValue(myDragOriginalRec)) { freeSpace = measurer.getMaxValue(myDragOriginalRec) - measurer.getMaxValue(myDragRec); } else { freeSpace = measurer.getMinValue(myDragRec) - measurer.getMinValue(myDragOriginalRec); } int max = -1; TabLabel maxLabel = null; for (TabLabel each : labels) { if (each == null) continue; final Rectangle eachBounds = each.getBounds(); if (measurer.getSize(eachBounds) > freeSpace + freeSpace *0.3) continue; Rectangle intersection = myDragRec.intersection(eachBounds); int size = intersection.width * intersection.height; if (size > max) { max = size; maxLabel = each; } } return maxLabel; }
protected void processDrag(MouseEvent event, Point targetScreenPoint, Point startPointScreen) { if (!myTabs.isTabDraggingEnabled() || !isDragSource(event) || !MouseDragHelper.checkModifiers(event)) return; SwingUtilities.convertPointFromScreen(startPointScreen, myTabs); if (isDragJustStarted()) { if (myPressedTabLabel == null) return; final Rectangle labelBounds = myPressedTabLabel.getBounds(); myHoldDelta = new Dimension(startPointScreen.x - labelBounds.x, startPointScreen.y - labelBounds.y); myDragSource = myPressedTabLabel.getInfo(); myDragRec = new Rectangle(startPointScreen, labelBounds.getSize()); myDragOriginalRec = (Rectangle)myDragRec.clone(); myDragOriginalRec.x -= myHoldDelta.width; myDragOriginalRec.y -= myHoldDelta.height; } else { if (myDragRec == null) return; final Point toPoint = SwingUtilities.convertPoint(event.getComponent(), event.getPoint(), myTabs); myDragRec.x = toPoint.x; myDragRec.y = toPoint.y; } myDragRec.x -= myHoldDelta.width; myDragRec.y -= myHoldDelta.height; final Rectangle headerRec = myTabs.getLastLayoutPass().getHeaderRectangle(); ScreenUtil.moveToFit(myDragRec, headerRec, null); int deadZoneX = 0; int deadZoneY = 0; final TabLabel top = findLabel(new Point(myDragRec.x + myDragRec.width / 2, myDragRec.y + deadZoneY)); final TabLabel bottom = findLabel(new Point(myDragRec.x + myDragRec.width / 2, myDragRec.y + myDragRec.height - deadZoneY)); final TabLabel left = findLabel(new Point(myDragRec.x + deadZoneX, myDragRec.y + myDragRec.height / 2)); final TabLabel right = findLabel(new Point(myDragRec.x + myDragRec.width - deadZoneX, myDragRec.y + myDragRec.height / 2)); TabLabel targetLabel; if (myTabs.isHorizontalTabs()) { targetLabel = findMostOverlapping(Axis.X, left, right); if (targetLabel == null) { targetLabel = findMostOverlapping(Axis.Y, top, bottom); } } else { targetLabel = findMostOverlapping(Axis.Y, top, bottom); if (targetLabel == null) { targetLabel = findMostOverlapping(Axis.X, left, right); } } if (targetLabel != null) { Rectangle saved = myDragRec; myDragRec = null; myTabs.reallocate(myDragSource, targetLabel.getInfo()); myDragOriginalRec = myTabs.myInfo2Label.get(myDragSource).getBounds(); myDragRec = saved; myTabs.moveDraggedTabLabel(); } else { myTabs.moveDraggedTabLabel(); final int border = myTabs.getTabsBorder().getTabBorderSize(); headerRec.x -= border; headerRec.y -= border; headerRec.width += border * 2; headerRec.height += border * 2; myTabs.repaint(headerRec); } event.consume(); }
protected void processDrag(MouseEvent event, Point targetScreenPoint, Point startPointScreen) { if (!myTabs.isTabDraggingEnabled() || !isDragSource(event)) return; SwingUtilities.convertPointFromScreen(startPointScreen, myTabs); if (isDragJustStarted()) { if (myPressedTabLabel == null) return; final Rectangle labelBounds = myPressedTabLabel.getBounds(); myHoldDelta = new Dimension(startPointScreen.x - labelBounds.x, startPointScreen.y - labelBounds.y); myDragSource = myPressedTabLabel.getInfo(); myDragRec = new Rectangle(startPointScreen, labelBounds.getSize()); myDragOriginalRec = (Rectangle)myDragRec.clone(); myDragOriginalRec.x -= myHoldDelta.width; myDragOriginalRec.y -= myHoldDelta.height; } else { if (myDragRec == null) return; final Point toPoint = SwingUtilities.convertPoint(event.getComponent(), event.getPoint(), myTabs); myDragRec.x = toPoint.x; myDragRec.y = toPoint.y; } myDragRec.x -= myHoldDelta.width; myDragRec.y -= myHoldDelta.height; final Rectangle headerRec = myTabs.getLastLayoutPass().getHeaderRectangle(); ScreenUtil.moveToFit(myDragRec, headerRec, null); int deadZoneX = 0; int deadZoneY = 0; final TabLabel top = findLabel(new Point(myDragRec.x + myDragRec.width / 2, myDragRec.y + deadZoneY)); final TabLabel bottom = findLabel(new Point(myDragRec.x + myDragRec.width / 2, myDragRec.y + myDragRec.height - deadZoneY)); final TabLabel left = findLabel(new Point(myDragRec.x + deadZoneX, myDragRec.y + myDragRec.height / 2)); final TabLabel right = findLabel(new Point(myDragRec.x + myDragRec.width - deadZoneX, myDragRec.y + myDragRec.height / 2)); TabLabel targetLabel; if (myTabs.isHorizontalTabs()) { targetLabel = findMostOverlapping(Axis.X, left, right); if (targetLabel == null) { targetLabel = findMostOverlapping(Axis.Y, top, bottom); } } else { targetLabel = findMostOverlapping(Axis.Y, top, bottom); if (targetLabel == null) { targetLabel = findMostOverlapping(Axis.X, left, right); } } if (targetLabel != null) { Rectangle saved = myDragRec; myDragRec = null; myTabs.reallocate(myDragSource, targetLabel.getInfo()); myDragOriginalRec = myTabs.myInfo2Label.get(myDragSource).getBounds(); myDragRec = saved; myTabs.moveDraggedTabLabel(); } else { myTabs.moveDraggedTabLabel(); final int border = myTabs.getTabsBorder().getTabBorderSize(); headerRec.x -= border; headerRec.y -= border; headerRec.width += border * 2; headerRec.height += border * 2; myTabs.repaint(headerRec); } event.consume(); }