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

📄 localrightclicklistener.java

📁 具有Ftp的基本功能
💻 JAVA
字号:
package Listener;

import java.awt.event.*;
import java.io.File;

import javax.swing.*;

import FtpLocalSource.LocalSourcePanelClass;
import GUI.RemoteFilePanel;
/**
 * 用于响应本地文件目录右键菜单按钮
 * @author 赖程远
 *
 */
public class LocalRightClickListener extends MouseAdapter
{
	private JPopupMenu				popupMenu;
	private JMenuItem				uploadMenuItem, deleteMenuItem,
			renameMenuItem;
	private JTabbedPane				tabbedPane;
	private LocalSourcePanelClass	localDirectory;
	/**
	 * 构造函数
	 * @param remotePane 用于显示远程FTP文件目录
	 * @param localDirectory 用于显示本地文件目录
	 */
	public LocalRightClickListener(final JTabbedPane remotePane,
			final LocalSourcePanelClass localDirectory)
	{
		this.localDirectory = localDirectory;
		this.tabbedPane = remotePane;
		popupMenu = new JPopupMenu();
		uploadMenuItem = new JMenuItem("上传", new ImageIcon("ArrowUp.png"));
		deleteMenuItem = new JMenuItem("删除", new ImageIcon("Delete.gif"));
		renameMenuItem = new JMenuItem("重命名", new ImageIcon("rename.gif"));
		
		popupMenu.add(uploadMenuItem);
		popupMenu.add(deleteMenuItem);
		popupMenu.add(renameMenuItem);
		
		// 事件监听
		uploadMenuItem.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				JTable table = localDirectory.getTable();
				int j = tabbedPane.getSelectedIndex();
				if (j < 0)
					return;
				RemoteFilePanel remoteFilePanel = (RemoteFilePanel) tabbedPane
						.getSelectedComponent();
				int i = table.getSelectedRow();
				if (i >= 0)
				{
					FtpLocalSource.ImagedTableCell tableCell = (FtpLocalSource.ImagedTableCell) table
							.getValueAt(i, 0);
					String s = tableCell.getText();
					remoteFilePanel.getConn().getTaskQueue().addUploadTask(s,
							localDirectory.getCurrentDirecory());
				}
			}
		});
		
		deleteMenuItem.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				JTable table = localDirectory.getTable();
				int i = table.getSelectedRow();
				if (i >= 0)
				{
					FtpLocalSource.ImagedTableCell tableCell = (FtpLocalSource.ImagedTableCell) table
							.getValueAt(i, 0);
					String s = tableCell.getText();
					String parent = localDirectory.getCurrentDirecory();
					File file = new File(parent, s);
					if (file.exists())
					{
						file.delete();
						localDirectory.updateUI();
					}
					
				}
			}
			
		});
		
		renameMenuItem.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				JTable table = localDirectory.getTable();
				int i = table.getSelectedRow();
				if (i >= 0)
				{
					FtpLocalSource.ImagedTableCell tableCell = (FtpLocalSource.ImagedTableCell) table
							.getValueAt(i, 0);
					String s = tableCell.getText();
					String parent = localDirectory.getCurrentDirecory();
					File file = new File(parent, s);
					if (file.exists())
					{
						String newname=JOptionPane.showInputDialog(null,"请输入名称:");
						file.renameTo(new File(parent,newname));
						localDirectory.updateUI();
					}
					
				}
			}
			
		});
	}
	
	public void mousePressed(MouseEvent e)
	{
		selectTable(e);
		checkForTriggerEvent(e);
	}
	
	public void mouseReleased(MouseEvent e)
	{
		selectTable(e);
		checkForTriggerEvent(e);
	}
	
	private void selectTable(MouseEvent e)
	{
		int row = localDirectory.getTable().rowAtPoint(e.getPoint());
		if (row >= 0)
			localDirectory.getTable().setRowSelectionInterval(row, row);
		
	}
	
	private void checkForTriggerEvent(MouseEvent e)
	{
		if (e.isPopupTrigger())
		{
			popupMenu.show(e.getComponent(), e.getX(), e.getY());
		}
		
	}
}

⌨️ 快捷键说明

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