View Javadoc

1   /*
2    * $Id: UndoAction.java,v 1.9 2007/07/04 19:42:49 edankert Exp $
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  package org.xmlhammer.gui.actions;
22  
23  import java.awt.Toolkit;
24  import java.awt.event.KeyEvent;
25  
26  import javax.swing.ImageIcon;
27  import javax.swing.KeyStroke;
28  import javax.swing.event.ChangeEvent;
29  import javax.swing.event.ChangeListener;
30  
31  import org.bounce.RunnableAction;
32  import org.xmlhammer.gui.ProjectView;
33  
34  /***
35   * An action that can be used to undo edits.
36   *
37   * @version	$Revision: 1.9 $, $Date: 2007/07/04 19:42:49 $
38   * @author Edwin Dankert <edankert@gmail.com>
39   */
40   public class UndoAction extends RunnableAction implements ChangeListener {
41   	private static final long serialVersionUID = 3546080276339046711L;
42      private ProjectView view = null;
43  	
44   	/***
45  	 * @param parent the XMLHammer parent.
46  	 */
47   	public UndoAction() {
48  		super( "Undo");
49  		
50  		putValue( MNEMONIC_KEY, new Integer( 'U'));
51  		putValue( ACCELERATOR_KEY, KeyStroke.getKeyStroke( KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
52  		putValue( SMALL_ICON, new ImageIcon( getClass().getResource( "/org/xmlhammer/gui/icons/etool16/undo_edit.gif")));
53  		putValue( SHORT_DESCRIPTION, "Undo");
54  		
55  		setEnabled( false);
56   	}
57      
58      public void setView(ProjectView view) {
59          if (this.view != null) {
60              this.view.getUndoManager().removeChangeListener(this);
61          }
62          
63          this.view = view;
64          
65          if (this.view != null) {
66              this.view.getUndoManager().addChangeListener(this);
67              setEnabled(this.view.getUndoManager().canUndo());
68          } else {
69              setEnabled(false);
70          }
71      }
72   	
73  	/***
74  	 * The implementation of the undo action, called 
75  	 * after a user action.
76  	 */
77   	public void run() {
78          if (view != null && view.getUndoManager().canUndo()) {
79              view.getUndoManager().undo();
80              setEnabled(view.getUndoManager().canUndo());
81          }
82      }
83  
84      public void stateChanged(ChangeEvent event) {
85          setEnabled(view.getUndoManager().canUndo());
86  
87          if (view.getUndoManager().isMark()) {
88              view.getFieldManager().setFieldsChanged(false);
89          }
90      }
91  }