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.UnsupportedFlavorException;
19 import java.awt.event.ActionEvent;
20 import java.awt.event.KeyEvent;
21 import java.io.IOException;
22 import java.io.StringReader;
23 import java.util.List;
24
25 import javax.swing.Action;
26 import javax.swing.KeyStroke;
27 import javax.swing.ListSelectionModel;
28
29 import org.abstracthorizon.aequo.gui.ErrorDialog;
30 import org.abstracthorizon.aequo.text.TextCompareEntry;
31 import org.abstracthorizon.aequo.text.TextModel;
32
33
34
35
36
37
38
39
40 public class PasteOverAction extends TextBaseAction {
41
42 protected Clipboard clipboard;
43
44 public PasteOverAction() {
45 setName("Paste Over");
46 putValue(Action.MNEMONIC_KEY, KeyEvent.VK_P);
47 putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
48
49 clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
50 }
51
52
53
54
55
56 public boolean isEnabled() {
57 return (column >= 0)
58 && isContiniousSelection()
59 && clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor) && isContiniousSelection();
60 }
61
62 public void perform(ActionEvent e) {
63 TextModel model = getTextModel();
64
65 ListSelectionModel selModel = model.getSelectionModel();
66 int min = selModel.getMinSelectionIndex();
67 int max = selModel.getMaxSelectionIndex();
68
69 selModel.setValueIsAdjusting(true);
70 try {
71 String text = (String)clipboard.getData(DataFlavor.stringFlavor);
72 System.out.println("Inserting: " + text);
73 List<String> list = TextModel.loadFile(new StringReader(text));
74
75 for (int i = max; i >= min; i--) {
76 TextCompareEntry entry = model.get(i);
77 String d = entry.getData(column);
78 if (d != null) {
79 int j = 0;
80 Object[] data = entry.getData();
81 while ((j < data.length) && ((j == column) || (j != column) && (data[j] == null))) {
82 j++;
83 }
84
85 if (j == data.length) {
86 model.remove(i);
87 max = max - 1;
88 } else {
89 entry.setData(column, null);
90 }
91 }
92 }
93
94 selModel.setValueIsAdjusting(true);
95 try {
96 int i = min;
97 int j = 0;
98 int lineNumbers[] = null;
99 if (i > max) {
100 lineNumbers = model.get(i).getLineNumbers();
101 } else {
102 while ((i <= max) && (j < list.size())) {
103 TextCompareEntry entry = model.get(i);
104 entry.setData(column, list.get(j));
105 lineNumbers = entry.getLineNumbers();
106 i++;
107 j++;
108 }
109 }
110 int[] currentLineNumbers = null;
111 if (j < list.size()) {
112 currentLineNumbers = new int[lineNumbers.length];
113 for (i = 0; i < lineNumbers.length; i++) {
114 currentLineNumbers[i] = lineNumbers[i];
115 }
116 }
117 while (j < list.size()) {
118 String[] data = new String[lineNumbers.length];
119 data[column] = list.get(j);
120
121 int[] newLineNumbers = new int[lineNumbers.length];
122 for (int k = 0; k < lineNumbers.length; k++) {
123 newLineNumbers[k] = currentLineNumbers[k];
124 }
125 currentLineNumbers[column]++;
126
127 TextCompareEntry entry = new TextCompareEntry(data, newLineNumbers);
128 model.addImpl(max + 1, entry);
129 entry.updateEntryStatus();
130 max++;
131 j++;
132 }
133 model.updateLineNumbers(min, -1);
134 model.notifyUpdate(min, max);
135 } finally {
136 selModel.setValueIsAdjusting(false);
137 }
138
139 model.setFileDirty(column);
140 model.updateLineNumbers(min, -1);
141 model.notifyUpdate(min, max);
142 } catch (IOException ex) {
143 ErrorDialog.showError(null, "Error trying to paste data", ex);
144 ex.printStackTrace();
145 } catch (UnsupportedFlavorException ex) {
146 ErrorDialog.showError(null, "Error trying to paste data", ex);
147 } finally {
148 selModel.setValueIsAdjusting(false);
149 }
150
151 }
152
153 protected boolean isContiniousSelection() {
154 boolean isContinious = true;
155 TextModel model = getTextModel();
156
157 ListSelectionModel selModel = model.getSelectionModel();
158 int min = selModel.getMinSelectionIndex();
159 int max = selModel.getMaxSelectionIndex();
160
161 int i = min;
162 while (isContinious && (i <= max)) {
163 isContinious = selModel.isSelectedIndex(i);
164 i++;
165 }
166 return isContinious;
167 }
168
169 }