• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:TransferHandlerを使ったJListのドラック&ドロップによる並べ替え
#navi(../)
#tags()
RIGHT:Posted by &author(aterai); at 2008-09-29
*TransferHandlerを使ったJListのドラック&ドロップによる並べ替え [#t227e219]
JListのアイテムを複数選択し、ドラック&ドロップで並べ替えを可能にするTransferHandlerを作成します。

-&jnlp;
-&jar;
-&zip;

//#screenshot
#ref(http://lh4.ggpht.com/_9Z4BYR88imo/TQTLeSCyHuI/AAAAAAAAAXo/v2OLiSPdgEY/s800/DnDReorderList.png)

**サンプルコード [#p71d9322]
#code(link){{
class ListItemTransferHandler extends TransferHandler {
  private final DataFlavor localObjectFlavor;
  private Object[] transferedObjects = null;
  public ListItemTransferHandler() {
    localObjectFlavor = new ActivationDataFlavor(
      Object[].class, DataFlavor.javaJVMLocalObjectMimeType, "Array of items");
  }
  @Override protected Transferable createTransferable(JComponent c) {
    JList list = (JList) c;
    indices = list.getSelectedIndices();
    transferedObjects = list.getSelectedValues();
    return new DataHandler(transferedObjects, localObjectFlavor.getMimeType());
  }
  @Override public boolean canImport(TransferSupport info) {
    if (!info.isDrop() || !info.isDataFlavorSupported(localObjectFlavor)) {
      return false;
    }
    return true;
  }
  @Override public int getSourceActions(JComponent c) {
    return TransferHandler.MOVE; //TransferHandler.COPY_OR_MOVE;
  }
  @Override public boolean importData(TransferSupport info) {
    if (!canImport(info)) {
      return false;
    }
    JList target = (JList)info.getComponent();
    JList.DropLocation dl = (JList.DropLocation)info.getDropLocation();
    DefaultListModel listModel = (DefaultListModel)target.getModel();
    int index = dl.getIndex();
    //boolean insert = dl.isInsert();
    int max = listModel.getSize();
    if(index<0 || index>max) {
      index = max;
    }
    addIndex = index;

    try {
      Object[] values = (Object[])info.getTransferable().getTransferData(localObjectFlavor);
      addCount = values.length;
      for(int i=0;i<values.length;i++) {
        int idx = index++;
        listModel.add(idx, values[i]);
        target.addSelectionInterval(idx, idx);
      }
      return true;
    }catch(UnsupportedFlavorException ufe) {
      ufe.printStackTrace();
    }catch(java.io.IOException ioe) {
      ioe.printStackTrace();
    }
    return false;
  }
  @Override protected void exportDone(JComponent c, Transferable data, int action) {
    cleanup(c, action == TransferHandler.MOVE);
  }
  private void cleanup(JComponent c, boolean remove) {
    if(remove && indices != null) {
      JList source = (JList)c;
      DefaultListModel model  = (DefaultListModel)source.getModel();
      if(addCount > 0) {
        for(int i=0;i<indices.length;i++) {
          if(indices[i]>=addIndex) {
            indices[i] += addCount;
          }
        }
      }
      for(int i=indices.length-1;i>=0;i--) {
        model.remove(indices[i]);
      }
    }
    indices  = null;
    addCount = 0;
    addIndex = -1;
  }
  private int[] indices = null;
  private int addIndex  = -1; //Location where items were added
  private int addCount  = 0;  //Number of items added.
}
}}

**解説 [#mebd4560]
上記のサンプルのTransferHandler((JDK 6 で導入されたTransferHandler#canImport メソッドなどを使用しています。))は、主に[http://java.sun.com/docs/books/tutorial/uiswing/examples/dnd/index.html#BasicDnD Drag and Drop and Data Transfer: Examples (The Java™ Tutorials > Creating a GUI with JFC/Swing > Drag and Drop and Data Transfer)]の[http://java.sun.com/docs/books/tutorial/uiswing/examples/dnd/DropDemoProject/src/dnd/ListTransferHandler.java ListTransferHandler.java]を参考にして作成しています。ただし、このListTransferHandler.javaはJList内での並べ替えは想定していない?ようなので、importData(...)メソッドや、cleanup()メソッドをすこし変更しています。
上記のサンプルのTransferHandler((JDK 6 で導入されたTransferHandler#canImport メソッドなどを使用しています。))は、主に[http://docs.oracle.com/javase/tutorial/uiswing/examples/dnd/index.html#BasicDnD Drag and Drop and Data Transfer: Examples (The Java™ Tutorials > Creating a GUI with JFC/Swing > Drag and Drop and Data Transfer)]の[http://docs.oracle.com/javase/tutorial/uiswing/examples/dnd/DropDemoProject/src/dnd/ListTransferHandler.java ListTransferHandler.java]を参考にして作成しています。ただし、このListTransferHandler.javaは項目を複数選択して、JList内での並べ替えは想定していない?(もしくはバグ)ようなので、importData(...)メソッドや、cleanup()メソッドをすこし変更しています。

#code{{
JList list = new JList(listModel);
list.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list.setTransferHandler(new ListItemTransferHandler());
list.setDropMode(DropMode.INSERT);
list.setDragEnabled(true);
}}

- importData
-- 使用されていない?importString(...)の内容をこちらに移動

- cleanup
-- 例えば、項目0,1,2を複数選択して、1と2の間にドロップすると、1,2,2になる
#code{{
for(int i=0;i<indices.length;i++) {
  //if(indices[i]>addIndex) {
  if(indices[i]>=addIndex) {
//...
}}

----
[[JListの項目をドラッグ&ドロップ>Swing/DnDList]]とは異なり、複数アイテムを選択してDrag&Dropによる移動が可能になっています。

**参考リンク [#qf7f31ba]
-[http://java.sun.com/docs/books/tutorial/uiswing/examples/dnd/index.html#BasicDnD Drag and Drop and Data Transfer: Examples (The Java™ Tutorials > Creating a GUI with JFC/Swing > Drag and Drop and Data Transfer)]
--[http://java.sun.com/docs/books/tutorial/uiswing/examples/dnd/DropDemoProject/src/dnd/ListTransferHandler.java ListTransferHandler.java]
-[http://docs.oracle.com/javase/tutorial/uiswing/examples/dnd/index.html#BasicDnD Drag and Drop and Data Transfer: Examples (The Java™ Tutorials > Creating a GUI with JFC/Swing > Drag and Drop and Data Transfer)]
--[http://docs.oracle.com/javase/tutorial/uiswing/examples/dnd/DropDemoProject/src/dnd/ListTransferHandler.java ListTransferHandler.java]
-[[JListの項目をドラッグ&ドロップ>Swing/DnDList]]
-[[JListのアイテムをラバーバンドで複数選択、ドラック&ドロップで並べ替え>Swing/DragSelectDropReordering]]

**コメント [#x063bcab]
- 複数選択して選択されたアイテムのインデックスに移動した場合、複写されるバグ?を修正。 -- [[aterai]] &new{2008-10-10 (金) 21:44:34};

#comment