Search in sources :

Example 1 with UDOLibrary

use of blue.udo.UDOLibrary in project blue by kunstmusik.

the class UDOTreeDropTarget method drop.

@Override
public void drop(DropTargetDropEvent dtde) {
    Point pt = dtde.getLocation();
    DropTargetContext dtc = dtde.getDropTargetContext();
    JTree tree = (JTree) dtc.getComponent();
    TreePath parentpath = tree.getClosestPathForLocation(pt.x, pt.y);
    Object node = parentpath.getLastPathComponent();
    if (dtde.isDataFlavorSupported(TransferableUDO.UDO_CAT_FLAVOR)) {
        if (!(node instanceof UDOCategory)) {
            dtde.rejectDrop();
            return;
        }
        if (dtde.getDropAction() == DnDConstants.ACTION_MOVE) {
            dtde.acceptDrop(dtde.getDropAction());
            Transferable tr = dtde.getTransferable();
            try {
                Object transferNode = tr.getTransferData(TransferableUDO.UDO_CAT_FLAVOR);
                UDOCategory udoCategory = (UDOCategory) transferNode;
                UDOCategory parentNode = (UDOCategory) node;
                UDOLibrary udoLibrary = (UDOLibrary) tree.getModel();
                udoLibrary.addCategory(parentNode, udoCategory);
                dtde.dropComplete(true);
            } catch (UnsupportedFlavorException | IOException e) {
                dtde.dropComplete(false);
            }
        } else {
            dtde.rejectDrop();
        }
    } else if (dtde.isDataFlavorSupported(TransferableUDO.UDO_FLAVOR)) {
        dtde.acceptDrop(dtde.getDropAction());
        try {
            Transferable tr = dtde.getTransferable();
            Object transferNode = tr.getTransferData(TransferableUDO.UDO_FLAVOR);
            UserDefinedOpcode udo = (UserDefinedOpcode) transferNode;
            UDOLibrary udoLibrary = (UDOLibrary) tree.getModel();
            // iLibrary.removeInstrument(instrument);
            if (node instanceof UDOCategory) {
                UDOCategory parentNode = (UDOCategory) node;
                udoLibrary.addUDO(parentNode, udo);
            } else if (node instanceof UserDefinedOpcode) {
                UDOCategory parentNode = (UDOCategory) parentpath.getPathComponent(parentpath.getPathCount() - 2);
                int index = ListUtil.indexOfByRef(parentNode.getUserDefinedOpcodes(), node);
                int closestRow = tree.getClosestRowForLocation(pt.x, pt.y);
                Rectangle bounds = tree.getRowBounds(closestRow);
                if (pt.y > bounds.y + bounds.height) {
                    udoLibrary.addUDO(parentNode, udo);
                } else {
                    udoLibrary.addUDO(parentNode, index, udo);
                }
            }
            dtde.dropComplete(true);
        } catch (UnsupportedFlavorException | IOException e) {
            dtde.dropComplete(false);
        }
    } else {
        dtde.rejectDrop();
    }
}
Also used : DropTargetContext(java.awt.dnd.DropTargetContext) Transferable(java.awt.datatransfer.Transferable) Rectangle(java.awt.Rectangle) Point(java.awt.Point) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) Point(java.awt.Point) JTree(javax.swing.JTree) UDOCategory(blue.udo.UDOCategory) TreePath(javax.swing.tree.TreePath) UDOLibrary(blue.udo.UDOLibrary) UserDefinedOpcode(blue.udo.UserDefinedOpcode)

Example 2 with UDOLibrary

use of blue.udo.UDOLibrary in project blue by kunstmusik.

the class BlueSystem method getUDOLibrary.

public static UDOLibrary getUDOLibrary() {
    if (udoLibrary == null) {
        String userInstrFileName = BlueSystem.getUserConfigurationDirectory() + File.separator + "udoLibrary.xml";
        File f = new File(userInstrFileName);
        if (f.exists()) {
            boolean error = false;
            try {
                Document doc = new Document(f);
                udoLibrary = udoLibrary.loadFromXML(doc.getRoot());
            } catch (ParseException e1) {
                e1.printStackTrace();
                error = true;
            } catch (Exception e) {
                e.printStackTrace();
                error = true;
            }
            if (error) {
                JOptionPane.showMessageDialog(null, "There was an error loading " + f.getAbsolutePath() + "\nPlease fix this file or remove it and restart blue.", "Error", JOptionPane.ERROR_MESSAGE);
                System.exit(0);
            }
        } else {
            udoLibrary = new UDOLibrary();
        }
    }
    return udoLibrary;
}
Also used : UDOLibrary(blue.udo.UDOLibrary) ParseException(electric.xml.ParseException) Document(electric.xml.Document) File(java.io.File) MissingResourceException(java.util.MissingResourceException) IOException(java.io.IOException) ParseException(electric.xml.ParseException)

Aggregations

UDOLibrary (blue.udo.UDOLibrary)2 IOException (java.io.IOException)2 UDOCategory (blue.udo.UDOCategory)1 UserDefinedOpcode (blue.udo.UserDefinedOpcode)1 Document (electric.xml.Document)1 ParseException (electric.xml.ParseException)1 Point (java.awt.Point)1 Rectangle (java.awt.Rectangle)1 Transferable (java.awt.datatransfer.Transferable)1 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 DropTargetContext (java.awt.dnd.DropTargetContext)1 File (java.io.File)1 MissingResourceException (java.util.MissingResourceException)1 JTree (javax.swing.JTree)1 TreePath (javax.swing.tree.TreePath)1