use of java.awt.datatransfer.StringSelection in project jdk8u_jdk by JetBrains.
the class TestDragSourceAdapter method main.
public static void main(String[] args) throws Exception {
DragSource ds = new DragSource();
TestDragSourceAdapter dsa1 = new TestDragSourceAdapter(1);
TestDragSourceAdapter dsa2 = new TestDragSourceAdapter(2);
Component c = new Button();
DragGestureRecognizer dgr = ds.createDefaultDragGestureRecognizer(c, DnDConstants.ACTION_COPY, e -> e.startDrag(null, null));
MouseEvent me = new MouseEvent(c, MouseEvent.MOUSE_PRESSED, 0, InputEvent.CTRL_MASK, 100, 100, 0, false);
DragGestureEvent dge = new DragGestureEvent(dgr, DnDConstants.ACTION_COPY, new Point(100, 100), Arrays.asList(me));
DragSourceContext dsc = new DragSourceContext(Toolkit.getDefaultToolkit().createDragSourceContextPeer(dge), dge, new Cursor(Cursor.HAND_CURSOR), null, null, new StringSelection("TEXT"), null);
ds.addDragSourceListener(dsa1);
ds.addDragSourceListener(dsa2);
ds.addDragSourceListener(dsa2);
ds.addDragSourceMotionListener(dsa1);
ds.addDragSourceMotionListener(dsa1);
ds.addDragSourceMotionListener(dsa2);
dsc.addDragSourceListener(dsa2);
byte[] serialized;
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
oos.writeObject(dsc);
serialized = bos.toByteArray();
}
DragSourceContext dsc_copy;
try (ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
ObjectInputStream ois = new ObjectInputStream(bis)) {
dsc_copy = (DragSourceContext) ois.readObject();
}
try {
dsc_copy.addDragSourceListener(dsa1);
throw new RuntimeException("Test failed. Listener addition succeeded");
} catch (TooManyListenersException ignored) {
}
try {
dsc_copy.addDragSourceListener(dsa2);
throw new RuntimeException("Test failed. Listener addition succeeded");
} catch (TooManyListenersException ignored) {
}
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
oos.writeObject(ds);
serialized = bos.toByteArray();
}
DragSource ds_copy;
try (ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
ObjectInputStream ois = new ObjectInputStream(bis)) {
ds_copy = (DragSource) ois.readObject();
}
DragSourceListener[] dsls = ds_copy.getDragSourceListeners();
assertEquals(3, dsls.length, "DragSourceListeners number");
assertEquals(1, Stream.of(dsls).filter(dsa1::equals).collect(Collectors.counting()).intValue());
assertEquals(2, Stream.of(dsls).filter(dsa2::equals).collect(Collectors.counting()).intValue());
DragSourceMotionListener[] dsmls = ds_copy.getDragSourceMotionListeners();
assertEquals(3, dsmls.length, "DragSourceMotionListeners number");
assertEquals(2, Stream.of(dsmls).filter(dsa1::equals).collect(Collectors.counting()).intValue());
assertEquals(1, Stream.of(dsmls).filter(dsa2::equals).collect(Collectors.counting()).intValue());
}
use of java.awt.datatransfer.StringSelection in project JMRI by JMRI.
the class SystemConsole method createFrame.
/**
* Layout the console frame
*/
private void createFrame() {
// Use a JmriJFrame to ensure that we fit on the screen
frame = new JmriJFrame(Bundle.getMessage("TitleConsole"));
pref = jmri.InstanceManager.getDefault(jmri.UserPreferencesManager.class);
// Add Help menu (Windows menu automaitically added)
// NOI18N
frame.addHelpMenu("package.apps.SystemConsole", true);
// Grab a reference to the system clipboard
final Clipboard clipboard = frame.getToolkit().getSystemClipboard();
// Setup the scroll pane
JScrollPane scroll = new JScrollPane(console);
frame.add(scroll, BorderLayout.CENTER);
// Add button to allow copy to clipboard
JPanel p = new JPanel();
JButton copy = new JButton(Bundle.getMessage("ButtonCopyClip"));
copy.addActionListener((ActionEvent event) -> {
StringSelection text = new StringSelection(console.getText());
clipboard.setContents(text, text);
});
p.add(copy);
// Add button to allow console window to be closed
JButton close = new JButton(Bundle.getMessage("ButtonClose"));
close.addActionListener((ActionEvent event) -> {
frame.setVisible(false);
frame.dispose();
});
p.add(close);
JButton stackTrace = new JButton(Bundle.getMessage("ButtonStackTrace"));
stackTrace.addActionListener((ActionEvent event) -> {
performStackTrace();
});
p.add(stackTrace);
// Add checkbox to enable/disable auto-scrolling
// Use the inverted SimplePreferenceState to default as enabled
p.add(autoScroll = new JCheckBox(Bundle.getMessage("CheckBoxAutoScroll"), !pref.getSimplePreferenceState(alwaysScrollCheck)));
autoScroll.addActionListener((ActionEvent event) -> {
doAutoScroll(console, autoScroll.isSelected());
pref.setSimplePreferenceState(alwaysScrollCheck, !autoScroll.isSelected());
});
// Add checkbox to enable/disable always on top
p.add(alwaysOnTop = new JCheckBox(Bundle.getMessage("CheckBoxOnTop"), pref.getSimplePreferenceState(alwaysOnTopCheck)));
alwaysOnTop.setVisible(true);
alwaysOnTop.setToolTipText(Bundle.getMessage("ToolTipOnTop"));
alwaysOnTop.addActionListener((ActionEvent event) -> {
frame.setAlwaysOnTop(alwaysOnTop.isSelected());
pref.setSimplePreferenceState(alwaysOnTopCheck, alwaysOnTop.isSelected());
});
frame.setAlwaysOnTop(alwaysOnTop.isSelected());
// Define the pop-up menu
copySelection = new JMenuItem(Bundle.getMessage("MenuItemCopy"));
copySelection.addActionListener((ActionEvent event) -> {
StringSelection text = new StringSelection(console.getSelectedText());
clipboard.setContents(text, text);
});
popup.add(copySelection);
JMenuItem menuItem = new JMenuItem(Bundle.getMessage("ButtonCopyClip"));
menuItem.addActionListener((ActionEvent event) -> {
StringSelection text = new StringSelection(console.getText());
clipboard.setContents(text, text);
});
popup.add(menuItem);
popup.add(new JSeparator());
JRadioButtonMenuItem rbMenuItem;
// Define the colour scheme sub-menu
schemeMenu = new JMenu(rbc.getString("ConsoleSchemeMenu"));
schemeGroup = new ButtonGroup();
for (final Scheme s : schemes) {
rbMenuItem = new JRadioButtonMenuItem(s.description);
rbMenuItem.addActionListener((ActionEvent event) -> {
setScheme(schemes.indexOf(s));
});
rbMenuItem.setSelected(getScheme() == schemes.indexOf(s));
schemeMenu.add(rbMenuItem);
schemeGroup.add(rbMenuItem);
}
popup.add(schemeMenu);
// Define the wrap style sub-menu
wrapMenu = new JMenu(rbc.getString("ConsoleWrapStyleMenu"));
wrapGroup = new ButtonGroup();
rbMenuItem = new JRadioButtonMenuItem(rbc.getString("ConsoleWrapStyleNone"));
rbMenuItem.addActionListener((ActionEvent event) -> {
setWrapStyle(WRAP_STYLE_NONE);
});
rbMenuItem.setSelected(getWrapStyle() == WRAP_STYLE_NONE);
wrapMenu.add(rbMenuItem);
wrapGroup.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem(rbc.getString("ConsoleWrapStyleLine"));
rbMenuItem.addActionListener((ActionEvent event) -> {
setWrapStyle(WRAP_STYLE_LINE);
});
rbMenuItem.setSelected(getWrapStyle() == WRAP_STYLE_LINE);
wrapMenu.add(rbMenuItem);
wrapGroup.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem(rbc.getString("ConsoleWrapStyleWord"));
rbMenuItem.addActionListener((ActionEvent event) -> {
setWrapStyle(WRAP_STYLE_WORD);
});
rbMenuItem.setSelected(getWrapStyle() == WRAP_STYLE_WORD);
wrapMenu.add(rbMenuItem);
wrapGroup.add(rbMenuItem);
popup.add(wrapMenu);
// Bind pop-up to objects
MouseListener popupListener = new PopupListener();
console.addMouseListener(popupListener);
frame.addMouseListener(popupListener);
// Add document listener to scroll to end when modified if required
console.getDocument().addDocumentListener(new DocumentListener() {
// References to the JTextArea and JCheckBox
// of this instantiation
JTextArea ta = console;
JCheckBox chk = autoScroll;
@Override
public void insertUpdate(DocumentEvent e) {
doAutoScroll(ta, chk.isSelected());
}
@Override
public void removeUpdate(DocumentEvent e) {
doAutoScroll(ta, chk.isSelected());
}
@Override
public void changedUpdate(DocumentEvent e) {
doAutoScroll(ta, chk.isSelected());
}
});
// Add the button panel to the frame & then arrange everything
frame.add(p, BorderLayout.SOUTH);
frame.pack();
}
use of java.awt.datatransfer.StringSelection in project jabref by JabRef.
the class BasePanel method copyKeyAndTitle.
private void copyKeyAndTitle() {
List<BibEntry> bes = mainTable.getSelectedEntries();
if (!bes.isEmpty()) {
storeCurrentEdit();
// OK: in a future version, this string should be configurable to allow arbitrary exports
StringReader sr = new StringReader("\\bibtexkey - \\begin{title}\\format[RemoveBrackets]{\\title}\\end{title}\n");
Layout layout;
try {
layout = new LayoutHelper(sr, Globals.prefs.getLayoutFormatterPreferences(Globals.journalAbbreviationLoader)).getLayoutFromText();
} catch (IOException e) {
LOGGER.info("Could not get layout", e);
return;
}
StringBuilder sb = new StringBuilder();
int copied = 0;
// Collect all non-null keys.
for (BibEntry be : bes) {
if (be.hasCiteKey()) {
copied++;
sb.append(layout.doLayout(be, bibDatabaseContext.getDatabase()));
}
}
if (copied == 0) {
output(Localization.lang("None of the selected entries have BibTeX keys."));
return;
}
final StringSelection ss = new StringSelection(sb.toString());
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, BasePanel.this);
if (copied == bes.size()) {
// All entries had keys.
output((bes.size() > 1 ? Localization.lang("Copied keys") : Localization.lang("Copied key")) + '.');
} else {
output(Localization.lang("Warning: %0 out of %1 entries have undefined BibTeX key.", Integer.toString(bes.size() - copied), Integer.toString(bes.size())));
}
}
}
use of java.awt.datatransfer.StringSelection in project jabref by JabRef.
the class ClipBoardManager method setClipboardContents.
/**
* Place a String on the clipboard, and make this class the
* owner of the Clipboard's contents.
*/
public void setClipboardContents(String aString) {
StringSelection stringSelection = new StringSelection(aString);
CLIPBOARD.setContents(stringSelection, this);
}
use of java.awt.datatransfer.StringSelection in project jabref by JabRef.
the class PreviewPanelTransferHandler method createTransferable.
@Override
protected Transferable createTransferable(JComponent component) {
if (component instanceof JEditorPane) {
// this method should be called from the preview panel only
// the default TransferHandler implementation is aware of HTML
// and returns an appropriate Transferable
// as textTransferHandler.createTransferable() is not available and
// I don't know any other method, I do the HTML conversion by hand
// First, get the HTML of the selected text
JEditorPane editorPane = (JEditorPane) component;
StringWriter stringWriter = new StringWriter();
try {
editorPane.getEditorKit().write(stringWriter, editorPane.getDocument(), editorPane.getSelectionStart(), editorPane.getSelectionEnd());
} catch (BadLocationException | IOException e) {
LOGGER.warn("Cannot write preview", e);
}
// Second, return the HTML (and text as fallback)
return new HtmlTransferable(stringWriter.toString(), editorPane.getSelectedText());
} else {
// if not called from the preview panel, return an error string
return new StringSelection(Localization.lang("Operation not supported"));
}
}
Aggregations