Classes in this Package | Line Coverage | Branch Coverage | Complexity | ||||||||
CharacterInsertion |
|
| 1.8;1,8 |
1 | /** |
|
2 | * Redistribution and use in source and binary forms, with or without |
|
3 | * modification, are permitted provided that the following conditions are |
|
4 | * met : |
|
5 | * |
|
6 | * . Redistributions of source code must retain the above copyright |
|
7 | * notice, this list of conditions and the following disclaimer. |
|
8 | * |
|
9 | * . Redistributions in binary form must reproduce the above copyright |
|
10 | * notice, this list of conditions and the following disclaimer in the |
|
11 | * documentation and/or other materials provided with the distribution. |
|
12 | * |
|
13 | * . The name of the author may not be used to endorse or promote products |
|
14 | * derived from this software without specific prior written permission. |
|
15 | * |
|
16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|
17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
19 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
|
20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
23 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|
24 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
25 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
|
27 | * |
|
28 | * $Id$ |
|
29 | */ |
|
30 | ||
31 | package palmed.edit.selection; |
|
32 | ||
33 | import palmed.edit.text.IText; |
|
34 | import palmed.edit.util.Coordinate; |
|
35 | ||
36 | /** |
|
37 | * This class implements an action to insert a character at a given position. |
|
38 | * |
|
39 | * @author Mathieu Champlon |
|
40 | * @version $Revision$ $Date$ |
|
41 | */ |
|
42 | public final class CharacterInsertion extends AbstractAction |
|
43 | { |
|
44 | /** |
|
45 | * The selection. |
|
46 | */ |
|
47 | private final ISelection selection_; |
|
48 | /** |
|
49 | * The text. |
|
50 | */ |
|
51 | private final IText text_; |
|
52 | /** |
|
53 | * The character to insert. |
|
54 | */ |
|
55 | private final char value_; |
|
56 | /** |
|
57 | * The insertion position. |
|
58 | */ |
|
59 | private final Coordinate insertion_; |
|
60 | /** |
|
61 | * The total number of inserted characters. |
|
62 | */ |
|
63 | private int count_; |
|
64 | ||
65 | /** |
|
66 | * Create a type action. |
|
67 | * |
|
68 | * @param selection the selection |
|
69 | * @param text the text |
|
70 | * @param value the character to insert |
|
71 | * @param insertion the insertion position |
|
72 | */ |
|
73 | public CharacterInsertion( final ISelection selection, final IText text, final char value, |
|
74 | final Coordinate insertion ) |
|
75 | { |
|
76 | 385 | super( selection ); |
77 | 385 | selection_ = selection; |
78 | 385 | text_ = text; |
79 | 385 | value_ = value; |
80 | 385 | insertion_ = insertion; |
81 | 385 | count_ = 1; |
82 | 385 | } |
83 | ||
84 | /** |
|
85 | * {@inheritDoc} |
|
86 | */ |
|
87 | protected void onPerform() |
|
88 | { |
|
89 | 385 | text_.insert( insertion_, value_ ); |
90 | 385 | selection_.forward(); |
91 | 385 | selection_.clear(); |
92 | 385 | } |
93 | ||
94 | /** |
|
95 | * {@inheritDoc} |
|
96 | */ |
|
97 | protected boolean onUndo() |
|
98 | { |
|
99 | 70 | for( int i = 0; i < count_; ++i ) |
100 | 45 | selection_.backward(); |
101 | 25 | backupData(); |
102 | 25 | selection_.delete(); |
103 | 25 | return false; |
104 | } |
|
105 | ||
106 | /** |
|
107 | * {@inheritDoc} |
|
108 | */ |
|
109 | protected boolean onRedo() |
|
110 | { |
|
111 | 10 | restoreData(); |
112 | 30 | for( int i = 0; i < count_; ++i ) |
113 | 20 | selection_.forward(); |
114 | 10 | return false; |
115 | } |
|
116 | ||
117 | /** |
|
118 | * {@inheritDoc} |
|
119 | */ |
|
120 | public boolean merge( final IAction action ) |
|
121 | { |
|
122 | 275 | if( !(action instanceof CharacterInsertion) ) |
123 | 50 | return false; |
124 | 225 | overwriteFinalState( (CharacterInsertion)action ); |
125 | 225 | ++count_; |
126 | 225 | return true; |
127 | } |
|
128 | } |