Example usage for com.intellij.openapi.ide KillRingTransferable KillRingTransferable

List of usage examples for com.intellij.openapi.ide KillRingTransferable KillRingTransferable

Introduction

In this page you can find the example usage for com.intellij.openapi.ide KillRingTransferable KillRingTransferable.

Prototype

public KillRingTransferable(@NotNull String data, @NotNull Document document, int startOffset, int endOffset,
        boolean cut) 

Source Link

Document

Creates new KillRingTransferable object.

Usage

From source file:com.intellij.ide.CopyPasteManagerEx.java

License:Apache License

/**
 * Merges given new data with the given old one and returns merge result in case of success.
 *
 * @param newData new data to merge/*ww w. j a  va2 s  .c o m*/
 * @param oldData old data to merge
 * @return merge result of the given data if possible; <code>null</code> otherwise
 * @throws IOException                as defined by {@link Transferable#getTransferData(DataFlavor)}
 * @throws UnsupportedFlavorException as defined by {@link Transferable#getTransferData(DataFlavor)}
 */
@Nullable
private static Transferable merge(@NotNull KillRingTransferable newData, @NotNull KillRingTransferable oldData)
        throws IOException, UnsupportedFlavorException {
    if (!oldData.isReadyToCombine() || !newData.isReadyToCombine()) {
        return null;
    }

    Document document = newData.getDocument();
    if (document == null || document != oldData.getDocument()) {
        return null;
    }

    Object newDataText = newData.getTransferData(DataFlavor.stringFlavor);
    Object oldDataText = oldData.getTransferData(DataFlavor.stringFlavor);
    if (newDataText == null || oldDataText == null) {
        return null;
    }

    if (oldData.isCut()) {
        if (newData.getStartOffset() == oldData.getStartOffset()) {
            return new KillRingTransferable(oldDataText.toString() + newDataText, document,
                    oldData.getStartOffset(), newData.getEndOffset(), newData.isCut());
        }
    }

    if (newData.getStartOffset() == oldData.getEndOffset()) {
        return new KillRingTransferable(oldDataText.toString() + newDataText, document,
                oldData.getStartOffset(), newData.getEndOffset(), false);
    }

    if (newData.getEndOffset() == oldData.getStartOffset()) {
        return new KillRingTransferable(newDataText.toString() + oldDataText, document,
                newData.getStartOffset(), oldData.getEndOffset(), false);
    }

    return null;
}