1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.editor.textEditor;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Set;
19
20 /***
21 * Selection status for text piece.
22 * @author Tomáš Studva 24.1.2006
23 * Martin Kollar
24 */
25 public class TextPieceSelectionStatusImpl implements ITextPieceSelectionStatus {
26
27
28 /***
29 * Info about associated text piece.
30 */
31 private ITextPieceKeeper keeper;
32
33 private Set<Interval> intervals = new HashSet<Interval>();
34
35 /***
36 * Constructor. Creates text selection status for text piece.
37 *
38 * @param keeper about associated text piece
39 */
40 public TextPieceSelectionStatusImpl(ITextPieceKeeper keeper) {
41 super();
42 this.keeper = keeper;
43 }
44
45
46 /***
47 * Sets nothing selected. Clears selection.
48 */
49 public void clear(){
50 this.intervals.clear();
51 }
52
53
54 /***
55 * @return List of Intervals that are not overlaping
56 */
57 public List<Interval> getSelectionIntervals() {
58 return new ArrayList<Interval>(this.intervals);
59 }
60
61
62 /***
63 * @return <code>true</code>, if something is selected, but it is not fully selected
64 */
65 public boolean isPartlySelected() {
66 return ( (!this.intervals.isEmpty()) && (!isFullySelected()) );
67 }
68
69
70 /***
71 * @return <code>true</code>, if whole ITextPieceKeeper is selected
72 */
73 public boolean isFullySelected() {
74 if (intervals.size() > 1)
75 return false;
76 return ( (getSelectionIntervals().get(0).getStart() == 0) &&
77 (getSelectionIntervals().get(0).getEnd() == keeper.getText().length()) );
78 }
79
80
81 /***
82 * Sets what is selected in ITextPieceKeeper
83 *
84 * @param intervals Collection of intervals, that are in range text and are
85 * not overlaping
86 */
87 public void setSelectionIntervals(Collection<Interval> intervals) {
88 this.intervals.clear();
89
90
91 this.intervals.addAll(intervals);
92 }
93
94 /***
95 * Makes logical OR of interval and this.intervals. Holding property that intervlas are
96 * not overlaping.
97 * @param interval Interval to add to selection
98 */
99 public void addSelectionInterval(Interval interval) {
100 List<Interval> selIntervals = getSelectionIntervals();
101
102 for (Interval selInterval: selIntervals){
103 if (interval.intersects(selInterval)){
104 interval = Interval.ORIntervals(interval, selInterval);
105
106 getSelectionIntervals().remove(selInterval);
107 }
108 }
109 getSelectionIntervals().add(interval);
110 }
111
112 /***
113 * @see ITextPieceSelectionStatus#xorSelectionIntervals(Collection)
114 */
115 public void xorSelectionIntervals(Collection<Interval> intervals) {
116
117
118 }
119
120 /***
121 * @see ITextPieceSelectionStatus#subtractSelectionInterval(Interval)
122 */
123 public void subtractSelectionInterval(Interval interval) {
124
125
126 }
127
128
129 }