Search in sources :

Example 71 with StringSelection

use of java.awt.datatransfer.StringSelection in project intellij-community by JetBrains.

the class ResourceBundleStructureViewComponent method getData.

@Override
public Object getData(final String dataId) {
    if (CommonDataKeys.VIRTUAL_FILE.is(dataId)) {
        return new ResourceBundleAsVirtualFile(myResourceBundle);
    } else if (PlatformDataKeys.FILE_EDITOR.is(dataId)) {
        return getFileEditor();
    } else if (ResourceBundle.ARRAY_DATA_KEY.is(dataId)) {
        return new ResourceBundle[] { myResourceBundle };
    } else if (IProperty.ARRAY_KEY.is(dataId)) {
        final Collection<ResourceBundleEditorViewElement> selectedElements = ((ResourceBundleEditor) getFileEditor()).getSelectedElements();
        if (selectedElements.isEmpty()) {
            return null;
        } else if (selectedElements.size() == 1) {
            return ContainerUtil.getFirstItem(selectedElements).getProperties();
        } else {
            return ContainerUtil.toArray(ContainerUtil.flatten(ContainerUtil.mapNotNull(selectedElements, (NullableFunction<ResourceBundleEditorViewElement, List<IProperty>>) element -> {
                final IProperty[] properties = element.getProperties();
                return properties == null ? null : ContainerUtil.newArrayList(properties);
            })), IProperty[]::new);
        }
    } else if (LangDataKeys.PSI_ELEMENT_ARRAY.is(dataId)) {
        final List<PsiElement> elements = new ArrayList<>();
        Collections.addAll(elements, getSelectedPsiFiles());
        final IProperty[] properties = (IProperty[]) getData(IProperty.ARRAY_KEY.getName());
        if (properties != null) {
            for (IProperty property : properties) {
                final PsiElement element = property.getPsiElement();
                if (element.isValid()) {
                    elements.add(element);
                }
            }
        }
        return elements.toArray(new PsiElement[elements.size()]);
    } else if (PlatformDataKeys.DELETE_ELEMENT_PROVIDER.is(dataId)) {
        if (getSelectedPsiFiles().length != 0) {
            return new ResourceBundleDeleteProvider();
        }
        final IProperty[] properties = IProperty.ARRAY_KEY.getData(this);
        if (properties != null && properties.length != 0) {
            return new PropertiesDeleteProvider(((ResourceBundleEditor) getFileEditor()).getPropertiesInsertDeleteManager(), properties);
        }
    } else if (UsageView.USAGE_TARGETS_KEY.is(dataId)) {
        final PsiElement[] chosenElements = (PsiElement[]) getData(LangDataKeys.PSI_ELEMENT_ARRAY.getName());
        if (chosenElements != null) {
            final UsageTarget[] usageTargets = new UsageTarget[chosenElements.length];
            for (int i = 0; i < chosenElements.length; i++) {
                usageTargets[i] = new PsiElement2UsageTargetAdapter(chosenElements[i]);
            }
            return usageTargets;
        }
    } else if (PlatformDataKeys.COPY_PROVIDER.is(dataId)) {
        return new CopyProvider() {

            @Override
            public void performCopy(@NotNull final DataContext dataContext) {
                final PsiElement[] selectedPsiElements = (PsiElement[]) getData(LangDataKeys.PSI_ELEMENT_ARRAY.getName());
                if (selectedPsiElements != null) {
                    final List<String> names = new ArrayList<>(selectedPsiElements.length);
                    for (final PsiElement element : selectedPsiElements) {
                        if (element instanceof PsiNamedElement) {
                            names.add(((PsiNamedElement) element).getName());
                        }
                    }
                    CopyPasteManager.getInstance().setContents(new StringSelection(StringUtil.join(names, "\n")));
                }
            }

            @Override
            public boolean isCopyEnabled(@NotNull final DataContext dataContext) {
                return true;
            }

            @Override
            public boolean isCopyVisible(@NotNull final DataContext dataContext) {
                return true;
            }
        };
    }
    return super.getData(dataId);
}
Also used : PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PopupHandler(com.intellij.ui.PopupHandler) NonNls(org.jetbrains.annotations.NonNls) ResourceBundleEditorRenderer(com.intellij.lang.properties.editor.inspections.ResourceBundleEditorRenderer) ContainerUtil(com.intellij.util.containers.ContainerUtil) DeleteProvider(com.intellij.ide.DeleteProvider) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) StringSelection(java.awt.datatransfer.StringSelection) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) ResourceBundle(com.intellij.lang.properties.ResourceBundle) PsiNamedElement(com.intellij.psi.PsiNamedElement) SafeDeleteHandler(com.intellij.refactoring.safeDelete.SafeDeleteHandler) Logger(com.intellij.openapi.diagnostic.Logger) StringUtil(com.intellij.openapi.util.text.StringUtil) Collection(java.util.Collection) NullableFunction(com.intellij.util.NullableFunction) UsageTarget(com.intellij.usages.UsageTarget) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) UsageView(com.intellij.usages.UsageView) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) List(java.util.List) IProperty(com.intellij.lang.properties.IProperty) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) CopyProvider(com.intellij.ide.CopyProvider) ResourceBundleDeleteProvider(com.intellij.lang.properties.projectView.ResourceBundleDeleteProvider) ContextHelpAction(com.intellij.ide.actions.ContextHelpAction) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) CopyPasteManager(com.intellij.openapi.ide.CopyPasteManager) CopyProvider(com.intellij.ide.CopyProvider) PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) ResourceBundleDeleteProvider(com.intellij.lang.properties.projectView.ResourceBundleDeleteProvider) StringSelection(java.awt.datatransfer.StringSelection) IProperty(com.intellij.lang.properties.IProperty) ArrayList(java.util.ArrayList) List(java.util.List) PsiElement(com.intellij.psi.PsiElement) UsageTarget(com.intellij.usages.UsageTarget) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) Collection(java.util.Collection) ResourceBundle(com.intellij.lang.properties.ResourceBundle)

