⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 replace.java

📁 此类为”替换对话框“所需的类
💻 JAVA
字号:
/*************************************************************************************
* 类名: Replace                                                               
*                                                                    
* 功能: 此类为”替换对话框“所需的类
*                                                                
**************************************************************************************/
package edit.com;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Replace extends JPanel
{
	private JPanel centerPanel;
	private JLabel findLabel;
	private JLabel replaceLabel;
	private JLabel matchLabel;
	private JComboBox findText;
	private JComboBox replaceText;
	private JCheckBox matchBox;
	private JButton okButton;
	private JButton cancelButton;
	private JButton allButton;
	private JDialog dialog;
    private ReplaceState state = new ReplaceState();
    private ReplaceTools tool = new ReplaceTool();

	public Replace()
	{
		setLayout( new BorderLayout() );

		centerPanel = new JPanel();
		addToCenterPanel( centerPanel );

	}

    public void setString( String s )
    {
    	state.setSourceString( s );
    }

    public String getReplaceAll() {
        return state.getReplaceAll();
    }

    public int showDialog( Component c , String s )
    {
    	state.setFlag( 0 );

    	Frame owner = null;

    	if( c instanceof Frame )
    	{
    		owner = ( Frame )c;
    	}
    	else
    	{
    		owner = ( Frame )SwingUtilities.getAncestorOfClass( Frame.class , c );
    	}

    	if( dialog == null || dialog.getOwner() != owner )
    	{
    		owner = null;
    		dialog = new JDialog( owner , true );
    		dialog.getContentPane().add( this );
			dialog.setResizable( false );
			dialog.setSize( 400 , 180 );
    	}

    	dialog.setTitle( s );
		dialog.show();

		return state.getFlag();
	}

    public void find()
    {
    	int cartPosition = 0;
    	int aLength = 0;

    	String text = ( String )findText.getSelectedItem();

    	if( !matchBox.isSelected() )
    	{
    		state.setSourceString( state.getSourceString().toLowerCase());
    		text = text.toLowerCase();
    	}
    	cartPosition = state.getSourceString().indexOf( text ,
                                                        state.getLastFindPosition() );

    	if( cartPosition > -1 )
    	{
    		aLength = ( ( String )findText.getSelectedItem() ).length();
	        state.setFindPosition( cartPosition + aLength );
            state.setLastFindPosition( cartPosition +
                      ( ( String )replaceText.getSelectedItem() ).length() );

	        state.getReplaceSet().setStart( cartPosition );
            state.getReplaceSet().setEnd( state.getFindPosition() );
		}
		else
		{
			JOptionPane.showMessageDialog( Replace.this , "End of file" , "End of File" ,
					JOptionPane.INFORMATION_MESSAGE );

		    state.getReplaceSet().setStart( -1 );
            state.getReplaceSet().setEnd( -1 );
		}
    }

	public void replaceAll()
    {
    	String text = ( String )findText.getSelectedItem();
        String replace = ( String )replaceText.getSelectedItem();

        state.setReplaceAll( state.getSourceString().replaceAll( text , replace ) );
    }

	public ReplaceSet getSingleSet()  // this is a potential error , may be return a cloned object !!!!
	{
		return state.getReplaceSet();
	}

//private function add private class defined
    private void addToCenterPanel( JPanel aPanel )
    {
    	aPanel.setLayout( new GridBagLayout() );

    	GridBagConstraints constraint = new GridBagConstraints();

    	findLabel = tool.createLabel( "Find       : ", Color.blue );
        replaceLabel = tool.createLabel( "Replace : ", Color.blue );
    	matchLabel = tool.createLabel( "Match Case" );

    	findText = tool.createText( new Dimension( 150 , 25 ),
                                    new Dimension( 150 , 25 ) );
    	findText.setEditable( true );

        replaceText = tool.createText( new Dimension( 150 , 25 ),
                                       new Dimension( 150 , 25 ) );
    	replaceText.setEditable( true );
    	matchBox = new JCheckBox();

    	okButton = tool.createButton( "    Start     ",
                                       new Dimension( 100 , 25 ),
                                       Color.orange,
                                       new OkListener() );
    	cancelButton = tool.createButton( "   Cancel  ",
                                          new Dimension( 100 , 25 ),
                                          Color.orange,
                                          new CancelListener() );
    	allButton = tool.createButton( "ReplaceAll",
                                        new Dimension( 100 , 25 ),
                                        Color.orange,
                                        new ReplaceAllListener() );

    	Box temBox = tool.createHorizontalBox( 6 );
    	Box temBox_2 = tool.createVerticalBox( 12 );

    	Box buttonBox = Box.createVerticalBox();
    	buttonBox.add( okButton );
    	buttonBox.add( cancelButton );
    	buttonBox.add( allButton );

    	tool.addToCenterWithConstraint( findLabel , constraint , aPanel, 0 , 0 , 1 ,
            1 , GridBagConstraints.NONE , GridBagConstraints.WEST );
    	tool.addToCenterWithConstraint( replaceLabel , constraint , aPanel, 0 , 1 , 2 ,
            1 , GridBagConstraints.NONE , GridBagConstraints.WEST );
    	tool.addToCenterWithConstraint( temBox , constraint , aPanel, 0 , 2 , 2 , 1 , GridBagConstraints.NONE ,
    			GridBagConstraints.WEST );
    	tool.addToCenterWithConstraint( matchBox , constraint , aPanel, 0 , 3 , 1 , 1 , GridBagConstraints.NONE ,
    			GridBagConstraints.WEST );
    	tool.addToCenterWithConstraint( findText , constraint , aPanel, 1 , 0 , 1 ,
            1 , GridBagConstraints.HORIZONTAL ,	GridBagConstraints.CENTER );
    	tool.addToCenterWithConstraint( replaceText ,constraint , aPanel, 1 , 1 , 1 ,
            1 , GridBagConstraints.NONE , GridBagConstraints.WEST );
    	tool.addToCenterWithConstraint( matchLabel , constraint , aPanel, 1 , 3 , 1 ,
            1 , GridBagConstraints.NONE , GridBagConstraints.WEST );
        tool.addToCenterWithConstraint( temBox_2 , constraint , aPanel, 2 , 0 , 1 ,
            1 , GridBagConstraints.NONE , GridBagConstraints.WEST );
   		tool.addToCenterWithConstraint( buttonBox , constraint , aPanel, 3 , 0 , 1 ,
            4 , GridBagConstraints.NONE , GridBagConstraints.WEST );

   		add( aPanel , BorderLayout.CENTER );

   }

	//private class defined
    private class OkListener implements ActionListener
    {
    	public void actionPerformed( ActionEvent a )
    	{
    			state.setFlag( 1 );
    			find();
    			state.getReplaceSet().setText( ( String )replaceText.getSelectedItem() );

    			tool.addItems( findText , replaceText );
	    		dialog.setVisible( false );
    	}
    }

	private class CancelListener implements ActionListener
	{
		public void actionPerformed( ActionEvent a )
		{
			state.setFindPosition( 0 );


		    tool.addItems( findText , replaceText );
		    dialog.setVisible( false );
		}
	}

    private class ReplaceAllListener implements ActionListener
    {
    	public void actionPerformed( ActionEvent a )
    	{
    		state.setFlag( 2 );

    		state.setFindPosition( 0 );
    	    tool.addItems( findText , replaceText );
    	    replaceAll();

    	    dialog.setVisible( false );
    	}
    }

}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -