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

📄 nfr_filehandleitem.java

📁 用swt作的一个类似飞鸽一样的局域网通信工具
💻 JAVA
字号:
package view.receiver;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.text.DecimalFormat;

import main.NetTransfer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.ProgressBar;

import extend.Configuration;

public class NFR_FileHandleItem extends Composite {

	private int hashFile;

	private long fileSize;

	private String fileName;  //  @jve:decl-index=0:

	private DecimalFormat df = new DecimalFormat("#.00");

	//	private Socket socket;

//	private DataInputStream inByte = null;

	private DataOutputStream outByte = null;

	private String type;

	private String filePath = "";  //  @jve:decl-index=0:

//	private Composite parent;

	public Display display;

	private CLabel fileLogo_cLabel = null;

	private CLabel fileName_cLabel = null;

	private CLabel fileSize_cLabel = null;

	private ProgressBar progressBar = null;

	private ControlButton link = null;

	private int port;

	private Image fileLogo = new Image(Display.getCurrent(), getClass()
			.getResourceAsStream("/view/picture/file.png"));

	private Image folderLogo = new Image(Display.getCurrent(), getClass()
			.getResourceAsStream("/view/picture/folder.png"));

	private CLabel transferSpeed_cLabel = null;

	public NFR_FileHandleItem(Composite parent, int style) {
		super(parent, style);
//		this.parent = parent;
		initialize();
	}

