use of ch.cyberduck.binding.application.NSPasteboard in project cyberduck by iterate-ch.
the class BookmarkTableDataSource method tableView_validateDrop_proposedRow_proposedDropOperation.
@Override
public NSUInteger tableView_validateDrop_proposedRow_proposedDropOperation(final NSTableView view, final NSDraggingInfo info, final NSInteger row, final NSUInteger operation) {
NSPasteboard draggingPasteboard = info.draggingPasteboard();
if (!this.getSource().allowsEdit()) {
// Do not allow drags for non writable collections
return NSDraggingInfo.NSDragOperationNone;
} else if (draggingPasteboard.availableTypeFromArray(NSArray.arrayWithObject(NSPasteboard.StringPboardType)) != null) {
String o = draggingPasteboard.stringForType(NSPasteboard.StringPboardType);
if (o != null) {
if (Scheme.isURL(o)) {
view.setDropRow(row, NSTableView.NSTableViewDropAbove);
return NSDraggingInfo.NSDragOperationCopy;
}
}
return NSDraggingInfo.NSDragOperationNone;
} else if (draggingPasteboard.availableTypeFromArray(NSArray.arrayWithObject(NSPasteboard.FilenamesPboardType)) != null) {
final NSObject o = draggingPasteboard.propertyListForType(NSPasteboard.FilenamesPboardType);
if (o != null) {
if (o.isKindOfClass(NSArray.CLASS)) {
final NSArray elements = Rococoa.cast(o, NSArray.class);
for (int i = 0; i < elements.count().intValue(); i++) {
String file = elements.objectAtIndex(new NSUInteger(i)).toString();
if (file.endsWith(".duck")) {
// Allow drag if at least one file is a serialized bookmark
view.setDropRow(row, NSTableView.NSTableViewDropAbove);
return NSDraggingInfo.NSDragOperationCopy;
}
}
// only allow other files if there is at least one bookmark
view.setDropRow(row, NSTableView.NSTableViewDropOn);
return NSDraggingInfo.NSDragOperationCopy;
}
}
return NSDraggingInfo.NSDragOperationNone;
} else if (draggingPasteboard.availableTypeFromArray(NSArray.arrayWithObject(NSPasteboard.URLPboardType)) != null) {
final NSObject o = draggingPasteboard.propertyListForType(NSPasteboard.URLPboardType);
if (o != null) {
if (o.isKindOfClass(NSArray.CLASS)) {
final NSArray elements = Rococoa.cast(o, NSArray.class);
for (int i = 0; i < elements.count().intValue(); i++) {
if (Scheme.isURL(elements.objectAtIndex(new NSUInteger(i)).toString())) {
view.setDropRow(row, NSTableView.NSTableViewDropAbove);
return NSDraggingInfo.NSDragOperationCopy;
}
}
}
}
return NSDraggingInfo.NSDragOperationNone;
} else if (!pasteboard.isEmpty()) {
view.setDropRow(row, NSTableView.NSTableViewDropAbove);
if (log.isDebugEnabled()) {
log.debug(String.format("Drag operation mask is %d", info.draggingSourceOperationMask().intValue()));
}
// We accept any file promise within the bounds
if (info.draggingSourceOperationMask().intValue() == NSDraggingInfo.NSDragOperationCopy.intValue()) {
return NSDraggingInfo.NSDragOperationCopy;
}
return NSDraggingInfo.NSDragOperationMove;
}
return NSDraggingInfo.NSDragOperationNone;
}
use of ch.cyberduck.binding.application.NSPasteboard in project cyberduck by iterate-ch.
the class BookmarkTableDataSource method tableView_acceptDrop_row_dropOperation.
/**
* @param info contains details on this dragging operation.
* @param row The proposed location is row and action is operation. The data source should incorporate the data
* from the dragging pasteboard at this time.
* @see NSTableView.DataSource Invoked by view when the mouse button is released over a table view that previously
* decided to allow a drop.
*/
@Override
public boolean tableView_acceptDrop_row_dropOperation(final NSTableView view, final NSDraggingInfo info, final NSInteger row, final NSUInteger operation) {
NSPasteboard draggingPasteboard = info.draggingPasteboard();
if (log.isDebugEnabled()) {
log.debug(String.format("Accept drop at row %s", row));
}
view.deselectAll(null);
final AbstractHostCollection source = this.getSource();
if (draggingPasteboard.availableTypeFromArray(NSArray.arrayWithObject(NSPasteboard.StringPboardType)) != null) {
String o = draggingPasteboard.stringForType(NSPasteboard.StringPboardType);
if (null == o) {
return false;
}
final Host h;
try {
h = HostParser.parse(o);
} catch (HostParserException e) {
return false;
}
source.add(row.intValue(), h);
view.selectRowIndexes(NSIndexSet.indexSetWithIndex(row), false);
view.scrollRowToVisible(row);
return true;
} else if (draggingPasteboard.availableTypeFromArray(NSArray.arrayWithObject(NSPasteboard.FilenamesPboardType)) != null) {
// We get a drag from another application e.g. Finder.app proposing some files
final NSObject object = draggingPasteboard.propertyListForType(NSPasteboard.FilenamesPboardType);
if (object != null) {
if (object.isKindOfClass(NSArray.CLASS)) {
final NSArray elements = Rococoa.cast(object, NSArray.class);
// If regular files are dropped, these will be uploaded to the dropped bookmark location
final List<TransferItem> uploads = new ArrayList<TransferItem>();
Host host = null;
for (int i = 0; i < elements.count().intValue(); i++) {
final String filename = elements.objectAtIndex(new NSUInteger(i)).toString();
final Local f = LocalFactory.get(filename);
if (filename.endsWith(".duck")) {
// Adding a previously exported bookmark file from the Finder
try {
source.add(row.intValue(), HostReaderFactory.get().read(f));
view.selectRowIndexes(NSIndexSet.indexSetWithIndex(row), true);
view.scrollRowToVisible(row);
} catch (AccessDeniedException e) {
log.error(String.format("Failure reading bookmark from %s. %s", f, e.getMessage()));
continue;
}
} else {
// The bookmark this file has been dropped onto
final Host h = source.get(row.intValue());
if (null == host) {
host = h;
}
// Upload to the remote host this bookmark points to
uploads.add(new TransferItem(new Path(new Path(PathNormalizer.normalize(h.getDefaultPath()), EnumSet.of(Path.Type.directory)), f.getName(), EnumSet.of(Path.Type.file)), f));
}
}
if (!uploads.isEmpty()) {
// If anything has been added to the queue, then process the queue
final Transfer t = new UploadTransfer(host, uploads);
TransferControllerFactory.get().start(t, new TransferOptions());
}
return true;
}
}
} else if (draggingPasteboard.availableTypeFromArray(NSArray.arrayWithObject(NSPasteboard.URLPboardType)) != null) {
final NSObject object = draggingPasteboard.propertyListForType(NSPasteboard.URLPboardType);
if (object != null) {
if (object.isKindOfClass(NSArray.CLASS)) {
final NSArray elements = Rococoa.cast(object, NSArray.class);
for (int i = 0; i < elements.count().intValue(); i++) {
final String url = elements.objectAtIndex(new NSUInteger(i)).toString();
if (StringUtils.isNotBlank(url)) {
final Host h;
try {
h = HostParser.parse(url);
} catch (HostParserException e) {
log.warn(e);
continue;
}
source.add(row.intValue(), h);
view.selectRowIndexes(NSIndexSet.indexSetWithIndex(row), true);
view.scrollRowToVisible(row);
}
}
return true;
}
}
return false;
} else if (!pasteboard.isEmpty()) {
if (info.draggingSourceOperationMask().intValue() == NSDraggingInfo.NSDragOperationCopy.intValue()) {
List<Host> duplicates = new ArrayList<Host>();
for (Host bookmark : pasteboard) {
final Host duplicate = new HostDictionary().deserialize(bookmark.serialize(SerializerFactory.get()));
// Make sure a new UUID is assigned for duplicate
duplicate.setUuid(null);
source.add(row.intValue(), duplicate);
duplicates.add(duplicate);
}
for (Host bookmark : duplicates) {
int index = source.indexOf(bookmark);
view.selectRowIndexes(NSIndexSet.indexSetWithIndex(new NSInteger(index)), true);
view.scrollRowToVisible(new NSInteger(index));
}
} else {
int insert = row.intValue();
for (Host bookmark : pasteboard) {
int previous = source.indexOf(bookmark);
if (previous == insert) {
// No need to move
continue;
}
source.remove(previous);
int moved;
if (previous < insert) {
moved = insert - 1;
} else {
moved = insert;
}
source.add(moved, bookmark);
}
for (Host bookmark : pasteboard) {
int index = source.indexOf(bookmark);
view.selectRowIndexes(NSIndexSet.indexSetWithIndex(new NSInteger(index)), true);
view.scrollRowToVisible(new NSInteger(index));
}
}
return true;
}
return false;
}
use of ch.cyberduck.binding.application.NSPasteboard in project cyberduck by iterate-ch.
the class CopyURLMenuDelegate method handle.
@Override
public void handle(final List<DescriptiveUrl> selected) {
final StringBuilder url = new StringBuilder();
for (Iterator<DescriptiveUrl> iter = selected.iterator(); iter.hasNext(); ) {
url.append(iter.next().getUrl());
if (iter.hasNext()) {
url.append("\n");
}
}
final NSPasteboard pboard = NSPasteboard.generalPasteboard();
pboard.declareTypes(NSArray.arrayWithObject(NSString.stringWithString(NSPasteboard.StringPboardType)), null);
if (!pboard.setStringForType(url.toString(), NSPasteboard.StringPboardType)) {
log.error(String.format("Error writing URL to %s", NSPasteboard.StringPboardType));
}
}
Aggregations