001package jmri.jmrit.throttle.list; 002 003import java.awt.Cursor; 004import java.awt.datatransfer.*; 005import java.awt.dnd.DragSource; 006import java.io.IOException; 007import java.util.ArrayList; 008 009import javax.swing.*; 010 011import jmri.jmrit.roster.RosterEntry; 012import jmri.jmrit.throttle.interfaces.ThrottleControllerUI; 013import jmri.jmrit.throttle.interfaces.ThrottleControllersUIContainer; 014import jmri.util.datatransfer.RosterEntrySelection; 015 016import org.slf4j.Logger; 017import org.slf4j.LoggerFactory; 018 019/** 020 * A class to handle transfers (drag'n drop) within and to the throttle list panel 021 * 022 * <hr> 023 * This file is part of JMRI. 024 * <p> 025 * JMRI is free software; you can redistribute it and/or modify it under the 026 * terms of version 2 of the GNU General Public License as published by the Free 027 * Software Foundation. See the "COPYING" file for a copy of this license. 028 * <p> 029 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY 030 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 031 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 032 * 033 * @author Lionel Jeanson 034 */ 035public class ThrottlesTableTransferHandler extends TransferHandler { 036 037 private JTable table = null; 038 039 public ThrottlesTableTransferHandler(JTable throttleControllers) { 040 table = throttleControllers; 041 } 042 043 @Override 044 protected Transferable createTransferable(JComponent c) { 045 assert (c == table); 046 return new ThrottleUITransferable(((ThrottlesTableModel) table.getModel()).getValueAt(table.getSelectedRow(), table.getSelectedColumn())); 047 } 048 049 @Override 050 public boolean canImport(TransferHandler.TransferSupport info) { 051 boolean b = info.getComponent() == table && info.isDrop() && 052 ( info.isDataFlavorSupported(ThrottleUITransferable.ThrottleControllerUIObjectFlavor) || 053 info.isDataFlavorSupported(RosterEntrySelection.rosterEntryFlavor) ); 054 055 table.setCursor(b ? DragSource.DefaultMoveDrop : DragSource.DefaultMoveNoDrop); 056 return b; 057 } 058 059 @Override 060 public int getSourceActions(JComponent c) { 061 return TransferHandler.COPY_OR_MOVE; 062 } 063 064 @Override 065 public boolean importData(TransferHandler.TransferSupport info) { 066 try { 067 JTable target = (JTable) info.getComponent(); 068 JTable.DropLocation dl = (JTable.DropLocation) info.getDropLocation(); 069 target.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 070 if (info.isDataFlavorSupported(ThrottleUITransferable.ThrottleControllerUIObjectFlavor)) { 071 try { 072 ThrottleControllerUI tcui = (ThrottleControllerUI) info.getTransferable().getTransferData(ThrottleUITransferable.ThrottleControllerUIObjectFlavor); 073 if (tcui != null) { 074 return ((ThrottlesTableModel) table.getModel()).moveThrottleController(tcui, dl.getRow(), dl.getColumn()); 075 } 076 return true; 077 } catch ( UnsupportedFlavorException | IOException e) { 078 log.error("Could not drag'n drop throttle frame.", e); 079 } 080 } 081 if (info.isDataFlavorSupported(RosterEntrySelection.rosterEntryFlavor)) { 082 try { 083 ArrayList<RosterEntry> REs = RosterEntrySelection.getRosterEntries(info.getTransferable()); 084 ThrottleControllersUIContainer tw = ((ThrottlesTableModel) table.getModel()).getThrottleControllersContainerAt(dl.getColumn()); 085 for (RosterEntry re : REs) { 086 ThrottleControllerUI tf = tw.newThrottleController(); 087 tf.toFront(); 088 tf.setRosterEntry(re); 089 } 090 return true; 091 } catch ( 092 UnsupportedFlavorException | 093 IOException e) { 094 log.error("Could not drag'n drop roster entry.", e); 095 } 096 } 097 } catch (ClassCastException e) { 098 log.error("CastException ", e); 099 } 100 return false; 101 } 102 103 @Override 104 protected void exportDone(JComponent c, Transferable t, int act) { 105 table.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 106 } 107 108 private static final Logger log = LoggerFactory.getLogger(ThrottlesTableTransferHandler.class); 109}