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

📄 ftpframe.java

📁 一个远程登陆器的原代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		String name = JOptionPane.showInputDialog(null,"Enter new directory name", "Create New Directory", JOptionPane.QUESTION_MESSAGE);
		if(selectedTable.equals("remote"))
		{
			name = remoteLocation.getText()+name;
			viewer.rfb.createRemoteDirectory(name);
		}
		else
		{
			name = localLocation.getText()+name;
			File f = new File(name);
			f.mkdir();
			refreshLocalLocation();
			historyComboBox.insertItemAt(new String("Created Local Directory: " + name),0);
			historyComboBox.setSelectedIndex(0);
		}
	}
	private void doClose()
	{
		try {
			this.setVisible(false);
			viewer.rfb.writeFramebufferUpdateRequest(
									0,
									0,
									viewer.rfb.framebufferWidth,
									viewer.rfb.framebufferHeight,
									true);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void doDelete()
	{
		System.out.println("Delete Button Pressed");
		//Call this method to delete a file at server
		if(selectedTable.equals("remote"))
		{	
			String sFileName = ((String) this.remoteFileTable.getSelectedValue());
			
//			 sf@2004 - Directory can't be deleted
			if (sFileName.substring(0, 2).equals(" [") && sFileName.substring((sFileName.length() - 1), sFileName.length()).equals("]"))
			{
				JOptionPane.showMessageDialog(null, (String)"Directory Deletion is not yet available in this version...", "FileTransfer Info", JOptionPane.INFORMATION_MESSAGE);
				return;
			}			
			
			// for (int i = 0; i < remoteList.contains(size(); i++) 
			// 	remoteFileTable.g(i));			
			// sf@2004 - Delete prompt
			if (remoteList.contains(sFileName))
			{
				int r = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete the file \n< " + sFileName + " >\n on Remote Machine ?", "File Transfer Warning", JOptionPane.YES_NO_OPTION);
				if (r == JOptionPane.NO_OPTION)
					return;
			}
			
			String fileName = remoteLocation.getText()+ sFileName.substring(1);
			viewer.rfb.deleteRemoteFile(fileName);
		}
		else
		{
			String sFileName = ((String) this.localFileTable.getSelectedValue());
			
//			 sf@2004 - Directory can't be deleted
			if (sFileName.substring(0, 2).equals(" [") && sFileName.substring((sFileName.length() - 1), sFileName.length()).equals("]"))
			{
				JOptionPane.showMessageDialog(null, (String)"Directory Deletion is not yet available in this version...", "FileTransfer Info", JOptionPane.INFORMATION_MESSAGE);
				return;
			}			
			// sf@2004 - Delete prompt
			if (localList.contains(sFileName))
			{
				int r = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete the file \n< " + sFileName + " >\n on Local Machine ?", "File Transfer Warning", JOptionPane.YES_NO_OPTION);
				if (r == JOptionPane.NO_OPTION)
					return;
			}			
			String s = localLocation.getText() + sFileName.substring(1);
			File f = new File(s);
			f.delete();
			refreshLocalLocation();
			historyComboBox.insertItemAt(new String("Deleted On Local Disk: " + s),0);
			historyComboBox.setSelectedIndex(0);
		}
	}

	private void doReceive()
	{
		System.out.println("Received Button Pressed");

		String sFileName = ((String) this.remoteFileTable.getSelectedValue());
		
		// sf@2004 - Directory can't be transfered
		if (sFileName.substring(0, 2).equals(" [") && sFileName.substring((sFileName.length() - 1), sFileName.length()).equals("]"))
		{
			JOptionPane.showMessageDialog(null, (String)"Directory Transfer is not yet available in this version...", "FileTransfer Info", JOptionPane.INFORMATION_MESSAGE);
			return;
		}
		
		// sf@2004 - Overwrite prompt
		if (localList.contains(sFileName))
		{
			int r = JOptionPane.showConfirmDialog(null, "The file < " + sFileName + " >\n already exists on Local Machine\n Are you sure you want to overwrite it ?", "File Transfer Warning", JOptionPane.YES_NO_OPTION);
			if (r == JOptionPane.NO_OPTION)
				return;
		}
		
		//updateHistory("Downloaded " + localSelection.toString());
		String remoteFileName = this.remoteLocation.getText();
		remoteFileName+= ((String) this.remoteFileTable.getSelectedValue()).substring(1);
		
		String localDestinationPath = this.localLocation.getText()+((String)this.remoteFileTable.getSelectedValue()).substring(1);
		viewer.rfb.requestRemoteFile(remoteFileName,localDestinationPath);
	}

	private void doSend()
	{
		System.out.println("Send Button Pressed");

		String sFileName = ((String) this.localFileTable.getSelectedValue());
		
		// sf@2004 - Directory can't be transfered
		if (sFileName.substring(0, 2).equals(" [") && sFileName.substring((sFileName.length() - 1), sFileName.length()).equals("]"))
		{
			JOptionPane.showMessageDialog(null, (String)"Directory Transfer is not yet available in this version...", "FileTransfer Info", JOptionPane.INFORMATION_MESSAGE);
			return;
		}
		
		// sf@2004 - Overwrite prompt
		if (remoteList.contains(sFileName))
		{
			int r = JOptionPane.showConfirmDialog(null, "The file < " + sFileName + " >\n already exists on Remote Machine\n Are you sure you want to overwrite it ?", "File Transfer Warning", JOptionPane.YES_NO_OPTION);
			if (r == JOptionPane.NO_OPTION)
				return;
		}
		//updateHistory("Uploaded " + localSelection.toString());
		String source = this.localLocation.getText();
		source += ((String) this.localFileTable.getSelectedValue()).substring(1);
		
		String destinationPath = this.remoteLocation.getText();
		
		viewer.rfb.offerLocalFile(source,destinationPath); 
	}
	
	//
	// sf@2004 - The user stops the current file transfer
	// 
	private void doStop()
	{
		viewer.rfb.fAbort = true;
	}
	/**
	 * Update History: This method updates the history pulldown menu with the message string
	 *
	 */
	private void updateHistory(String message)
	{
		System.out.println("History: " + message);
		historyComboBox.insertItemAt(new String(message), 0);
	}
	
	/**
	 * This method updates the file table to the current selection of the remoteComboBox
	 *
	 */
	public void changeRemoteDrive()
	{
		remoteSelection = null;
	
		if (!updateDriveList) {
			String drive =	remoteDrivesComboBox.getSelectedItem().toString().substring(0,1)+ ":\\";
			viewer.rfb.readServerDirectory(drive);
			remoteLocation.setText(drive);
		}
		remoteList.clear();
		remoteFileTable.setListData(remoteList);
	}
	/**
	 * changeLocalDrive updates the file table
	 * to the current selection of the localComboBox
	 */
	private void changeLocalDrive()
	{
		File currentDrive = new File(localDrivesComboBox.getSelectedItem().toString());
		if(currentDrive.canRead())
		{
			localSelection = null;
			localStatus.setText("");
			changeLocalDirectory(currentDrive);
		}
		else
		{
			localList.clear();
			localStatus.setText("WARNING: Drive " + localDrivesComboBox.getSelectedItem().toString());
			connectionStatus.setText(" > WARNING - Local Drive unavailable (possibly restricted access or media not present)");
		}
	}
	/**
	 * Determines which FileTable was double-clicked and updates the table
	 */
	public void mouseClicked(MouseEvent e)
	{
		
		if(e.getClickCount() == 1)
		{								// Single clicked
			if (e.getSource() == localFileTable )
			{  			// on local file table 
				updateLocalFileTableSelection();
			}
			else if (e.getSource() == remoteFileTable)
			{
				updateRemoteFileTableSelection();						// on a remote file table
			}
		}
		else if (e.getClickCount() == 2)
		{						// Mouse Double clicked
			if (e.getSource() == localFileTable)
			{				// Clicked on local file
				updateLocalFileTable();
			}
			else if (e.getSource() == remoteFileTable)
			{		// Clicked on remote file
				updateRemoteFileTable();
			}
		}
	}
	/*
	 * Updates the globally accessible remote file selection if a file is single clicked in the RemoteFileTable
	 *
	 */
	private void updateRemoteFileTableSelection() {
		selectedTable = "remote";
		localFileTable.setBackground(new Color(238, 238, 238));
		remoteFileTable.setBackground(new Color(255, 255, 255));
		String name = (remoteFileTable.getSelectedValue().toString()).substring(1);
		if( !name.substring(0, 2).equals(" ["))	
			remoteSelection = remoteLocation.getText() + name.substring(0, name.length());
		
	}

	/*
	 * Updates the globally accessible local file selection 
	 * if a file is single clicked in the LocalFileTable 
	 *
	 */
	private void updateLocalFileTableSelection() {
		selectedTable="local";
		remoteFileTable.setBackground(new Color(238, 238, 238));
		localFileTable.setBackground(new Color(255, 255, 255));
		File currentSelection = new File(currentLocalDirectory, getTrimmedSelection());
		
		if(currentSelection.isFile()) 
			localSelection = currentSelection.getAbsoluteFile();

	}
	/**
	 * Updates the Remote File Table based on selection.  Called from mouseClicked handler
	 */
	public void updateRemoteFileTable() {
		String name = null;
		String action = null;
		String drive = null;
		name = (remoteFileTable.getSelectedValue().toString()).substring(1);

		if (name.equals("[..]"))
		{
			action = "up";
			remoteSelection = null;
			drive = remoteLocation.getText().substring(0, remoteLocation.getText().length() - 1);
			// JOptionPane.showMessageDialog(null, (String)drive, "FileTransfer DEBUG", JOptionPane.INFORMATION_MESSAGE);
			int index = drive.lastIndexOf("\\");
			drive = drive.substring(0, index + 1);

			remoteLocation.setText(drive);
			viewer.rfb.readServerDirectory(drive);
			remoteList.clear();
			remoteFileTable.setListData(remoteList);	
		}
		else if (!name.substring(0, 2).equals(" [") && !name.substring((name.length() - 1), name.length()).equals("]"))
		{
			action = "file";
			// Set the global remoteSelection field (used for get/put buttons)
			remoteSelection = remoteLocation.getText() + name.substring(0, name.length());
			drive = remoteLocation.getText();
			// ??
		}
		else
		{ 
			action = "down";
			remoteSelection = null;
			name = name.substring(1, name.length() - 1);
			drive = remoteLocation.getText() + name + "\\";
			remoteLocation.setText(drive);
			viewer.rfb.readServerDirectory(drive);
			remoteList.clear();
			remoteFileTable.setListData(remoteList);	
		}	
		//remoteLocation.setText(drive);	
	}
	/**
	 * Updates the Local File Table based on selection. Called from MouseClicked handler
	 */

	private void updateLocalFileTable()
	{
		localStatus.setText("");
		File currentSelection = new File(currentLocalDirectory , getTrimmedSelection());		// Selection

		if (getTrimmedSelection().equals(".."))
		{ // The [..] selected
			localSelection = null;	// No selection since directory changed
			currentSelection = currentLocalDirectory.getParentFile();
			if(currentSelection != null)
			{
				changeLocalDirectory(currentSelection);
			}
			else
			{
				localStatus.setText("You are at the root !"); 
			}
		}
		else if (currentSelection.isFile())
		{
			localSelection = currentSelection.getAbsoluteFile();
		}
		else if (currentSelection.isDirectory())
		{
			localSelection = null;	// No selection since directory changed
			changeLocalDirectory(currentSelection);
		}
	}

	/*
	 * Trims off the [] of a directory entry if it exists, else ignores it
	 * 
	 */
	private String getTrimmedSelection(){
		String currentSelection = (localFileTable.getSelectedValue().toString()).substring(1);
				if(currentSelection.substring(0,1).equals("[") &&
				currentSelection.substring(currentSelection.length()-1,currentSelection.length()).equals("]")){
				return currentSelection.substring(1,currentSelection.length()-1);
				} else {
					return currentSelection;
				}
	}

	/*
	 *  Reads the localDriveComboBox and returns the first readable drive for populating
	 *  the file table on load, so it's not looking at the A:\ drive when it opens. 
	 */
	 public File getFirstReadableLocalDrive(){
		File currentDrive;
		// sf@ - Select C: as default first readable drive
		for(int i = 0; i < localDrivesComboBox.getItemCount() ; i++)
		{
			currentDrive = new File(localDrivesComboBox.getItemAt(i).toString());
			if(localDrivesComboBox.getItemAt(i).toString().substring(0,1).toUpperCase().equals("C") && currentDrive.canRead())
			{
				localDrivesComboBox.setSelectedIndex(i);
				return currentDrive;
			}
		}
		// if C: not available, take the first readable drive, this time.
		for(int i = 0; i < localDrivesComboBox.getItemCount() ; i++)
		{
			currentDrive = new File(localDrivesComboBox.getItemAt(i).toString());
			if(currentDrive.canRead())
			{
				localDrivesComboBox.setSelectedIndex(i);
				return currentDrive;
			}
		}
		
		localStatus.setText("ERROR!: No Local Drives are Readable"); 
	 	return null;
	}
	

	/*
	 * Navigates the local file structure up or down one directory
	 */
	public void changeLocalDirectory(File dir)
	{
			currentLocalDirectory = dir;	// Updates Global
			File allFiles[] = dir.listFiles();	// Reads files
			String[] contents = dir.list();

			localList.clear();
			localList.addElement(" [..]");
			
			// Populate the Lists
			for (int i = 0; i < contents.length; i++)
			{
				if (allFiles[i].isDirectory())
					// localList.addElement("[" + contents[i] + "]");
					DirsList.add(" [" + contents[i] + "]"); // sf@2004
				else
				{
					// localList.addElement(contents[i]);
					FilesList.add(" " + contents[i]); // sf@2004
				}
			}
			// sf@2004
			for (int i = 0; i < DirsList.size(); i++) 
				localList.addElement(DirsList.get(i));
			for (int i = 0; i < FilesList.size(); i++) 
				localList.addElement(FilesList.get(i));
			
			FilesList.clear();
			DirsList.clear();
			
			localFileTable.setListData(localList);
			if(dir.toString().charAt(dir.toString().length()-1)==(File.separatorChar))
			{
				localLocation.setText(dir.toString());
			}
			else
			{
				localLocation.setText(dir.toString()+File.separator);	// Display updated location above file table
			}
			localStatus.setText("Total Files / Folders: " + (localList.size()-1));
	}
	public void mouseEntered(MouseEvent e) {
	}
	public void mouseExited(MouseEvent e) {
	}
	public void mousePressed(MouseEvent e) {
	}
	public void mouseReleased(MouseEvent e) {
	}

} //  @jve:visual-info  decl-index=0 visual-constraint="10,10"

⌨️ 快捷键说明

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