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

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

Introduction

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

Prototype

public boolean isReadyToCombine() 

Source Link

Usage

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

License:Apache License

/**
 * Stores given content within the current manager. It is merged with already stored ones
 * if necessary (see {@link KillRingTransferable}).
 *
 * @param content content to store//from  www. ja v a2  s. com
 * @return content that is either the given one or the one that was assembled from it and already stored one
 */
@NotNull
private Transferable addNewContentToStack(@NotNull Transferable content) {
    try {
        String clipString = getStringContent(content);
        if (clipString == null) {
            return content;
        }

        if (content instanceof KillRingTransferable) {
            KillRingTransferable killRingContent = (KillRingTransferable) content;
            if (killRingContent.isReadyToCombine() && !myData.isEmpty()) {
                Transferable prev = myData.get(0);
                if (prev instanceof KillRingTransferable) {
                    Transferable merged = merge(killRingContent, (KillRingTransferable) prev);
                    if (merged != null) {
                        myData.set(0, merged);
                        return merged;
                    }
                }
            }
            if (killRingContent.isReadyToCombine()) {
                addToTheTopOfTheStack(killRingContent);
                return killRingContent;
            }
        }

        Transferable same = null;
        for (Transferable old : myData) {
            if (clipString.equals(getStringContent(old))) {
                same = old;
                break;
            }
        }

        if (same == null) {
            addToTheTopOfTheStack(content);
        } else {
            moveContentToStackTop(same);
        }
    } catch (UnsupportedFlavorException ignore) {
    } catch (IOException ignore) {
    }
    return content;
}

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/*from  ww w  .  ja  va  2s  .  c  om*/
 * @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;
}