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   * JEditTextArea extension targeted specifically at XML-editing.
    38   *
    39   * //@todo move font handling to subclass
    40   *
    41   * @author Ole.Matzura
    42   */
    43
    44  public class JUndoableTextArea extends JTextArea implements	Undoable, UndoableEditListener, FocusListener, FindAndReplaceable, JEditorStatusBarTarget
    45  {
    46  	public static final int UNDO_LIMIT = 1500;
    47
    48  	private UndoManager undoManager;
    49  	private boolean discardEditsOnSet = true;
    50  	private FindAndReplaceDialog findAndReplaceAction;
    51
    52  	public JUndoableTextArea()
    53  	{
    54  		init();
    55  	}
    56
    57  	private void init()
    58  	{
    59  		//setBorder(BorderFactory.createEtchedBorder());
    60  		getDocument().addUndoableEditListener(this);
    61  		addFocusListener(this);
    62
    63  		setMinimumSize( new Dimension( 50, 50 ));
    64  		addKeyListener( new KeyAdapter() {
    65
    66  			public void keyPressed(KeyEvent e)
    67  			{
    68  				if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu Z" )))
    69  				   undo();
    70  				else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu Y" )))
    71  					redo();
    72  				else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu X" )))
    73  					cut();
    74  				else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu C" )))
    75  					copy();
    76  				else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "F3" )))
    77  					findAndReplace();
    78  			}} );
    79
    80  		JTextComponentPopupMenu.add(  this  );
    81  	}
    82
    83  	public JUndoableTextArea( int i, int j )
    84  	{
    85  		super( i, j );
    86  		init();
    87  	}
    88
    89  	public JUndoableTextArea( String text )
    90  	{
    91  		super( text );
    92  		init();
    93  	}
    94
    95  	protected void findAndReplace()
    96  	{
    97  		if( findAndReplaceAction == null )
    98  			findAndReplaceAction = new FindAndReplaceDialog( this );
    99
    100 		findAndReplaceAction.show();
    101 	}
    102
    103 	public void setText(String text)
    104 	{
    105 		super.setText(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 	private void createUndoMananger()
    126 	{
    127 		undoManager = new UndoManager();
    128 		undoManager.setLimit(UNDO_LIMIT);
    129 	}
    130
    131 	public void focusGained(FocusEvent fe)
    132 	{
    133 		if (isEditable() && undoManager == null )
    134 			createUndoMananger();
    135 	}
    136
    137 	public void focusLost(FocusEvent fe)
    138 	{
    139 		//removeUndoMananger();
    140 	}
    141
    142 	public void undoableEditHappened(UndoableEditEvent e)
    143 	{
    144 		if (undoManager != null)
    145 			undoManager.addEdit(e.getEdit());
    146 	}
    147
    148 	public void undo()
    149 	{
    150 		if( !isEditable()  )
    151 		{
    152 			getToolkit().beep();
    153 			return;
    154 		}
    155
    156 		try
    157 		{
    158 			if( undoManager != null )
    159 				undoManager.undo();
    160 		}
    161 		catch (CannotUndoException cue)
    162 		{
    163 			Toolkit.getDefaultToolkit().beep();
    164 		}
    165 	}
    166
    167 	public void redo()
    168 	{
    169 		if( !isEditable()  )
    170 		{
    171 			getToolkit().beep();
    172 			return;
    173 		}
    174
    175 		try
    176 		{
    177 			if( undoManager != null )
    178 				undoManager.redo();
    179 		}
    180 		catch (CannotRedoException cue)
    181 		{
    182 			Toolkit.getDefaultToolkit().beep();
    183 		}
    184 	}
    185
    186 	public void setSelectedText(String txt)
    187 	{
    188 		replaceSelection( txt );
    189 	}
    190
    191 	public boolean canRedo()
    192 	{
    193 		return undoManager != null && undoManager.canRedo();
    194 	}
    195
    196 	public boolean canUndo()
    197 	{
    198 		return undoManager != null && undoManager.canUndo();
    199 	}
    200 }