View Javadoc

1   /*
2    * $Id$
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.util;
22  
23  import javax.swing.JComboBox;
24  import javax.swing.undo.AbstractUndoableEdit;
25  
26  import org.xmlhammer.gui.Page;
27  import org.xmlhammer.gui.history.HistoryComboBox;
28  
29  public class UndoableComboBoxEdit extends AbstractUndoableEdit {
30      private static final long serialVersionUID = 8615885869967750791L;
31  
32      private Object current = null;
33      private Object previous = null;
34      private Page page = null;
35      private JComboBox combo = null;
36      
37      public UndoableComboBoxEdit(Page page, JComboBox combo, Object current, Object previous) {
38          super();
39          
40          this.page = page;
41          this.combo = combo;
42          this.current = current;
43          this.previous = previous;
44      }
45  
46      public void undo() {
47          super.undo();
48  
49          page.getProjectView().getOverviewPanel().selectNode(page);
50          combo.requestFocusInWindow();
51          combo.setSelectedItem(previous);
52  
53          if (combo.isEditable() && previous != null) {
54              combo.getEditor().setItem(previous);
55          } else if (previous == null && combo instanceof HistoryComboBox) {
56              combo.getEditor().setItem(((HistoryComboBox)combo).getEmptyValue());
57          }
58      }
59  
60      public void redo() {
61          super.redo();
62  
63          page.getProjectView().getOverviewPanel().selectNode(page);
64          combo.requestFocusInWindow();
65          combo.setSelectedItem(current);
66  
67          if (combo.isEditable() && current != null) {
68              combo.getEditor().setItem(current);
69          } else if (current == null && combo instanceof HistoryComboBox) {
70              combo.getEditor().setItem(((HistoryComboBox)combo).getEmptyValue());
71          }
72      }
73      
74      public String getUndoPresentationName() {
75          return "Set "+previous.toString();
76      }
77  
78      public String getRedoPresentationName() {
79          return "Reset "+current.toString();
80      }
81      
82      public String toString() {
83          StringBuffer buffer = new StringBuffer("ComboBoxEdit Undo ");
84  
85          if ( previous != null) {
86              buffer.append(previous.toString());
87          } else {
88              buffer.append("null");
89          }
90          
91          buffer.append(" Redo ");
92  
93          if ( current != null) {
94              buffer.append(current.toString());
95          } else {
96              buffer.append("null");
97          }
98  
99          return buffer.toString();
100     }
101 }