| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 /* Copyright (C) 2005 Simon David Rycroft 2 3 This program is free software; you can redistribute it and/or 4 modify it under the terms of the GNU General Public License 5 as published by the Free Software Foundation; either version 2 6 of the License, or (at your option) any later version. 7 8 This program is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License 14 along with this program; if not, write to the Free Software 15 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 16 17 import java.awt.BorderLayout; 18 import java.awt.Color; 19 import java.awt.Container; 20 21 import java.awt.dnd.DropTargetListener; 22 import java.awt.dnd.DropTargetDropEvent; 23 import java.awt.dnd.DropTargetDragEvent; 24 import java.awt.dnd.DropTargetEvent; 25 import java.awt.dnd.DropTarget; 26 import java.awt.dnd.DnDConstants; 27 import java.awt.datatransfer.*; 28 29 import java.awt.event.MouseEvent; 30 import java.awt.event.MouseListener; 31 import java.awt.GridLayout; 32 import java.io.File; 33 import java.io.PrintStream; 34 import java.io.BufferedReader; 35 import java.net.URL; 36 import java.net.URI; 37 import java.util.Arrays; 38 import java.util.Iterator; 39 import java.util.List; 40 import java.util.Vector; 41 import javax.swing.BorderFactory; 42 import javax.swing.JApplet; 43 import javax.swing.JButton; 44 import javax.swing.JFileChooser; 45 import javax.swing.JLabel; 46 import javax.swing.JOptionPane; 47 import javax.swing.JPanel; 48 import javax.swing.JProgressBar; 49 import javax.swing.JScrollPane; 50 import javax.swing.JTable; 51 import javax.swing.table.TableColumn; 52 import javax.swing.UIManager; 53 import javax.swing.SwingConstants; 54 import netscape.javascript.JSObject; 55 56 import java.net.UnknownHostException; 57 import java.net.MalformedURLException; 58 import javax.swing.UnsupportedLookAndFeelException; 59 60 public class Main extends JApplet implements MouseListener, DropTargetListener { 61 62 JTable table; 63 JButton add, remove, upload, help; 64 TableData tabledata; 65 TableColumn sizeColumn; 66 File [] files; 67 JLabel progCompletion; 68 JProgressBar progBar; 69 int sentBytes,totalBytes,buttonClicked; 70 Color backgroundColour, columnHeadColourBack, columnHeadColourFore; 71 PostletLabels pLabels; 72 73 // Boolean set to false when a javascript method is executed 74 boolean javascript; 75 76 // Parameters 77 URL endPageURL, helpPageURL, destinationURL; 78 boolean warnMessage, autoUpload; 79 String language,endpage,helppage; 80 int maxThreads; 81 String [] fileExtensions; 82 83 // URI list flavor (Hack for linux) 84 DataFlavor uriListFlavor; 85 86 public void init() { 87 // First thing, output the version, for debugging purposes. 88 System.out.println("POSTLET VERSION: 0.10"); 89 String date = "$Date: 2006-09-14 12:24:14 +0100 (Thu, 14 Sep 2006) $"; 90 System.out.println(date.substring(7,date.length()-1)); 91 92 // URI list flavor: 93 try { 94 uriListFlavor = new DataFlavor("text/uri-list;class=java.lang.String"); 95 } 96 catch (ClassNotFoundException cnfe){ 97 errorMessage(System.out, "No class found for DataFlavor"); 98 } 99 100 // Set the javascript to false, and start listening for clicks 101 javascript = false; 102 JavascriptListener jsListen = new JavascriptListener(this); 103 jsListen.start(); 104 buttonClicked = 0; // Default of add click. 105 106 getParameters(); 107 pLabels = new PostletLabels(language, getCodeBase()); 108 layoutGui(); 109 110 } 111 112 private void layoutGui(){ 113 114 // Set the look of the applet 115 try { 116 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 117 } catch (UnsupportedLookAndFeelException exc){;} catch (IllegalAccessException exc){;} catch (ClassNotFoundException exc){;} catch (InstantiationException exc){;} 118 119 // Get the main pane to add content to. 120 Container pane = getContentPane(); 121 122 // Attempt to add drop listener to the whole applet 123 try { 124 DropTarget dt = new DropTarget(); 125 dt.addDropTargetListener(this); 126 pane.setDropTarget(dt); 127 } catch (java.util.TooManyListenersException tmle){ 128 errorMessage(System.out, "Too many listeners to drop!"); 129 } 130 131 // Table for the adding of Filenames and sizes to. 132 tabledata = new TableData(pLabels.getLabel(0),pLabels.getLabel(1)+" -KB "); 133 table = new JTable(tabledata); 134 table.setColumnSelectionAllowed(false); 135 //table.setDragEnabled(false); // This method is not available to Java 3! 136 sizeColumn = table.getColumn(pLabels.getLabel(1)+" -KB "); 137 sizeColumn.setMaxWidth(100); 138 table.getColumn(pLabels.getLabel(1)+" -KB ").setMinWidth(100); 139 if (columnHeadColourBack != null && backgroundColour != null){ 140 errorMessage(System.out, "setting the tables colours"); 141 table.getTableHeader().setBackground(columnHeadColourBack); 142 table.getTableHeader().setForeground(columnHeadColourFore); 143 table.setBackground(backgroundColour); 144 } 145 JScrollPane scrollPane = new JScrollPane(table); 146 scrollPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); 147 148 // Add the scroll pane/table to the main pane 149 pane.add(scrollPane, BorderLayout.CENTER); 150 151 JPanel rightPanel = new JPanel(new GridLayout(4,1,10,10)); 152 rightPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); 153 154 add = new JButton(pLabels.getLabel(6)); 155 add.addMouseListener(this); 156 rightPanel.add(add); 157 158 remove = new JButton(pLabels.getLabel(7)); 159 remove.addMouseListener(this); 160 remove.setEnabled(false); 161 rightPanel.add(remove); 162 163 upload = new JButton(pLabels.getLabel(8)); 164 upload.addMouseListener(this); 165 upload.setEnabled(false); 166 rightPanel.add(upload); 167 168 help = new JButton(pLabels.getLabel(9)); 169 help.addMouseListener(this); 170 rightPanel.add(help); 171 pane.add(rightPanel,"East"); 172 173 JPanel progPanel = new JPanel(new GridLayout(2, 1)); 174 progPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); 175 176 progCompletion = new JLabel(pLabels.getLabel(10),SwingConstants.CENTER); 177 progPanel.add(progCompletion); 178 179 progBar = new JProgressBar(); 180 progPanel.add(progBar); 181 progPanel.setBorder(BorderFactory.createEmptyBorder(5,25,5,25)); 182 183 if (backgroundColour != null){ 184 pane.setBackground(backgroundColour); 185 rightPanel.setBackground(backgroundColour); 186 scrollPane.setBackground(backgroundColour); 187 progPanel.setBackground(backgroundColour); 188 } 189 // Always set the table background colour as White. 190 // May change this if required, only would require alot of Params! 191 scrollPane.getViewport().setBackground(Color.white); 192 193 pane.add(progPanel,"South"); 194 195 // If the destination has not been set/isn't a proper URL 196 // Then deactivate the buttons. 197 if (destinationURL == null) 198 add.setEnabled(false); 199 } 200 201 public void errorMessage(PrintStream out, String message){ 202 out.println("***"+message+"***"); 203 } 204 205 // Helper method for getting the parameters from the webpage. 206 private void getParameters(){ 207 208 /* LANGUAGE */ 209 try { 210 language = getParameter("language"); 211 if (language == "" || language == null) 212 language = "EN"; 213 } catch (NullPointerException nullLang){ 214 // Default language being set 215 language = "EN"; 216 errorMessage(System.out,"language is null"); 217 } 218 219 /* DESTINATION */ 220 try { 221 destinationURL = new URL(getParameter("destination")); 222 // Following line is for testing, and to hard code the applet to postlet.com 223 //destinationURL = new URL("http://www.postlet.com/example/javaUpload.php"); 224 } catch(java.net.MalformedURLException malurlex){ 225 // Do something here for badly formed destination, which is ESENTIAL. 226 errorMessage(System.out, "Badly formed destination:###"+getParameter("destination")+"###"); 227 JOptionPane.showMessageDialog(null, pLabels.getLabel(3),pLabels.getLabel(5), JOptionPane.ERROR_MESSAGE); 228 } catch(java.lang.NullPointerException npe){ 229 // Do something here for the missing destination, which is ESENTIAL. 230 errorMessage(System.out,"destination is null"); 231 JOptionPane.showMessageDialog(null, pLabels.getLabel(4), pLabels.getLabel(5), JOptionPane.ERROR_MESSAGE); 232 } 233 234 /* BACKGROUND */ 235 try { 236 Integer bgci = new Integer(getParameter("backgroundcolour")); 237 backgroundColour = new Color(bgci.intValue()); 238 } catch(NumberFormatException numfe){ 239 errorMessage(System.out, "background colour is not a number:###"+getParameter("backgroundcolour")+"###"); 240 } catch (NullPointerException nullred){ 241 errorMessage(System.out, "background colour is null"); 242 } 243 244 /* TABLEHEADERFOREGROUND */ 245 try { 246 Integer thfi = new Integer(getParameter("tableheadercolour")); 247 columnHeadColourFore = new Color(thfi.intValue()); 248 } catch(NumberFormatException numfe){ 249 errorMessage(System.out, "table header colour is not a number:###"+getParameter("tableheadcolour")+"###"); 250 } catch (NullPointerException nullred){ 251 errorMessage(System.out, "table header colour is null"); 252 } 253 254 /* TABLEHEADERBACKGROUND */ 255 try { 256 Integer thbi = new Integer(getParameter("tableheaderbackgroundcolour")); 257 columnHeadColourBack = new Color(thbi.intValue()); 258 } catch(NumberFormatException numfe){ 259 errorMessage(System.out, "table header back colour is not a number:###"+getParameter("tableheaderbackgroundcolour")+"###"); 260 } catch (NullPointerException nullred){ 261 errorMessage(System.out, "table header back colour is null"); 262 } 263 264 /* FILEEXTENSIONS */ 265 try { 266 fileExtensions = getParameter("fileextensions").split(","); 267 } catch(NullPointerException nullfileexts){ 268 errorMessage(System.out, "file extensions is null"); 269 } 270 271 /* WARNINGMESSAGE */ 272 try { 273 if (getParameter("warnmessage").toLowerCase() == "true") 274 warnMessage = true; 275 else 276 warnMessage = false; 277 } catch(NullPointerException nullwarnmessage){ 278 errorMessage(System.out, "warnmessage is null"); 279 warnMessage = false; 280 } 281 282 /* AUTOUPLOAD */ 283 try { 284 if (getParameter("autoupload").toLowerCase() == "true") 285 autoUpload = true; 286 else 287 autoUpload = false; 288 } catch(NullPointerException nullwarnmessage){ 289 errorMessage(System.out, "autoUpload is null"); 290 autoUpload = false; 291 } 292 293 /* MAXTHREADS */ 294 try { 295 Integer maxts = new Integer(getParameter("maxthreads")); 296 maxThreads = maxts.intValue(); 297 } catch (NullPointerException nullmaxthreads){ 298 errorMessage(System.out, "maxthreads is null"); 299 } catch (NumberFormatException nummaxthreads){ 300 errorMessage(System.out, "maxthread is not a number");} 301 302 /* ENDPAGE */ 303 try { 304 endPageURL = new URL(getParameter("endpage")); 305 } catch(java.net.MalformedURLException malurlex){ 306 errorMessage(System.out, "endpage is badly formed:###"+getParameter("endpage")+"###"); 307 } catch(java.lang.NullPointerException npe){ 308 errorMessage(System.out, "endpage is null"); 309 } 310 311 /* HELPPAGE */ 312 try { 313 helpPageURL = new URL(getParameter("helppage")); 314 } catch(java.net.MalformedURLException malurlex){ 315 errorMessage(System.out, "helppage is badly formed:###"+getParameter("helppage")+"###"); 316 } catch(java.lang.NullPointerException npe){ 317 errorMessage(System.out, "helppage is null"); 318 } 319 } 320 321 public void removeClick() { 322 if(table.getSelectedRowCount()>0) { 323 File [] fileTemp = new File[files.length-table.getSelectedRowCount()]; 324 int [] selectedRows = table.getSelectedRows(); 325 Arrays.sort(selectedRows); 326 int k=0; 327 for (int i=0; i<files.length;i++){ 328 if (Arrays.binarySearch(selectedRows,i)<0){ 329 fileTemp[k]=files[i]; 330 k++; 331 } 332 } 333 files = fileTemp; 334 tableUpdate(); 335 } 336 if (files.length==0) { 337 upload.setEnabled(false); 338 remove.setEnabled(false); 339 } 340 } 341 342 public void uploadClick() { 343 if(files.length>0) { 344 if (warnMessage){ 345 JOptionPane.showMessageDialog(null, pLabels.getLabel(11), pLabels.getLabel(12), JOptionPane.INFORMATION_MESSAGE); 346 } 347 add.setEnabled(false); 348 remove.setEnabled(false); 349 help.setEnabled(false); 350 upload.setEnabled(false); 351 sentBytes = 0; 352 progBar.setMaximum(totalBytes); 353 progBar.setMinimum(0); 354 UploadManager u; 355 try { 356 u = new UploadManager(files, this, destinationURL, maxThreads); 357 } catch(java.lang.NullPointerException npered){ 358 u = new UploadManager(files, this, destinationURL); 359 } 360 u.start(); 361 } 362 } 363 364 public synchronized void setProgress(int a) { 365 sentBytes += a; 366 progBar.setValue(sentBytes); 367 if (sentBytes == totalBytes){ 368 progCompletion.setText(pLabels.getLabel(2)); 369 if (endPageURL != null){ 370 getAppletContext().showDocument(endPageURL); 371 } else { 372 // Just ignore this error, as it is most likely from the endpage 373 // not being set. 374 // Attempt at calling Javascript after upload is complete. 375 JSObject win = (JSObject) JSObject.getWindow(this); 376 win.eval("postletFinished();"); 377 } 378 // Reset the applet 379 progBar.setValue(0); 380 files = new File[0]; 381 tableUpdate(); 382 add.setEnabled(true); 383 help.setEnabled(true); 384 } 385 } 386 387 public void tableUpdate() { 388 totalBytes = 0; 389 String [] filenames = new String[files.length]; 390 int [] fileSize = new int[files.length]; 391 for(int i=0; i<files.length; i++) { 392 filenames[i] = files[i].getAbsolutePath(); 393 fileSize[i] = (int)files[i].length(); 394 totalBytes += (int)files[i].length(); 395 } 396 int i=0; 397 String [][] rowData = new String[255][2]; 398 while(i<files.length) { 399 rowData[i][0] = files[i].getName(); 400 rowData[i][1] = ""+files[i].length(); 401 i++; 402 } 403 tabledata.formatTable(rowData,i); 404 sizeColumn.setMaxWidth(100); 405 sizeColumn.setMinWidth(100); 406 repaint(); 407 } 408 409 public void addClick() { 410 JFileChooser chooser = new JFileChooser(); 411 412 progBar.setValue(0); 413 if (fileExtensions != null){ 414 UploaderFileFilter filter = new UploaderFileFilter(); 415 for (int i=1; i<fileExtensions.length; i++){ 416 filter.addExtension(fileExtensions[i]); 417 } 418 filter.setDescription(fileExtensions[0]); 419 chooser.addChoosableFileFilter(filter); 420 } 421 else { 422 chooser.setFileFilter(chooser.getAcceptAllFileFilter()); 423 } 424 425 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 426 chooser.setMultiSelectionEnabled(true); 427 chooser.getSelectedFile(); 428 chooser.setDialogTitle(pLabels.getLabel(14)); 429 int returnVal = chooser.showOpenDialog(null); 430 if (returnVal == JFileChooser.APPROVE_OPTION) { 431 File [] tempFiles = chooser.getSelectedFiles(); 432 Vector filesForUpload = new Vector(); 433 for (int i=0; i<tempFiles.length; i++){ 434 if (tempFiles[i].isDirectory()){ 435 File [] subDirFiles = tempFiles[i].listFiles(); 436 for (int j = 0; j<subDirFiles.length; j++){ 437 if (subDirFiles[j].isFile()) 438 filesForUpload.add(subDirFiles[j]); 439 } 440 441 } else 442 filesForUpload.add(tempFiles[i]); 443 } 444 if (files == null){ 445 files = new File[0]; 446 } 447 tempFiles = new File[filesForUpload.size()+files.length]; 448 for (int i=0; i<files.length; i++) 449 tempFiles[i] = files[i]; 450 for (int i=0; i<filesForUpload.size(); i++){ 451 tempFiles[i+files.length] = (File)filesForUpload.elementAt(i); 452 } 453 files = tempFiles; 454 tableUpdate(); 455 } 456 if (files != null && files.length>0) { 457 upload.setEnabled(true); 458 remove.setEnabled(true); 459 } 460 if (files !=null && autoUpload){ 461 uploadClick(); 462 } 463 } 464 465 public void helpClick() { 466 // Open a web page in another frame/window 467 // Unless specified as a parameter, this will be a help page 468 // on the postlet website. 469 470 try { 471 getAppletContext().showDocument(helpPageURL, "_blank"); 472 } catch (NullPointerException nohelppage){ 473 // Show a popup with help instead! 474 try {getAppletContext().showDocument(new URL("http://www.postlet.com/help/"), "_blank");}catch(MalformedURLException mfue){;} 475 } 476 477 } 478 479 public String getCookie(){ 480 481 // Method reads the cookie in from the Browser using the LiveConnect object. 482 // May also add an option to set the cookie using an applet parameter FIXME! 483 try { 484 JSObject win = (JSObject) JSObject.getWindow(this); 485 String cookie = ""+(String)win.eval("document.cookie"); 486 return cookie; 487 } 488 catch (Exception e){ 489 return ""; 490 } 491 } 492 493 public void javascriptAddClicked(){ 494 495 // Set a variable so that the listening thread can call the add click method 496 buttonClicked = 0; 497 javascript = true; 498 } 499 public void javascriptUploadClicked(){ 500 501 // As above 502 buttonClicked = 1; 503 javascript = true; 504 } 505 public boolean getJavascriptStatus(){ 506 507 return javascript; 508 } 509 public void setJavascriptStatus(){ 510 511 javascript = false; 512 } 513 public boolean isUploadEnabled(){ 514 515 return upload.isEnabled(); 516 } 517 public boolean isAddEnabled(){ 518 519 return add.isEnabled(); 520 } 521 public boolean isRemoveEnabled(){ 522 523 return remove.isEnabled(); 524 } 525 public int getButtonClicked(){ 526 527 return buttonClicked; 528 } 529 530 public void mouseClicked(MouseEvent e) { 531 if(e.getSource()==add && add.isEnabled()) {addClick();} 532 if(e.getSource()==upload && upload.isEnabled()) {uploadClick();} 533 if(e.getSource()==remove && remove.isEnabled()) {removeClick();} 534 if(e.getSource()==help && help.isEnabled()) {helpClick();} 535 } 536 537 public void drop(DropTargetDropEvent dtde) { 538 dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); 539 Transferable trans = dtde.getTransferable(); 540 try { 541 java.awt.datatransfer.DataFlavor dataFlavour []; 542 dataFlavour = dtde.getCurrentDataFlavors(); 543 String mimeType; 544 Vector filesFromDrop = new Vector(); 545 boolean filesFound = false; 546 while (!filesFound){ 547 for (int i=0; i<dataFlavour.length; i++){/* 548 mimeType = dataFlavour[i].getMimeType(); 549 System.out.println(i+": "+dataFlavour[i].toString()); 550 System.out.println(i+": "+mimeType); 551 System.out.println(i+": "+dataFlavour[i].getPrimaryType()); 552 System.out.println(i+": "+dataFlavour[i].getHumanPresentableName()); 553 System.out.println(i+": "+dataFlavour[i].getSubType());*/ 554 if (dataFlavour[i].isFlavorJavaFileListType()){ 555 // Windows 556 System.out.println("Windows"); 557 List listOfFiles = (List)trans.getTransferData(DataFlavor.javaFileListFlavor); 558 Iterator iter = listOfFiles.iterator(); 559 while (iter.hasNext()) { 560 File tempFile = (File) iter.next(); 561 filesFromDrop.add(tempFile); 562 } 563 filesFound = true; 564 } else if (dataFlavour[i].equals(uriListFlavor)){ 565 // Linux 566 BufferedReader in = new BufferedReader(dataFlavour[i].getReaderForText(trans)); 567 String line = in.readLine(); 568 while(line!=null && line !=""){ 569 try { 570 File tempFile = new File(new URI(line)); 571 filesFromDrop.add(tempFile); 572 } 573 catch (java.net.URISyntaxException usee){;} 574 catch (java.lang.IllegalArgumentException iae){;} 575 line = in.readLine(); 576 } 577 filesFound = true; 578 } 579 } 580 } 581 File [] tempFiles = new File[filesFromDrop.size()]; 582 filesFromDrop.copyInto(tempFiles); 583 Vector filesForUpload = new Vector(); 584 for (int j=0; j<tempFiles.length; j++){ 585 if (tempFiles[j].isDirectory()){ 586 File [] subDirFiles = tempFiles[j].listFiles(); 587 for (int k = 0; k<subDirFiles.length; k++){ 588 if (subDirFiles[k].isFile()) 589 filesForUpload.add(subDirFiles[k]); 590 } 591 592 } else 593 filesForUpload.add(tempFiles[j]); 594 } 595 if (files == null){ 596 files = new File[0]; 597 } 598 tempFiles = new File[filesForUpload.size()+files.length]; 599 for (int j=0; j<files.length; j++) 600 tempFiles[j] = files[j]; 601 for (int j=0; j<filesForUpload.size(); j++){ 602 tempFiles[j+files.length] = (File)filesForUpload.elementAt(j); 603 } 604 files = tempFiles; 605 tableUpdate(); 606 607 if (files != null && files.length>0) { 608 upload.setEnabled(true); 609 remove.setEnabled(true); 610 } 611 if (files !=null && autoUpload){ 612 uploadClick(); 613 } 614 615 } 616 catch (java.awt.datatransfer.UnsupportedFlavorException usfe){;} 617 catch (java.io.IOException ioe){;} 618 dtde.dropComplete(true); 619 } 620 public void dropActionChanged(DropTargetDragEvent dtde){;} 621 public void dragOver(DropTargetDragEvent dtde){;} 622 public void dragExit(DropTargetEvent dte){;} 623 public void dragEnter(DropTargetDragEvent dtde){;} 624 625 public void mouseEntered(MouseEvent e){;} 626 public void mouseExited(MouseEvent e){;} 627 public void mousePressed(MouseEvent e){;} 628 public void mouseReleased(MouseEvent e){;} 629 630 } 631 632
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |