[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/imagex/postlet/rev165/ -> Main.java (source)

   1  /*
   2   * $Id: Main.java 169 2008-08-05 16:26:13Z sdrycroft $
   3   */
   4  
   5  /*    Copyright (C) 2005 Simon David Rycroft
   6  
   7      This program is free software; you can redistribute it and/or
   8      modify it under the terms of the GNU General Public License
   9      as published by the Free Software Foundation; either version 2
  10      of the License, or (at your option) any later version.
  11  
  12      This program is distributed in the hope that it will be useful,
  13      but WITHOUT ANY WARRANTY; without even the implied warranty of
  14      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15      GNU General Public License for more details.
  16  
  17      You should have received a copy of the GNU General Public License
  18      along with this program; if not, write to the Free Software
  19      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  20  
  21  import java.awt.BorderLayout;
  22  import java.awt.Color;
  23  import java.awt.Container;
  24  
  25  import java.awt.dnd.DropTargetListener;
  26  import java.awt.dnd.DropTargetDropEvent;
  27  import java.awt.dnd.DropTargetDragEvent;
  28  import java.awt.dnd.DropTargetEvent;
  29  import java.awt.dnd.DropTarget;
  30  import java.awt.dnd.DnDConstants;
  31  import java.awt.datatransfer.*;
  32  
  33  import java.awt.event.MouseEvent;
  34  import java.awt.event.MouseListener;
  35  import java.awt.GridLayout;
  36  import java.io.File;
  37  import java.io.PrintStream;
  38  import java.io.BufferedReader;
  39  import java.net.URL;
  40  import java.net.URI;
  41  import java.util.Arrays;
  42  import java.util.Iterator;
  43  import java.util.List;
  44  import java.util.Vector;
  45  import javax.swing.BorderFactory;
  46  import javax.swing.ImageIcon;
  47  import javax.swing.JApplet;
  48  import javax.swing.JButton;
  49  import javax.swing.JFileChooser;
  50  import javax.swing.JLabel;
  51  import javax.swing.JOptionPane;
  52  import javax.swing.JPanel;
  53  import javax.swing.JProgressBar;
  54  import javax.swing.JScrollPane;
  55  import javax.swing.JTable;
  56  import javax.swing.table.TableColumn;
  57  import javax.swing.UIManager;
  58  import javax.swing.SwingConstants;
  59  import netscape.javascript.JSObject;
  60  
  61  import java.net.MalformedURLException;
  62  import javax.swing.UnsupportedLookAndFeelException;
  63  
  64  public class Main extends JApplet implements MouseListener, DropTargetListener {
  65  
  66      private JTable table;
  67      private JScrollPane scrollPane;
  68      private JPanel rightPanel;
  69      private JButton add,remove,upload,help;
  70      private ImageIcon dropIcon,dropIconUpload,dropIconAdded;    
  71      private TableData tabledata;
  72      private TableColumn sizeColumn;
  73      private File [] files;
  74      private JLabel progCompletion,iconLabel;
  75      private JProgressBar progBar;
  76      private int sentBytes,totalBytes,buttonClicked,maxPixels,percentComplete,maxFileSize;
  77      private Color backgroundColour,columnHeadColourBack,columnHeadColourFore;
  78      private PostletLabels pLabels;
  79      private Vector failedFiles,uploadedFiles;
  80      private UploadManager upMan;
  81      private JFileChooser chooser;
  82  
  83      // Default error PrintStream!
  84      private PrintStream out = System.out;
  85  
  86      // Boolean set to false when a javascript method is executed
  87      private boolean javascript;
  88  
  89      // Parameters
  90      private URL endPageURL, helpPageURL, destinationURL,dropImageURL,dropImageUploadURL,dropImageAddedURL;
  91      private boolean warnMessage,autoUpload,helpButton,failedFileMessage,addButton,removeButton,uploadButton;
  92      private String language, dropImage, dropImageAdded, dropImageUpload, proxy, fileToRemove;
  93      private int maxThreads;
  94      private String [] fileExtensions;
  95  
  96      // URI list flavor (Hack for linux/KDE)
  97      private DataFlavor uriListFlavor;
  98  
  99      // JSObject for doing the shit!
 100      private JSObject jso;
 101      
 102      // Javascript functions called by Postlet
 103      /**
 104       * postletStatus(int)
 105       *    sends the percentage complete status every time it changes
 106       * 
 107       * postletFinished()
 108       *    executed when postlet has finished uploading
 109       * 
 110       * postletFiles(string)
 111       *    sends a list of files queued for upload
 112       * 
 113       * postletError(int, string)
 114       *    Informs of an error, along with an optional text string
 115       *    int:
 116       *        0 - File too big
 117       *        1 - File wrong format
 118       *        2 - File corrupt (missing)
 119       *        3 - Upload failed
 120       */
 121      private static final String [] postletJS = {"postletStatus","postletFinished","postletFiles","postletError"};
 122      
 123  
 124      // Postlet Version (Mainly for diagnostics and tracking)
 125      public static final String postletVersion = "0.15";
 126  
 127      public void init() {
 128          // First thing, output the version, for debugging purposes.
 129          System.out.println("POSTLET VERSION: "+postletVersion);
 130          String date = "$Date: 2008-08-05 17:26:13 +0100 (Tue, 05 Aug 2008) $";
 131          System.out.println(date.substring(7,date.length()-1));
 132  
 133          // URI list flavor:
 134          try {
 135              uriListFlavor = new DataFlavor("text/uri-list;class=java.lang.String");
 136          }
 137          catch (ClassNotFoundException cnfe){
 138              errorMessage("No class found for DataFlavor");
 139          }
 140          
 141          // New JSObject for calling methods etc
 142          try {
 143              jso = (JSObject) JSObject.getWindow(this);
 144          }catch(netscape.javascript.JSException njjse){
 145              errorMessage("Unable to create JSO. Safari?");
 146          }
 147  
 148          // Set the javascript to false, and start listening for clicks
 149          javascript = false;
 150          JavascriptListener jsListen = new JavascriptListener(this);
 151          jsListen.start();
 152          buttonClicked = 0; // Default of add click.
 153          percentComplete = 0; // Just started, nothing done!
 154  
 155          getParameters();// Also sets pLabels
 156          layoutGui();
 157          createChooser();
 158          // Vector of failedFiles
 159          failedFiles = new Vector();
 160          uploadedFiles = new Vector();
 161  
 162      }
 163  
 164      private void createChooser(){
 165          chooser = new JFileChooser();
 166  
 167          progBar.setValue(0);
 168          if (fileExtensions != null){
 169              UploaderFileFilter filter = new UploaderFileFilter();
 170              for (int i=1; i<fileExtensions.length; i++){
 171                  filter.addExtension(fileExtensions[i]);
 172              }
 173              filter.setDescription(fileExtensions[0]);
 174              chooser.addChoosableFileFilter(filter);
 175          }
 176          else {
 177              chooser.setFileFilter(chooser.getAcceptAllFileFilter());
 178          }
 179  
 180          chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
 181          chooser.setMultiSelectionEnabled(true);
 182          chooser.setDialogTitle(pLabels.getLabel(14));
 183      }
 184      private void layoutGui(){
 185          //JOptionPane.showMessageDialog(null, "This is a CVS version of Postlet, use with caution","CVS",JOptionPane.INFORMATION_MESSAGE);
 186          // Set the look of the applet
 187          try {
 188              UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
 189          } catch (UnsupportedLookAndFeelException exc){;} catch (IllegalAccessException exc){;} catch (ClassNotFoundException exc){;} catch (InstantiationException exc){;}
 190  
 191          // Get the main pane to add content to.
 192          Container pane = getContentPane();
 193  
 194          // Attempt to add drop listener to the whole applet
 195          try {
 196              DropTarget dt = new DropTarget();
 197              dt.addDropTargetListener(this);
 198              pane.setDropTarget(dt);
 199          } catch (java.util.TooManyListenersException tmle){
 200              errorMessage( "Too many listeners to drop!");
 201          }
 202          // Table for the adding of Filenames and sizes to.
 203          tabledata = new TableData(pLabels.getLabel(0),pLabels.getLabel(1)+" -KB ");
 204          table = new JTable(tabledata);
 205          table.setColumnSelectionAllowed(false);
 206          //table.setDragEnabled(false); // This method is not available to Java 3!
 207          sizeColumn = table.getColumn(pLabels.getLabel(1)+" -KB ");
 208          sizeColumn.setMaxWidth(100);
 209          table.getColumn(pLabels.getLabel(1)+" -KB ").setMinWidth(100);
 210          if (columnHeadColourBack != null && backgroundColour != null){
 211              errorMessage( "setting the tables colours");
 212              table.getTableHeader().setBackground(columnHeadColourBack);
 213              table.getTableHeader().setForeground(columnHeadColourFore);
 214              table.setBackground(backgroundColour);
 215          }
 216          scrollPane = new JScrollPane(table);
 217          scrollPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
 218  
 219          if (backgroundColour != null){
 220              scrollPane.setBackground(backgroundColour);
 221          }
 222          // Always set the table background colour as White.
 223          // May change this if required, only would require alot of Params!
 224          scrollPane.getViewport().setBackground(Color.white);
 225          
 226          if (dropImageURL!=null){
 227              // Instead of the table, we'll add a lovely image to the center
 228              // of the applet to drop images on.
 229              dropIcon = new ImageIcon(dropImageURL);
 230              iconLabel = new JLabel(dropIcon);
 231              pane.add(iconLabel, BorderLayout.CENTER);            
 232          }
 233          else {
 234              // Add the scroll pane/table to the main pane
 235              pane.add(scrollPane, BorderLayout.CENTER);
 236          }
 237          if (dropImageUploadURL!=null)
 238              dropIconUpload = new ImageIcon(dropImageUploadURL);
 239          if (dropImageAddedURL!=null)
 240              dropIconAdded = new ImageIcon(dropImageAddedURL);
 241  
 242          errorMessage("Adding button");
 243          if (helpButton)
 244              rightPanel = new JPanel(new GridLayout(4,1,10,10));
 245          else
 246              rightPanel = new JPanel(new GridLayout(3,1,10,10));
 247          rightPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
 248  
 249          add = new JButton(pLabels.getLabel(6));
 250          if(addButton){
 251              add.addMouseListener(this);
 252              rightPanel.add(add);
 253          }
 254  
 255          remove = new JButton(pLabels.getLabel(7));
 256          if(removeButton){
 257              remove.addMouseListener(this);
 258              remove.setEnabled(false);
 259              rightPanel.add(remove);
 260          }
 261  
 262          upload = new JButton(pLabels.getLabel(8));
 263          if(uploadButton){
 264              upload.addMouseListener(this);
 265              upload.setEnabled(false);
 266              rightPanel.add(upload);
 267          }
 268  
 269          help = new JButton(pLabels.getLabel(9));
 270          if (helpButton){
 271              help.addMouseListener(this);
 272              rightPanel.add(help);
 273          }
 274          if (backgroundColour != null)
 275              rightPanel.setBackground(backgroundColour);
 276          if(addButton || removeButton || helpButton || uploadButton)
 277              pane.add(rightPanel,"East");
 278  
 279          JPanel progPanel = new JPanel(new GridLayout(2, 1));
 280          progPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
 281  
 282          progCompletion = new JLabel(pLabels.getLabel(10),SwingConstants.CENTER);
 283          progPanel.add(progCompletion);
 284  
 285          progBar = new JProgressBar();
 286          progPanel.add(progBar);
 287          progPanel.setBorder(BorderFactory.createEmptyBorder(5,25,5,25));
 288  
 289          if (backgroundColour != null){
 290              pane.setBackground(backgroundColour);
 291              progPanel.setBackground(backgroundColour);
 292          }
 293          pane.add(progPanel,"South");
 294  
 295          // If the destination has not been set/isn't a proper URL
 296          // Then deactivate the buttons. 
 297          if (destinationURL == null)
 298              add.setEnabled(false);
 299      }
 300  
 301      protected void errorMessage(String message){
 302          out.println("*** "+message+" ***");
 303      }
 304      // Helper method for getting the parameters from the webpage.
 305      private void getParameters(){
 306          
 307          /* MAX FILE SIZE */
 308          try {
 309              maxFileSize = Integer.parseInt(getParameter("maxfilesize"));
 310          } catch (NullPointerException nullMaxSize){
 311              errorMessage("maxfilesize is null");
 312              maxFileSize = Integer.MAX_VALUE;
 313          } catch (NumberFormatException nfemaxfilesize){
 314              errorMessage("maxfilesize is not a number");
 315              maxFileSize = Integer.MAX_VALUE;
 316          }
 317          
 318          /* PROXY */
 319          try {
 320              proxy = getParameter("proxy");
 321              if(proxy.equals("") || proxy.equals(null) || proxy.toLowerCase().equals("false")){
 322                  proxy = "";
 323              }
 324          } catch (NullPointerException nullProxy){
 325              proxy = "";
 326              errorMessage("proxy is null");
 327          }
 328  
 329          /* LANGUAGE */
 330          try {
 331              language = getParameter("language");
 332              if (language.equals("") || language.equals(null))
 333                  language = "EN";
 334          } catch (NullPointerException nullLang){
 335              // Default language being set
 336              language = "EN";
 337              errorMessage("language is null");
 338          }
 339      // This method (getParameters) relies on labels from PostletLabels if
 340      // there is an error.
 341          pLabels = new PostletLabels(language, getCodeBase());
 342  
 343          /* DESTINATION */
 344          try {
 345              destinationURL = new URL(getParameter("destination"));
 346          // Following line is for testing, and to hard code the applet to postlet.com
 347          //destinationURL = new URL("http://www.postlet.com/example/javaUpload.php");
 348          } catch(java.net.MalformedURLException malurlex){
 349              // Do something here for badly formed destination, which is ESENTIAL.
 350              errorMessage( "Badly formed destination:###"+getParameter("destination")+"###");
 351              JOptionPane.showMessageDialog(null, ""+pLabels.getLabel(3), ""+pLabels.getLabel(5), JOptionPane.ERROR_MESSAGE);
 352          } catch(java.lang.NullPointerException npe){
 353              // Do something here for the missing destination, which is ESENTIAL.
 354              errorMessage("destination is null");
 355              JOptionPane.showMessageDialog(null, pLabels.getLabel(4), pLabels.getLabel(5), JOptionPane.ERROR_MESSAGE);
 356          }
 357  
 358          /* BACKGROUND */
 359          try {
 360              Integer bgci = new Integer(getParameter("backgroundcolour"));
 361              backgroundColour = new Color(bgci.intValue());
 362          } catch(NumberFormatException numfe){
 363              errorMessage( "background colour is not a number:###"+getParameter("backgroundcolour")+"###");
 364          } catch (NullPointerException nullred){
 365              errorMessage( "background colour is null");
 366          }
 367  
 368          /* TABLEHEADERFOREGROUND */
 369          try {
 370              Integer thfi = new Integer(getParameter("tableheadercolour"));
 371              columnHeadColourFore = new Color(thfi.intValue());
 372          } catch(NumberFormatException numfe){
 373              errorMessage( "table header colour is not a number:###"+getParameter("tableheadcolour")+"###");
 374          } catch (NullPointerException nullred){
 375              errorMessage( "table header colour is null");
 376          }
 377  
 378          /* TABLEHEADERBACKGROUND */
 379          try {
 380              Integer thbi = new Integer(getParameter("tableheaderbackgroundcolour"));
 381              columnHeadColourBack = new Color(thbi.intValue());
 382          } catch(NumberFormatException numfe){
 383              errorMessage( "table header back colour is not a number:###"+getParameter("tableheaderbackgroundcolour")+"###");
 384          } catch (NullPointerException nullred){
 385              errorMessage( "table header back colour is null");
 386          }
 387  
 388          /* FILEEXTENSIONS */
 389          try {
 390              fileExtensions = getParameter("fileextensions").split(",");
 391          } catch(NullPointerException nullfileexts){
 392              errorMessage( "file extensions is null");
 393          }
 394  
 395          /* WARNINGMESSAGE */
 396          try {
 397              if (getParameter("warnmessage").toLowerCase().equals("true"))
 398                  warnMessage = true;
 399              else
 400                  warnMessage = false;
 401          } catch(NullPointerException nullwarnmessage){
 402              errorMessage( "warnmessage is null");
 403              warnMessage = false;
 404          }
 405  
 406          /* AUTOUPLOAD */
 407          try {
 408              if (getParameter("autoupload").toLowerCase().equals("true"))
 409                  autoUpload = true;
 410              else
 411                  autoUpload = false;
 412          } catch(NullPointerException nullwarnmessage){
 413              errorMessage( "autoUpload is null");
 414              autoUpload = false;
 415          }
 416  
 417          /* MAXTHREADS */
 418          try {
 419              Integer maxts = new Integer(getParameter("maxthreads"));
 420              maxThreads = maxts.intValue();
 421          } catch (NullPointerException nullmaxthreads){
 422              errorMessage( "maxthreads is null");
 423          } catch (NumberFormatException nummaxthreads){
 424              errorMessage( "maxthread is not a number");}
 425  
 426          /* ENDPAGE */
 427          try {
 428              endPageURL = new URL(getParameter("endpage"));
 429          } catch(java.net.MalformedURLException malurlex){
 430              errorMessage( "endpage is badly formed:###"+getParameter("endpage")+"###");
 431          } catch(java.lang.NullPointerException npe){
 432              errorMessage( "endpage is null");
 433          }
 434  
 435          /* HELPPAGE */
 436          try {
 437              helpPageURL = new URL(getParameter("helppage"));
 438          } catch(java.net.MalformedURLException malurlex){
 439              errorMessage( "helppage is badly formed:###"+getParameter("helppage")+"###");
 440          } catch(java.lang.NullPointerException npe){
 441              errorMessage( "helppage is null");
 442          }
 443  
 444          /* HELP BUTTON */
 445          try {
 446              if (getParameter("helpbutton").toLowerCase().trim().equals("true"))
 447                  helpButton = true;
 448              else
 449                  helpButton = false;
 450          } catch(NullPointerException nullwarnmessage){
 451              errorMessage( "helpbutton is null");
 452              helpButton = false;
 453          }
 454  
 455          /* ADD BUTTON */
 456          try {
 457              if (getParameter("addbutton").toLowerCase().trim().equals("false"))
 458                  addButton = false;
 459              else
 460                  addButton = true;
 461          } catch(NullPointerException nullwarnmessage){
 462              errorMessage( "addbutton is null");
 463              addButton = true;
 464          }
 465  
 466          /* REMOVE BUTTON */
 467          try {
 468              if (getParameter("removebutton").toLowerCase().trim().equals("false"))
 469                  removeButton = false;
 470              else
 471                  removeButton = true;
 472          } catch(NullPointerException nullwarnmessage){
 473              errorMessage( "removebutton is null");
 474              removeButton = true;
 475          }
 476  
 477          /* UPLOAD BUTTON */
 478          try {
 479              if (getParameter("uploadbutton").toLowerCase().trim().equals("false"))
 480                  uploadButton = false;
 481              else
 482                  uploadButton = true;
 483          } catch(NullPointerException nullwarnmessage){
 484              errorMessage( "uploadbutton is null");
 485              uploadButton = true;
 486          }
 487                  
 488          /* REPLACE TABLE WITH "DROP" IMAGE */
 489          try {
 490              dropImage = getParameter("dropimage");
 491              if (dropImage!=null)
 492                  dropImageURL = new URL(dropImage);
 493          } catch(MalformedURLException urlexception){
 494              try {
 495                  URL codeBase = getCodeBase();
 496                  dropImageURL = new URL(codeBase.getProtocol()+"://"+codeBase.getHost()+codeBase.getPath()+dropImage);
 497              } catch(MalformedURLException urlexception2){
 498                  errorMessage("dropimage is not a valid reference");
 499              }
 500          }
 501          /* REPLACE TABLE WITH "DROP" IMAGE (UPLOAD IMAGE)*/
 502          try {
 503              dropImageUpload = getParameter("dropimageupload");
 504              if (dropImageUpload!=null)
 505                  dropImageUploadURL = new URL(dropImageUpload);
 506          } catch(MalformedURLException urlexception){
 507              try {
 508                  URL codeBase = getCodeBase();
 509                  dropImageUploadURL = new URL(codeBase.getProtocol()+"://"+codeBase.getHost()+codeBase.getPath()+dropImageUpload);
 510              } catch(MalformedURLException urlexception2){
 511                  errorMessage("dropimageupload is not a valid reference");
 512              }
 513          }
 514          /* REPLACE TABLE WITH "DROP" IMAGE (ADDED IMAGE)*/
 515          try {
 516              dropImageAdded = getParameter("dropimageadded");
 517              if (dropImageAdded!=null)
 518                  dropImageAddedURL = new URL(dropImageAdded);
 519          } catch(MalformedURLException urlexception){
 520              try {
 521                  URL codeBase = getCodeBase();
 522                  dropImageAddedURL = new URL(codeBase.getProtocol()+"://"+codeBase.getHost()+codeBase.getPath()+dropImageAdded);
 523              } catch(MalformedURLException urlexception2){
 524                  errorMessage("dropimageupload is not a valid reference");
 525              }
 526          }
 527          
 528          /* FAILED FILES WARNING */
 529          // This should be set to false if failed files are being handled in
 530          // javascript
 531          try {
 532              if (getParameter("failedfilesmessage").toLowerCase().trim().equals("true"))
 533                  failedFileMessage = true;
 534              else
 535                  failedFileMessage = false;
 536          } catch (NullPointerException nullfailedfilemessage){
 537              errorMessage( "failedfilemessage is null");
 538              failedFileMessage = false;
 539          }
 540          
 541          /* MAX PIXELS FOR AN UPLOADED IMAGE */
 542          // This supports PNG, GIF and JPEG images only. All other images will
 543          // not be resized
 544          try {
 545              Integer maxps = new Integer(getParameter("maxpixels"));
 546              maxPixels = maxps.intValue();
 547          } catch (NullPointerException nullmaxpixels){
 548              errorMessage( "maxpixels is null");
 549          } catch (NumberFormatException nummaxpixels){
 550              errorMessage( "maxpixels is not a number");}
 551      }
 552      
 553      private void removeClick() {
 554          if(table.getSelectedRowCount()>0) {
 555              File [] fileTemp = new File[files.length-table.getSelectedRowCount()];
 556              int [] selectedRows = table.getSelectedRows();
 557              Arrays.sort(selectedRows);
 558              int k=0;
 559              for (int i=0; i<files.length;i++){
 560                  if (Arrays.binarySearch(selectedRows,i)<0){
 561                      fileTemp[k]=files[i];
 562                      k++;
 563                  }
 564              }
 565              files = fileTemp;
 566              tableUpdate();
 567          }
 568          if (files.length==0) {
 569              upload.setEnabled(false);
 570              remove.setEnabled(false);
 571          }
 572      }
 573  
 574      public void uploadClick() {
 575          if(files != null && files.length>0) {
 576              if (warnMessage){
 577                  JOptionPane.showMessageDialog(null, pLabels.getLabel(11), pLabels.getLabel(12), JOptionPane.INFORMATION_MESSAGE);
 578              }
 579              add.setEnabled(false);
 580              remove.setEnabled(false);
 581              help.setEnabled(false);
 582              upload.setEnabled(false);
 583              if (dropImageURL!=null && dropImageUploadURL!=null){
 584                  iconLabel.setIcon(dropIconUpload);
 585                  repaint();
 586              }
 587              sentBytes = 0;
 588              progBar.setMaximum(totalBytes);
 589              progBar.setMinimum(0);
 590              try {
 591                  upMan = new UploadManager(files, this, destinationURL, maxThreads);
 592              } catch(java.lang.NullPointerException npered){
 593                  upMan = new UploadManager(files, this, destinationURL);
 594              }
 595              upMan.start();
 596          }
 597      }
 598  
 599      protected synchronized void setProgress(int a) {
 600          if(totalBytes>0){
 601              sentBytes += a;
 602              progBar.setValue(sentBytes);
 603              if((sentBytes*100)/totalBytes>percentComplete){
 604                  percentComplete = (sentBytes*100)/totalBytes;
 605                  try {
 606                      jso.eval("try{"+postletJS[0]+"("+percentComplete+");}catch(e){;}");
 607                  } catch (netscape.javascript.JSException jseps){
 608                      errorMessage("Unable to send status to Javascript");
 609                  } catch (NullPointerException npe){
 610                      errorMessage("Unable to send status to Javascript");
 611                  }
 612              }
 613              if (sentBytes >= totalBytes){
 614                  if(sentBytes == totalBytes){
 615                      // Upload is complete. Check for failed files.
 616                      if (failedFiles.size()>0 && failedFileMessage){
 617                          // There is at least one failed file. Show an error message
 618                          String failedFilesString = "\r\n";
 619                          for (int i=0; i<failedFiles.size(); i++){
 620                              File tempFile = (File)failedFiles.elementAt(i);
 621                              failedFilesString += tempFile.getName()+"\r\n";
 622                          }
 623                          JOptionPane.showMessageDialog(null, pLabels.getLabel(16)+":"+failedFilesString,pLabels.getLabel(5), JOptionPane.ERROR_MESSAGE);
 624                      }
 625                      progCompletion.setText(pLabels.getLabel(2));
 626                      if (endPageURL != null){
 627                          errorMessage("Changing browser page");
 628                          getAppletContext().showDocument(endPageURL);
 629                      } else {
 630                          try {
 631                              // Just ignore this error, as it is most likely from the endpage
 632                              // not being set.
 633                              // Attempt at calling Javascript after upload is complete.
 634                              errorMessage("Executing: "+postletJS[1]+"();");
 635                              jso.eval("try{"+postletJS[1]+"();}catch(e){;}");
 636                          }
 637                          catch (netscape.javascript.JSException jse){
 638                              // Not to worry, just means the end page and a postletFinished
 639                              // method aren't set. Just finish, and let the web page user
 640                              // exit the page
 641                              errorMessage("postletFinished, and End page unset");
 642                          }
 643                          catch (NullPointerException npe){
 644                              errorMessage("postletFinished, and End page unset, and JS not executed");
 645                          }
 646                      }
 647                  }
 648                  // Reset the applet
 649                  totalBytes = 0;
 650                  percentComplete = 0;
 651                  progBar.setValue(0);
 652                  files = new File[0];
 653                  tableUpdate();
 654                  add.setEnabled(true);
 655                  help.setEnabled(true);
 656                  if (dropImageURL!=null && dropImageUploadURL!=null){
 657                      iconLabel.setIcon(dropIcon);
 658                  }
 659                  failedFiles.clear();
 660                  uploadedFiles.clear();
 661                  repaint();
 662  
 663              }
 664          }
 665      }
 666      
 667      // Get and set the proxy server
 668      public String getProxy(){
 669          return proxy;
 670      }
 671      
 672      public void setProxy(String p){
 673          proxy = p;
 674      }
 675  
 676      // Adds a file that HASN'T uploaded to an array. Once uploading is complete,
 677      // these can be listed with a popup box.
 678      public void addFailedFile(File f){
 679          failedFiles.add(f);
 680      }
 681  
 682      // Adds a file that HAS uploaded to an array. These are passed along with
 683      // failed files to a javascript method.
 684      public void addUploadedFile(File f){
 685          uploadedFiles.add(f);
 686      }
 687  
 688      public int getMaxPixels(){
 689          return maxPixels;
 690      }
 691      
 692      public void setMaxPixels(int pixels){
 693          maxPixels = pixels;
 694      }
 695      
 696      public int getMaxFileSize(){
 697          return maxFileSize;
 698      }
 699      
 700      public void setMaxFileSize(int f){
 701          maxFileSize = f;
 702      }
 703  
 704      private void tableUpdate() {
 705          totalBytes = 0;
 706          String [] filenames = new String[files.length];
 707          int [] fileSize = new int[files.length];
 708          for(int i=0; i<files.length; i++) {
 709              filenames[i] = files[i].getAbsolutePath();
 710              fileSize[i] = (int)files[i].length();
 711              totalBytes += (int)files[i].length();
 712          }
 713          int i=0;
 714          // FIXME - THIS SEEMS SILLY!********************************************
 715          String [][] rowData = new String[files.length][2];
 716          while(i<files.length) {
 717              rowData[i][0] = files[i].getName();
 718              rowData[i][1] = ""+files[i].length();
 719              i++;
 720          }
 721          // *********************************************************************
 722          tabledata.formatTable(rowData,i);
 723          sizeColumn.setMaxWidth(100);
 724          sizeColumn.setMinWidth(100);
 725          repaint();
 726      }
 727  
 728      public void addClick() {
 729          int returnVal = chooser.showOpenDialog(null);
 730          if (returnVal == JFileChooser.APPROVE_OPTION) {
 731              File [] tempFiles = chooser.getSelectedFiles();
 732              Vector filesForUpload = new Vector();
 733              for (int i=0; i<tempFiles.length; i++){
 734                  if (tempFiles[i].isDirectory()){
 735                      File [] subDirFiles = tempFiles[i].listFiles();
 736                      for (int j = 0; j<subDirFiles.length; j++){
 737                          if (subDirFiles[j].isFile()){
 738                              if(subDirFiles[j].length()<maxFileSize){
 739                                  filesForUpload.add(subDirFiles[j]);
 740                              } else {
 741                                  fileTooBig(subDirFiles[j]);
 742                              }
 743                          }
 744                      }
 745  
 746                  } else {
 747                      if(tempFiles[i].length()<maxFileSize){
 748                          filesForUpload.add(tempFiles[i]);
 749                      } else {
 750                          fileTooBig(tempFiles[i]);
 751                      }
 752                  }
 753              }
 754              if (files == null){
 755                  files = new File[0];
 756              }
 757              tempFiles = new File[filesForUpload.size()+files.length];
 758              for (int i=0; i<files.length; i++)
 759                  tempFiles[i] = files[i];
 760              for (int i=0; i<filesForUpload.size(); i++){
 761                  tempFiles[i+files.length] = (File)filesForUpload.elementAt(i);
 762              }
 763              files = tempFiles;
 764              tableUpdate();
 765          }
 766          if (files != null && files.length>0) {
 767              upload.setEnabled(true);
 768              remove.setEnabled(true);            
 769              if (dropImageURL!=null && dropImageAddedURL!=null){
 770                  iconLabel.setIcon(dropIconAdded);
 771                  repaint();
 772              }
 773              try {
 774                  jso.eval("try{"+postletJS[2]+"('"+getFiles()+"');}catch(e){;}");
 775              } catch(netscape.javascript.JSException jsepf){
 776                  errorMessage("Unable to send info about files added");
 777              } catch(NullPointerException npe){
 778                  errorMessage("Unable to send info about files added");
 779              }
 780          }
 781          if (files !=null && autoUpload){
 782              uploadClick();
 783          }
 784          createChooser();// Not sure if this is necesary. FIXME
 785      }
 786      
 787      public void fileTooBig(File f){
 788          errorMessage("file too big: "+f.getName()+" - "+f.length());
 789          if(warnMessage){
 790              JOptionPane.showMessageDialog(null, ""+pLabels.getLabel(1)+" - "+f.getName(), ""+pLabels.getLabel(5), JOptionPane.ERROR_MESSAGE);
 791          }
 792          addFailedFile(f);
 793          try{
 794              jso.eval("try{"+postletJS[3]+"(0,'"+f.getName().replace("'","`")+"');}catch(e){;}");
 795          } catch(netscape.javascript.JSException jsepf){
 796              errorMessage("Unable to send info about 'file too big'");
 797          } catch(NullPointerException npe){
 798              errorMessage("Unable to send info about 'file too big'");
 799          }
 800      }
 801          
 802      public void fileNotAllowed(File f){
 803          errorMessage("file not allowed: "+f.getName());
 804          addFailedFile(f);
 805          try{
 806              jso.eval("try{"+postletJS[3]+"(1,'"+f.getName().replace("'","`")+"');}catch(e){;}");
 807          } catch(netscape.javascript.JSException jsepf){
 808              errorMessage("Unable to send info about 'file not allowed'");
 809          }
 810      }
 811      
 812      public void fileNotFound(File f){
 813          errorMessage("file not found: "+f.getName());
 814          addFailedFile(f);
 815          try{
 816              jso.eval("try{"+postletJS[3]+"(2,'"+f.getName().replace("'","`")+"');}catch(e){;}");
 817          } catch(netscape.javascript.JSException jsepf){
 818              errorMessage("Unable to send info about 'file not found'");
 819          } catch(NullPointerException npe){
 820              errorMessage("Unable to send info about 'file not found'");
 821          }
 822      }
 823      
 824      public void fileUploadFailed(File f){
 825          errorMessage("file upload failed: "+f.getName());
 826          addFailedFile(f);
 827          try{
 828              jso.eval("try{"+postletJS[3]+"(3,'"+f.getName().replace("'","`")+"');}catch(e){;}");
 829          } catch(netscape.javascript.JSException jsepf){
 830              errorMessage("Unable to send info about 'file upload failed'");
 831          } catch(NullPointerException npe){
 832              errorMessage("Unable to send info about 'file upload failed'");
 833          }
 834      }
 835  
 836      public void helpClick() {
 837          // Open a web page in another frame/window
 838          // Unless specified as a parameter, this will be a help page
 839          // on the postlet website.
 840  
 841          try {
 842              getAppletContext().showDocument(helpPageURL, "_blank");
 843          } catch (NullPointerException nohelppage){
 844              // Show a popup with help instead!
 845              try {getAppletContext().showDocument(new URL("http://www.postlet.com/help/"), "_blank");}catch(MalformedURLException mfue){;}// Hard coded URL, no need for catch
 846          }
 847  
 848      }
 849  
 850      public String getCookie(){
 851  
 852          // Method reads the cookie in from the Browser using the LiveConnect object.
 853          // May also add an option to set the cookie using an applet parameter FIXME!
 854          try {
 855              String cookie = new String();
 856              cookie = (String)jso.eval("try{document.cookie;}catch(e){;}");
 857              errorMessage("Cookie is:###"+cookie+"###");
 858              return cookie;
 859          }
 860          catch (Exception e){
 861              errorMessage("Failed to get cookie");
 862              return "";
 863          }
 864      }
 865      
 866      /**
 867       * Cancel all upload of files.
 868       */
 869      public void cancelUpload(){
 870          upMan.cancelUpload();
 871          errorMessage("Canceled upload");
 872          if(totalBytes>0){
 873              setProgress(totalBytes+1);
 874          }
 875      }
 876      /**
 877       * This method has been altered due to IE (and Safari) being shite
 878       * (it did return an array - oh well, backwards stepping).
 879       */
 880      public String getFailedFiles(){
 881          if (failedFiles.size()>0){
 882              String failedFilesString = "";
 883              // Return a "/" delimited string (as "/" is not a legal character).
 884              for(int i=0; i<failedFiles.size(); i++){
 885                  File tempFile = (File)failedFiles.elementAt(i);
 886                  failedFilesString += tempFile.getName()+"/";
 887              }
 888              return failedFilesString.replace("'","`");
 889              /*
 890              String [] arrayFailedFiles = new String[failedFiles.size()];
 891              for (int i=0; i<failedFiles.size(); i++){
 892                  File tempFile = (File)failedFiles.elementAt(i);
 893                  arrayFailedFiles[i] = tempFile.getName();
 894              }
 895              return arrayFailedFiles;
 896              */
 897          }
 898          return null;
 899      }
 900      
 901      /**
 902       * This method returns all the files that have been added to Postlet
 903       */
 904      public String getFiles(){
 905          String fileString = ""+files.length;
 906          for(int i=0; i<files.length; i++){
 907              fileString += "/"+files[i].getName();
 908          }
 909          return fileString.replace("'","`");
 910      }
 911  
 912      public String getUploadedFiles(){
 913          if (uploadedFiles.size()>0){
 914              String uploadedFilesString = "";
 915              // Return a "/" delimited string (as "/" is not a legal character).
 916              for(int i=0; i<uploadedFiles.size(); i++){
 917                  File tempFile = (File)uploadedFiles.elementAt(i);
 918                  uploadedFilesString += tempFile.getName()+"/";
 919              }
 920              return uploadedFilesString.replace("'","`");
 921              /*
 922              String [] arrayUploadedFiles = new String[uploadedFiles.size()];
 923              for (int i=0; i<uploadedFiles.size(); i++){
 924                  File tempFile = (File)uploadedFiles.elementAt(i);
 925                  arrayUploadedFiles[i] = tempFile.getName();
 926              }
 927              return arrayUploadedFiles;
 928              */
 929          }
 930          return null;
 931      }
 932      
 933      public void changedDestination(String destination){
 934          // Change the destination before upload.
 935          try {
 936              destinationURL = new URL(destination);
 937          } catch(java.net.MalformedURLException malurlex){
 938              // Do something here for badly formed destination, which is ESENTIAL.
 939              errorMessage( "Badly formed destination:###"+destination+"###");
 940              JOptionPane.showMessageDialog(null, ""+pLabels.getLabel(3), ""+pLabels.getLabel(5), JOptionPane.ERROR_MESSAGE);
 941          } catch(java.lang.NullPointerException npe){
 942              // Do something here for the missing destination, which is ESENTIAL.
 943              errorMessage("destination is null");
 944              JOptionPane.showMessageDialog(null, pLabels.getLabel(4), pLabels.getLabel(5), JOptionPane.ERROR_MESSAGE);
 945          }
 946      }
 947      
 948      protected void doRemoveFile(String number){
 949          try {
 950              int fileNumber = Integer.parseInt(number);
 951              if(files.length>fileNumber && fileNumber>-1){
 952                  File [] fileTemp = new File[files.length-1];
 953                  int j=0;
 954                  for(int i=0;i<files.length;i++){
 955                      if(i!=fileNumber){
 956                          fileTemp[j] = files[i];
 957                          j++;
 958                      }
 959                  }
 960                  files = fileTemp;
 961                  tableUpdate();
 962                  if (files.length==0) {
 963                      upload.setEnabled(false);
 964                      remove.setEnabled(false);
 965                  }
 966              }
 967              try {
 968                  jso.eval("try{"+postletJS[2]+"('"+getFiles()+"');}catch(e){;}");
 969              } catch(netscape.javascript.JSException jsepf){
 970                  errorMessage("Unable to send info about files added");
 971              } catch(NullPointerException npe){
 972                  errorMessage("Unable to send info about files added");
 973              }
 974          } catch (NumberFormatException nfe){
 975              errorMessage("removeFile not a number");
 976          }
 977      }
 978  
 979      public void postletAdd(){
 980  
 981          // Set a variable so that the listening thread can call the add click method
 982          buttonClicked = 0;
 983          javascript = true;
 984      }
 985      public void removeFile(String number){
 986          // As above
 987          buttonClicked = 3;
 988          fileToRemove = number;
 989          javascript = true;
 990      }
 991      public String getFileToRemove(){
 992          return fileToRemove;
 993      }
 994      public void postletUpload(){
 995          // As above
 996          buttonClicked = 1;
 997          javascript = true;
 998      }
 999      public void postletCancel(){
1000          // As above
1001          buttonClicked = 2;
1002          javascript = true;
1003      }
1004      public boolean getJavascriptStatus(){
1005  
1006          return javascript;
1007      }
1008      public void setJavascriptStatus(){
1009  
1010          javascript = false;
1011      }
1012      public boolean isUploadEnabled(){
1013  
1014          return upload.isEnabled();
1015      }
1016      public boolean isAddEnabled(){
1017  
1018          return add.isEnabled();
1019      }
1020      public boolean isRemoveEnabled(){
1021  
1022          return remove.isEnabled();
1023      }
1024      public int getButtonClicked(){
1025  
1026          return buttonClicked;
1027      }
1028  
1029      public void mouseClicked(MouseEvent e) {
1030          if(e.getSource()==add && add.isEnabled())        {addClick();}
1031          if(e.getSource()==upload && upload.isEnabled())    {uploadClick();}
1032          if(e.getSource()==remove && remove.isEnabled())    {removeClick();}
1033          if(e.getSource()==help && help.isEnabled())        {helpClick();}
1034      }
1035  
1036      public void drop(DropTargetDropEvent dtde) {
1037          dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
1038          Transferable trans = dtde.getTransferable();
1039          try {
1040          java.awt.datatransfer.DataFlavor dataFlavour [];
1041          dataFlavour = dtde.getCurrentDataFlavors();
1042          String mimeType;
1043          Vector filesFromDrop = new Vector();
1044          boolean filesFound = false;
1045          while (!filesFound){
1046              for (int i=0; i<dataFlavour.length; i++){/*
1047                  mimeType = dataFlavour[i].getMimeType();
1048                  System.out.println(i+": "+dataFlavour[i].toString());
1049                  System.out.println(i+": "+mimeType);
1050                  System.out.println(i+": "+dataFlavour[i].getPrimaryType());
1051                  System.out.println(i+": "+dataFlavour[i].getHumanPresentableName());
1052                  System.out.println(i+": "+dataFlavour[i].getSubType());*/
1053                  if (dataFlavour[i].isFlavorJavaFileListType()){
1054                      // Windows
1055                      errorMessage("Windows D'n'D");
1056                      List listOfFiles = (List)trans.getTransferData(DataFlavor.javaFileListFlavor);
1057                      Iterator iter = listOfFiles.iterator();
1058                      while (iter.hasNext()) {
1059                          File tempFile = (File) iter.next();
1060                          filesFromDrop.add(tempFile);
1061                      }
1062                      filesFound = true;
1063                  } else if (dataFlavour[i].equals(uriListFlavor)){
1064                      // Linux
1065                      errorMessage("Linux (Mac?) D'n'D");
1066                      BufferedReader in = new BufferedReader(dataFlavour[i].getReaderForText(trans));
1067                      String line = in.readLine();
1068                      while(line!=null && !line.equals("")){
1069                          try {
1070                              File tempFile = new File(new URI(line));
1071                              filesFromDrop.add(tempFile);
1072                          }
1073                          catch (java.net.URISyntaxException usee){;}
1074                          catch (java.lang.IllegalArgumentException iae){;}
1075                          line = in.readLine();
1076                      }
1077                      filesFound = true;
1078                  }
1079              }
1080          }
1081          File [] tempFiles = new File[filesFromDrop.size()];
1082          filesFromDrop.copyInto(tempFiles);
1083          Vector filesForUpload = new Vector();
1084          for (int j=0; j<tempFiles.length; j++){
1085              if (tempFiles[j].isDirectory()){
1086                  File [] subDirFiles = tempFiles[j].listFiles();
1087                  for (int k = 0; k<subDirFiles.length; k++){
1088                      if (subDirFiles[k].isFile())
1089                          filesForUpload.add(subDirFiles[k]);
1090                  }
1091  
1092              } else
1093              filesForUpload.add(tempFiles[j]);
1094          }
1095          if (files == null){
1096              files = new File[0];
1097          }
1098          tempFiles = new File[filesForUpload.size()+files.length];
1099          for (int j=0; j<files.length; j++)
1100              tempFiles[j] = files[j];
1101          for (int j=0; j<filesForUpload.size(); j++){
1102              tempFiles[j+files.length] = (File)filesForUpload.elementAt(j);
1103          }
1104          files = tempFiles;
1105          tableUpdate();
1106  
1107          if (files != null && files.length>0) {
1108              errorMessage("Enabling the upload and remove buttons");
1109              upload.setEnabled(true);
1110              remove.setEnabled(true);
1111          }
1112          if (files !=null && autoUpload){
1113              uploadClick();
1114          }
1115  
1116          }
1117          catch (java.awt.datatransfer.UnsupportedFlavorException usfe){;}
1118          catch (java.io.IOException ioe){;}
1119          dtde.dropComplete(true);
1120      }
1121      public void dropActionChanged(DropTargetDragEvent dtde){;}
1122      public void dragOver(DropTargetDragEvent dtde){;}
1123      public void dragExit(DropTargetEvent dte){;}
1124      public void dragEnter(DropTargetDragEvent dtde){;}
1125  
1126      public void mouseEntered(MouseEvent e){;}
1127      public void mouseExited(MouseEvent e){;}
1128      public void mousePressed(MouseEvent e){;}
1129      public void mouseReleased(MouseEvent e){;}
1130  
1131  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7