summaryrefslogtreecommitdiff
path: root/cmpcj/src/de/schwarzrot/control/dnd/TreeSourceAndDestTransferHandler.java
blob: c61d943e02d45e24c01f86bdea6fa1cc75d4019b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
 * ======================== legal notice ======================
 * 
 * File: TreeSourceAndDestTransferHandler.java Created: 13. June 2012, 04:57
 * Author: <a href="mailto:geronimo013@gmx.de">Geronimo</a> Project: cmpc - a
 * java frontend (client) part of compound media player uses external players to
 * play the media
 * 
 * CMP - compound media player
 * 
 * is a client/server mediaplayer intended to play any media from any
 * workstation without the need to export or mount shares. cmps is an easy to
 * use backend with a (ready to use) HTML-interface. Additionally the backend
 * supports authentication via HTTP-digest authorization. cmpc is a client with
 * vdr-like osd-menues.
 * 
 * Copyright (c) 2012 Reinhard Mantey, some rights reserved! published under
 * Creative Commons by-sa For details see
 * http://creativecommons.org/licenses/by-sa/3.0/
 * 
 * The cmp project's homepage is at
 * http://projects.vdr-developer.org/projects/cmp
 * 
 * --------------------------------------------------------------
 */
package de.schwarzrot.control.dnd;


import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.io.File;
import java.util.List;
import java.util.Map;
import javax.swing.JComponent;
import javax.swing.JTree;
import javax.swing.TransferHandler;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import ca.odell.glazedlists.EventList;
import de.schwarzrot.media.domain.Genre;
import de.schwarzrot.media.domain.Media;
import de.schwarzrot.media.service.AbstractMediaChangeCommand;
import de.schwarzrot.media.service.ChangeMediaCommand;


public class TreeSourceAndDestTransferHandler extends TransferHandler {
    private static final long serialVersionUID = 713L;


    public TreeSourceAndDestTransferHandler(JTree tree, Map<File, DefaultMutableTreeNode> cache,
            EventList<AbstractMediaChangeCommand> changes) {
        this.tree = tree;
        nodeCache = cache;
        this.changes = changes;
    }


    // dest
    @Override
    public boolean canImport(TransferHandler.TransferSupport support) {
        if (!support.isDrop())
            return false;
        support.setShowDropLocation(true);

        if (support.isDataFlavorSupported(TreePathTransferable.TreePathFlavor) && support.getDropAction() == MOVE)
            return true;
        if (support.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
            return true;

        return false;
    }


    // source
    @Override
    public Transferable createTransferable(JComponent c) {
        if (c != tree)
            return null;
        TreePath selectionPath = tree.getSelectionPath();

        return new TreePathTransferable(selectionPath);
    }


    // source
    @Override
    public void exportDone(JComponent c, Transferable data, int action) {
        tree.updateUI();
    }


    // source
    @Override
    public int getSourceActions(JComponent c) {
        return MOVE;
    }


    // dest
    @Override
    public boolean importData(TransferHandler.TransferSupport support) {
        if (!canImport(support))
            return false;
        JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
        TreePath targetPath = tree.getClosestPathForLocation(dl.getDropPoint().x, dl.getDropPoint().y);
        DefaultMutableTreeNode targetNode = (DefaultMutableTreeNode) targetPath.getLastPathComponent();

        if (support.isDataFlavorSupported(TreePathTransferable.TreePathFlavor)) {
            // move a genre in tree
            try {
                TreePath sourcePath = (TreePath) support.getTransferable().getTransferData(
                        TreePathTransferable.TreePathFlavor);
                DefaultMutableTreeNode transferNode = (DefaultMutableTreeNode) sourcePath.getLastPathComponent();
                DefaultMutableTreeNode sourceParentNode = (DefaultMutableTreeNode) transferNode.getParent();
                Genre target = (Genre) targetNode.getUserObject();
                Genre transfer = (Genre) transferNode.getUserObject();

                sourceParentNode.remove(transferNode);
                targetNode.add(transferNode);
                nodeCache.remove(transfer.getRealPath());
                transfer.setParent(target);
                nodeCache.put(transfer.getRealPath(), transferNode);
                transfer.update();
                refreshNodeCache();
                if (!changes.contains(transfer)) {
                    changes.getReadWriteLock().writeLock().lock();
                    changes.add(new ChangeMediaCommand(transfer));
                    changes.getReadWriteLock().writeLock().unlock();
                }
            } catch (Throwable t) {
                t.printStackTrace();
            }
        } else if (support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            // move media files from one genre to another (no visible tree change)
            try {
                @SuppressWarnings("unchecked")
                List<File> fileList = (List<File>) support.getTransferable().getTransferData(
                        DataFlavor.javaFileListFlavor);

                for (File f : fileList)
                    move2TargetNode(targetNode, f); // each media can have different parent, so don't optimize here                
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
        return false;
    }


    protected void cacheNode(DefaultMutableTreeNode node) {
        Genre g = (Genre) node.getUserObject();

        nodeCache.put(g.getRealPath(), node);
        for (int i = 0; i < node.getChildCount(); ++i) {
            DefaultMutableTreeNode sub = (DefaultMutableTreeNode) node.getChildAt(i);

            cacheNode(sub);
        }
    }


    protected void move2TargetNode(DefaultMutableTreeNode targetNode, File mediaPath) {
        if (targetNode == null || mediaPath == null)
            return;
        Genre targetGenre = (Genre) targetNode.getUserObject();
        DefaultMutableTreeNode node = nodeCache.get(mediaPath.getParentFile());

        if (targetGenre != null && node != null) {
            Media transferMedia = null;
            Genre sourceGenre = (Genre) node.getUserObject();

            for (Media m : sourceGenre.getMediaList()) {
                if (m.getRealPath().equals(mediaPath)) {
                    transferMedia = m;
                    break;
                }
            }

            if (transferMedia != null) {
                transferMedia.setParent(targetGenre);
                if (!changes.contains(transferMedia)) {
                    try {
                        changes.getReadWriteLock().writeLock().lock();
                        changes.add(new ChangeMediaCommand(transferMedia));
                        changes.getReadWriteLock().writeLock().unlock();
                    } catch (Throwable t) {
                        t.printStackTrace();
                    }
                }
            } else {
                System.err.println("transfer media not found in genre medialist!");
            }
        } else {
            if (targetGenre == null)
                System.err.println("failed to determine target genre!");
            if (node == null)
                System.err.println("failed to determine source parent node of " + mediaPath.getAbsolutePath());
        }
    }


    protected void refreshNodeCache() {
        nodeCache.clear();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getModel().getRoot();

        cacheNode(node);
    }

    private JTree tree;
    private EventList<AbstractMediaChangeCommand> changes;
    private Map<File, DefaultMutableTreeNode> nodeCache;
}