1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.elements;
14 import java.awt.Graphics2D;
15 import java.awt.Shape;
16 import java.awt.font.TextLayout;
17 import java.awt.geom.Rectangle2D;
20 * Auxiliary class for drawing positioned and single lines of laid out text.
22 * @author Tuukka Lehtonen
29 public interface BoundsProcedure {
30 Rectangle2D getBounds(Line line);
33 public static BoundsProcedure BBOX = new BoundsProcedure() {
35 public Rectangle2D getBounds(Line line) {
40 public static BoundsProcedure ABBOX = new BoundsProcedure() {
42 public Rectangle2D getBounds(Line line) {
47 public final String document;
48 public final int startOffset;
49 public final int endOffset;
59 public Line(String document, int startOffset, int endOffset) {
60 int len = document.length();
61 if (endOffset < startOffset)
62 throw new IllegalArgumentException("endOffset (" + endOffset + ") < startOffset (" + startOffset + ")");
63 if (startOffset < 0 || startOffset > len)
64 throw new IllegalArgumentException("startOffset " + startOffset + " out of bounds [0," + len + "]");
65 if (endOffset < 0 || endOffset > len)
66 throw new IllegalArgumentException("endOffset " + endOffset + " out of bounds [0," + len + "]");
67 this.document = document;
68 this.startOffset = startOffset;
69 this.endOffset = endOffset;
72 public void render(Graphics2D g) {
74 layout.draw(g, alignedPosX, alignedPosY);
77 public String getText() {
78 return document.substring(startOffset, endOffset);
81 public boolean intersectsRange(int start, int end) {
82 return start < endOffset && end > startOffset;
85 public boolean containsRange(int start, int end) {
86 return start >= startOffset && start <= endOffset
87 && end > startOffset && end <= endOffset;
90 public boolean containsOffset(int caret) {
91 return startOffset <= caret && caret <= endOffset;
94 public Shape getLogicalHighlightShape(int start, int end) {
95 return layout.getLogicalHighlightShape(
96 Math.max(0, start - startOffset),
97 Math.min(endOffset - startOffset, end - startOffset));
100 public Shape[] getCaretShapes(int caret) {
101 return layout.getCaretShapes(caret - startOffset);
104 public void translate(Graphics2D g, float xOffset, float yOffset) {
105 g.translate(alignedPosX + xOffset, alignedPosY + yOffset);
108 public void translateInv(Graphics2D g, float xOffset, float yOffset) {
109 g.translate(-alignedPosX - xOffset, - alignedPosY - yOffset);
112 public void alignOffset(double x, double y) {
113 alignedPosX = drawPosX + (float) x;
114 alignedPosY = drawPosY + (float) y;
116 abbox = new Rectangle2D.Float();
117 abbox.setFrame(bbox.getX() + x, bbox.getY() + y, bbox.getWidth(), bbox.getHeight());
118 //System.out.println("offset(" + x + ", " + y + "): " + this);
122 public String toString() {
123 return getClass().getSimpleName() + "[startOffset="
124 + startOffset + ", endOffset=" + endOffset
125 + ", x=" + drawPosX + ", y=" + drawPosY
126 + ", ax='" + alignedPosX + ", ay=" + alignedPosY
127 + ", text='" + getText()
128 + ", bbox='" + bbox + ", abbox=" + abbox