Example 72 with StringSelection

use of java.awt.datatransfer.StringSelection in project intellij-community by JetBrains.

the class ShowXPathAction method actionPerformed.

public void actionPerformed(AnActionEvent e) {
    final Editor editor = CommonDataKeys.EDITOR.getData(e.getDataContext());
    if (editor == null) {
        return;
    }
    final Project project = editor.getProject();
    if (project == null) {
        return;
    }
    final PsiDocumentManager docmgr = PsiDocumentManager.getInstance(project);
    final Document document = editor.getDocument();
    docmgr.commitDocument(document);
    final PsiFile psiFile = docmgr.getPsiFile(document);
    if (!(psiFile instanceof XmlFile)) {
        return;
    }
    final PsiElement element = psiFile.findElementAt(editor.getCaretModel().getOffset());
    if (!(element instanceof XmlElement || element instanceof PsiWhiteSpace)) {
        XPathAppComponent.showEditorHint("No suitable context for an XPath-expression selected.", editor);
        return;
    }
    final PsiElement node = XPathExpressionGenerator.transformToValidShowPathNode(element);
    if (node == null) {
        XPathAppComponent.showEditorHint("No suitable context for an XPath-expression selected.", editor);
        return;
    }
    final Config cfg = XPathAppComponent.getInstance().getConfig();
    final RangeHighlighter h = HighlighterUtil.highlightNode(editor, node, cfg.getContextAttributes(), cfg);
    final String path = XPathSupport.getInstance().getUniquePath((XmlElement) node, null);
    final JTextField label = new JTextField(path);
    label.setPreferredSize(new Dimension(label.getPreferredSize().width + new JLabel("M").getPreferredSize().width, label.getPreferredSize().height));
    label.setOpaque(false);
    label.setEditable(false);
    label.setBorder(null);
    label.setHorizontalAlignment(SwingConstants.CENTER);
    label.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
    final JPanel p = new NonOpaquePanel(new BorderLayout());
    final JLabel l = new JLabel("XPath:");
    p.add(l, BorderLayout.WEST);
    p.add(label, BorderLayout.CENTER);
    InplaceButton copy = new InplaceButton(ActionsBundle.message("action.EditorCopy.text"), PlatformIcons.COPY_ICON, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            CopyPasteManager.getInstance().setContents(new StringSelection(path));
        }
    });
    p.add(copy, BorderLayout.EAST);
    final LightweightHint hint = new LightweightHint(p) {

        public void hide() {
            super.hide();
            HighlighterUtil.removeHighlighter(editor, h);
        }
    };
    final Point point = editor.visualPositionToXY(editor.getCaretModel().getVisualPosition());
    point.y += editor.getLineHeight() / 2;
    HintHint hintHint = new HintHint(editor, point).setAwtTooltip(true).setContentActive(true).setExplicitClose(true).setShowImmediately(true);
    HintManagerImpl.getInstanceImpl().showEditorHint(hint, editor, point, HintManager.HIDE_BY_ANY_KEY, 0, false, hintHint);
}
Also used : ActionEvent(java.awt.event.ActionEvent) Document(com.intellij.openapi.editor.Document) StringSelection(java.awt.datatransfer.StringSelection) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) NonOpaquePanel(com.intellij.ui.components.panels.NonOpaquePanel) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) XmlFile(com.intellij.psi.xml.XmlFile) LightweightHint(com.intellij.ui.LightweightHint) InplaceButton(com.intellij.ui.InplaceButton) Project(com.intellij.openapi.project.Project) HintHint(com.intellij.ui.HintHint) ActionListener(java.awt.event.ActionListener) XmlElement(com.intellij.psi.xml.XmlElement) Editor(com.intellij.openapi.editor.Editor) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 73 with StringSelection

use of java.awt.datatransfer.StringSelection in project intellij-community by JetBrains.

the class CopyRevisionNumberAction method actionPerformed.

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
    List<VcsRevisionNumber> revisions = getRevisionNumbersFromContext(e);
    // we want hashes from old to new, e.g. to be able to pass to native client in terminal
    revisions = ContainerUtil.reverse(revisions);
    CopyPasteManager.getInstance().setContents(new StringSelection(getHashesAsString(revisions)));
}
Also used : VcsRevisionNumber(com.intellij.openapi.vcs.history.VcsRevisionNumber) StringSelection(java.awt.datatransfer.StringSelection)

Example 74 with StringSelection

use of java.awt.datatransfer.StringSelection in project intellij-community by JetBrains.

the class FileHistoryPanelImpl method performCopy.

@Override
public void performCopy(@NotNull DataContext dataContext) {
    String text = StringUtil.join(getSelectedRevisions(), revision -> getPresentableText(revision, true), "\n");
    CopyPasteManager.getInstance().setContents(new StringSelection(text));
}
Also used : StringSelection(java.awt.datatransfer.StringSelection)

Example 75 with StringSelection

use of java.awt.datatransfer.StringSelection in project intellij-community by JetBrains.

the class GrCopyStringConcatenationContentIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final StringBuilder buffer = new StringBuilder();
    getValue(element, buffer);
    final Transferable contents = new StringSelection(buffer.toString());
    CopyPasteManager.getInstance().setContents(contents);
}
Also used : Transferable(java.awt.datatransfer.Transferable) StringSelection(java.awt.datatransfer.StringSelection)

Aggregations

StringSelection (java.awt.datatransfer.StringSelection)99 Clipboard (java.awt.datatransfer.Clipboard)28 ActionEvent (java.awt.event.ActionEvent)11 Transferable (java.awt.datatransfer.Transferable)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 JTextArea (javax.swing.JTextArea)7 Test (org.junit.Test)7 JPanel (javax.swing.JPanel)6 ActionListener (java.awt.event.ActionListener)5 JScrollPane (javax.swing.JScrollPane)5 Editor (com.intellij.openapi.editor.Editor)4 Project (com.intellij.openapi.project.Project)4 PsiFile (com.intellij.psi.PsiFile)4 ClipboardOwner (java.awt.datatransfer.ClipboardOwner)4 JMenuItem (javax.swing.JMenuItem)4 BibEntry (org.jabref.model.entry.BibEntry)4 AnAction (com.intellij.openapi.actionSystem.AnAction)3 PsiElement (com.intellij.psi.PsiElement)3 BorderLayout (java.awt.BorderLayout)3