Esempio n. 1
0
 @Override
 public boolean importData(TransferHandler.TransferSupport info) {
   JTable target = (JTable) info.getComponent();
   JTable.DropLocation dl = (JTable.DropLocation) info.getDropLocation();
   int rowTo = dl.getRow();
   int max = table.getModel().getRowCount();
   if ((rowTo < 0) || (rowTo > max)) {
     rowTo = max;
   }
   target.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
   try {
     Integer rowFrom = (Integer) info.getTransferable().getTransferData(localObjectFlavor);
     if (rowTo > rowFrom) {
       rowTo--;
     }
     if ((rowFrom != -1) && (rowFrom != rowTo)) {
       StateTableModel stateTableModel = (StateTableModel) table.getModel();
       stateTableModel.reorder(rowFrom, rowTo);
       target.getSelectionModel().addSelectionInterval(rowTo, rowTo);
       return true;
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
   return false;
 }
  /**
   * Imports data flavors and returns a true or false if the flavor is flavored or not
   *
   * @return Boolean
   */
  public boolean canImport(TransferHandler.TransferSupport info) {
    // we only import Strings
    if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) {
      return false;
    }

    JList.DropLocation dl = (JList.DropLocation) info.getDropLocation();
    if (dl.getIndex() == -1) {
      return false;
    }
    return true;
  }
  /**
   * Imports data if the specific flavor is supported
   *
   * @return Boolean
   */
  public boolean importData(TransferHandler.TransferSupport info) {
    if (!info.isDrop()) {
      return false;
    }

    // Check for String flavor
    if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) {
      System.out.println("Data flavor not supported.");
      return false;
    }

    JList.DropLocation dl = (JList.DropLocation) info.getDropLocation();
    DefaultListModel listModel = (DefaultListModel) list.getModel();
    int index = dl.getIndex();
    boolean insert = dl.isInsert();
    // Get the current string under the drop.
    // String value = (String)listModel.getElementAt(index);

    // Get the string that is being dropped.
    Transferable t = info.getTransferable();
    String data;
    try {
      data = (String) t.getTransferData(DataFlavor.stringFlavor);
    } catch (Exception e) {
      return false;
    }

    /**
     * This is commented out for the basicdemo.html tutorial page. * If you add this code snippet
     * back and delete the * "return false;" line, the list will accept drops * of type string.
     */
    // Perform the actual import.
    if (insert) {
      listModel.add(index, data);
    } else {
      listModel.set(index, data);
    }
    return true;
  }