1 package gov.nasa.pds.ltdt.gui.util;
2
3 import gov.nasa.pds.ltdt.gui.MainWindow;
4
5 import java.awt.datatransfer.DataFlavor;
6 import java.awt.datatransfer.StringSelection;
7 import java.awt.datatransfer.Transferable;
8 import java.util.ArrayList;
9 import java.util.List;
10
11 import javax.swing.JComponent;
12 import javax.swing.JList;
13 import javax.swing.JOptionPane;
14 import javax.swing.TransferHandler;
15
16 /***
17 * Handles data reformatting when a keyword got drag and drop from the
18 * Dictionary List panel to Template Editing panel.
19 * @author jwang
20 *
21 */
22 public class ListTransferHandler extends TransferHandler{
23
24 private int[] indices = null;
25 private MainWindow window;
26
27 public ListTransferHandler (MainWindow window) {
28 this.window = window;
29 }
30
31 /***
32 * construct the string to be copied over to the target panel.
33 * The object type will be added with END OBJECT
34 * The element type will be copied over straight
35 * @param component
36 * @return reformatted output string
37 */
38 protected String exportString(JComponent component) {
39 JList list = (JList)component;
40
41 Object[] values = list.getSelectedValues();
42
43 StringBuffer buf = new StringBuffer();
44 StringBuffer message = new StringBuffer();
45
46 for (int i=0; i<values.length; i++) {
47 String val = (String)values[i];
48 buf.append(val == null ? "" : DictionaryUtility.list2editblock(window, val, message));
49
50
51 }
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 window.editorPane.jta.grabFocus();
72
73 return buf.toString();
74
75 }
76
77
78 /***
79 * get a Transferable object
80 */
81 protected Transferable createTransferable(JComponent component) {
82
83
84 return new StringSelection(exportString(component));
85 }
86
87 /***
88 * returns the type of action performed by this transfer
89 */
90 public int getSourceActions(JComponent component) {
91 return COPY;
92 }
93
94 /***
95 * flags true if the transferred data is indeed a string
96 */
97 public boolean canImport(JComponent component, DataFlavor[] flavors) {
98 for (int i = 0; i < flavors.length; i++) {
99 if (DataFlavor.stringFlavor.equals(flavors[i])) {
100 return true;
101 }
102 }
103 return false;
104 }
105
106 }