Gridarta Editor
Paste.java
Go to the documentation of this file.
1 /*
2  * InputHandler.java - Manages key bindings and executes actions
3  * Copyright (C) 1999 Slava Pestov
4  * Copyright (C) 2000-2023 The Gridarta Developers.
5  *
6  * You may use and modify this package for any purpose. Redistribution is
7  * permitted, in both source and binary form, provided that this notice
8  * remains intact in all source distributions of this package.
9  */
10 
11 package net.sf.gridarta.textedit.textarea.actions;
12 
13 import java.awt.Toolkit;
14 import java.awt.datatransfer.DataFlavor;
15 import java.awt.datatransfer.Transferable;
16 import java.awt.datatransfer.UnsupportedFlavorException;
17 import java.awt.event.ActionEvent;
18 import java.awt.event.ActionListener;
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import javax.swing.text.BadLocationException;
24 import org.apache.log4j.Category;
25 import org.apache.log4j.Logger;
26 
31 public class Paste implements ActionListener {
32 
36  private static final Category LOG = Logger.getLogger(Paste.class);
37 
42  @Override
43  public void actionPerformed(final ActionEvent e) {
44  final JEditTextArea textArea = InputHandler.getTextArea(e);
45 
46  // set above string to the system clipboard
47  final Transferable trans = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(textArea);
48  try {
49  // now read the system clipboard in plain-text format
50  final DataFlavor dataFlavor = new DataFlavor("text/plain; charset=unicode");
51  final StringBuilder buff = new StringBuilder();
52  try (BufferedReader bufferedReader = new BufferedReader(dataFlavor.getReaderForText(trans))) {
53  // read everything into the buffer 'buff'
54  boolean start = true;
55  while (true) {
56  final String line = bufferedReader.readLine();
57  if (line == null) {
58  break;
59  }
60  if (start) {
61  start = false;
62  } else {
63  buff.append('\n');
64  }
65  buff.append(line);
66  }
67  }
68 
69  final String selectedText = textArea.getSelectedText();
70  if (selectedText != null && !selectedText.isEmpty()) {
71  textArea.setSelectedText(buff.toString());
72  } else {
73  textArea.getDocument().insertString(textArea.getCaretPosition(), buff.toString(), null);
74  }
75  } catch (final ClassNotFoundException ex) {
76  LOG.error("syntax.InputHandler: Paste action failed due to ClassNotFoundException");
77  } catch (final UnsupportedFlavorException ex) {
78  LOG.error("syntax.InputHandler: Paste action failed because clipboard data flavour is not supported.");
79  } catch (final IOException ex) {
80  LOG.error("syntax.InputHandler: Paste action failed due to IOException");
81  } catch (final BadLocationException ex) {
82  LOG.error("syntax.InputHandler: Paste action failed due to BadLocationException");
83  } catch (final NullPointerException ex) {
84  // this happens when clipboard is empty, it's not an error
85  }
86  }
87 
88 }
net.sf.gridarta.textedit.textarea.actions.Paste.actionPerformed
void actionPerformed(final ActionEvent e)
Get content of the system clipboard and insert it at caret position.
Definition: Paste.java:43
net.sf.gridarta.textedit.textarea.JEditTextArea.getDocument
Document getDocument()
Returns the document this text area is editing.
Definition: JEditTextArea.java:500
net.sf.gridarta.textedit.textarea.JEditTextArea
jEdit's text area component.
Definition: JEditTextArea.java:91
net.sf.gridarta.textedit.textarea
This package contains the other part of the script editor.
net.sf.gridarta
Base package of all Gridarta classes.
net.sf
net.sf.gridarta.textedit.textarea.InputHandler
An input handler converts the user's key strokes into concrete actions.
Definition: InputHandler.java:36
net.sf.gridarta.textedit.textarea.actions.Paste.LOG
static final Category LOG
The Logger for printing log messages.
Definition: Paste.java:36
net.sf.gridarta.textedit
net.sf.gridarta.textedit.textarea.JEditTextArea.getSelectedText
String getSelectedText()
Returns the selected text, or null if no selection is active.
Definition: JEditTextArea.java:709
net.sf.gridarta.textedit.textarea.actions.Paste
Action listener for PASTE actions.
Definition: Paste.java:31
net
net.sf.gridarta.textedit.textarea.JEditTextArea.setSelectedText
void setSelectedText(@NotNull final String selectedText)
Replaces the selection with the specified text.
Definition: JEditTextArea.java:717
net.sf.gridarta.textedit.textarea.InputHandler.getTextArea
static JEditTextArea getTextArea(final EventObject evt)
Returns the text area that fired the specified event.
Definition: InputHandler.java:144
net.sf.gridarta.textedit.textarea.JEditTextArea.getCaretPosition
int getCaretPosition()
Returns the caret position.
Definition: JEditTextArea.java:605