Search in sources :

Example 1 with ClipboardOwner

use of java.awt.datatransfer.ClipboardOwner in project jdk8u_jdk by JetBrains.

the class SunClipboard method lostOwnershipNow.

protected void lostOwnershipNow(final AppContext disposedContext) {
    final SunClipboard sunClipboard = SunClipboard.this;
    ClipboardOwner owner = null;
    Transferable contents = null;
    synchronized (sunClipboard) {
        final AppContext context = sunClipboard.contentsContext;
        if (context == null) {
            return;
        }
        if (disposedContext == null || context == disposedContext) {
            owner = sunClipboard.owner;
            contents = sunClipboard.contents;
            sunClipboard.contentsContext = null;
            sunClipboard.owner = null;
            sunClipboard.contents = null;
            sunClipboard.clearNativeContext();
            context.removePropertyChangeListener(AppContext.DISPOSED_PROPERTY_NAME, sunClipboard);
        } else {
            return;
        }
    }
    if (owner != null) {
        owner.lostOwnership(sunClipboard, contents);
    }
}
Also used : AppContext(sun.awt.AppContext) Transferable(java.awt.datatransfer.Transferable) ClipboardOwner(java.awt.datatransfer.ClipboardOwner)

Example 2 with ClipboardOwner

use of java.awt.datatransfer.ClipboardOwner in project otertool by wuntee.

the class GuiWorkshop method setClipboardContents.

public static void setClipboardContents(String s) {
    StringSelection stringSelection = new StringSelection(s);
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents(stringSelection, new ClipboardOwner() {

        public void lostOwnership(Clipboard arg0, Transferable arg1) {
        }
    });
}
Also used : Transferable(java.awt.datatransfer.Transferable) ClipboardOwner(java.awt.datatransfer.ClipboardOwner) Clipboard(java.awt.datatransfer.Clipboard) StringSelection(java.awt.datatransfer.StringSelection)

Example 3 with ClipboardOwner

use of java.awt.datatransfer.ClipboardOwner in project kotlin by JetBrains.

the class CopyAsDiagnosticTestAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    Editor editor = e.getData(CommonDataKeys.EDITOR);
    PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
    assert editor != null && psiFile != null;
    BindingContext bindingContext = ResolutionUtils.analyzeFully((KtFile) psiFile);
    List<CheckerTestUtil.ActualDiagnostic> diagnostics = CheckerTestUtil.getDiagnosticsIncludingSyntaxErrors(bindingContext, psiFile, false, null, null);
    String result = CheckerTestUtil.addDiagnosticMarkersToText(psiFile, diagnostics).toString();
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents(new StringSelection(result), new ClipboardOwner() {

        @Override
        public void lostOwnership(Clipboard clipboard, Transferable contents) {
        }
    });
}
Also used : Transferable(java.awt.datatransfer.Transferable) ClipboardOwner(java.awt.datatransfer.ClipboardOwner) PsiFile(com.intellij.psi.PsiFile) Clipboard(java.awt.datatransfer.Clipboard) Editor(com.intellij.openapi.editor.Editor) BindingContext(org.jetbrains.kotlin.resolve.BindingContext) StringSelection(java.awt.datatransfer.StringSelection)

Example 4 with ClipboardOwner

use of java.awt.datatransfer.ClipboardOwner in project jabref by JabRef.

the class ExportToClipboardAction method run.

