SWT (JFACE) FTP client Implementation _java programming

Source: Internet
Author: User
Tags ftp getmessage gettext null null win32 ftp client
Jar Package List:





Org.eclipse.jface_3.4.2.m20090107-0800.jar


Org.eclipse.swt.win32.win32.x86_3.4.1.v3452b.jar


Org.eclipse.core.commands_3.4.0.i20080509-2000.jar


Org.eclipse.core.runtime_3.4.0.v20080512.jar


Org.eclipse.equinox.common_3.4.0.v20080421-2006.jar


Org.eclipse.osgi_3.4.3.r34x_v20081215-1030.jar


Commons-net-ftp-2.0.jar





The demo code is as follows:





Ftpwindow.java


Copy Code code as follows:



Package swt_jface.demo13;


Import Java.io.File;


Import Java.io.FileOutputStream;


Import java.io.IOException;


Import org.apache.commons.net.ProtocolCommandEvent;


Import Org.apache.commons.net.ProtocolCommandListener;


Import org.apache.commons.net.ftp.FTPClient;


Import Org.apache.commons.net.ftp.FTPFile;


Import org.apache.commons.net.ftp.FTPReply;


Import org.eclipse.jface.action.Action;


Import Org.eclipse.jface.action.ActionContributionItem;


Import Org.eclipse.jface.action.MenuManager;


Import Org.eclipse.jface.action.Separator;


Import Org.eclipse.jface.action.ToolBarManager;


Import Org.eclipse.jface.dialogs.Dialog;


Import Org.eclipse.jface.dialogs.MessageDialog;


Import Org.eclipse.jface.resource.ImageDescriptor;


Import org.eclipse.jface.viewers.IStructuredSelection;


Import Org.eclipse.jface.viewers.TableViewer;


Import Org.eclipse.jface.window.ApplicationWindow;


Import Org.eclipse.swt.SWT;


Import Org.eclipse.swt.custom.SashForm;


Import Org.eclipse.swt.custom.StyleRange;


Import Org.eclipse.swt.custom.StyledText;


Import Org.eclipse.swt.dnd.DND;


Import Org.eclipse.swt.dnd.DragSource;


Import org.eclipse.swt.dnd.DragSourceEvent;


Import Org.eclipse.swt.dnd.DragSourceListener;


Import Org.eclipse.swt.dnd.DropTarget;


Import org.eclipse.swt.dnd.DropTargetEvent;


Import Org.eclipse.swt.dnd.DropTargetListener;


Import Org.eclipse.swt.dnd.TextTransfer;


Import Org.eclipse.swt.dnd.Transfer;


Import org.eclipse.swt.events.DisposeEvent;


Import Org.eclipse.swt.events.DisposeListener;


Import Org.eclipse.swt.graphics.Color;


Import Org.eclipse.swt.graphics.Image;


Import Org.eclipse.swt.layout.FillLayout;


Import Org.eclipse.swt.layout.GridData;


Import Org.eclipse.swt.layout.GridLayout;


Import Org.eclipse.swt.widgets.Button;


Import Org.eclipse.swt.widgets.Composite;


Import Org.eclipse.swt.widgets.Control;


Import Org.eclipse.swt.widgets.DirectoryDialog;


Import Org.eclipse.swt.widgets.Display;


Import org.eclipse.swt.widgets.Event;


Import Org.eclipse.swt.widgets.Group;


Import Org.eclipse.swt.widgets.Label;


Import Org.eclipse.swt.widgets.Listener;


Import Org.eclipse.swt.widgets.Shell;


Import org.eclipse.swt.widgets.Table;


Import Org.eclipse.swt.widgets.TableColumn;


public class Ftpwindow extends Applicationwindow {





Tableviewer Localdirbrowser;


Tableviewer Remotedirbrowser;


Label labelpathlocal;


Label Labelpathremote;


Styledtext Textlog;


Connectiondialog Connectiondialog;


Action Actionuplocaldir;


Action Actionupremotedir;


Action Actionbrowselocaldir;


Action Actionconnect;


Action Actiondisconnect;


Action actiondisplayabout;


Action Actionexit;


FtpClient ftp;


ConnectionInfo ConnectionInfo;


Public Ftpwindow (Shell Parentshell) {


Super (Parentshell);


Createactions ();


Addstatusline ();


Addcoolbar (SWT. FLAT | Swt. right);


Addtoolbar (SWT. FLAT);


Addmenubar ();


FTP = new FtpClient ();


Ftp.addprotocolcommandlistener (New Protocolcommandlistener () {


public void Protocolcommandsent (Protocolcommandevent e) {


LogMessage (">" + E.getcommand (), false);


}


public void protocolreplyreceived (Protocolcommandevent e) {


LogMessage ("<" + E.getmessage (), false);


}


});


}


private void Createactions () {


Actionuplocaldir = new Action () {


public void Run () {


if (localdirbrowser.getinput () = null)


Return


File Dir = ((File) Localdirbrowser.getinput ()). Getparentfile ();


if (dir!= null) {


Localdirbrowser.setinput (dir);


Labelpathlocal.settext ("Path:" + dir);


}


}


};


Actionuplocaldir.settext ("Up");


Actionuplocaldir.settooltiptext ("Up one level-local dir");


Actionuplocaldir.setimagedescriptor (Imagedescriptor.createfromfile (null, "C:/icons/ftp/up.gif"));


Actionbrowselocaldir = new Action () {


public void Run () {


Directorydialog dialog = new Directorydialog (Getshell ());


String path = Dialog.open ();


if (path = = null)


Return


File File = new file (path);


Localdirbrowser.setinput (file);


Labelpathlocal.settext ("Path:" + file);


}


};


Actionbrowselocaldir.settext ("Browse ...");


Actionbrowselocaldir.settooltiptext ("Browse local Directory");


Actionbrowselocaldir.setimagedescriptor (Imagedescriptor.createfromfile (null, "C:/icons/ftp/browse.gif"));


Actionconnect = new Action () {


public void Run () {


if (Connectiondialog = null)


Connectiondialog = new Connectiondialog (ftpwindow.this);


if (connectiondialog.open () = = Dialog.ok) {


ConnectionInfo = Connectiondialog.getconnectioninfo ();


if (ConnectionInfo = = null) {


Logerror ("Failed to get connection information.");


} else {


LogMessage ("Connecting to" + Connectioninfo.host, True);


try {


Ftp.connect (


Connectioninfo.host,


Connectioninfo.port);


if (! Ftpreply.ispositivecompletion (Ftp.getreplycode ()))


throw new RuntimeException ("FTP server refused connection.");


LogMessage ("Connected to" + Connectioninfo.host, True);


catch (Exception e) {


Logerror (E.tostring ());


Return


}


try {


if (FTP


. Login (


Connectioninfo.username,


Connectioninfo.password)) {


LogMessage ("Logged in as User:" + connectioninfo.username, True);


}


Labelpathremote.settext ("Path:" + ftp.printworkingdirectory ());


Lists files.


ftpfile[] files = ftp.listfiles ();


Remotedirbrowser.setinput (files);


catch (IOException E1) {


Logerror (E1.getmessage ());


try {


Ftp.disconnect ();


catch (IOException E2) {


//


}


}


}


}


}


};


Actionconnect.settext ("Connect");


Actionconnect.settooltiptext ("Connect to remote host");


Actionconnect.setimagedescriptor (Imagedescriptor.createfromfile (null, "C:/icons/ftp/connect.gif"));


Actiondisconnect = new Action () {


public void Run () {


try {


Ftp.logout ();


Ftp.disconnect ();


}catch (Exception e) {


Logerror (E.tostring ());


}


}


};


Actiondisconnect.settext ("Disconnect");


Actiondisconnect.settooltiptext ("Disconnect from remote host");


Actiondisconnect.setimagedescriptor (Imagedescriptor.createfromfile (null, "C:/icons/ftp/stop.gif"));


Actionupremotedir = new Action () {


public void Run () {


try {


if (Ftp.changetoparentdirectory ()) {


Labelpathremote.settext ("Path:" + ftp.printworkingdirectory ());


ftpfile[] files = ftp.listfiles ();


Remotedirbrowser.setinput (files);


}


catch (Exception e) {


Logerror (E.tostring ());


}


}


};


Actionupremotedir.settext ("Up");


Actionupremotedir.settooltiptext ("Up one level-remote dir");


Actionupremotedir.setimagedescriptor (Imagedescriptor.createfromfile (null, "C:/icons/ftp/up.gif"));





Actiondisplayabout = new Action () {


public void Run () {


Messagedialog.openinformation (Getshell (), "about", "FTP Client v1.0\nall right reserved by Jack Li Guojie.");


}


};


Actiondisplayabout.settext ("about");


Actiondisplayabout.setimagedescriptor (Imagedescriptor.createfromfile (null, "C:/icons/ftp/about.gif"));





Actionexit = new Action () {


public void Run () {


if (! Messagedialog.openconfirm (Getshell (), "Confirm", "Are you sure to exit?")


Return


try {


Ftp.disconnect ();


}catch (Exception e) {


}


Close ();


}


};


Actionexit.settext ("Exit");


Actionexit.setimagedescriptor (Imagedescriptor.createfromfile (null, "C:/icons/ftp/close.gif"));





}


private void Dragndropsupport () {


int operations = DND. drop_copy | Dnd. Drop_move;


Final DragSource DragSource = new DragSource (Remotedirbrowser.getcontrol (), operations);


transfer[] formats = new transfer[] {texttransfer.getinstance ()};


Dragsource.settransfer (formats);


Dragsource.adddraglistener (New Dragsourcelistener () {


public void DragStart (Dragsourceevent event) {


System.out.println ("DND starts");


Istructuredselection selection = (istructuredselection) remotedirbrowser.getselection ();


Ftpfile file = (ftpfile) selection.getfirstelement ();


if (file = = NULL | | file.isdirectory ()) {


Event.doit = false;


}


}


public void Dragsetdata (Dragsourceevent event) {





if (Texttransfer.getinstance (). Issupportedtype (Event.datatype)) {


Istructuredselection selection = (istructuredselection) remotedirbrowser.getselection ();


Ftpfile file = (ftpfile) selection.getfirstelement ();


if (file = = NULL | | file.isdirectory ()) {


Event.doit = false;


} else {


Event.data = File.getname ();


}


}


}


public void dragfinished (Dragsourceevent event) {


}


});


Remotedirbrowser


. GetControl ()


. Adddisposelistener (New Disposelistener () {


public void widgetdisposed (Disposeevent e) {


Dragsource.dispose ();


}


});


Final DropTarget droptarget = new DropTarget (Localdirbrowser.getcontrol (), operations);


Droptarget.settransfer (formats);


Droptarget.adddroplistener (New Droptargetlistener () {


public void DragEnter (Droptargetevent event) {


}


public void DragLeave (Droptargetevent event) {


}


public void Dragoperationchanged (Droptargetevent event) {


}


public void DragOver (Droptargetevent event) {


}


public void Drop (Droptargetevent event) {


if (Texttransfer.getinstance (). Issupportedtype (Event.currentdatatype)) {


String text = (string) event.data;


File target = new file ((File) localdirbrowser.getinput (), text);


if (target.exists ()) {


if (! Messagedialog.openconfirm (Getshell (), "overwriting confirmation", "Overwrite file" + target + "?")) {


Return


}


}





try {


FileOutputStream stream = new FileOutputStream (target);


if (Ftp.retrievefile (text, stream)) {


LogMessage ("File retrieved successfully.", true);


Localdirbrowser.refresh ();


}else{


Logerror ("Failed to retrieve file:" + text);


}


Stream.Close ();


catch (IOException e) {


E.printstacktrace ();


}


}


}


public void Dropaccept (Droptargetevent event) {


}


});


Localdirbrowser.getcontrol (). Adddisposelistener (New Disposelistener () {


public void widgetdisposed (Disposeevent e) {


Droptarget.dispose ();


}


});


}


Protected Menumanager Createmenumanager () {





Menumanager bar = new Menumanager ();


Menumanager menufile = new Menumanager ("&file");


Menufile.add (Actionconnect);


Menufile.add (Actiondisconnect);


Menufile.add (New Separator ());


Menufile.add (Actionexit);


Menumanager menulocal = new Menumanager ("&local");


Menulocal.add (Actionbrowselocaldir);


Menulocal.add (Actionuplocaldir);


Menumanager menuremote = new Menumanager ("&remote");


Menuremote.add (Actionupremotedir);


Menumanager menuhelp = new Menumanager ("&help");


Menuhelp.add (actiondisplayabout);


Bar.add (Menufile);


Bar.add (menulocal);


Bar.add (Menuremote);


Bar.add (MENUHELP);


Bar.updateall (TRUE);


return bar;


}


public static void Addaction (





Toolbarmanager Manager,


Action Action,


Boolean DisplayText) {


if (!displaytext) {


Manager.add (action);


Return


} else {


Actioncontributionitem item = new Actioncontributionitem (action);


Item.setmode (Actioncontributionitem.mode_force_text);


Manager.add (item);


}


}


protected Toolbarmanager Createtoolbarmanager (int style) {





Toolbarmanager manager = Super.createtoolbarmanager (style);


Addaction (Manager, Actionconnect, true);


Addaction (Manager, Actiondisconnect, true);


Manager.add (New Separator ());


Addaction (Manager, Actionbrowselocaldir, true);


Addaction (Manager, Actionuplocaldir, true);


Manager.add (New Separator ());


Addaction (Manager, Actionupremotedir, true);


Manager.add (New Separator ());


Addaction (Manager, Actiondisplayabout, true);


Addaction (Manager, Actionexit, true);


Manager.update (TRUE);





Return manager;


}


Protected control createcontents (composite parent) {


Composite composite = new Composite (parent, SWT. NULL);


Composite.setlayout (New Filllayout ());


Sashform verticalform = new Sashform (composite, SWT. VERTICAL);


Sashform horizontalform = new Sashform (Verticalform, SWT. horizontal);


Composite Compositelocaldir = new Composite (Horizontalform, SWT. NULL);


GridLayout GridLayout = new GridLayout ();


gridlayout.horizontalspacing = 1;


gridlayout.verticalspacing = 1;


Compositelocaldir.setlayout (gridLayout);


Group Compositelocaldirtop = new Group (Compositelocaldir, SWT. NULL);


Compositelocaldirtop.settext ("local");


GridLayout gridLayout2 = new GridLayout (3, false);


gridlayout2.marginheight = 0;


Compositelocaldirtop.setlayout (GRIDLAYOUT2);


Compositelocaldirtop.setlayoutdata (New Griddata (griddata.fill_horizontal));


labelpathlocal = new Label (Compositelocaldirtop, SWT. NULL);


Labelpathlocal.setlayoutdata (New Griddata (griddata.fill_horizontal));


Labelpathlocal.settext ("Path:");


Button Buttonuplocaldir = New button (Compositelocaldirtop, SWT. PUSH);


Buttonuplocaldir.settext (Actionuplocaldir.gettext ());


Buttonuplocaldir.addlistener (SWT. Selection, New Listener () {


public void Handleevent (event event) {


Actionuplocaldir.run ();


}


});


Button Buttonbrowselocaldir = New button (Compositelocaldirtop, SWT. PUSH);


Buttonbrowselocaldir.settext (Actionbrowselocaldir.gettext ());


Buttonbrowselocaldir.addlistener (SWT. Selection, New Listener () {


public void Handleevent (event event) {


Actionbrowselocaldir.run ();


}


});


Table table = new Table (Compositelocaldir, SWT. BORDER);


TableColumn tcfile = new TableColumn (table, SWT. left);


Tcfile.settext ("Name");


TableColumn tcsize = new TableColumn (table, SWT. NULL);


Tcsize.settext ("Size");


TableColumn tcdate = new TableColumn (table, SWT. NULL);


Tcdate.settext ("Date");


Tcfile.setwidth (200);


Tcsize.setwidth (100);


Tcdate.setwidth (100);


Table.setheadervisible (TRUE);


Table.setlayoutdata (New Griddata (Griddata.fill_both));


Localdirbrowser = new localdirectorybrowser (table);


Table.addlistener (SWT. MouseDoubleClick, New Listener () {


public void Handleevent (event event) {


Istructuredselection selection = (istructuredselection) localdirbrowser.getselection ();


File File = (file) selection.getfirstelement ();


if (file!= null && file.isdirectory ()) {


Localdirbrowser.setinput (file);


Labelpathlocal.settext ("Path:" + file);


}


}


});


Composite Compositeremotedir = new Composite (Horizontalform, SWT. NULL);


GridLayout = new GridLayout ();


gridlayout.horizontalspacing = 1;


gridlayout.verticalspacing = 1;


Compositeremotedir.setlayout (gridLayout);


Group Compositeremotedirtop = new Group (Compositeremotedir, SWT. NULL);


Compositeremotedirtop.settext ("Remote");


GridLayout2 = new GridLayout (2, false);


gridlayout2.marginheight = 0;


Compositeremotedirtop.setlayout (GRIDLAYOUT2);


Compositeremotedirtop.setlayoutdata (New Griddata (griddata.fill_horizontal));


Labelpathremote = new Label (Compositeremotedirtop, SWT. NULL);


Labelpathremote.setlayoutdata (New Griddata (griddata.fill_horizontal));


Labelpathremote.settext ("Path:");


Button Buttonupremotedir = New button (Compositeremotedirtop, SWT. PUSH);


Buttonupremotedir.settext (Actionuplocaldir.gettext ());


Buttonupremotedir.addlistener (SWT. Selection, New Listener () {


public void Handleevent (event event) {


Actionupremotedir.run ();


}


});


Table Tableremote = new Table (Compositeremotedir, SWT. BORDER);


TableColumn tcfileremote = new TableColumn (Tableremote, SWT. left);


Tcfileremote.settext ("Name");


TableColumn tcsizeremote = new TableColumn (Tableremote, SWT. NULL);


Tcsizeremote.settext ("Size");


TableColumn tcdateremote = new TableColumn (Tableremote, SWT. NULL);


Tcdateremote.settext ("Date");


Tcfileremote.setwidth (200);


Tcsizeremote.setwidth (100);


Tcdateremote.setwidth (100);


Tableremote.setheadervisible (TRUE);


Tableremote.setlayoutdata (New Griddata (Griddata.fill_both));


Remotedirbrowser = new Remotedirectorybrowser (tableremote);


Tableremote.addlistener (SWT. MouseDoubleClick, New Listener () {


public void Handleevent (event event) {


Istructuredselection selection = (istructuredselection) remotedirbrowser.getselection ();


Ftpfile file = (ftpfile) selection.getfirstelement ();


if (file!= null && file.isdirectory ()) {


try {


Ftp.changeworkingdirectory (File.getname ());


Labelpathremote.settext ("Path:" + ftp.printworkingdirectory ());


Remotedirbrowser.setinput (Ftp.listfiles ());


catch (IOException e) {


Logerror (E.tostring ());


}


}


}


});


Textlog =


New Styledtext (


Verticalform,


Swt. BORDER | Swt. H_scroll | Swt. V_scroll);


Localdirbrowser.setinput (File.listroots () [0]);


Labelpathlocal.settext ("Path:" + file.listroots () [0]);


Verticalform.setweights (New int[]{4, 1});


Dragndropsupport ();


Gettoolbarcontrol (). SetBackground (New Color (Getshell (). Getdisplay (), 230, 230, 230));


Getshell (). SetImage (New Image (Getshell (). Getdisplay (), "c:/icons/ftp/ftp.gif"));


Getshell (). SetText ("FTP Client v1.0");





return composite;


}


private void LogMessage (String message, Boolean Showinstatusbar) {


Stylerange styleRange1 = new Stylerange ();


Stylerange1.start = Textlog.getcharcount ();


Stylerange1.length = Message.length ();


Stylerange1.foreground = Getshell (). Getdisplay (). Getsystemcolor (SWT. Color_dark_green);


Stylerange1.fontstyle = SWT. NORMAL;


Textlog.append (message + "\ r \ n");


Textlog.setstylerange (STYLERANGE1);


Textlog.setselection (Textlog.getcharcount ());


if (Showinstatusbar) {


SetStatus (message);


}


}


private void Logerror (String message) {


Stylerange styleRange1 = new Stylerange ();


Stylerange1.start = Textlog.getcharcount ();


Stylerange1.length = Message.length ();


Stylerange1.foreground = Getshell (). Getdisplay (). Getsystemcolor (SWT. color_dark_red);


Stylerange1.fontstyle = SWT. NORMAL;


Textlog.append (message + "\ r \ n");


Textlog.setstylerange (STYLERANGE1);


Textlog.setselection (Textlog.getcharcount ());


}


public static void Main (string[] args) {


Applicationwindow window = new Ftpwindow (null);


Window.setblockonopen (TRUE);


window.open ();


Display.getcurrent (). Dispose ();


}


}





Connectiondialog.java


Copy Code code as follows:



Package swt_jface.demo13;


Import Java.io.File;


Import Org.eclipse.jface.dialogs.Dialog;


Import org.eclipse.jface.dialogs.DialogSettings;


Import Org.eclipse.swt.SWT;


Import Org.eclipse.swt.layout.GridData;


Import Org.eclipse.swt.layout.GridLayout;


Import Org.eclipse.swt.widgets.Composite;


Import Org.eclipse.swt.widgets.Control;


Import Org.eclipse.swt.widgets.Label;


Import Org.eclipse.swt.widgets.Text;


Class ConnectionInfo {


Public String host;


public int port;


public String password;


Public String username;





}


public class Connectiondialog extends Dialog {





private static final String Dialog_setting_file = "Ftp.connection.xml";


private static final String Key_host = "HOST";


private static final String Key_port = "PORT";


private static final String Key_username = "USER";


private static final String Key_password = "PASSWORD";





Text Texthost;


Text Textport;


Text Textusername;


Text Textpassword;





Dialogsettings dialogsettings;


ConnectionInfo ConnectionInfo;





Connectiondialog (Ftpwindow window) {





Super (Window.getshell ());


ConnectionInfo = null;





Dialogsettings = new Dialogsettings ("FTP");


try {


Dialogsettings.load (Dialog_setting_file);


}catch (Exception e) {


E.printstacktrace ();


}


}


Protected control Createdialogarea (composite parent) {


Getshell (). SetText ("Connection Settings");





Composite composite = (composite) super.createdialogarea (parent);


Composite.setlayout (New GridLayout (2, false));





New Label (composite, SWT. NULL). SetText ("Host:");


Texthost = new Text (composite, SWT. BORDER);


Texthost.setlayoutdata (New Griddata (griddata.fill_horizontal));





New Label (composite, SWT. NULL). SetText ("Port:");


Textport = new Text (composite, SWT. BORDER);


Textport.setlayoutdata (New Griddata (griddata.fill_horizontal));





New Label (composite, SWT. NULL). SetText ("Username:");


Textusername = new Text (composite, SWT. BORDER);


Textusername.setlayoutdata (New Griddata (griddata.fill_horizontal));





New Label (composite, SWT. NULL). SetText ("Password:");


Textpassword = new Text (composite, SWT. PASSWORD | Swt. BORDER);


Textpassword.setlayoutdata (New Griddata (griddata.fill_horizontal));


try {


Texthost.settext (Dialogsettings.get (key_host));


Textport.settext (Dialogsettings.getint (key_port) + "");


Textusername.settext (Dialogsettings.get (key_username));


Textpassword.settext (Dialogsettings.get (Key_password));


}catch (Exception e) {


}


return composite;


}


Public ConnectionInfo Getconnectioninfo () {


return connectioninfo;


}


protected void okpressed () {


try {


if (! New File (Dialog_setting_file). Exists ()) {


New File (Dialog_setting_file). CreateNewFile ();


}


Dialogsettings.put (Key_host, Texthost.gettext ());


Dialogsettings.put (Key_port, Integer.parseint (Textport.gettext (). Trim ()));


Dialogsettings.put (Key_username, Textusername.gettext ());


Dialogsettings.put (Key_password, Textpassword.gettext ());


Dialogsettings.save (Dialog_setting_file);


}catch (Exception e) {


E.printstacktrace ();


}


ConnectionInfo = new ConnectionInfo ();


Connectioninfo.host = Texthost.gettext ();


Connectioninfo.port = Integer.parseint (Textport.gettext (). Trim ());


Connectioninfo.username = Textusername.gettext ();


Connectioninfo.password = Textpassword.gettext ();


Super.okpressed ();


}


}





Fileiconutil.java


Copy Code code as follows:



Package swt_jface.demo13;


Import Java.io.File;


Import Org.apache.commons.net.ftp.FTPFile;


Import Org.eclipse.jface.resource.ImageRegistry;


Import Org.eclipse.swt.graphics.Image;


Import Org.eclipse.swt.graphics.ImageData;


Import Org.eclipse.swt.program.Program;


Import Org.eclipse.swt.widgets.Display;


public class Fileiconutil {





Static Imageregistry Imageregistry;





Static Image Iconfolder;


Static Image IconFile;





static {


Iconfolder = new Image (Display.getcurrent (), "c:/icons/folder.gif");


IconFile = new Image (Display.getcurrent (), "c:/icons/file.gif");


}


public static Image GetIcon (file file) {


if (File.isdirectory ()) return iconfolder;


int lastdotpos = File.getname (). IndexOf ('. ');


if (Lastdotpos = = 1) return iconfile;


Image image = GetIcon (File.getname (). substring (Lastdotpos + 1));


return image = = null? Iconfile:image;


}


public static Image GetIcon (Ftpfile file) {


if (File.isdirectory ()) return iconfolder;


int lastdotpos = File.getname (). IndexOf ('. ');


if (Lastdotpos = = 1) return iconfile;


Image image = GetIcon (File.getname (). substring (Lastdotpos + 1));


return image = = null? Iconfile:image;


}


private static Image GetIcon (String extension) {


if (imageregistry = = null) Imageregistry = new Imageregistry ();


Image image = imageregistry.get (extension);


if (image!= null) return image;


Program program = program.findprogram (extension);


ImageData ImageData = (program = = null null:program.getImageData ());


if (ImageData!= null) {


Image = New Image (Display.getcurrent (), imagedata);


Imageregistry.put (extension, image);


} else {


image = IconFile;


}


return image;


}


}





Localdirectorybrowser.java





Copy Code code as follows:



Package swt_jface.demo13;


Import Java.io.File;


Import Java.util.Date;


Import Org.eclipse.jface.viewers.ILabelProviderListener;


Import Org.eclipse.jface.viewers.IStructuredContentProvider;


Import Org.eclipse.jface.viewers.ITableLabelProvider;


Import Org.eclipse.jface.viewers.TableViewer;


Import Org.eclipse.jface.viewers.Viewer;


Import Org.eclipse.swt.graphics.Image;


Import org.eclipse.swt.widgets.Table;


public class Localdirectorybrowser extends Tableviewer {


Public localdirectorybrowser (table table) {


Super (table);


Init ();


}





private void init () {


Setcontentprovider (New Istructuredcontentprovider () {


Public object[] GetElements (Object inputelement) {


File dir = (file) inputelement;


return Dir.listfiles ();


}


public void Dispose () {


}


public void inputchanged (


Viewer Viewer,


Object Oldinput,


Object newinput) {


}


});





Setlabelprovider (New Itablelabelprovider () {


Public Image Getcolumnimage (Object element, int columnindex) {


if (columnindex = 0)


Return Fileiconutil.geticon ((File) element);


return null;


}


Public String Getcolumntext (Object element, int columnindex) {


Switch (columnindex) {


Case 0:


Return ((File) Element). GetName ();


Case 1:


Return ((File) Element). Length () + "";


Case 2:


return new Date ((File) Element). LastModified ()). ToString ();


Default


Return "";


}


}


public void AddListener (Ilabelproviderlistener listener) {


}


public void Dispose () {


}


public boolean Islabelproperty (Object element, String property) {


return false;


}


public void RemoveListener (Ilabelproviderlistener listener) {


}


});


}


}


Package swt_jface.demo13;


Import Java.io.File;


Import Java.util.Date;


Import Org.eclipse.jface.viewers.ILabelProviderListener;


Import Org.eclipse.jface.viewers.IStructuredContentProvider;


Import Org.eclipse.jface.viewers.ITableLabelProvider;


Import Org.eclipse.jface.viewers.TableViewer;


Import Org.eclipse.jface.viewers.Viewer;


Import Org.eclipse.swt.graphics.Image;


Import org.eclipse.swt.widgets.Table;


public class Localdirectorybrowser extends Tableviewer {


Public localdirectorybrowser (table table) {


Super (table);


Init ();


}





private void init () {


Setcontentprovider (New Istructuredcontentprovider () {


Public object[] GetElements (Object inputelement) {


File dir = (file) inputelement;


return Dir.listfiles ();


}


public void Dispose () {


}


public void inputchanged (


Viewer Viewer,


Object Oldinput,


Object newinput) {


}


});





Setlabelprovider (New Itablelabelprovider () {


Public Image Getcolumnimage (Object element, int columnindex) {


if (columnindex = 0)


Return Fileiconutil.geticon ((File) element);


return null;


}


Public String Getcolumntext (Object element, int columnindex) {


Switch (columnindex) {


Case 0:


Return ((File) Element). GetName ();


Case 1:


Return ((File) Element). Length () + "";


Case 2:


return new Date ((File) Element). LastModified ()). ToString ();


Default


Return "";


}


}


public void AddListener (Ilabelproviderlistener listener) {


}


public void Dispose () {


}


public boolean Islabelproperty (Object element, String property) {


return false;


}


public void RemoveListener (Ilabelproviderlistener listener) {


}


});


}


}








Remotedirectorybrowser.java





Copy Code code as follows:



Package swt_jface.demo13;


Import Java.util.Calendar;


Import Org.apache.commons.net.ftp.FTPFile;


Import Org.eclipse.jface.viewers.ILabelProviderListener;


Import Org.eclipse.jface.viewers.IStructuredContentProvider;


Import Org.eclipse.jface.viewers.ITableLabelProvider;


Import Org.eclipse.jface.viewers.TableViewer;


Import Org.eclipse.jface.viewers.Viewer;


Import Org.eclipse.swt.graphics.Image;


Import org.eclipse.swt.widgets.Table;


public class Remotedirectorybrowser extends Tableviewer {


Public remotedirectorybrowser (table table) {


Super (table);


Init ();


}





private void init () {


Setcontentprovider (New Istructuredcontentprovider () {


Public object[] GetElements (Object inputelement) {


ftpfile[] files = (ftpfile[]) inputelement;


return files;


}


public void Dispose () {


}


public void inputchanged (


Viewer Viewer,


Object Oldinput,


Object newinput) {


}


});





Setlabelprovider (New Itablelabelprovider () {


Public Image Getcolumnimage (Object element, int columnindex) {


if (columnindex = 0)


Return Fileiconutil.geticon ((ftpfile) element);


return null;


}


Public String Getcolumntext (Object element, int columnindex) {


Switch (columnindex) {


Case 0:


Return ((ftpfile) Element). GetName ();


Case 1:


Return ((ftpfile) Element). GetSize () + "";


Case 2:


Calendar cal = ((ftpfile) Element). Gettimestamp ();


Return Cal.get (calendar.year) + "-" +


Cal.get (Calendar.month) + "-" +


Cal.get (Calendar.day_of_month) + "" +


Cal.get (Calendar.hour_of_day) + ":" +


Cal.get (Calendar.minute) + ":" +


Cal.get (Calendar.second);


Default


Return "";


}


}


public void AddListener (Ilabelproviderlistener listener) {


}


public void Dispose () {


}


public boolean Islabelproperty (Object element, String property) {


return false;


}


public void RemoveListener (Ilabelproviderlistener listener) {


}


});


}





}





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.