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 CutAction extends TextBaseAction {
37
38
39 protected DeleteAction deleteAction;
40
41
42
43
44 public CutAction() {
45 setName("Cut");
46 setMessage("Cut");
47 setDescription("Cut");
48 putValue(Action.MNEMONIC_KEY, KeyEvent.VK_C);
49 putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
50
51 deleteAction = new DeleteAction();
52 }
53
54
55
56
57
58 public boolean isEnabled() {
59 return (column >= 0) && isSelectionValid();
60 }
61
62
63
64
65
66 public void setTextModel(TextModel textModel) {
67 super.setTextModel(textModel);
68 deleteAction.setTextModel(textModel);
69 }
70
71
72
73
74
75 public void perform(ActionEvent e) {
76
77 TextModel model = getTextModel();
78
79 StringBuffer text = new StringBuffer();
80 ListSelectionModel selModel = model.getSelectionModel();
81 int min = selModel.getMinSelectionIndex();
82 int max = selModel.getMaxSelectionIndex();
83 for (int i = min; i < max; i++) {
84 CompareEntry<String> entry = model.get(i);
85 text.append(entry.getData(column)).append('\n');
86 }
87
88 final String copyText = text.toString();
89
90 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
91 clipboard.setContents(new Transferable() {
92
93 public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
94 if (!isDataFlavorSupported(flavor)) {
95 throw new UnsupportedFlavorException(flavor);
96 }
97 return copyText.toString();
98 }
99
100 public DataFlavor[] getTransferDataFlavors() {
101 return new DataFlavor[]{DataFlavor.stringFlavor};
102 }
103
104 public boolean isDataFlavorSupported(DataFlavor flavor) {
105 return DataFlavor.stringFlavor.equals(flavor);
106 }
107
108 }, null);
109 deleteAction.actionPerformed(e);
110 }
111 }