1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.editor.textEditor.policies;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.eclipse.draw2d.ColorConstants;
18 import org.eclipse.draw2d.FigureUtilities;
19 import org.eclipse.draw2d.Graphics;
20 import org.eclipse.draw2d.IFigure;
21 import org.eclipse.draw2d.RectangleFigure;
22 import org.eclipse.draw2d.geometry.Rectangle;
23 import org.eclipse.gef.Request;
24 import org.eclipse.gef.editpolicies.SelectionEditPolicy;
25
26 import sk.uniba.euromath.editor.textEditor.ITextPieceKeeper;
27 import sk.uniba.euromath.editor.textEditor.Interval;
28 import sk.uniba.euromath.editor.textEditor.requests.RequestConstants;
29 import sk.uniba.euromath.editor.textEditor.requests.selectionRequest.TextSelectRequest;
30
31 /***
32 * Creates and cares about figures that represent selection. It cares about
33 * figures of two kinds
34 * <ul>
35 * <li> border of the selection - this figure is painted when the selections
36 * ends
37 * <li> filled selection - this figure is created when the selection starts and
38 * is justified when draging in method updateDraggingFeedback
39 * </ul>
40 *
41 * Should be installed in EditPolicy.SELECTION_FEEDBACK_ROLE
42 *
43 * @author Martin Kollar
44 */
45 abstract public class AbstractTextSelectionEditPolicy extends
46 SelectionEditPolicy {
47
48 /***
49 *
50 */
51 protected final List<IFigure> selectionFigures = new ArrayList<IFigure>();
52
53 /***
54 * Constructor.
55 */
56 public AbstractTextSelectionEditPolicy() {
57 super();
58 }
59
60 /***
61 * Creates Figure that is used when the selection is finished is used to
62 * show what is selected after selection is finished
63 *
64 * @param start
65 * @param end
66 *
67 * @return <code>RectangleFigure</code> that is painted
68 */
69 private IFigure createSelectionFigure(int start, int end) {
70 RectangleFigure result = new RectangleFigure();
71
72 if (!getHostFigure().isShowing()) {
73 getHost().getViewer().reveal(getHost());
74 }
75
76 Rectangle r = getHostKeeper().getTextLocator()
77 .getTextBounds(start, end);
78
79 if (r == null)
80 r = new Rectangle(0, 0, 0, 0);
81 result.setBounds(r);
82 result.setLineStyle(Graphics.LINE_SOLID);
83 result.setForegroundColor(ColorConstants.white);
84
85 FigureUtilities.makeGhostShape(result);
86 return result;
87 }
88
89 /***
90 * @see org.eclipse.gef.editpolicies.SelectionEditPolicy#hideSelection()
91 */
92 @Override
93 protected void hideSelection() {
94 for (IFigure figure : this.selectionFigures) {
95 removeFeedback(figure);
96 }
97 }
98
99 /***
100 *
101 * @see org.eclipse.gef.editpolicies.SelectionEditPolicy#showSelection()
102 */
103 @Override
104 protected void showSelection() {
105 for (IFigure figure : this.selectionFigures) {
106 addFeedback(figure);
107 }
108 }
109
110 /***
111 *
112 */
113 private void updateSelectionFigures() {
114 this.selectionFigures.clear();
115 for (Interval interval : getHostKeeper().getSelectionStatus()
116 .getSelectionIntervals()) {
117 this.selectionFigures.add(createSelectionFigure(interval.getStart(),
118 interval.getEnd()));
119 }
120 }
121
122 @Override
123 public void showSourceFeedback(Request request) {
124 if (!request.getType().equals(RequestConstants.TEXT_SELECT))
125 throw new IllegalArgumentException();
126 hideSelection();
127
128 updateHostSelectionState((TextSelectRequest) request);
129 updateSelectionFigures();
130 showSelection();
131 }
132
133 /***
134 * @param request
135 */
136 protected void updateHostSelectionState(TextSelectRequest request) {
137 if (request.getSelectType().equals(TextSelectRequest.NEW_SELECTION)) {
138
139
140 getHostKeeper().getSelectionStatus().setSelectionIntervals(
141 request.getSelection());
142 } else {
143
144 getHostKeeper().getSelectionStatus().xorSelectionIntervals(
145 request.getSelection());
146 }
147
148 }
149
150 /***
151 * Helper method.
152 *
153 * @return
154 */
155 protected ITextPieceKeeper getHostKeeper() {
156 return (ITextPieceKeeper) getHost();
157 }
158
159 /***
160 * @see org.eclipse.gef.EditPolicy#understandsRequest(org.eclipse.gef.Request)
161 *
162 * @return <code>true</code> if request is TextSelectionRequest else
163 * return <code>false</code>
164 */
165 @Override
166 public boolean understandsRequest(Request req) {
167 if (RequestConstants.TEXT_SELECT.equals(req.getType())) {
168 return true;
169 }
170 return false;
171 }
172
173 }