Search in sources :

Example 86 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project airavata by apache.

the class GraphCanvas method drop.

private void drop(final DropTargetDropEvent event) {
    logger.debug("Event:" + event);
    Transferable transferable = event.getTransferable();
    try {
        // Cannot cast transferable.
        final ComponentReference componentReference = (ComponentReference) transferable.getTransferData(ComponentSourceTransferable.FLAVOR);
        final Point location = event.getLocation();
        // The component might not have loaded if the network is slow.
        new Thread() {

            @Override
            public void run() {
                try {
                    Component component = componentReference.getComponent();
                    addNode(component, location);
                    // To be able to delete the added node by the keyboard.
                    GraphCanvas.this.panel.requestFocusInWindow();
                    // XXX this sometimes throws exception.
                    event.dropComplete(true);
                } catch (ComponentException e) {
                    // If there is any error, the component tree viewer
                    // shows the error dialog.
                    logger.error(e.getMessage(), e);
                    event.dropComplete(false);
                } catch (ComponentRegistryException e) {
                    logger.error(e.getMessage(), e);
                    event.dropComplete(false);
                }
            }
        }.start();
    } catch (UnsupportedFlavorException e) {
        // Should not happen.
        logger.error(e.getMessage(), e);
    } catch (IOException e) {
        // Should not happen.
        logger.error(e.getMessage(), e);
    }
}
Also used : ComponentSourceTransferable(org.apache.airavata.xbaya.ui.widgets.component.ComponentSourceTransferable) Transferable(java.awt.datatransfer.Transferable) ComponentReference(org.apache.airavata.workflow.model.component.ComponentReference) ComponentException(org.apache.airavata.workflow.model.component.ComponentException) Point(java.awt.Point) ComponentRegistryException(org.apache.airavata.workflow.model.component.ComponentRegistryException) IOException(java.io.IOException) InputComponent(org.apache.airavata.workflow.model.component.system.InputComponent) Component(org.apache.airavata.workflow.model.component.Component) OutputComponent(org.apache.airavata.workflow.model.component.system.OutputComponent) JComponent(javax.swing.JComponent) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Example 87 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project tetrad by cmu-phil.

the class SessionFileTransferHandler method importData.

@Override
public boolean importData(TransferSupport support) {
    try {
        List<File> files = (List<File>) support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
        for (File file : files) {
            Preferences.userRoot().put("sessionSaveLocation", file.getParent());
            Session session = DesktopController.getInstance().getSessionByName(file.getName());
            if (session != null) {
                if (session.isEmpty()) {
                    DesktopController.getInstance().closeSessionByName(file.getName());
                } else {
                    int ret = JOptionPane.showConfirmDialog(JOptionUtils.centeringComp(), "Replace existing session by that name?.", "Confirm", JOptionPane.YES_NO_OPTION);
                    if (ret == JOptionPane.YES_OPTION) {
                        DesktopController.getInstance().closeSessionByName(file.getName());
                    } else {
                        return false;
                    }
                }
            }
            try (InputStream in = Files.newInputStream(file.toPath())) {
                DecompressibleInputStream objIn = new DecompressibleInputStream(in);
                Object o = objIn.readObject();
                TetradMetadata metadata = null;
                SessionWrapper sessionWrapper = null;
                if (o instanceof TetradMetadata) {
                    metadata = (TetradMetadata) o;
                    sessionWrapper = (SessionWrapper) objIn.readObject();
                } else if (o instanceof SessionWrapper) {
                    metadata = null;
                    sessionWrapper = (SessionWrapper) o;
                }
                in.close();
                if (metadata == null) {
                    throw new NullPointerException("Could not read metadata.");
                }
                if (sessionWrapper == null) {
                    Version version = metadata.getVersion();
                    Date date = metadata.getDate();
                    SimpleDateFormat df = new SimpleDateFormat("MMM dd, yyyy");
                    JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "Could not load session. The version of the session was \n" + version + "; it was saved on " + df.format(date) + ". You " + "\nmight try loading it with that version instead.");
                    return false;
                }
                SessionEditorWorkbench graph = new SessionEditorWorkbench(sessionWrapper);
                String name = file.getName();
                sessionWrapper.setName(name);
                SessionEditor editor = new SessionEditor(name, graph);
                DesktopController.getInstance().addSessionEditor(editor);
                DesktopController.getInstance().closeEmptySessions();
                DesktopController.getInstance().putMetadata(sessionWrapper, metadata);
            } catch (FileNotFoundException exception) {
                LOGGER.error("", exception);
                JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "That wasn't a TETRAD session file: " + file);
            } catch (Exception exception) {
                LOGGER.error("", exception);
                JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "An error occurred attempting to load the session.");
            }
        }
    } catch (UnsupportedFlavorException | IOException exception) {
        LOGGER.error("", exception);
    }
    return super.importData(support);
}
Also used : TetradMetadata(edu.cmu.tetradapp.model.TetradMetadata) DecompressibleInputStream(edu.cmu.tetradapp.app.DecompressibleInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) DecompressibleInputStream(edu.cmu.tetradapp.app.DecompressibleInputStream) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) Date(java.util.Date) SessionEditorWorkbench(edu.cmu.tetradapp.app.SessionEditorWorkbench) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Version(edu.cmu.tetrad.util.Version) List(java.util.List) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) SessionEditor(edu.cmu.tetradapp.app.SessionEditor) Session(edu.cmu.tetrad.session.Session) SessionWrapper(edu.cmu.tetradapp.model.SessionWrapper)

Example 88 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project tetrad by cmu-phil.

the class TabularDataTransferHandler method importData.

public boolean importData(JComponent c, Transferable t) {
    if (c instanceof TabularDataJTable) {
        try {
            TabularDataJTable tabularData = (TabularDataJTable) c;
            String s = (String) t.getTransferData(DataFlavor.stringFlavor);
            int startRow = tabularData.getSelectedRow();
            int startCol = tabularData.getSelectedColumn();
            if (startRow == 0) {
                startRow = 1;
            }
            if (startCol < getNumLeadingCols()) {
                startCol = getNumLeadingCols();
            }
            if (!checkRanges(s, startCol, tabularData)) {
                return false;
            }
            boolean shouldAsk = false;
            boolean shiftDown = true;
            BufferedReader preReader = new BufferedReader(new CharArrayReader(s.toCharArray()));
            String preLine = preReader.readLine();
            StringTokenizer preTokenizer = new StringTokenizer(preLine, "\t");
            int numTokens = preTokenizer.countTokens();
            for (int col = startCol; col < startCol + numTokens; col++) {
                Object value = tabularData.getValueAt(startRow, col);
                if (!"".equals(value) && !(null == value)) {
                    shouldAsk = true;
                }
                if (startRow - getNumLeadingRows() >= tabularData.getDataSet().getNumRows() || startCol - getNumLeadingCols() >= tabularData.getDataSet().getNumColumns()) {
                    shouldAsk = false;
                    shiftDown = false;
                }
            }
            if (shouldAsk) {
                String[] choices = new String[] { "Shift corresponding cells down to make room", "Replace corresponding cells" };
                Object choice = JOptionPane.showInputDialog(JOptionUtils.centeringComp(), "How should the clipboard contents be pasted?", "Paste Contents", JOptionPane.INFORMATION_MESSAGE, null, choices, choices[0]);
                // Null means the user cancelled the input.
                if (choice == null) {
                    return false;
                }
                shiftDown = choice.equals(choices[0]);
            }
            doPaste(s, startRow, startCol, shiftDown, tabularData);
        } catch (UnsupportedFlavorException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }
    return false;
}
Also used : StringTokenizer(java.util.StringTokenizer) CharArrayReader(java.io.CharArrayReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Example 89 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project tetrad by cmu-phil.

the class SubgraphSelection method getTransferData.

/**
 * @return an object which represents the data to be transferred.  The class
 * of the object returned is defined by the representation class of the
 * flavor.
 *
 * @param flavor the requested flavor for the data
 * @throws IOException                if the data is no longer available in
 *                                    the requested flavor.
 * @throws UnsupportedFlavorException if the requested data flavor is not
 *                                    supported.
 * @see DataFlavor#getRepresentationClass
 */
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
    if (!isDataFlavorSupported(flavor)) {
        throw new UnsupportedFlavorException(flavor);
    }
    List returnList = this.graphElements;
    this.graphElements = null;
    return returnList;
}
Also used : List(java.util.List) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Example 90 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project RFToolsControl by McJty.

the class GuiProgrammer method handleClipboard.

private boolean handleClipboard(int keyCode) {
    if (Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) || Keyboard.isKeyDown(Keyboard.KEY_RCONTROL)) {
        if (keyCode == Keyboard.KEY_A) {
            selectAll();
        } else if (keyCode == Keyboard.KEY_C) {
            if (!checkSelection()) {
                GuiTools.showMessage(mc, this, getWindowManager(), 50, 50, TextFormatting.RED + "Nothing is selected!");
            } else {
                ProgramCardInstance instance = makeGridInstance(true);
                String json = instance.writeToJson();
                try {
                    StringSelection selection = new StringSelection(json);
                    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                    clipboard.setContents(selection, selection);
                } catch (Exception e) {
                    GuiTools.showMessage(mc, this, getWindowManager(), 50, 50, TextFormatting.RED + "Error copying to clipboard!");
                }
            }
            return true;
        } else if (keyCode == Keyboard.KEY_Z) {
            if (undoProgram != null) {
                ProgramCardInstance curProgram = makeGridInstance(false);
                clearGrid(false);
                loadProgram(undoProgram);
                undoProgram = curProgram;
            }
            return true;
        } else if (keyCode == Keyboard.KEY_X) {
            if (!checkSelection()) {
                GuiTools.showMessage(mc, this, getWindowManager(), 50, 50, TextFormatting.RED + "Nothing is selected!");
            } else {
                ProgramCardInstance instance = makeGridInstance(true);
                String json = instance.writeToJson();
                try {
                    StringSelection selection = new StringSelection(json);
                    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                    clipboard.setContents(selection, selection);
                    undoProgram = makeGridInstance(false);
                    clearGrid(checkSelection());
                } catch (Exception e) {
                    GuiTools.showMessage(mc, this, getWindowManager(), 50, 50, TextFormatting.RED + "Error copying to clipboard!");
                }
            }
            return true;
        } else if (keyCode == Keyboard.KEY_V) {
            try {
                Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                String data = (String) clipboard.getData(DataFlavor.stringFlavor);
                ProgramCardInstance program = ProgramCardInstance.readFromJson(data);
                undoProgram = makeGridInstance(false);
                mergeProgram(program, getSelectedGridHolder());
            } catch (UnsupportedFlavorException e) {
                GuiTools.showMessage(mc, this, getWindowManager(), 50, 50, TextFormatting.RED + "Clipboard does not contain program!");
            } catch (Exception e) {
                GuiTools.showMessage(mc, this, getWindowManager(), 50, 50, TextFormatting.RED + "Error reading from clipboard!");
            }
        }
    }
    return false;
}
Also used : ProgramCardInstance(mcjty.rftoolscontrol.logic.grid.ProgramCardInstance) Clipboard(java.awt.datatransfer.Clipboard) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException) StringSelection(java.awt.datatransfer.StringSelection)

Aggregations

UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)106 IOException (java.io.IOException)92 Transferable (java.awt.datatransfer.Transferable)55 List (java.util.List)32 DataFlavor (java.awt.datatransfer.DataFlavor)26 File (java.io.File)25 Point (java.awt.Point)18 ArrayList (java.util.ArrayList)15 Clipboard (java.awt.datatransfer.Clipboard)14 JTree (javax.swing.JTree)9 TreePath (javax.swing.tree.TreePath)9 JComponent (javax.swing.JComponent)7 TransferHandler (javax.swing.TransferHandler)7 DropTargetContext (java.awt.dnd.DropTargetContext)6 URL (java.net.URL)5 DefaultTableModel (javax.swing.table.DefaultTableModel)5 Image (java.awt.Image)4 ActionEvent (java.awt.event.ActionEvent)4 MouseEvent (java.awt.event.MouseEvent)4 NodeInterface (com.sldeditor.common.NodeInterface)3