001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.lib.client.util;
015:
016: import org.jetbrains.annotations.NotNull;
017: import org.pentaho.reportdesigner.lib.client.commands.KeyStrokeUtil;
018: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
019:
020: import javax.swing.*;
021: import javax.swing.event.UndoableEditEvent;
022: import javax.swing.event.UndoableEditListener;
023: import javax.swing.text.Document;
024: import javax.swing.text.JTextComponent;
025: import javax.swing.undo.CannotRedoException;
026: import javax.swing.undo.CannotUndoException;
027: import javax.swing.undo.UndoManager;
028: import javax.swing.undo.UndoableEditSupport;
029: import java.awt.event.ActionEvent;
030:
031: /**
032: * User: Martin
033: * Date: 24.01.2006
034: * Time: 09:23:10
035: */
036: public class UndoHelper {
037: @NotNull
038: private static final String UNDO = "Undo";
039: @NotNull
040: private static final String REDO = "Redo";
041:
042: private UndoHelper() {
043: }
044:
045: public static void installUndoSupport(@NotNull
046: JTextComponent jTextComponent) {
047: final UndoManager undo = new UndoManager();
048: Document doc = jTextComponent.getDocument();
049:
050: UndoableEditSupport undoableEditSupport = new UndoableEditSupport();
051: undoableEditSupport.addUndoableEditListener(undo);
052: undoableEditSupport.beginUpdate();
053:
054: // Listen for undo and redo events
055: doc.addUndoableEditListener(new UndoableEditListener() {
056: public void undoableEditHappened(@NotNull
057: UndoableEditEvent evt) {
058: undo.addEdit(evt.getEdit());
059: }
060: });
061:
062: // Create an undo action and add it to the text component
063: jTextComponent.getActionMap().put(UNDO,
064: new AbstractAction(UNDO) {
065: public void actionPerformed(@NotNull
066: ActionEvent evt) {
067: try {
068: if (undo.canUndo()) {
069: undo.undo();
070: }
071: } catch (CannotUndoException e) {
072: UncaughtExcpetionsModel.getInstance()
073: .addException(e);
074: }
075: }
076: });
077:
078: // Bind the undo action to ctl-Z
079: jTextComponent
080: .getInputMap()
081: .put(
082: KeyStrokeUtil
083: .getKeyStroke(TranslationManager
084: .getInstance()
085: .getTranslation(
086: TranslationManager.COMMON_BUNDLE_PREFIX,
087: "UndoHelper.Undo.Accelerator")),
088: UNDO);
089:
090: // Create a redo action and add it to the text component
091: jTextComponent.getActionMap().put(REDO,
092: new AbstractAction(REDO) {
093: public void actionPerformed(@NotNull
094: ActionEvent evt) {
095: try {
096: if (undo.canRedo()) {
097: undo.redo();
098: }
099: } catch (CannotRedoException e) {
100: UncaughtExcpetionsModel.getInstance()
101: .addException(e);
102: }
103: }
104: });
105:
106: // Bind the redo action to ctl-Y
107: jTextComponent
108: .getInputMap()
109: .put(
110: KeyStrokeUtil
111: .getKeyStroke(TranslationManager
112: .getInstance()
113: .getTranslation(
114: TranslationManager.COMMON_BUNDLE_PREFIX,
115: "UndoHelper.Redo.Accelerator")),
116: REDO);
117: }
118: }
|