Search in sources :

Example 1 with Schema

use of gov.sandia.n2a.db.Schema in project n2a by frothga.

the class SafeTextTransferHandler method importData.

public boolean importData(TransferSupport support) {
    try {
        String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
        if (data.startsWith("N2A.schema")) {
            if (safeTypes == null)
                return false;
            Schema schema = new Schema();
            MNode nodes = new MVolatile();
            BufferedReader br = new BufferedReader(new StringReader(data));
            schema.read(br);
            if (schema.type.startsWith("Clip"))
                schema.type = schema.type.substring(4);
            if (!safeTypes.contains(schema.type)) {
                br.close();
                return false;
            }
            nodes.read(br);
            br.close();
            // Process into a suitable string
            for (MNode n : nodes) {
                String key = n.key();
                String value = n.get();
                if (key.startsWith("@"))
                    data = value + key;
                else if (value.isEmpty())
                    data = key;
                else
                    data = key + "=" + value;
                // Only process the first node
                break;
            }
        }
        JTextComponent comp = (JTextComponent) support.getComponent();
        InputContext ic = comp.getInputContext();
        if (ic != null)
            ic.endComposition();
        comp.replaceSelection(data);
        return true;
    } catch (UnsupportedFlavorException | IOException e) {
    }
    return false;
}
Also used : Schema(gov.sandia.n2a.db.Schema) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) InputContext(java.awt.im.InputContext) JTextComponent(javax.swing.text.JTextComponent) IOException(java.io.IOException) MNode(gov.sandia.n2a.db.MNode) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) MVolatile(gov.sandia.n2a.db.MVolatile)

Aggregations

MNode (gov.sandia.n2a.db.MNode)1 MVolatile (gov.sandia.n2a.db.MVolatile)1 Schema (gov.sandia.n2a.db.Schema)1 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 InputContext (java.awt.im.InputContext)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 JTextComponent (javax.swing.text.JTextComponent)1