📄 defaultregionchecker.java
字号:
/*
* Created on Mar 11, 2005
*/
package org.flexdock.docking.defaults;
import java.awt.Component;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import org.flexdock.docking.Dockable;
import org.flexdock.docking.DockingConstants;
import org.flexdock.docking.DockingManager;
import org.flexdock.docking.DockingPort;
import org.flexdock.docking.RegionChecker;
/**
* @author Christopher Butler
*/
public class DefaultRegionChecker implements RegionChecker, DockingConstants {
/**
* Returns the docking region of the supplied {@code Component} that
* contains the coordinates of the specified {@code Point}. If either
* {@code comp} or {@code point} is {@code null}, then
* {@code UNKNOWN_REGION} is returned. If the specified {@code Component}
* bounds do not contain the supplied {@code Point}, then
* {@code UNKNOWN_REGION} is returned.
* <p>
* This implementation assumes that {@code comp} is a {@code Component}
* embedded within a {@code DockingPort}. If {@code comp} is itself a
* {@code DockingPort}, then {@code CENTER_REGION} is returned. Otherwise,
* the returned region is based upon a section of the bounds of the
* specified {@code Component} relative to the containing
* {@code DockingPort}.
* <p>
* This method divides the specified {@code Component's} bounds into four
* {@code Rectangles} determined by {@code getNorthRegion(Component c)},
* {@code getSouthRegion(Component c)}, {@code getEastRegion(Component c)},
* and {@code getWestRegion(Component c)}, respectively. Each
* {@code Rectangle} is then checked to see if it contains the specified
* {@code Point}. The order of precedence is NORTH, SOUTH, EAST, and then
* WEST. If the specified {@code Point} is contained by the
* {@code Component} bounds but none of the sub-{@code Rectangles}, then
* {@code CENTER_REGION} is returned.
* <p>
* For NORTH and SOUTH {@code Rectangles}, the distance is checked between
* the top/bottom and left or right edge of the regional bounds. If the
* horizontal distance to the regional edge is smaller than the vertical
* distance, then EAST or WEST takes precendence of NORTH or SOUTH. This
* allows for proper determination between "northeast", "northwest",
* "southeast", and "southwest" cases.
*
* @param comp
* the {@code Component} whose region is to be examined.
* @param point
* the coordinates whose region is to be determined.
* @return the docking region containing the specified {@code Point}.
* @see RegionChecker#getRegion(Component, Point)
* @see #getNorthRegion(Component)
* @see #getSouthRegion(Component)
* @see #getEastRegion(Component)
* @see #getWestRegion(Component)
*/
public String getRegion(Component comp, Point point) {
if (comp == null || point == null)
return UNKNOWN_REGION;
// make sure the point is actually inside of the target dockingport
Rectangle targetArea = comp.getBounds();
// if our target component is the dockingport itself, then getBounds()
// would
// have returned a target area relative to the dockingport's parent.
// reset
// relative to the dockingport.
if (comp instanceof DockingPort)
targetArea.setLocation(0, 0);
if (!targetArea.contains(point))
return UNKNOWN_REGION;
// if our target component is the dockingport, then the dockingport is
// currently empty and all points within it are in the CENTER
if (comp instanceof DockingPort)
return CENTER_REGION;
// start with the north region
Rectangle north = getNorthRegion(comp);
int rightX = north.x + north.width;
if (north.contains(point)) {
// check NORTH_WEST
Rectangle west = getWestRegion(comp);
if (west.contains(point)) {
Polygon westPoly = new Polygon();
westPoly.addPoint(0, 0);
westPoly.addPoint(0, north.height);
westPoly.addPoint(west.width, north.height);
return westPoly.contains(point) ? WEST_REGION : NORTH_REGION;
}
// check NORTH_EAST
Rectangle east = getEastRegion(comp);
if (east.contains(point)) {
Polygon eastPoly = new Polygon();
eastPoly.addPoint(rightX, 0);
eastPoly.addPoint(rightX, north.height);
eastPoly.addPoint(east.x, north.height);
return eastPoly.contains(point) ? EAST_REGION : NORTH_REGION;
}
return NORTH_REGION;
}
// check with the south region
Rectangle south = getSouthRegion(comp);
int bottomY = south.y + south.height;
if (south.contains(point)) {
// check SOUTH_WEST
Rectangle west = getWestRegion(comp);
if (west.contains(point)) {
Polygon westPoly = new Polygon();
westPoly.addPoint(0, south.y);
westPoly.addPoint(west.width, south.y);
westPoly.addPoint(0, bottomY);
return westPoly.contains(point) ? WEST_REGION : SOUTH_REGION;
}
// check SOUTH_EAST
Rectangle east = getEastRegion(comp);
if (east.contains(point)) {
Polygon eastPoly = new Polygon();
eastPoly.addPoint(east.y, south.y);
eastPoly.addPoint(rightX, south.y);
eastPoly.addPoint(rightX, bottomY);
return eastPoly.contains(point) ? EAST_REGION : SOUTH_REGION;
}
return SOUTH_REGION;
}
// Now check EAST and WEST. We've already checked NORTH and SOUTH, so we
// don't have to
// check for NE, SE, NW, and SW anymore.
Rectangle east = getEastRegion(comp);
if (east.contains(point))
return EAST_REGION;
Rectangle west = getWestRegion(comp);
if (west.contains(point))
return WEST_REGION;
// not in any of the outer regions, so return CENTER.
return CENTER_REGION;
}
/**
* Returns the rectangular bounds within the specified component that
* represent it's {@code DockingConstants.NORTH_REGION}. This method
* dispatches to {@code getRegionBounds(Component c, String region)},
* passing an argument of {@code DockingConstants.NORTH_REGION} for the
* region parameter. If the specified {@code Component} is {@code null},
* then a {@code null} reference is returned.
*
* @param c
* the {@code Component} whose north region is to be returned.
* @return the bounds containing the north region of the specified
* {@code Component}.
* @see RegionChecker#getNorthRegion(Component)
* @see #getRegionBounds(Component, String)
*/
public Rectangle getNorthRegion(Component c) {
return getRegionBounds(c, NORTH_REGION);
}
/**
* Returns the rectangular bounds within the specified component that
* represent it's {@code DockingConstants.SOUTH_REGION}. This method
* dispatches to {@code getRegionBounds(Component c, String region)},
* passing an argument of {@code DockingConstants.SOUTH_REGION} for the
* region parameter. If the specified {@code Component} is {@code null},
* then a {@code null} reference is returned.
*
* @param c
* the {@code Component} whose south region is to be returned.
* @return the bounds containing the north region of the specified
* {@code Component}.
* @see RegionChecker#getSouthRegion(Component)
* @see #getRegionBounds(Component, String)
*/
public Rectangle getSouthRegion(Component c) {
return getRegionBounds(c, SOUTH_REGION);
}
/**
* Returns the rectangular bounds within the specified component that
* represent it's {@code DockingConstants.EAST_REGION}. This method
* dispatches to {@code getRegionBounds(Component c, String region)},
* passing an argument of {@code DockingConstants.EAST_REGION} for the
* region parameter. If the specified {@code Component} is {@code null},
* then a {@code null} reference is returned.
*
* @param c
* the {@code Component} whose east region is to be returned.
* @return the bounds containing the north region of the specified
* {@code Component}.
* @see RegionChecker#getEastRegion(Component)
* @see #getRegionBounds(Component, String)
*/
public Rectangle getEastRegion(Component c) {
return getRegionBounds(c, EAST_REGION);
}
/**
* Returns the rectangular bounds within the specified component that
* represent it's {@code DockingConstants.WEST_REGION}. This method
* dispatches to {@code getRegionBounds(Component c, String region)},
* passing an argument of {@code DockingConstants.WEST_REGION} for the
* region parameter. If the specified {@code Component} is {@code null},
* then a {@code null} reference is returned.
*
* @param c
* the {@code Component} whose west region is to be returned.
* @return the bounds containing the north region of the specified
* {@code Component}.
* @see RegionChecker#getWestRegion(Component)
* @see #getRegionBounds(Component, String)
*/
public Rectangle getWestRegion(Component c) {
return getRegionBounds(c, WEST_REGION);
}
/**
* Returns the bounding {@code Rectangle} within the specified component
* that represents the specified region. If {@code c} or {@code region} are
* null, then this method returns a {@code null} reference.
* <p>
* This method dispatches to
* {@code getRegionSize(Component c, String region)} to determine the
* proportional size of the specified {@code Component} dedicated to the
* specified region. It then multiplies this value by the relevant
* {@code Component} dimension (<i>{@code width} for east/west,
* {@code height} for north/south</i>) and returns a {@code Rectangle} with
* the resulting dimension, spanning the {@code Component} edge for the
* specified region.
*
* @param c
* the {@code Component} whose region bounds are to be returned.
* @param region
* the specified region that is to be examined.
* @return the bounds containing the supplied region of the specified
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -