/* * $Id: Main.java 169 2008-08-05 16:26:13Z sdrycroft $ */ /* Copyright (C) 2005 Simon David Rycroft This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.dnd.DropTargetListener; import java.awt.dnd.DropTargetDropEvent; import java.awt.dnd.DropTargetDragEvent; import java.awt.dnd.DropTargetEvent; import java.awt.dnd.DropTarget; import java.awt.dnd.DnDConstants; import java.awt.datatransfer.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.GridLayout; import java.io.File; import java.io.PrintStream; import java.io.BufferedReader; import java.net.URL; import java.net.URI; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Vector; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JProgressBar; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.TableColumn; import javax.swing.UIManager; import javax.swing.SwingConstants; import netscape.javascript.JSObject; import java.net.MalformedURLException; import javax.swing.UnsupportedLookAndFeelException; public class Main extends JApplet implements MouseListener, DropTargetListener { private JTable table; private JScrollPane scrollPane; private JPanel rightPanel; private JButton add,remove,upload,help; private ImageIcon dropIcon,dropIconUpload,dropIconAdded; private TableData tabledata; private TableColumn sizeColumn; private File [] files; private JLabel progCompletion,iconLabel; private JProgressBar progBar; private int sentBytes,totalBytes,buttonClicked,maxPixels,percentComplete,maxFileSize; private Color backgroundColour,columnHeadColourBack,columnHeadColourFore; private PostletLabels pLabels; private Vector failedFiles,uploadedFiles; private UploadManager upMan; private JFileChooser chooser; // Default error PrintStream! private PrintStream out = System.out; // Boolean set to false when a javascript method is executed private boolean javascript; // Parameters private URL endPageURL, helpPageURL, destinationURL,dropImageURL,dropImageUploadURL,dropImageAddedURL; private boolean warnMessage,autoUpload,helpButton,failedFileMessage,addButton,removeButton,uploadButton; private String language, dropImage, dropImageAdded, dropImageUpload, proxy, fileToRemove; private int maxThreads; private String [] fileExtensions; // URI list flavor (Hack for linux/KDE) private DataFlavor uriListFlavor; // JSObject for doing the shit! private JSObject jso; // Javascript functions called by Postlet /** * postletStatus(int) * sends the percentage complete status every time it changes * * postletFinished() * executed when postlet has finished uploading * * postletFiles(string) * sends a list of files queued for upload * * postletError(int, string) * Informs of an error, along with an optional text string * int: * 0 - File too big * 1 - File wrong format * 2 - File corrupt (missing) * 3 - Upload failed */ private static final String [] postletJS = {"postletStatus","postletFinished","postletFiles","postletError"}; // Postlet Version (Mainly for diagnostics and tracking) public static final String postletVersion = "0.15"; public void init() { // First thing, output the version, for debugging purposes. System.out.println("POSTLET VERSION: "+postletVersion); String date = "$Date: 2008-08-05 17:26:13 +0100 (Tue, 05 Aug 2008) $"; System.out.println(date.substring(7,date.length()-1)); // URI list flavor: try { uriListFlavor = new DataFlavor("text/uri-list;class=java.lang.String"); } catch (ClassNotFoundException cnfe){ errorMessage("No class found for DataFlavor"); } // New JSObject for calling methods etc try { jso = (JSObject) JSObject.getWindow(this); }catch(netscape.javascript.JSException njjse){ errorMessage("Unable to create JSO. Safari?"); } // Set the javascript to false, and start listening for clicks javascript = false; JavascriptListener jsListen = new JavascriptListener(this); jsListen.start(); buttonClicked = 0; // Default of add click. percentComplete = 0; // Just started, nothing done! getParameters();// Also sets pLabels layoutGui(); createChooser(); // Vector of failedFiles failedFiles = new Vector(); uploadedFiles = new Vector(); } private void createChooser(){ chooser = new JFileChooser(); progBar.setValue(0); if (fileExtensions != null){ UploaderFileFilter filter = new UploaderFileFilter(); for (int i=1; i0) { File [] fileTemp = new File[files.length-table.getSelectedRowCount()]; int [] selectedRows = table.getSelectedRows(); Arrays.sort(selectedRows); int k=0; for (int i=0; i0) { if (warnMessage){ JOptionPane.showMessageDialog(null, pLabels.getLabel(11), pLabels.getLabel(12), JOptionPane.INFORMATION_MESSAGE); } add.setEnabled(false); remove.setEnabled(false); help.setEnabled(false); upload.setEnabled(false); if (dropImageURL!=null && dropImageUploadURL!=null){ iconLabel.setIcon(dropIconUpload); repaint(); } sentBytes = 0; progBar.setMaximum(totalBytes); progBar.setMinimum(0); try { upMan = new UploadManager(files, this, destinationURL, maxThreads); } catch(java.lang.NullPointerException npered){ upMan = new UploadManager(files, this, destinationURL); } upMan.start(); } } protected synchronized void setProgress(int a) { if(totalBytes>0){ sentBytes += a; progBar.setValue(sentBytes); if((sentBytes*100)/totalBytes>percentComplete){ percentComplete = (sentBytes*100)/totalBytes; try { jso.eval("try{"+postletJS[0]+"("+percentComplete+");}catch(e){;}"); } catch (netscape.javascript.JSException jseps){ errorMessage("Unable to send status to Javascript"); } catch (NullPointerException npe){ errorMessage("Unable to send status to Javascript"); } } if (sentBytes >= totalBytes){ if(sentBytes == totalBytes){ // Upload is complete. Check for failed files. if (failedFiles.size()>0 && failedFileMessage){ // There is at least one failed file. Show an error message String failedFilesString = "\r\n"; for (int i=0; i0) { upload.setEnabled(true); remove.setEnabled(true); if (dropImageURL!=null && dropImageAddedURL!=null){ iconLabel.setIcon(dropIconAdded); repaint(); } try { jso.eval("try{"+postletJS[2]+"('"+getFiles()+"');}catch(e){;}"); } catch(netscape.javascript.JSException jsepf){ errorMessage("Unable to send info about files added"); } catch(NullPointerException npe){ errorMessage("Unable to send info about files added"); } } if (files !=null && autoUpload){ uploadClick(); } createChooser();// Not sure if this is necesary. FIXME } public void fileTooBig(File f){ errorMessage("file too big: "+f.getName()+" - "+f.length()); if(warnMessage){ JOptionPane.showMessageDialog(null, ""+pLabels.getLabel(1)+" - "+f.getName(), ""+pLabels.getLabel(5), JOptionPane.ERROR_MESSAGE); } addFailedFile(f); try{ jso.eval("try{"+postletJS[3]+"(0,'"+f.getName().replace("'","`")+"');}catch(e){;}"); } catch(netscape.javascript.JSException jsepf){ errorMessage("Unable to send info about 'file too big'"); } catch(NullPointerException npe){ errorMessage("Unable to send info about 'file too big'"); } } public void fileNotAllowed(File f){ errorMessage("file not allowed: "+f.getName()); addFailedFile(f); try{ jso.eval("try{"+postletJS[3]+"(1,'"+f.getName().replace("'","`")+"');}catch(e){;}"); } catch(netscape.javascript.JSException jsepf){ errorMessage("Unable to send info about 'file not allowed'"); } } public void fileNotFound(File f){ errorMessage("file not found: "+f.getName()); addFailedFile(f); try{ jso.eval("try{"+postletJS[3]+"(2,'"+f.getName().replace("'","`")+"');}catch(e){;}"); } catch(netscape.javascript.JSException jsepf){ errorMessage("Unable to send info about 'file not found'"); } catch(NullPointerException npe){ errorMessage("Unable to send info about 'file not found'"); } } public void fileUploadFailed(File f){ errorMessage("file upload failed: "+f.getName()); addFailedFile(f); try{ jso.eval("try{"+postletJS[3]+"(3,'"+f.getName().replace("'","`")+"');}catch(e){;}"); } catch(netscape.javascript.JSException jsepf){ errorMessage("Unable to send info about 'file upload failed'"); } catch(NullPointerException npe){ errorMessage("Unable to send info about 'file upload failed'"); } } public void helpClick() { // Open a web page in another frame/window // Unless specified as a parameter, this will be a help page // on the postlet website. try { getAppletContext().showDocument(helpPageURL, "_blank"); } catch (NullPointerException nohelppage){ // Show a popup with help instead! try {getAppletContext().showDocument(new URL("http://www.postlet.com/help/"), "_blank");}catch(MalformedURLException mfue){;}// Hard coded URL, no need for catch } } public String getCookie(){ // Method reads the cookie in from the Browser using the LiveConnect object. // May also add an option to set the cookie using an applet parameter FIXME! try { String cookie = new String(); cookie = (String)jso.eval("try{document.cookie;}catch(e){;}"); errorMessage("Cookie is:###"+cookie+"###"); return cookie; } catch (Exception e){ errorMessage("Failed to get cookie"); return ""; } } /** * Cancel all upload of files. */ public void cancelUpload(){ upMan.cancelUpload(); errorMessage("Canceled upload"); if(totalBytes>0){ setProgress(totalBytes+1); } } /** * This method has been altered due to IE (and Safari) being shite * (it did return an array - oh well, backwards stepping). */ public String getFailedFiles(){ if (failedFiles.size()>0){ String failedFilesString = ""; // Return a "/" delimited string (as "/" is not a legal character). for(int i=0; i0){ String uploadedFilesString = ""; // Return a "/" delimited string (as "/" is not a legal character). for(int i=0; ifileNumber && fileNumber>-1){ File [] fileTemp = new File[files.length-1]; int j=0; for(int i=0;i0) { errorMessage("Enabling the upload and remove buttons"); upload.setEnabled(true); remove.setEnabled(true); } if (files !=null && autoUpload){ uploadClick(); } } catch (java.awt.datatransfer.UnsupportedFlavorException usfe){;} catch (java.io.IOException ioe){;} dtde.dropComplete(true); } public void dropActionChanged(DropTargetDragEvent dtde){;} public void dragOver(DropTargetDragEvent dtde){;} public void dragExit(DropTargetEvent dte){;} public void dragEnter(DropTargetDragEvent dtde){;} public void mouseEntered(MouseEvent e){;} public void mouseExited(MouseEvent e){;} public void mousePressed(MouseEvent e){;} public void mouseReleased(MouseEvent e){;} }