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 java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.NoSuchElementException;
24
25 import org.vectomatic.client.rep.events.IShapeSelectionListener;
26 import org.vectomatic.common.events.IDrawingModelListener;
27 import org.vectomatic.common.model.Attribute;
28 import org.vectomatic.common.model.CloneShapeVisitor;
29 import org.vectomatic.common.model.DrawingModel;
30 import org.vectomatic.common.model.FloatAttributeValue;
31 import org.vectomatic.common.model.IAttributeValue;
32 import org.vectomatic.common.model.Shape;
33 import org.vectomatic.common.model.geometry.ShapeGroup;
34
35
36
37
38
39
40
41 public class ShapeSelection implements IDrawingModelListener {
42 private Shape _rootShape;
43 private List<Shape> _selectedShapes;
44 private DrawingModel _model;
45 private CloneShapeVisitor _cloner;
46 private List<IShapeSelectionListener> _shapeSelectionListeners;
47 private int _current;
48
49 public ShapeSelection(DrawingModel model) {
50 _selectedShapes = new ArrayList<Shape>();
51 _model = model;
52 _model.addDrawingModelListener(this);
53 _cloner = new CloneShapeVisitor();
54 }
55
56 public void addShapeSelectionListener(IShapeSelectionListener listener) {
57 if (_shapeSelectionListeners == null) {
58 _shapeSelectionListeners = new ArrayList<IShapeSelectionListener>();
59 }
60 _shapeSelectionListeners.add(listener);
61 }
62
63 public void removeShapeSelectionListener(IShapeSelectionListener listener) {
64 if (_shapeSelectionListeners != null) {
65 _shapeSelectionListeners.remove(listener);
66 }
67 }
68
69 public void fireSelectionHasChanged() {
70 if (_shapeSelectionListeners != null) {
71 for (int i = 0, size = _shapeSelectionListeners.size(); i < size; i++) {
72 IShapeSelectionListener listener = _shapeSelectionListeners.get(i);
73 listener.selectionChanged(this);
74 }
75 }
76 }
77
78
79
80
81
82
83
84 public Shape getRootShape() {
85 return _rootShape;
86 }
87
88
89
90
91
92 public List<Shape> getSelectedShapes() {
93 return _selectedShapes;
94 }
95
96
97
98
99 public Shape select(List<Shape> shapes) {
100 _rootShape = null;
101 _selectedShapes.clear();
102
103
104 if (shapes.size() > 1) {
105
106 List<Shape> shapeClones = new ArrayList<Shape>();
107 for (int i = 0, size = shapes.size(); i < size; i++) {
108 Shape shape = shapes.get(i);
109 _selectedShapes.add(shape);
110 shape.acceptVisitor(_cloner);
111 shapeClones.add(_cloner.getClone());
112 }
113 _rootShape = new ShapeGroup(shapeClones);
114 } else if (shapes.size() > 0) {
115
116 Shape shape = shapes.get(0);
117 _selectedShapes.add(shape);
118 shape.acceptVisitor(_cloner);
119 _rootShape = _cloner.getClone();
120 }
121 if (_rootShape != null) {
122 _rootShape.setAttribute(Attribute.FILL_OPACITY, new FloatAttributeValue(0.2f));
123 }
124 fireSelectionHasChanged();
125 return _rootShape;
126 }
127
128
129
130
131
132 public void modelHasChanged(DrawingModel model) {
133 List<Shape> newSelectedShapes = new ArrayList<Shape>();
134 for (int i = 0, size = _selectedShapes.size(); i < size; i++) {
135 Shape shape = _selectedShapes.get(i);
136 if (_model.contains(shape)) {
137 newSelectedShapes.add(shape);
138 }
139 }
140 if (!newSelectedShapes.equals(_selectedShapes)) {
141 select(newSelectedShapes);
142 } else {
143
144
145 if (_selectedShapes.size() > 1) {
146 List<Shape> shapes = ((ShapeGroup)_rootShape).getShapes();
147 for (int i = 0, size = shapes.size(); i < size; i++) {
148 shapes.get(i).copyAttributes(_selectedShapes.get(i));
149 }
150 _rootShape.setAttribute(Attribute.FILL_OPACITY, new FloatAttributeValue(0.2f));
151 } else if (_selectedShapes.size() == 1) {
152 _rootShape.copyAttributes(_selectedShapes.get(0));
153 _rootShape.setAttribute(Attribute.FILL_OPACITY, new FloatAttributeValue(0.2f));
154 }
155 }
156 }
157
158 public Iterator<Shape> iterator() {
159 _current = 0;
160 return new Iterator<Shape>() {
161
162 public boolean hasNext() {
163 return _current < _selectedShapes.size();
164 }
165
166 public Shape next() {
167 if (_current >= _selectedShapes.size()) {
168 throw new NoSuchElementException();
169 }
170 return _selectedShapes.get(_current++);
171 }
172
173 public void remove() {
174 throw new UnsupportedOperationException();
175 }
176 };
177 }
178
179 public boolean hasAttributeChanged(Attribute attribute, IAttributeValue value) {
180 boolean changed = false;
181 for (int i = 0, size = _selectedShapes.size(); i < size; i++) {
182 Shape shape = _selectedShapes.get(i);
183 IAttributeValue attributeValue = shape.getAttribute(attribute);
184 if (!value.equals(attributeValue)) {
185 changed = true;
186 break;
187 }
188 }
189 return changed;
190 }
191 }