Created
September 10, 2010 21:55
-
-
Save swinton/574440 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dropify; | |
import processing.core.PApplet; | |
import processing.core.PImage; | |
import java.awt.dnd.*; | |
import java.awt.datatransfer.*; | |
import java.io.BufferedReader; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.Socket; | |
import java.net.UnknownHostException; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.regex.*; | |
public class Dropify extends PApplet implements DropTargetListener { | |
Pattern pattern = Pattern.compile("^(http://open\\.spotify\\.com\\/(:?track|album|artist|(:?user/.+?/playlist))/[a-zA-Z0-9]+)$", Pattern.MULTILINE); | |
List<String> found = new ArrayList<String>(); | |
PImage bgInactive, bgActive; | |
boolean active = false; | |
public void setup() { | |
size(256, 256); | |
DropTarget dt = new DropTarget(this, this); | |
bgInactive = loadImage("Spotify-inactive-256x256.png"); | |
bgActive = loadImage("Spotify-active-256x256.png"); | |
smooth(); | |
noLoop(); | |
} | |
public void draw() { | |
background(213); | |
if (active) { | |
background(bgActive); | |
} else { | |
background(bgInactive); | |
} | |
} | |
public void mouseClicked() { | |
if (true) return; | |
println("clicked! " + active); | |
active = ! active; | |
redraw(); | |
} | |
private String send() { | |
String response = null; | |
try { | |
Socket clientSocket = new Socket("localhost", 1234); | |
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); | |
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); | |
Iterator<String> iter = found.iterator(); | |
StringBuffer message = new StringBuffer(); | |
while (iter.hasNext()) { | |
message.append(iter.next() + "\n"); | |
} | |
outToServer.writeBytes(message.toString()); | |
// response = inFromServer.readLine(); | |
clientSocket.close(); | |
found.clear(); | |
} catch (UnknownHostException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return response; | |
} | |
public void dragEnter(DropTargetDragEvent event) { | |
event.acceptDrag(DnDConstants.ACTION_COPY); | |
active = true; | |
redraw(); | |
} | |
public void dragExit(DropTargetEvent event) { | |
active = false; | |
redraw(); | |
} | |
public void dragOver(DropTargetDragEvent event) { | |
event.acceptDrag(DnDConstants.ACTION_COPY); | |
active = true; | |
redraw(); | |
} | |
public void drop(DropTargetDropEvent event) { | |
event.acceptDrop(DnDConstants.ACTION_COPY); | |
Transferable transferable = event.getTransferable(); | |
DataFlavor flavors[] = transferable.getTransferDataFlavors(); | |
for (int i = 0; i < flavors.length; i++) { | |
try { | |
Object stuff = transferable.getTransferData(flavors[i]); | |
if (stuff instanceof String) { | |
Matcher m = pattern.matcher((String)stuff); | |
while (m.find()) { | |
String s = m.group(0); | |
if (!found.contains(s)) { | |
found.add(s); | |
} | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
System.out.print("\n\n!!! Found: " + found); | |
if (!found.isEmpty()) { | |
event.dropComplete(true); | |
send(); | |
} else { | |
event.dropComplete(false); | |
} | |
active = false; | |
redraw(); | |
} | |
public void dropActionChanged(DropTargetDragEvent event) { | |
// println("dropActionChanged"); | |
} | |
public static void main(String _args[]) { | |
PApplet.main(new String[] { dropify.Dropify.class.getName() }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment