Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package ftpclientgui; import dirList.FileListItem; import dirList.FileListItemTypes; import dirList.IFileListItemAdapter; import ftpclient.FTPManager; import ftpclient.FTPManagerEvent; import ftpclient.IFTPManagerListener; import interfaces.ISettingsProvider; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.LinkOption; import java.text.DateFormat; import java.util.Date; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; import javax.swing.Timer; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import org.apache.commons.net.ftp.FTPFile; /** * * @author Przemo */ public class MainWindow extends javax.swing.JFrame implements IFTPManagerListener, ActionListener, WindowListener, ListSelectionListener { private final FTPManager mngr; private Timer tmr; private IFileListItemAdapter fsAdapter = null; private final ISettingsProvider sprvd; public IFileListItemAdapter getFsAdapter() { return fsAdapter; } public void setFsAdapter(IFileListItemAdapter fsAdapter) { this.fsAdapter = fsAdapter; } /** * Creates new form MainWindow * @param ftpMngr * @param sprvd */ public MainWindow(FTPManager ftpMngr, ISettingsProvider sprvd) { initComponents(); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setResizable(false); this.mngr = ftpMngr; this.sprvd = sprvd; if (ftpMngr != null) { ftpMngr.addListener(this); } init(); } private void init() { this.labTime.setText(DateFormat.getTimeInstance().format(new Date())); this.labStatus.setText("Not logged in."); this.dirList.addSelectionListener(this); tmr = new Timer(950, this); tmr.start(); } private void closeResources() { tmr.stop(); tmr = null; this.dispose(); System.exit(0); } private void resetForm() { this.labStatus.setText("Not logged in."); this.labLocation.setText(""); this.dirList.removeAll(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jMenuItem1 = new javax.swing.JMenuItem(); fileChoose = new javax.swing.JFileChooser(); panStatus = new javax.swing.JPanel(); labVersion = new javax.swing.JLabel(); labTime = new javax.swing.JLabel(); jSeparator1 = new javax.swing.JSeparator(); labStatus = new javax.swing.JLabel(); jSeparator2 = new javax.swing.JSeparator(); panMain = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); labLocation = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); txtUploadFile = new javax.swing.JTextField(); btnBrowse = new javax.swing.JButton(); btnUploasd = new javax.swing.JButton(); dirList = new dirList.DirectoryList(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenuItem2 = new javax.swing.JMenuItem(); jMenuItem3 = new javax.swing.JMenuItem(); jMenuItem4 = new javax.swing.JMenuItem(); jMenu2 = new javax.swing.JMenu(); jMenuItem5 = new javax.swing.JMenuItem(); jMenuItem1.setText("jMenuItem1"); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); panStatus.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED)); panStatus.setPreferredSize(new java.awt.Dimension(401, 25)); labVersion.setText("jLabel1"); labTime.setText("jLabel1"); jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL); labStatus.setText("jLabel1"); jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL); javax.swing.GroupLayout panStatusLayout = new javax.swing.GroupLayout(panStatus); panStatus.setLayout(panStatusLayout); panStatusLayout.setHorizontalGroup(panStatusLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panStatusLayout.createSequentialGroup().addComponent(labVersion) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(labStatus).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(labTime) .addContainerGap())); panStatusLayout.setVerticalGroup(panStatusLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING).addComponent(jSeparator2) .addGroup(panStatusLayout.createSequentialGroup() .addGroup(panStatusLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(labVersion).addComponent(labStatus)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(panStatusLayout.createSequentialGroup().addComponent(labTime).addGap(0, 0, Short.MAX_VALUE))); jLabel1.setText("Current location: "); jLabel2.setText("Upload File:"); txtUploadFile.setEditable(false); btnBrowse.setText("..."); btnBrowse.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnBrowseActionPerformed(evt); } }); btnUploasd.setText("Upload"); btnUploasd.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnUploasdActionPerformed(evt); } }); javax.swing.GroupLayout panMainLayout = new javax.swing.GroupLayout(panMain); panMain.setLayout(panMainLayout); panMainLayout .setHorizontalGroup(panMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panMainLayout.createSequentialGroup().addGap(15, 15, 15).addComponent(jLabel1) .addGap(37, 37, 37) .addComponent(labLocation, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(panMainLayout.createSequentialGroup() .addGroup(panMainLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panMainLayout.createSequentialGroup().addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtUploadFile, javax.swing.GroupLayout.PREFERRED_SIZE, 436, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnBrowse, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnUploasd, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) .addComponent(dirList, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap())); panMainLayout.setVerticalGroup(panMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panMainLayout.createSequentialGroup().addGap(4, 4, 4) .addGroup(panMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(labLocation)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(dirList, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(panMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(txtUploadFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnBrowse).addComponent(btnUploasd)))); jMenu1.setText("File"); jMenuItem2.setText("Connect"); jMenuItem2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem2ActionPerformed(evt); } }); jMenu1.add(jMenuItem2); jMenuItem3.setLabel("Disconnect"); jMenuItem3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem3ActionPerformed(evt); } }); jMenu1.add(jMenuItem3); jMenuItem4.setLabel("Exit"); jMenuItem4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem4ActionPerformed(evt); } }); jMenu1.add(jMenuItem4); jMenuBar1.add(jMenu1); jMenu2.setText("Edit"); jMenuItem5.setLabel("Settings"); jMenuItem5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem5ActionPerformed(evt); } }); jMenu2.add(jMenuItem5); jMenuBar1.add(jMenu2); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(panMain, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(panStatus, javax.swing.GroupLayout.DEFAULT_SIZE, 651, Short.MAX_VALUE) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(panMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(panStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))); pack(); }// </editor-fold>//GEN-END:initComponents /** * Disconnect clicked. * @param evt */ private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed resetForm(); }//GEN-LAST:event_jMenuItem3ActionPerformed private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed closeResources(); }//GEN-LAST:event_jMenuItem4ActionPerformed /** * Connect menu item clicked * @param evt */ private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed LoginForm frm = new LoginForm(mngr); frm.setLocation((int) (this.getX() + this.getWidth() / 2 - frm.getPreferredSize().getWidth() / 2), this.getY() + this.getHeight() / 2); frm.setVisible(true); }//GEN-LAST:event_jMenuItem2ActionPerformed /** * Browse button clicked. Show the file chooser and fill in the text field afterwards * @param evt */ private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowseActionPerformed if (fileChoose.showOpenDialog(this) == JOptionPane.OK_OPTION) { txtUploadFile.setText(fileChoose.getSelectedFile().getPath()); } }//GEN-LAST:event_btnBrowseActionPerformed private void btnUploasdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUploasdActionPerformed if (!txtUploadFile.getText().equals("") && java.nio.file.Files .exists(java.nio.file.Paths.get(txtUploadFile.getText()), LinkOption.NOFOLLOW_LINKS)) { try { String message; if (!mngr.UploadFile(new java.io.File(txtUploadFile.getText()).getName(), new java.io.BufferedInputStream(new java.io.FileInputStream(txtUploadFile.getText())))) { message = "File could not be uploaded!"; } else { message = "File uploaded sucesfully."; txtUploadFile.setText(""); } JOptionPane.showMessageDialog(this, message); } catch (FileNotFoundException ex) { Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); } } }//GEN-LAST:event_btnUploasdActionPerformed /** * Settings clicked */ private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed SwingUtilities.invokeLater(new Runnable() { @Override public void run() { SettingsForm sf = new SettingsForm(sprvd); sf.setVisible(true); } }); }//GEN-LAST:event_jMenuItem5ActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnBrowse; private javax.swing.JButton btnUploasd; private dirList.DirectoryList dirList; private javax.swing.JFileChooser fileChoose; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JMenu jMenu1; private javax.swing.JMenu jMenu2; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuItem jMenuItem1; private javax.swing.JMenuItem jMenuItem2; private javax.swing.JMenuItem jMenuItem3; private javax.swing.JMenuItem jMenuItem4; private javax.swing.JMenuItem jMenuItem5; private javax.swing.JSeparator jSeparator1; private javax.swing.JSeparator jSeparator2; private javax.swing.JLabel labLocation; private javax.swing.JLabel labStatus; private javax.swing.JLabel labTime; private javax.swing.JLabel labVersion; private javax.swing.JPanel panMain; private javax.swing.JPanel panStatus; private javax.swing.JTextField txtUploadFile; // End of variables declaration//GEN-END:variables @Override public void registerAction(FTPManagerEvent evt) { if (evt.action.equals(FTPManagerEvent.EVENT_LOGIN_ACTION)) { if ((int) evt.result == 1) { labStatus.setText("Logged In"); labLocation.setText(mngr.getHomeDirectory()); updateDirFilesList(); dirList.InitialCurrentFolderPath("/"); } else { labStatus.setText("Not logged in"); } } } private void updateDirFilesList() { try { if (mngr != null) { if (fsAdapter == null) { fsAdapter = new FTPListItemAdapter(); } FTPFile[] fs = mngr.getCurrentDirectoryFilesList(); FTPFile[] ds = mngr.getCurrentDirectoryStructure(); FileListItem[] fsitem = new FileListItem[ds.length + fs.length + 1]; //allow for navigation directory .. fsitem[0] = new FileListItem("..", FileListItemTypes.DIRECTORY); for (int i = 0; i < ds.length; i++) { fsitem[i + 1] = fsAdapter.getFileListItem(ds[i]); } for (int i = 0; i < fs.length; i++) { fsitem[i + ds.length + 1] = fsAdapter.getFileListItem(fs[i]); } dirList.setListData(fsitem); dirList.revalidate(); } } catch (IOException ex) { Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); } } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == tmr) { this.labTime.setText(DateFormat.getTimeInstance().format(new Date())); } } @Override public void windowOpened(WindowEvent e) { } @Override public void windowClosing(WindowEvent e) { } @Override public void windowClosed(WindowEvent e) { closeResources(); } @Override public void windowIconified(WindowEvent e) { } @Override public void windowDeiconified(WindowEvent e) { } @Override public void windowActivated(WindowEvent e) { } @Override public void windowDeactivated(WindowEvent e) { } /** * List selection changed * @param e */ @Override public void valueChanged(ListSelectionEvent e) { if (e.getSource().equals(dirList)) { try { //try to change the folder in FTPManager if (mngr != null && mngr.changeDirectory(dirList.getCurrentFolderPath())) { this.labLocation.setText(dirList.getCurrentFolderPath()); updateDirFilesList(); } } catch (IOException ex) { Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); } } } }