001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Evgeniya G. Maenkova
019: * @version $Revision$
020: */package javax.swing.undo;
021:
022: import java.io.Serializable;
023: import javax.swing.UIManager;
024:
025: import org.apache.harmony.x.swing.Utilities;
026:
027: public class AbstractUndoableEdit implements UndoableEdit, Serializable {
028:
029: private static final long serialVersionUID = 580150227676302096L;
030:
031: protected static final String UndoName = "Undo";
032:
033: protected static final String RedoName = "Redo";
034:
035: private boolean alive;
036:
037: private boolean hasBeenDone;
038:
039: public AbstractUndoableEdit() {
040: alive = true;
041: hasBeenDone = true;
042: }
043:
044: public boolean replaceEdit(final UndoableEdit anEdit) {
045: return false;
046: }
047:
048: public boolean addEdit(final UndoableEdit anEdit) {
049: return false;
050: }
051:
052: /*
053: * The format of the string is based on 1.5 release behavior
054: * which can be revealed using the following code:
055: *
056: * Object obj = new AbstractUndoableEdit() {};
057: * System.out.println(obj.toString());
058: */
059: @Override
060: public String toString() {
061: return super .toString() + " alive: " + alive + " hasBeenDone: "
062: + hasBeenDone;
063: }
064:
065: public String getUndoPresentationName() {
066: return getOperationPresentationName(getUndoName());
067: }
068:
069: public String getRedoPresentationName() {
070: return getOperationPresentationName(getRedoName());
071: }
072:
073: public String getPresentationName() {
074: return "";
075: }
076:
077: public boolean isSignificant() {
078: return true;
079: }
080:
081: public boolean canUndo() {
082: return alive && hasBeenDone;
083: }
084:
085: public boolean canRedo() {
086: return alive && !hasBeenDone;
087: }
088:
089: public void undo() {
090: if (!canUndo()) {
091: throw new CannotUndoException();
092: }
093:
094: hasBeenDone = false;
095: }
096:
097: public void redo() {
098: if (!canRedo()) {
099: throw new CannotRedoException();
100: }
101:
102: hasBeenDone = true;
103: }
104:
105: public void die() {
106: alive = false;
107: }
108:
109: final String getUndoName() {
110: return UIManager.getString("AbstractUndoableEdit.undoText");
111: }
112:
113: final String getRedoName() {
114: return UIManager.getString("AbstractUndoableEdit.redoText");
115: }
116:
117: private String getOperationPresentationName(
118: final String operationName) {
119: final String presentationName = getPresentationName();
120: return Utilities.isEmptyString(presentationName) ? operationName
121: : operationName + " " + presentationName;
122: }
123: }
|