Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Drag and drop for non-String objects

4 views
Skip to first unread message

Hendrik Maryns

unread,
Apr 27, 2011, 11:46:59 AM4/27/11
to
To: comp.lang.java.programmer
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hendrik Maryns schreef:
| Nigel Wade schreef:
| | Hendrik Maryns wrote:
| |
| |> -----BEGIN PGP SIGNED MESSAGE-----
| |> Hash: SHA1
| |>
| |> Hi all,
| |>
| |> IrCOd like to revive a thread I started a while ago but which got no
| |> satisfying answers.
| |>
| |> I want to implement drag and drop for non-String objects.
| |>
| |> Setting: a JList with a DefaultListModel. It contains FormularCOs (a
| |> class of my own making, with a whole hierarchy underneath it:
| |> Conjunction, Negation etc.). I then do
| |>
| |> result.setDragEnabled(true);
| |> result.setTransferHandler(new FormulaTransferHandler());
| |> result.setDropMode(DropMode.INSERT);
| |>
| |> on the JList. Here follows an almost minimal SSCCE, note that it needs
| |> Java 6:
| |>
| |
| | Here you specify that the DataFlavor which drop will accept is of class
| | Formula...
| |
| |
| |> ~ this.localFormulaFlavor = new ActivationDataFlavor(Formula.class,
| |> ~ DataFlavor.javaJVMLocalObjectMimeType, "Arrays of formulas");
| |> ~ }
| |
| |> ~ /**
| |> ~ * The data flavor for formulas local to the JVM.
| |> ~ */
| |> ~ private final DataFlavor localFormulaFlavor;
| |>
| |> ~ /**
| |> ~ * The formulas being transfered.
| |> ~ */
| |> ~ private Object[] transferedFormulas = null;
| |>
| |> ~ @Override
| |> ~ 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?
| |> ~ }
| |
| | ..but what you actually transfer is an Object[]
|
| I see. What solution would you suggest: convert the Object[] to
| Formula[] and update above also, or simply use Object as the local
| flavor above and let the JList find out? I guess there is no need for
| Formula indeed, since I do not invoke any methods on it, so I could as
| well use Object[].

Ok, the latter option now makes the drop being accepted, but nothing
changes, i.e.: everything in the list remains at its place.

The only change with the above is:

~ public FormulaTransferHandler() {
~ this.localFormulaFlavor = new ActivationDataFlavor(Object[].class,
~ DataFlavor.javaJVMLocalObjectMimeType, "Arrays of objects");
~ }

Also, I note that this should actually belong on c.l.j.gui, so f-up to
there.

Thanks already, though!
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.9 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iEYEARECAAYFAkiErq8ACgkQBGFP0CTku6NvkQCeLxauaEPalb2tjpwCqMewPHsw
0xEAoMXrJPy98GjUZwq5bNufuUBlb3/u
=yXs+
-----END PGP SIGNATURE-----

---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Hendrik Maryns

unread,
Apr 27, 2011, 11:47:00 AM4/27/11
to
To: comp.lang.java.gui

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

Hendrik Maryns schreef:
| Hendrik Maryns schreef:
| | Nigel Wade schreef:
| | | Hendrik Maryns wrote:
| | |
| | |> -----BEGIN PGP SIGNED MESSAGE-----
| | |> Hash: SHA1
| | |>
| | |> Hi all,
| | |>

| | |> I d like to revive a thread I started a while ago but which got no


| | |> satisfying answers.
| | |>
| | |> I want to implement drag and drop for non-String objects.
| | |>

| | |> Setting: a JList with a DefaultListModel. It contains Formula s (a

Ok, sorry for this premature post, of course, I had to change the cast
in the importData method as well:

~ final Object[] formulas = (Object[]) info.getTransferable()
~ .getTransferData(this.localObjectFlavor);
~ int index = ((JList.DropLocation)
info.getDropLocation()).getIndex();
~ final DefaultListModel listModel = (DefaultListModel) ((JList) info
~ .getComponent()).getModel();
~ for (final Object element : formulas) {
~ listModel.add(index++, element);
~ }

This works almost perfectly. Almost, since there is little bug: if you
try dropping a formula immediately before itself, it disappears.
Debugging tells me the reason is both formulas end up being selected
after the import, and then both get deleted in exportDone. If you drop
it immediately *after* itself, this does not occur. Is this a Swing
bug, or am I doing something wrong? I do think exportDone is the proper
place to remove the moved objects, but the method I use seems flawed.

Suggestions?

Ah, and a follow-up questions: can somebody explain how to do the TODOs
in the file: export via serialization?

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.9 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iEYEARECAAYFAkiEsxYACgkQBGFP0CTku6OTOQCgqDgomZ0RpRV8x1T45cACr6Bf
TN8AnibXzcPXOQUNhAQV7/8/iqOXjh9e
=RHVO

Nigel Wade

unread,
Apr 27, 2011, 11:47:00 AM4/27/11
to
To: comp.lang.java.gui
Hendrik Maryns wrote:

Not wise. Your drop will accept an array of anything, but you only want to
accept Formula. You should really specify Formula[] in the DataFlavor,
otherwise your canImport() method has to check every entry in the transfer data
to ensure it is a Formula, and that's not good given how often canImport() gets
invoked.

>
> Ok, the latter option now makes the drop being accepted, but nothing
> changes, i.e.: everything in the list remains at its place.

Well, you are working with a single JList. Your exportMethod() which ought to
remove elements for MOVE uses getSelectedIndices() to determine what to get rid
of. Does that make sense after you have modified the list in importData(), you
need to verify that? Also, your loop count is wrong, you delete one too few
elements (i < indices.length - 1) so if you only move 1 item it doesn't get
deleted.


--
Nigel Wade

Hendrik Maryns

unread,
Apr 27, 2011, 11:47:01 AM4/27/11
to
To: comp.lang.java.gui

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

Nigel Wade schreef:

If everythingrCOs fine, the JList can contain only Formulas anyway, but
yourCOre right, better to be on the safe side.

|> Ok, the latter option now makes the drop being accepted, but nothing
|> changes, i.e.: everything in the list remains at its place.
|
| Well, you are working with a single JList.

Indeed. The DnD is only supposed to happen inside the JList into itself.

| Your exportMethod() which ought to
| remove elements for MOVE uses getSelectedIndices() to determine what
to get rid
| of. Does that make sense after you have modified the list in
importData(), you
| need to verify that?

It moves the elements which are selected, even if they are not a
contiguous block, but inserts them as one contiguous block at the drop
index. The inserted elements normally are NOT selected, so it is safe
to afterwards simply delete all selected elements. However, it seems
like if I drop just before (one of) the selected element(s), the
elements being inserted do become selected and thus are deleted as well.
~ This reeks like a bug in DefaultListModel to me.

| Also, your loop count is wrong, you delete one too few
| elements (i < indices.length - 1) so if you only move 1 item it
doesn't get
| deleted.

I noticed as well, fixed.

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.9 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iEYEARECAAYFAkiFxVQACgkQBGFP0CTku6P80gCgoBGa8JAWDxSExR+TQQMN8Uuy
gkQAn0aIDAC14wblqiQ7KYoaLFHF15WV
=3+Qf
-----END PGP SIGNATURE-----

0 new messages