PageRenderTime 102ms CodeModel.GetById 61ms app.highlight 7ms RepoModel.GetById 33ms app.codeStats 0ms

/src/mpv5/ui/misc/DragTableHandlerForDBOs.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 97 lines | 69 code | 13 blank | 15 comment | 11 complexity | f70973d4aca8ec481f7edf20005e7829 MD5 | raw file
 1package mpv5.ui.misc;
 2
 3import java.awt.datatransfer.DataFlavor;
 4import java.awt.datatransfer.Transferable;
 5import java.awt.dnd.DnDConstants;
 6import java.util.ArrayList;
 7import java.util.List;
 8import java.util.logging.Level;
 9import java.util.logging.Logger;
10import javax.swing.JComponent;
11import javax.swing.JTable;
12import javax.swing.TransferHandler;
13import mpv5.db.common.Context;
14import mpv5.db.common.DatabaseObject;
15import mpv5.db.common.NodataFoundException;
16
17/**
18 *
19 */
20public class DragTableHandlerForDBOs extends TransferHandler {
21
22    private final JTable table;
23    private final Context context;
24
25    public DragTableHandlerForDBOs(JTable table, Context context) {
26        this.table = table;
27        this.table.setTransferHandler(this);
28        this.table.setDragEnabled(true);
29        this.context = context;
30    }
31
32    @Override
33    public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
34        if (table == comp) {
35            return false;
36        }
37        return true;
38    }
39
40    @Override
41    protected Transferable createTransferable(JComponent c) {
42        if (c == table) {
43            int[] row = table.getSelectedRows();
44
45            List<DatabaseObject> l = new ArrayList<DatabaseObject>();
46            if (table.getModel().getValueAt(row[0], 0) instanceof DatabaseObject) {
47                for (int i = 0; i < row.length; i++) {
48                    int ro = row[i];
49                    l.add((DatabaseObject) table.getModel().getValueAt(ro, 0));
50
51                }
52
53                return new DatabaseObjectTransferable(l);
54            } else {
55                try {
56                    for (int i = 0; i < row.length; i++) {
57                        int ro = row[i];
58                        l.add(DatabaseObject.getObject(context, Integer.valueOf(table.getModel().getValueAt(ro, 0).toString())));
59                    }
60            
61                    return new DatabaseObjectTransferable(l);
62                } catch (NodataFoundException ex) {
63                    l.add(DatabaseObject.getObject(context));
64          
65                    return new DatabaseObjectTransferable(l);
66                }
67            }
68        }
69        return null;
70    }
71
72    @Override
73    public boolean importData(JComponent comp, Transferable t) {
74//        if (comp == table) {
75//            try {
76//                DatabaseObject value = t.getTransferData(DatabaseObjectTransferable.FLAVOR);
77//
78//                int row = table.getSelectedRow();
79//                int col = table.getSelectedColumn();
80//                table.getModel().setValueAt(value, row, col);
81//                return true;
82//            } catch (Exception e) {
83//            }
84//        }
85//        return super.importData(comp, t);
86        return false;
87    }
88
89    @Override
90    public int getSourceActions(JComponent c) {
91        if (table == c) {
92            return DnDConstants.ACTION_COPY_OR_MOVE;
93        } else {
94            return super.getSourceActions(c);
95        }
96    }
97}