Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Drag and drop w...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 16054 of 16078
Post > Topic >>

Drag and drop with objects other than strings

by Hendrik Maryns <gtw37bn02@[EMAIL PROTECTED] > May 6, 2008 at 04:23 PM

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I have a little Swing app which has an edit field, implemented by a
JList, containing formulas.  The JList has a DefaultListModel which
contains objects of class Formula.  I would like to enable DnD.  I read
through the Swing tutorial on DnD
(http://java.sun.com/docs/books/tutorial/uiswing/dnd/intro.html),
but it
only gives examples using Strings.

Did someone implement drag and drop for JVM objects already?

Below is my go at it, but it doesn’t work in that the cursor shows the
‘forbidden’ icon all the time, i.e. canImport() always returns false.

Help appreciated.

==FormulaTransferHandler.java==
import java.awt.datatransfer.*;
import java.io.IOException;
import javax.activation.DataHandler;
import javax.swing.*;

/**
~ * A transfer handler for formulas. Based on Sun’s Swing DnD tutorial.
~ */
public class FormulaTransferHandler extends TransferHandler {
~  /**
~   * Eclipse generated 16/4/8
~   */
~  private static final long serialVersionUID = 4382734030287436893L;

~  /**
~   * Initialize the formula transfer handler. Sets the supported
~   * flavors. For now these are:
~   * <ul>
~   * <li>new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType +
~   * ";class=de.uni_tuebingen.sfb.lichtenstein.formulas.Formula")</li>
~   * </ul>
~   */
~  public FormulaTransferHandler() {
~    try {
~      this.localFormulaFlavor = new DataFlavor(
~          DataFlavor.javaJVMLocalObjectMimeType + ";class=\""
~              + Formula[].class.getName() + "\"");
~      this.localFormulaFlavor
~          .setHumanPresentableName("Arrays of formulas");
~    } catch (final ClassNotFoundException exc) {
~      exc.printStackTrace();
~    }
~      this.supportedFlavors = new DataFlavor[1];
~      this.supportedFlavors[0] = this.localFormulaFlavor;
~      this.supportedFlavors[1] = new DataFlavor(Formula[].class,
~          "Formulas");
~    }
~  }

~  /**
~   * The data flavor for formulas local to the JVM.
~   */
~  private DataFlavor localFormulaFlavor;
~  /**
~   * The supported flavors of data.
~   */
~  private final DataFlavor[] supportedFlavors;
~  /**
~   * The formulas being transfered.
~   */
~  private Object[] transferedFormulas = null;

~  @[EMAIL PROTECTED]
  protected Transferable createTransferable(final JComponent c) {
~    final JList formulaList = (JList) c;
~    this.transferedFormulas = formulaList.getSelectedValues();
~    return new DataHandler(this.transferedFormulas,
~        this.localFormulaFlavor.getMimeType());
~    // TODO: add support via serialization?
~  }

~  @[EMAIL PROTECTED]
  public boolean canImport(final TransferHandler.TransferSupport info) {
~    // only support drop, I don’t see a use for paste here
~    if (!info.isDrop()) {
~      return false;
~    }
~    if (!info.isDataFlavorSupported(this.localFormulaFlavor)) {
~      // TODO: allow serialization?
~      return false;
~    }
~    final JList.DropLocation dl = (JList.DropLocation) info
~        .getDropLocation();
~    return true;
~  }

~  @[EMAIL PROTECTED]
  public int getSourceActions(final JComponent c) {
~    return TransferHandler.COPY_OR_MOVE;
~  }

~  @[EMAIL PROTECTED]
  public boolean importData(final TransferHandler.TransferSupport info) {
~    if (this.canImport(info)) {
~      try {
~        final Formula[] formulas = (Formula[]) info.getTransferable()
~            .getTransferData(this.localFormulaFlavor);
~        int index = ((JList.DropLocation) info.getDropLocation())
~            .getIndex();
~        final DefaultListModel listModel = (DefaultListModel) ((JList)
info
~            .getComponent()).getModel();
~        for (final Formula element : formulas) {
~          listModel.add(index++, element);
~        }
~        return true;
~      } catch (final UnsupportedFlavorException ufe) {
~        ufe.printStackTrace();
~      } catch (final IOException ioe) {
~        ioe.printStackTrace();
~      }
~    }
~    return false;
~  }

~  @[EMAIL PROTECTED]
  protected void exportDone(final JComponent c,
~      final Transferable data, final int action) {
~    if (action == TransferHandler.MOVE
~        && this.transferedFormulas != null) {
~      final DefaultListModel model = (DefaultListModel) ((JList) c)
~          .getModel();
~      for (int i = this.transferedFormulas.length - 1; i >= 0; i--) {
~        model.removeElement(this.transferedFormulas[i]);
~      }
~    }
~  }
}
== ==

Formula contains a lot more, but to try the example,

public interface Formula {}

will suffice.

I know there are other problems with this, but for now I’d like to get
it ‘basically’ to work.

It is difficult to debug, since the Eclipse’s debugging program requires
me to use the mouse, so stuf gets strange.

All I can find out is that
info.isDataFlavorSupported(this.localFormulaFlavor) returns false, since
localFormulaFlavor is
java.awt.datatransfer.DataFlavor[mimetype=application/x-java-jvm-local-objectref;representationclass=[Lde.uni_tuebingen.sfb.lichtenstein.formulas.Formula;]
whereas the supported flavors of info are
javax.activation.ActivationDataFlavor[mimetype=application/x-java-jvm-local-objectref;representationclass=[Lde.uni_tuebingen.sfb.lichtenstein.formulas.Formula;]

But why this is so is unclear to me.

TIA, H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFIIGnne+7xMGD3itQRAuEkAJ4+VepJYpHaF8/5IF3whSi2LyKqjwCdEy1u
osfxXU/ej92tBGprQAAKO1o=
=A/7w
-----END PGP SIGNATURE-----




 1 Posts in Topic:
Drag and drop with objects other than strings
Hendrik Maryns <gtw37b  2008-05-06 16:23:35 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed May 14 5:57:26 CDT 2008.