svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / fmap / dal / serverexplorer / filesystem / swing / FilesystemExplorerWizardPanel.java @ 42775
History | View | Annotate | Download (27.1 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.serverexplorer.filesystem.swing; |
25 |
|
26 |
import java.awt.Component; |
27 |
import java.awt.GridBagConstraints; |
28 |
import java.awt.GridBagLayout; |
29 |
import java.awt.Insets; |
30 |
import java.awt.event.ActionEvent; |
31 |
import java.awt.event.ActionListener; |
32 |
import java.io.File; |
33 |
import java.util.ArrayList; |
34 |
import java.util.Iterator; |
35 |
import java.util.List; |
36 |
import java.util.prefs.Preferences; |
37 |
|
38 |
import javax.swing.AbstractListModel; |
39 |
import javax.swing.JButton; |
40 |
import javax.swing.JList; |
41 |
import javax.swing.JPanel; |
42 |
import javax.swing.JScrollPane; |
43 |
import javax.swing.ListModel; |
44 |
import javax.swing.ScrollPaneConstants; |
45 |
import javax.swing.SwingUtilities; |
46 |
import javax.swing.event.AncestorEvent; |
47 |
import javax.swing.event.AncestorListener; |
48 |
import javax.swing.event.ListSelectionEvent; |
49 |
import javax.swing.event.ListSelectionListener; |
50 |
import javax.swing.filechooser.FileFilter; |
51 |
|
52 |
import org.gvsig.andami.PluginServices; |
53 |
import org.gvsig.andami.messages.Messages; |
54 |
import org.gvsig.andami.messages.NotificationManager; |
55 |
import org.gvsig.app.ApplicationLocator; |
56 |
import org.gvsig.app.ApplicationManager; |
57 |
import org.gvsig.app.gui.WizardPanel; |
58 |
import org.gvsig.app.prepareAction.PrepareContext; |
59 |
import org.gvsig.fmap.dal.DALLocator; |
60 |
import org.gvsig.fmap.dal.DataManager; |
61 |
import org.gvsig.fmap.dal.DataStoreParameters; |
62 |
import org.gvsig.fmap.dal.exception.DataException; |
63 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemFileFilter; |
64 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
65 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
66 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
67 |
import org.gvsig.gui.beans.swing.JFileChooser; |
68 |
import org.gvsig.tools.ToolsLocator; |
69 |
import org.gvsig.tools.dispose.Disposable; |
70 |
import org.gvsig.tools.dynobject.DynObject; |
71 |
import org.gvsig.tools.i18n.I18nManager; |
72 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
73 |
import org.slf4j.Logger; |
74 |
import org.slf4j.LoggerFactory; |
75 |
|
76 |
/**
|
77 |
* @author jmvivo
|
78 |
*
|
79 |
*/
|
80 |
public abstract class FilesystemExplorerWizardPanel extends WizardPanel |
81 |
implements ActionListener, ListSelectionListener, Disposable { |
82 |
|
83 |
/**
|
84 |
*
|
85 |
*/
|
86 |
private static final long serialVersionUID = -3371957786521876903L; |
87 |
|
88 |
private static final Logger LOG = LoggerFactory |
89 |
.getLogger(FilesystemExplorerWizardPanel.class); |
90 |
|
91 |
public static final String OPEN_LAYER_FILE_CHOOSER_ID = |
92 |
"OPEN_LAYER_FILE_CHOOSER_ID";
|
93 |
|
94 |
protected static final String ADD_COMMAND = "ADD"; |
95 |
protected static final String EDIT_COMMAND = "PROPERTIES"; |
96 |
protected static final String REMOVE_COMMAND = "REMOVE"; |
97 |
protected static final String UP_COMMAND = "UP"; |
98 |
protected static final String DOWN_COMMAND = "DOWN"; |
99 |
|
100 |
private static String lastPath = null; |
101 |
private static MyFileFilter lastFilter = null; |
102 |
private static final String DEFAULT_FILTER = "All_supported"; |
103 |
|
104 |
private JList fileList; |
105 |
private JScrollPane fileListScroll; |
106 |
private JPanel buttonsPanel; |
107 |
private JButton addButton; |
108 |
private JButton propertiesButton; |
109 |
private JButton removeButton; |
110 |
private JButton upButton; |
111 |
private JButton downButton; |
112 |
|
113 |
protected FilesystemServerExplorer explorer;
|
114 |
private ArrayList<MyFileFilter> filters; |
115 |
|
116 |
public FilesystemExplorerWizardPanel() {
|
117 |
super();
|
118 |
this.addAncestorListener(new AncestorListener() { |
119 |
public void ancestorAdded(AncestorEvent ae) { |
120 |
initExplorer(); |
121 |
initFilters(); |
122 |
} |
123 |
public void ancestorRemoved(AncestorEvent ae) { |
124 |
dispose(); |
125 |
} |
126 |
public void ancestorMoved(AncestorEvent ae) { |
127 |
} |
128 |
}); |
129 |
} |
130 |
|
131 |
public void setTabName(String name) { |
132 |
super.setTabName(name);
|
133 |
} |
134 |
|
135 |
public DataStoreParameters[] getParameters() { |
136 |
return ((FilesystemStoreListModel) getFileList().getModel())
|
137 |
.getParameters(); |
138 |
} |
139 |
|
140 |
public void initWizard() { |
141 |
setTabName(PluginServices.getText(this, "Fichero")); |
142 |
if (lastPath == null) { |
143 |
Preferences prefs = Preferences.userRoot().node("gvsig.foldering"); |
144 |
lastPath = prefs.get("DataFolder", null); |
145 |
} |
146 |
initExplorer(); |
147 |
initFilters(); |
148 |
initUI(); |
149 |
} |
150 |
|
151 |
private void initExplorer() { |
152 |
if (this.explorer == null) { |
153 |
DataManager dm = DALLocator.getDataManager(); |
154 |
FilesystemServerExplorerParameters param; |
155 |
try {
|
156 |
param |
157 |
= (FilesystemServerExplorerParameters) dm |
158 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
159 |
param.setInitialpath(lastPath); |
160 |
explorer |
161 |
= (FilesystemServerExplorer) dm.openServerExplorer( |
162 |
FilesystemServerExplorer.NAME, param); |
163 |
} catch (Exception e) { |
164 |
throw new RuntimeException(e); |
165 |
} |
166 |
} |
167 |
} |
168 |
|
169 |
private void initFilters() { |
170 |
if (this.filters == null) { |
171 |
int mode = FilesystemServerExplorer.MODE_ALL;
|
172 |
this.filters = new ArrayList<MyFileFilter>(); |
173 |
if (this.getMapCtrl() == null) { |
174 |
mode = FilesystemServerExplorer.MODE_FEATURE; |
175 |
} else {
|
176 |
mode |
177 |
= FilesystemServerExplorer.MODE_GEOMETRY |
178 |
| FilesystemServerExplorer.MODE_RASTER; |
179 |
} |
180 |
|
181 |
// First filter in list will be 'All formats' filter
|
182 |
this.filters.add(new MyFileFilter(explorer.getFilter(mode, |
183 |
Messages.get(DEFAULT_FILTER)))); |
184 |
|
185 |
@SuppressWarnings("unchecked") |
186 |
Iterator<FilesystemFileFilter> iter = explorer.getFilters(mode);
|
187 |
while (iter.hasNext()) {
|
188 |
this.filters.add(new MyFileFilter(iter.next())); |
189 |
} |
190 |
} |
191 |
} |
192 |
|
193 |
private void initUI() { |
194 |
this.setLayout(new GridBagLayout()); |
195 |
GridBagConstraints constr = new GridBagConstraints(); |
196 |
|
197 |
constr.gridwidth = GridBagConstraints.RELATIVE;
|
198 |
constr.gridheight = GridBagConstraints.RELATIVE;
|
199 |
constr.fill = GridBagConstraints.BOTH;
|
200 |
constr.anchor = GridBagConstraints.FIRST_LINE_START;
|
201 |
constr.weightx = 1;
|
202 |
constr.weighty = 1;
|
203 |
constr.ipadx = 3;
|
204 |
constr.ipady = 3;
|
205 |
|
206 |
this.add(getFileListScroll(), constr);
|
207 |
|
208 |
constr.gridwidth = GridBagConstraints.REMAINDER;
|
209 |
constr.gridheight = GridBagConstraints.RELATIVE;
|
210 |
constr.fill = GridBagConstraints.NONE;
|
211 |
constr.anchor = GridBagConstraints.FIRST_LINE_END;
|
212 |
constr.weightx = 0;
|
213 |
constr.weighty = 0;
|
214 |
this.add(getButtonsPanel(), constr);
|
215 |
|
216 |
this.updateButtons();
|
217 |
this.updateContainingWizardAcceptButton();
|
218 |
|
219 |
} |
220 |
|
221 |
protected class FilesystemStoreListModel extends AbstractListModel { |
222 |
|
223 |
private static final long serialVersionUID = -726119349962990665L; |
224 |
private ArrayList<FilesystemStoreParameters> theList; |
225 |
|
226 |
public FilesystemStoreListModel() {
|
227 |
theList = new ArrayList<FilesystemStoreParameters>(); |
228 |
} |
229 |
|
230 |
public DataStoreParameters[] getParameters() { |
231 |
return theList.toArray(new DataStoreParameters[0]); |
232 |
} |
233 |
|
234 |
public Object getElementAt(int index) { |
235 |
return theList.get(index).getFile().getName();
|
236 |
} |
237 |
|
238 |
public FilesystemStoreParameters getStoreParameterAt(int index) { |
239 |
return theList.get(index);
|
240 |
} |
241 |
|
242 |
public int getSize() { |
243 |
return theList.size();
|
244 |
} |
245 |
|
246 |
public DynObject getDynObjectAt(int index) { |
247 |
return (DynObject) theList.get(index);
|
248 |
} |
249 |
|
250 |
public void add(DynObject dynObject) { |
251 |
this.theList.add((FilesystemStoreParameters) dynObject);
|
252 |
this.fireIntervalAdded(this, this.theList.size() - 1, |
253 |
this.theList.size() - 1); |
254 |
} |
255 |
|
256 |
public void addAll(List<FilesystemStoreParameters> toAdd) { |
257 |
int index0 = this.getSize() - 1; |
258 |
if (index0 < 0) { |
259 |
index0 = 0;
|
260 |
} |
261 |
this.theList.addAll(toAdd);
|
262 |
this.fireIntervalAdded(this, index0, this.theList.size() - 1); |
263 |
} |
264 |
|
265 |
public void remove(int i) { |
266 |
this.theList.remove(i);
|
267 |
this.fireIntervalRemoved(this, i, i); |
268 |
|
269 |
} |
270 |
|
271 |
public void up(FilesystemStoreParameters item) { |
272 |
int curIndex = this.theList.indexOf(item); |
273 |
if (curIndex < 1) { |
274 |
return;
|
275 |
} |
276 |
this.theList.remove(item);
|
277 |
this.theList.add(curIndex - 1, item); |
278 |
this.fireContentsChanged(this, curIndex, curIndex - 1); |
279 |
} |
280 |
|
281 |
public void down(FilesystemStoreParameters item) { |
282 |
int curIndex = this.theList.indexOf(item); |
283 |
if (curIndex < 0) { |
284 |
return;
|
285 |
} else
|
286 |
if (curIndex == this.theList.size() - 1) { |
287 |
return;
|
288 |
} |
289 |
this.theList.remove(item);
|
290 |
this.theList.add(curIndex + 1, item); |
291 |
this.fireContentsChanged(this, curIndex, curIndex + 1); |
292 |
} |
293 |
|
294 |
public void forceUpdate(int index) { |
295 |
this.fireContentsChanged(this, index, index); |
296 |
} |
297 |
|
298 |
} |
299 |
|
300 |
protected JList getFileList() { |
301 |
if (fileList == null) { |
302 |
fileList = new JList(new FilesystemStoreListModel()); |
303 |
|
304 |
fileList.addListSelectionListener(this);
|
305 |
} |
306 |
return fileList;
|
307 |
} |
308 |
|
309 |
private JScrollPane getFileListScroll() { |
310 |
if (fileListScroll == null) { |
311 |
fileListScroll = new JScrollPane(); |
312 |
fileListScroll.setViewportView(getFileList()); |
313 |
fileListScroll |
314 |
.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
|
315 |
fileListScroll |
316 |
.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
317 |
|
318 |
} |
319 |
return fileListScroll;
|
320 |
} |
321 |
|
322 |
private Component getButtonsPanel() { |
323 |
if (buttonsPanel == null) { |
324 |
buttonsPanel = new JPanel(); |
325 |
|
326 |
buttonsPanel.setLayout(new GridBagLayout()); |
327 |
|
328 |
GridBagConstraints constr = new GridBagConstraints(); |
329 |
|
330 |
constr.anchor = GridBagConstraints.CENTER;
|
331 |
constr.fill = GridBagConstraints.NONE;
|
332 |
constr.ipadx = 3;
|
333 |
constr.ipady = 3;
|
334 |
constr.insets = new Insets(3, 3, 3, 3); |
335 |
constr.gridwidth = GridBagConstraints.REMAINDER;
|
336 |
|
337 |
buttonsPanel.add(getAddButton(), constr); |
338 |
buttonsPanel.add(getPropertiesButton(), constr); |
339 |
buttonsPanel.add(getRemoveButton(), constr); |
340 |
buttonsPanel.add(getUpButton(), constr); |
341 |
buttonsPanel.add(getDownButton(), constr); |
342 |
// buttonsPanel.add(new JLabel(), constrLbl);
|
343 |
} |
344 |
return buttonsPanel;
|
345 |
} |
346 |
|
347 |
private JButton getAddButton() { |
348 |
if (addButton == null) { |
349 |
addButton = |
350 |
ToolsSwingLocator.getUsabilitySwingManager().createJButton(); |
351 |
addButton.setText(getLocalizedText("add"));
|
352 |
addButton.setMargin(new java.awt.Insets(2, 2, 2, 2)); |
353 |
addButton.setActionCommand(ADD_COMMAND); |
354 |
addButton.addActionListener(this);
|
355 |
} |
356 |
return addButton;
|
357 |
} |
358 |
|
359 |
private JButton getPropertiesButton() { |
360 |
if (propertiesButton == null) { |
361 |
propertiesButton = |
362 |
ToolsSwingLocator.getUsabilitySwingManager().createJButton(); |
363 |
propertiesButton.setText(getLocalizedText("properties"));
|
364 |
propertiesButton.setMargin(new java.awt.Insets(2, 2, 2, 2)); |
365 |
propertiesButton.setActionCommand(EDIT_COMMAND); |
366 |
propertiesButton.addActionListener(this);
|
367 |
} |
368 |
return propertiesButton;
|
369 |
} |
370 |
|
371 |
private JButton getRemoveButton() { |
372 |
if (removeButton == null) { |
373 |
removeButton = |
374 |
ToolsSwingLocator.getUsabilitySwingManager().createJButton(); |
375 |
removeButton.setText(getLocalizedText("remove"));
|
376 |
removeButton.setMargin(new java.awt.Insets(2, 2, 2, 2)); |
377 |
removeButton.setActionCommand(REMOVE_COMMAND); |
378 |
removeButton.addActionListener(this);
|
379 |
} |
380 |
return removeButton;
|
381 |
} |
382 |
|
383 |
private JButton getUpButton() { |
384 |
if (upButton == null) { |
385 |
upButton = |
386 |
ToolsSwingLocator.getUsabilitySwingManager().createJButton(); |
387 |
upButton.setText(getLocalizedText("up"));
|
388 |
upButton.setMargin(new java.awt.Insets(2, 2, 2, 2)); |
389 |
upButton.setActionCommand(UP_COMMAND); |
390 |
upButton.addActionListener(this);
|
391 |
} |
392 |
return upButton;
|
393 |
} |
394 |
|
395 |
private JButton getDownButton() { |
396 |
if (downButton == null) { |
397 |
downButton = |
398 |
ToolsSwingLocator.getUsabilitySwingManager().createJButton(); |
399 |
downButton.setText(getLocalizedText("down"));
|
400 |
downButton.setMargin(new java.awt.Insets(2, 2, 2, 2)); |
401 |
downButton.setActionCommand(DOWN_COMMAND); |
402 |
downButton.addActionListener(this);
|
403 |
} |
404 |
return downButton;
|
405 |
} |
406 |
|
407 |
private String getLocalizedText(String txt) { |
408 |
try {
|
409 |
return PluginServices.getText(this, txt); |
410 |
} catch (Exception e) { |
411 |
return txt;
|
412 |
} |
413 |
} |
414 |
|
415 |
public void actionPerformed(ActionEvent e) { |
416 |
String command = e.getActionCommand();
|
417 |
FilesystemStoreListModel model = |
418 |
(FilesystemStoreListModel) getFileList().getModel(); |
419 |
|
420 |
if (command == ADD_COMMAND) {
|
421 |
this.addFilesFromParameters(this.chooseFilesParameters()); |
422 |
} else
|
423 |
if (command == EDIT_COMMAND) {
|
424 |
showPropertiesDialog(model.getDynObjectAt(getFileList().getSelectedIndex())); |
425 |
|
426 |
} else
|
427 |
if (command == REMOVE_COMMAND) {
|
428 |
int[] selecteds = getFileList().getSelectedIndices(); |
429 |
for (int i = selecteds.length - 1; i > -1; i--) { |
430 |
model.remove(selecteds[i]); |
431 |
} |
432 |
getFileList().setSelectedIndex(-1);
|
433 |
|
434 |
this.updateContainingWizardAcceptButton();
|
435 |
|
436 |
} else
|
437 |
if (command == UP_COMMAND) {
|
438 |
List<FilesystemStoreParameters> items =
|
439 |
new ArrayList<FilesystemStoreParameters>(); |
440 |
int[] selecteds = getFileList().getSelectedIndices(); |
441 |
if (selecteds.length == 0 || selecteds[0] == 0) { |
442 |
return;
|
443 |
} |
444 |
for (int i = 0; i < selecteds.length; i++) { |
445 |
items.add(model.getStoreParameterAt(selecteds[i])); |
446 |
selecteds[i]--; |
447 |
} |
448 |
Iterator<FilesystemStoreParameters> iter =
|
449 |
items.iterator(); |
450 |
while (iter.hasNext()) {
|
451 |
model.up(iter.next()); |
452 |
} |
453 |
getFileList().setSelectedIndices(selecteds); |
454 |
|
455 |
} else
|
456 |
if (command == DOWN_COMMAND) {
|
457 |
List<FilesystemStoreParameters> items =
|
458 |
new ArrayList<FilesystemStoreParameters>(); |
459 |
int[] selecteds = |
460 |
getFileList().getSelectedIndices(); |
461 |
if (selecteds.length == 0 |
462 |
|| selecteds[selecteds.length - 1] == model
|
463 |
.getSize() - 1) {
|
464 |
return;
|
465 |
} |
466 |
for (int i = selecteds.length - 1; i > -1; i--) { |
467 |
items.add(model |
468 |
.getStoreParameterAt(selecteds[i])); |
469 |
selecteds[i]++; |
470 |
} |
471 |
Iterator<FilesystemStoreParameters> iter =
|
472 |
items.iterator(); |
473 |
while (iter.hasNext()) {
|
474 |
model.down(iter.next()); |
475 |
} |
476 |
getFileList().setSelectedIndices(selecteds); |
477 |
|
478 |
} else {
|
479 |
throw new IllegalArgumentException(command); |
480 |
} |
481 |
|
482 |
} |
483 |
|
484 |
protected void showPropertiesDialog(final DynObject parameters) { |
485 |
FilesystemExplorerPropertiesPanelManager manager = ApplicationLocator.getFilesystemExplorerPropertiesPanelManager(); |
486 |
FilesystemExplorerPropertiesPanel panel = manager.createPanel(parameters); |
487 |
manager.showPropertiesDialog(parameters,panel); |
488 |
|
489 |
refreshFileList(); |
490 |
} |
491 |
|
492 |
protected void refreshFileList() { |
493 |
// force repaint
|
494 |
ListModel lm = this.getFileList().getModel(); |
495 |
if (lm instanceof FilesystemStoreListModel) { |
496 |
FilesystemStoreListModel mm = (FilesystemStoreListModel) lm; |
497 |
mm.forceUpdate(getFileList().getSelectedIndex()); |
498 |
} |
499 |
} |
500 |
|
501 |
public void addFiles(List<File> files) { |
502 |
File[] filesArray = files.toArray(new File[files.size()]); |
503 |
this.addFilesFromParameters(this.getParametersList(null, filesArray, null)); |
504 |
} |
505 |
|
506 |
private void addFilesFromParameters(List<FilesystemStoreParameters> filesParams) { |
507 |
if ( filesParams.isEmpty() ) {
|
508 |
return;
|
509 |
} |
510 |
FilesystemStoreListModel model = (FilesystemStoreListModel) getFileList().getModel(); |
511 |
model.addAll(filesParams); |
512 |
getFileList().setModel(model); |
513 |
|
514 |
this.updateContainingWizardAcceptButton();
|
515 |
try {
|
516 |
fileList.setSelectedIndex(fileList.getModel().getSize() - 1);
|
517 |
} catch (Exception ex) { |
518 |
// Ignore errors
|
519 |
} |
520 |
} |
521 |
|
522 |
/**
|
523 |
* check number of files already selected for adding
|
524 |
*/
|
525 |
private void updateContainingWizardAcceptButton() { |
526 |
int curr_add_file_count = getFileList().getModel().getSize();
|
527 |
this.callStateChanged(curr_add_file_count > 0); |
528 |
} |
529 |
|
530 |
|
531 |
public boolean areSettingsValid() { |
532 |
int curr_add_file_count = getFileList().getModel().getSize();
|
533 |
boolean r = curr_add_file_count > 0; |
534 |
return r;
|
535 |
} |
536 |
|
537 |
public List<DynObject> getSelecteds() { |
538 |
ArrayList<DynObject> list = new ArrayList<DynObject>(); |
539 |
JList fList = this.getFileList(); |
540 |
int[] selecteds = fList.getSelectedIndices(); |
541 |
FilesystemStoreListModel model = |
542 |
(FilesystemStoreListModel) fList.getModel(); |
543 |
|
544 |
for (int index : selecteds) { |
545 |
list.add((DynObject) model.getStoreParameterAt(index)); |
546 |
} |
547 |
return list;
|
548 |
} |
549 |
|
550 |
public void valueChanged(ListSelectionEvent e) { |
551 |
this.updateButtons();
|
552 |
} |
553 |
|
554 |
public class MyFileFilter extends FileFilter { |
555 |
|
556 |
public FilesystemFileFilter filter = null; |
557 |
|
558 |
public MyFileFilter(FilesystemFileFilter params) {
|
559 |
this.filter = params;
|
560 |
} |
561 |
|
562 |
/**
|
563 |
* @see javax.swing.filechooser.FileFilter#accept(java.io.File)
|
564 |
*/
|
565 |
public boolean accept(File f) { |
566 |
if (f.isDirectory()) {
|
567 |
return true; |
568 |
} |
569 |
return filter.accept(f);
|
570 |
|
571 |
} |
572 |
|
573 |
/**
|
574 |
* @see javax.swing.filechooser.FileFilter#getDescription()
|
575 |
*/
|
576 |
public String getDescription() { |
577 |
return filter.getDescription();
|
578 |
} |
579 |
|
580 |
public String getName() { |
581 |
return filter.getDataStoreProviderName();
|
582 |
} |
583 |
} |
584 |
|
585 |
private List<FilesystemStoreParameters> chooseFilesParameters() { |
586 |
|
587 |
// this.callStateChanged(true);
|
588 |
|
589 |
JFileChooser fileChooser =
|
590 |
new JFileChooser(OPEN_LAYER_FILE_CHOOSER_ID, |
591 |
explorer.getCurrentPath()); |
592 |
fileChooser.setMultiSelectionEnabled(true);
|
593 |
fileChooser.setAcceptAllFileFilterUsed(false);
|
594 |
|
595 |
Iterator<MyFileFilter> iter = this.filters.iterator(); |
596 |
while (iter.hasNext()) {
|
597 |
fileChooser.addChoosableFileFilter(iter.next()); |
598 |
} |
599 |
|
600 |
if (filters.size() > 0) { |
601 |
MyFileFilter firstf = filters.get(0);
|
602 |
// Set first filter
|
603 |
fileChooser.setFileFilter(firstf); |
604 |
} |
605 |
|
606 |
// If there is a last filter, use it
|
607 |
if (lastFilter != null && filters.contains(lastFilter)) { |
608 |
fileChooser.setFileFilter(lastFilter); |
609 |
} |
610 |
|
611 |
int result = fileChooser.showOpenDialog(this); |
612 |
|
613 |
List<FilesystemStoreParameters> toAdd =
|
614 |
new ArrayList<FilesystemStoreParameters>(); |
615 |
|
616 |
if (result == JFileChooser.APPROVE_OPTION) { |
617 |
lastFilter = (MyFileFilter) fileChooser.getFileFilter(); |
618 |
lastPath = fileChooser.getCurrentDirectory().getAbsolutePath(); |
619 |
JFileChooser.setLastPath(OPEN_LAYER_FILE_CHOOSER_ID, fileChooser.getCurrentDirectory());
|
620 |
String providerName = null; |
621 |
if( lastFilter!=null ) { |
622 |
if( !DEFAULT_FILTER.equalsIgnoreCase(lastFilter.getDescription()) ) {
|
623 |
providerName = lastFilter.getName(); |
624 |
} |
625 |
} |
626 |
toAdd = this.getParametersList(
|
627 |
fileChooser.getCurrentDirectory(), |
628 |
fileChooser.getSelectedFiles(), |
629 |
providerName |
630 |
); |
631 |
} |
632 |
return toAdd;
|
633 |
} |
634 |
|
635 |
private List<FilesystemStoreParameters> getParametersList(File folder, File[] files, String providerName) { |
636 |
ApplicationManager application = ApplicationLocator.getManager(); |
637 |
|
638 |
PrepareContext context = this.getPrepareDataStoreContext();
|
639 |
|
640 |
List<DataStoreParameters> params = new ArrayList<DataStoreParameters>(); |
641 |
List<DataStoreParameters> preparedParams = null; |
642 |
List<FilesystemStoreParameters> returnParams = new ArrayList<FilesystemStoreParameters>(); |
643 |
|
644 |
for ( File aFile : files ) { |
645 |
String currentProvider = providerName;
|
646 |
DataStoreParameters param; |
647 |
try {
|
648 |
if( folder == null) { |
649 |
explorer.setCurrentPath(aFile.getParentFile()); |
650 |
} else {
|
651 |
explorer.setCurrentPath(folder); |
652 |
} |
653 |
if( currentProvider == null ) { |
654 |
for( int i=1; i<this.filters.size(); i++) { |
655 |
MyFileFilter filter = this.filters.get(i);
|
656 |
if( filter.accept(aFile)) {
|
657 |
currentProvider = filter.getName(); |
658 |
break;
|
659 |
} |
660 |
} |
661 |
} |
662 |
if( currentProvider == null ) { |
663 |
// TODO: ask user
|
664 |
continue;
|
665 |
} |
666 |
param = explorer.createStoreParameters(aFile, currentProvider); |
667 |
if ( param == null ) { |
668 |
// TODO show warning
|
669 |
continue;
|
670 |
} |
671 |
params.add(param); |
672 |
|
673 |
} catch (DataException e) {
|
674 |
NotificationManager.addError(e); |
675 |
return null; |
676 |
} |
677 |
} |
678 |
|
679 |
try {
|
680 |
preparedParams |
681 |
= application.prepareOpenDataStoreParameters(params, context); |
682 |
} catch (Exception e) { |
683 |
NotificationManager.addError(e); |
684 |
} |
685 |
|
686 |
if ( preparedParams != null ) { |
687 |
for ( int i = 0; i < preparedParams.size(); i++ ) { |
688 |
returnParams.add((FilesystemStoreParameters) preparedParams.get(i)); |
689 |
} |
690 |
} |
691 |
return returnParams;
|
692 |
} |
693 |
|
694 |
protected abstract PrepareContext getPrepareDataStoreContext(); |
695 |
|
696 |
/**
|
697 |
* Refers to up/down/remove buttons etc. Called when selection changes
|
698 |
*/
|
699 |
private void updateButtons() { |
700 |
int selectedIndex = this.getFileList().getSelectedIndex(); |
701 |
int size = this.getFileList().getModel().getSize(); |
702 |
int[] selecteds = this.getFileList().getSelectedIndices(); |
703 |
if (size < 1) { |
704 |
this.getPropertiesButton().setEnabled(false); |
705 |
this.getRemoveButton().setEnabled(false); |
706 |
this.getUpButton().setEnabled(false); |
707 |
this.getDownButton().setEnabled(false); |
708 |
return;
|
709 |
} else
|
710 |
if (size == 1) { |
711 |
this.getUpButton().setEnabled(false); |
712 |
this.getDownButton().setEnabled(false); |
713 |
} else {
|
714 |
this.getUpButton().setEnabled(selectedIndex > 0); |
715 |
|
716 |
this.getDownButton().setEnabled(
|
717 |
selectedIndex > -1
|
718 |
&& selecteds[selecteds.length - 1] < size - 1); |
719 |
} |
720 |
this.getPropertiesButton().setEnabled(
|
721 |
selectedIndex > -1
|
722 |
&& this.getFileList().getSelectedIndices().length == 1); |
723 |
this.getRemoveButton().setEnabled(selectedIndex > -1); |
724 |
} |
725 |
|
726 |
public void close() { |
727 |
this.dispose();
|
728 |
} |
729 |
|
730 |
public void dispose() { |
731 |
if (explorer != null) { |
732 |
explorer.dispose(); |
733 |
explorer = null;
|
734 |
} |
735 |
if (filters != null) { |
736 |
filters.clear(); |
737 |
filters = null;
|
738 |
} |
739 |
} |
740 |
|
741 |
class RefreshThread extends Thread { |
742 |
|
743 |
private int waitt = 0; |
744 |
private JList comp = null; |
745 |
|
746 |
public RefreshThread(JList c, int t) { |
747 |
waitt = t; |
748 |
comp = c; |
749 |
} |
750 |
public void run() { |
751 |
|
752 |
try {
|
753 |
/*
|
754 |
* We might need to wait a bit because the GUI thread needs
|
755 |
* some time to update (this is because we have used a modal dialog
|
756 |
* and we refresh after that dialog is closed)
|
757 |
*/
|
758 |
Thread.sleep(waitt);
|
759 |
} catch (Exception exc) { |
760 |
|
761 |
} |
762 |
|
763 |
SwingUtilities.invokeLater(new Runnable() { |
764 |
public void run() { |
765 |
try {
|
766 |
// System.out.println("REFRESH");
|
767 |
comp.clearSelection(); |
768 |
} catch (Exception exc) { |
769 |
LOG.info("Error while refreshing components.", exc);
|
770 |
} |
771 |
} |
772 |
}); |
773 |
} |
774 |
} |
775 |
|
776 |
protected void fixParametersFromContext(final DynObject parameters) { |
777 |
// By default do nothing
|
778 |
} |
779 |
} |