FTP client : Small Application « SWT JFace Eclipse « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » SWT JFace Eclipse » Small ApplicationScreenshots 
FTP client

/*******************************************************************************
 * All Right Reserved. Copyright (c) 1998, 2004 Jackwind Li Guojie
 
 * Created on 2004-7-14 7:19:55 by JACK $Id$
 *  
 ******************************************************************************/

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;

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;

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;
import org.eclipse.swt.widgets.Text;

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;

class ConnectionInfo {
  public String host;

  public int port;

  public String password;

  public String username;

}
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();
      // ignore.
    }
  }

  /*
   * (non-Javadoc)
   
   * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
   */
  protected Control createDialogArea(Composite parent) {
    getShell().setText("Connection Settings");

    Composite composite = (Compositesuper.createDialogArea(parent);
    composite.setLayout(new GridLayout(2false));

    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));

    // sets initial values.
    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) {
      // ignore.
    }

    return composite;
  }

  /**
   * Returns a ConnectionInfo object containing connection information.
   
   @return
   */
  public ConnectionInfo getConnectionInfo() {
    return connectionInfo;
  }

  /*
   * (non-Javadoc)
   
   * @see org.eclipse.jface.dialogs.Dialog#okPressed()
   */
  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();
      // ignore
    }

    connectionInfo = new ConnectionInfo();
    connectionInfo.host = textHost.getText();
    connectionInfo.port = Integer.parseInt(textPort.getText().trim());
    connectionInfo.username = textUsername.getText();
    connectionInfo.password = textPassword.getText();

    super.okPressed();
  }

}

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;

  /**
   @param parentShell
   */
  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() {
    // Up one level - local dir
    actionUpLocalDir = new Action() {
      public void run() {
        if (localDirBrowser.getInput() == null)
          return;
        File dir = ((FilelocalDirBrowser.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, "icons/ftp/up.gif"));

    // browse for local dir
    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, "icons/ftp/browse.gif"));

    // connect
    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 {
            // connects to remote host.
            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 {
              // logins in.
              if (ftp.login(connectionInfo.username,
                  connectionInfo.password)) {
                logMessage("Logged in as user: "
                    + connectionInfo.username, true);
              }
              // gets current working directory.
              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,
        "icons/ftp/connect.gif"));

    // disconnect
    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, "icons/ftp/stop.gif"));

    // up one level - remote dir.
    actionUpRemoteDir = new Action() {
      public void run() {
        try {
          if (ftp.changeToParentDirectory()) {
            // gets current working directory.
            labelPathRemote.setText("Path: "
                + ftp.printWorkingDirectory());

            // Lists files.
            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, "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, "icons/ftp/about.gif"));

    actionExit = new Action() {
      public void run() {
        if (!MessageDialog.openConfirm(getShell()"Confirm",
            "Are you sure you want to exit?"))
          return;
        try {
          ftp.disconnect();
        catch (Exception e) {
          // ignore.
        }
        close();
      }
    };
    actionExit.setText("Exit");
    actionExit.setImageDescriptor(ImageDescriptor.createFromFile(null,
        "icons/ftp/close.gif"));

  }

  private void dragNDropSupport() {
    // --- Drag source ---

    //  Allows text to be moved only.
    int operations = DND.DROP_COPY | DND.DROP_MOVE;
    final DragSource dragSource = new DragSource(remoteDirBrowser
        .getControl(), operations);

    // Data should be transfered in plain text format.
    Transfer[] formats = new Transfer[] { TextTransfer.getInstance() };
    dragSource.setTransfer(formats);

    dragSource.addDragListener(new DragSourceListener() {
      public void dragStart(DragSourceEvent event) {
        System.out.println("DND starts");
        // disallows DND if no remote file is selected.
        IStructuredSelection selection = (IStructuredSelectionremoteDirBrowser
            .getSelection();
        FTPFile file = (FTPFileselection.getFirstElement();
        if (file == null || file.isDirectory()) {
          event.doit = false;
        }
      }

      public void dragSetData(DragSourceEvent event) {
        // Provides the text data.
        if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
          IStructuredSelection selection = (IStructuredSelectionremoteDirBrowser
              .getSelection();
          FTPFile file = (FTPFileselection.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();
      }
    });

    // --- Drop target ---
    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 = (Stringevent.data;
          File target = new File((FilelocalDirBrowser.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);
              // refreshes the file list.
              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();
      }
    });
  }

  /*
   * (non-Javadoc)
   
   * @see org.eclipse.jface.window.ApplicationWindow#createMenuManager()
   */
  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);
    }
  }

  /*
   * (non-Javadoc)
   
   * @see org.eclipse.jface.window.ApplicationWindow#createToolBarManager(int)
   */
  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;
  }

  /*
   * (non-Javadoc)
   
   * @see org.eclipse.jface.window.Window#createContents(org.eclipse.swt.widgets.Composite)
   */
  protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NULL);
    composite.setLayout(new FillLayout());

    // the vertical sashform.
    SashForm verticalForm = new SashForm(composite, SWT.VERTICAL);

    // the horizontal sashform.
    SashForm horizontalForm = new SashForm(verticalForm, SWT.HORIZONTAL);

    // Local dir browser.
    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(3false);
    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 = (IStructuredSelectionlocalDirBrowser
            .getSelection();
        File file = (Fileselection.getFirstElement();
        if (file != null && file.isDirectory()) {
          localDirBrowser.setInput(file);
          labelPathLocal.setText("Path: " + file);
        }
      }
    });

    // Remote directory browser.

    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(2false);
    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 = (IStructuredSelectionremoteDirBrowser
            .getSelection();
        FTPFile file = (FTPFileselection.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());
          }
        }
      }
    });

    // the log box.
    textLog = new StyledText(verticalForm, SWT.BORDER | SWT.H_SCROLL
        | SWT.V_SCROLL);

    localDirBrowser.setInput(File.listRoots()[0]);
    labelPathLocal.setText("Path: " + File.listRoots()[0]);

    // resize sashform children.
    verticalForm.setWeights(new int[] { 4});

    // adding drag and drop support.
    dragNDropSupport();

    getToolBarControl().setBackground(
        new Color(getShell().getDisplay()230230230));

    getShell().setImage(
        new Image(getShell().getDisplay()"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();
  }
}

