Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/virtuosotechnologies/asaph/standardgui/UndoHelper.java


1   /*
2   ================================================================================
3   
4     FILE:  UndoHelper.java
5     
6     PROJECT:
7     
8       Asaph
9     
10    CONTENTS:
11    
12      A helper for handling undo listening
13    
14    PROGRAMMERS:
15    
16      Daniel Azuma (DA)  <dazuma@kagi.com>
17    
18    COPYRIGHT:
19    
20      Copyright (C) 2003  Daniel Azuma  (dazuma@kagi.com)
21      
22      This program is free software; you can redistribute it and/or
23      modify it under the terms of the GNU General Public License as
24      published by the Free Software Foundation; either version 2
25      of the License, or (at your option) any later version.
26      
27      This program is distributed in the hope that it will be useful,
28      but WITHOUT ANY WARRANTY; without even the implied warranty of
29      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30      GNU General Public License for more details.
31      
32      You should have received a copy of the GNU General Public
33      License along with this program; if not, write to
34        Free Software Foundation, Inc.
35        59 Temple Place, Suite 330
36        Boston, MA 02111-1307 USA
37  
38  ================================================================================
39  */
40  
41  
42  package com.virtuosotechnologies.asaph.standardgui;
43  
44  
45  import java.util.List;
46  import java.util.ArrayList;
47  import javax.swing.undo.UndoableEdit;
48  import javax.swing.undo.AbstractUndoableEdit;
49  import javax.swing.undo.CannotUndoException;
50  import javax.swing.undo.CannotRedoException;
51  import javax.swing.event.UndoableEditEvent;
52  import javax.swing.event.UndoableEditListener;
53  
54  
55  /**
56   * The song body editor: A helper for handling undo listening
57   */
58  /*package*/ abstract class UndoHelper
59  implements UndoableEditListener
60  {
61    private List edits_;
62    private SongBodyEditorPane pane_;
63    private EditorSelection oldSelection_;
64    
65    
66    /*package*/ UndoHelper(
67      SongBodyEditorPane pane)
68    {
69      pane_ = pane;
70      edits_ = new ArrayList();
71      oldSelection_ = pane_.getSelection().createCopy();
72    }
73    
74    
75    public void undoableEditHappened(
76      final UndoableEditEvent ev)
77    {
78      edits_.add(ev.getEdit());
79    }
80    
81    
82    /*package*/ final void finish(
83      final EditorSelection newSelection)
84    {
85      assert newSelection == null || !newSelection.isOriginal();
86      final EditorSelection selection = pane_.getSelection();
87      final UndoableEdit[] editArray = new UndoableEdit[edits_.size()];
88      edits_.toArray(editArray);
89      pane_.emitUndoableEditEvent(
90        new AbstractUndoableEdit()
91        {
92          public void undo()
93          throws CannotUndoException
94          {
95            super.undo();
96            for (int i=editArray.length-1; i>=0; --i)
97            {
98              editArray[i].undo();
99            }
100           rebuildView();
101           selection.invalidate();
102           selection.set(oldSelection_, true);
103         }
104         
105         public void redo()
106         throws CannotRedoException
107         {
108           super.redo();
109           for (int i=0; i<editArray.length; ++i)
110           {
111             editArray[i].redo();
112           }
113           rebuildView();
114           if (newSelection != null)
115           {
116             selection.invalidate();
117             selection.set(newSelection, true);
118           }
119         }
120       });
121     rebuildView();
122     if (newSelection != null)
123     {
124       selection.invalidate();
125       selection.set(newSelection, true);
126     }
127   }
128   
129   
130   protected abstract void rebuildView();
131 }