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

📄 replacebyinputwindow.java

📁 替换jsp中的中文为sturts中的bundle
💻 JAVA
字号:
package com.pengjj.app;

import java.io.File;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.pengjj.util.PengjjLog;

/**
 * <p>Title: 输入窗口</p>
 * <p>Description: 通过用户自己输入需要替换的文本和替换方式</p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: digitalchina</p>
 * <p>FileName: ReplaceByInput.java</p>
 * @author pengjja Sep 13, 2006
 * @version 1.0
 */
public class ReplaceByInputWindow extends PengjjLog{
	/**
	 * 替换后的字符串
	 */
	private Text textReplace;
	private String replace;
	/**
	 * 文件类型
	 */
	private Text textFileType;
	private String typeName;
	/**
	 * 要替换的字符串
	 */
	private Text textResource;
	private String regex;
	/**
	 * 文件目录
	 */
	private String fileName;
	private Text textDirectory;
	private static Label label ;
	protected Shell shell;
	private ProcessTask process = new ProcessTask(this);
	public String sFinish = "false";
	private ReplaceByInput replaceByInput = new ReplaceByInput(this);
	private File fileResource;
	
	private Thread thread;
	private Thread threadReplace; 
	/**
	 * Launch the application
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			ReplaceByInputWindow window = new ReplaceByInputWindow();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * Open the window
	 */
	public void open() {
		final Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	/**
	 * Create contents of the window
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(617, 422);
		shell.setText("文本替换器");
		/**
		 * 选择文件的目录
		 */
		final Button button = new Button(shell, SWT.NONE);
		button.setText("请选择要替换的文件目录");
		button.setBounds(11, 15, 193, 24);
		textDirectory = new Text(shell, SWT.BORDER);
		textDirectory.setBounds(11, 51, 591, 26);
		button.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				DirectoryDialog dlg = new DirectoryDialog(shell);
				dlg.setText("请选择要替换的文件目录"); // 设置窗口标题
				dlg.setFilterPath("d:/"); // 设置初始目录
				String dir = dlg.open(); // 打开对话框并返回一个包含所选目录路径的字符串
				log.debug("you want to getFile,the file is--" + dir);
				if (dir != null) {
					textDirectory.setText(dir);
				}
			}
		});

		/**
		 * 输入要替换的文件类型
		 */
		final Button buttonFileType = new Button(shell, SWT.NONE);
		buttonFileType.setText("请输入要查找的文件类型");
		buttonFileType.setBounds(11, 86, 258, 25);
		textFileType = new Text(shell, SWT.BORDER);
		textFileType.setBounds(9, 127, 591, 26);	
		/**
		 * 输入要替换的字符串:正则表达式
		 */
		final Button buttonResource = new Button(shell, SWT.NONE);
		buttonResource.setText("请输入要查找的内容(支持正则表达式)");
		buttonResource.setBounds(8, 166, 258, 25);
		textResource = new Text(shell, SWT.BORDER);
		textResource.setBounds(8, 203, 591, 26);
		
		
		/**
		 * 输入替换后的字符串
		 */
		final Button buttonReplace = new Button(shell, SWT.NONE);
		buttonReplace.setText("替换为:");
		buttonReplace.setBounds(8, 240, 257, 24);

		textReplace = new Text(shell, SWT.BORDER);
		textReplace.setBounds(8, 276, 586, 25);
		
		label = new Label(shell, SWT.NONE);
		label.setBounds(8, 338, 594, 38);
		//PENGJJ_TODO 用作测试:发布后需要去掉
		textDirectory.setText("e:/flow");
		textResource.setText("pengjj");
		textReplace.setText("");
		final Button buttonConfirm = new Button(shell, SWT.NONE);
		buttonConfirm.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				fileName = textDirectory.getText();
				regex = textResource.getText();
				replace = textReplace.getText();
				typeName = textFileType.getText();
				if (fileName == null || fileName.trim().length() <= 0) {
					MessageDialog.openWarning(shell, "您没有选择文件所在的目录",
							"请选择文件所在的目录");
				} else if (regex == null || regex.trim().length() <= 0) {
					MessageDialog.openWarning(shell, "您没有输入要查找的内容",
							"请输入要查找的内容");
				} else {
					//替换反斜杠为斜杠
					//System.out.println("the filename is ---"+fileName);
					//fileName = fileName.replaceAll("\\","/");
						boolean flag = MessageDialog.openConfirm(shell,
								"进行确认提交", "确认所有的信息都正确?");
						if (flag) {
							// 得到文件得相关信息
							fileResource = new File(fileName);
							
							sFinish = "false";
							//显示进度
							thread = new Thread() {
								public void run() {
									process.start();
								}
							};
							thread.start();
							
							//文件替换
							threadReplace = new Thread() {
								public void run() {
									//开始替换文件处理
									replaceByInput.startReplace(fileResource,null,typeName,regex,replace);
								}
							};
							threadReplace.start();
							label.setText("替换的结果请查看log目录下的相关log文件,");
						}					
				}
			}
		});
		buttonConfirm.setText("确认");
		buttonConfirm.setBounds(13, 308, 193, 28);
		//
	}
	/**
	 * 设置label的text
	 * 
	 * @param str
	 */
	public void setText(String str) {
		label.setText(str);
	}
	/**
	 * 得到处理文件的名称
	 * @return
	 */
	public String getResourceProcess() {
		return label.getText();
	}
	/**
	 * 是否完成
	 * @return
	 */
	public String getFinish() {
		return this.sFinish;
	}
	public void setFinish(String str){
		this.sFinish=str;
	}
}

⌨️ 快捷键说明

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