use of java.awt.datatransfer.Clipboard in project JMRI by JMRI.
the class ReportContextAction method actionPerformed.
@Override
public void actionPerformed(ActionEvent ev) {
// JmriJFrame to ensure fits on screen
final JFrame frame = new JmriJFrame(Bundle.getMessage("TitleContext"));
final Clipboard clipboard = frame.getToolkit().getSystemClipboard();
pane = new JTextArea();
// add a little space at top
pane.append("\n");
pane.setEditable(false);
pane.setLineWrap(true);
pane.setWrapStyleWord(true);
pane.setColumns(120);
JScrollPane scroll = new JScrollPane(pane);
frame.add(scroll, BorderLayout.CENTER);
ReportContext r = new ReportContext();
addString(r.getReport(true));
// add a little space at bottom
pane.append("\n");
// Add button to allow copy to clipboard
JPanel p = new JPanel();
JButton copy = new JButton(Bundle.getMessage("ButtonCopyClip"));
copy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
StringSelection text = new StringSelection(pane.getText());
clipboard.setContents(text, text);
}
});
p.add(copy);
JButton close = new JButton(Bundle.getMessage("ButtonClose"));
close.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
frame.setVisible(false);
frame.dispose();
}
});
p.add(close);
frame.add(p, BorderLayout.SOUTH);
frame.pack();
// start scrolled to top
pane.setCaretPosition(0);
JScrollBar b = scroll.getVerticalScrollBar();
b.setValue(b.getMaximum());
// show
frame.setVisible(true);
}
use of java.awt.datatransfer.Clipboard in project knime-core by knime.
the class CopyAction method actionPerformed.
/**
* {@inheritDoc}
*/
public void actionPerformed(final ActionEvent e) {
int[] rows = m_table.getSelectedRows();
int[] cols = m_table.getSelectedColumns();
boolean isContiguousBlockSelected = rows.length == rows[rows.length - 1] - rows[0] + 1 && cols.length == cols[cols.length - 1] - cols[0] + 1;
if (!isContiguousBlockSelected) {
JOptionPane.showMessageDialog(null, "This function cannot be used for multiple selection", "KNIME", JOptionPane.INFORMATION_MESSAGE);
return;
}
StringBuilder builder = new StringBuilder();
for (int i = 0; i < rows.length; i++) {
for (int k = 0; k < cols.length; k++) {
Object value = m_table.getValueAt(rows[i], cols[k]);
if (value instanceof Cell) {
builder.append(((Cell) value).getText());
} else {
builder.append(value.toString());
}
if (k < cols.length - 1) {
builder.append("\t");
}
}
builder.append("\n");
}
StringSelection str = new StringSelection(builder.toString());
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(str, str);
}
use of java.awt.datatransfer.Clipboard in project knime-core by knime.
the class CutAction method actionPerformed.
/**
* {@inheritDoc}
*/
public void actionPerformed(final ActionEvent e) {
int[] rows = m_table.getSelectedRows();
int[] cols = m_table.getSelectedColumns();
boolean isContiguousBlockSelected = rows.length == rows[rows.length - 1] - rows[0] + 1 && cols.length == cols[cols.length - 1] - cols[0] + 1;
if (!isContiguousBlockSelected) {
JOptionPane.showMessageDialog(null, "This function cannot be used for multiple selection", "KNIME", JOptionPane.INFORMATION_MESSAGE);
return;
}
StringBuilder builder = new StringBuilder();
for (int i = 0; i < rows.length; i++) {
for (int k = 0; k < cols.length; k++) {
Object value = m_table.getValueAt(rows[i], cols[k]);
if (value instanceof Cell) {
builder.append(((Cell) value).getText());
} else {
builder.append(value.toString());
}
m_table.setValueAt("", rows[i], cols[k]);
if (k < cols.length - 1) {
builder.append("\t");
}
}
builder.append("\n");
}
StringSelection str = new StringSelection(builder.toString());
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(str, str);
}
use of java.awt.datatransfer.Clipboard in project megameklab by MegaMek.
the class MenuBarCreator method jMenuExportEntityClipboard_actionPerformed.
public void jMenuExportEntityClipboard_actionPerformed(ActionEvent event) {
MechView mview = new MechView(parentFrame.getEntity(), true, true, false);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection stringSelection = new StringSelection(mview.getMechReadout());
clipboard.setContents(stringSelection, this);
}
use of java.awt.datatransfer.Clipboard in project Terasology by MovingBlocks.
the class AbstractEditorScreen method pasteJson.
/**
* Attempts to serialize the system clipboard's contents - if successful,
* sets the current state of the editor to the serialized {@link JsonTree}.
*/
protected void pasteJson() {
// Get the clipboard contents.
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable t = clipboard.getContents(null);
// Attempt to convert them to a string.
String clipboardContents = null;
try {
if (t != null) {
clipboardContents = (String) t.getTransferData(DataFlavor.stringFlavor);
}
} catch (UnsupportedFlavorException | IOException e) {
logger.warn("Could not fetch clipboard contents.", e);
}
if (clipboardContents != null) {
try {
// Attempt to serialize them to a JsonTree and reset the editor state.
JsonElement json = new JsonParser().parse(clipboardContents);
JsonTree node = JsonTreeConverter.serialize(json);
resetState(node);
} catch (JsonSyntaxException | NullPointerException e) {
logger.warn("Could not construct a valid tree from clipboard contents.", e);
}
}
}
Aggregations