2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.openapi
.editor
.actions
;
18 import com
.intellij
.ide
.CopyPasteManagerEx
;
19 import com
.intellij
.ide
.DataManager
;
20 import com
.intellij
.openapi
.actionSystem
.*;
21 import com
.intellij
.openapi
.editor
.Editor
;
22 import com
.intellij
.openapi
.fileEditor
.FileDocumentManager
;
23 import com
.intellij
.openapi
.ide
.CopyPasteManager
;
24 import com
.intellij
.openapi
.project
.Project
;
25 import com
.intellij
.openapi
.project
.DumbAware
;
26 import com
.intellij
.ui
.UIBundle
;
29 import javax
.swing
.text
.DefaultEditorKit
;
31 import java
.awt
.datatransfer
.DataFlavor
;
32 import java
.awt
.datatransfer
.Transferable
;
33 import java
.awt
.datatransfer
.UnsupportedFlavorException
;
34 import java
.awt
.event
.ActionEvent
;
35 import java
.io
.IOException
;
36 import java
.util
.Arrays
;
37 import java
.util
.List
;
42 public class MultiplePasteAction
extends AnAction
implements DumbAware
{
43 public MultiplePasteAction() {
44 setEnabledInModalContext(true);
47 public void actionPerformed(final AnActionEvent e
) {
48 final DataContext dataContext
= e
.getDataContext();
49 Project project
= PlatformDataKeys
.PROJECT
.getData(dataContext
);
50 Component focusedComponent
= e
.getData(PlatformDataKeys
.CONTEXT_COMPONENT
);
51 Editor editor
= PlatformDataKeys
.EDITOR
.getData(dataContext
);
53 if (!(focusedComponent
instanceof JComponent
)) return;
55 final ContentChooser
<Transferable
> chooser
= new ContentChooser
<Transferable
>(project
, UIBundle
.message(
56 "choose.content.to.paste.dialog.title"), true){
57 protected String
getStringRepresentationFor(final Transferable content
) {
59 return (String
)content
.getTransferData(DataFlavor
.stringFlavor
);
61 catch (UnsupportedFlavorException e1
) {
64 catch (IOException e1
) {
69 protected List
<Transferable
> getContents() {
70 return Arrays
.asList(CopyPasteManager
.getInstance().getAllContents());
73 protected void removeContentAt(final Transferable content
) {
74 ((CopyPasteManagerEx
)CopyPasteManager
.getInstance()).removeContent(content
);
78 if (chooser
.getAllContents().size() > 0) {
82 chooser
.close(ContentChooser
.CANCEL_EXIT_CODE
);
86 final int selectedIndex
= chooser
.getSelectedIndex();
87 ((CopyPasteManagerEx
)CopyPasteManager
.getInstance()).moveContentTopStackTop(chooser
.getAllContents().get(selectedIndex
));
90 if (!FileDocumentManager
.getInstance().requestWriting(editor
.getDocument(), project
)){
94 final AnAction pasteAction
= ActionManager
.getInstance().getAction(IdeActions
.ACTION_PASTE
);
95 AnActionEvent newEvent
= new AnActionEvent(e
.getInputEvent(),
96 DataManager
.getInstance().getDataContext(focusedComponent
),
97 e
.getPlace(), e
.getPresentation(),
98 ActionManager
.getInstance(),
100 pasteAction
.actionPerformed(newEvent
);
103 final Action pasteAction
= ((JComponent
)focusedComponent
).getActionMap().get(DefaultEditorKit
.pasteAction
);
104 if (pasteAction
!= null) {
105 pasteAction
.actionPerformed(new ActionEvent(focusedComponent
, ActionEvent
.ACTION_PERFORMED
, ""));
111 public void update(AnActionEvent e
) {
112 final boolean enabled
= isEnabled(e
);
113 if (ActionPlaces
.isPopupPlace(e
.getPlace())) {
114 e
.getPresentation().setVisible(enabled
);
117 e
.getPresentation().setEnabled(enabled
);
121 private static boolean isEnabled(AnActionEvent e
) {
122 Object component
= e
.getData(PlatformDataKeys
.CONTEXT_COMPONENT
);
123 if (!(component
instanceof JComponent
)) return false;
124 if (e
.getData(PlatformDataKeys
.EDITOR
) != null) return true;
125 Action pasteAction
= ((JComponent
)component
).getActionMap().get(DefaultEditorKit
.pasteAction
);
126 return pasteAction
!= null;