package com.fitbank.manual; import com.fitbank.common.crypto.Decrypt; import com.fitbank.util.Debug; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Robot; import java.awt.image.RenderedImage; import java.io.File; import java.io.FileOutputStream; import java.net.MalformedURLException; import java.net.URL; import javax.imageio.ImageIO; import org.jdesktop.jdic.browser.WebBrowser; import org.jdesktop.jdic.browser.WebBrowserEvent; import org.jdesktop.jdic.browser.WebBrowserListener; /** * Dialogo de captura de pantallas. * * @author FitBank CI */ public class CaptureDialog extends javax.swing.JDialog { private static final String BASE_URL = "%s/FBSVS?usuario=%s&password=%s&subs=%s&tran=%s"; private final String appURL; private final String appUser; private final String appPassword; private WebBrowser webBrowser = new WebBrowser(); private Thread timeout; private File base; private String subs; private String tran; private String script; private int tabs; private int tab; public CaptureDialog(java.awt.Frame parent, boolean modal, String appURL, String appUser, String appPassword) { super(parent, modal); this.getContentPane().add(webBrowser, BorderLayout.CENTER); webBrowser.setPreferredSize(new Dimension(1024, 650)); initComponents(); this.appURL = appURL; this.appUser = appUser; try { this.appPassword = new Decrypt().encrypt(appPassword); } catch (Exception e) { // Excepción desconocida!!! throw new RuntimeException(e); } } public void capture(File base, String subs, String tran, int tabs, String script) { this.base = base; this.subs = subs; this.tran = tran; this.script = script; this.tabs = tabs; this.tab = 2; webBrowser.addWebBrowserListener(new WebBrowserListener() { public void downloadStarted(WebBrowserEvent wbe) { } public void downloadCompleted(WebBrowserEvent wbe) { } public void downloadProgress(WebBrowserEvent wbe) { } public void downloadError(WebBrowserEvent wbe) { } public void documentCompleted(WebBrowserEvent wbe) { webBrowser.executeScript(CaptureDialog.this.script); capture(); } public void titleChange(WebBrowserEvent wbe) { } public void statusTextChange(WebBrowserEvent wbe) { } public void windowClose(WebBrowserEvent wbe) { } }); this.timeout = new Thread() { public void run() { try { Thread.sleep(30000); Debug.warn("Timeout: " + CaptureDialog.this.isVisible()); } catch (InterruptedException e) { // Ignorar } Debug.debug("Cerrando proceso de captura..."); if (CaptureDialog.this.isVisible()) { CaptureDialog.this.setVisible(false); Debug.debug("Eliminando ventana..."); CaptureDialog.this.dispose(); } Debug.debug("Listo."); } }; try { URL url = new URL(String.format(BASE_URL, appURL, appUser, appPassword, subs, tran)); webBrowser.setURL(url); } catch (MalformedURLException e) { throw new RuntimeException(e); } updateTitle(); this.timeout.start(); Debug.debug("Iniciando proceso de captura..."); setVisible(true); Debug.debug("Completado proceso de captura."); } /** 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() { setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); pack(); }// //GEN-END:initComponents private void capture() { try { Thread.sleep(500); } catch (InterruptedException e) { Debug.error(e); } Rectangle r = webBrowser.getBounds(); r.setLocation(webBrowser.getLocationOnScreen()); try { RenderedImage i = new Robot().createScreenCapture(r); File dir = new File(base, "img"); dir.mkdir(); ImageIO.write(i, "png", new FileOutputStream(new File(dir, subs + tran + "_" + (tab - 1) + ".png"))); } catch (Exception e) { Debug.error(e); } if (tab >= tabs) { this.timeout.interrupt(); } else { tab += 1; webBrowser.executeScript("MostrarTab(" + tab + ")"); updateTitle(); capture(); } } private void updateTitle() { setTitle("Capturando " + subs + ":" + tran + " tab " + (tab - 1) + " de " + Math.max(tabs - 1, 1)); } // Variables declaration - do not modify//GEN-BEGIN:variables // End of variables declaration//GEN-END:variables }