Debian Codegraph @

src/org/stathissideris/ascii2image/graphics/DiagramShape.java

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * DiTAA - Diagrams Through Ascii Art
3 *
4 * Copyright (C) 2004 Efstathios Sideris
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21 package org.stathissideris.ascii2image.graphics;
22
23 import java.awt.Color;
24 import java.awt.Rectangle;
25 import java.awt.geom.Ellipse2D;
26 import java.awt.geom.GeneralPath;
27 import java.awt.geom.Rectangle2D;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.Iterator;
31
32 import org.stathissideris.ascii2image.text.TextGrid;
33
34 /**
35 *
36 * @author Efstathios Sideris
37 */
38 public class DiagramShape extends DiagramComponent {
39
40 private static final boolean DEBUG = false;
41
42 public static final int TYPE_SIMPLE = 0;
43 public static final int TYPE_ARROWHEAD = 1;
44 public static final int TYPE_POINT_MARKER = 2;
45 public static final int TYPE_DOCUMENT = 3;
46 public static final int TYPE_STORAGE = 4;
47 public static final int TYPE_IO = 5;
48 public static final int TYPE_DECISION = 6;
49 public static final int TYPE_MANUAL_OPERATION = 7; // upside-down trapezoid
50 public static final int TYPE_TRAPEZOID = 8; // rightside-up trapezoid
51 public static final int TYPE_ELLIPSE = 9;
52 public static final int TYPE_CUSTOM = 9999;
53
54 protected int type = TYPE_SIMPLE;
55
56 private Color fillColor = null;
57 private Color strokeColor = Color.black;
58
59 private boolean isClosed = false;
60 private boolean isStrokeDashed = false;
61
62 protected ArrayList points = new ArrayList();
63
64 CustomShapeDefinition definition = null;
65
66 public static void main(String[] args) {
67 }
68
69 public static DiagramShape createArrowhead(TextGrid grid, TextGrid.Cell cell, int cellXSize, int cellYSize) {
70 if(!grid.isArrowhead(cell)) return null;
71 if(grid.isNorthArrowhead(cell)) return createNorthArrowhead(grid, cell, cellXSize, cellYSize);
72 if(grid.isSouthArrowhead(cell)) return createSouthArrowhead(grid, cell, cellXSize, cellYSize);
73 if(grid.isWestArrowhead(cell)) return createWestArrowhead(grid, cell, cellXSize, cellYSize);
74 if(grid.isEastArrowhead(cell)) return createEastArrowhead(grid, cell, cellXSize, cellYSize);
75 return null;
76 }
77
78 private static DiagramShape createNorthArrowhead(TextGrid grid, TextGrid.Cell cell, int cellXSize, int cellYSize) {
79 if(!grid.isNorthArrowhead(cell)) return null;
80 DiagramShape shape = new DiagramShape();
81 shape.addToPoints(new ShapePoint(
82 Diagram.getCellMidX(cell,cellXSize),
83 Diagram.getCellMinY(cell,cellYSize)));
84 shape.addToPoints(new ShapePoint(
85 Diagram.getCellMinX(cell,cellXSize),
86 Diagram.getCellMaxY(cell,cellYSize)));
87 shape.addToPoints(new ShapePoint(
88 Diagram.getCellMaxX(cell,cellXSize),
89 Diagram.getCellMaxY(cell,cellYSize)));
90 shape.setClosed(true);
91 shape.setFillColor(Color.black);
92 shape.setStrokeColor(Color.black);
93 shape.setType(TYPE_ARROWHEAD);
94 return shape;
95 }
96
97 private static DiagramShape createSouthArrowhead(TextGrid grid, TextGrid.Cell cell, int cellXSize, int cellYSize) {
98 if(!grid.isSouthArrowhead(cell)) return null;
99 DiagramShape shape = new DiagramShape();
100 shape.addToPoints(new ShapePoint(
101 Diagram.getCellMinX(cell,cellXSize),
102 Diagram.getCellMinY(cell,cellYSize)));
103 shape.addToPoints(new ShapePoint(
104 Diagram.getCellMidX(cell,cellXSize),
105 Diagram.getCellMaxY(cell,cellYSize)));
106 shape.addToPoints(new ShapePoint(
107 Diagram.getCellMaxX(cell,cellXSize),
108 Diagram.getCellMinY(cell,cellYSize)));
109 shape.setClosed(true);
110 shape.setFillColor(Color.black);
111 shape.setStrokeColor(Color.black);
112 shape.setType(TYPE_ARROWHEAD);
113 return shape;
114 }
115
116 private static DiagramShape createWestArrowhead(TextGrid grid, TextGrid.Cell cell, int cellXSize, int cellYSize) {
117 if(!grid.isWestArrowhead(cell)) return null;
118 DiagramShape shape = new DiagramShape();
119 shape.addToPoints(new ShapePoint(
120 Diagram.getCellMaxX(cell,cellXSize),
121 Diagram.getCellMinY(cell,cellYSize)));
122 shape.addToPoints(new ShapePoint(
123 Diagram.getCellMinX(cell,cellXSize),
124 Diagram.getCellMidY(cell,cellYSize)));
125 shape.addToPoints(new ShapePoint(
126 Diagram.getCellMaxX(cell,cellXSize),
127 Diagram.getCellMaxY(cell,cellYSize)));
128 shape.setClosed(true);
129 shape.setFillColor(Color.black);
130 shape.setStrokeColor(Color.black);
131 shape.setType(TYPE_ARROWHEAD);
132 return shape;
133 }
134
135 private static DiagramShape createEastArrowhead(TextGrid grid, TextGrid.Cell cell, int cellXSize, int cellYSize) {
136 if(!grid.isEastArrowhead(cell)) return null;
137 DiagramShape shape = new DiagramShape();
138 shape.addToPoints(new ShapePoint(
139 Diagram.getCellMinX(cell,cellXSize),
140 Diagram.getCellMinY(cell,cellYSize)));
141 shape.addToPoints(new ShapePoint(
142 Diagram.getCellMaxX(cell,cellXSize),
143 Diagram.getCellMidY(cell,cellYSize)));
144 shape.addToPoints(new ShapePoint(
145 Diagram.getCellMinX(cell,cellXSize),
146 Diagram.getCellMaxY(cell,cellYSize)));
147 shape.setClosed(true);
148 shape.setFillColor(Color.black);
149 shape.setStrokeColor(Color.black);
150 shape.setType(TYPE_ARROWHEAD);
151 return shape;
152 }
153
154 public static DiagramShape createSmallLine(TextGrid grid, TextGrid.Cell cell, int cellXSize, int cellYSize) {
155 if (grid.isLine(cell)) {
156 DiagramShape shape = new DiagramShape();
157 if (grid.isHorizontalLine(cell)) {
158 shape.addToPoints(
159 new ShapePoint(
160 cell.x * cellXSize,
161 cell.y * cellYSize + cellYSize / 2));
162 shape.addToPoints(
163 new ShapePoint(
164 cell.x * cellXSize + cellXSize - 1,
165 cell.y * cellYSize + cellYSize / 2));
166 } else if (grid.isVerticalLine(cell)) {
167 shape.addToPoints(
168 new ShapePoint(
169 cell.x * cellXSize + cellXSize / 2,
170 cell.y * cellYSize));
171 shape.addToPoints(
172 new ShapePoint(
173 cell.x * cellXSize + cellXSize / 2,
174 cell.y * cellYSize + cellYSize - 1));
175 }
176
177 //the -1 above, make a difference: the second point
178 //should not fall into the next cell, because this
179 //results in a failure of a proper end-of-line
180 //plotting correction
181 return shape;
182 }
183 return null;
184 }
185
186 public void addToPoints(ShapePoint point){
187 points.add(point);
188 }
189
190 public Iterator getPointsIterator(){
191 return points.iterator();
192 }
193
194 public void scale(float factor){
195 Iterator it = getPointsIterator();
196 while(it.hasNext()){
197 ShapePoint point = (ShapePoint) it.next();
198 point.x *= factor;
199 point.y *= factor;
200 }
201 }
202
203 public boolean isEmpty(){
204 return points.isEmpty();
205 }
206
207 public boolean isFilled(){
208 return (fillColor != null);
209 }
210
211 public void setIsNotFilled(){
212 fillColor = null;
213 }
214
215 public boolean isPointLinesEnd(ShapePoint point){
216 if(isClosed()) return false; //no line-ends in closed shapes!
217 if(point == points.get(0)) return true;
218 if(point == points.get(points.size() - 1)) return true;
219 return false;
220 }
221
222 //TODO: method in development: isRectangle()
223 public boolean isRectangle(){
224 if(points.size() != 4) return false;
225 ShapePoint p1 = (ShapePoint) points.get(0);
226 ShapePoint p2 = (ShapePoint) points.get(1);
227 ShapePoint p3 = (ShapePoint) points.get(2);
228 ShapePoint p4 = (ShapePoint) points.get(3);
229 if(p1.isInLineWith(p2)
230 && p2.isInLineWith(p3)
231 && p3.isInLineWith(p4)
232 && p4.isInLineWith(p1)) return true;
233 return false;
234 }
235
236 /**
237 * Crude way to determine which of the two shapes is smaller,
238 * based just on their bounding boxes. Used in markup
239 * assignment precendence.
240 *
241 * @param other
242 * @return
243 */
244 public boolean isSmallerThan(DiagramShape other){
245 Rectangle bounds = getBounds();
246 Rectangle otherBounds = other.getBounds();
247
248 int area = bounds.height * bounds.width;
249 int otherArea = otherBounds.height * otherBounds.width;
250
251 if(area < otherArea) {
252 return true;
253 }
254 return false;
255 }
256
257 /**
258 * @return
259 */
260 public Color getFillColor() {
261 return fillColor;
262 }
263
264 /**
265 * @return
266 */
267 public Color getStrokeColor() {
268 return strokeColor;
269 }
270
271 /**
272 * @param color
273 */
274 public void setFillColor(Color color) {
275 fillColor = color;
276 }
277
278 /**
279 * @param color
280 */
281 public void setStrokeColor(Color color) {
282 strokeColor = color;
283 }
284
285 /**
286 * @return
287 */
288 public boolean isClosed() {
289 return isClosed;
290 }
291
292 /**
293 * @param b
294 */
295 public void setClosed(boolean b) {
296 isClosed = b;
297 }
298
299 public void printDebug(){
300 System.out.print("DiagramShape: ");
301 System.out.println(points.size()+" points");
302 }
303
304 /**
305 * @return
306 */
307 public ArrayList getPoints() {
308 return points;
309 }
310
311 public ShapePoint getPoint(int i) {
312 return (ShapePoint) points.get(i);
313 }
314
315 public void setPoint(int i, ShapePoint point) {
316 points.set(i, point);
317 }
318
319
320 public boolean equals(Object object){
321 DiagramShape shape = null;
322 if(!(object instanceof DiagramShape)) { return false; }
323 else shape = (DiagramShape) object;
324 if(getPoints().size() != shape.getPoints().size()) return false;
325
326 if(DEBUG) System.out.println("comparing shapes:");
327
328 if(DEBUG) System.out.println("points1: ");
329 HashMap points1 = new HashMap();
330 Iterator it = getPointsIterator();
331 while(it.hasNext()){
332 ShapePoint point = (ShapePoint) it.next();
333 points1.put( ""+((int) point.x)+","+((int) point.y), null);
334 if(DEBUG) System.out.println(((int) point.x)+", "+((int) point.y));
335 }
336
337 if(DEBUG) System.out.println("points2: ");
338 HashMap points2 = new HashMap();
339 it = shape.getPointsIterator();
340 while(it.hasNext()){
341 ShapePoint point = (ShapePoint) it.next();
342 points2.put( ""+((int) point.x)+","+((int) point.y), null);
343 if(DEBUG) System.out.println(((int) point.x)+", "+((int) point.y));
344 }
345
346 it = points1.keySet().iterator();
347 while(it.hasNext()){
348 String key = (String) it.next();
349 if(!points2.containsKey(key)) {
350 if (DEBUG)
351 System.out.println("\tare not equal");
352 return false;
353 }
354 }
355 if (DEBUG)
356 System.out.println("\tare equal");
357 return true;
358 }
359
360 public GeneralPath makeIntoPath() {
361 int size = getPoints().size();
362
363 if(size < 2) return null;
364
365 GeneralPath path = new GeneralPath();
366 ShapePoint point = (ShapePoint) getPoints().get(0);
367 path.moveTo((int) point.x, (int) point.y);
368 for(int i = 1; i < size; i++){
369 point = (ShapePoint) getPoints().get(i);
370 path.lineTo((int) point.x, (int) point.y);
371 }
372 if(isClosed() && size > 2){
373 path.closePath();
374 }
375 return path;
376 }
377
378 public GeneralPath makeMarkerPath(Diagram diagram){
379 if(points.size() != 1) return null;
380 ShapePoint center = (ShapePoint) this.getPoint(0);
381 float diameter =
382 (float) 0.7 * Math.min(diagram.getCellWidth(), diagram.getCellHeight());
383 return new GeneralPath(new Ellipse2D.Float(
384 center.x - diameter/2,
385 center.y - diameter/2,
386 diameter,
387 diameter));
388 }
389
390 public Rectangle getBounds(){
391 Rectangle bounds = makeIntoPath().getBounds();
392 return bounds;
393 }
394
395 public GeneralPath makeIntoRenderPath(Diagram diagram) {
396 int size = getPoints().size();
397
398 if(getType() == TYPE_POINT_MARKER){
399 return makeMarkerPath(diagram);
400 }
401
402 if(getType() == TYPE_DOCUMENT && points.size() == 4){
403 return makeDocumentPath(diagram);
404 }
405
406 if(getType() == TYPE_STORAGE && points.size() == 4){
407 return makeStoragePath(diagram);
408 }
409
410 if(getType() == TYPE_IO && points.size() == 4){
411 return makeIOPath(diagram);
412 }
413
414 if(getType() == TYPE_DECISION && points.size() == 4){
415 return makeDecisionPath(diagram);
416 }
417
418 if(getType() == TYPE_MANUAL_OPERATION && points.size() == 4){
419 return makeTrapezoidPath(diagram, true);
420 }
421
422 if(getType() == TYPE_TRAPEZOID && points.size() == 4){
423 return makeTrapezoidPath(diagram, false);
424 }
425
426 if(getType() == TYPE_ELLIPSE && points.size() == 4){
427 return makeEllipsePath(diagram);
428 }
429
430 if(size < 2) return null;
431
432 GeneralPath path = new GeneralPath();
433 ShapePoint point = (ShapePoint) getPoints().get(0);
434 TextGrid.Cell cell = diagram.getCellFor(point);
435 //path.moveTo((int) point.x, (int) point.y);
436 ShapePoint previous = (ShapePoint) getPoints().get(size - 1);
437 ShapePoint next = (ShapePoint) getPoints().get(1);
438 ShapePoint entryPoint;
439 ShapePoint exitPoint;
440
441 if(point.getType() == ShapePoint.TYPE_NORMAL){
442 //if(isClosed()){
443 path.moveTo((int) point.x, (int) point.y);
444 /*} else {
445 ShapePoint projectionPoint = getCellEdgeProjectionPointBetween(point, next, diagram);
446 path.moveTo((int) projectionPoint.x, (int) projectionPoint.y);
447 }*/
448 } else if(point.getType() == ShapePoint.TYPE_ROUND){
449 entryPoint = getCellEdgePointBetween(point, previous, diagram);
450 exitPoint = getCellEdgePointBetween(point, next, diagram);
451 path.moveTo(entryPoint.x, entryPoint.y);
452 path.quadTo(point.x, point.y, exitPoint.x, exitPoint.y);
453 }
454
455 for(int i = 1; i < size; i++){
456 previous = point;
457 point = (ShapePoint) getPoints().get(i);
458 if(i < size - 1)
459 next = (ShapePoint) getPoints().get(i + 1);
460 else next = (ShapePoint) getPoints().get(0);
461
462 cell = diagram.getCellFor(point);
463
464 if(point.getType() == ShapePoint.TYPE_NORMAL)
465 //if(!isPointLinesEnd(point))
466 path.lineTo((int) point.x, (int) point.y);
467 /*else { //it is line's end, so we plot it at the projected intersection of the line with the cell's edge
468 ShapePoint projectionPoint = getCellEdgeProjectionPointBetween(point, previous, diagram);
469 path.lineTo((int) projectionPoint.x, (int) projectionPoint.y);
470 }*/
471 else if(point.getType() == ShapePoint.TYPE_ROUND){
472 entryPoint = getCellEdgePointBetween(point, previous, diagram);
473 exitPoint = getCellEdgePointBetween(point, next, diagram);
474
475 path.lineTo(entryPoint.x, entryPoint.y);
476 path.quadTo(point.x, point.y, exitPoint.x, exitPoint.y);
477 //if(!isPointLinesEnd(next)){
478 if(next.getType() == ShapePoint.TYPE_NORMAL)
479 path.lineTo(next.x, next.y);
480 else if(next.getType() == ShapePoint.TYPE_ROUND){
481 entryPoint = getCellEdgePointBetween(next, point, diagram);
482 path.lineTo(entryPoint.x, entryPoint.y);
483 }
484 /*} else {
485 entryPoint = getCellEdgeProjectionPointBetween(next, point, diagram);
486 path.lineTo(entryPoint.x, entryPoint.y);
487 }*/
488 }
489 }
490 //TODO: this shouldn't be needed, but it is!
491 if(isClosed() && size > 2){
492 path.closePath();
493 }
494 return path;
495 }
496
497 public ArrayList getEdges(){
498 ArrayList edges = new ArrayList();
499 if(this.points.size() == 1) return edges;
500 int noOfPoints = points.size();
501 for(int i = 0; i < noOfPoints - 1; i++){
502 ShapePoint startPoint = (ShapePoint) points.get(i);
503 ShapePoint endPoint = (ShapePoint) points.get(i + 1);
504 ShapeEdge edge = new ShapeEdge(startPoint, endPoint, this);
505 edges.add(edge);
506 }
507 //if it is closed return edge that connects the
508 //last point to the first
509 if(this.isClosed()){
510 ShapePoint firstPoint = (ShapePoint) points.get(0);
511 ShapePoint lastPoint = (ShapePoint) points.get(points.size() - 1);
512 ShapeEdge edge = new ShapeEdge(lastPoint, firstPoint, this);
513 edges.add(edge);
514 }
515 return edges;
516 }
517
518 /**
519 * Finds the point that represents the intersection between the cell edge
520 * that contains pointInCell and the line connecting pointInCell and
521 * otherPoint.
522 *
523 * Returns C, if A is point in cell and B is otherPoint:
524 * <pre>
525 * Cell
526 * +-----+
527 * | A |C B
528 * | *--*------------------*
529 * | |
530 * +-----+
531 *</pre>
532 *
533 * @param pointInCell
534 * @param otherPoint
535 * @return
536 */
537 public ShapePoint getCellEdgePointBetween(ShapePoint pointInCell, ShapePoint otherPoint, Diagram diagram){
538 if(pointInCell == null || otherPoint == null || diagram == null)
539 throw new IllegalArgumentException("None of the parameters can be null");
540 if(pointInCell.equals(otherPoint))
541 throw new IllegalArgumentException("The two points cannot be the same");
542
543 ShapePoint result = null;
544 TextGrid.Cell cell = diagram.getCellFor(pointInCell);
545
546 if(cell == null)
547 throw new RuntimeException("Upexpected error, cannot find cell corresponding to point "+pointInCell+" for diagram "+diagram);
548
549 if(otherPoint.isNorthOf(pointInCell))
550 result = new ShapePoint(pointInCell.x,
551 diagram.getCellMinY(cell));
552 else if(otherPoint.isSouthOf(pointInCell))
553 result = new ShapePoint(pointInCell.x,
554 diagram.getCellMaxY(cell));
555 else if(otherPoint.isWestOf(pointInCell))
556 result = new ShapePoint(diagram.getCellMinX(cell),
557 pointInCell.y);
558 else if(otherPoint.isEastOf(pointInCell))
559 result = new ShapePoint(diagram.getCellMaxX(cell),
560 pointInCell.y);
561
562 if(result == null)
563 throw new RuntimeException("Upexpected error, cannot find cell edge point for points "+pointInCell+" and "+otherPoint+" for diagram "+diagram);
564
565
566 return result;
567 }
568
569
570 /**
571 *
572 * Returns C, if A is point in cell and B is otherPoint:
573 *
574 * <pre>
575 * Cell
576 * +-----+
577 * | A | B
578 * C *--*--+------------------*
579 * | |
580 * +-----+
581 * </pre>
582 *
583 * @param pointInCell
584 * @param otherPoint
585 * @param diagram
586 * @return
587 */
588
589 public ShapePoint getCellEdgeProjectionPointBetween(ShapePoint pointInCell, ShapePoint otherPoint, Diagram diagram){
590 if(pointInCell == null || otherPoint == null || diagram == null)
591 throw new IllegalArgumentException("None of the parameters can be null");
592 if(pointInCell.equals(otherPoint))
593 throw new IllegalArgumentException("The two points cannot be the same: "+pointInCell+" and "+otherPoint+" passed");
594
595 ShapePoint result = null;
596 TextGrid.Cell cell = diagram.getCellFor(pointInCell);
597
598 if(cell == null)
599 throw new RuntimeException("Upexpected error, cannot find cell corresponding to point "+pointInCell+" for diagram "+diagram);
600
601 if(otherPoint.isNorthOf(pointInCell))
602 result = new ShapePoint(pointInCell.x,
603 diagram.getCellMaxY(cell));
604 else if(otherPoint.isSouthOf(pointInCell))
605 result = new ShapePoint(pointInCell.x,
606 diagram.getCellMinY(cell));
607 else if(otherPoint.isWestOf(pointInCell))
608 result = new ShapePoint(diagram.getCellMaxX(cell),
609 pointInCell.y);
610 else if(otherPoint.isEastOf(pointInCell))
611 result = new ShapePoint(diagram.getCellMinX(cell),
612 pointInCell.y);
613
614 if(result == null)
615 throw new RuntimeException("Upexpected error, cannot find cell edge point for points "+pointInCell+" and "+otherPoint+" for diagram "+diagram);
616
617
618 return result;
619 }
620
621 public boolean contains(ShapePoint point){
622 GeneralPath path = makeIntoPath();
623 if(path != null) return path.contains(point);
624 return false;
625 }
626
627 public boolean contains(Rectangle2D rect){
628 GeneralPath path = makeIntoPath();
629 if(path != null) return path.contains(rect);
630 return false;
631 }
632
633 public boolean intersects(Rectangle2D rect){
634 GeneralPath path = makeIntoPath();
635 if(path != null) return path.intersects(rect);
636 return false;
637 }
638
639 public boolean dropsShadow(){
640 return (isClosed()
641 && getType() != DiagramShape.TYPE_ARROWHEAD
642 && getType() != DiagramShape.TYPE_POINT_MARKER
643 && !isStrokeDashed());
644 }
645
646 /**
647 * @return
648 */
649 public int getType() {
650 return type;
651 }
652
653 /**
654 * @param i
655 */
656 public void setType(int i) {
657 type = i;
658 }
659
660 public void moveEndsToCellEdges(TextGrid grid, Diagram diagram){
661 if(isClosed()) return;
662
663 ShapePoint linesEnd = (ShapePoint) points.get(0);
664 ShapePoint nextPoint = (ShapePoint) points.get(1);
665
666 ShapePoint projectionPoint = getCellEdgeProjectionPointBetween(linesEnd, nextPoint, diagram);
667
668 linesEnd.moveTo(projectionPoint);
669
670 linesEnd = (ShapePoint) points.get(points.size() - 1);
671 nextPoint = (ShapePoint) points.get(points.size() - 2);
672
673 projectionPoint = getCellEdgeProjectionPointBetween(linesEnd, nextPoint, diagram);
674
675 linesEnd.moveTo(projectionPoint);
676 }
677
678 public void connectEndsToAnchors(TextGrid grid, Diagram diagram){
679 if(isClosed()) return;
680
681 ShapePoint linesEnd;
682 ShapePoint nextPoint;
683
684 linesEnd = (ShapePoint) points.get(0);
685 nextPoint = (ShapePoint) points.get(1);
686
687 connectEndToAnchors(grid, diagram, nextPoint, linesEnd);
688
689 linesEnd = (ShapePoint) points.get(points.size() - 1);
690 nextPoint = (ShapePoint) points.get(points.size() - 2);
691
692 connectEndToAnchors(grid, diagram, nextPoint, linesEnd);
693
694 }
695
696
697 //TODO: improve connect Ends To Arrowheads to take direction into account
698 private void connectEndToAnchors(
699 TextGrid grid,
700 Diagram diagram,
701 ShapePoint nextPoint,
702 ShapePoint linesEnd){
703
704 if(isClosed()) return;
705
706 TextGrid.Cell anchorCell;
707 anchorCell = getPossibleAnchorCell(linesEnd, nextPoint, diagram);
708
709 if(grid.isArrowhead(anchorCell)){
710 linesEnd.x = diagram.getCellMidX(anchorCell);
711 linesEnd.y = diagram.getCellMidY(anchorCell);
712 linesEnd.setLocked(true);
713 } else if (grid.isCorner(anchorCell) || grid.isIntersection(anchorCell)){
714 linesEnd.x = diagram.getCellMidX(anchorCell);
715 linesEnd.y = diagram.getCellMidY(anchorCell);
716 linesEnd.setLocked(true);
717 }
718 }
719
720 /**
721 * Given the end of a line, the next point and a Diagram, it
722 * returns the cell that may contain intersections or arrowheads
723 * to which the line's end should be connected
724 *
725 * @param linesEnd
726 * @param nextPoint
727 * @param diagram
728 * @return
729 */
730 private static TextGrid.Cell getPossibleAnchorCell(
731 ShapePoint linesEnd,
732 ShapePoint nextPoint,
733 Diagram diagram
734 ){
735 ShapePoint cellPoint = null;
736
737 if(nextPoint.isNorthOf(linesEnd))
738 cellPoint = new ShapePoint(linesEnd.x, linesEnd.y + diagram.getCellHeight());
739 if(nextPoint.isSouthOf(linesEnd))
740 cellPoint = new ShapePoint(linesEnd.x, linesEnd.y - diagram.getCellHeight());
741 if(nextPoint.isWestOf(linesEnd))
742 cellPoint = new ShapePoint(linesEnd.x + diagram.getCellWidth(), linesEnd.y);
743 if(nextPoint.isEastOf(linesEnd))
744 cellPoint = new ShapePoint(linesEnd.x - diagram.getCellWidth(), linesEnd.y);
745
746 return diagram.getCellFor(cellPoint);
747 }
748
749
750 public String toString(){
751 String s = "DiagramShape, "+points.size()+" points: ";
752 Iterator it = getPointsIterator();
753 while(it.hasNext()){
754 ShapePoint point = (ShapePoint) it.next();
755 s += point;
756 if(it.hasNext()) s += " ";
757 }
758 return s;
759 }
760
761 /**
762 * @return
763 */
764 public boolean isStrokeDashed() {
765 return isStrokeDashed;
766 }
767
768 /**
769 * @param b
770 */
771 public void setStrokeDashed(boolean b) {
772 isStrokeDashed = b;
773 }
774
775 private GeneralPath makeStoragePath(Diagram diagram) {
776 if(points.size() != 4) return null;
777 Rectangle bounds = makeIntoPath().getBounds();
778 ShapePoint point1 = new ShapePoint((float)bounds.getMinX(), (float)bounds.getMinY());
779 ShapePoint point2 = new ShapePoint((float)bounds.getMaxX(), (float)bounds.getMinY());
780 ShapePoint point3 = new ShapePoint((float)bounds.getMaxX(), (float)bounds.getMaxY());
781 ShapePoint point4 = new ShapePoint((float)bounds.getMinX(), (float)bounds.getMaxY());
782
783 ShapePoint pointMidTop = new ShapePoint((float)bounds.getCenterX(), (float)bounds.getMinY());
784 ShapePoint pointMidBottom = new ShapePoint((float)bounds.getCenterX(), (float)bounds.getMaxY());
785
786 float diameterX = bounds.width;
787 float diameterY = 0.75f * diagram.getCellHeight();
788
789 //control point offset X, and Y
790 float cpOffsetX = bounds.width / 6;
791 float cpOffsetYTop = diagram.getCellHeight() / 2;
792 float cpOffsetYBottom = 10 * diagram.getCellHeight() / 14;
793 //float cpOffsetYBottom = cpOffsetYTop;
794
795 GeneralPath path = new GeneralPath();
796
797 //top of cylinder
798 path.moveTo(point1.x, point1.y);
799 path.curveTo(
800 point1.x + cpOffsetX, point1.y + cpOffsetYTop,
801 point2.x - cpOffsetX, point2.y + cpOffsetYTop,
802 point2.x, point2.y
803 );
804 path.curveTo(
805 point2.x - cpOffsetX, point2.y - cpOffsetYTop,
806 point1.x + cpOffsetX, point1.y - cpOffsetYTop,
807 point1.x, point1.y
808 );
809
810 //side of cylinder
811 path.moveTo(point1.x, point1.y);
812 path.lineTo(point4.x, point4.y);
813
814 path.curveTo(
815 point4.x + cpOffsetX, point4.y + cpOffsetYBottom,
816 point3.x - cpOffsetX, point3.y + cpOffsetYBottom,
817 point3.x, point3.y
818 );
819
820 path.lineTo(point2.x, point2.y);
821
822 return path;
823 }
824
825 private GeneralPath makeDocumentPath(Diagram diagram) {
826 if(points.size() != 4) return null;
827 Rectangle bounds = makeIntoPath().getBounds();
828 ShapePoint point1 = new ShapePoint((float)bounds.getMinX(), (float)bounds.getMinY());
829 ShapePoint point2 = new ShapePoint((float)bounds.getMaxX(), (float)bounds.getMinY());
830 ShapePoint point3 = new ShapePoint((float)bounds.getMaxX(), (float)bounds.getMaxY());
831 ShapePoint point4 = new ShapePoint((float)bounds.getMinX(), (float)bounds.getMaxY());
832
833 ShapePoint pointMid = new ShapePoint((float)bounds.getCenterX(), (float)bounds.getMaxY());
834
835 GeneralPath path = new GeneralPath();
836 path.moveTo(point1.x, point1.y);
837 path.lineTo(point2.x, point2.y);
838 path.lineTo(point3.x, point3.y);
839
840 //int controlDX = diagram.getCellWidth();
841 //int controlDY = diagram.getCellHeight() / 2;
842
843 int controlDX = bounds.width / 6;
844 int controlDY = bounds.height / 8;
845
846 path.quadTo(pointMid.x + controlDX, pointMid.y - controlDY, pointMid.x, pointMid.y);
847 path.quadTo(pointMid.x - controlDX, pointMid.y + controlDY, point4.x, point4.y);
848 path.closePath();
849
850 return path;
851 }
852
853 // to draw a circle with 4 Bezier curves, set the control points at this ratio of
854 // the radius above & below the side points
855 // thanks to G. Adam Stanislav, http://whizkidtech.redprince.net/bezier/circle/
856 private static final float KAPPA = 4f * ((float) Math.sqrt(2) - 1) / 3f;
857
858 private GeneralPath makeEllipsePath(Diagram diagram) {
859 if(points.size() != 4) return null;
860 Rectangle bounds = makeIntoPath().getBounds();
861 float xOff = (float) bounds.getWidth() * 0.5f * KAPPA;
862 float yOff = (float) bounds.getHeight() * 0.5f * KAPPA;
863 ShapePoint pointMid = new ShapePoint((float)bounds.getCenterX(), (float)bounds.getCenterY());
864
865 ShapePoint left = new ShapePoint((float)bounds.getMinX(), (float)pointMid.getY());
866 ShapePoint right = new ShapePoint((float)bounds.getMaxX(), (float)pointMid.getY());
867 ShapePoint top = new ShapePoint((float)pointMid.getX(), (float)bounds.getMinY());
868 ShapePoint bottom = new ShapePoint((float)pointMid.getX(), (float)bounds.getMaxY());
869
870 GeneralPath path = new GeneralPath();
871 path.moveTo(top.x, top.y);
872 path.curveTo(top.x + xOff, top.y, right.x, right.y - yOff, right.x, right.y);
873 path.curveTo(right.x, right.y + yOff, bottom.x + xOff, bottom.y, bottom.x, bottom.y);
874 path.curveTo(bottom.x - xOff, bottom.y, left.x, left.y + yOff, left.x, left.y);
875 path.curveTo(left.x, left.y - yOff, top.x - xOff, top.y, top.x, top.y);
876 path.closePath();
877
878 return path;
879 }
880
881 private GeneralPath makeTrapezoidPath(Diagram diagram, boolean inverted) {
882 if(points.size() != 4) return null;
883 Rectangle bounds = makeIntoPath().getBounds();
884 float offset = 0.7f * diagram.getCellWidth(); // fixed slope
885 if (inverted) offset = -offset;
886 ShapePoint ul = new ShapePoint((float)bounds.getMinX() + offset, (float)bounds.getMinY());
887 ShapePoint ur = new ShapePoint((float)bounds.getMaxX() - offset, (float)bounds.getMinY());
888 ShapePoint br = new ShapePoint((float)bounds.getMaxX() + offset, (float)bounds.getMaxY());
889 ShapePoint bl = new ShapePoint((float)bounds.getMinX() - offset, (float)bounds.getMaxY());
890
891 ShapePoint pointMid = new ShapePoint((float)bounds.getCenterX(), (float)bounds.getMaxY());
892
893 GeneralPath path = new GeneralPath();
894 path.moveTo(ul.x, ul.y);
895 path.lineTo(ur.x, ur.y);
896 path.lineTo(br.x, br.y);
897 path.lineTo(bl.x, bl.y);
898 path.closePath();
899
900 return path;
901 }
902
903 private GeneralPath makeDecisionPath(Diagram diagram) {
904 if(points.size() != 4) return null;
905 Rectangle bounds = makeIntoPath().getBounds();
906 ShapePoint pointMid = new ShapePoint((float)bounds.getCenterX(), (float)bounds.getCenterY());
907 ShapePoint left = new ShapePoint((float)bounds.getMinX(), (float)pointMid.getY());
908 ShapePoint right = new ShapePoint((float)bounds.getMaxX(), (float)pointMid.getY());
909 ShapePoint top = new ShapePoint((float)pointMid.getX(), (float)bounds.getMinY());
910 ShapePoint bottom = new ShapePoint((float)pointMid.getX(), (float)bounds.getMaxY());
911
912 GeneralPath path = new GeneralPath();
913 path.moveTo(left.x, left.y);
914 path.lineTo(top.x, top.y);
915 path.lineTo(right.x, right.y);
916 path.lineTo(bottom.x, bottom.y);
917
918 path.closePath();
919
920 return path;
921 }
922
923 private GeneralPath makeIOPath(Diagram diagram) {
924 if(points.size() != 4) return null;
925 Rectangle bounds = makeIntoPath().getBounds();
926 ShapePoint point1 = new ShapePoint((float)bounds.getMinX(), (float)bounds.getMinY());
927 ShapePoint point2 = new ShapePoint((float)bounds.getMaxX(), (float)bounds.getMinY());
928 ShapePoint point3 = new ShapePoint((float)bounds.getMaxX(), (float)bounds.getMaxY());
929 ShapePoint point4 = new ShapePoint((float)bounds.getMinX(), (float)bounds.getMaxY());
930
931 float offset = diagram.getCellWidth() / 2;
932
933 GeneralPath path = new GeneralPath();
934 path.moveTo(point1.x + offset, point1.y);
935 path.lineTo(point2.x + offset, point2.y);
936 path.lineTo(point3.x - offset, point3.y);
937 path.lineTo(point4.x - offset, point4.y);
938 path.closePath();
939
940 return path;
941 }
942
943 public CustomShapeDefinition getDefinition() {
944 return definition;
945 }
946
947 public void setDefinition(CustomShapeDefinition definition) {
948 this.definition = definition;
949 }
950
951 }