001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.support.components;
014:
015: import java.awt.Dimension;
016: import java.awt.Toolkit;
017: import java.awt.event.FocusEvent;
018: import java.awt.event.FocusListener;
019: import java.awt.event.KeyAdapter;
020: import java.awt.event.KeyEvent;
021:
022: import javax.swing.JTextArea;
023: import javax.swing.KeyStroke;
024: import javax.swing.event.UndoableEditEvent;
025: import javax.swing.event.UndoableEditListener;
026: import javax.swing.undo.CannotRedoException;
027: import javax.swing.undo.CannotUndoException;
028: import javax.swing.undo.UndoManager;
029:
030: import com.eviware.soapui.support.UISupport;
031: import com.eviware.soapui.support.actions.FindAndReplaceDialog;
032: import com.eviware.soapui.support.actions.FindAndReplaceable;
033: import com.eviware.soapui.support.components.JEditorStatusBar.JEditorStatusBarTarget;
034: import com.eviware.soapui.support.swing.JTextComponentPopupMenu;
035:
036: /**
037: * JEditTextArea extension targeted specifically at XML-editing.
038: *
039: * //@todo move font handling to subclass
040: *
041: * @author Ole.Matzura
042: */
043:
044: public class JUndoableTextArea extends JTextArea implements Undoable,
045: UndoableEditListener, FocusListener, FindAndReplaceable,
046: JEditorStatusBarTarget {
047: public static final int UNDO_LIMIT = 1500;
048:
049: private UndoManager undoManager;
050: private boolean discardEditsOnSet = true;
051: private FindAndReplaceDialog findAndReplaceAction;
052:
053: public JUndoableTextArea() {
054: init();
055: }
056:
057: private void init() {
058: //setBorder(BorderFactory.createEtchedBorder());
059: getDocument().addUndoableEditListener(this );
060: addFocusListener(this );
061:
062: setMinimumSize(new Dimension(50, 50));
063: addKeyListener(new KeyAdapter() {
064:
065: public void keyPressed(KeyEvent e) {
066: if (KeyStroke.getKeyStrokeForEvent(e).equals(
067: UISupport.getKeyStroke("menu Z")))
068: undo();
069: else if (KeyStroke.getKeyStrokeForEvent(e).equals(
070: UISupport.getKeyStroke("menu Y")))
071: redo();
072: else if (KeyStroke.getKeyStrokeForEvent(e).equals(
073: UISupport.getKeyStroke("menu X")))
074: cut();
075: else if (KeyStroke.getKeyStrokeForEvent(e).equals(
076: UISupport.getKeyStroke("menu C")))
077: copy();
078: else if (KeyStroke.getKeyStrokeForEvent(e).equals(
079: UISupport.getKeyStroke("F3")))
080: findAndReplace();
081: }
082: });
083:
084: JTextComponentPopupMenu.add(this );
085: }
086:
087: public JUndoableTextArea(int i, int j) {
088: super (i, j);
089: init();
090: }
091:
092: public JUndoableTextArea(String text) {
093: super (text);
094: init();
095: }
096:
097: protected void findAndReplace() {
098: if (findAndReplaceAction == null)
099: findAndReplaceAction = new FindAndReplaceDialog(this );
100:
101: findAndReplaceAction.show();
102: }
103:
104: public void setText(String text) {
105: super .setText(text);
106:
107: if (discardEditsOnSet && undoManager != null)
108: undoManager.discardAllEdits();
109: }
110:
111: public boolean isDiscardEditsOnSet() {
112: return discardEditsOnSet;
113: }
114:
115: public void setDiscardEditsOnSet(boolean discardEditsOnSet) {
116: this .discardEditsOnSet = discardEditsOnSet;
117: }
118:
119: public UndoManager getUndoManager() {
120: return undoManager;
121: }
122:
123: private void createUndoMananger() {
124: undoManager = new UndoManager();
125: undoManager.setLimit(UNDO_LIMIT);
126: }
127:
128: public void focusGained(FocusEvent fe) {
129: if (isEditable() && undoManager == null)
130: createUndoMananger();
131: }
132:
133: public void focusLost(FocusEvent fe) {
134: //removeUndoMananger();
135: }
136:
137: public void undoableEditHappened(UndoableEditEvent e) {
138: if (undoManager != null)
139: undoManager.addEdit(e.getEdit());
140: }
141:
142: public void undo() {
143: if (!isEditable()) {
144: getToolkit().beep();
145: return;
146: }
147:
148: try {
149: if (undoManager != null)
150: undoManager.undo();
151: } catch (CannotUndoException cue) {
152: Toolkit.getDefaultToolkit().beep();
153: }
154: }
155:
156: public void redo() {
157: if (!isEditable()) {
158: getToolkit().beep();
159: return;
160: }
161:
162: try {
163: if (undoManager != null)
164: undoManager.redo();
165: } catch (CannotRedoException cue) {
166: Toolkit.getDefaultToolkit().beep();
167: }
168: }
169:
170: public void setSelectedText(String txt) {
171: replaceSelection(txt);
172: }
173:
174: public boolean canRedo() {
175: return undoManager != null && undoManager.canRedo();
176: }
177:
178: public boolean canUndo() {
179: return undoManager != null && undoManager.canUndo();
180: }
181: }
|