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.editpolicies.SelectionEditPolicy;
24
25 import sk.uniba.euromath.editor.textEditor.ITextPieceKeeper;
26 import sk.uniba.euromath.editor.textEditor.Interval;
27
28 /***
29 * Creates and cares about figures that represent selection. It cares about
30 * figures of two kinds
31 * <ul>
32 * <li> border of the selection - this figure is painted when the selections
33 * ends
34 * <li> filled selection - this figure is created when the selection starts and
35 * is justified when draging in method updateDraggingFeedback
36 * </ul>
37 *
38 * Should be installed in EditPolicy.SELECTION_FEEDBACK_ROLE
39 *
40 * @author Martin Kollar
41 */
42 public class TextSelectionEditPolicy extends
43 SelectionEditPolicy {
44
45 /***
46 * List of displayed(shown) selection figures.
47 */
48 protected final List<IFigure> selectionFigures = new ArrayList<IFigure>();
49
50 /***
51 * Constructor.
52 */
53 public TextSelectionEditPolicy() {
54 super();
55 }
56
57 /***
58 * Creates Figure sourrounding keepers text of interval.
59 *
60 * @param start
61 * index of first character bounded by figure
62 * @param end
63 * index of last character bounded by figure
64 *
65 * @return <code>RectangleFigure</code> that is painted
66 */
67 private IFigure createSelectionFigure(int start, int end) {
68 RectangleFigure result = new RectangleFigure();
69
70 if (!getHostFigure().isShowing()) {
71 getHost().getViewer().reveal(getHost());
72 }
73
74 Rectangle r = getHostKeeper().getTextLocator().getTextBounds(
75 start, end);
76
77 if (r == null)
78 r = new Rectangle(0, 0, 0, 0);
79 result.setBounds(r);
80 result.setLineStyle(Graphics.LINE_SOLID);
81 result.setForegroundColor(ColorConstants.white);
82
83 FigureUtilities.makeGhostShape(result);
84 return result;
85 }
86
87 /***
88 * Hides selection.
89 */
90 @Override
91 protected void hideSelection() {
92 for (IFigure figure : this.selectionFigures) {
93 removeFeedback(figure);
94 }
95 }
96
97 /***
98 * Shows selection.
99 */
100 @Override
101 protected void showSelection() {
102 for (IFigure figure : this.selectionFigures) {
103 addFeedback(figure);
104 }
105 }
106
107 /***
108 * Updates selection figures according to keeper's selection status.
109 */
110 private void updateSelectionFigures() {
111 this.selectionFigures.clear();
112 for (Interval interval : getHostKeeper().getSelectionStatus()
113 .getSelectionIntervals()) {
114 this.selectionFigures
115 .add(createSelectionFigure(interval
116 .getStart(), interval
117 .getEnd()));
118 }
119 }
120
121
122 @Override
123 protected void setSelectedState(int type) {
124 hideSelection();
125 updateSelectionFigures();
126 showSelection();
127 }
128
129 /***
130 * Helper method.
131 *
132 * @return
133 */
134 protected ITextPieceKeeper getHostKeeper() {
135 return (ITextPieceKeeper) getHost();
136 }
137 }