1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.vectomatic.client.rep.controller;
19
20 import org.vectomatic.client.rep.RepApplication;
21 import org.vectomatic.client.rep.command.ICommand;
22 import org.vectomatic.client.rep.command.NewShapeCommand;
23 import org.vectomatic.client.rep.view.Cursor;
24 import org.vectomatic.client.rep.view.DrawingView;
25 import org.vectomatic.common.model.Attribute;
26 import org.vectomatic.common.model.geometry.Point;
27 import org.vectomatic.common.model.geometry.Polyline;
28
29
30
31
32
33 public class NewPolylineController extends ControllerBase {
34 private Point[] _pts;
35 private int _count;
36 private MouseControllerButton _button;
37
38 public NewPolylineController(RepApplication app) {
39 super(app);
40 _pts = new Point[10];
41 for (int i = 0; i < _pts.length; i++) {
42 _pts[i] = new Point();
43 }
44 _button = new MouseControllerButton(_app.getIcons().polylineIcon().createImage(), _app.getConstants().newPolylineCommand(), this);
45 }
46
47 public MouseControllerButton getButton() {
48 return _button;
49 }
50
51 @Override
52 public void activate(DrawingView view) {
53 _count = 0;
54 view.setCursor(Cursor.CURSOR_CROSSHAIR);
55 }
56
57 private void addPoint(Point p) {
58 if (_count == _pts.length) {
59
60 Point[] pts = new Point[2 * _pts.length];
61 for (int i = 0; i < _pts.length; i++) {
62 pts[i] = _pts[i];
63 }
64 for (int i = _pts.length; i < pts.length; i++) {
65 pts[i] = new Point();
66 }
67 _pts = pts;
68 }
69 p.copyTo(_pts[_count]);
70 _count++;
71 }
72
73 @Override
74 public void mouseDown(DrawingView view, Point p, int modifiers) {
75 view.toModelCoordinates(p);
76 if (_count == 0) {
77 p.copyTo(_pts[0]);
78 p.copyTo(_pts[1]);
79 _count = 2;
80 } else {
81
82 addPoint(p);
83
84
85 float tol = view.convertToReferenceLength(3);
86 if (_pts[_count - 2].subtract(_pts[0], p).length() < tol) {
87
88 _pts[0].copyTo(_pts[_count - 2]);
89 deactivate(view);
90 } else if (_pts[_count - 2].subtract(_pts[_count - 3], p).length() < tol) {
91
92 _count--;
93 deactivate(view);
94 }
95 }
96 }
97
98 @Override
99 public void mouseMove(DrawingView view, Point p, int modifiers) {
100 view.toModelCoordinates(p);
101 if (_count > 1) {
102 p.copyTo(_pts[_count - 1]);
103 if (_count > 2) {
104 float tol = view.convertToReferenceLength(3);
105 if (_pts[_count - 1].subtract(_pts[0], p).length() < tol) {
106 view.setCursor(Cursor.CURSOR_CLOSED_POLYLINE);
107 } else if (_pts[_count - 1].subtract(_pts[_count - 2], p).length() < tol) {
108 view.setCursor(Cursor.CURSOR_OPEN_POLYLINE);
109 } else {
110 view.setCursor(Cursor.CURSOR_CROSSHAIR);
111 }
112 }
113 }
114 }
115
116
117 @Override
118 public void render(DrawingView view) {
119 if (_count > 0) {
120
121 _app.getLineStyleController().getStyle().acceptVisitor(view.getStrokeStyleVisitor());
122
123 view.setLineWidth(_app.getLineWidthController().getLineWidth().getValue());
124 view.moveTo(_pts[0].x, _pts[0].y);
125 for (int i = 1; i < _count; i++) {
126 view.lineTo(_pts[i].x, _pts[i].y);
127 }
128
129 view.stroke();
130 }
131 }
132
133 @Override
134 public void deactivate(DrawingView view) {
135 if (_count > 2) {
136 Polyline polyline = new Polyline(_pts, _count - 1);
137 polyline.setAttribute(Attribute.LINE_STYLE, _app.getLineStyleController().getStyle());
138 polyline.setAttribute(Attribute.LINE_OPACITY, _app.getLineStyleController().getOpacity());
139 polyline.setAttribute(Attribute.FILL_STYLE, _app.getFillStyleController().getStyle());
140 polyline.setAttribute(Attribute.FILL_OPACITY, _app.getFillStyleController().getOpacity());
141 polyline.setAttribute(Attribute.LINE_WIDTH, _app.getLineWidthController().getLineWidth());
142 ICommand command = new NewShapeCommand(_app, polyline);
143 command.execute();
144 _app.getHistory().addCommand(command);
145 activate(view);
146 }
147 }
148 }