package com.drawbridge.text;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.LinkedList;
public class DBSelection
{
private final DBDocument mTextComponent;
private boolean mVisible = false;
private DBCaret mCaret;
private Color mSelectionColor;
public DBSelection(DBDocument textComponent, Color selectionColor) {
this.mTextComponent = textComponent;
this.mCaret = mTextComponent.getCaret();
this.mSelectionColor = selectionColor;
}
public void Paint(Graphics g)
{
// Draw Caret
if (mVisible)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(mSelectionColor);
if (mCaret.getDotCharPosition() != null && mCaret.getMarkCharPosition() != null) {
LinkedList<Rectangle> rectangles = getSelectionRectangles();
for (int i = 0; i < rectangles.size(); i++) {
g2.fillRect(rectangles.get(i).x, rectangles.get(i).y, rectangles.get(i).width, rectangles.get(i).height);
}
}
else {
throw new RuntimeException("Exception: DotPosition or MarkPosition is null");
}
}
}
public void setVisible(boolean visible)
{
mVisible = visible;
}
public boolean isOverSelection(int x, int y)
{
LinkedList<Rectangle> rectangles = getSelectionRectangles();
for (int i = 0; i < rectangles.size(); i++) {
if (rectangles.get(i).contains(x, y)) {
return true;
}
}
return false;
}
public LinkedList<Rectangle> getSelectionRectangles()
{
LinkedList<Rectangle> rectangles = new LinkedList<Rectangle>();
// Check the direction and swap appropriately
boolean backwards = false;
if (mCaret.getDotCharPosition().y < mCaret.getMarkCharPosition().y) {
backwards = true;
}
else if (mCaret.getDotCharPosition().y == mCaret.getMarkCharPosition().y && mCaret.getDotCharPosition().x < mCaret.getMarkCharPosition().x) {
backwards = true;
}
Point startPixel = new Point(0, 0);
Point endPixel = new Point(0, 0);
Point startChar = new Point(0, 0);
Point endChar = new Point(0, 0);
int rowHeight = mTextComponent.getLineHeight() + mTextComponent.getLineSpacing();
int lineWidth = mTextComponent.getPaddedWidth();
if (backwards) {
startChar = mCaret.getDotCharPosition();
startPixel.x = mCaret.getDotPixelPosition().x + mTextComponent.getInsets().left;
startPixel.y = mCaret.getDotPixelPosition().y + mTextComponent.getInsets().top;
endPixel.y = mCaret.getMarkPixelPosition().y + mTextComponent.getInsets().top;
endPixel.x = mCaret.getMarkPixelPosition().x + mTextComponent.getInsets().left;
endChar = mCaret.getMarkCharPosition();
}
else {
startChar = mCaret.getMarkCharPosition();
if (mCaret.getMarkPixelPosition() != null) {
startPixel.x = mCaret.getMarkPixelPosition().x + mTextComponent.getInsets().left;
startPixel.y = mCaret.getMarkPixelPosition().y + mTextComponent.getInsets().top;
}
if (mCaret.getDotPixelPosition() != null) {
endPixel.x = mCaret.getDotPixelPosition().x + mTextComponent.getInsets().left;
endPixel.y = mCaret.getDotPixelPosition().y + mTextComponent.getInsets().top;
}
endChar = mCaret.getDotCharPosition();
}
if (startChar.y == endChar.y) {
rectangles.add(new Rectangle(startPixel.x, startPixel.y, endPixel.x - startPixel.x, rowHeight));
}
else {
// Draw the rectangles
for (int i = startChar.y; i <= endChar.y; i++) {
if (i == startChar.y) { // top
rectangles.add(new Rectangle(startPixel.x, startPixel.y, lineWidth - startPixel.x + mTextComponent.getInsets().left, rowHeight));
}
else if (i == endChar.y) { // bottom
rectangles.add(new Rectangle(mTextComponent.getInsets().left, endPixel.y, endPixel.x - mTextComponent.getInsets().left, rowHeight));
}
else { // middle
rectangles.add(new Rectangle(mTextComponent.getInsets().left, mTextComponent.getInsets().top + (i * rowHeight), mTextComponent.getPaddedWidth(), rowHeight));
}
}
}
return rectangles;
}
}