From 8145dd47cb94b4c0db3fed10c621bdb89f6b842e Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Thu, 23 Jan 2014 22:23:22 +0400 Subject: [PATCH] IDEA-114381 (check clipboard data flavours before loading the whole content) --- .../intellij/ide/JavaFilePasteProvider.java | 20 +- .../editorActions/PasteHandler.java | 115 ++++------ .../template/macro/ClipboardMacro.java | 22 +- .../execution/impl/ConsoleViewImpl.java | 13 +- .../intellij/ide/FileListPasteProvider.java | 10 +- .../com/intellij/ide/PsiCopyPasteManager.java | 27 +-- .../ide/actions/PasteReferenceProvider.java | 32 +-- .../ide/macro/ClipboardContentMacro.java | 30 +-- .../impl/ProjectViewDropTarget.java | 6 +- .../unscramble/AnalyzeStacktraceUtil.java | 14 +- .../editor/EditorModificationUtil.java | 197 ++++++++++-------- .../openapi/ide/CopyPasteManager.java | 11 +- .../intellij/ide/ClipboardSynchronizer.java | 75 ++++--- .../com/intellij/ide/CopyPasteManagerEx.java | 63 +++--- .../intellij/ide/dnd/FileCopyPasteUtil.java | 19 +- .../actions/ClipboardVsValueContents.java | 15 +- .../openapi/editor/actions/PasteAction.java | 31 ++- .../editor/actions/PasteFromX11Action.java | 44 ++-- .../editor/actions/SimplePasteAction.java | 35 ++-- .../fileEditor/impl/text/FileDropHandler.java | 17 +- .../lang/ant/config/explorer/AntExplorer.java | 4 +- .../MavenProjectsNavigatorPanel.java | 4 +- .../plugins/terminal/JBTerminalPanel.java | 11 +- .../actions/CommonEditActionsProvider.java | 18 +- .../uiDesigner/CutCopyPasteSupport.java | 17 +- 25 files changed, 370 insertions(+), 480 deletions(-) diff --git a/java/java-impl/src/com/intellij/ide/JavaFilePasteProvider.java b/java/java-impl/src/com/intellij/ide/JavaFilePasteProvider.java index 15431a062047..f56df6418b39 100644 --- a/java/java-impl/src/com/intellij/ide/JavaFilePasteProvider.java +++ b/java/java-impl/src/com/intellij/ide/JavaFilePasteProvider.java @@ -15,7 +15,7 @@ */ package com.intellij.ide; -import com.intellij.lang.StdLanguages; +import com.intellij.lang.java.JavaLanguage; import com.intellij.openapi.actionSystem.CommonDataKeys; import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.actionSystem.LangDataKeys; @@ -34,7 +34,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.Transferable; /** * @author yole @@ -127,20 +126,7 @@ public class JavaFilePasteProvider implements PasteProvider { @Nullable private static PsiJavaFile createJavaFileFromClipboardContent(final Project project) { - PsiJavaFile file = null; - Transferable content = CopyPasteManager.getInstance().getContents(); - if (content != null) { - String text = null; - try { - text = (String)content.getTransferData(DataFlavor.stringFlavor); - } - catch (Exception e) { - // ignore; - } - if (text != null) { - file = (PsiJavaFile) PsiFileFactory.getInstance(project).createFileFromText("A.java", StdLanguages.JAVA, text); - } - } - return file; + String text = CopyPasteManager.getInstance().getContents(DataFlavor.stringFlavor); + return text != null ? (PsiJavaFile)PsiFileFactory.getInstance(project).createFileFromText("A.java", JavaLanguage.INSTANCE, text) : null; } } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/PasteHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/PasteHandler.java index 50a52c36d9fd..93c53deac63f 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/PasteHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/PasteHandler.java @@ -27,6 +27,7 @@ import com.intellij.openapi.editor.*; import com.intellij.openapi.editor.actionSystem.EditorActionHandler; import com.intellij.openapi.editor.actionSystem.EditorActionManager; import com.intellij.openapi.editor.actionSystem.EditorTextInsertHandler; +import com.intellij.openapi.editor.actions.PasteAction; import com.intellij.openapi.editor.ex.EditorEx; import com.intellij.openapi.extensions.ExtensionPointName; import com.intellij.openapi.extensions.Extensions; @@ -47,14 +48,13 @@ import com.intellij.util.Producer; import com.intellij.util.containers.HashMap; import com.intellij.util.text.CharArrayUtil; import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.Nullable; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.util.Map; public class PasteHandler extends EditorActionHandler implements EditorTextInsertHandler { - public static final String TRANSFERABLE_PROVIDER = "PasteTransferableProvider"; - private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.editorActions.PasteHandler"); private static final ExtensionPointName EP_NAME = ExtensionPointName.create("com.intellij.customPasteProvider"); @@ -66,21 +66,11 @@ public class PasteHandler extends EditorActionHandler implements EditorTextInser @Override public void execute(final Editor editor, final DataContext dataContext) { - execute(editor, dataContext, new Producer() { - @Override - public Transferable produce() { - CopyPasteManager copyPasteManager = CopyPasteManager.getInstance(); - Transferable contents = copyPasteManager.getContents(); - if (contents != null) { - copyPasteManager.stopKillRings(); - } - return contents; - } - }); + execute(editor, dataContext, null); } @Override - public void execute(final Editor editor, final DataContext dataContext, final Producer transferableProvider) { + public void execute(final Editor editor, final DataContext dataContext, @Nullable final Producer producer) { if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return; final Document document = editor.getDocument(); @@ -88,12 +78,15 @@ public class PasteHandler extends EditorActionHandler implements EditorTextInser return; } - final DataContext context = new DataContext() { - @Override - public Object getData(@NonNls String dataId) { - return TRANSFERABLE_PROVIDER.equals(dataId) ? transferableProvider : dataContext.getData(dataId); - } - }; + DataContext context = dataContext; + if (producer != null) { + context = new DataContext() { + @Override + public Object getData(@NonNls String dataId) { + return PasteAction.TRANSFERABLE_PROVIDER.is(dataId) ? producer : dataContext.getData(dataId); + } + }; + } final Project project = editor.getProject(); if (project == null || editor.isColumnMode() || editor.getSelectionModel().hasBlockSelection()) { @@ -119,7 +112,7 @@ public class PasteHandler extends EditorActionHandler implements EditorTextInser return; } } - doPaste(editor, project, file, document, transferableProvider); + doPaste(editor, project, file, document, producer); } catch (ReadOnlyFragmentModificationException e) { EditorActionManager.getInstance().getReadonlyFragmentModificationHandler(document).handle(e); @@ -133,8 +126,22 @@ public class PasteHandler extends EditorActionHandler implements EditorTextInser final Project project, final PsiFile file, final Document document, - final Producer transferableFunction) { - Transferable content = transferableFunction.produce(); + final Producer producer) { + Transferable content = null; + + if (producer != null) { + content = producer.produce(); + } + else { + CopyPasteManager manager = CopyPasteManager.getInstance(); + if (manager.areDataFlavorsAvailable(DataFlavor.stringFlavor)) { + content = manager.getContents(); + if (content != null) { + manager.stopKillRings(); + } + } + } + if (content != null) { String text = null; try { @@ -148,7 +155,7 @@ public class PasteHandler extends EditorActionHandler implements EditorTextInser final CodeInsightSettings settings = CodeInsightSettings.getInstance(); final Map extraData = new HashMap(); - for(CopyPastePostProcessor processor: Extensions.getExtensions(CopyPastePostProcessor.EP_NAME)) { + for (CopyPastePostProcessor processor : Extensions.getExtensions(CopyPastePostProcessor.EP_NAME)) { TextBlockTransferableData data = processor.extractTransferableData(content); if (data != null) { extraData.put(processor, data); @@ -173,23 +180,11 @@ public class PasteHandler extends EditorActionHandler implements EditorTextInser blockIndentAnchorColumn = col; } - // We assume that EditorModificationUtil.insertStringAtCaret() is smart enough to understand that text that is currently - // selected at editor (if any) should be removed. - // - //if (selectionModel.hasSelection()) { - // ApplicationManager.getApplication().runWriteAction( - // new Runnable() { - // public void run() { - // EditorModificationUtil.deleteSelectedText(editor); - // } - // } - // ); - //} + // We assume that EditorModificationUtil.insertStringAtCaret() is smart enough to remove currently selected text (if any). RawText rawText = RawText.fromTransferable(content); - String newText = text; - for(CopyPastePreProcessor preProcessor: Extensions.getExtensions(CopyPastePreProcessor.EP_NAME)) { + for (CopyPastePreProcessor preProcessor : Extensions.getExtensions(CopyPastePreProcessor.EP_NAME)) { newText = preProcessor.preprocessOnPaste(project, file, editor, newText, rawText); } int indentOptions = text.equals(newText) ? settings.REFORMAT_ON_PASTE : CodeInsightSettings.REFORMAT_BLOCK; @@ -199,18 +194,17 @@ public class PasteHandler extends EditorActionHandler implements EditorTextInser indentOptions = CodeInsightSettings.INDENT_BLOCK; } - int length = text.length(); - final String text1 = text; - + final String _text = text; ApplicationManager.getApplication().runWriteAction( new Runnable() { @Override public void run() { - EditorModificationUtil.insertStringAtCaret(editor, text1, false, true); + EditorModificationUtil.insertStringAtCaret(editor, _text, false, true); } } ); + int length = text.length(); int offset = caretModel.getOffset() - length; if (offset < 0) { length += offset; @@ -223,7 +217,7 @@ public class PasteHandler extends EditorActionHandler implements EditorTextInser selectionModel.removeSelection(); final Ref indented = new Ref(Boolean.FALSE); - for(Map.Entry e: extraData.entrySet()) { + for (Map.Entry e : extraData.entrySet()) { //noinspection unchecked e.getKey().processTransferableData(project, editor, bounds, caretOffset, indented, e.getValue()); } @@ -285,41 +279,6 @@ public class PasteHandler extends EditorActionHandler implements EditorTextInser else { indentPlainTextBlock(document, startOffset, endOffset, originalCaretCol); } - - - //boolean hasNewLine = false; - //for (int i = endOffset - 1; i >= startOffset; i--) { - // char c = chars.charAt(i); - // if (c == '\n' || c == '\r') { - // hasNewLine = true; - // break; - // } - // if (c != ' ' && c != '\t') return; // do not indent if does not end with line separator - //} - // - //if (!hasNewLine) return; - //int lineStart = CharArrayUtil.shiftBackwardUntil(chars, startOffset - 1, "\n\r") + 1; - //int spaceEnd = CharArrayUtil.shiftForward(chars, lineStart, " \t"); - //if (startOffset <= spaceEnd) { // we are in starting spaces - // if (lineStart != startOffset) { - // String deletedS = chars.subSequence(lineStart, startOffset).toString(); - // document.deleteString(lineStart, startOffset); - // startOffset = lineStart; - // endOffset -= deletedS.length(); - // document.insertString(endOffset, deletedS); - // LogicalPosition pos = new LogicalPosition(editor.getCaretModel().getLogicalPosition().line, originalCaretCol); - // editor.getCaretModel().moveToLogicalPosition(pos); - // } - // - // PsiDocumentManager.getInstance(project).commitAllDocuments(); - // PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(document); - // if (LanguageFormatting.INSTANCE.forContext(file) != null) { - // indentBlockWithFormatter(project, document, startOffset, endOffset, file); - // } - // else { - // indentPlainTextBlock(document, startOffset, endOffset, originalCaretCol); - // } - //} } private static void indentEachLine(Project project, Editor editor, int startOffset, int endOffset) { diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/macro/ClipboardMacro.java b/platform/lang-impl/src/com/intellij/codeInsight/template/macro/ClipboardMacro.java index 01f0800ea7db..f1c3906e7385 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/macro/ClipboardMacro.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/macro/ClipboardMacro.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,9 +20,6 @@ import com.intellij.codeInsight.template.ExpressionContext; import com.intellij.openapi.ide.CopyPasteManager; import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.Transferable; -import java.awt.datatransfer.UnsupportedFlavorException; -import java.io.IOException; /** * @author yole @@ -34,20 +31,7 @@ public class ClipboardMacro extends SimpleMacro { @Override protected String evaluateSimpleMacro(Expression[] params, ExpressionContext context) { - Transferable contents = CopyPasteManager.getInstance().getContents(); - if (contents != null) { - String result = ""; - try { - result = (String) contents.getTransferData(DataFlavor.stringFlavor); - } - catch (UnsupportedFlavorException ignored) { - } - catch (IOException ignored) { - } - if (result != null) { - return result; - } - } - return ""; + String text = CopyPasteManager.getInstance().getContents(DataFlavor.stringFlavor); + return text != null ? text : ""; } } diff --git a/platform/lang-impl/src/com/intellij/execution/impl/ConsoleViewImpl.java b/platform/lang-impl/src/com/intellij/execution/impl/ConsoleViewImpl.java index 24b037da2202..c5ad2799230e 100644 --- a/platform/lang-impl/src/com/intellij/execution/impl/ConsoleViewImpl.java +++ b/platform/lang-impl/src/com/intellij/execution/impl/ConsoleViewImpl.java @@ -79,7 +79,6 @@ import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.awt.*; import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.Transferable; import java.awt.event.MouseEvent; import java.io.IOException; import java.util.*; @@ -661,7 +660,7 @@ public class ConsoleViewImpl extends JPanel implements ConsoleView, ObservableCo if (cycleUsed) { clearHyperlinkAndFoldings(); } - + CommandProcessor.getInstance().executeCommand(myProject, new Runnable() { @Override public void run() { @@ -1262,15 +1261,7 @@ public class ConsoleViewImpl extends JPanel implements ConsoleView, ObservableCo private static class PasteHandler extends ConsoleAction { @Override public void execute(final ConsoleViewImpl consoleView, final DataContext context) { - final Transferable content = CopyPasteManager.getInstance().getContents(); - if (content == null) return; - String s = null; - try { - s = (String)content.getTransferData(DataFlavor.stringFlavor); - } - catch (Exception e) { - consoleView.getToolkit().beep(); - } + String s = CopyPasteManager.getInstance().getContents(DataFlavor.stringFlavor); if (s == null) return; ApplicationManager.getApplication().assertIsDispatchThread(); Editor editor = consoleView.myEditor; diff --git a/platform/lang-impl/src/com/intellij/ide/FileListPasteProvider.java b/platform/lang-impl/src/com/intellij/ide/FileListPasteProvider.java index 16a19ebba113..8d3a2005ff1e 100644 --- a/platform/lang-impl/src/com/intellij/ide/FileListPasteProvider.java +++ b/platform/lang-impl/src/com/intellij/ide/FileListPasteProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,6 @@ import com.intellij.ide.dnd.LinuxDragAndDropSupport; import com.intellij.openapi.actionSystem.CommonDataKeys; import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.actionSystem.LangDataKeys; -import com.intellij.openapi.actionSystem.PlatformDataKeys; import com.intellij.openapi.ide.CopyPasteManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.LocalFileSystem; @@ -50,6 +49,8 @@ public class FileListPasteProvider implements PasteProvider { final IdeView ideView = LangDataKeys.IDE_VIEW.getData(dataContext); if (project == null || ideView == null) return; + if (!FileCopyPasteUtil.isFileListFlavorAvailable()) return; + final Transferable contents = CopyPasteManager.getInstance().getContents(); if (contents == null) return; final List fileList = FileCopyPasteUtil.getFileList(contents); @@ -88,8 +89,7 @@ public class FileListPasteProvider implements PasteProvider { @Override public boolean isPasteEnabled(@NotNull DataContext dataContext) { - final Transferable contents = CopyPasteManager.getInstance().getContents(); - final IdeView ideView = LangDataKeys.IDE_VIEW.getData(dataContext); - return contents != null && FileCopyPasteUtil.isFileListFlavorSupported(contents) && ideView != null; + return LangDataKeys.IDE_VIEW.getData(dataContext) != null && + FileCopyPasteUtil.isFileListFlavorAvailable(); } } diff --git a/platform/lang-impl/src/com/intellij/ide/PsiCopyPasteManager.java b/platform/lang-impl/src/com/intellij/ide/PsiCopyPasteManager.java index f42623be4218..39892efe708a 100644 --- a/platform/lang-impl/src/com/intellij/ide/PsiCopyPasteManager.java +++ b/platform/lang-impl/src/com/intellij/ide/PsiCopyPasteManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package com.intellij.ide; import com.intellij.ide.dnd.LinuxDragAndDropSupport; @@ -67,26 +66,10 @@ public class PsiCopyPasteManager { }); } - @Nullable public PsiElement[] getElements(boolean[] isCopied) { try { - Transferable content = myCopyPasteManager.getContents(); - if (content == null) { - return null; - } - - Object transferData; - try { - transferData = content.getTransferData(ourDataFlavor); - } - catch (UnsupportedFlavorException e) { - return null; - } - catch (IOException e) { - return null; - } - + Object transferData = myCopyPasteManager.getContents(ourDataFlavor); if (!(transferData instanceof MyData)) { return null; } @@ -100,9 +83,7 @@ public class PsiCopyPasteManager { return myRecentData.getElements(); } catch (Exception e) { - if (LOG.isDebugEnabled()) { - LOG.debug(e); - } + LOG.debug(e); return null; } } @@ -278,7 +259,7 @@ public class PsiCopyPasteManager { } } else if (flavor.equals(LinuxDragAndDropSupport.kdeCutMarkFlavor) && !myDataProxy.isCopied()) { - return new ByteArrayInputStream("1".getBytes()); + return new ByteArrayInputStream("1".getBytes(CharsetToolkit.UTF8_CHARSET)); } return null; diff --git a/platform/lang-impl/src/com/intellij/ide/actions/PasteReferenceProvider.java b/platform/lang-impl/src/com/intellij/ide/actions/PasteReferenceProvider.java index c3a685c956f8..00322d26b48c 100644 --- a/platform/lang-impl/src/com/intellij/ide/actions/PasteReferenceProvider.java +++ b/platform/lang-impl/src/com/intellij/ide/actions/PasteReferenceProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,21 +13,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package com.intellij.ide.actions; import com.intellij.codeInsight.FileModificationService; -import com.intellij.codeInsight.editorActions.PasteHandler; import com.intellij.ide.IdeBundle; import com.intellij.ide.PasteProvider; import com.intellij.openapi.actionSystem.CommonDataKeys; import com.intellij.openapi.actionSystem.DataContext; -import com.intellij.openapi.actionSystem.PlatformDataKeys; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.command.CommandProcessor; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.EditorModificationUtil; +import com.intellij.openapi.editor.actions.PasteAction; import com.intellij.openapi.extensions.Extensions; import com.intellij.openapi.ide.CopyPasteManager; import com.intellij.openapi.project.Project; @@ -114,30 +112,20 @@ public class PasteReferenceProvider implements PasteProvider { } @Nullable - private static String getCopiedFqn(final DataContext dataContext) { - @SuppressWarnings("unchecked") - final Producer transferableProducer = (Producer)dataContext.getData(PasteHandler.TRANSFERABLE_PROVIDER); - if (transferableProducer == null) return null; + private static String getCopiedFqn(final DataContext context) { + Producer producer = PasteAction.TRANSFERABLE_PROVIDER.getData(context); - final Transferable transferable = transferableProducer.produce(); - if (transferable != null) { - try { - return (String)transferable.getTransferData(CopyReferenceAction.ourFlavor); - } - catch (Exception ignored) { } - } - - final CopyPasteManager manager = CopyPasteManager.getInstance(); - if (manager.isDataFlavorAvailable(CopyReferenceAction.ourFlavor)) { - final Transferable contents = manager.getContents(); - if (contents != null) { + if (producer != null) { + Transferable transferable = producer.produce(); + if (transferable != null) { try { - return (String)contents.getTransferData(CopyReferenceAction.ourFlavor); + return (String)transferable.getTransferData(CopyReferenceAction.ourFlavor); } catch (Exception ignored) { } } + return null; } - return null; + return CopyPasteManager.getInstance().getContents(CopyReferenceAction.ourFlavor); } } diff --git a/platform/lang-impl/src/com/intellij/ide/macro/ClipboardContentMacro.java b/platform/lang-impl/src/com/intellij/ide/macro/ClipboardContentMacro.java index 8e926537ea8f..a758507c8a50 100644 --- a/platform/lang-impl/src/com/intellij/ide/macro/ClipboardContentMacro.java +++ b/platform/lang-impl/src/com/intellij/ide/macro/ClipboardContentMacro.java @@ -1,20 +1,31 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.intellij.ide.macro; import com.intellij.ide.IdeBundle; import com.intellij.openapi.actionSystem.DataContext; -import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.ide.CopyPasteManager; import org.jetbrains.annotations.Nullable; import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.Transferable; /** * @author nik */ public class ClipboardContentMacro extends Macro { - private static final Logger LOG = Logger.getInstance(ClipboardContentMacro.class); - @Override public String getName() { return "ClipboardContent"; @@ -28,15 +39,6 @@ public class ClipboardContentMacro extends Macro { @Nullable @Override public String expand(DataContext dataContext) throws ExecutionCancelledException { - Transferable contents = CopyPasteManager.getInstance().getContents(); - if (contents == null) return null; - - try { - return (String)contents.getTransferData(DataFlavor.stringFlavor); - } - catch (Exception e) { - LOG.info(e); - return null; - } + return CopyPasteManager.getInstance().getContents(DataFlavor.stringFlavor); } } diff --git a/platform/lang-impl/src/com/intellij/ide/projectView/impl/ProjectViewDropTarget.java b/platform/lang-impl/src/com/intellij/ide/projectView/impl/ProjectViewDropTarget.java index ce9de80aff35..369301ab1e76 100644 --- a/platform/lang-impl/src/com/intellij/ide/projectView/impl/ProjectViewDropTarget.java +++ b/platform/lang-impl/src/com/intellij/ide/projectView/impl/ProjectViewDropTarget.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -81,7 +81,7 @@ class ProjectViewDropTarget implements DnDNativeTarget { if (targetNode == null || (dropAction & DnDConstants.ACTION_COPY_OR_MOVE) == 0) { return false; } - else if (sourceNodes == null && !FileCopyPasteUtil.isFileListFlavorSupported(event)) { + else if (sourceNodes == null && !FileCopyPasteUtil.isFileListFlavorAvailable(event)) { return false; } else if (sourceNodes != null && ArrayUtilRt.find(sourceNodes, targetNode) != -1) { @@ -124,7 +124,7 @@ class ProjectViewDropTarget implements DnDNativeTarget { assert targetNode != null; final int dropAction = event.getAction().getActionId(); if (sourceNodes == null) { - if (FileCopyPasteUtil.isFileListFlavorSupported(event)) { + if (FileCopyPasteUtil.isFileListFlavorAvailable(event)) { List fileList = FileCopyPasteUtil.getFileListFromAttachedObject(attached); if (!fileList.isEmpty()) { getDropHandler(dropAction).doDropFiles(fileList, targetNode); diff --git a/platform/lang-impl/src/com/intellij/unscramble/AnalyzeStacktraceUtil.java b/platform/lang-impl/src/com/intellij/unscramble/AnalyzeStacktraceUtil.java index 6bf36675a6ac..eaf4e0e7b5bd 100644 --- a/platform/lang-impl/src/com/intellij/unscramble/AnalyzeStacktraceUtil.java +++ b/platform/lang-impl/src/com/intellij/unscramble/AnalyzeStacktraceUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,7 +48,6 @@ import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.awt.*; import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.Transferable; /** * @author yole @@ -67,16 +66,7 @@ public class AnalyzeStacktraceUtil { @Nullable public static String getTextInClipboard() { - final CopyPasteManager copyPasteManager = CopyPasteManager.getInstance(); - if (copyPasteManager.isDataFlavorAvailable(DataFlavor.stringFlavor)) { - final Transferable contents = copyPasteManager.getContents(); - if (contents != null) { - try { - return (String)contents.getTransferData(DataFlavor.stringFlavor); - } catch (Exception ignore) { } - } - } - return null; + return CopyPasteManager.getInstance().getContents(DataFlavor.stringFlavor); } public interface ConsoleFactory { diff --git a/platform/platform-api/src/com/intellij/openapi/editor/EditorModificationUtil.java b/platform/platform-api/src/com/intellij/openapi/editor/EditorModificationUtil.java index ff1e8a82aa34..7507cf525ef6 100644 --- a/platform/platform-api/src/com/intellij/openapi/editor/EditorModificationUtil.java +++ b/platform/platform-api/src/com/intellij/openapi/editor/EditorModificationUtil.java @@ -23,8 +23,8 @@ import com.intellij.openapi.ide.CopyPasteManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.text.LineTokenizer; -import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiDocumentManager; +import com.intellij.util.Producer; import org.jetbrains.annotations.Nullable; import java.awt.datatransfer.DataFlavor; @@ -34,7 +34,7 @@ import java.io.IOException; import java.util.List; public class EditorModificationUtil { - private EditorModificationUtil() {} + private EditorModificationUtil() { } public static void deleteSelectedText(Editor editor) { SelectionModel selectionModel = editor.getSelectionModel(); @@ -133,97 +133,86 @@ public class EditorModificationUtil { } @Nullable - public static TextRange pasteFromClipboard(Editor editor) { - return pasteFromTransferrable(CopyPasteManager.getInstance().getContents(), editor); + public static TextRange pasteTransferable(Editor editor, @Nullable Producer producer) { + String text = getStringContent(producer); + if (text == null) return null; + + int caretOffset = editor.getCaretModel().getOffset(); + insertStringAtCaret(editor, text, false, true); + return new TextRange(caretOffset, caretOffset + text.length()); + } + + public static void pasteTransferableAsBlock(Editor editor, @Nullable Producer producer) { + String text = getStringContent(producer); + if (text == null) return; + + int caretLine = editor.getCaretModel().getLogicalPosition().line; + int originalCaretLine = caretLine; + int selectedLinesCount = 0; + + final SelectionModel selectionModel = editor.getSelectionModel(); + if (selectionModel.hasBlockSelection()) { + final LogicalPosition start = selectionModel.getBlockStart(); + final LogicalPosition end = selectionModel.getBlockEnd(); + assert start != null; + assert end != null; + LogicalPosition caret = new LogicalPosition(Math.min(start.line, end.line), Math.min(start.column, end.column)); + selectedLinesCount = Math.abs(end.line - start.line); + caretLine = caret.line; + + deleteSelectedText(editor); + editor.getCaretModel().moveToLogicalPosition(caret); + } + + LogicalPosition caretToRestore = editor.getCaretModel().getLogicalPosition(); + + String[] lines = LineTokenizer.tokenize(text.toCharArray(), false); + if (lines.length > 1 || selectedLinesCount == 0) { + int longestLineLength = 0; + for (int i = 0; i < lines.length; i++) { + String line = lines[i]; + longestLineLength = Math.max(longestLineLength, line.length()); + editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(caretLine + i, caretToRestore.column)); + insertStringAtCaret(editor, line, false, true); + } + caretToRestore = new LogicalPosition(originalCaretLine, caretToRestore.column + longestLineLength); + } + else { + for (int i = 0; i <= selectedLinesCount; i++) { + editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(caretLine + i, caretToRestore.column)); + insertStringAtCaret(editor, text, false, true); + } + caretToRestore = new LogicalPosition(originalCaretLine, caretToRestore.column + text.length()); + } + + editor.getCaretModel().moveToLogicalPosition(caretToRestore); + zeroWidthBlockSelectionAtCaretColumn(editor, caretLine, caretLine + selectedLinesCount); } @Nullable - public static TextRange pasteFromTransferrable(Transferable content, Editor editor) { - if (content != null) { - try { - String s = getStringContent(content); - - int caretOffset = editor.getCaretModel().getOffset(); - insertStringAtCaret(editor, s, false, true); - return new TextRange(caretOffset, caretOffset + s.length()); - } catch (Exception exception) { - editor.getComponent().getToolkit().beep(); - } - } - - return null; - } - - private static String getStringContent(final Transferable content) throws UnsupportedFlavorException, IOException { - RawText raw = RawText.fromTransferable(content); - String s; - if (raw != null) { - s = raw.rawText; + private static String getStringContent(@Nullable Producer producer) { + Transferable content = null; + if (producer != null) { + content = producer.produce(); } else { - s = (String)content.getTransferData(DataFlavor.stringFlavor); - } - - s = StringUtil.convertLineSeparators(s); - return s; - } - - public static void pasteFromClipboardAsBlock(Editor editor) { - pasteTransferableAsBlock(editor, null); - } - - public static void pasteTransferableAsBlock(Editor editor, @Nullable Transferable content) { - if (content == null) { - content = CopyPasteManager.getInstance().getContents(); - } - - if (content != null) { - try { - int caretLine = editor.getCaretModel().getLogicalPosition().line; - int originalCaretLine = caretLine; - - int selectedLinesCount = 0; - final SelectionModel selectionModel = editor.getSelectionModel(); - if (selectionModel.hasBlockSelection()) { - final LogicalPosition start = selectionModel.getBlockStart(); - final LogicalPosition end = selectionModel.getBlockEnd(); - assert start != null; - assert end != null; - LogicalPosition caret = new LogicalPosition(Math.min(start.line, end.line), Math.min(start.column, end.column)); - selectedLinesCount = Math.abs(end.line - start.line); - caretLine = caret.line; - - deleteSelectedText(editor); - editor.getCaretModel().moveToLogicalPosition(caret); - } - - LogicalPosition caretToRestore = editor.getCaretModel().getLogicalPosition(); - String s = getStringContent(content); - - String[] lines = LineTokenizer.tokenize(s.toCharArray(), false); - if (lines.length > 1 || selectedLinesCount == 0) { - int longestLineLength = 0; - for (int i = 0; i < lines.length; i++) { - String line = lines[i]; - longestLineLength = Math.max(longestLineLength, line.length()); - editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(caretLine + i, caretToRestore.column)); - insertStringAtCaret(editor, line, false, true); - } - caretToRestore = new LogicalPosition(originalCaretLine, caretToRestore.column + longestLineLength); - } - else { - for (int i = 0; i <= selectedLinesCount; i++) { - editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(caretLine + i, caretToRestore.column)); - insertStringAtCaret(editor, s, false, true); - } - caretToRestore = new LogicalPosition(originalCaretLine, caretToRestore.column + s.length()); - } - editor.getCaretModel().moveToLogicalPosition(caretToRestore); - zeroWidthBlockSelectionAtCaretColumn(editor, caretLine, caretLine + selectedLinesCount); - } catch (Exception exception) { - editor.getComponent().getToolkit().beep(); + CopyPasteManager manager = CopyPasteManager.getInstance(); + if (manager.areDataFlavorsAvailable(DataFlavor.stringFlavor)) { + content = manager.getContents(); } } + if (content == null) return null; + + RawText raw = RawText.fromTransferable(content); + if (raw != null) return raw.rawText; + + try { + return (String)content.getTransferData(DataFlavor.stringFlavor); + } + catch (UnsupportedFlavorException ignore) { } + catch (IOException ignore) { } + + return null; } /** @@ -361,4 +350,40 @@ public class EditorModificationUtil { insertStringAtCaret(editor, str, toProcessOverwriteMode, true); } } + + /** @deprecated use {@link #pasteTransferable(Editor, Producer)} (to remove in IDEA 14) */ + @SuppressWarnings("UnusedDeclaration") + public static TextRange pasteFromClipboard(Editor editor) { + return pasteTransferable(editor, null); + } + + /** @deprecated use {@link #pasteTransferable(Editor, Producer)} (to remove in IDEA 14) */ + @SuppressWarnings("SpellCheckingInspection,UnusedDeclaration") + public static TextRange pasteFromTransferrable(final Transferable content, Editor editor) { + return pasteTransferable(editor, new Producer() { + @Nullable + @Override + public Transferable produce() { + return content; + } + }); + } + + @SuppressWarnings("UnusedDeclaration") + /** @deprecated use {@link #pasteTransferableAsBlock(Editor, Producer)} (to remove in IDEA 14) */ + public static void pasteFromClipboardAsBlock(Editor editor) { + pasteTransferableAsBlock(editor, (Producer)null); + } + + @SuppressWarnings("UnusedDeclaration") + /** @deprecated use {@link #pasteTransferableAsBlock(Editor, Producer)} (to remove in IDEA 14) */ + public static void pasteTransferableAsBlock(Editor editor, @Nullable final Transferable content) { + pasteTransferableAsBlock(editor, new Producer() { + @Nullable + @Override + public Transferable produce() { + return content; + } + }); + } } diff --git a/platform/platform-api/src/com/intellij/openapi/ide/CopyPasteManager.java b/platform/platform-api/src/com/intellij/openapi/ide/CopyPasteManager.java index 968082c77fe0..c96ce0ae3f81 100644 --- a/platform/platform-api/src/com/intellij/openapi/ide/CopyPasteManager.java +++ b/platform/platform-api/src/com/intellij/openapi/ide/CopyPasteManager.java @@ -39,11 +39,20 @@ public abstract class CopyPasteManager { public abstract void removeContentChangedListener(ContentChangedListener listener); - public abstract boolean isDataFlavorAvailable(@Nullable DataFlavor flavor); + /** @deprecated use {@link #getContents(DataFlavor)} or {@link #areDataFlavorsAvailable(DataFlavor...)} (to remove in IDEA 14) */ + @SuppressWarnings("unused") + public boolean isDataFlavorAvailable(@Nullable DataFlavor flavor) { + return flavor != null && areDataFlavorsAvailable(flavor); + } + + public abstract boolean areDataFlavorsAvailable(@NotNull DataFlavor... flavors); @Nullable public abstract Transferable getContents(); + @Nullable + public abstract T getContents(@NotNull DataFlavor flavor); + public abstract Transferable[] getAllContents(); public abstract void setContents(@NotNull Transferable content); diff --git a/platform/platform-impl/src/com/intellij/ide/ClipboardSynchronizer.java b/platform/platform-impl/src/com/intellij/ide/ClipboardSynchronizer.java index d46b868f8d02..562afc37ea15 100644 --- a/platform/platform-impl/src/com/intellij/ide/ClipboardSynchronizer.java +++ b/platform/platform-impl/src/com/intellij/ide/ClipboardSynchronizer.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,7 +26,6 @@ import com.intellij.openapi.util.registry.Registry; import com.intellij.ui.mac.foundation.Foundation; import com.intellij.ui.mac.foundation.ID; import com.sun.jna.IntegerType; -import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import sun.awt.datatransfer.DataTransferer; @@ -56,10 +55,6 @@ import java.util.Set; public class ClipboardSynchronizer implements ApplicationComponent { private static final Logger LOG = Logger.getInstance("#com.intellij.ide.ClipboardSynchronizer"); - @NonNls private static final String DATA_TRANSFER_TIMEOUT_PROPERTY = "sun.awt.datatransfer.timeout"; - @NonNls private static final String LONG_TIMEOUT = "2000"; - @NonNls private static final String SHORT_TIMEOUT = "100"; - private final ClipboardHandler myClipboardHandler; public static ClipboardSynchronizer getInstance() { @@ -97,9 +92,9 @@ public class ClipboardSynchronizer implements ApplicationComponent { return "ClipboardSynchronizer"; } - public boolean isDataFlavorAvailable(@NotNull final DataFlavor dataFlavor) { + public boolean areDataFlavorsAvailable(@NotNull DataFlavor... flavors) { try { - return myClipboardHandler.isDataFlavorAvailable(dataFlavor); + return myClipboardHandler.areDataFlavorsAvailable(flavors); } catch (IllegalStateException e) { LOG.info(e); @@ -128,14 +123,18 @@ public class ClipboardSynchronizer implements ApplicationComponent { private static class ClipboardHandler { - public void init() { - } + public void init() { } - public void dispose() { - } + public void dispose() { } - public boolean isDataFlavorAvailable(@NotNull final DataFlavor dataFlavor) { - return Toolkit.getDefaultToolkit().getSystemClipboard().isDataFlavorAvailable(dataFlavor); + public boolean areDataFlavorsAvailable(@NotNull DataFlavor... flavors) { + Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); + for (DataFlavor flavor : flavors) { + if (clipboard.isDataFlavorAvailable(flavor)) { + return true; + } + } + return false; } @Nullable @@ -197,9 +196,9 @@ public class ClipboardSynchronizer implements ApplicationComponent { } @Override - public boolean isDataFlavorAvailable(@NotNull final DataFlavor dataFlavor) { - final Transferable contents = getContents(); - return contents != null && contents.isDataFlavorSupported(dataFlavor); + public boolean areDataFlavorsAvailable(@NotNull DataFlavor... flavors) { + Transferable contents = getContents(); + return contents != null && ClipboardSynchronizer.areDataFlavorsAvailable(contents, flavors); } @Override @@ -251,7 +250,7 @@ public class ClipboardSynchronizer implements ApplicationComponent { } @Nullable - public static Transferable getContentsSafe() { + private static Transferable getContentsSafe() { final Ref result = new Ref(); Foundation.executeOnMainThread(new Runnable() { @@ -308,6 +307,9 @@ public class ClipboardSynchronizer implements ApplicationComponent { private static class XWinClipboardHandler extends ClipboardHandler { + private static final String DATA_TRANSFER_TIMEOUT_PROPERTY = "sun.awt.datatransfer.timeout"; + private static final String LONG_TIMEOUT = "2000"; + private static final String SHORT_TIMEOUT = "100"; private static final FlavorTable FLAVOR_MAP = (FlavorTable)SystemFlavorMap.getDefaultFlavorMap(); private volatile Transferable myCurrentContent = null; @@ -325,19 +327,19 @@ public class ClipboardSynchronizer implements ApplicationComponent { } @Override - public boolean isDataFlavorAvailable(@NotNull final DataFlavor dataFlavor) { - final Transferable currentContent = myCurrentContent; + public boolean areDataFlavorsAvailable(@NotNull DataFlavor... flavors) { + Transferable currentContent = myCurrentContent; if (currentContent != null) { - return currentContent.isDataFlavorSupported(dataFlavor); + return ClipboardSynchronizer.areDataFlavorsAvailable(currentContent, flavors); } try { - final Collection contents = checkContentsQuick(); + Collection contents = checkContentsQuick(); if (contents != null) { - return contents.contains(dataFlavor); + return ClipboardSynchronizer.areDataFlavorsAvailable(contents, flavors); } - return super.isDataFlavorAvailable(dataFlavor); + return super.areDataFlavorsAvailable(flavors); } catch (NullPointerException e) { LOG.warn("Java bug #6322854", e); @@ -438,9 +440,9 @@ public class ClipboardSynchronizer implements ApplicationComponent { private volatile Transferable myContent = null; @Override - public boolean isDataFlavorAvailable(@NotNull final DataFlavor dataFlavor) { - final Transferable content = myContent; - return content != null && content.isDataFlavorSupported(dataFlavor); + public boolean areDataFlavorsAvailable(@NotNull DataFlavor... flavors) { + Transferable content = myContent; + return content != null && ClipboardSynchronizer.areDataFlavorsAvailable(content, flavors); } @Override @@ -458,4 +460,23 @@ public class ClipboardSynchronizer implements ApplicationComponent { myContent = null; } } + + + private static boolean areDataFlavorsAvailable(Transferable contents, DataFlavor... flavors) { + for (DataFlavor flavor : flavors) { + if (contents.isDataFlavorSupported(flavor)) { + return true; + } + } + return false; + } + + private static boolean areDataFlavorsAvailable(Collection contents, DataFlavor... flavors) { + for (DataFlavor flavor : flavors) { + if (contents.contains(flavor)) { + return true; + } + } + return false; + } } diff --git a/platform/platform-impl/src/com/intellij/ide/CopyPasteManagerEx.java b/platform/platform-impl/src/com/intellij/ide/CopyPasteManagerEx.java index 01a74a41b810..7c00252cfa8e 100644 --- a/platform/platform-impl/src/com/intellij/ide/CopyPasteManagerEx.java +++ b/platform/platform-impl/src/com/intellij/ide/CopyPasteManagerEx.java @@ -17,7 +17,6 @@ package com.intellij.ide; import com.intellij.ide.ui.UISettings; import com.intellij.openapi.Disposable; -import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Document; import com.intellij.openapi.extensions.Extensions; import com.intellij.openapi.ide.CopyPasteManager; @@ -68,8 +67,8 @@ public class CopyPasteManagerEx extends CopyPasteManager implements ClipboardOwn } @Override - public boolean isDataFlavorAvailable(@Nullable DataFlavor flavor) { - return flavor != null && myClipboardSynchronizer.isDataFlavorAvailable(flavor); + public boolean areDataFlavorsAvailable(@NotNull DataFlavor... flavors) { + return flavors.length > 0 && myClipboardSynchronizer.areDataFlavorsAvailable(flavors); } public void setContents(@NotNull final Transferable content) { @@ -209,8 +208,13 @@ public class CopyPasteManagerEx extends CopyPasteManager implements ClipboardOwn return null; } - private static String getStringContent(Transferable content) throws UnsupportedFlavorException, IOException { - return (String)content.getTransferData(DataFlavor.stringFlavor); + private static String getStringContent(Transferable content) { + try { + return (String)content.getTransferData(DataFlavor.stringFlavor); + } + catch (UnsupportedFlavorException ignore) { } + catch (IOException ignore) { } + return null; } private void deleteAfterAllowedMaximum() { @@ -220,50 +224,47 @@ public class CopyPasteManagerEx extends CopyPasteManager implements ClipboardOwn } } + @Override public Transferable getContents() { return myClipboardSynchronizer.getContents(); } - public Transferable[] getAllContents() { - deleteAfterAllowedMaximum(); - - Transferable content = getContents(); - if (content != null) { + @Nullable + @Override + public T getContents(@NotNull DataFlavor flavor) { + if (areDataFlavorsAvailable(flavor)) { try { - String clipString = getStringContent(content); - String dataString = null; - - if (!myData.isEmpty()) { - dataString = getStringContent(myData.get(0)); - } - - if (clipString != null && clipString.length() > 0 && !Comparing.equal(clipString, dataString)) { - myData.add(0, content); + Transferable contents = getContents(); + if (contents != null) { + @SuppressWarnings("unchecked") T data = (T)contents.getTransferData(flavor); + return data; } } catch (UnsupportedFlavorException ignore) { } catch (IOException ignore) { } } + return null; + } + + @Override + public Transferable[] getAllContents() { + String clipString = getContents(DataFlavor.stringFlavor); + if (clipString != null && (myData.isEmpty() || !Comparing.equal(clipString, getStringContent(myData.get(0))))) { + addToTheTopOfTheStack(new StringSelection(clipString)); + } return myData.toArray(new Transferable[myData.size()]); } public void removeContent(Transferable t) { - Transferable old = getContents(); - boolean isCurrentClipboardContent = myData.indexOf(t) == 0; + boolean isCurrentClipboardContent = !myData.isEmpty() && Comparing.equal(t, myData.get(0)); myData.remove(t); - Transferable _new = null; if (isCurrentClipboardContent) { - if (!myData.isEmpty()) { - _new = myData.get(0); - setSystemClipboardContent(_new); - } - else { - _new = new StringSelection(""); - setSystemClipboardContent(_new); - } + Transferable old = getContents(); + Transferable _new = !myData.isEmpty() ? myData.get(0) : new StringSelection(""); + setSystemClipboardContent(_new); + fireContentChanged(old, _new); } - fireContentChanged(old, _new); } public void moveContentTopStackTop(Transferable t) { diff --git a/platform/platform-impl/src/com/intellij/ide/dnd/FileCopyPasteUtil.java b/platform/platform-impl/src/com/intellij/ide/dnd/FileCopyPasteUtil.java index 0ebdccb36cdb..cecde708b61b 100644 --- a/platform/platform-impl/src/com/intellij/ide/dnd/FileCopyPasteUtil.java +++ b/platform/platform-impl/src/com/intellij/ide/dnd/FileCopyPasteUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,9 +16,9 @@ package com.intellij.ide.dnd; import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.fileTypes.FileTypeRegistry; import com.intellij.openapi.fileTypes.UnknownFileType; +import com.intellij.openapi.ide.CopyPasteManager; import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VfsUtil; @@ -36,9 +36,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -/** - * todo: migrate all CCP/DnD support classes to JDK6 TransferHandlers (IDEA 12?) - */ public class FileCopyPasteUtil { private static final Logger LOG = Logger.getInstance("#com.intellij.ide.dnd.FileCopyPasteUtil"); @@ -77,19 +74,19 @@ public class FileCopyPasteUtil { return createDataFlavor(DataFlavor.javaJVMLocalObjectMimeType, klass, false); } - public static boolean isFileListFlavorSupported(@NotNull final Transferable transferable) { - return transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor) || - transferable.isDataFlavorSupported(LinuxDragAndDropSupport.uriListFlavor) || - transferable.isDataFlavorSupported(LinuxDragAndDropSupport.gnomeFileListFlavor); + public static boolean isFileListFlavorAvailable() { + return CopyPasteManager.getInstance().areDataFlavorsAvailable( + DataFlavor.javaFileListFlavor, LinuxDragAndDropSupport.uriListFlavor, LinuxDragAndDropSupport.gnomeFileListFlavor + ); } - public static boolean isFileListFlavorSupported(@NotNull final DnDEvent event) { + public static boolean isFileListFlavorAvailable(@NotNull DnDEvent event) { return event.isDataFlavorSupported(DataFlavor.javaFileListFlavor) || event.isDataFlavorSupported(LinuxDragAndDropSupport.uriListFlavor) || event.isDataFlavorSupported(LinuxDragAndDropSupport.gnomeFileListFlavor); } - public static boolean isFileListFlavorSupported(@NotNull final DataFlavor[] transferFlavors) { + public static boolean isFileListFlavorAvailable(@NotNull DataFlavor[] transferFlavors) { for (DataFlavor flavor : transferFlavors) { if (flavor != null && (flavor.equals(DataFlavor.javaFileListFlavor) || flavor.equals(LinuxDragAndDropSupport.uriListFlavor) || diff --git a/platform/platform-impl/src/com/intellij/openapi/diff/actions/ClipboardVsValueContents.java b/platform/platform-impl/src/com/intellij/openapi/diff/actions/ClipboardVsValueContents.java index 966ed4685833..583fd3afc70f 100644 --- a/platform/platform-impl/src/com/intellij/openapi/diff/actions/ClipboardVsValueContents.java +++ b/platform/platform-impl/src/com/intellij/openapi/diff/actions/ClipboardVsValueContents.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,7 +26,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.Transferable; /** * @author Jeka @@ -72,15 +71,7 @@ public class ClipboardVsValueContents extends DiffRequest { @Nullable public static DiffContent createClipboardContent() { - Transferable content = CopyPasteManager.getInstance().getContents(); - if (content != null) { - try { - String text = (String)(content.getTransferData(DataFlavor.stringFlavor)); - return text != null ? new SimpleContent(text) : null; - } - catch (Exception ignored) { - } - } - return null; + String text = CopyPasteManager.getInstance().getContents(DataFlavor.stringFlavor); + return text != null ? new SimpleContent(text) : null; } } diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/actions/PasteAction.java b/platform/platform-impl/src/com/intellij/openapi/editor/actions/PasteAction.java index a0d99d815549..7851070f9d95 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/actions/PasteAction.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/actions/PasteAction.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,30 +13,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -/* - * Created by IntelliJ IDEA. - * User: max - * Date: May 13, 2002 - * Time: 7:50:36 PM - * To change template for new class use - * Code Style | Class Templates options (Tools | IDE Options). - */ package com.intellij.openapi.editor.actions; import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.actionSystem.DataKey; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.EditorModificationUtil; import com.intellij.openapi.editor.actionSystem.EditorAction; import com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler; import com.intellij.openapi.editor.ex.EditorEx; +import com.intellij.openapi.util.TextRange; import com.intellij.util.Producer; import java.awt.datatransfer.Transferable; +/** + * @author max + * @since May 13, 2002 + */ public class PasteAction extends EditorAction { - public static final String TRANSFERABLE_PROVIDER = "PasteTransferableProvider"; - + public static final DataKey> TRANSFERABLE_PROVIDER = DataKey.create("PasteTransferableProvider"); + public PasteAction() { super(new Handler()); } @@ -44,15 +41,13 @@ public class PasteAction extends EditorAction { private static class Handler extends EditorWriteActionHandler { @Override public void executeWriteAction(Editor editor, DataContext dataContext) { - Producer producer = (Producer)dataContext.getData(TRANSFERABLE_PROVIDER); - + Producer producer = TRANSFERABLE_PROVIDER.getData(dataContext); if (editor.isColumnMode() || editor.getSelectionModel().hasBlockSelection()) { - EditorModificationUtil.pasteTransferableAsBlock(editor, producer == null ? null : producer.produce()); + EditorModificationUtil.pasteTransferableAsBlock(editor, producer); } else { - editor.putUserData(EditorEx.LAST_PASTED_REGION, - producer == null ? EditorModificationUtil.pasteFromClipboard(editor) : - EditorModificationUtil.pasteFromTransferrable(producer.produce(), editor)); + TextRange range = EditorModificationUtil.pasteTransferable(editor, producer); + editor.putUserData(EditorEx.LAST_PASTED_REGION, range); } } } diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/actions/PasteFromX11Action.java b/platform/platform-impl/src/com/intellij/openapi/editor/actions/PasteFromX11Action.java index fcd7b9799360..3e20ba800dae 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/actions/PasteFromX11Action.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/actions/PasteFromX11Action.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,18 +18,18 @@ package com.intellij.openapi.editor.actions; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.CommonDataKeys; import com.intellij.openapi.actionSystem.DataContext; -import com.intellij.openapi.actionSystem.PlatformDataKeys; import com.intellij.openapi.actionSystem.Presentation; -import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.EditorModificationUtil; import com.intellij.openapi.editor.actionSystem.EditorAction; import com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler; import com.intellij.openapi.editor.event.EditorMouseEventArea; import com.intellij.openapi.editor.ex.EditorEx; -import com.intellij.openapi.ui.Messages; import com.intellij.openapi.util.SystemInfo; -import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.util.TextRange; +import com.intellij.util.Producer; +import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.awt.*; @@ -74,21 +74,27 @@ public class PasteFromX11Action extends EditorAction { public static class Handler extends EditorWriteActionHandler { @Override public void executeWriteAction(Editor editor, DataContext dataContext) { - final Clipboard clip = editor.getComponent().getToolkit().getSystemSelection(); - if (clip != null) { - Transferable res = null; - try { - res = clip.getContents(null); - } - catch (Exception e) { - if (!ApplicationManager.getApplication().isHeadlessEnvironment()) { - LOG.info(e); - Messages.showErrorDialog(editor.getProject(), "Cannot paste from X11 clipboard: " + e.getLocalizedMessage(), "Cannot Paste"); - return; - } - } - editor.putUserData(EditorEx.LAST_PASTED_REGION, EditorModificationUtil.pasteFromTransferrable(res, editor)); + Clipboard clip = editor.getComponent().getToolkit().getSystemSelection(); + if (clip == null) return; + + final Transferable content; + try { + content = clip.getContents(null); } + catch (Exception e) { + LOG.info(e); + return; + } + if (content == null) return; + + TextRange range = EditorModificationUtil.pasteTransferable(editor, new Producer() { + @Nullable + @Override + public Transferable produce() { + return content; + } + }); + editor.putUserData(EditorEx.LAST_PASTED_REGION, range); } } } diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/actions/SimplePasteAction.java b/platform/platform-impl/src/com/intellij/openapi/editor/actions/SimplePasteAction.java index 40a9adbfa2dc..a19a9a85a7e5 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/actions/SimplePasteAction.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/actions/SimplePasteAction.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,29 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -/* - * Created by IntelliJ IDEA. - * User: max - * Date: May 13, 2002 - * Time: 7:50:36 PM - * To change template for new class use - * Code Style | Class Templates options (Tools | IDE Options). - */ package com.intellij.openapi.editor.actions; import com.intellij.openapi.actionSystem.ActionPlaces; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.actionSystem.Presentation; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.EditorModificationUtil; import com.intellij.openapi.editor.actionSystem.EditorAction; import com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler; import com.intellij.openapi.editor.ex.EditorEx; +import com.intellij.openapi.util.TextRange; import com.intellij.util.Producer; import java.awt.datatransfer.Transferable; +/** + * @author max + * @since May 13, 2002 + */ public class SimplePasteAction extends EditorAction { public SimplePasteAction() { super(new Handler()); @@ -45,23 +42,21 @@ public class SimplePasteAction extends EditorAction { public void update(AnActionEvent e) { super.update(e); if (ActionPlaces.isPopupPlace(e.getPlace())) { - e.getPresentation().setVisible(e.getPresentation().isEnabled()); + Presentation presentation = e.getPresentation(); + presentation.setVisible(presentation.isEnabled()); } } private static class Handler extends EditorWriteActionHandler { @Override public void executeWriteAction(Editor editor, DataContext dataContext) { - Producer producer = (Producer) dataContext.getData(PasteAction.TRANSFERABLE_PROVIDER); - + Producer producer = PasteAction.TRANSFERABLE_PROVIDER.getData(dataContext); if (editor.isColumnMode()) { - EditorModificationUtil.pasteTransferableAsBlock(editor, producer == null ? null : producer.produce()); - } else { - editor.putUserData(EditorEx.LAST_PASTED_REGION, - producer == null ? - EditorModificationUtil.pasteFromClipboard(editor) - : EditorModificationUtil.pasteFromTransferrable(producer.produce(), editor)); - + EditorModificationUtil.pasteTransferableAsBlock(editor, producer); + } + else { + TextRange range = EditorModificationUtil.pasteTransferable(editor, producer); + editor.putUserData(EditorEx.LAST_PASTED_REGION, range); } } diff --git a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/text/FileDropHandler.java b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/text/FileDropHandler.java index 972ac670b7bd..342cb0959f88 100644 --- a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/text/FileDropHandler.java +++ b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/text/FileDropHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -44,21 +44,18 @@ public class FileDropHandler implements EditorDropHandler { myEditor = editor; } - @Override public boolean canHandleDrop(final DataFlavor[] transferFlavors) { - return transferFlavors != null && FileCopyPasteUtil.isFileListFlavorSupported(transferFlavors); + return transferFlavors != null && FileCopyPasteUtil.isFileListFlavorAvailable(transferFlavors); } @Override public void handleDrop(@NotNull final Transferable t, @Nullable final Project project, EditorWindow editorWindow) { - if (project == null || !FileCopyPasteUtil.isFileListFlavorSupported(t)) { - return; - } - - final List fileList = FileCopyPasteUtil.getFileList(t); - if (fileList != null) { - openFiles(project, fileList, editorWindow); + if (project != null) { + final List fileList = FileCopyPasteUtil.getFileList(t); + if (fileList != null) { + openFiles(project, fileList, editorWindow); + } } } diff --git a/plugins/ant/src/com/intellij/lang/ant/config/explorer/AntExplorer.java b/plugins/ant/src/com/intellij/lang/ant/config/explorer/AntExplorer.java index 6f4ca1bf1b60..2976c03f50c0 100644 --- a/plugins/ant/src/com/intellij/lang/ant/config/explorer/AntExplorer.java +++ b/plugins/ant/src/com/intellij/lang/ant/config/explorer/AntExplorer.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -854,7 +854,7 @@ public class AntExplorer extends SimpleToolWindowPanel implements DataProvider, @Override public boolean canImport(final TransferSupport support) { - return FileCopyPasteUtil.isFileListFlavorSupported(support.getDataFlavors()); + return FileCopyPasteUtil.isFileListFlavorAvailable(support.getDataFlavors()); } private VirtualFile[] getAntFiles(final TransferSupport support) { diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/navigator/MavenProjectsNavigatorPanel.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/navigator/MavenProjectsNavigatorPanel.java index 525f0e74b3d7..86761532f6a0 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/navigator/MavenProjectsNavigatorPanel.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/navigator/MavenProjectsNavigatorPanel.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -318,7 +318,7 @@ public class MavenProjectsNavigatorPanel extends SimpleToolWindowPanel implement @Override public boolean canImport(final TransferSupport support) { - return FileCopyPasteUtil.isFileListFlavorSupported(support.getDataFlavors()); + return FileCopyPasteUtil.isFileListFlavorAvailable(support.getDataFlavors()); } } } diff --git a/plugins/terminal/src/org/jetbrains/plugins/terminal/JBTerminalPanel.java b/plugins/terminal/src/org/jetbrains/plugins/terminal/JBTerminalPanel.java index 259d93c56665..754f5661db5e 100644 --- a/plugins/terminal/src/org/jetbrains/plugins/terminal/JBTerminalPanel.java +++ b/plugins/terminal/src/org/jetbrains/plugins/terminal/JBTerminalPanel.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -44,7 +44,6 @@ import org.jetbrains.annotations.NotNull; import java.awt.*; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; -import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; @@ -74,7 +73,7 @@ public class JBTerminalPanel extends TerminalPanel implements FocusListener, Ter registerKeymapActions(this); addFocusListener(this); - + mySettingsProvider.addListener(this); } @@ -170,11 +169,7 @@ public class JBTerminalPanel extends TerminalPanel implements FocusListener, Ter @Override protected String getClipboardContent() throws IOException, UnsupportedFlavorException { - Transferable contents = CopyPasteManager.getInstance().getContents(); - if (contents == null) { - return null; - } - return (String)contents.getTransferData(DataFlavor.stringFlavor); + return CopyPasteManager.getInstance().getContents(DataFlavor.stringFlavor); } @Override diff --git a/plugins/ui-designer-core/src/com/intellij/designer/actions/CommonEditActionsProvider.java b/plugins/ui-designer-core/src/com/intellij/designer/actions/CommonEditActionsProvider.java index 304a26c6d2bc..6361ac2b722b 100644 --- a/plugins/ui-designer-core/src/com/intellij/designer/actions/CommonEditActionsProvider.java +++ b/plugins/ui-designer-core/src/com/intellij/designer/actions/CommonEditActionsProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,7 +40,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.Transferable; import java.util.List; import java.util.Map; @@ -260,17 +259,7 @@ public class CommonEditActionsProvider implements DeleteProvider, CopyProvider, @Nullable private String getSerializedComponentData() { try { - CopyPasteManager copyPasteManager = CopyPasteManager.getInstance(); - if (!copyPasteManager.isDataFlavorAvailable(DATA_FLAVOR)) { - return null; - } - - Transferable content = copyPasteManager.getContents(); - if (content == null) { - return null; - } - - Object transferData = content.getTransferData(DATA_FLAVOR); + Object transferData = CopyPasteManager.getInstance().getContents(DATA_FLAVOR); if (transferData instanceof SerializedComponentData) { SerializedComponentData data = (SerializedComponentData)transferData; String xmlComponents = data.getSerializedComponents(); @@ -279,8 +268,7 @@ public class CommonEditActionsProvider implements DeleteProvider, CopyProvider, } } } - catch (Throwable e) { - } + catch (Throwable ignored) { } return null; } diff --git a/plugins/ui-designer/src/com/intellij/uiDesigner/CutCopyPasteSupport.java b/plugins/ui-designer/src/com/intellij/uiDesigner/CutCopyPasteSupport.java index 4fef2c8fbf4c..a4ecbfbec91f 100644 --- a/plugins/ui-designer/src/com/intellij/uiDesigner/CutCopyPasteSupport.java +++ b/plugins/ui-designer/src/com/intellij/uiDesigner/CutCopyPasteSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,7 +39,6 @@ import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.awt.*; import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.Transferable; import java.io.StringReader; import java.util.ArrayList; import java.util.List; @@ -165,7 +164,7 @@ public final class CutCopyPasteSupport implements CopyProvider, CutProvider, Pas if (parentLayout != null) { container.setLayoutManager(parentLayout); } - + final int x = Integer.parseInt(e.getAttributeValue(ATTRIBUTE_X)); final int y = Integer.parseInt(e.getAttributeValue(ATTRIBUTE_Y)); @@ -204,17 +203,7 @@ public final class CutCopyPasteSupport implements CopyProvider, CutProvider, Pas @Nullable private static String getSerializedComponents() { try { - final CopyPasteManager copyPasteManager = CopyPasteManager.getInstance(); - if (!copyPasteManager.isDataFlavorAvailable(ourDataFlavor)) { - return null; - } - - final Transferable content = copyPasteManager.getContents(); - if (content == null) { - return null; - } - - final Object transferData = content.getTransferData(ourDataFlavor); + final Object transferData = CopyPasteManager.getInstance().getContents(ourDataFlavor); if (!(transferData instanceof SerializedComponentData)) { return null; }