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