All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.codename1.designer.RunOnDevice Maven / Gradle / Ivy

/*
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores
 * CA 94065 USA or visit www.oracle.com if you need additional information or
 * have any questions.
 */

package com.codename1.designer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;

/**
 * Allows executing platform files to upload files to devices
 *
 * @author Shai Almog
 */
public class RunOnDevice extends javax.swing.JPanel {
    private Process currentProcess;
    private boolean canceled;
    
    /** Creates new form RunOnDevice */
    public RunOnDevice(String helpResource) {
        initComponents();
        try {
            help.setPage(getClass().getResource(helpResource));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static RunOnDevice showRunDialog(java.awt.Component parent, String helpRes) {
        RunOnDevice r = new RunOnDevice(helpRes);
        JDialog d = new JDialog(SwingUtilities.windowForComponent(parent), "Run On Device");
        d.getContentPane().setLayout(new java.awt.BorderLayout());
        d.getContentPane().add(java.awt.BorderLayout.CENTER, r);
        d.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        d.setLocationByPlatform(true);
        d.pack();
        d.setVisible(true);
        return r;
    }

    public boolean waitForProcess(Process currentProcess, boolean isLast, String message) {
        try {
            this.currentProcess = currentProcess;
            if(message != null) {
                informationLine.setText(message);
            }
            appendToTextArea(currentProcess.getInputStream());
            int result = currentProcess.waitFor();
            if(canceled) {
                return false;
            }
            if(result != 0) {
                prog.setIndeterminate(false);
                cancelButton.setEnabled(false);
                okButton.setEnabled(true);
                informationLine.setText("ERROR: see details for further information");
                return false;
            }
            if (isLast) {
                prog.setIndeterminate(false);
                prog.setValue(prog.getMaximum());
                informationLine.setText("Run completed successfully!");
                cancelButton.setEnabled(false);
                okButton.setEnabled(true);
            }
            return true;
        } catch (InterruptedException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    private void appendToTextArea(final InputStream i) {
        new Thread() {
            public void run() {
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(i));
                    String lineRead = null;
                    while ((lineRead = reader.readLine()) != null) {
                        System.out.println(lineRead);
                        final String lineToAdd = lineRead;
                        SwingUtilities.invokeLater(new Runnable() {

                            public void run() {
                                String t = details.getText();
                                details.setText(t + "\n" + lineToAdd);
                            }
                        });
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }.start();
    }


    /** 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")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        informationLine = new javax.swing.JLabel();
        prog = new javax.swing.JProgressBar();
        cancelButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        details = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        help = new javax.swing.JTextPane();

        FormListener formListener = new FormListener();

        jTabbedPane1.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);
        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setOpaque(false);

        jLabel1.setText("Preparing Device Preview");
        jLabel1.setName("jLabel1"); // NOI18N

        informationLine.setText("Blackberry users please be advised that only OS's 4.7 or newer are supported!");
        informationLine.setName("informationLine"); // NOI18N

        prog.setIndeterminate(true);
        prog.setName("prog"); // NOI18N

        cancelButton.setText("Cancel");
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(formListener);

        okButton.setText("OK");
        okButton.setEnabled(false);
        okButton.setName("okButton"); // NOI18N
        okButton.addActionListener(formListener);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 596, Short.MAX_VALUE)
            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                    .add(10, 10, 10)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jPanel1Layout.createSequentialGroup()
                            .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                            .add(256, 256, 256))
                        .add(informationLine, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 576, Short.MAX_VALUE)
                        .add(prog, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 576, Short.MAX_VALUE)
                        .add(jPanel1Layout.createSequentialGroup()
                            .add(cancelButton)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(okButton)
                            .add(242, 242, 242)))
                    .addContainerGap()))
        );

        jPanel1Layout.linkSize(new java.awt.Component[] {cancelButton, okButton}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 357, Short.MAX_VALUE)
            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jLabel1)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(informationLine)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(prog, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(cancelButton)
                        .add(okButton))
                    .addContainerGap(263, Short.MAX_VALUE)))
        );

        jTabbedPane1.addTab("Overview", jPanel1);

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setOpaque(false);

        jScrollPane1.setName("jScrollPane1"); // NOI18N
        jScrollPane1.setOpaque(false);

        details.setColumns(20);
        details.setRows(5);
        details.setName("details"); // NOI18N
        jScrollPane1.setViewportView(details);

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 596, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Details", jPanel2);

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

        help.setContentType("text/html");
        help.setEditable(false);
        help.setName("help"); // NOI18N
        jScrollPane2.setViewportView(help);

        jTabbedPane1.addTab("Help", jScrollPane2);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 601, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
        );
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == cancelButton) {
                RunOnDevice.this.cancelButtonActionPerformed(evt);
            }
            else if (evt.getSource() == okButton) {
                RunOnDevice.this.okButtonActionPerformed(evt);
            }
        }
    }// //GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        canceled = true;
        currentProcess.destroy();
        SwingUtilities.windowForComponent(this).dispose();
}//GEN-LAST:event_cancelButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        SwingUtilities.windowForComponent(this).dispose();
    }//GEN-LAST:event_okButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextArea details;
    private javax.swing.JTextPane help;
    private javax.swing.JLabel informationLine;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JButton okButton;
    private javax.swing.JProgressBar prog;
    // End of variables declaration//GEN-END:variables

}