	public NFR_FileHandleItem(Composite parent, int style, Socket socket) {
		super(parent, style);
//		this.parent = parent;
		this.display = parent.getDisplay();
//		this.socket = socket;
		try {
//			inByte = new DataInputStream(socket.getInputStream());
			outByte = new DataOutputStream(socket.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
		initialize();
	}

	public void thisDispose() {
		if ((fileLogo!=null)&&!fileLogo.isDisposed())
			fileLogo.dispose();
		if ((folderLogo!=null)&&!folderLogo.isDisposed())
			folderLogo.dispose();
		if ((fileLogo_cLabel!=null)&&!fileLogo_cLabel.isDisposed())
			fileLogo_cLabel.dispose();
		if ((fileName_cLabel!=null)&&!fileName_cLabel.isDisposed())
			fileName_cLabel.dispose();
		if ((fileSize_cLabel!=null)&&!fileSize_cLabel.isDisposed())
			fileSize_cLabel.dispose();
		if ((progressBar!=null)&&!progressBar.isDisposed())
			progressBar.dispose();
		if ((link!=null)&&!link.isDisposed())
			link.thisDispose();
		this.dispose();
		free();
	}
	
	protected void free(){
		this.df = null;
		this.display = null;
		this.fileLogo = null;
		this.fileLogo_cLabel = null;
		this.fileName = null;
		this.fileName_cLabel = null;
		this.filePath = null;
		this.fileSize_cLabel = null;
		this.folderLogo = null;
		this.link = null;
		this.outByte = null;
		this.progressBar = null;
		this.transferSpeed_cLabel = null;
		this.type = null;
	}
	
	public void setConnectPort(int port) {
		this.port = port;
	}

	public void setType(String type) {
		this.type = type;
		if (type.equals("FILE")) {
			fileLogo_cLabel.setImage(fileLogo);
		} else if (type.equals("FOLDER")) {
			fileLogo_cLabel.setImage(folderLogo);
		}
	}

	public String getType() {
		return this.type;
	}

	public void setProcess(int selection) {
		progressBar.setToolTipText(String.valueOf(selection)+"%");
		progressBar.setSelection(selection);
	}

	public void setMaxProcess(int max) {
		progressBar.setMaximum(max);
	}

	public int getProcess() {
		return progressBar.getSelection();
	}

	public void setHashFile(int hashFile) {
		this.hashFile = hashFile;
	}

	public int getHashFile() {
		return this.hashFile;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
		fileName_cLabel.setText(fileName);
	}

	public String getFileName() {
		return this.fileName;
	}

	public void setFileSize(long fileSize) {
		this.fileSize = fileSize;
		float tfSize = (float)fileSize;
		String[] units = { "B", "KB", "MB", "GB", "TB" };
		int h = 0;
		for (h = 0; tfSize > 1024f; h++) {
			tfSize = (tfSize / 1024f);
		}
		fileSize_cLabel.setText(this.df.format(tfSize) + units[h]);
	}
	
	public void setSpeed(String speed){
		transferSpeed_cLabel.setText(speed);
	}
	
	public void setTransfered(long fileSize) {
		float tfSize = (float)fileSize;
		String[] units = { "B", "KB", "MB", "GB", "TB" };
		int h = 0;
		for (h = 0; tfSize > 1024f; h++) {
			tfSize = (tfSize / 1024f);
		}
		fileSize_cLabel.setText(this.df.format(tfSize) + units[h]);
	}
	
	public void setPercent(int percent){
		fileSize_cLabel.setText(String.valueOf(percent)+"%");
	}
	
	public long getFileSize() {
		return (long)this.fileSize;
	}

	public void setLinkText(String text) {
		this.link.setControlMode(text);
	}

	public String toString() {
		return "HashFile=" + getHashFile() + " FileName=" + getFileName()
				+ " FileSize=" + getFileSize();
	}

	private void initialize() {
		GridData gridData11 = new GridData();
		gridData11.horizontalAlignment = GridData.END;
		gridData11.grabExcessHorizontalSpace = false;
		gridData11.grabExcessVerticalSpace = false;
		gridData11.widthHint = 55;
		gridData11.horizontalIndent = 0;
		gridData11.verticalAlignment = GridData.FILL;
		GridData gridData4 = new org.eclipse.swt.layout.GridData();
		gridData4.verticalSpan = 2;
		gridData4.verticalAlignment = GridData.FILL;
		gridData4.horizontalAlignment = GridData.FILL;
		GridData gridData3 = new org.eclipse.swt.layout.GridData();
		gridData3.horizontalAlignment = GridData.END;
		gridData3.grabExcessHorizontalSpace = false;
		gridData3.grabExcessVerticalSpace = false;
		gridData3.widthHint = 60;
		gridData3.heightHint = -1;
		gridData3.verticalAlignment = GridData.FILL;
		GridData gridData2 = new org.eclipse.swt.layout.GridData();
		gridData2.grabExcessHorizontalSpace = true;
		gridData2.horizontalAlignment = GridData.BEGINNING;
		gridData2.verticalAlignment = GridData.FILL;
		gridData2.grabExcessVerticalSpace = true;
		GridData gridData1 = new org.eclipse.swt.layout.GridData();
		gridData1.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData1.grabExcessVerticalSpace = false;
		gridData1.grabExcessHorizontalSpace = false;
		gridData1.heightHint = 8;
		gridData1.horizontalSpan = 3;
		gridData1.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridComposite = new GridData();
		gridComposite.horizontalAlignment = GridData.FILL;
		gridComposite.verticalAlignment = GridData.FILL;
		gridComposite.grabExcessHorizontalSpace = true;
		this.setLayoutData(gridComposite);
		GridData gridData = new org.eclipse.swt.layout.GridData();
		gridData.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData.grabExcessVerticalSpace = true;
		gridData.grabExcessHorizontalSpace = false;
		gridData.verticalSpan = 3;
		gridData.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridLayout gridLayout = new GridLayout();
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginWidth = 0;
		gridLayout.marginHeight = 0;
		gridLayout.numColumns = 5;
		gridLayout.verticalSpacing = 0;
		fileLogo_cLabel = new CLabel(this, SWT.NONE);
		fileLogo_cLabel.setText("");
		fileLogo_cLabel.setBackground(new Color(Display.getCurrent(), 228, 224,
				203));
		fileLogo_cLabel.setLayoutData(gridData);
		fileLogo_cLabel.setImage(fileLogo);
		fileName_cLabel = new CLabel(this, SWT.NONE);
		fileName_cLabel.setText("");
		fileName_cLabel.setBackground(new Color(Display.getCurrent(), 228, 224,
				203));
		fileName_cLabel.setLayoutData(gridData2);
		transferSpeed_cLabel = new CLabel(this, SWT.NONE);
		transferSpeed_cLabel.setText("");
		transferSpeed_cLabel.setBackground(new Color(Display.getCurrent(), 228, 224,
				203));
		transferSpeed_cLabel.setLayoutData(gridData11);
		fileSize_cLabel = new CLabel(this, SWT.NONE);
		fileSize_cLabel.setText("");
		fileSize_cLabel.setBackground(new Color(Display.getCurrent(), 228, 224,
				203));
		fileSize_cLabel.setLayoutData(gridData3);
		link = new ControlButton(this, SWT.NONE);
		link.setControlMode("save");
		link.setBackground(new Color(Display.getCurrent(), 228, 224, 203));
		link.setLayoutData(gridData4);
		link.addMouseListener(new MouseListener() {

			public void mouseDoubleClick(MouseEvent e) {

			}

			public void mouseDown(MouseEvent e) {

			}

			public void mouseUp(MouseEvent e) {
				if (link.getControlMode().equals("save")) {
					if (type.equals("FILE")) {
						FileDialog fd = new FileDialog(getShell(), SWT.SAVE);
						fd.setFileName(fileName);
						fd.open();
						filePath = fd.getFilterPath() + "\\" + fd.getFileName();
						System.out.println(filePath);
						if (!fd.getFilterPath().equals("")) {
							try {
								outByte.writeUTF("ReqFile|" + hashFile + "|"
										+ filePath + "|"
										+ NFR_FileHandleItem.this.hashCode()
										+ "|" + port + "|");
							} catch (IOException e1) {
								e1.printStackTrace();
							}
							link.setControlMode("cancel");
						}

					} else if (type.equals("FOLDER")) {
						DirectoryDialog dd = new DirectoryDialog(getShell(),
								SWT.SAVE);
						dd.open();
						filePath = dd.getFilterPath();
						System.out.println(filePath);
						if (!filePath.equals("")) {
							try {
								outByte.writeUTF("ReqDir|" + hashFile + "|"
										+ filePath + "|"
										+ NFR_FileHandleItem.this.hashCode()
										+ "|" + port + "|");
							} catch (IOException e1) {
								e1.printStackTrace();
							}
							link.setControlMode("cancel");
						}
					}
				}else if (link.getControlMode().equals("cancel")) {
					try {
						outByte.writeUTF("Cancel|"+ NFR_FileHandleItem.this.hashCode()
										+ "|");
						link.setControlMode("save");
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				} else if (link.getControlMode().equals("open")) {
					Configuration rc = new Configuration(NetTransfer.SetupFile);
				    String cmd = rc.getValue("Command");
				    if (cmd.equals("")) cmd = "cmd /c start \"\"";
					if (!NFR_FileHandleItem.this.getType().equals("FOLDER")) {
						try {
							
							Runtime.getRuntime().exec(cmd+" \""+NFR_FileHandleItem.this.filePath+"\""); 
						} catch (IOException e1) {
							e1.printStackTrace();
						}
					} else {
						try {
							StringBuffer filePath = new StringBuffer(
							NFR_FileHandleItem.this.filePath);
							if (filePath.charAt(filePath.length() - 1) == '\\') {
								Runtime.getRuntime().exec(
										cmd+" \""+NFR_FileHandleItem.this.filePath+ NFR_FileHandleItem.this.fileName+"\"");
							} else {
								Runtime.getRuntime().exec(
										cmd+" \""+NFR_FileHandleItem.this.filePath+"\\"+ NFR_FileHandleItem.this.fileName+"\"");
							}
						} catch (IOException e1) {
							e1.printStackTrace();
						}
					}
				}
			}
		});
		progressBar = new ProgressBar(this, SWT.SMOOTH);
		progressBar
				.setBackground(new Color(Display.getCurrent(), 228, 224, 203));
		progressBar.setSelection(0);
		progressBar.setLayoutData(gridData1);
		this.setLayout(gridLayout);
		this.setBackground(new Color(Display.getCurrent(), 228, 224, 203));
		this.setSize(new org.eclipse.swt.graphics.Point(300, 30));

	}
}

⌨️ 快捷键说明

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