/* * @(#)CDView.java * * This file is part of webCDwriter - Network CD/DVD Writing. * * Copyright (C) 2001-2005 Jörg P. M. Haeger * * webCDwriter is free software. See CDcreator.java for details. */ import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.dnd.DropTarget; import java.awt.dnd.DropTargetDragEvent; import java.awt.dnd.DropTargetDropEvent; import java.awt.dnd.DropTargetEvent; import java.awt.dnd.DropTargetListener; import java.awt.Point; import java.io.BufferedReader; import java.io.Reader; import javax.swing.JTree; import javax.swing.DefaultListSelectionModel; import javax.swing.TransferHandler; import javax.swing.tree.DefaultTreeSelectionModel; import javax.swing.tree.TreePath; /** * The CD/DVD description view. * * @version 20050930 * @author Jörg P. M. Haeger */ class CDView extends JTreeTable { public CDView(TreeTableModel treeTableModel) { super(treeTableModel); tree.setSelectionModel(new DefaultTreeSelectionModel()); setSelectionModel(new MyListSelectionModel()); DropTarget target = new DropTarget(this, new DropTargetListener() { public void dragEnter(DropTargetDragEvent dtde) { Version.debug("DnD", "dragEnter"); } public void dragOver(DropTargetDragEvent dtde) { // Version.debug("DnD", "dragOver"); } public void dropActionChanged(DropTargetDragEvent dtde) { Version.debug("DnD", "dropActionChanged"); } public void dragExit(DropTargetEvent dte) { Version.debug("DnD", "dragExit"); } public void drop(DropTargetDropEvent dtde) { dtde.acceptDrop(java.awt.dnd.DnDConstants.ACTION_COPY); drop2(dtde); } }); setTransferHandler(new TransferHandler("text")); } public java.awt.Dimension getPreferredSize() { java.awt.Dimension d = super.getPreferredSize(); java.awt.Dimension d2 = getParent().getSize(); if (d.height < d2.height) d.height = d2.height; return d; } private void drop2(DropTargetDropEvent dtde) { if (!CDcreator.DnD) { javax.swing.JOptionPane.showMessageDialog( CDcreator.mainWin, "Feature of the Pro Edition!"); return; } Point p = dtde.getLocation(); int row = tree.getClosestRowForLocation(p.x, p.y); if (row < 0) return; java.awt.Rectangle bounds = tree.getRowBounds(row); if (!bounds.contains(p)) p = new Point(0, 0); TreePath destPath = tree.getClosestPathForLocation(p.x, p.y); Version.debug("DnD", "drop at " + p + ", " + destPath); Transferable transferable = dtde.getTransferable(); DataFlavor[] flavors = transferable.getTransferDataFlavors(); int choice = -1, choiceB = -1; for (int i = 0; i < flavors.length; i++) { DataFlavor flavor = flavors[i]; Version.debug("DnDflavors", i + " " + flavor.getRepresentationClass().getName() + ", " + flavor.getPrimaryType() + ", " + flavor.getSubType()); if (flavor.isFlavorJavaFileListType()) choice = i; if (flavor.getRepresentationClass().getName() .equals("java.io.Reader") && flavor.getPrimaryType().equals("text") && flavor.getSubType().equals("uri-list")) choiceB = i; } if (choice >= 0) try { java.util.List list = (java.util.List) transferable.getTransferData( flavors[choice]); if (destPath != null) tree.setSelectionPath(destPath); java.util.Iterator iter = list.iterator(); while (iter.hasNext()) { java.io.File file = (java.io.File) iter.next(); Version.debug("DnD", "drop " + file); file = new SecFile(file.getPath()); TreePath[] paths = { new TreePath(new DirTreeNode(file)) }; CDcreator.model.transfer(paths); } } catch (Exception e) { Version.debug("DnD", e.getMessage()); } else if (choiceB >= 0) try { Reader in0 = (Reader) transferable.getTransferData( flavors[choiceB]); if (destPath != null) tree.setSelectionPath(destPath); BufferedReader in = new BufferedReader(in0); while (true) { String line = in.readLine(); if (line == null) break; int len = line.length(); if (len > 0 && line.charAt(len - 1) < 32) // cut trailing 0 character line = line.substring(0, len - 1); Version.debug("DnD", "<" + line + ">"); if (line.startsWith("file:")) { java.io.File file = new SecFile(line.substring(5)); TreePath[] paths = { new TreePath(new DirTreeNode(file)) }; CDcreator.model.transfer(paths); } } } catch (Exception e) { Version.debug("DnD", e.getMessage()); } else { if (destPath != null) tree.setSelectionPath(destPath); CDcreator.model.transfer(); } } class MyListSelectionModel extends DefaultListSelectionModel { public MyListSelectionModel() { super(); } public void addSelectionInterval(int index0, int index1) { // System.out.println("addSelectionInterval(" // + index0 + ", " + index1 + ")"); tree.addSelectionInterval(index0, index1); super.addSelectionInterval(index0, index1); } public boolean isSelectedIndex(int index) { // System.out.println("isSelectedIndex(" + index + ")"); return tree.isRowSelected(index); } public void removeIndexInterval(int index0, int index1) { System.out.println("removeIndexInterval(" + index0 + ", " + index1 + ")"); tree.removeSelectionInterval(index0, index1); super.removeIndexInterval(index0, index1); } public void removeSelectionInterval(int index0, int index1) { // System.out.println("removeSelectionInterval(" // + index0 + ", " + index1 + ")"); super.removeSelectionInterval(index0, index1); } public void setLeadSelectionIndex(int index) { // System.out.println("setLeadSelectionIndex(" + index + ")"); super.setLeadSelectionIndex(index); } public void setSelectionInterval(int index0, int index1) { // System.out.println("setSelectionInterval(" // + index0 + ", " + index1 + ")"); tree.setSelectionInterval(index0, index1); super.setSelectionInterval(index0, index1); } } }