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-2015 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  final StringBuilder buff = new StringBuilder();
46 
47  // set above string to the system clipboard
48  final Transferable trans = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(textArea);
49  try {
50  // now read the system clipboard in plain-text format
51  final DataFlavor dataFlavor = new DataFlavor("text/plain; charset=unicode");
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 }
void setSelectedText(@NotNull final String selectedText)
Replaces the selection with the specified text.
Action listener for PASTE actions.
Definition: Paste.java:31
void actionPerformed(final ActionEvent e)
Get content of the system clipboard and insert it at caret position.
Definition: Paste.java:43
An input handler converts the user's key strokes into concrete actions.
Base package of all Gridarta classes.
String getSelectedText()
Returns the selected text, or null if no selection is active.
This package contains the other part of the script editor.
int getCaretPosition()
Returns the caret position.
Document getDocument()
Returns the document this text area is editing.
static JEditTextArea getTextArea(final EventObject evt)
Returns the text area that fired the specified event.
static final Category LOG
The Logger for printing log messages.
Definition: Paste.java:36