/*******************************************************************************
 * All Right Reserved. Copyright (c) 1998, 2004 Jackwind Li Guojie
 
 * Created on 2004-7-14 7:42:05 by JACK $Id$
 *  
 ******************************************************************************/

class LocalDirectoryBrowser extends TableViewer {

  /**
   @param table
   */
  public LocalDirectoryBrowser(Table table) {
    super(table);
    init();
  }

  private void init() {
    // the content provider.
    setContentProvider(new IStructuredContentProvider() {
      public Object[] getElements(Object inputElement) {
        File dir = (FileinputElement;
        return dir.listFiles();
      }

      public void dispose() {
      }

      public void inputChanged(Viewer viewer, Object oldInput,
          Object newInput) {
      }
    });

    // the label provider.
    setLabelProvider(new ITableLabelProvider() {
      public Image getColumnImage(Object element, int columnIndex) {
        if (columnIndex == 0)
          return FileIconUtil.getIcon((Fileelement);
        return null;
      }

      public String getColumnText(Object element, int columnIndex) {
        switch (columnIndex) {
        case 0:
          return ((Fileelement).getName();
        case 1:
          return ((Fileelement).length() "";
        case 2:
          return new Date(((Fileelement).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) {
      }
    });
  }

}
/*******************************************************************************
 * All Right Reserved. Copyright (c) 1998, 2004 Jackwind Li Guojie
 
 * Created on 2004-7-14 22:41:43 by JACK $Id$
 *  
 ******************************************************************************/

class RemoteDirectoryBrowser extends TableViewer {
  /**
   @param table
   */
  public RemoteDirectoryBrowser(Table table) {
    super(table);
    init();
  }

  private void init() {
    // the content provider.
    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) {
      }
    });

    // the label provider.
    setLabelProvider(new ITableLabelProvider() {
      public Image getColumnImage(Object element, int columnIndex) {
        if (columnIndex == 0)
          return FileIconUtil.getIcon((FTPFileelement);
        return null;
      }

      public String getColumnText(Object element, int columnIndex) {
        switch (columnIndex) {
        case 0:
          return ((FTPFileelement).getName();
        case 1:
          return ((FTPFileelement).getSize() "";
        case 2:
          Calendar cal = ((FTPFileelement).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) {
      }
    });
  }

}

           
       
Related examples in the same category
1. SWT Control in One ExampleSWT Control in One Example
2. SWT Text Editor
3. Small application:you library
4. This application has save,load, sorting, and searching functions common to basic address booksThis application has save,load, sorting, and searching functions common to basic address books
5. Another Address Book Demo in SWTAnother Address Book Demo in SWT
6. SWT Shapes
7. Widget Test 2Widget Test 2
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.