📄 tablecellbackground.java
字号:
{
final TableCellBackground merged = createMergedInstance(cellBounds);
merged.color = color;
merged.mergeRightBorder(background);
return merged;
}
// the merged area is dominated by the background ...
if (cellBounds.equals(getBounds()))
{
final TableCellBackground merged = createMergedInstance(cellBounds);
merged.color = color;
merged.mergeAllBorders(background);
return merged;
}
// the merged area is dominated by the background ...
if (cellBounds.equals(background.getBounds()))
{
final TableCellBackground merged = background.createMergedInstance(cellBounds);
merged.color = color;
merged.mergeAllBorders(this);
return merged;
}
// we don't copy the borders, just the plain background color
// as the borders won't fit ...
// our background should define the cells background ...
if (getBounds().contains(cellBounds))
{
final TableCellBackground merged = createMergedInstance(cellBounds);
merged.color = color;
return merged;
}
else
{
final TableCellBackground merged = background.createMergedInstance(cellBounds);
merged.color = color;
return merged;
}
// create an unmerged instance in any other case, as the bounds are not releated
// in a useable way.
}
/**
* Checks, whether the given background definition defines a bottom border of the
* unified background.
*
* @param bounds the unified bounds.
* @param background the background definition which should be checked.
* @return true, if the background definition defines a bottom border, false otherwise.
*/
private boolean isBottomBorderDefinition
(final Rectangle2D bounds, final TableCellBackground background)
{
final Rectangle2D bgBounds = background.getBounds();
return ((bgBounds.getHeight() == 0) &&
(bgBounds.getY() == (bounds.getHeight() + bounds.getY())));
}
/**
* The background defines a bottom border for the cell.
*
* @param background the background
*/
private void mergeBottomBorder (final TableCellBackground background)
{
// map the background's top border to the bottom side
if (getColorBottom() == null || getBorderSizeBottom() == 0)
{
setBorderBottom(background.getColorTop(), background.getBorderSizeTop());
}
}
/**
* Checks, whether the given background definition defines a right border of the
* unified background.
*
* @param bounds the unified bounds.
* @param background the background definition which should be checked.
* @return true, if the background definition defines a right border, false otherwise.
*/
private boolean isRightBorderDefinition
(final Rectangle2D bounds, final TableCellBackground background)
{
final Rectangle2D bgBounds = background.getBounds();
return ((bgBounds.getWidth() == 0) &&
(bgBounds.getX() == (bounds.getX() + bounds.getWidth())));
}
/**
* Merges the left border of the background definition with the right border
* of this one.
*
* @param background the other background definition.
*/
private void mergeRightBorder (final TableCellBackground background)
{
// map the background's left border to the right side
if (getColorRight() == null || getBorderSizeRight() == 0)
{
setBorderRight(background.getColorLeft(), background.getBorderSizeLeft());
}
}
/**
* Merges the borders of the <code>background</code> cell and stores the result in
* the <code>merged</code> cell. This should only be called, if the bounds of both
* background definitions are equal, or the result may be undefined.
*
* @param background the background cell that should be merged with this cell.
*/
private void mergeAllBorders(final TableCellBackground background)
{
if (getColorTop() == null || getBorderSizeTop() == 0)
{
setBorderTop(background.getColorTop(), background.getBorderSizeTop());
}
if (getColorLeft() == null || getBorderSizeLeft() == 0)
{
setBorderLeft(background.getColorLeft(), background.getBorderSizeLeft());
}
if (getColorRight() == null || getBorderSizeRight() == 0)
{
setBorderRight(background.getColorRight(), background.getBorderSizeRight());
}
if (getColorBottom() == null || getBorderSizeBottom() == 0)
{
setBorderBottom(background.getColorBottom(), background.getBorderSizeBottom());
}
}
/**
* Creates an merged instance of this background for the given bounds.
*
* @param bounds the new bounds of the merged background
* @return a copy of this background with new bounds.
*/
protected TableCellBackground createMergedInstance (final Rectangle2D bounds)
{
try
{
final TableCellBackground bg = (TableCellBackground) clone();
bg.setBounds(bounds);
return bg;
}
catch (CloneNotSupportedException cne)
{
// should not happen
throw new IllegalStateException("Clone caused an unexpected error.");
}
}
/**
* Adds two colors, the result is the mixed color of the base color and the paint color.
*
* @param base the base color
* @param paint the overlay color
* @return the merged colors.
*/
private Color addColor(final Color base, final Color paint)
{
final BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
final Graphics g = img.getGraphics();
g.setColor(Color.white);
g.drawRect(0, 0, 1, 1);
g.setColor(base);
g.drawRect(0, 0, 1, 1);
g.setColor(paint);
g.drawRect(0, 0, 1, 1);
return new Color(img.getRGB(0, 0), true);
}
/**
* Returns an String representation of this table cell background.
*
* @return the string representation of this cell background.
*/
public String toString()
{
final StringBuffer b = new StringBuffer();
b.append("TableCellBackground={bounds=");
b.append(getBounds());
b.append(", color=");
b.append(color);
b.append(", colorTop=");
b.append(colorTop);
b.append(", widthTop=");
b.append(borderSizeTop);
b.append(", colorLeft=");
b.append(colorLeft);
b.append(", widthLeft=");
b.append(borderSizeLeft);
b.append(", colorBottom=");
b.append(colorBottom);
b.append(", widthBottom=");
b.append(borderSizeBottom);
b.append(", colorRight=");
b.append(colorRight);
b.append(", widthRight=");
b.append(borderSizeRight);
b.append("}");
return b.toString();
}
/**
* Tests this object for equality with another object.
*
* @param o the other object.
*
* @return A boolean.
*/
public boolean equals(final Object o)
{
if (this == o)
{
return true;
}
if (!(o instanceof TableCellBackground))
{
return false;
}
final TableCellBackground tableCellBackground = (TableCellBackground) o;
if (borderSizeBottom != tableCellBackground.borderSizeBottom)
{
return false;
}
if (borderSizeLeft != tableCellBackground.borderSizeLeft)
{
return false;
}
if (borderSizeRight != tableCellBackground.borderSizeRight)
{
return false;
}
if (borderSizeTop != tableCellBackground.borderSizeTop)
{
return false;
}
if (color != null ? !color.equals(tableCellBackground.color)
: tableCellBackground.color != null)
{
return false;
}
if (colorBottom != null ? !colorBottom.equals(tableCellBackground.colorBottom)
: tableCellBackground.colorBottom != null)
{
return false;
}
if (colorLeft != null ? !colorLeft.equals(tableCellBackground.colorLeft)
: tableCellBackground.colorLeft != null)
{
return false;
}
if (colorRight != null ? !colorRight.equals(tableCellBackground.colorRight)
: tableCellBackground.colorRight != null)
{
return false;
}
if (colorTop != null ? !colorTop.equals(tableCellBackground.colorTop)
: tableCellBackground.colorTop != null)
{
return false;
}
return true;
}
/**
* Returns a hash code for this object.
*
* @return A hash code.
*/
public int hashCode()
{
int result;
result = Float.floatToIntBits(borderSizeTop);
result = 29 * result + Float.floatToIntBits(borderSizeBottom);
result = 29 * result + Float.floatToIntBits(borderSizeLeft);
result = 29 * result + Float.floatToIntBits(borderSizeRight);
result = 29 * result + (colorTop != null ? colorTop.hashCode() : 0);
result = 29 * result + (colorLeft != null ? colorLeft.hashCode() : 0);
result = 29 * result + (colorBottom != null ? colorBottom.hashCode() : 0);
result = 29 * result + (colorRight != null ? colorRight.hashCode() : 0);
result = 29 * result + (color != null ? color.hashCode() : 0);
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -