1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.jguiraffe.examples.tutorial.mainwnd;
17
18 import java.io.File;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import net.sf.jguiraffe.examples.tutorial.model.DirectoryData;
26 import net.sf.jguiraffe.examples.tutorial.model.FileData;
27 import net.sf.jguiraffe.examples.tutorial.viewset.ViewSettings;
28 import net.sf.jguiraffe.gui.app.Application;
29 import net.sf.jguiraffe.gui.builder.action.ActionStore;
30 import net.sf.jguiraffe.gui.builder.components.WidgetHandler;
31 import net.sf.jguiraffe.gui.builder.components.model.TableHandler;
32 import net.sf.jguiraffe.gui.builder.components.model.TreeHandler;
33 import net.sf.jguiraffe.gui.builder.components.model.TreeNodePath;
34
35 import org.apache.commons.configuration.HierarchicalConfiguration;
36 import org.apache.commons.configuration.tree.ConfigurationNode;
37 import org.apache.commons.configuration.tree.DefaultConfigurationNode;
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public class MainWndController
52 {
53
54 static final String ACTGRP_SINGLE_FILE = "SINGLE_FILE";
55
56
57 static final String ACTGRP_SINGLE = "SINGLE_SEL";
58
59
60 static final String ACTGRP_SELECTION = "SELECTION";
61
62
63 private final Application application;
64
65
66 private final TreeHandler tree;
67
68
69 private final TableHandler table;
70
71
72 private final WidgetHandler widgetTable;
73
74
75 private final HierarchicalConfiguration treeModel;
76
77
78 private final Map<File, ConfigurationNode> fileSystems;
79
80
81 private final Map<File, ViewSettings> viewSettings;
82
83
84 private Object iconDirectory;
85
86
87 private Object iconFile;
88
89
90
91
92
93
94
95
96
97 public MainWndController(Application app, TreeHandler treeHandler,
98 TableHandler tabHandler, WidgetHandler widgetTab)
99 {
100 application = app;
101 tree = treeHandler;
102 table = tabHandler;
103 widgetTable = widgetTab;
104 treeModel = tree.getModel();
105 fileSystems = new HashMap<File, ConfigurationNode>();
106 viewSettings = new HashMap<File, ViewSettings>();
107 }
108
109
110
111
112
113
114 public Application getApplication()
115 {
116 return application;
117 }
118
119
120
121
122
123
124 public TreeHandler getTree()
125 {
126 return tree;
127 }
128
129
130
131
132
133
134 public TableHandler getTable()
135 {
136 return table;
137 }
138
139
140
141
142
143
144 public HierarchicalConfiguration getTreeModel()
145 {
146 return treeModel;
147 }
148
149
150
151
152
153
154 public Object getIconDirectory()
155 {
156 return iconDirectory;
157 }
158
159
160
161
162
163
164 public void setIconDirectory(Object iconDirectory)
165 {
166 this.iconDirectory = iconDirectory;
167 }
168
169
170
171
172
173
174 public Object getIconFile()
175 {
176 return iconFile;
177 }
178
179
180
181
182
183
184 public void setIconFile(Object iconFile)
185 {
186 this.iconFile = iconFile;
187 }
188
189
190
191
192
193
194
195
196 public List<File> getSelectedFiles()
197 {
198 int[] indices = getTable().getSelectedIndices();
199 List<File> files = new ArrayList<File>(indices.length);
200 List<Object> model = getTable().getModel();
201
202 for (int idx : indices)
203 {
204 FileData data = (FileData) model.get(idx);
205 files.add(data.getFile());
206 }
207
208 return files;
209 }
210
211
212
213
214
215
216
217 public void selectSubDirectory(File dir)
218 {
219 TreeNodePath currentPath = tree.getSelectedPath();
220 TreeNodePath newPath = currentPath.append(dir.getName());
221 tree.setSelectedPath(newPath);
222 }
223
224
225
226
227
228
229
230
231 void readDirectory(TreeNodePath path)
232 {
233 widgetTable.setVisible(false);
234 application.execute(new ReadDirectoryCommand(this, path));
235 }
236
237
238
239
240
241
242
243
244
245 void fileSystemChanged(File root)
246 {
247 boolean load = false;
248 ConfigurationNode node = fileSystems.get(root);
249
250 if (node == null)
251 {
252
253 node = new DefaultConfigurationNode(root.getPath(),
254 new DirectoryData(root));
255 fileSystems.put(root, node);
256 load = true;
257 }
258
259
260 treeModel.setRootNode(new DefaultConfigurationNode());
261 treeModel.addNodes(null, Collections.singleton(node));
262
263 if (load)
264 {
265
266 readDirectory(new TreeNodePath(node));
267 }
268 }
269
270
271
272
273
274
275
276
277
278 void treeSelectionChanged(TreeNodePath path)
279 {
280 if (path != null)
281 {
282 DirectoryData dirData = (DirectoryData) path.getTargetNode()
283 .getValue();
284 if (!dirData.isInitialized())
285 {
286 readDirectory(path);
287 }
288 else
289 {
290 fillTable(dirData, viewSettings.get(dirData.getDirectory()));
291 }
292 }
293 }
294
295
296
297
298
299
300
301 void fillTable(DirectoryData dirData, ViewSettings settings)
302 {
303 getTable().clearSelection();
304 List<Object> model = getTable().getModel();
305 int size = model.size();
306 if (size > 0)
307 {
308 model.clear();
309 table.rowsDeleted(0, size - 1);
310 }
311
312 model.addAll(dirData.getContent());
313 getTable().rowsInserted(0, dirData.getContent().size() - 1);
314 if (!dirData.getContent().isEmpty())
315 {
316 getTable().setSelectedIndex(0);
317 }
318
319
320 getApplication().getApplicationContext().setTypedProperty(
321 DirectoryData.class, dirData);
322 if (settings != null)
323 {
324
325 getApplication().getApplicationContext().setTypedProperty(
326 ViewSettings.class, settings);
327 viewSettings.put(dirData.getDirectory(), settings);
328
329 applyViewSettings(settings);
330 widgetTable.setVisible(true);
331 }
332
333 tableSelectionChanged();
334 }
335
336
337
338
339
340
341
342
343 void applyViewSettings(ViewSettings settings)
344 {
345 widgetTable.setBackgroundColor(settings.getBackgroundColor());
346 widgetTable.setForegroundColor(settings.getForegroundColor());
347 table.setSelectionBackground(settings.getSelectionBackground());
348 table.setSelectionForeground(settings.getSelectionForeground());
349 }
350
351
352
353
354
355
356 void tableSelectionChanged()
357 {
358 ActionStore as = getApplication().getApplicationContext()
359 .getActionStore();
360 int[] indices = getTable().getSelectedIndices();
361 as.enableGroup(ACTGRP_SELECTION, indices.length > 0);
362 as.enableGroup(ACTGRP_SINGLE, indices.length == 1);
363
364 boolean singleFile = false;
365 if (indices.length == 1)
366 {
367 List<File> files = getSelectedFiles();
368 if (files.get(0).isFile())
369 {
370 singleFile = true;
371 }
372 }
373 as.enableGroup(ACTGRP_SINGLE_FILE, singleFile);
374 }
375
376
377
378
379 void refresh()
380 {
381 TreeNodePath path = tree.getSelectedPath();
382 if (path != null)
383 {
384 DirectoryData dirData = (DirectoryData) path.getTargetNode()
385 .getValue();
386 dirData.setContent(null);
387 treeSelectionChanged(path);
388 }
389 }
390 }