Search in sources :

Example 1 with InstrumentLibrary

use of blue.InstrumentLibrary in project blue by kunstmusik.

the class InstrumentTreeDropTarget 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(TransferableInstrument.INSTR_CAT_FLAVOR)) {
        if (!(node instanceof InstrumentCategory)) {
            dtde.rejectDrop();
            return;
        }
        if (dtde.getDropAction() == DnDConstants.ACTION_MOVE) {
            dtde.acceptDrop(dtde.getDropAction());
            Transferable tr = dtde.getTransferable();
            try {
                Object transferNode = tr.getTransferData(TransferableInstrument.INSTR_CAT_FLAVOR);
                InstrumentLibrary iLibrary = (InstrumentLibrary) tree.getModel();
                InstrumentCategory parentNode = (InstrumentCategory) node;
                InstrumentCategory instrumentCategory = (InstrumentCategory) transferNode;
                // iLibrary.removeCategory(instrumentCategory);
                iLibrary.addCategory(parentNode, instrumentCategory);
                dtde.dropComplete(true);
            } catch (UnsupportedFlavorException | IOException e) {
                dtde.dropComplete(false);
            }
        } else {
            dtde.rejectDrop();
        }
    } else if (dtde.isDataFlavorSupported(TransferableInstrument.INSTR_FLAVOR)) {
        dtde.acceptDrop(dtde.getDropAction());
        try {
            Transferable tr = dtde.getTransferable();
            Object transferNode = tr.getTransferData(TransferableInstrument.INSTR_FLAVOR);
            Instrument instrument = (Instrument) transferNode;
            InstrumentLibrary iLibrary = (InstrumentLibrary) tree.getModel();
            if (instrument instanceof BlueSynthBuilder) {
                ((BlueSynthBuilder) instrument).clearParameters();
            }
            // iLibrary.removeInstrument(instrument);
            if (node instanceof InstrumentCategory) {
                InstrumentCategory parentNode = (InstrumentCategory) node;
                iLibrary.addInstrument(parentNode, instrument);
            } else if (node instanceof Instrument) {
                InstrumentCategory parentNode = (InstrumentCategory) parentpath.getPathComponent(parentpath.getPathCount() - 2);
                int index = ListUtil.indexOfByRef(parentNode.getInstruments(), node);
                int closestRow = tree.getClosestRowForLocation(pt.x, pt.y);
                Rectangle bounds = tree.getRowBounds(closestRow);
                if (pt.y > bounds.y + bounds.height) {
                    iLibrary.addInstrument(parentNode, instrument);
                } else {
                    iLibrary.addInstrument(parentNode, index, instrument);
                }
            }
            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) BlueSynthBuilder(blue.orchestra.BlueSynthBuilder) Point(java.awt.Point) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) Point(java.awt.Point) JTree(javax.swing.JTree) TreePath(javax.swing.tree.TreePath) InstrumentLibrary(blue.InstrumentLibrary) Instrument(blue.orchestra.Instrument) InstrumentCategory(blue.orchestra.InstrumentCategory)

Example 2 with InstrumentLibrary

use of blue.InstrumentLibrary in project blue by kunstmusik.

the class InstrumentImportPane method importInstrument.

private void importInstrument() {
    InstrumentOption iOption = iTableModel.getInstrumentOption(instrumentTable.getSelectedRow());
    if (iOption == null) {
        return;
    }
    try {
        Instrument instrument = BlueShareRemoteCaller.getInstrument(iOption);
        if (instrument == null) {
            String error = BlueSystem.getString("blueShare.importError");
            JOptionPane.showMessageDialog(null, error, BlueSystem.getString("message.error"), JOptionPane.ERROR_MESSAGE);
            return;
        }
        // data.getOrchestra().addInstrument(instrument);
        // instrumentTreeModel.reload();
        InstrumentLibrary instrLib = BlueSystem.getUserInstrumentLibrary();
        instrLib.importInstrument(instrument);
        String message = BlueSystem.getString("blueShare.importSuccess");
        JOptionPane.showMessageDialog(null, message, BlueSystem.getString("common.success"), JOptionPane.PLAIN_MESSAGE);
    } catch (ParseException pe) {
        String error = BlueSystem.getString("blueShare.selectServer.couldNotReadResponse");
        JOptionPane.showMessageDialog(null, error, BlueSystem.getString("message.error"), JOptionPane.ERROR_MESSAGE);
        return;
    } catch (XmlRpcException xre) {
        String error = "Error: " + xre.getLocalizedMessage();
        JOptionPane.showMessageDialog(null, error, BlueSystem.getString("message.error"), JOptionPane.ERROR_MESSAGE);
        return;
    } catch (IOException ioe) {
        String error = "Error: " + ioe.getLocalizedMessage();
        JOptionPane.showMessageDialog(null, error, BlueSystem.getString("message.error"), JOptionPane.ERROR_MESSAGE);
        return;
    }
}
Also used : InstrumentLibrary(blue.InstrumentLibrary) Instrument(blue.orchestra.Instrument) ParseException(electric.xml.ParseException) IOException(java.io.IOException) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Aggregations

InstrumentLibrary (blue.InstrumentLibrary)2 Instrument (blue.orchestra.Instrument)2 IOException (java.io.IOException)2 BlueSynthBuilder (blue.orchestra.BlueSynthBuilder)1 InstrumentCategory (blue.orchestra.InstrumentCategory)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 JTree (javax.swing.JTree)1 TreePath (javax.swing.tree.TreePath)1 XmlRpcException (org.apache.xmlrpc.XmlRpcException)1