Command Pattern - Example: FTP GUI : Command Pattern « Design Pattern « 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 » Design Pattern » Command PatternScreenshots 
Command Pattern - Example: FTP GUI
Command Pattern - Example: FTP GUI

/*

Software Architecture Design Patterns in Java
by Partha Kuchana 

Auerbach Publications

*/



import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;

public class FTPGUI extends JFrame {
  public static final String newline = "\n";

  public static final String UPLOAD = "Upload";

  public static final String DOWNLOAD = "Download";

  public static final String DELETE "Delete";

  public static final String EXIT = "Exit";

  private JPanel pnlFTPUI;

  private JList localList;

  private JList remoteList;

  private DefaultListModel defLocalList, defRemoteList;

  private UploadButton btnUpload;

  private DownloadButton btnDownload;

  private DeleteButton btnDelete;

  public FTPGUI() throws Exception {
    super("Command Pattern - Example");

    // Create controls
    defLocalList = new DefaultListModel();
    defRemoteList = new DefaultListModel();
    localList = new JList(defLocalList);
    remoteList = new JList(defRemoteList);
    pnlFTPUI = new JPanel();

    localList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    localList.setSelectedIndex(-1);
    JScrollPane spLocalList = new JScrollPane(localList);

    remoteList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    remoteList.setSelectedIndex(-1);
    JScrollPane spRemoteList = new JScrollPane(remoteList);

    //Create Labels
    JLabel lblLocalList = new JLabel("Local List:");
    JLabel lblRemoteList = new JLabel("Remote List:");
    JLabel lblSpacer = new JLabel("         ");

    //Create buttons
    btnUpload = new UploadButton(FTPGUI.UPLOAD);
    btnUpload.setMnemonic(KeyEvent.VK_U);
    btnDownload = new DownloadButton(FTPGUI.DOWNLOAD);
    btnDownload.setMnemonic(KeyEvent.VK_N);
    btnDelete = new DeleteButton(FTPGUI.DELETE);
    btnDelete.setMnemonic(KeyEvent.VK_D);
    ExitButton btnExit = new ExitButton(FTPGUI.EXIT);
    btnExit.setMnemonic(KeyEvent.VK_X);

    buttonHandler vf = new buttonHandler();

    btnUpload.addActionListener(vf);
    btnDownload.addActionListener(vf);
    btnDelete.addActionListener(vf);
    btnExit.addActionListener(vf);

    JPanel lstPanel = new JPanel();

    GridBagLayout gridbag2 = new GridBagLayout();
    lstPanel.setLayout(gridbag2);
    GridBagConstraints gbc2 = new GridBagConstraints();
    lstPanel.add(lblLocalList);
    lstPanel.add(lblRemoteList);
    lstPanel.add(spLocalList);
    lstPanel.add(spRemoteList);
    lstPanel.add(lblSpacer);

    gbc2.gridx = 0;
    gbc2.gridy = 0;
    gridbag2.setConstraints(lblLocalList, gbc2);
    gbc2.gridx = 1;
    gbc2.gridy = 0;
    gridbag2.setConstraints(lblSpacer, gbc2);

    gbc2.gridx = 5;
    gbc2.gridy = 0;
    gridbag2.setConstraints(lblRemoteList, gbc2);
    gbc2.gridx = 0;
    gbc2.gridy = 1;
    gridbag2.setConstraints(spLocalList, gbc2);
    gbc2.gridx = 5;
    gbc2.gridy = 1;
    gridbag2.setConstraints(spRemoteList, gbc2);

    //-----------------------------------
    //For layout purposes, put the buttons in a separate panel
    JPanel buttonPanel = new JPanel();

    //----------------------------------------------
    GridBagLayout gridbag = new GridBagLayout();
    buttonPanel.setLayout(gridbag);
    GridBagConstraints gbc = new GridBagConstraints();
    buttonPanel.add(lstPanel);
    buttonPanel.add(btnUpload);
    buttonPanel.add(btnDownload);
    buttonPanel.add(btnDelete);
    buttonPanel.add(btnExit);

    gbc.insets.top = 5;
    gbc.insets.bottom = 5;
    gbc.insets.left = 5;
    gbc.insets.right = 5;

    gbc.anchor = GridBagConstraints.WEST;
    gbc.gridx = 1;
    gbc.gridy = 0;
    gridbag.setConstraints(btnUpload, gbc);
    gbc.gridx = 2;
    gbc.gridy = 0;
    gridbag.setConstraints(btnDownload, gbc);
    gbc.gridx = 3;
    gbc.gridy = 0;
    gridbag.setConstraints(btnDelete, gbc);
    gbc.gridx = 4;
    gbc.gridy = 0;
    gridbag.setConstraints(btnExit, gbc);

    gbc.gridx = 0;
    gbc.gridy = 1;
    gridbag.setConstraints(lstPanel, gbc);

    gbc.anchor = GridBagConstraints.EAST;
    gbc.insets.left = 2;
    gbc.insets.right = 2;
    gbc.insets.top = 40;

    //****************************************************
    //Add the buttons and the log to the frame
    Container contentPane = getContentPane();
    contentPane.add(lstPanel, BorderLayout.CENTER);
    contentPane.add(buttonPanel, BorderLayout.SOUTH);

    initialize();
    try {
      UIManager.setLookAndFeel(new WindowsLookAndFeel());
      SwingUtilities.updateComponentTreeUI(FTPGUI.this);
    catch (Exception ex) {
      System.out.println(ex);
    }

  }

  private void initialize() {
    // fill some test data here into the listbox.
    defLocalList.addElement("first.html");
    defLocalList.addElement("second.html");
    defLocalList.addElement("third.html");
    defLocalList.addElement("fourth.html");
    defLocalList.addElement("fifth.html");
    defLocalList.addElement("Design Patterns 1.html");

    defRemoteList.addElement("sixth.html");
    defRemoteList.addElement("seventh.html");
    defRemoteList.addElement("eighth.html");
    defRemoteList.addElement("ninth.html");
    defRemoteList.addElement("Design Patterns 2.html");

  }

  public static void main(String[] argsthrows Exception {

    JFrame frame = new FTPGUI();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    //frame.pack();
    frame.setSize(450300);
    frame.setVisible(true);
  }

  class buttonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      CommandInterface CommandObj = (CommandInterfacee.getSource();
      CommandObj.processEvent();
    }
  }

  interface CommandInterface {
    public void processEvent();
  }

  class UploadButton extends JButton implements CommandInterface {

    public void processEvent() {
      int index = localList.getSelectedIndex();
      String selectedItem = localList.getSelectedValue().toString();
      ((DefaultListModellocalList.getModel()).remove(index);

      ((DefaultListModelremoteList.getModel()).addElement(selectedItem);
    }

    public UploadButton(String name) {
      super(name);
    }
  }

  class DownloadButton extends JButton implements CommandInterface {
    public void processEvent() {
      int index = remoteList.getSelectedIndex();
      String selectedItem = remoteList.getSelectedValue().toString();
      ((DefaultListModelremoteList.getModel()).remove(index);

      ((DefaultListModellocalList.getModel()).addElement(selectedItem);
    }

    public DownloadButton(String name) {
      super(name);
    }
  }

  class DeleteButton extends JButton implements CommandInterface {

    public void processEvent() {
      int index = localList.getSelectedIndex();
      if (index >= 0) {
        ((DefaultListModellocalList.getModel()).remove(index);
      }

      index = remoteList.getSelectedIndex();
      if (index >= 0) {
        ((DefaultListModelremoteList.getModel()).remove(index);
      }
    }

    public DeleteButton(String name) {
      super(name);
    }
  }

  class ExitButton extends JButton implements CommandInterface {

    public void processEvent() {
      System.exit(1);
    }

    public ExitButton(String name) {
      super(name);
    }
  }

}// end of class


           
       
Related examples in the same category
1. Command pattern in Java 1Command pattern in Java 1
2. Command pattern in Java 2Command pattern in Java 2
3. Command pattern in Java 3Command pattern in Java 3
4. Command pattern in Java 4Command pattern in Java 4
5. Command Pattern 2 in Java
6. Command pattern: Shopping
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.