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

📄 showfiles.java

📁 J2ME下开发的P2P搜索程序,功能较基础,较简单,可供学习和参考使用.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 *   Importing the basic packages required by various classes during 
 *	 the execution of the program.	
 */ 
  import com.sun.kjava.*;
  import java.lang.*;
  import javax.microedition.io.*;
  import java.io.*;
  import java.util.*;
 /**
  *  The packages below are included because these packages are 
  *	 required by the XML classes.
  */ 
 import org.kxml.*;
 import org.kxml.io.*;
 import org.kxml.parser.*;
 
//  Declaration of a class showfiles

  public class showfiles extends Spotlet 
  {

//  Declaring button variables.

  	private Button browse_directory;
  	private Button search;
  	private Button close;
// Declaring ScrollTextBox and textField variables. 
  	private ScrollTextBox filelist;
      private TextField option;	
 

  	Vector information_vector;
  	String parent_information;
  	String text_for_display;
/**
 *   Declaraing variables to folder_data where the folder information  
  *   is stored similerly the variable file_data where the file 
  *   information is stored.
  */  
  	String folder_data;
  	String file_data;
  	
  	int counter;
  	
  	String host_address;
//  Declaring StreamConnection variable used for Sockets.. 
  	StreamConnection socket = null;
//  Declaring InputStreams and OutputStreams used by the socket 
//  connections.
  	InputStream socket_inputstream;
     OutputStream socket_outputstream;

  	Vector folder_vector;
  /**
   *   Declaraing variables for the xmlparser and parse event(Where 
   *   the call backs after the parsing will be stored.	
   */  
  	AbstractXmlParser xmlparser;
  	ParseEvent event;
		// Constructor for the class showfiles..
  	showfiles(String text_for_display, Vector folder_vector, String address, String parent_information,boolean viewfiles_flag)
  	{
	// initializing the parameters with the varaiables declared 
	// earlier.
  			host_address = address;	
  			information_vector = folder_vector;
  			this.parent_information = parent_information;
	// The current graphics is obtained...	
  		Graphics graphics = Graphics.getGraphics();	
/**
 *  Initializing the variable of the graphics class and clearing   
 *	the drawing region by calling the appropriate functions of 
 *	the Graphics class namely (resetDrawRegion() and clearScreen()).
 */  

  	    graphics.resetDrawRegion();
          graphics.clearScreen();	
  		
/**
 *  Displaying a string (Title) at a specified position by using 
  *	the member function of the Graphics class
  */ 
  	    graphics.drawString("Shared Files / Folders",40,5);
  		
//	To make the spotlet as the current spotlet..
  	    register(NO_EVENT_OPTIONS);	
  
// Initializing the button object "search",placing it on the screen.
  		search = new Button("Search",10,20);
  		
/**
 *  Initializing the ScrollTextBox object filelist, placing it on 
 *	the screen at the appropriate position and then painting(drawing it).
  */ 
  		filelist = new ScrollTextBox(text_for_display,10,40,130,100);
  		filelist.paint();
//	Initializing the TextField object "option", placing it on the screen
  		option = new TextField("Dir ID:",10,145,50,10);	
  		
// Initializing the button object "browse_directory", placing it 
// on the screen.
  		browse_directory = new Button("Browse",70,145);
  		
/**
 *  Initializing the button object close, placing it on the screen 
 *	at the appropriate position and then painting(drawing it).
 */ 
  		close = new Button("Close",120,145);
  		close.paint();
// If the viewfiles_flag is false then paint the buttons initialized
// earlier else not.  	
  
          if(!viewfiles_flag)
  		{
  		search.paint();
  		browse_directory.paint();
  		option.paint();
  		option.setUpperCase(true);
         option.setFocus();
  		}
  
  	}	// End Constructor..
  	
// Event Handling function. (KeyDown).
  	  public void keyDown(int x)
         {
// Handle the input if the TextField is in focus.	
            if(option.hasFocus())
              {
                option.handleKeyDown(x);
              }
         }  // End KeyDowm
  
// Event Handling function. (penDown)..

  	  public void penDown(int x, int y)    
  	   {
// If search(Client Search) Button is pressed 
  		   if(search.pressed(x,y))
             {
// Remove the focus from the TextField and kill the TextField 
// blinking caret.
  				option.loseFocus();
  				option.killCaret();
// To show a new class fist unregister all the controls of the 
 // existing class
  				unregister();
// Call the class serverfiles with the parameters.
  				new searchfiles(host_address,parent_information);
  		   }
  
  
// If browse button is pressed then call the 
// method browse_directory() with parameter 1.
  		   if(browse_directory.pressed(x,y))
             {
  				
  				browseDirectory(1);
			    }	
 
  
// If close button is pressed.
  		   if(close.pressed(x,y))
             {
// If the parent_information is root then..
               if (parent_information.equals("ROOT"))
               {
// Remove the focus from the TextField and kill the TextField 
// blinking caret.
  				option.loseFocus();
				option.killCaret();
 // To show a new class fist unregister all the controls of the 
 // existing class
  				unregister();
 // Create an object of the class peer_application and call the 
 // function startReading() to send a request to the server
  				peer_application refresh_application = new peer_application();
  				refresh_application.startReading();
 // Then call the parser function it will return a vector.
  				Vector ip_addresses =  refresh_application.parseData();
  			    String text_for_display = refresh_application.returnTextForDisplay();
  				Vector users_connected = refresh_application.returnUsersConnected();

 // Call the class userlist_screen with the parameters obtained in the
 // previous lines.. 
  				new userlist_screen(text_for_display,ip_addresses,users_connected, false,null);
                }   // End If...
	// Else if parent_information is not root then call the method 
	// browseDirectory() with a parameter 0.
  			  else 
  			  {
  			    browseDirectory(0);
  			  }
  			  
  		   }
	// Event handling for the ScrollTextBox.
  		 
            if (filelist.contains(x,y))
  				 filelist.handlePenMove(x,y);
  	    }	// End penDown
  
	// Event handling for the ScrollTextBox.
      public void penMove(int x, int y)
      {
           if (filelist.contains(x,y))
  			filelist.handlePenMove(x,y);
      }
  
	// method browseDirectory()
  	void browseDirectory(int state)
  	{
  		if (state == 1)
  		{
	// Two boolean flags are initialized.. 
  			boolean connection_flag = false;
  			boolean directory_flag  = false;
  			String directory_name = "";
  
  			folder_vector = new Vector();
  			
  			text_for_display = "";
  			folder_data = "";
  			file_data = "";
  			counter = 1;
		// Option entered by the user is taken 
  			   String directory_number = option.getText();
  			
  			try
  			   {
		// Converted to integer..  	
  				 int directoryID = Integer.parseInt(directory_number); 
  				try
  				{
		// The ip_address of the user selected is taken out 
		// from the vector.
  				directory_name = (String)information_vector.elementAt(directoryID-1);
  					
  			   try
  			   {
	// A Socket connection is made on a port with the Listener 	
  				socket =  			  		        (StreamConnection)Connector.open("socket://"+host_address+":7070",Connector.READ_WRITE,true);	
  
	// If the socket is null then the connection is not established.
  				if (socket != null) 
  				{
  				System.out.println("Connection is established to localhost on port 7070...");
  				}
  							
  	///  Opening the Input and Output Streams to the Sockets....				 
  				
  			  socket_inputstream = socket.openInputStream();
  			  socket_outputstream = socket.openOutputStream();
  			  }
  			  catch(IOException ae)
  			  {
  			   System.out.println("Couldn't open socket:");
  			   }
	// An XMLRequest which is to be sent to the Listener is formed..
  		String xmlRequest = "<?xml version='1.0' encoding='utf-8'?> <p2p_lng><request type=\"SEARCH\"><scope type=\""+directory_name+"*.*\" mask=''></scope></request></p2p_lng>";
  							
	// It is converted to byte array
  		byte [] byteXmlRequest = xmlRequest.getBytes();
	// The length of the byte array is taken out.

⌨️ 快捷键说明

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