1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.aequo.text.action;
14
15 import java.awt.event.ActionEvent;
16 import java.util.ArrayList;
17 import java.util.List;
18
19 import javax.swing.ListSelectionModel;
20
21 import org.abstracthorizon.aequo.text.TextCompareEntry;
22 import org.abstracthorizon.aequo.text.TextModel;
23
24
25
26
27
28
29 public class SplitAction extends TextBaseAction {
30
31
32
33
34 public SplitAction() {
35 setDirectionalAction(true);
36 setName("Split");
37 setMessage("Split");
38 setDescription("Split");
39
40
41 }
42
43
44
45
46
47 public boolean isEnabled() {
48 return (column >= 0) && isContinuousSelected();
49 }
50
51
52
53
54
55 public void perform(ActionEvent e) {
56 TextModel model = getTextModel();
57
58 ListSelectionModel selModel = model.getSelectionModel();
59 int min = selModel.getMinSelectionIndex();
60 int max = selModel.getMaxSelectionIndex();
61 selModel.setValueIsAdjusting(true);
62 try {
63 int max2 = max - min + 1 + max;
64
65 int startLineNumber = model.get(min).getLineNumber(destColumn);
66 int endLineNumber = model.get(max).getLineNumber(column);
67
68 List<TextCompareEntry> newEntries = new ArrayList<TextCompareEntry>();
69 for (int i = max; i >= min; i--) {
70 TextCompareEntry entry = model.get(i);
71 String[] data = entry.getData();
72 String[] newData = new String[2];
73 newData[destColumn] = data[destColumn];
74 newData[column] = null;
75 data[destColumn] = null;
76
77 int[] lineNumbers = entry.getLineNumbers();
78 int[] newLineNumbers = new int[2];
79 newLineNumbers[destColumn] = lineNumbers[destColumn];
80 newLineNumbers[column] = endLineNumber;
81 lineNumbers[destColumn] = startLineNumber;
82
83 if (newData[destColumn] == null) {
84 max2 = max2 - 1;
85 } else {
86 TextCompareEntry newEntry = new TextCompareEntry(newData, newLineNumbers);
87 newEntry.updateEntryStatus();
88 newEntries.add(newEntry);
89 }
90 entry.updateEntryStatus();
91 if (data[column] == null) {
92 model.removeImpl(i);
93 max = max - 1;
94 max2 = max2 - 1;
95 }
96 }
97 int i = max + 1;
98 for (TextCompareEntry entry : newEntries) {
99 model.addImpl(i, entry);
100 }
101 model.notifyUpdate(min, max2);
102 selModel.addSelectionInterval(max, max2);
103 } finally {
104 selModel.setValueIsAdjusting(false);
105 }
106 }
107 }