View Javadoc

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 replace the selection (if any) with a new line.
38   *
39   * @author Mathieu Champlon
40   * @version $Revision$ $Date$
41   */
42  public final class NewLineInsertion 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 insertion position.
54       */
55      private final Coordinate insertion_;
56      /***
57       * The total number of inserted characters.
58       */
59      private int count_;
60  
61      /***
62       * Create an enter action.
63       *
64       * @param selection the selection
65       * @param text the text
66       * @param insertion the insertion position
67       */
68      public NewLineInsertion( final ISelection selection, final IText text, final Coordinate insertion )
69      {
70          super( selection );
71          selection_ = selection;
72          text_ = text;
73          insertion_ = insertion;
74          count_ = 1;
75      }
76  
77      /***
78       * {@inheritDoc}
79       */
80      protected void onPerform()
81      {
82          backupData();
83          selection_.delete();
84          text_.insertNewLine( insertion_ );
85          selection_.forward();
86          selection_.clear();
87      }
88  
89      /***
90       * {@inheritDoc}
91       */
92      protected boolean onUndo()
93      {
94          for( int i = 0; i < count_; ++i )
95              selection_.backward();
96          restoreData();
97          return true;
98      }
99  
100     /***
101      * {@inheritDoc}
102      */
103     protected boolean onRedo()
104     {
105         selection_.delete();
106         for( int i = 0; i < count_; ++i )
107         {
108             text_.insertNewLine( insertion_ );
109             selection_.forward();
110         }
111         return false;
112     }
113 
114     /***
115      * {@inheritDoc}
116      */
117     public boolean merge( final IAction action )
118     {
119         if( !(action instanceof NewLineInsertion) )
120             return false;
121         overwriteFinalState( (NewLineInsertion)action );
122         ++count_;
123         return true;
124     }
125 }