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

📄 nfs_filehandleitem.java

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

import java.io.File;
import java.text.DecimalFormat;
import java.util.Hashtable;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ScrolledComposite;
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.Display;
import org.eclipse.swt.widgets.Link;
import extend.ListDir;

public class NFS_FileHandleItem extends Composite {
	
	private File file;
	private ScrolledComposite scrolledComposite = null;
	private Composite parent;
	private CLabel fileLogo_cLabel = null;
	private CLabel fileName_cLabel = null;
	private CLabel fileSize_cLabel = null;
	private Link link = null;
	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 DecimalFormat df = new DecimalFormat("#.00");
	private Hashtable selectedFiles;
	static public long getFolderSize(File dir){
		long dirSize = 0;
		ListDir dirTree = new ListDir(dir.getPath());
		for(int i=0;i<dirTree.fileList.length;i++){
			if (!dirTree.fileList[i].isHidden()) {
				if(dirTree.fileList[i].isFile()){
					dirSize = dirSize+dirTree.fileList[i].length();
				} else if (dirTree.fileList[i].isDirectory()) {
					dirSize = dirSize+getFolderSize(dirTree.fileList[i]);
				}
			}
		}
		return dirSize;
	}
	
	public NFS_FileHandleItem(Composite parent, int style) {
		super(parent, style);
		this.parent = parent;
		initialize();
	}
	
	public NFS_FileHandleItem(Composite parent, int style, ScrolledComposite scrolledComposite,Hashtable selectedFiles) {
		super(parent, style);
		this.parent = parent;
		this.scrolledComposite = scrolledComposite;
		this.selectedFiles = selectedFiles;
		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 ((link!=null)&&!link.isDisposed()) link.dispose();
		this.dispose();
	}
	public void setType(String type){
		if (type.equals("FILE")){
			fileLogo_cLabel.setImage(fileLogo);
		}else if (type.equals("FOLDER")){
			fileLogo_cLabel.setImage(folderLogo);
		}
	}
	
	public void setFile(File file){
		this.file = file;
		if (file.isDirectory()) {
			setType("FOLDER");
			setFileSize(getFolderSize(file));
			System.out.println("NFS_FileHandleItem = "+getFolderSize(file));
		}
		else{
			setType("FILE");
			setFileSize(file.length());
		}
		setFileName(file.getName());
	}
	
	public void setFileName(String fileName){
		fileName_cLabel.setText(fileName);
	}
	
	public void setFileSize(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]);
	}
	
	private void initialize() {
		GridData gridData4 = new org.eclipse.swt.layout.GridData();
		gridData4.verticalSpan = 2;
		gridData4.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		gridData4.grabExcessHorizontalSpace = false;
		gridData4.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridData3 = new org.eclipse.swt.layout.GridData();
		gridData3.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData3.grabExcessHorizontalSpace = false;
		gridData3.grabExcessVerticalSpace = true;
		gridData3.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridData2 = new org.eclipse.swt.layout.GridData();
		gridData2.grabExcessHorizontalSpace = true;
		gridData2.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData2.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData2.grabExcessVerticalSpace = true;
		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 = 4;
		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);
		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 Link(this, SWT.NONE);
		link.setText("  <a>Remove</a>  ");
		link.setBackground(new Color(Display.getCurrent(), 228, 224, 203));
		link.setLayoutData(gridData4);
		link.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				NFS_FileHandleItem.this.thisDispose();
				selectedFiles.remove(new Integer(NFS_FileHandleItem.this.file.hashCode()));
				if (scrolledComposite!=null) scrolledComposite.setMinSize(parent.computeSize(SWT.DEFAULT,SWT.DEFAULT));
				parent.pack();
			}
		});
		
		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 + -