use of java.awt.datatransfer.UnsupportedFlavorException in project jna by java-native-access.
the class AlphaMaskDemo2 method run.
public void run() {
// Must find a graphics configuration with a depth of 32 bits
GraphicsConfiguration gconfig = WindowUtils.getAlphaCompatibleGraphicsConfiguration();
frame = new JFrame("Alpha Mask Demo");
alphaWindow = new JWindow(frame, gconfig);
icon = new JLabel();
icon.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
alphaWindow.getContentPane().add(icon);
JButton quit = new JButton("Quit");
JLabel label = new JLabel("Drag this window by its image");
label.setHorizontalAlignment(SwingConstants.CENTER);
alphaWindow.getContentPane().add(label, BorderLayout.NORTH);
alphaWindow.getContentPane().add(quit, BorderLayout.SOUTH);
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
MouseInputAdapter handler = new MouseInputAdapter() {
private Point offset;
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e))
offset = e.getPoint();
}
public void mouseReleased(MouseEvent e) {
offset = null;
}
public void mouseDragged(MouseEvent e) {
if (offset != null) {
Window w = (Window) e.getSource();
Point where = e.getPoint();
where.translate(-offset.x, -offset.y);
Point loc = w.getLocationOnScreen();
loc.translate(where.x, where.y);
w.setLocation(loc.x, loc.y);
}
}
};
alphaWindow.addMouseListener(handler);
alphaWindow.addMouseMotionListener(handler);
JPanel p = new JPanel(new BorderLayout(8, 8));
p.setBorder(new EmptyBorder(8, 8, 8, 8));
p.setTransferHandler(new TransferHandler() {
private static final long serialVersionUID = 1L;
public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
List<DataFlavor> list = Arrays.asList(transferFlavors);
if (list.contains(URL_FLAVOR) || list.contains(URI_LIST_FLAVOR) || list.contains(DataFlavor.imageFlavor) || list.contains(DataFlavor.javaFileListFlavor)) {
return true;
}
if (DataFlavor.selectBestTextFlavor(transferFlavors) != null) {
return true;
}
System.err.println("No acceptable flavor found in " + Arrays.asList(transferFlavors));
return false;
}
public boolean importData(JComponent comp, Transferable t) {
try {
if (t.isDataFlavorSupported(URL_FLAVOR)) {
URL url = (URL) t.getTransferData(URL_FLAVOR);
setImage(Toolkit.getDefaultToolkit().getImage(url));
return true;
}
if (t.isDataFlavorSupported(URI_LIST_FLAVOR)) {
String s = (String) t.getTransferData(URI_LIST_FLAVOR);
String[] uris = s.split("[\r\n]");
if (uris.length > 0) {
URL url = new URL(uris[0]);
setImage(Toolkit.getDefaultToolkit().getImage(url));
return true;
}
return false;
}
if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
Image image = (Image) t.getTransferData(DataFlavor.imageFlavor);
setImage(image);
return true;
}
if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
List<File> files = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
File f = files.get(0);
URL url = new URL("file://" + f.toURI().toURL().getPath());
Image image = Toolkit.getDefaultToolkit().getImage(url);
setImage(image);
return true;
}
DataFlavor flavor = DataFlavor.selectBestTextFlavor(t.getTransferDataFlavors());
if (flavor != null) {
Reader reader = flavor.getReaderForText(t);
char[] buf = new char[512];
StringBuilder b = new StringBuilder();
int count;
// encoding wrong
while ((count = reader.read(buf)) > 0) {
for (int i = 0; i < count; i++) {
if (buf[i] != 0)
b.append(buf, i, 1);
}
}
String html = b.toString();
Pattern p = Pattern.compile("<img.*src=\"([^\\\"\">]+)\"", Pattern.CANON_EQ | Pattern.UNICODE_CASE);
Matcher m = p.matcher(html);
if (m.find()) {
URL url = new URL(m.group(1));
System.out.println("Load image from " + url);
Image image = Toolkit.getDefaultToolkit().getImage(url);
setImage(image);
return true;
}
System.err.println("Can't parse text: " + html);
return false;
}
System.err.println("No flavor available: " + Arrays.asList(t.getTransferDataFlavors()));
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
}
return false;
}
});
p.add(new JLabel("<html><center>Drop an image with an alpha channel onto this window<br>" + "You may also adjust the overall transparency with the slider</center></html>"), BorderLayout.NORTH);
p.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
final JSlider slider = new JSlider(0, 255, 255);
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int value = slider.getValue();
WindowUtils.setWindowAlpha(alphaWindow, value / 255f);
}
});
p.add(slider, BorderLayout.SOUTH);
frame.getContentPane().add(p);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
centerOnScreen(frame);
frame.setVisible(true);
WindowUtils.setWindowTransparent(alphaWindow, true);
alphaWindow.setLocation(frame.getX() + frame.getWidth() + 4, frame.getY());
try {
URL url = getClass().getResource("tardis.png");
if (url != null) {
setImage(Toolkit.getDefaultToolkit().getImage(url));
}
} catch (Exception e) {
}
}
use of java.awt.datatransfer.UnsupportedFlavorException in project jdk8u_jdk by JetBrains.
the class TargetFileListFrame method extractListOfFiles.
private java.util.List<File> extractListOfFiles(DropTargetDropEvent dtde) {
BufferedReader reader = null;
ArrayList<File> files = new ArrayList<File>();
try {
reader = new BufferedReader((Reader) dtde.getTransferable().getTransferData(dropFlavor));
String line;
while ((line = reader.readLine()) != null) {
files.add(new File(new URI(line)));
}
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {
}
}
}
return files;
}
use of java.awt.datatransfer.UnsupportedFlavorException in project intellij-community by JetBrains.
the class EditorImpl method handleDrop.
static boolean handleDrop(@NotNull EditorImpl editor, @NotNull final Transferable t, int dropAction) {
final EditorDropHandler dropHandler = editor.getDropHandler();
if (Registry.is("debugger.click.disable.breakpoints")) {
try {
if (t.isDataFlavorSupported(GutterDraggableObject.flavor)) {
Object attachedObject = t.getTransferData(GutterDraggableObject.flavor);
if (attachedObject instanceof GutterIconRenderer) {
GutterDraggableObject object = ((GutterIconRenderer) attachedObject).getDraggableObject();
if (object != null) {
object.remove();
Point mouseLocationOnScreen = MouseInfo.getPointerInfo().getLocation();
JComponent editorComponent = editor.getComponent();
Point editorComponentLocationOnScreen = editorComponent.getLocationOnScreen();
IdeGlassPaneUtil.installPainter(editorComponent, new ExplosionPainter(new Point(mouseLocationOnScreen.x - editorComponentLocationOnScreen.x, mouseLocationOnScreen.y - editorComponentLocationOnScreen.y), editor.getGutterComponentEx().getDragImage((GutterIconRenderer) attachedObject)), editor.getDisposable());
return true;
}
}
}
} catch (UnsupportedFlavorException | IOException e) {
LOG.warn(e);
}
}
if (dropHandler != null && dropHandler.canHandleDrop(t.getTransferDataFlavors())) {
dropHandler.handleDrop(t, editor.getProject(), null, dropAction);
return true;
}
final int caretOffset = editor.getCaretModel().getOffset();
if (editor.myDraggedRange != null && editor.myDraggedRange.getStartOffset() <= caretOffset && caretOffset < editor.myDraggedRange.getEndOffset()) {
return false;
}
if (editor.myDraggedRange != null) {
editor.getCaretModel().moveToOffset(editor.mySavedCaretOffsetForDNDUndoHack);
}
CommandProcessor.getInstance().executeCommand(editor.myProject, () -> {
try {
editor.getSelectionModel().removeSelection();
final int offset;
if (editor.myDraggedRange != null) {
editor.getCaretModel().moveToOffset(caretOffset);
offset = caretOffset;
} else {
offset = editor.getCaretModel().getOffset();
}
if (editor.getDocument().getRangeGuard(offset, offset) != null) {
return;
}
editor.putUserData(LAST_PASTED_REGION, null);
EditorActionHandler pasteHandler = EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_PASTE);
LOG.assertTrue(pasteHandler instanceof EditorTextInsertHandler);
((EditorTextInsertHandler) pasteHandler).execute(editor, editor.getDataContext(), () -> t);
TextRange range = editor.getUserData(LAST_PASTED_REGION);
if (range != null) {
editor.getCaretModel().moveToOffset(range.getStartOffset());
editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
}
} catch (Exception exception) {
LOG.error(exception);
}
}, EditorBundle.message("paste.command.name"), DND_COMMAND_KEY, UndoConfirmationPolicy.DEFAULT, editor.getDocument());
return true;
}
use of java.awt.datatransfer.UnsupportedFlavorException in project intellij-community by JetBrains.
the class MultiplePasteAction method actionPerformed.
@Override
public void actionPerformed(final AnActionEvent e) {
final DataContext dataContext = e.getDataContext();
Project project = CommonDataKeys.PROJECT.getData(dataContext);
Component focusedComponent = e.getData(PlatformDataKeys.CONTEXT_COMPONENT);
Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
if (!(focusedComponent instanceof JComponent))
return;
final CopyPasteManagerEx copyPasteManager = CopyPasteManagerEx.getInstanceEx();
final ContentChooser<Transferable> chooser = new ContentChooser<Transferable>(project, UIBundle.message("choose.content.to.paste.dialog.title"), true, true) {
@Override
protected String getStringRepresentationFor(final Transferable content) {
try {
return (String) content.getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException | IOException e1) {
return "";
}
}
@Override
protected List<Transferable> getContents() {
return Arrays.asList(CopyPasteManager.getInstance().getAllContents());
}
@Override
protected void removeContentAt(final Transferable content) {
copyPasteManager.removeContent(content);
}
};
if (!chooser.getAllContents().isEmpty()) {
chooser.show();
} else {
chooser.close(DialogWrapper.CANCEL_EXIT_CODE);
}
if (chooser.isOK()) {
List<Transferable> selectedContents = chooser.getSelectedContents();
if (selectedContents.size() == 1) {
copyPasteManager.moveContentToStackTop(selectedContents.get(0));
} else {
copyPasteManager.setContents(new StringSelection(chooser.getSelectedText()));
}
if (editor != null) {
if (editor.isViewer())
return;
final AnAction pasteAction = ActionManager.getInstance().getAction(IdeActions.ACTION_PASTE);
AnActionEvent newEvent = new AnActionEvent(e.getInputEvent(), DataManager.getInstance().getDataContext(focusedComponent), e.getPlace(), e.getPresentation(), ActionManager.getInstance(), e.getModifiers());
pasteAction.actionPerformed(newEvent);
} else {
final Action pasteAction = ((JComponent) focusedComponent).getActionMap().get(DefaultEditorKit.pasteAction);
if (pasteAction != null) {
pasteAction.actionPerformed(new ActionEvent(focusedComponent, ActionEvent.ACTION_PERFORMED, ""));
}
}
}
}
use of java.awt.datatransfer.UnsupportedFlavorException in project jmeter by apache.
the class ArgumentsPanel method addFromClipboard.
/**
* Add values from the clipboard
*/
protected void addFromClipboard() {
GuiUtils.stopTableEditing(table);
int rowCount = table.getRowCount();
try {
String clipboardContent = GuiUtils.getPastedText();
if (clipboardContent == null) {
return;
}
String[] clipboardLines = clipboardContent.split("\n");
for (String clipboardLine : clipboardLines) {
String[] clipboardCols = clipboardLine.split("\t");
if (clipboardCols.length > 0) {
Argument argument = createArgumentFromClipboard(clipboardCols);
tableModel.addRow(argument);
}
}
if (table.getRowCount() > rowCount) {
checkButtonsStatus();
// Highlight (select) and scroll to the appropriate rows.
int rowToSelect = tableModel.getRowCount() - 1;
table.setRowSelectionInterval(rowCount, rowToSelect);
table.scrollRectToVisible(table.getCellRect(rowCount, 0, true));
}
} catch (IOException ioe) {
JOptionPane.showMessageDialog(this, "Could not add read arguments from clipboard:\n" + ioe.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE);
} catch (UnsupportedFlavorException ufe) {
JOptionPane.showMessageDialog(this, "Could not add retrieve " + DataFlavor.stringFlavor.getHumanPresentableName() + " from clipboard" + ufe.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
Aggregations