List of usage examples for java.awt.datatransfer DataFlavor selectBestTextFlavor
public static final DataFlavor selectBestTextFlavor(DataFlavor[] availableFlavors)
From source file:Main.java
public static void main(String[] args) { DataFlavor df1 = new DataFlavor("text/plain; charset=ASCII", "Plain ASCII text"); df1.selectBestTextFlavor(new DataFlavor[] { DataFlavor.stringFlavor }); }
From source file:UTest.java
public boolean importData(JComponent src, Transferable transferable) { // Ok, here's the tricky part... println("Receiving data from " + src); println("Transferable object is: " + transferable); println("Valid data flavors: "); DataFlavor[] flavors = transferable.getTransferDataFlavors(); DataFlavor listFlavor = null; DataFlavor objectFlavor = null; DataFlavor readerFlavor = null; int lastFlavor = flavors.length - 1; // Check the flavors and see if we find one we like. // If we do, save it. for (int f = 0; f <= lastFlavor; f++) { println(" " + flavors[f]); if (flavors[f].isFlavorJavaFileListType()) { listFlavor = flavors[f];/* w w w . j a v a2s . c o m*/ } if (flavors[f].isFlavorSerializedObjectType()) { objectFlavor = flavors[f]; } if (flavors[f].isRepresentationClassReader()) { readerFlavor = flavors[f]; } } // Ok, now try to display the content of the drop. try { DataFlavor bestTextFlavor = DataFlavor.selectBestTextFlavor(flavors); BufferedReader br = null; String line = null; if (bestTextFlavor != null) { println("Best text flavor: " + bestTextFlavor.getMimeType()); println("Content:"); Reader r = bestTextFlavor.getReaderForText(transferable); br = new BufferedReader(r); line = br.readLine(); while (line != null) { println(line); line = br.readLine(); } br.close(); } else if (listFlavor != null) { java.util.List list = (java.util.List) transferable.getTransferData(listFlavor); println(list); } else if (objectFlavor != null) { println("Data is a java object:\n" + transferable.getTransferData(objectFlavor)); } else if (readerFlavor != null) { println("Data is an InputStream:"); br = new BufferedReader((Reader) transferable.getTransferData(readerFlavor)); line = br.readLine(); while (line != null) { println(line); } br.close(); } else { // Don't know this flavor type yet... println("No text representation to show."); } println("\n\n"); } catch (Exception e) { println("Caught exception decoding transfer:"); println(e); return false; } return true; }
From source file:org.gtdfree.GTDFree.java
protected TrayIcon getTrayIcon() { if (trayIcon == null) { if (ApplicationHelper.isGTKLaF()) { trayIcon = new TrayIcon(ApplicationHelper.loadImage(ApplicationHelper.icon_name_large_tray_splash)); } else {/*from www .j a va 2 s.com*/ trayIcon = new TrayIcon(ApplicationHelper.loadImage(ApplicationHelper.icon_name_small_tray_splash)); } trayIcon.setImageAutoSize(true); trayIcon.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1) { trayIconPopup.setVisible(false); if (getJFrame().isVisible()) { getJFrame().setVisible(false); } else { pushVisible(); } } else { if (trayIconPopup.isVisible()) { trayIconPopup.setVisible(false); } else { Point p = new Point(e.getPoint()); /* * Disabled, because we are anyway doing things like rollover, * which are probably done by Frame. if (getJFrame().isShowing()) { SwingUtilities.convertPointFromScreen(p, getJFrame()); trayIconPopup.show(getJFrame(), p.x, p.y); } else { }*/ trayIconPopup.show(null, p.x, p.y); } } } }); trayIcon.setToolTip("GTD-Free - " + Messages.getString("GTDFree.Tray.desc")); //$NON-NLS-1$ //$NON-NLS-2$ /* * Necessary only when popup is showing with null window. Hides popup. */ MouseListener hideMe = new MouseAdapter() { @Override public void mouseExited(MouseEvent e) { if (e.getComponent() instanceof JMenuItem) { JMenuItem jm = (JMenuItem) e.getComponent(); jm.getModel().setRollover(false); jm.getModel().setArmed(false); jm.repaint(); } Point p = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), trayIconPopup); //System.out.println(p.x+" "+p.y+" "+trayIconPopup.getWidth()+" "+trayIconPopup.getHeight()); if (p.x < 0 || p.x >= trayIconPopup.getWidth() || p.y < 0 || p.y >= trayIconPopup.getHeight()) { trayIconPopup.setVisible(false); } } @Override public void mouseEntered(MouseEvent e) { if (e.getComponent() instanceof JMenuItem) { JMenuItem jm = (JMenuItem) e.getComponent(); jm.getModel().setRollover(true); jm.getModel().setArmed(true); jm.repaint(); } } }; trayIconPopup = new JPopupMenu(); trayIconPopup.addMouseListener(hideMe); JMenuItem mi = new JMenuItem(Messages.getString("GTDFree.Tray.Drop")); //$NON-NLS-1$ mi.setIcon(ApplicationHelper.getIcon(ApplicationHelper.icon_name_small_collecting)); mi.setToolTipText(Messages.getString("GTDFree.Tray.Drop.desc")); //$NON-NLS-1$ mi.addMouseListener(hideMe); /* * Workaround for tray, if JFrame is showing, then mouse click is not fired */ mi.addMouseListener(new MouseAdapter() { private boolean click = false; @Override public void mousePressed(MouseEvent e) { click = true; } @Override public void mouseReleased(MouseEvent e) { if (click) { click = false; doMouseClicked(e); } } @Override public void mouseExited(MouseEvent e) { click = false; } private void doMouseClicked(MouseEvent e) { trayIconPopup.setVisible(false); Clipboard c = null; if (e.getButton() == MouseEvent.BUTTON1) { c = Toolkit.getDefaultToolkit().getSystemClipboard(); } else if (e.getButton() == MouseEvent.BUTTON2) { c = Toolkit.getDefaultToolkit().getSystemSelection(); } else { return; } try { Object o = c.getData(DataFlavor.stringFlavor); if (o != null) { getEngine().getGTDModel().collectAction(o.toString()); } flashMessage(Messages.getString("GTDFree.Tray.Collect.ok"), e.getLocationOnScreen()); //$NON-NLS-1$ } catch (Exception e1) { Logger.getLogger(this.getClass()).debug("Internal error.", e1); //$NON-NLS-1$ flashMessage(Messages.getString("GTDFree.Tray.Collect.fail") + e1.getMessage(), //$NON-NLS-1$ e.getLocationOnScreen()); } } }); TransferHandler th = new TransferHandler() { private static final long serialVersionUID = 1L; @Override public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) { return DataFlavor.selectBestTextFlavor(transferFlavors) != null; } @Override public boolean importData(JComponent comp, Transferable t) { try { DataFlavor f = DataFlavor.selectBestTextFlavor(t.getTransferDataFlavors()); Object o = t.getTransferData(f); if (o != null) { getEngine().getGTDModel().collectAction(o.toString()); } return true; } catch (UnsupportedFlavorException e) { Logger.getLogger(this.getClass()).debug("Internal error.", e); //$NON-NLS-1$ } catch (IOException e) { Logger.getLogger(this.getClass()).debug("Internal error.", e); //$NON-NLS-1$ } return false; } }; mi.setTransferHandler(th); trayIconPopup.add(mi); mi = new JMenuItem(); mi.setIcon(ApplicationHelper.getIcon(ApplicationHelper.icon_name_large_delete)); mi.setText(Messages.getString("GTDFree.Tray.Hide")); //$NON-NLS-1$ mi.setToolTipText(Messages.getString("GTDFree.Tray.Hide.desc")); //$NON-NLS-1$ mi.addMouseListener(hideMe); mi.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { trayIconPopup.setVisible(false); if (getJFrame().isVisible()) { getJFrame().setVisible(false); } } }); trayIconPopup.add(mi); mi = new JMenuItem(); mi.setIcon(ApplicationHelper.getIcon(ApplicationHelper.icon_name_small_splash)); mi.setText(Messages.getString("GTDFree.Tray.Show")); //$NON-NLS-1$ mi.setToolTipText(Messages.getString("GTDFree.Tray.Show.desc")); //$NON-NLS-1$ mi.addMouseListener(hideMe); mi.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { trayIconPopup.setVisible(false); pushVisible(); } }); trayIconPopup.add(mi); mi = new JMenuItem(); mi.setIcon(ApplicationHelper.getIcon(ApplicationHelper.icon_name_large_exit)); mi.setText(Messages.getString("GTDFree.Tray.Exit")); //$NON-NLS-1$ mi.addMouseListener(hideMe); mi.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { trayIconPopup.setVisible(false); close(false); } }); trayIconPopup.add(mi); } return trayIcon; }
From source file:pl.otros.logview.gui.actions.read.DragAndDropFilesHandler.java
private boolean isText(TransferSupport support) { boolean b = DataFlavor.selectBestTextFlavor(support.getDataFlavors()) != null; LOGGER.info("Data transfer is text: " + b); return b;/*from ww w . j av a2s . c om*/ }
From source file:pl.otros.logview.gui.actions.read.DragAndDropFilesHandler.java
public List<String> getFileUris(TransferSupport support) { BufferedReader reader = null; List<String> files = new ArrayList<String>(); try {// ww w .j av a 2 s. c o m reader = new BufferedReader(DataFlavor.selectBestTextFlavor(support.getDataFlavors()) .getReaderForText(support.getTransferable())); String uri = null; while ((uri = reader.readLine()) != null) { files.add(uri); } } catch (UnsupportedFlavorException e) { throw new RuntimeException("The dropped data is not supported. The unsupported DataFlavors are " + StringUtils.join(support.getDataFlavors(), ","), e); } catch (IOException e) { throw new RuntimeException("Cannot read the dropped data.", e); } return files; }
From source file:pl.otros.logview.gui.Log4jPatternParserEditor.java
private void enableDragAndDrop() { logFileContent.setDragEnabled(true); final TransferHandler defaultTransferHandler = logFileContent.getTransferHandler(); TransferHandler transferHandler = new TransferHandler() { @Override//from w w w. j av a 2 s . c om public boolean importData(TransferSupport support) { if (isText(support)) { try { String transferData = (String) support.getTransferable() .getTransferData(DataFlavor.stringFlavor); if (transferData.startsWith("file://")) { String firstLine = transferData; if (firstLine.indexOf('\n') > 0) { firstLine = firstLine.substring(0, firstLine.indexOf('\n') - 1); } loadLogContent(VFS.getManager().resolveFile(firstLine)); } else { defaultTransferHandler.importData(support); } return true; } catch (UnsupportedFlavorException e) { LOGGER.warning("Can't import data, UnsupportedFlavorException: " + e.getMessage()); } catch (IOException e) { LOGGER.warning("Can't import data, IOException: " + e.getMessage()); } } if (isListOfFiles(support)) { try { List data = (List) support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor); if (data.size() > 0) { File file = (File) data.get(0); loadLogContent(VFS.getManager().resolveFile(file.getAbsolutePath())); return true; } } catch (UnsupportedFlavorException e) { LOGGER.warning("Can't import data, UnsupportedFlavorException: " + e.getMessage()); } catch (IOException e) { LOGGER.warning("Can't import data, IOException: " + e.getMessage()); } } return defaultTransferHandler.importData(support); } @Override public boolean canImport(TransferSupport support) { if (isText(support) || isListOfFiles(support)) { return true; } return defaultTransferHandler.canImport(support); } private boolean isListOfFiles(TransferSupport support) { return support.isDataFlavorSupported(DataFlavor.javaFileListFlavor); } private boolean isText(TransferSupport support) { return DataFlavor.selectBestTextFlavor(support.getDataFlavors()) != null; } }; logFileContent.setTransferHandler(transferHandler); loadLog.setTransferHandler(transferHandler); logFileContentLabel.setTransferHandler(transferHandler); }