alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (AbstractUndoableEdit.java)

This example Java source code file (AbstractUndoableEdit.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

abstractundoableedit, cannotredoexception, cannotundoexception, gui, redoname, serializable, string, swing, undoname

The AbstractUndoableEdit.java Java example source code

/*
 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package javax.swing.undo;

import java.io.Serializable;
import javax.swing.UIManager;

/**
 * An abstract implementation of <code>UndoableEdit,
 * implementing simple responses to all boolean methods in
 * that interface.
 *
 * @author Ray Ryan
 */
public class AbstractUndoableEdit implements UndoableEdit, Serializable {

    /**
     * String returned by <code>getUndoPresentationName;
     * as of Java 2 platform v1.3.1 this field is no longer used. This value
     * is now localized and comes from the defaults table with key
     * <code>AbstractUndoableEdit.undoText.
     *
     * @see javax.swing.UIDefaults
     */
    protected static final String UndoName = "Undo";

    /**
     * String returned by <code>getRedoPresentationName;
     * as of Java 2 platform v1.3.1 this field is no longer used. This value
     * is now localized and comes from the defaults table with key
     * <code>AbstractUndoableEdit.redoText.
     *
     * @see javax.swing.UIDefaults
     */
    protected static final String RedoName = "Redo";

    /**
     * Defaults to true; becomes false if this edit is undone, true
     * again if it is redone.
     */
    boolean hasBeenDone;

    /**
     * True if this edit has not received <code>die; defaults
     * to <code>true.
     */
    boolean alive;

    /**
     * Creates an <code>AbstractUndoableEdit which defaults
     * <code>hasBeenDone and alive to true.
     */
    public AbstractUndoableEdit() {
        super();

        hasBeenDone = true;
        alive = true;
    }

    /**
     * Sets <code>alive to false. Note that this
     * is a one way operation; dead edits cannot be resurrected.
     * Sending <code>undo or redo to
     * a dead edit results in an exception being thrown.
     *
     * <p>Typically an edit is killed when it is consolidated by
     * another edit's <code>addEdit or replaceEdit
     * method, or when it is dequeued from an <code>UndoManager.
     */
    public void die() {
        alive = false;
    }

    /**
     * Throws <code>CannotUndoException if canUndo
     * returns <code>false. Sets hasBeenDone
     * to <code>false. Subclasses should override to undo the
     * operation represented by this edit. Override should begin with
     * a call to super.
     *
     * @exception CannotUndoException if <code>canUndo
     *    returns <code>false
     * @see     #canUndo
     */
    public void undo() throws CannotUndoException {
        if (!canUndo()) {
            throw new CannotUndoException();
        }
        hasBeenDone = false;
    }

    /**
     * Returns true if this edit is <code>alive
     * and <code>hasBeenDone is true.
     *
     * @return true if this edit is <code>alive
     *    and <code>hasBeenDone is true
     *
     * @see     #die
     * @see     #undo
     * @see     #redo
     */
    public boolean canUndo() {
        return alive && hasBeenDone;
    }

    /**
     * Throws <code>CannotRedoException if canRedo
     * returns false. Sets <code>hasBeenDone to true.
     * Subclasses should override to redo the operation represented by
     * this edit. Override should begin with a call to super.
     *
     * @exception CannotRedoException if <code>canRedo
     *     returns <code>false
     * @see     #canRedo
     */
    public void redo() throws CannotRedoException {
        if (!canRedo()) {
            throw new CannotRedoException();
        }
        hasBeenDone = true;
    }

    /**
     * Returns <code>true if this edit is alive
     * and <code>hasBeenDone is false.
     *
     * @return <code>true if this edit is alive
     *   and <code>hasBeenDone is false
     * @see     #die
     * @see     #undo
     * @see     #redo
     */
    public boolean canRedo() {
        return alive && !hasBeenDone;
    }

    /**
     * This default implementation returns false.
     *
     * @param anEdit the edit to be added
     * @return false
     *
     * @see UndoableEdit#addEdit
     */
    public boolean addEdit(UndoableEdit anEdit) {
        return false;
    }

    /**
     * This default implementation returns false.
     *
     * @param anEdit the edit to replace
     * @return false
     *
     * @see UndoableEdit#replaceEdit
     */
    public boolean replaceEdit(UndoableEdit anEdit) {
        return false;
    }

    /**
     * This default implementation returns true.
     *
     * @return true
     * @see UndoableEdit#isSignificant
     */
    public boolean isSignificant() {
        return true;
    }

    /**
     * This default implementation returns "". Used by
     * <code>getUndoPresentationName and
     * <code>getRedoPresentationName to
     * construct the strings they return. Subclasses should override to
     * return an appropriate description of the operation this edit
     * represents.
     *
     * @return the empty string ""
     *
     * @see     #getUndoPresentationName
     * @see     #getRedoPresentationName
     */
    public String getPresentationName() {
        return "";
    }

    /**
     * Retreives the value from the defaults table with key
     * <code>AbstractUndoableEdit.undoText and returns
     * that value followed by a space, followed by
     * <code>getPresentationName.
     * If <code>getPresentationName returns "",
     * then the defaults value is returned alone.
     *
     * @return the value from the defaults table with key
     *    <code>AbstractUndoableEdit.undoText, followed
     *    by a space, followed by <code>getPresentationName
     *    unless <code>getPresentationName is "" in which
     *    case, the defaults value is returned alone.
     * @see #getPresentationName
     */
    public String getUndoPresentationName() {
        String name = getPresentationName();
        if (!"".equals(name)) {
            name = UIManager.getString("AbstractUndoableEdit.undoText") +
                " " + name;
        } else {
            name = UIManager.getString("AbstractUndoableEdit.undoText");
        }

        return name;
    }

    /**
     * Retreives the value from the defaults table with key
     * <code>AbstractUndoableEdit.redoText and returns
     * that value followed by a space, followed by
     * <code>getPresentationName.
     * If <code>getPresentationName returns "",
     * then the defaults value is returned alone.
     *
     * @return the value from the defaults table with key
     *    <code>AbstractUndoableEdit.redoText, followed
     *    by a space, followed by <code>getPresentationName
     *    unless <code>getPresentationName is "" in which
     *    case, the defaults value is returned alone.
     * @see #getPresentationName
     */
    public String getRedoPresentationName() {
        String name = getPresentationName();
        if (!"".equals(name)) {
            name = UIManager.getString("AbstractUndoableEdit.redoText") +
                " " + name;
        } else {
            name = UIManager.getString("AbstractUndoableEdit.redoText");
        }

        return name;
    }

    /**
     * Returns a string that displays and identifies this
     * object's properties.
     *
     * @return a String representation of this object
     */
    public String toString()
    {
        return super.toString()
            + " hasBeenDone: " + hasBeenDone
            + " alive: " + alive;
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java AbstractUndoableEdit.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.