VisClient/org/hfbk/vis/Allesfresser.java

Go to the documentation of this file.
00001 /*
00002  * Created on 01.05.2008
00003  */
00004 package org.hfbk.vis;
00005 
00006 import java.awt.Toolkit;
00007 import java.awt.datatransfer.DataFlavor;
00008 import java.awt.datatransfer.Transferable;
00009 import java.awt.datatransfer.UnsupportedFlavorException;
00010 import java.awt.dnd.DnDConstants;
00011 import java.awt.dnd.DropTargetAdapter;
00012 import java.awt.dnd.DropTargetDropEvent;
00013 import java.io.File;
00014 import java.io.IOException;
00015 import java.util.List;
00016 
00017 import org.dronus.graph.Node;
00018 import org.hfbk.vis.visnode.VisAudio;
00019 import org.hfbk.vis.visnode.VisDir;
00020 import org.hfbk.vis.visnode.VisImage;
00021 import org.hfbk.vis.visnode.VisImagefield;
00022 import org.hfbk.vis.visnode.VisMap;
00023 import org.hfbk.vis.visnode.VisNode;
00024 import org.hfbk.vis.visnode.VisObj;
00025 import org.hfbk.vis.visnode.VisRoute;
00026 import org.hfbk.vis.visnode.VisSound;
00027 import org.hfbk.vis.visnode.VisSrt;
00028 import org.hfbk.vis.visnode.VisText;
00029 import org.hfbk.vis.visnode.VisVideo;
00030 import org.lwjgl.util.vector.Vector3f;
00031 
00057 public class Allesfresser extends DropTargetAdapter {
00058 
00059         VisClient client;
00060 
00061         public Allesfresser(VisClient client) {
00062                 this.client = client;
00063         }
00064 
00068         public void frissClipboard() {
00069                 Transferable tf = Toolkit.getDefaultToolkit().getSystemClipboard()
00070                                 .getContents(null);
00071                 
00072                 friss(tf);
00073         }
00074 
00080         public void drop(DropTargetDropEvent dtde) {
00081                 dtde.acceptDrop(DnDConstants.ACTION_MOVE);
00082                 final Transferable tf = dtde.getTransferable();
00083                 friss(tf);              
00084         }
00085 
00092         public void friss(Transferable tf) {
00093                 try {
00094 
00095                         // DEBUG for (DataFlavor d: tf.getTransferDataFlavors())
00096                         // System.out.println(d);
00097                         String string=null;
00098                         
00099                         // we have some files/dirs (MS Windows, Mac OS X)
00100                         if (tf.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
00101                                 string = "";
00102                                 for (Object f : (List) tf
00103                                                 .getTransferData(DataFlavor.javaFileListFlavor))
00104                                         string += "file://" + ((File) f).getPath() + "\n";
00105                         }else{
00106                                 // don't rely on DataFlavor.stringFlavor, but fetch everything that
00107                                 // could be interpreted as string.
00108                                 DataFlavor stringFlavor = null;
00109                                 for (DataFlavor d : tf.getTransferDataFlavors())
00110                                         if (d.getRepresentationClass() == String.class)
00111                                                 stringFlavor = d;
00112         
00113                                 // we have some string (or files on non-windows)
00114                                 string = (String) tf.getTransferData(stringFlavor);
00115                         }
00116                         
00117                         final String text=string;
00118                         new Thread("Allesfresser DropThread"){
00119                                 public void run() {
00120                                         friss(text);
00121                                 }
00122                         }.start();                      
00123                 } catch (UnsupportedFlavorException e) {
00124                         System.out.println("Drop: format not supported.");
00125                 } catch (IOException e) {
00126                         System.out.println("Drop: can't open " + e);
00127                 }
00128         }
00129 
00130         void friss(String text) {
00131                 // an imagefield to collect incoming pics and movies if any.
00132                 VisNode imf = new VisImagefield(new Node("Dropped"), new Vector3f(
00133                                 client.mouseViewpoint));
00134                 for (String path : text.split("\n")) { // try to interpret any line as
00135                                                                                                 // url / file pathes
00136                         path = path.trim();
00137                         if (path.matches("^(file|http)://[\\s\\S]*")) { // is it a path?
00138                                 VisNode v = frissFile(path);
00139                                 if (v != null)
00140                                         imf.add(v);
00141                         }
00142                 }
00143 
00144                 if (imf.children.size() > 0) { // we found some image(s)
00145                         client.setViewpoint((Vector3f) client.mouseViewpoint, 1);
00146                         if (imf.children.size() > 1) // add the whole field
00147                                 client.root.add(imf);
00148                         else { // pull out the only image and drop this useless imagefield
00149                                 VisNode image = imf.children.get(0);
00150                                 image.position = new Vector3f(client.mouseViewpoint);
00151                                 client.root.add(image);
00152                         }
00153                 } else {
00154                         // at least we just could display the text.
00155                         client.root.add(new VisText(new Node(text), new Vector3f(
00156                                         client.mouseViewpoint)));
00157                         client.setViewpoint((Vector3f) client.mouseViewpoint, 1);
00158                 }
00159         }
00160 
00161         public VisNode frissFile(String path) {
00162                 
00163                 if (Prefs.current.debug) System.out.println(path);
00164                 Node pn = new Node(cleanPath(path));
00165                 
00166                 VisNode v = null;
00167                 // is it an image/video?
00168                 if (path.toLowerCase().matches(
00169                                 ".*\\.(" + Prefs.current.suffixesImage + ")")) {
00170                         v = new VisImage(pn.text, "", new Vector3f(), true);
00171                 } else if (path.toLowerCase().matches(
00172                                 ".*\\.(" + Prefs.current.suffixesAudio + ")")) {
00173                         v = new VisAudio(pn, new Vector3f());
00174                 } else if (path.toLowerCase().matches(
00175                                 ".*\\.(" + Prefs.current.suffixesVideo + ")")) {
00176                         v = new VisVideo(pn, new Vector3f());
00177                 } else if (path.toLowerCase().matches(".*?\\.(wav)(.|\\s)*")) {
00178                         VisSound vs = new VisSound(pn, new Vector3f(client.mouseViewpoint));
00179                         vs.trigger(true);
00180                         v = vs;
00181                 } else if (path.toLowerCase().matches(".*?\\.(txt)(.|\\s)*")) {
00182                         String text = FileUtils.read(pn.text);
00183                         v = new VisText(new Node(text), new Vector3f());
00184                 } else if (path.toLowerCase().matches(".*?\\.(obj)(.|\\s)*")) {
00185                         v = new VisObj(pn, new Vector3f());
00186                         // is it some subtitles file?
00187                 } else if (path.toLowerCase().matches(".*?\\.(srt)(.|\\s)*")) {
00188                         v = new VisSrt(pn, new Vector3f());
00189                 } else if (path.toLowerCase().matches(".*?\\.(coord)(.|\\s)*")) {
00190                         v = new VisMap(pn, new Vector3f());
00191                         // }else if (path.toLowerCase().matches(".*?\\.(coords)(.|\\s)*")){
00192                         // recurseCoords(path.trim(),imf);
00193                 } else if (path.toLowerCase().matches(".*?/gps/.*?\\.(txt)(.|\\s)*")) {
00194                         v = new VisRoute(pn, new Vector3f());
00195                 } else if (path.toLowerCase().matches(".*?\\.(vs)(.|\\s)*")) {
00196                         new VisClientScripter(client).source(cleanPath(path));
00197                 } else if (path.matches("(http)://(.|\\s)*")) {
00198                         client.root.fetch("spider", path, null);
00199                 } else if (path.matches("file:/.*")) {
00200                         if (new File(pn.text).isDirectory())
00201                                 v = new VisDir(pn, new Vector3f());
00202                 }
00203 
00204                 return v;
00205         }
00206 
00208         public static String cleanPath(String path) {
00209 
00210                 if (path.matches("file:/.*?\\:.*?")) {// correct bad windows path in
00211                                                                                                 // uri:
00212                         path = path.replace("file://", "/");// a driver letter is not root
00213                                                                                                 // enough
00214                         // so it gets prefixed by / which we
00215                         // remove here again.
00216                         path = path.replace('\\', '/');
00217                 }
00218                 path = path.replace("file://", "");
00219 
00220                 return path.trim();
00221         }
00222 }

Generated on Tue Apr 7 17:57:20 2009 for visclient by  doxygen 1.5.1