Package ca.canucksoftware.wosqi

Source Code of ca.canucksoftware.wosqi.ReceiveFile

/*
* WebOSQuickInstallAboutBox.java
*/

package ca.canucksoftware.wosqi;

import ca.canucksoftware.utils.FileUtils;
import ca.canucksoftware.webos.DeviceInfo;
import ca.canucksoftware.webos.WebOSConnection;
import javax.swing.JFileChooser;
import javax.swing.Icon;
import java.awt.Component;
import java.awt.Container;
import javax.swing.UIManager;
import javax.swing.JButton;
import javax.swing.JDialog;
import java.io.*;
import java.text.MessageFormat;
import javax.swing.JOptionPane;
import java.util.*;
import java.util.Timer;
import java.util.ResourceBundle;

public class ReceiveFile extends javax.swing.JDialog {
    private ResourceBundle bundle;
    public Timer t;
    private WebOSConnection webOS;
    private File dest;
    private String src;
    private boolean transferStarted;

    public ReceiveFile(java.awt.Frame parent, WebOSConnection connection) {
        super(parent);
        bundle = WebOSQuickInstallApp.bundle;
        initComponents();
        webOS = connection;
        dest = null;
        transferStarted = false;
        t = new Timer();
        if(!webOS.isConnected()) {
            DeviceInfo info = webOS.getDeviceInfo();
            if(info!=null && !info.model().equals(DeviceInfo.Model.Unknown.toString())) {
                JOptionPane.showMessageDialog(rootPane, MessageFormat.format(bundle
                        .getString("{0}_IS_DISCONNECTED._PLEASE_RECONNECT_THEN_TRY_AGAIN."),
                        new Object[] {info.model()}));
            } else {
                JOptionPane.showMessageDialog(rootPane, bundle
                        .getString("DEVICE_IS_DISCONNECTED._PLEASE_RECONNECT_THEN_TRY_AGAIN."));
            }
            t.schedule(new DoDispose(), 200);
        }
    }

    public void startTransfer() {
        if(dest!=null && dest.exists()) {
            transferStarted = true;
            jButton1.setEnabled(false);
            jButton2.setEnabled(false);
            jButton3.setEnabled(false);
            jTextField2.setEnabled(false);
            setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
            jButton2.setText(bundle.getString("PLEASE_WAIT..."));
            src = formatSrc(jTextField1.getText());
            t.schedule(new DoTransfer(), 200);
        }
    }

    private String formatSrc(String file) {
        String result = null;
        result = file.replace("\\", "/");
        if(!result.startsWith("/"))
            result = "/" + result;
        return result;
    }

    public void closeCustTransfer() {
        dispose();
    }

    private void loadFileChooser() {
        JFileChooser fc = new JFileChooser(); //Create a file chooser
        fc.setDialogTitle("");
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        disableNewFolderButton(fc);
        if (fc.showDialog(rootPane, bundle.getString("SELECT"))==
                JFileChooser.APPROVE_OPTION) {
            dest = fc.getSelectedFile();
            jTextField2.setText(dest.getAbsolutePath());
        }
    }

    private void disableNewFolderButton(Container c) {
        int len = c.getComponentCount();
        for(int i=0; i<len; i++) {
            Component comp = c.getComponent(i);
            if(comp instanceof JButton) {
                JButton b = (JButton)comp;
                Icon icon = b.getIcon();
                if(icon != null && (icon == UIManager.getIcon("FileChooser.newFolderIcon")
                        || icon == UIManager.getIcon("FileChooser.upFolderIcon")))
                    b.setEnabled(false);
            } else if (comp instanceof Container) {
                disableNewFolderButton((Container)comp);
            }
        }
    }

    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        jButton3 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(bundle.getString("ReceiveFile.title")); // NOI18N
        setIconImage(null);
        setModal(true);
        setName("transfer"); // NOI18N
        setResizable(false);

        jLayeredPane1.setName("jLayeredPane1"); // NOI18N

        jLayeredPane2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(192, 192, 192)));
        jLayeredPane2.setName("jLayeredPane2"); // NOI18N

        jButton3.setFont(jButton3.getFont().deriveFont(jButton3.getFont().getSize()+1f));
        jButton3.setText(bundle.getString("ReceiveFile.jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jButton3.setBounds(260, 100, 79, 23);
        jLayeredPane2.add(jButton3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton2.setFont(jButton2.getFont().deriveFont(jButton2.getFont().getSize()+1f));
        jButton2.setText(bundle.getString("ReceiveFile.jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jButton2.setBounds(110, 100, 140, 25);
        jLayeredPane2.add(jButton2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton1.setFont(jButton1.getFont());
        jButton1.setText(bundle.getString("ReceiveFile.jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton1.setBounds(400, 58, 45, 23);
        jLayeredPane2.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTextField2.setFont(jTextField2.getFont());
        jTextField2.setEnabled(false);
        jTextField2.setName("jTextField2"); // NOI18N
        jTextField2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextField2MouseClicked(evt);
            }
        });
        jTextField2.setBounds(120, 60, 270, 20);
        jLayeredPane2.add(jTextField2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getSize()+3f));
        jLabel1.setText(bundle.getString("ReveiveFile.jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jLabel1.setBounds(10, 60, 140, 20);
        jLayeredPane2.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTextField1.setFont(jTextField1.getFont());
        jTextField1.setName("jTextField1"); // NOI18N
        jTextField1.setBounds(80, 20, 360, 24);
        jLayeredPane2.add(jTextField1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setFont(jLabel2.getFont().deriveFont(jLabel2.getFont().getSize()+3f));
        jLabel2.setText(bundle.getString("ReveiveFile.jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        jLabel2.setBounds(10, 20, 70, 20);
        jLayeredPane2.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane2.setBounds(10, 10, 450, 140);
        jLayeredPane1.add(jLayeredPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        loadFileChooser();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        startTransfer();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        closeCustTransfer();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jTextField2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField2MouseClicked
        if(!transferStarted)
            loadFileChooser();
    }//GEN-LAST:event_jTextField2MouseClicked
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    javax.swing.JButton jButton1;
    javax.swing.JButton jButton2;
    javax.swing.JButton jButton3;
    javax.swing.JLabel jLabel1;
    javax.swing.JLabel jLabel2;
    javax.swing.JLayeredPane jLayeredPane1;
    javax.swing.JLayeredPane jLayeredPane2;
    javax.swing.JTextField jTextField1;
    javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

    class DoTransfer extends TimerTask  {
        public void run() {
            dest=new File(dest, FileUtils.getFilename(src));
            if(dest.exists()) {
                dest.delete();
            }
            if(webOS.receiveFile(src, dest)) {
                JOptionPane.showMessageDialog(rootPane,
                        bundle.getString("FILE_TRANSFERRED_SUCCESSFULLY."));
            } else {
                JOptionPane.showMessageDialog(rootPane,
                        bundle.getString("FILE_TRANSFER_FAILED."));
            }
            jButton1.setEnabled(true);
            jButton2.setEnabled(true);
            jButton3.setEnabled(true);
            jTextField2.setEnabled(true);
            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dispose();
        }
    }
    class DoDispose extends TimerTask  {
        public void run() {
            dispose();
        }
    }
}
TOP

Related Classes of ca.canucksoftware.wosqi.ReceiveFile

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.