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