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 / FilesystemExplorerWizardPanelController.java @ 43554
History | View | Annotate | Download (19.4 KB)
1 |
package org.gvsig.fmap.dal.serverexplorer.filesystem.swing; |
---|---|
2 |
|
3 |
import org.gvsig.fmap.dal.swing.dataStoreParameters.DataStoreParametersPanelManager; |
4 |
import org.gvsig.fmap.dal.swing.dataStoreParameters.DataStoreParametersPanel; |
5 |
|
6 |
import java.awt.Component; |
7 |
import java.awt.Window; |
8 |
import java.awt.event.ActionEvent; |
9 |
import java.awt.event.ActionListener; |
10 |
import java.io.File; |
11 |
import java.util.ArrayList; |
12 |
import java.util.Iterator; |
13 |
import java.util.List; |
14 |
import java.util.Locale; |
15 |
|
16 |
import javax.swing.DefaultListSelectionModel; |
17 |
import javax.swing.JFileChooser; |
18 |
import javax.swing.ListSelectionModel; |
19 |
import javax.swing.event.ListSelectionEvent; |
20 |
import javax.swing.event.ListSelectionListener; |
21 |
import javax.swing.table.TableColumn; |
22 |
|
23 |
import org.cresques.cts.IProjection; |
24 |
import org.slf4j.Logger; |
25 |
import org.slf4j.LoggerFactory; |
26 |
|
27 |
import org.gvsig.andami.messages.NotificationManager; |
28 |
import org.gvsig.app.ApplicationLocator; |
29 |
import org.gvsig.app.ApplicationManager; |
30 |
import org.gvsig.app.prepareAction.PrepareContext; |
31 |
import org.gvsig.filedialogchooser.FileDialogChooser; |
32 |
import org.gvsig.fmap.dal.DataStore; |
33 |
import org.gvsig.fmap.dal.DataStoreParameters; |
34 |
import org.gvsig.fmap.dal.exception.DataException; |
35 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
36 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
37 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel.MyFileFilter; |
38 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
39 |
import org.gvsig.fmap.mapcontext.MapContext; |
40 |
import org.gvsig.tools.ToolsLocator; |
41 |
import org.gvsig.tools.dynobject.DynObject; |
42 |
import org.gvsig.tools.i18n.I18nManager; |
43 |
import org.gvsig.tools.util.ToolsUtilLocator; |
44 |
|
45 |
|
46 |
/**
|
47 |
* @author fdiaz
|
48 |
*
|
49 |
*/
|
50 |
public class FilesystemExplorerWizardPanelController extends FilesystemExplorerWizardPanelView { |
51 |
|
52 |
|
53 |
/**
|
54 |
*
|
55 |
*/
|
56 |
private static final long serialVersionUID = -2351856603294610783L; |
57 |
|
58 |
@SuppressWarnings("unused") |
59 |
private static final Logger LOG = LoggerFactory |
60 |
.getLogger(FilesystemExplorerWizardPanelController.class); |
61 |
|
62 |
/**
|
63 |
*
|
64 |
*/
|
65 |
public static final String OPEN_LAYER_FILE_CHOOSER_ID = "OPEN_LAYER_FILE_CHOOSER_ID"; |
66 |
|
67 |
private static final String DEFAULT_FILTER = "All_supported"; |
68 |
|
69 |
protected FilesystemServerExplorer explorer;
|
70 |
private ArrayList<MyFileFilter> filters; |
71 |
protected MapContext mapContext;
|
72 |
protected int selectedIndex; |
73 |
private static MyFileFilter lastFilter = null; |
74 |
private boolean tabularOnly; |
75 |
|
76 |
/**
|
77 |
* @param mapContext
|
78 |
* @param explorer
|
79 |
* @param filters
|
80 |
*/
|
81 |
public FilesystemExplorerWizardPanelController(MapContext mapContext, FilesystemServerExplorer explorer, ArrayList<MyFileFilter> filters) { |
82 |
this.mapContext = mapContext;
|
83 |
this.explorer = explorer;
|
84 |
this.filters = filters;
|
85 |
this.selectedIndex = -1; |
86 |
|
87 |
initComponents(); |
88 |
translate(); |
89 |
} |
90 |
|
91 |
private void translate() { |
92 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
93 |
btnAdd.setText(i18nManager.getTranslation("Anadir"));
|
94 |
btnProperties.setText(i18nManager.getTranslation("Properties"));
|
95 |
btnRemove.setText(i18nManager.getTranslation("Eliminar"));
|
96 |
btnUp.setText(i18nManager.getTranslation("arriba"));
|
97 |
btnDown.setText(i18nManager.getTranslation("abajo"));
|
98 |
|
99 |
btnAdd.setToolTipText(i18nManager.getTranslation(btnAdd.getToolTipText())); |
100 |
btnProperties.setToolTipText(i18nManager.getTranslation(btnProperties.getToolTipText())); |
101 |
btnRemove.setToolTipText(i18nManager.getTranslation(btnRemove.getToolTipText())); |
102 |
btnUp.setToolTipText(i18nManager.getTranslation(btnUp.getToolTipText())); |
103 |
btnDown.setToolTipText(i18nManager.getTranslation(btnDown.getToolTipText())); |
104 |
|
105 |
btnChangeViewProjection.setText(i18nManager.getTranslation(btnChangeViewProjection.getText())); |
106 |
btnChangeViewProjection.setToolTipText(i18nManager.getTranslation(btnChangeViewProjection.getToolTipText())); |
107 |
} |
108 |
|
109 |
/**
|
110 |
* @param locale
|
111 |
*
|
112 |
*/
|
113 |
public void setLocate(Locale locale) { |
114 |
Locale l = super.getLocale(); |
115 |
if (!l.equals(locale)) {
|
116 |
translate(); |
117 |
} |
118 |
super.setLocale(locale);
|
119 |
} |
120 |
|
121 |
private void initComponents() { |
122 |
|
123 |
this.btnAdd.addActionListener(new ActionListener() { |
124 |
@Override
|
125 |
public void actionPerformed(ActionEvent e) { |
126 |
doAdd(); |
127 |
} |
128 |
}); |
129 |
|
130 |
this.btnProperties.addActionListener(new ActionListener() { |
131 |
@Override
|
132 |
public void actionPerformed(ActionEvent e) { |
133 |
doOpenProperties(); |
134 |
} |
135 |
}); |
136 |
|
137 |
this.btnRemove.addActionListener(new ActionListener() { |
138 |
@Override
|
139 |
public void actionPerformed(ActionEvent e) { |
140 |
doRemove(); |
141 |
} |
142 |
}); |
143 |
|
144 |
this.btnUp.addActionListener(new ActionListener() { |
145 |
@Override
|
146 |
public void actionPerformed(ActionEvent e) { |
147 |
doUp(); |
148 |
} |
149 |
}); |
150 |
|
151 |
this.btnDown.addActionListener(new ActionListener() { |
152 |
@Override
|
153 |
public void actionPerformed(ActionEvent e) { |
154 |
doDown(); |
155 |
} |
156 |
}); |
157 |
|
158 |
this.btnChangeViewProjection.addActionListener(new ActionListener() { |
159 |
@Override
|
160 |
public void actionPerformed(ActionEvent e) { |
161 |
doChangeViewProjection(); |
162 |
} |
163 |
}); |
164 |
|
165 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
166 |
|
167 |
this.tableFiles.setModel(new FilesystemExplorerWizardTableModel()); |
168 |
this.tableFiles.getColumn(
|
169 |
i18nManager.getTranslation(FilesystemExplorerWizardTableModel.COLUMN_STORE_PARAMETERS_NAME)) |
170 |
.setCellRenderer(new FilesystemStoreParameteresCellRenderer(mapContext));
|
171 |
TableColumn columnTiled = this.tableFiles.getColumn( |
172 |
i18nManager.getTranslation(FilesystemExplorerWizardTableModel.COLUMN_TILED_NAME)); |
173 |
columnTiled.setMaxWidth(80);
|
174 |
columnTiled.setCellRenderer(new TiledCellRenderer());
|
175 |
this.tableFiles.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
176 |
this.tableFiles.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
177 |
|
178 |
@Override
|
179 |
public void valueChanged(ListSelectionEvent e) { |
180 |
handleSelectionEvent(e); |
181 |
} |
182 |
}); |
183 |
this.btnChangeViewProjection.setEnabled(false); |
184 |
} |
185 |
|
186 |
|
187 |
protected void updateComponents() { |
188 |
btnUp.setEnabled(selectedIndex > 0 && selectedIndex < tableFiles.getRowCount());
|
189 |
btnDown.setEnabled(selectedIndex >= 0 && selectedIndex < (tableFiles.getRowCount() - 1)); |
190 |
btnProperties.setEnabled(selectedIndex >= 0 && selectedIndex < tableFiles.getRowCount());
|
191 |
btnRemove.setEnabled(selectedIndex >= 0 && selectedIndex < tableFiles.getRowCount());
|
192 |
} |
193 |
|
194 |
protected void handleSelectionEvent(ListSelectionEvent e){ |
195 |
if (e.getValueIsAdjusting()) {
|
196 |
return;
|
197 |
} |
198 |
final DefaultListSelectionModel target = (DefaultListSelectionModel)e.getSource(); |
199 |
selectedIndex = target.getAnchorSelectionIndex(); |
200 |
|
201 |
updateComponents(); |
202 |
} |
203 |
|
204 |
protected void doAdd() { |
205 |
|
206 |
List<FilesystemStoreParameters> list = chooseFilesParameters();
|
207 |
FilesystemExplorerWizardTableModel model = (FilesystemExplorerWizardTableModel) this.tableFiles.getModel();
|
208 |
|
209 |
for (FilesystemStoreParameters filesystemStoreParameters : list) {
|
210 |
model.add(filesystemStoreParameters); |
211 |
updateTable(); |
212 |
int index = model.getRowCount()-1; |
213 |
this.tableFiles.getSelectionModel().setSelectionInterval(index, index);
|
214 |
} |
215 |
updateChangeViewProjection(); |
216 |
} |
217 |
|
218 |
protected void doRemove() { |
219 |
FilesystemExplorerWizardTableModel model = (FilesystemExplorerWizardTableModel)this.tableFiles.getModel();
|
220 |
if(selectedIndex>=0 && selectedIndex<model.getRowCount()){ |
221 |
int index = selectedIndex;
|
222 |
model.remove(index); |
223 |
updateTable(); |
224 |
if(index>(model.getRowCount()-1)) { |
225 |
index = model.getRowCount()-1;
|
226 |
}; |
227 |
this.tableFiles.getSelectionModel().setSelectionInterval(index, index);
|
228 |
updateChangeViewProjection(); |
229 |
} |
230 |
} |
231 |
|
232 |
protected void doOpenProperties() { |
233 |
FilesystemExplorerWizardTableModel model = (FilesystemExplorerWizardTableModel) this.tableFiles.getModel();
|
234 |
if (selectedIndex >= 0 && selectedIndex < model.getRowCount()) { |
235 |
int index = selectedIndex;
|
236 |
FilesystemStoreParameters parameters = model.getParams(index); |
237 |
showPropertiesDialog((DynObject) parameters); |
238 |
updateTable(); |
239 |
this.tableFiles.getSelectionModel().setSelectionInterval(index, index);
|
240 |
updateChangeViewProjection(); |
241 |
} |
242 |
} |
243 |
|
244 |
protected void doUp() { |
245 |
FilesystemExplorerWizardTableModel model = (FilesystemExplorerWizardTableModel)this.tableFiles.getModel();
|
246 |
if(selectedIndex>0 && selectedIndex<model.getRowCount()){ |
247 |
int index = selectedIndex;
|
248 |
model.up(index); |
249 |
updateTable(); |
250 |
this.tableFiles.getSelectionModel().setSelectionInterval(index-1, index-1); |
251 |
updateChangeViewProjection(); |
252 |
} |
253 |
} |
254 |
|
255 |
protected void doDown() { |
256 |
FilesystemExplorerWizardTableModel model = (FilesystemExplorerWizardTableModel)this.tableFiles.getModel();
|
257 |
if(selectedIndex>=0 && selectedIndex<model.getRowCount()-1){ |
258 |
int index = selectedIndex;
|
259 |
model.down(index); |
260 |
updateTable(); |
261 |
this.tableFiles.getSelectionModel().setSelectionInterval(index+1, index+1); |
262 |
updateChangeViewProjection(); |
263 |
} |
264 |
} |
265 |
|
266 |
protected void showPropertiesDialog(final DynObject parameters) { |
267 |
DataStoreParametersPanelManager manager = DALSwingLocator.getDataStoreParametersPanelManager(); |
268 |
DataStoreParametersPanel panel = manager.createDataStoreParametersPanel(parameters); |
269 |
panel.setExcludeGeometryOptions(false);
|
270 |
manager.showPropertiesDialog(parameters, panel); |
271 |
} |
272 |
|
273 |
private void updateTable(){ |
274 |
((FilesystemExplorerWizardTableModel)this.tableFiles.getModel()).fireTableDataChanged();
|
275 |
updateRowHeights(); |
276 |
this.tableFiles.getSelectionModel().setSelectionInterval(selectedIndex, selectedIndex);
|
277 |
} |
278 |
|
279 |
private void updateRowHeights() { |
280 |
|
281 |
/*
|
282 |
* Auto adjust the height of rows in a JTable.
|
283 |
* The only way to know the row height for sure is to render each cell
|
284 |
* to determine the rendered height. After your table is populated with
|
285 |
* data you can do:
|
286 |
*
|
287 |
*/
|
288 |
for (int row = 0; row < this.tableFiles.getRowCount(); row++) { |
289 |
int rowHeight = this.tableFiles.getRowHeight(); |
290 |
for (int column = 0; column < this.tableFiles.getColumnCount(); column++) |
291 |
{ |
292 |
Component comp = this.tableFiles.prepareRenderer(this.tableFiles.getCellRenderer(row, column), row, column); |
293 |
rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
|
294 |
} |
295 |
this.tableFiles.setRowHeight(row, rowHeight);
|
296 |
} |
297 |
} |
298 |
|
299 |
|
300 |
private List<FilesystemStoreParameters> chooseFilesParameters() { |
301 |
|
302 |
FileDialogChooser fileChooser = ToolsUtilLocator.getFileDialogChooserManager().create(OPEN_LAYER_FILE_CHOOSER_ID); |
303 |
fileChooser.setCurrentDirectory(explorer.getCurrentPath()); |
304 |
fileChooser.setMultiSelectionEnabled(true);
|
305 |
fileChooser.setAcceptAllFileFilterUsed(false);
|
306 |
|
307 |
Iterator<MyFileFilter> iter = this.filters.iterator(); |
308 |
while (iter.hasNext()) {
|
309 |
fileChooser.addChoosableFileFilter(iter.next()); |
310 |
} |
311 |
|
312 |
if (filters.size() > 0) { |
313 |
MyFileFilter firstf = filters.get(0);
|
314 |
// Set first filter
|
315 |
fileChooser.setFileFilter(firstf); |
316 |
} |
317 |
|
318 |
// If there is a last filter, use it
|
319 |
if (lastFilter != null && filters.contains(lastFilter)) { |
320 |
fileChooser.setFileFilter(lastFilter); |
321 |
} |
322 |
|
323 |
int result = fileChooser.showOpenDialog(this); |
324 |
|
325 |
List<FilesystemStoreParameters> toAdd = new ArrayList<>(); |
326 |
|
327 |
if (result == JFileChooser.APPROVE_OPTION) { |
328 |
lastFilter = (MyFileFilter) fileChooser.getFileFilter(); |
329 |
String providerName = null; |
330 |
if( lastFilter!=null ) { |
331 |
if( !DEFAULT_FILTER.equalsIgnoreCase(lastFilter.getDescription()) ) {
|
332 |
providerName = lastFilter.getName(); |
333 |
} |
334 |
} |
335 |
toAdd = this.getParametersList(
|
336 |
fileChooser.getCurrentDirectory(), |
337 |
fileChooser.getSelectedFiles(), |
338 |
providerName |
339 |
); |
340 |
} |
341 |
return toAdd;
|
342 |
} |
343 |
|
344 |
|
345 |
private List<FilesystemStoreParameters> getParametersList(File folder, File[] files, String providerName) { |
346 |
ApplicationManager application = ApplicationLocator.getManager(); |
347 |
|
348 |
PrepareContext context = this.getPrepareDataStoreContext();
|
349 |
|
350 |
List<DataStoreParameters> params = new ArrayList<>(); |
351 |
List<DataStoreParameters> preparedParams = null; |
352 |
List<FilesystemStoreParameters> returnParams = new ArrayList<>(); |
353 |
|
354 |
for ( File aFile : files ) { |
355 |
String currentProvider = providerName;
|
356 |
DataStoreParameters param; |
357 |
try {
|
358 |
if( folder == null) { |
359 |
explorer.setCurrentPath(aFile.getParentFile()); |
360 |
} else {
|
361 |
explorer.setCurrentPath(folder); |
362 |
} |
363 |
if( currentProvider == null ) { |
364 |
for( int i=1; i<this.filters.size(); i++) { |
365 |
MyFileFilter filter = this.filters.get(i);
|
366 |
if( filter.accept(aFile)) {
|
367 |
currentProvider = filter.getName(); |
368 |
break;
|
369 |
} |
370 |
} |
371 |
} |
372 |
if( currentProvider == null ) { |
373 |
// TODO: ask user
|
374 |
continue;
|
375 |
} |
376 |
param = explorer.createStoreParameters(aFile, currentProvider); |
377 |
if ( param == null ) { |
378 |
// TODO show warning
|
379 |
continue;
|
380 |
} |
381 |
params.add(param); |
382 |
|
383 |
} catch (DataException e) {
|
384 |
NotificationManager.addError(e); |
385 |
return null; |
386 |
} |
387 |
} |
388 |
|
389 |
try {
|
390 |
preparedParams |
391 |
= application.prepareOpenDataStoreParameters(params, context); |
392 |
} catch (Exception e) { |
393 |
NotificationManager.addError(e); |
394 |
} |
395 |
|
396 |
if ( preparedParams != null ) { |
397 |
for ( int i = 0; i < preparedParams.size(); i++ ) { |
398 |
returnParams.add((FilesystemStoreParameters) preparedParams.get(i)); |
399 |
} |
400 |
} |
401 |
return returnParams;
|
402 |
} |
403 |
|
404 |
/**
|
405 |
* @return
|
406 |
*/
|
407 |
protected PrepareContext getPrepareDataStoreContext() {
|
408 |
return new PrepareContext() { |
409 |
|
410 |
@Override
|
411 |
public IProjection getViewProjection() {
|
412 |
return FilesystemExplorerWizardPanelController.this.mapContext.getProjection();
|
413 |
} |
414 |
|
415 |
@Override
|
416 |
public Window getOwnerWindow() { |
417 |
return null; |
418 |
} |
419 |
}; |
420 |
} |
421 |
|
422 |
/**
|
423 |
* @return DataStoreParameters[]
|
424 |
*/
|
425 |
public DataStoreParameters[] getParameters() { |
426 |
return ((FilesystemExplorerWizardTableModel)this.tableFiles.getModel()).getParameters(); |
427 |
} |
428 |
|
429 |
/**
|
430 |
* @param files
|
431 |
*/
|
432 |
public void addFiles(List<File> files) { |
433 |
File[] filesArray = files.toArray(new File[files.size()]); |
434 |
this.addFilesFromParameters(this.getParametersList(null, filesArray, null)); |
435 |
} |
436 |
|
437 |
private void addFilesFromParameters(List<FilesystemStoreParameters> filesParams) { |
438 |
if ( filesParams.isEmpty() ) {
|
439 |
return;
|
440 |
} |
441 |
FilesystemExplorerWizardTableModel model = (FilesystemExplorerWizardTableModel)this.tableFiles.getModel();
|
442 |
model.add(filesParams); |
443 |
updateChangeViewProjection(); |
444 |
this.updateTable();
|
445 |
} |
446 |
|
447 |
/**
|
448 |
* @param parameters
|
449 |
* @return boolean
|
450 |
*/
|
451 |
public boolean isTiled(DataStoreParameters parameters) { |
452 |
return ((FilesystemExplorerWizardTableModel)this.tableFiles.getModel()).isTiled(parameters); |
453 |
} |
454 |
|
455 |
/**
|
456 |
* @param explorer
|
457 |
*/
|
458 |
public void setExplorer(FilesystemServerExplorer explorer) { |
459 |
this.explorer = explorer;
|
460 |
} |
461 |
|
462 |
/**
|
463 |
* @param filters
|
464 |
*/
|
465 |
public void setFilters(ArrayList<MyFileFilter> filters) { |
466 |
this.filters=filters;
|
467 |
} |
468 |
|
469 |
/**
|
470 |
* Dispose filters and explorer.
|
471 |
*
|
472 |
*/
|
473 |
public void dispose() { |
474 |
if (explorer != null) { |
475 |
explorer.dispose(); |
476 |
explorer = null;
|
477 |
} |
478 |
if (filters != null) { |
479 |
filters.clear(); |
480 |
filters = null;
|
481 |
} |
482 |
} |
483 |
|
484 |
private void updateChangeViewProjection() { |
485 |
FilesystemExplorerWizardTableModel model = (FilesystemExplorerWizardTableModel) this.tableFiles.getModel();
|
486 |
if (selectedIndex < 0 && selectedIndex >= model.getRowCount()) { |
487 |
this.btnChangeViewProjection.setEnabled(false); |
488 |
} |
489 |
IProjection projection; |
490 |
DynObject parameters = (DynObject) model.getParams(0);
|
491 |
if( parameters.getDynClass().getDynField(DataStore.METADATA_CRS) != null ) { |
492 |
projection = (IProjection) parameters.getDynValue(DataStore.METADATA_CRS); |
493 |
if( mapContext == null || mapContext.getLayers().isEmpty() ) { |
494 |
if( projection == null ) { |
495 |
this.btnChangeViewProjection.setEnabled(false); |
496 |
} else if( projection.equals(mapContext.getProjection()) ) { |
497 |
this.btnChangeViewProjection.setEnabled(false); |
498 |
} else {
|
499 |
for( DataStoreParameters p : model.getParameters()) {
|
500 |
projection = (IProjection) p.getDynValue(DataStore.METADATA_CRS); |
501 |
if( projection != null ) { |
502 |
String s = projection.getFullCode();
|
503 |
if( s.contains("@") ) { |
504 |
this.btnChangeViewProjection.setEnabled(false); |
505 |
return;
|
506 |
} |
507 |
} |
508 |
} |
509 |
this.btnChangeViewProjection.setEnabled(true); |
510 |
} |
511 |
} |
512 |
} |
513 |
} |
514 |
|
515 |
private void doChangeViewProjection() { |
516 |
FilesystemExplorerWizardTableModel model = (FilesystemExplorerWizardTableModel) this.tableFiles.getModel();
|
517 |
if( model.getRowCount() <= 0 || |
518 |
(selectedIndex < 0 && selectedIndex >= model.getRowCount()) ) {
|
519 |
this.btnChangeViewProjection.setEnabled(false); |
520 |
return;
|
521 |
} |
522 |
IProjection projection; |
523 |
DynObject parameters = (DynObject) model.getParams(selectedIndex); |
524 |
if( parameters.getDynClass().getDynField(DataStore.METADATA_CRS) != null ) { |
525 |
projection = (IProjection) parameters.getDynValue(DataStore.METADATA_CRS); |
526 |
if( mapContext == null || mapContext.getLayers().isEmpty() ) { |
527 |
mapContext.setProjection(projection); |
528 |
updateTable(); |
529 |
} |
530 |
} |
531 |
} |
532 |
|
533 |
void setTabularOnly(boolean tabularOnly) { |
534 |
this.tabularOnly = tabularOnly;
|
535 |
this.btnChangeViewProjection.setVisible(!tabularOnly);
|
536 |
} |
537 |
} |