1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.aequo.text.action;
14
15 import java.awt.Toolkit;
16 import java.awt.datatransfer.Clipboard;
17 import java.awt.datatransfer.DataFlavor;
18 import java.awt.datatransfer.Transferable;
19 import java.awt.datatransfer.UnsupportedFlavorException;
20 import java.awt.event.ActionEvent;
21 import java.awt.event.KeyEvent;
22 import java.io.IOException;
23
24 import javax.swing.Action;
25 import javax.swing.KeyStroke;
26 import javax.swing.ListSelectionModel;
27
28 import org.abstracthorizon.aequo.CompareEntry;
29 import org.abstracthorizon.aequo.text.TextModel;
30
31
32
33
34
35
36 public class CopyAction extends TextBaseAction {
37
38
39
40
41 public CopyAction() {
42 setName("Copy");
43 setMessage("Copy");
44 setDescription("Copy");
45 putValue(Action.MNEMONIC_KEY, KeyEvent.VK_C);
46 putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
47 }
48
49
50
51
52
53 public boolean isEnabled() {
54 return (column >= 0) && isSelectionValid();
55 }
56
57
58
59
60
61 public void perform(ActionEvent e) {
62
63 TextModel model = getTextModel();
64
65 StringBuffer text = new StringBuffer();
66 ListSelectionModel selModel = model.getSelectionModel();
67 int min = selModel.getMinSelectionIndex();
68 int max = selModel.getMaxSelectionIndex();
69 for (int i = min; i < max; i++) {
70 CompareEntry<String> entry = model.get(i);
71 text.append(entry.getData(column)).append('\n');
72 }
73
74 final String copyText = text.toString();
75 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
76 clipboard.setContents(new Transferable() {
77
78 public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
79 if (!isDataFlavorSupported(flavor)) {
80 throw new UnsupportedFlavorException(flavor);
81 }
82 return copyText.toString();
83 }
84
85 public DataFlavor[] getTransferDataFlavors() {
86 return new DataFlavor[]{DataFlavor.stringFlavor};
87 }
88
89 public boolean isDataFlavorSupported(DataFlavor flavor) {
90 return DataFlavor.stringFlavor.equals(flavor);
91 }
92
93 }, null);
94 }
95 }