JAVA Implementation of simple FTP software --> Implementation of the upload/download queue window (7)

Source: Internet
Author: User

1. First, let's take a look at the queue window interface.


2. Check the upload queue window.



3. Check the download queue window.

4. Queue window implementation

Package COM. oyp. FTP. panel. queue; import static Java. AWT. borderlayout. center; import static Java. AWT. borderlayout. east; import static javax. swing. listselectionmodel. single_selection; import Java. AWT. borderlayout; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. io. file; import Java. io. ioexception; import Java. util. export list; import javax. swing. boxlayout; import javax. swing. Jbutton; import javax. swing. jpanel; import javax. swing. jscrollpane; import javax. swing. jtable; import javax. swing. jtogglebutton; import javax. swing. jtoolbar; import javax. swing. listselectionmodel; import javax. swing. timer; import javax. swing. table. defaulttablemodel; import COM. oyp. FTP. ftpclientframe; import COM. oyp. FTP. utils. ftpclient; import COM. oyp. FTP. utils. ftpfile;/*** task queue control panel */public class queuepanel Extends jpanel implements actionlistener {private jtable queuetable = new jtable (); // display the table component private jscrollpane scrollpane = new jscrollpane (); Private ftpclientframe; // reference object of the main form private string [] columns; private ftpclient; // control class private timer queuetimer of the FTP protocol; // queue timer private timer list <object []> localqueue; // upload queue private queue list <object []> ftpqueue on the local panel; // download queue PRI on the FTP panel Vate jtogglebutton stopbutton; private Boolean stop = false; // control variable of the queue/*** default constructor */Public queuepanel () {initcomponent ();} /*** custom constructor ** @ Param frame * main form */Public queuepanel (ftpclientframe frame) {This. frame = frame; // obtain the upload queue localqueue = (upload list <object []>) frame of the local panel from the main form. getlocalpanel (). getqueue (); // get the ftpqueue = (download list <object []>) frame of the FTP panel from the main form. getftppanel (). getqueue (); initc Omponent (); // initialize the form interface // create a timer to execute the queue refresh task queuetimer = new timer (1000, new actionlistener () every second () {/*** timer event processing method */@ overridepublic void actionreceivmed (actionevent e) {If (localqueue. size () + ftpqueue. size () = queuetable. getrowcount () // If the queue size does not change return; // end this method without refreshqueue (); // otherwise, refresh the table data that shows the queue information});} private void initcomponent () {borderlayout cardlayout = new borderlayout (); setlayout (Card Layout); columns = new string [] {"Task Name", "direction", "host", "execution status"}; queuetable. setmodel (New defaulttablemodel (new object [] [] {}, columns); queuetable. gettableheader (). setreorderingallowed (false); scrollpane. setviewportview (queuetable); cardlayout. layoutcontainer (scrollpane); add (scrollpane, center); jtoolbar controltool = new jtoolbar (jtoolbar. vertical); controltool. setrolover (true); controltool. setfloatable (False); jbutton upbutton = new jbutton ("Move Up"); upbutton. setactioncommand ("up"); upbutton. addactionlistener (this); jbutton downbutton = new jbutton ("Move Down"); downbutton. setactioncommand ("down"); downbutton. addactionlistener (this); stopbutton = new jtogglebutton ("pause"); stopbutton. setactioncommand ("Stop & start"); stopbutton. addactionlistener (this); jbutton delbutton = new jbutton ("delete"); delbutton. setactioncommand (" Del "); delbutton. addactionlistener (this); jbutton clearbutton = new jbutton ("clear"); clearbutton. setactioncommand ("clear"); clearbutton. addactionlistener (this); controltool. setlayout (New boxlayout (controltool, boxlayout. y_axis); controltool. add (upbutton); controltool. add (downbutton); controltool. add (stopbutton); controltool. add (delbutton); controltool. add (clearbutton); add (controltool, East);} public void star Tqueue () {ftpclient = frame. getftpclient (); queuetimer. start ();}/*** interface button event handling method */@ overridepublic void actionreceivmed (actionevent e) {final string command = E. getactioncommand (); If (command. equals ("Stop & start") {// process the pause button event if (stopbutton. isselected () {stop = true; stopbutton. settext ("continue");} else {stop = false; stopbutton. settext ("pause") ;}// process the move up and move down button event if (command. equals ("up") | command. equals (" Down ") {up_down_action (command); // call the method for processing the move-up and move-down actions} If (command. equals ("Del") {// event int ROW = queuetable for processing the delete button. getselectedrow (); // get the current row of the table that displays the queue if (row <0) return; // get the first table unit value of the selected row object valueat = queuetable. getvalueat (row, 0); // If the selected content is the object of the file class if (valueat instanceof file) {file = (File) valueat; int size = localqueue. size (); // get the size of the upload queue for (INT I = 0; I <size; I ++) {// traverse the upload queue if (localq Ueue. get (I) [0]. equals (File) {localqueue. remove (I); // delete a file object from the upload queue }}} else if (valueat instanceof string) {// if you select a String object string filestr = (string) valueat; int size = ftpqueue. size (); // get the size of the upload queue for (INT I = 0; I <size; I ++) {// traverse the upload queue // obtain the file object ftpfile = (ftpfile) ftpqueue In the upload queue. get (I) [0]; If (ftpfile. getabsolutepath (). equals (filestr) {ftpqueue. remove (I); // Delete the object from the upload queue.} refreshqueue (); // Refresh the queue list} If (command. equals ("clear") {// process the localqueue event of the clear button. clear (); // call the clear () method of the queue on the local panel ftpqueue. clear (); // call the clear () of the queue on the FTP panel () method }}/ *** handling method of the up and down actions of a queue task ** @ Param command * move up or down Command */private void up_down_action (final string command) {int ROW = queuetable. getselectedrow (); // obtain the current row number of the queue table if (row <0) return; // obtain the first unit value of the current row object valueat = queuetable. getvalueat (row, 0); // obtain the second ticket of the currently selected row The element value is used to determine the upload or download direction. String orientation = (string) queuetable. getvalueat (row, 1); If (orientation. equals ("Upload") {// if it is an upload task string value = (File) valueat ). getabsolutepath (); int size = localqueue. size (); For (INT I = 0; I <size; I ++) {// traverses the upload queue object [] Que = localqueue. get (I); file = (File) que [0]; // traverses the object if (file. getabsolutepath (). equals (value) {listselectionmodel selmodel = queuet Able. getselectionmodel (); // obtain the Selection Model selmodel of the table // set the choice mode of the Selection Model. setselectionmode (listselectionmodel. single_selection); int dsize = localqueue. size (); // get the size of the local upload queue int Drow = queuetable. getselectedrow (); // get the current row number of the queue table int queueval = localqueue. size ()-dsize; int next =-1; int selindex =-1; if (command. equals ("up") {if (I <1) // restriction Selection Range return; selindex = Drow-queueval-1; next = I-1 ;} else {if (I> = Size-1) // restriction Selection Range: return; selindex = Drow-queueval + 1; next = I + 1 ;} // exchange the task object [] temp = localqueue. get (next); localqueue. set (next, que); localqueue. set (I, temp); refreshqueue (); // refresh the list of queue tables // set the table to select the first row of selmodel. setselectioninterval (0, selindex); break ;}} else if (orientation. equals ("Download") {// if it is a download task string value = (string) valueat; int size = ftpqueue. size (); // obtain the size of the FTP download queue for (int I = 0; I <size; I ++) {// traverses the download queue object [] Que = ftpqueue. get (I); ftpfile file = (ftpfile) que [0]; // obtain the FTP file object of each download task if (file. getabsolutepath (). equals (value) {// listselectionmodel selmodel = queuetable. getselectionmodel (); // obtain the Selection Model of the task queue table // set the model to use the single-choice mode selmodel. setselectionmode (single_selection); int dsize = ftpqueue. size (); int Drow = queuetable. getselectedrow (); int queueval = ftpqueue. size ()-DSI Ze; int next =-1; int selindex =-1; if (command. equals ("up") {if (I <1) // restriction Selection Range return; selindex = Drow-queueval-1; next = I-1 ;} else {if (I> = size-1) // select the range of return; selindex = Drow-queueval + 1; next = I + 1 ;} // exchange the task content of a queue. object [] temp = ftpqueue. get (next); ftpqueue. set (next, que); ftpqueue. set (I, temp); refreshqueue (); // refresh the table list of the task queue // select the first row of the table selmodel. setselectioninterval (0, selindex ); Break ;}}}/*** Method for refreshing the queue */private synchronized void refreshqueue () {// If the automatic shutdown button is pressed and the upload and download queues have tasks if (frame. getshutdownbutton (). isselected () & localqueue. isempty () & ftpqueue. isempty () {try {// execute the system shut-down command with a delay of 30 seconds runtime.getruntime(cmd.exe C ("shutdown-s-t 30");} catch (ioexception e) {e. printstacktrace () ;}/// data model object for table creation ululttablemodel model = new defaulttablemodel (columns, 0); // obtain the task object in the local upload queue [] Localqueuearray = localqueue. toarray (); // traverses the local upload task for (INT I = 0; I <localqueuearray. length; I ++) {object [] queuevalue = (object []) localqueuearray [I]; If (queuevalue = NULL) continue; file localfile = (file) queuevalue [0]; // Add the upload queue task to the data model of the table component. addrow (new object [] {localfile. getabsolutefile (), "Upload", ftpclient. getserver (), I = 0? "Uploading": "waiting for upload"});} // obtain the task object of the download queue [] ftpqueuearray = ftpqueue. toarray (); // traverses the download queue for (INT I = 0; I <ftpqueuearray. length; I ++) {object [] queuevalue = (object []) ftpqueuearray [I]; If (queuevalue = NULL) continue; ftpfile = (ftpfile) queuevalue [0]; // Add the download queue task to the data model of the table component. addrow (new object [] {ftpfile. getabsolutepath (), "Download", ftpclient. getserver (), I = 0? "Downloading": "waiting for download"});} queuetable. setmodel (model); // set the table data model used in this method for the table} public Boolean isstop () {return stop ;}}

5. Implementation of the upload queue window

Package COM. oyp. FTP. panel. queue; import Java. AWT. cardlayout; import javax. swing. jpanel; import javax. swing. jscrollpane; import javax. swing. jtable; import javax. swing. swingutilities; import javax. swing. table. defaulttablemodel; import javax. swing. table. tablecolumn; import COM. oyp. FTP. panel. queuetablecellranderer; import COM. oyp. FTP. utils. progressarg; public class uploadpanel extends jpanel {private jtable uploadtable = new jtable (); // table component private jscrollpane scrollpane = new jscrollpane (); Private defaulttablemodel model; // table data model/*** constructor, used to initialize the program interface */Public uploadpanel () {cardlayout = new cardlayout (); setlayout (cardlayout ); progressarg = new progressarg (-1,-1,-1); Model = new defaulttablemodel (new object [] [] {new object [] {"", "", "", "", progressarg }}, New String [] {"file name", "size", "Remote File Name", "host", "status"}); uploadtable. setmodel (model); uploadtable. gettableheader (). setreorderingallowed (false); uploadtable. setrowselectionallowed (false); tablecolumn column = uploadtable. getcolumn ("status"); column. setcellrenderer (New queuetablecellranderer (); scrollpane. setviewportview (uploadtable); cardlayout. layoutcontainer (scrollpane); add (scrollpane, "queue ");} /*** Method for adding a new task to the table component of the upload queue ** @ Param values *-array object for adding a row of data to the table */Public void addrow (final object [] values) {runnable = new runnable () {public void run () {model. insertrow (0, values); // Add data to the table's Data Model }}; if (swingutilities. iseventdispatchthread () runnable. run (); // execute elseswingutilities In the event queue. invokelater (runnable); // or an event queue call }}

6. Implementation of the download queue window

Package COM. oyp. FTP. panel. queue; import Java. AWT. cardlayout; import javax. swing. jpanel; import javax. swing. jscrollpane; import javax. swing. jtable; import javax. swing. swingutilities; import javax. swing. table. defaulttablemodel; import javax. swing. table. tablecolumn; import COM. oyp. FTP. panel. queuetablecellranderer; import COM. oyp. FTP. utils. progressarg; public class downloadpanel extends jpanel {private jtable downloadtable = new jtable (); Private jscrollpane scrollpane = new jscrollpane (); Private defaulttablemodel model; Public downloadpanel () {cardlayout = new cardlayout (); setlayout (cardlayout); progressarg = new progressarg (-1,-1,-1 ); model = new defaulttablemodel (new object [] [] {new object [] {"", "", progressarg }}, new String [] {"file name", "size", "Local file name", "host", "status"}); downloadtable. setmodel (model); downloadtable. gettableheader (). setreorderingallowed (false); downloadtable. setrowselectionallowed (false); tablecolumn column = downloadtable. getcolumn ("status"); column. setcellrenderer (New queuetablecellranderer (); scrollpane. setviewportview (downloadtable); cardlayout. layoutcontainer (scrollpane); add (scrollpane, "queue");} public void addrow (final object [] values) {runnable = new runnable () {public void run () {model. insertrow (0, values) ;}}; if (swingutilities. iseventdispatchthread () runnable. run (); elseswingutilities. invokelater (runnable );}}

The following window details the changes after uploading and downloading

1. Add an upload task

2. upload task completion

3. Add a download task

4. Download task completion

========================================================== ========================================================== ============================

Author: Ouyang Peng: Welcome to repost. sharing with others is the source of progress!

Reprinted Please retain the original address: http://blog.csdn.net/ouyang_peng

========================================================== ========================================================== ==============================

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.