1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.aequo.file.action;
14
15 import java.awt.Component;
16 import java.awt.Dimension;
17 import java.io.File;
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.swing.AbstractListModel;
22 import javax.swing.Box;
23 import javax.swing.BoxLayout;
24 import javax.swing.Icon;
25 import javax.swing.JLabel;
26 import javax.swing.JList;
27 import javax.swing.JOptionPane;
28 import javax.swing.JScrollPane;
29 import javax.swing.ListSelectionModel;
30 import javax.swing.border.EmptyBorder;
31
32 import org.abstracthorizon.aequo.action.BaseAction;
33 import org.abstracthorizon.aequo.file.FileCompareEntry;
34 import org.abstracthorizon.aequo.file.FilesModel;
35
36
37
38
39
40
41 public abstract class FileBaseAction extends BaseAction {
42
43
44 protected FilesModel filesModel;
45
46
47 protected FileCompareEntry[] preselectedEntries;
48
49
50
51
52 public FileBaseAction() {
53 }
54
55
56
57
58
59 public void setFilesModel(FilesModel model) {
60 this.filesModel = model;
61 }
62
63
64
65
66
67 public FilesModel getFilesModel() {
68 return filesModel;
69 }
70
71
72
73
74
75 public boolean isOneSelected() {
76 ListSelectionModel selModel = getFilesModel().getSelectionModel();
77 int min = selModel.getMinSelectionIndex();
78 int max = selModel.getMaxSelectionIndex();
79 return ((min >= 0) && (min == max));
80 }
81
82
83
84
85
86 public boolean isSelectionValid() {
87 ListSelectionModel selModel = getFilesModel().getSelectionModel();
88 int min = selModel.getMinSelectionIndex();
89 int max = selModel.getMaxSelectionIndex();
90 return ((min >= 0) && (min <= max));
91 }
92
93
94
95
96
97
98 protected static FileCompareEntry[] collectSelectedEntries(FilesModel model) {
99 ArrayList<FileCompareEntry> entries = new ArrayList<FileCompareEntry>();
100 ListSelectionModel selModel = model.getSelectionModel();
101 int min = selModel.getMinSelectionIndex();
102 int max = selModel.getMaxSelectionIndex();
103 int i = min;
104 int level = 0;
105 while (i <= max) {
106 if (selModel.isSelectedIndex(i)) {
107 FileCompareEntry entry = model.get(i);
108 if (level >= entry.getLevel()) {
109 FileCompareEntry previousEntry = model.get(i - 1);
110 collectRecursively(previousEntry, entries);
111 }
112 entries.add(entry);
113 level = entry.getLevel();
114 }
115 i++;
116 }
117 collectRecursively(entries.get(entries.size() - 1), entries);
118
119 FileCompareEntry[] res = new FileCompareEntry[entries.size()];
120 return entries.toArray(res);
121 }
122
123
124
125
126
127
128 protected static void collectRecursively(FileCompareEntry entry, List<FileCompareEntry> list) {
129 if (entry.hasChildren()) {
130 for (FileCompareEntry e : entry.getChildren()) {
131 list.add(e);
132 collectRecursively(e, list);
133 }
134 }
135 }
136
137
138
139
140
141 protected int showConfirmDialog() {
142 int res = super.showConfirmDialog();
143 if (res != JOptionPane.OK_OPTION) {
144 preselectedEntries = null;
145 }
146 return res;
147 }
148
149
150
151
152
153 public Object updateMessage() {
154 preselectedEntries = collectSelectedEntries(getFilesModel());
155
156 Box box = new Box(BoxLayout.Y_AXIS);
157 Component comp = null;
158 if (message instanceof String) {
159 JLabel l = new JLabel((String)message);
160 l.setBorder(new EmptyBorder(0, 0, 10, 0));
161 l.setAlignmentX(Component.LEFT_ALIGNMENT);
162 comp = l;
163 } else if (message instanceof Icon) {
164 JLabel l = new JLabel((Icon)message);
165 l.setAlignmentX(Component.LEFT_ALIGNMENT);
166 l.setBorder(new EmptyBorder(0, 0, 10, 0));
167 comp = l;
168 } else if (message instanceof Component) {
169 comp = (Component)message;
170 }
171 if (comp != null) {
172 box.add(comp);
173 }
174
175 int c = column;
176 if (c < 0) {
177 c = 0;
178 }
179 final int len = getFilesModel().getData(c).getAbsolutePath().length();
180
181 JList list = new JList(new AbstractListModel() {
182
183 public Object getElementAt(int index) {
184 int c = column;
185 if (c < 0) {
186 c = 0;
187 }
188 File f = preselectedEntries[index].getData(c);
189 String name = f.getAbsolutePath();
190 if (name.length() > len) {
191 name = name.substring(len);
192 } else {
193 name = "/";
194 }
195 return name;
196 }
197
198 public int getSize() {
199 return preselectedEntries.length;
200 }
201
202 });
203 list.setVisibleRowCount(5);
204 JScrollPane sp = new JScrollPane(list);
205 sp.setAlignmentX(Component.LEFT_ALIGNMENT);
206 box.add(sp);
207 Dimension d = box.getPreferredSize();
208 if (d.width > 450) {
209 box.setPreferredSize(new Dimension(450, d.height));
210 }
211
212 return box;
213 }
214
215
216
217
218
219
220
221
222 protected FileCompareEntry[] getPreselectedEntries() {
223 if (preselectedEntries != null) {
224 FileCompareEntry[] res = preselectedEntries;
225 preselectedEntries = null;
226 return res;
227 }
228 return collectSelectedEntries(getFilesModel());
229 }
230 }