use of blue.soundObject.AudioFile in project blue by kunstmusik.
the class ScoreTimelineDropTargetListener method drop.
/*
* (non-Javadoc)
*
* @see java.awt.dnd.DropTargetListener#drop(java.awt.dnd.DropTargetDropEvent)
*/
@Override
public void drop(DropTargetDropEvent dtde) {
try {
Transferable tr = dtde.getTransferable();
if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
dtde.acceptDrop(DnDConstants.ACTION_LINK);
List list = (List) tr.getTransferData(DataFlavor.javaFileListFlavor);
if (list.size() != 1) {
dtde.dropComplete(false);
return;
}
String s = list.get(0).toString().trim();
if (!s.toLowerCase().endsWith("wav") && !s.toLowerCase().endsWith("aif") && !s.toLowerCase().endsWith("aiff")) {
dtde.dropComplete(false);
return;
}
String sObjName = s.substring(s.lastIndexOf(File.separator) + 1);
Point p = dtde.getLocation();
int index = sTimeCanvas.pObj.getLayerNumForY(p.y);
AudioFile af = new AudioFile();
af.setName(sObjName);
af.setSoundFileName(BlueSystem.getRelativePath(s));
PolyObject pObj = sTimeCanvas.getPolyObject();
float startTime = (float) p.x / timeState.getPixelSecond();
float dur = SoundFileUtilities.getDurationInSeconds(s);
af.setStartTime(startTime);
af.setSubjectiveDuration(dur);
pObj.addSoundObject(index, af);
dtde.dropComplete(true);
return;
} else if (dtde.isDataFlavorSupported(DataFlavor.stringFlavor)) {
dtde.acceptDrop(DnDConstants.ACTION_LINK);
String str = (String) tr.getTransferData(DataFlavor.stringFlavor);
if (!str.startsWith("file://")) {
dtde.dropComplete(false);
return;
}
str = str.substring(7).trim();
if (!str.toLowerCase().endsWith("wav") && !str.toLowerCase().endsWith("aif") && !str.toLowerCase().endsWith("aiff")) {
System.err.println("Could not open file: " + str);
dtde.dropComplete(false);
return;
}
str = URLDecoder.decode(str);
str = str.replaceAll(" ", "\\ ");
File f = new File(str);
if (!f.exists()) {
dtde.dropComplete(false);
return;
}
String sObjName = str.substring(str.lastIndexOf(File.separator) + 1);
Point p = dtde.getLocation();
int index = sTimeCanvas.pObj.getLayerNumForY(p.y);
AudioFile af = new AudioFile();
af.setName(sObjName);
af.setSoundFileName(str);
PolyObject pObj = sTimeCanvas.getPolyObject();
float startTime = (float) p.x / timeState.getPixelSecond();
float dur = SoundFileUtilities.getDurationInSeconds(str);
af.setStartTime(startTime);
af.setSubjectiveDuration(dur);
pObj.addSoundObject(index, af);
dtde.dropComplete(true);
return;
}
dtde.rejectDrop();
} catch (UnsupportedFlavorException | IOException | UnsupportedAudioFileException e) {
e.printStackTrace();
dtde.rejectDrop();
}
}
Aggregations