@Override
public void run() {
    BasePanel panel = frame.getCurrentBasePanel();
    if (panel == null) {
        return;
    }
    if (panel.getSelectedEntries().isEmpty()) {
        message = Localization.lang("This operation requires one or more entries to be selected.");
        getCallBack().update();
        return;
    }
    List<IExportFormat> exportFormats = new LinkedList<>(ExportFormats.getExportFormats().values());
    Collections.sort(exportFormats, (e1, e2) -> e1.getDisplayName().compareTo(e2.getDisplayName()));
    String[] exportFormatDisplayNames = new String[exportFormats.size()];
    for (int i = 0; i < exportFormats.size(); i++) {
        IExportFormat exportFormat = exportFormats.get(i);
        exportFormatDisplayNames[i] = exportFormat.getDisplayName();
    }
    JList<String> list = new JList<>(exportFormatDisplayNames);
    list.setBorder(BorderFactory.createEtchedBorder());
    list.setSelectionInterval(0, 0);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    int answer = JOptionPane.showOptionDialog(frame, list, Localization.lang("Select export format"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { Localization.lang("Export"), Localization.lang("Cancel") }, Localization.lang("Export"));
    if (answer == JOptionPane.NO_OPTION) {
        return;
    }
    IExportFormat format = exportFormats.get(list.getSelectedIndex());
    // Set the global variable for this database's file directory before exporting,
    // so formatters can resolve linked files correctly.
    // (This is an ugly hack!)
    Globals.prefs.fileDirForDatabase = frame.getCurrentBasePanel().getBibDatabaseContext().getFileDirectories(Globals.prefs.getFileDirectoryPreferences());
    File tmp = null;
    try {
        // To simplify the exporter API we simply do a normal export to a temporary
        // file, and read the contents afterwards:
        tmp = File.createTempFile("jabrefCb", ".tmp");
        tmp.deleteOnExit();
        List<BibEntry> entries = panel.getSelectedEntries();
        // Write to file:
        format.performExport(panel.getBibDatabaseContext(), tmp.getPath(), panel.getBibDatabaseContext().getMetaData().getEncoding().orElse(Globals.prefs.getDefaultEncoding()), entries);
        // Read the file and put the contents on the clipboard:
        StringBuilder sb = new StringBuilder();
        try (Reader reader = new InputStreamReader(new FileInputStream(tmp), panel.getBibDatabaseContext().getMetaData().getEncoding().orElse(Globals.prefs.getDefaultEncoding()))) {
            int s;
            while ((s = reader.read()) != -1) {
                sb.append((char) s);
            }
        }
        ClipboardOwner owner = (clipboard, content) -> {
        // Do nothing
        };
        RtfTransferable rs = new RtfTransferable(sb.toString());
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(rs, owner);
        message = Localization.lang("Entries exported to clipboard") + ": " + entries.size();
    } catch (Exception e) {
        //To change body of catch statement use File | Settings | File Templates.
        LOGGER.error("Error exporting to clipboard", e);
        message = Localization.lang("Error exporting to clipboard");
    } finally {
        // Clean up:
        if ((tmp != null) && !tmp.delete()) {
            LOGGER.info("Cannot delete temporary clipboard file");
        }
    }
}
Also used : ListSelectionModel(javax.swing.ListSelectionModel) ClipboardOwner(java.awt.datatransfer.ClipboardOwner) IExportFormat(org.jabref.logic.exporter.IExportFormat) BibEntry(org.jabref.model.entry.BibEntry) JList(javax.swing.JList) AbstractWorker(org.jabref.gui.worker.AbstractWorker) ExportFormats(org.jabref.logic.exporter.ExportFormats) BorderFactory(javax.swing.BorderFactory) FileInputStream(java.io.FileInputStream) Reader(java.io.Reader) JOptionPane(javax.swing.JOptionPane) InputStreamReader(java.io.InputStreamReader) BasePanel(org.jabref.gui.BasePanel) File(java.io.File) Globals(org.jabref.Globals) Objects(java.util.Objects) List(java.util.List) JabRefFrame(org.jabref.gui.JabRefFrame) Localization(org.jabref.logic.l10n.Localization) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) LinkedList(java.util.LinkedList) Collections(java.util.Collections) Toolkit(java.awt.Toolkit) BibEntry(org.jabref.model.entry.BibEntry) BasePanel(org.jabref.gui.BasePanel) InputStreamReader(java.io.InputStreamReader) IExportFormat(org.jabref.logic.exporter.IExportFormat) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ClipboardOwner(java.awt.datatransfer.ClipboardOwner) LinkedList(java.util.LinkedList) FileInputStream(java.io.FileInputStream) File(java.io.File) JList(javax.swing.JList)

Example 5 with ClipboardOwner

use of java.awt.datatransfer.ClipboardOwner in project jdk8u_jdk by JetBrains.

the class SunClipboard method setContents.

public synchronized void setContents(Transferable contents, ClipboardOwner owner) {
    // should throw NPE
    if (contents == null) {
        throw new NullPointerException("contents");
    }
    initContext();
    final ClipboardOwner oldOwner = this.owner;
    final Transferable oldContents = this.contents;
    try {
        this.owner = owner;
        this.contents = new TransferableProxy(contents, true);
        setContentsNative(contents);
    } finally {
        if (oldOwner != null && oldOwner != owner) {
            EventQueue.invokeLater(new Runnable() {

                public void run() {
                    oldOwner.lostOwnership(SunClipboard.this, oldContents);
                }
            });
        }
    }
}
Also used : Transferable(java.awt.datatransfer.Transferable) ClipboardOwner(java.awt.datatransfer.ClipboardOwner)

Aggregations

ClipboardOwner (java.awt.datatransfer.ClipboardOwner)7 Transferable (java.awt.datatransfer.Transferable)6 Clipboard (java.awt.datatransfer.Clipboard)4 StringSelection (java.awt.datatransfer.StringSelection)3 Editor (com.intellij.openapi.editor.Editor)1 PsiFile (com.intellij.psi.PsiFile)1 Point (java.awt.Point)1 Toolkit (java.awt.Toolkit)1 DataFlavor (java.awt.datatransfer.DataFlavor)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 Collections (java.util.Collections)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Objects (java.util.Objects)1 BorderFactory (javax.swing.BorderFactory)1 JList (javax.swing.JList)1 JOptionPane (javax.swing.JOptionPane)1