Package clipborad;
Import javax.swing.jtextarea; import java.awt.datatransfer. *; import java.awt. *; import javax.swing. *; Import java.io. *; import javax.swing.undo. *; import javax.swing .Text. *; Import javax.swing.Event. *; Import java.awt.event. *; / ** * Title: Undo, Redo, Copy, Paste, Cut * Description: Undo, Redo with Java: Undo, Redo , Copy, Paste, Cut * Copyright: Copyright (C) 2001 * Company: * @Author: Peng Jianxiong * @version 1.0 * /
Public Class CliptextArea EXTENDS JTEXTAREA IMPLEments CLIPBOARDOWNER {
CLIPBOARD STRBOARD = this.gettoolkit (). GetSystemClipboard (); undomanager undo = new undomanager (); document doc = getDocument ();
Public CliptextArea () {Doc.addundoableEditListener (New undoableEditlistener () {public void dec (e) {doc_addedit (e);}})
AddActionMap ();
PUBLIC VOID AddActionMap () {getActionMap (). put ("undo", new AbstractAction ("undo") {public void actionPerformed (ActionEvent EVT) {type {if (undo.canundo ()) {undo.undo ();} } Catch (cannotueException E) {}}}); getInputMap (). PUT (Keystroke.getKeystroke ("Control Z"), "undo");
GetActionMap (). PUT ("REDO", New AbstractAction ("REDO") {public void actionPerformed (ActionEvent evt) {type {iv () {undo.redo ();}} catch (cannotredoException E) {}}}); GetInputMap (). Put (Keystroke.getKeystroke ("Control R"), "Redo");
getActionMap (). Put ("Copy", New AbstractAction ("copy") {public void actionPerformed ();}}); getInputMap (). put (Keystroke.getKeystroke ("Control C"), "Copy"); getActionMap (). Put ("cut", new AbstractAction ("cut") {public void actionPerformed (ActionEvent EVT) {cut ();}}); getInputMap (). Put (Keystroke.getKeystroke) Control X ")," CUT ");
GetActionMap (). PUT ("Paste", New AbstractAction ("Paste") {public void actionPerformed (ActionEvent EVT) {PASTE ();}}); getInputMap (). put (Keystroke.getKeystroke ("Control v", "Paste");
}
Public void copy () {string scopy = getSelectedText (); if (scopy! = null) {stringselection sSelection = new stringselection; strboard.setContents (sSelection, this);}
public void cut () {String sCopy = getSelectedText (); if (sCopy = null!) {StringSelection sSelection = new StringSelection (sCopy); strBoard.setContents (sSelection, this); this.replaceSelection ( "");}}
public void paste () {Transferable sTransf = strBoard.getContents (this); if (! sTransf = null) {try {String sPaste = (String) sTransf.getTransferData (DataFlavor.stringFlavor); this.replaceRange (sPaste, getSelectionStart () GetSelectionEND ());} catch (unsupportedflavorexception e) {} catch (ooException ie) {}}}
Public Void Lostownership (Clipboard Clipboard, Transferable Contents) {
}
Void Doc_addedit (undoableeditevent e) {undo.addedit (E.GetEDIt ());}
}