View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    7    *
    8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
    9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10   *  See the GNU Lesser General Public License for more details at gnu.org.
    11   */
    12
    13  package com.eviware.soapui.support.components;
    14
    15  import java.awt.Dimension;
    16  import java.awt.Toolkit;
    17  import java.awt.event.FocusEvent;
    18  import java.awt.event.FocusListener;
    19  import java.awt.event.KeyAdapter;
    20  import java.awt.event.KeyEvent;
    21
    22  import javax.swing.JTextArea;
    23  import javax.swing.KeyStroke;
    24  import javax.swing.event.UndoableEditEvent;
    25  import javax.swing.event.UndoableEditListener;
    26  import javax.swing.undo.CannotRedoException;
    27  import javax.swing.undo.CannotUndoException;
    28  import javax.swing.undo.UndoManager;
    29
    30  import com.eviware.soapui.support.UISupport;
    31  import com.eviware.soapui.support.actions.FindAndReplaceDialog;
    32  import com.eviware.soapui.support.actions.FindAndReplaceable;
    33  import com.eviware.soapui.support.components.JEditorStatusBar.JEditorStatusBarTarget;
    34  import com.eviware.soapui.support.swing.JTextComponentPopupMenu;
    35
    36  /***
    37   * JTextArea with Undo/Redo keyboard/popup support
    38   *
    39   * @author Ole.Matzura
    40   */
    41
    42  public class JUndoableTextArea extends JTextArea implements	Undoable, UndoableEditListener, FocusListener, FindAndReplaceable, JEditorStatusBarTarget
    43  {
    44  	public static final int UNDO_LIMIT = 1500;
    45
    46  	private UndoManager undoManager;
    47  	private boolean discardEditsOnSet = false;
    48  	private FindAndReplaceDialog findAndReplaceAction;
    49
    50  	public JUndoableTextArea()
    51  	{
    52  		super();
    53  		init();
    54  	}
    55
    56  	private void init()
    57  	{
    58  		//setBorder(BorderFactory.createEtchedBorder());
    59  		getDocument().addUndoableEditListener(this);
    60  		addFocusListener(this);
    61
    62  		setMinimumSize( new Dimension( 50, 50 ));
    63  		addKeyListener( new KeyAdapter() {
    64
    65  			public void keyPressed(KeyEvent e)
    66  			{
    67  				if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu Z" )))
    68  				   undo();
    69  				else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu Y" )))
    70  					redo();
    71  				else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu X" )))
    72  					cut();
    73  				else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu C" )))
    74  					copy();
    75  				else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "F3" )))
    76  					findAndReplace();
    77  			}} );
    78
    79  		JTextComponentPopupMenu.add(  this  );
    80  	}
    81
    82  	public JUndoableTextArea( int i, int j )
    83  	{
    84  		super( i, j );
    85  		init();
    86  	}
    87
    88  	public JUndoableTextArea( String text )
    89  	{
    90  		super( text );
    91  		init();
    92  	}
    93
    94  	protected void findAndReplace()
    95  	{
    96  		if( findAndReplaceAction == null )
    97  			findAndReplaceAction = new FindAndReplaceDialog( this );
    98
    99  		findAndReplaceAction.show();
    100 	}
    101
    102 	public void setText(String text)
    103 	{
    104 		ensureUndoManager();
    105 		super.setText(text == null ? "" : text);
    106
    107 		if( discardEditsOnSet && undoManager != null )
    108 			undoManager.discardAllEdits();
    109 	}
    110
    111 	public boolean isDiscardEditsOnSet()
    112 	{
    113 		return discardEditsOnSet;
    114 	}
    115
    116 	public void setDiscardEditsOnSet(boolean discardEditsOnSet)
    117 	{
    118 		this.discardEditsOnSet = discardEditsOnSet;
    119 	}
    120
    121 	public UndoManager getUndoManager()
    122 	{
    123 		return undoManager;
    124 	}
    125
    126 	private void ensureUndoManager()
    127 	{
    128 		if (isEditable() && undoManager == null )
    129 		{
    130 			undoManager = new UndoManager();
    131 			undoManager.setLimit(UNDO_LIMIT);
    132 		}
    133 	}
    134
    135 	public void focusGained(FocusEvent fe)
    136 	{
    137 		ensureUndoManager();
    138 	}
    139
    140 	public void focusLost(FocusEvent fe)
    141 	{
    142 		//removeUndoMananger();
    143 	}
    144
    145 	public void undoableEditHappened(UndoableEditEvent e)
    146 	{
    147 		if (undoManager != null)
    148 			undoManager.addEdit(e.getEdit());
    149 	}
    150
    151 	public void undo()
    152 	{
    153 		if( !isEditable()  )
    154 		{
    155 			getToolkit().beep();
    156 			return;
    157 		}
    158
    159 		try
    160 		{
    161 			if( undoManager != null )
    162 				undoManager.undo();
    163 		}
    164 		catch (CannotUndoException cue)
    165 		{
    166 			Toolkit.getDefaultToolkit().beep();
    167 		}
    168 	}
    169
    170 	public void redo()
    171 	{
    172 		if( !isEditable()  )
    173 		{
    174 			getToolkit().beep();
    175 			return;
    176 		}
    177
    178 		try
    179 		{
    180 			if( undoManager != null )
    181 				undoManager.redo();
    182 		}
    183 		catch (CannotRedoException cue)
    184 		{
    185 			Toolkit.getDefaultToolkit().beep();
    186 		}
    187 	}
    188
    189 	public void setSelectedText(String txt)
    190 	{
    191 		replaceSelection( txt );
    192 	}
    193
    194 	public boolean canRedo()
    195 	{
    196 		return undoManager != null && undoManager.canRedo();
    197 	}
    198
    199 	public boolean canUndo()
    200 	{
    201 		return undoManager != null && undoManager.canUndo();
    202 	}
    203 }