@Override public void actionPerformed(ActionEvent event) { SComponent component = (SComponent)event.getSource(); try { STransferHandler th = component.getTransferHandler(); Clipboard clipboard = component.getSession().getClipboard(); switch (this.name) { case "cut": // cut (MOVE) from component to clipboard th.exportToClipboard(component, clipboard, MOVE); break; case "copy": // copy (COPY) from component to clipboard th.exportToClipboard(component, clipboard, COPY); break; case "paste": // import the data from the clipboard Transferable transferable = clipboard.getContents(null); TransferSupport support = new TransferSupport(component, transferable); th.importData(support); break; } } catch(Exception e) { throw new RuntimeException(e); } } }
/** * Sets a drop action programmatically - it must be supported by the source component and a valid action * @param dropAction */ public void setDropAction(int dropAction) { if(!this.isDrop) throw new IllegalStateException("isDrop was false when setDropAction was called"); int action = dropAction & getSourceDropActions(); if(action != COPY && action != MOVE && action != LINK) { throw new IllegalArgumentException("unsupported drop action " + dropAction); } this.dropAction = dropAction; }
transferSupport.setIsDrop(target, source, mouseEvent, action); if ((transferSupport.getSourceDropActions() & action) == 0) {
private boolean dragStart(SComponent source, STransferHandler sourceTH, SComponent target, int action, SMouseEvent mouseEvent) { SCursor cursor = getSession().getCursor(); // clean transfersupport (in case a old dragging was aborted for any reason and didn't clean up) this.transferSupport = null; // Look for a CustomDragHandler if (sourceTH instanceof CustomDragHandler) { if(((CustomDragHandler) sourceTH).dragStart(source, target, action, mouseEvent)) { return true; } } // This call must and usually will set the TransferSupport sourceTH.exportAsDrag(source, mouseEvent, action); if (this.transferSupport == null) { // abort drag operation return false; } // Start drag operation // Get the Visual Representation Label and use it SLabel label = sourceTH.getVisualRepresentationLabel(getTransferSupport().getTransferable()); if (label != null) { cursor.setLabel(label); } // Show it cursor.setShowCursor(true); return true; }
/** * Exports component's data for a drag-operation - it is required for this method to call * the setTransferSupport Method in the SDragAndDropManager to set a TransferSupport, to be used for this * drag-operation * @param component * @param event * @param action */ public void exportAsDrag(SComponent component, SMouseEvent event, int action) { // (Swing) InputEvent -> (wingS) SMouseEvent int sourceActions = this.getSourceActions(component); // invalid action or requested action != actions allowed by source if((action != MOVE && action != COPY && action != LINK) || (sourceActions & action) == 0) { action = NONE; } if(action != NONE) { Transferable transferable = createTransferable(component, event); component.getSession().getSDragAndDropManager().setTransferSupport(new TransferSupport(component, transferable)); } else { exportDone(component, null, NONE); } }
private void drop(SComponent source, STransferHandler sourceTH, SComponent target, STransferHandler targetTH, int action, SMouseEvent mouseEvent) { // check for the custom drop-handler if (targetTH instanceof CustomDropHandler) { if (((CustomDropHandler) targetTH).drop(source, target, action, mouseEvent)) { return; } } transferSupport.setIsDrop(target, source, mouseEvent, action); if ((transferSupport.getSourceDropActions() & action) == 0) { // if dropping isn't allowed with the requested action resetDndStatus(); return; } // Import the data and call exportDone on the source-TH if (targetTH != null && targetTH.canImport(transferSupport)) { targetTH.importData(transferSupport); sourceTH.exportDone(source, transferSupport.getTransferable(), action); } resetDndStatus(); }
/** * Prepares the TransferSupport to be used in drop cases * @param component * @param event * @param action */ public void setIsDrop(SComponent component, SComponent source, SMouseEvent event, int action) { this.isDrop = true; this.component = component; this.source = source; this.requestedDropAction = action; setDropLocation(this.component, event.getPoint()); }
/** * Returns the drop action to be used - either getUserDropAction in case that none was set programatically * or the action set with setDropAction(int) * @return */ public int getDropAction() { if(dropAction == -1) return getUserDropAction(); return dropAction; }
/** * Returns if this component can import the given Data within a TransferSupport * @param support The TransferSupport to be checked * @return Returns true if the Component can import the data, false if not */ public boolean canImport(STransferHandler.TransferSupport support) { return canImport(support.getComponent(), support.getDataFlavors()); }
@Override public boolean importData(TransferSupport support) { if(support.isDrop()) { int index = ((STextComponent.DropLocation)support.getDropLocation()).getIndex(); ((STextComponent)support.getComponent()).setCaretPosition(index); ((STextComponent)support.getComponent()).setSelectionStart(index); ((STextComponent)support.getComponent()).setSelectionEnd(index); } return super.importData(support); }
/** * Imports the data specified in TransferSupport into the component specified in TransferSupport * @param support * @return */ public boolean importData(STransferHandler.TransferSupport support) { return importData(support.getComponent(), support.getTransferable()); }