Revision 42570 trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/java/org/gvsig/app/gui/WizardPanel.java

View differences:

WizardPanel.java
3 3
 *
4 4
 * Copyright (C) 2007-2013 gvSIG Association.
5 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.
6
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10 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.
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15 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.
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 19
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
23 22
 */
24 23
package org.gvsig.app.gui;
25 24

  
26 25
import java.awt.Window;
27
import java.util.logging.Level;
28 26
import javax.swing.JOptionPane;
29 27

  
30 28
import javax.swing.JPanel;
31
import org.apache.commons.lang3.StringUtils;
32 29

  
33 30
import org.cresques.cts.IProjection;
34 31
import org.gvsig.app.ApplicationLocator;
......
49 46
import org.gvsig.fmap.mapcontrol.MapControlCreationException;
50 47
import org.gvsig.fmap.mapcontrol.MapControlLocator;
51 48
import org.gvsig.tools.ToolsLocator;
49
import org.gvsig.tools.dispose.DisposeUtils;
52 50
import org.gvsig.tools.i18n.I18nManager;
53 51
import org.gvsig.tools.identitymanagement.UnauthorizedException;
54 52
import org.slf4j.Logger;
......
56 54

  
57 55
public abstract class WizardPanel extends JPanel {
58 56

  
59
	private static final Logger logger = LoggerFactory.getLogger(WizardPanel.class);
57
    private static final Logger logger = LoggerFactory.getLogger(WizardPanel.class);
60 58
    private static final long serialVersionUID = 5317749209668850863L;
61 59
    private String tabName = "TabName";
62 60
    private MapControl mapCtrl = null;
63
    private WizardListenerSupport listenerSupport = new WizardListenerSupport();
64
	private MapContext mapContext;
65
	private boolean b_isMapControlAvailable = false;
61
    private final WizardListenerSupport listenerSupport = new WizardListenerSupport();
62
    private MapContext mapContext;
63
    private boolean b_isMapControlAvailable = false;
66 64

  
67 65
    public void addWizardListener(WizardListener listener) {
68 66
        listenerSupport.addWizardListener(listener);
......
97 95

  
98 96
    /**
99 97
     * Executes the wizard and returns anything created in the process.
100
     * 
98
     *
101 99
     * @return anything created in the process
102 100
     */
103 101
    public Object executeWizard() {
......
111 109

  
112 110
    /**
113 111
     * You can use it to interact with the MapControl component that will
114
     * receive the new layer, in order to get user feedback
115
     * (for instance a bounding box). Check the
116
     * {@link #isMapControlAvailable()} method before accessing the MapControl
117
     * because it may not be available (for instance when adding layers
118
     * to a MapContext not associated with a View).
119
     * 
112
     * receive the new layer, in order to get user feedback (for instance a
113
     * bounding box). Check the {@link #isMapControlAvailable()} method before
114
     * accessing the MapControl because it may not be available (for instance
115
     * when adding layers to a MapContext not associated with a View).
116
     *
120 117
     * For the moment, this method will return a non-null MapControl for
121 118
     * compatibility reasons, but you should still check
122
     * {@link #isMapControlAvailable()} to be sure it is a valid one,
123
     * as it could only be a fake MapControl.
124
     * 
125
     * It is recommended to use {@link #getMapContext()} method
126
     * when no interaction is needed with the map user interface
127
     * (for instance to get the active projection, visible extent, etc)
128
     * 
119
     * {@link #isMapControlAvailable()} to be sure it is a valid one, as it
120
     * could only be a fake MapControl.
121
     *
122
     * It is recommended to use {@link #getMapContext()} method when no
123
     * interaction is needed with the map user interface (for instance to get
124
     * the active projection, visible extent, etc)
125
     *
129 126
     * @return Returns the mapCtrl.
130 127
     */
131 128
    public MapControl getMapCtrl() {
132
    	if (mapCtrl!=null) {
133
    		return mapCtrl;
134
    	}
135
    	else if (mapContext!=null) {
129
        if (mapCtrl != null) {
130
            return mapCtrl;
131
        } else if (mapContext != null) {
136 132
    		// if MapContext has been set, create a fake MapControl
137
    		// for compatibility purposes
138
    		MapControl mc;
139
                try {
140
                    mc = MapControlLocator.getMapControlManager().createJMapControlPanel(mapContext);
141
                } catch (MapControlCreationException ex) {
142
                    logger.warn("Can't create a MapControl.", ex);
143
                    throw  new RuntimeException(ex);
144
                }
145
    		mapCtrl=mc;
146
    	}
147
    	return mapCtrl;
133
            // for compatibility purposes
134
            MapControl mc;
135
            try {
136
                mc = MapControlLocator.getMapControlManager().createJMapControlPanel(mapContext);
137
            } catch (MapControlCreationException ex) {
138
                logger.warn("Can't create a MapControl.", ex);
139
                throw new RuntimeException(ex);
140
            }
141
            mapCtrl = mc;
142
        }
143
        return mapCtrl;
148 144
    }
149 145

  
150 146
    /**
151 147
     * Sets the MapControl that will receive the new layer
152
     * @param mapCtrl
153
     *            The mapCtrl to set.
148
     *
149
     * @param mapCtrl The mapCtrl to set.
154 150
     */
155 151
    public void setMapCtrl(MapControl mapCtrl) {
156 152
        this.mapCtrl = mapCtrl;
157
        b_isMapControlAvailable = (mapCtrl!=null);
153
        b_isMapControlAvailable = (mapCtrl != null);
158 154
    }
159
    
155

  
160 156
    /**
161
     * You can use it to extract information from
162
     * the MapContext that will receive the new layer.
163
     * For example, projection to use, or visible extent.
164
     * 
157
     * You can use it to extract information from the MapContext that will
158
     * receive the new layer. For example, projection to use, or visible extent.
159
     *
165 160
     * @return Returns the MapContext.
166 161
     */
167 162
    public MapContext getMapContext() {
168
    	if (this.mapContext!=null || this.mapCtrl==null) {
169
    		return this.mapContext;
170
    	}
171
    	else {
172
    		return this.mapCtrl.getMapContext();
173
    	}
174
        
163
        if (this.mapContext != null || this.mapCtrl == null) {
164
            return this.mapContext;
165
        } else {
166
            return this.mapCtrl.getMapContext();
167
        }
168

  
175 169
    }
176 170

  
177 171
    /**
178 172
     * Sets the MapContext that will receive the new layer
179
     * @param mapContext
180
     *            The mapContext to set.
173
     *
174
     * @param mapContext The mapContext to set.
181 175
     */
182 176
    public void setMapContext(MapContext mapContext) {
183 177
        this.mapContext = mapContext;
184 178
    }
185 179

  
186 180
    /**
187
     * Checks whether the MapControl is available.
188
     * The MapControl may not be available in some circumstances, for instance
189
     * when adding layers to a MapContext not associated with a View.
190
     * 
181
     * Checks whether the MapControl is available. The MapControl may not be
182
     * available in some circumstances, for instance when adding layers to a
183
     * MapContext not associated with a View.
184
     *
191 185
     * A MapContext should always be available on the {@link #getMapContext()}
192 186
     * method.
193
     * 
187
     *
194 188
     * @return true if the MapControl is available, false otherwise
195 189
     */
196 190
    public boolean isMapControlAvailable() {
197
    	return b_isMapControlAvailable ;
191
        return b_isMapControlAvailable;
198 192
    }
199 193

  
200 194
    protected void doAddLayer(
201 195
            final String layerName, final DataStoreParameters parameters) {
202
    	final boolean b_isMapControlAvail = this.isMapControlAvailable();
203
    	final MapControl mapControl = this.getMapCtrl();
204
    	final MapContext mapContext = this.getMapContext();
205
    	final ApplicationManager application = ApplicationLocator.getManager();
206
        final MapContextManager manager =
207
            MapContextLocator.getMapContextManager();
196
        final boolean b_isMapControlAvail = this.isMapControlAvailable();
197
        final MapControl mapControl = this.getMapCtrl();
198
        final MapContext mapContext = this.getMapContext();
199
        final ApplicationManager application = ApplicationLocator.getManager();
200
        final MapContextManager manager
201
                = MapContextLocator.getMapContextManager();
208 202

  
209 203
        logger.info("addLayer('{}',...)", layerName);
210 204
        Thread task = new Thread(new Runnable() {
211 205

  
206
            @Override
212 207
            public void run() {
213 208
                FLayer layer = null;
214 209
                FLayer preparedLayer = null;
215
                try {                    
210
                try {
216 211
                    DataManager dataManager = DALLocator.getDataManager();
217 212
                    DataStore dataStore = dataManager.openStore(parameters.getDataStoreName(), parameters);
218 213
                    String layerName = dataStore.getName();
219
                    layer = manager.createLayer(layerName, dataStore);   
220
                    dataStore.dispose(); 
221
                    dataStore = null;
222
                    preparedLayer =
223
                        application.prepareOpenLayer(layer,
224
                            new PrepareContextView_v1() {
214
                    layer = manager.createLayer(layerName, dataStore);
215
                    DisposeUtils.disposeQuietly(dataStore);
216
                    preparedLayer
217
                            = application.prepareOpenLayer(layer,
218
                                    new PrepareContextView_v1() {
225 219

  
226
                                public Window getOwnerWindow() {
227
                                    return null;
228
                                }
220
                                        @Override
221
                                        public Window getOwnerWindow() {
222
                                            return null;
223
                                        }
229 224

  
230
                                public MapControl getMapControl() {
231
                                    return mapControl;
232
                                }
225
                                        @Override
226
                                        public MapControl getMapControl() {
227
                                            return mapControl;
228
                                        }
233 229

  
234
								public IProjection getViewProjection() {
235
									return mapContext.getProjection();
236
								}
230
                                        @Override
231
                                        public IProjection getViewProjection() {
232
                                            return mapContext.getProjection();
233
                                        }
237 234

  
238
								public MapContext getMapContext() {
239
									return mapContext;
240
								}
235
                                        @Override
236
                                        public MapContext getMapContext() {
237
                                            return mapContext;
238
                                        }
241 239

  
242
								public boolean isMapControlAvailable() {
243
									return b_isMapControlAvail;
244
								}
245
                            });
240
                                        @Override
241
                                        public boolean isMapControlAvailable() {
242
                                            return b_isMapControlAvail;
243
                                        }
244
                                    });
246 245
                    if (preparedLayer != null) {
247 246
                        mapContext.getLayers().addLayer(preparedLayer);
248 247
                    }
......
250 249
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
251 250
                    ApplicationManager application = ApplicationLocator.getManager();
252 251
                    String resource = "";
253
                    if( e.getResource() instanceof FilesystemStoreParameters ) {
254
                        resource = ((FilesystemStoreParameters)e.getResource()).getFile().getPath();
252
                    if (e.getResource() instanceof FilesystemStoreParameters) {
253
                        resource = ((FilesystemStoreParameters) e.getResource()).getFile().getPath();
255 254
                    }
256 255
                    application.messageDialog(
257 256
                            i18nManager.getTranslation("_User_0_is_not_authorized_to_1_on_resource_2_3",
258
                            new String[] {
259
                              e.getIdentity().getID(),
260
                              e.getActionName(),
261
                              e.getResourceName(),
262
                              resource
263
                            }),     
264
                            i18nManager.getTranslation("_Unauthorized_access"), 
257
                                    new String[]{
258
                                        e.getIdentity().getID(),
259
                                        e.getActionName(),
260
                                        e.getResourceName(),
261
                                        resource
262
                                    }),
263
                            i18nManager.getTranslation("_Unauthorized_access"),
265 264
                            JOptionPane.WARNING_MESSAGE
266 265
                    );
267
                    logger.warn("Unauthorized access to layer '"+layerName+"'.",e);
268
                    return;
269
                    
266
                    logger.warn("Unauthorized access to layer '" + layerName + "'.", e);
267

  
270 268
                } catch (Exception e) {
271 269
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
272 270
                    ApplicationManager application = ApplicationLocator.getManager();
273 271
                    application.messageDialog(
274
                            i18nManager.getTranslation("_Cant_load_the_layer") + " ("+layerName+")",
275
                            i18nManager.getTranslation("_Probems_loading_the_layer"), 
272
                            i18nManager.getTranslation("_Cant_load_the_layer") + " (" + layerName + ")",
273
                            i18nManager.getTranslation("_Probems_loading_the_layer"),
276 274
                            JOptionPane.WARNING_MESSAGE
277 275
                    );
278
                    logger.warn("Can't load layer '"+layerName+"'.",e);
279
                    return;
276
                    logger.warn("Can't load layer '" + layerName + "'.", e);
280 277

  
281 278
                } finally {
282
                    if (preparedLayer != null && preparedLayer != layer) {
283
                        preparedLayer.dispose();
284
                        preparedLayer = null;
279
                    if (preparedLayer != layer) {
280
                        DisposeUtils.disposeQuietly(preparedLayer);
285 281
                    }
286
                    if (layer != null) {
287
                        layer.dispose();
288
                        layer = null;
289
                    }
282
                    DisposeUtils.disposeQuietly(layer);
290 283
                }
291 284
            }
292 285
        });
293 286
        task.start();
294 287

  
295 288
    }
296
    
289

  
297 290
    /**
298
     * 
291
     *
299 292
     * @param mapControl
300 293
     * @param layerName
301 294
     * @param parameters
302
     * @deprecated Use {@link #doAddLayer(String, DataStoreParameters)}
303
     * in combination with {@link #setMapCtrl(MapControl)} if you need
304
     * to set the MapControl. Note that MapControl is automatically initialized
305
     * when creating the panel from the AddLayer extension.
295
     * @deprecated Use {@link #doAddLayer(String, DataStoreParameters)} in
296
     * combination with {@link #setMapCtrl(MapControl)} if you need to set the
297
     * MapControl. Note that MapControl is automatically initialized when
298
     * creating the panel from the AddLayer extension.
306 299
     */
307 300
    protected void doAddLayer(final MapControl mapControl,
308
        final String layerName, final DataStoreParameters parameters) {
309
    	this.setMapCtrl(mapControl);
310
    	doAddLayer(layerName, parameters);
301
            final String layerName, final DataStoreParameters parameters) {
302
        this.setMapCtrl(mapControl);
303
        doAddLayer(layerName, parameters);
311 304
    }
312
    
305

  
313 306
    /**
314
     * This method is called for example when user changes
315
     * tab in add layer dialog (new tab's settings are valid?)
316
     * 
317
     * @return whether current wizard settings are enough
318
     * (for example, to enable an Accept button in a container)
307
     * This method is called for example when user changes tab in add layer
308
     * dialog (new tab's settings are valid?)
309
     *
310
     * @return whether current wizard settings are enough (for example, to
311
     * enable an Accept button in a container)
319 312
     */
320 313
    public boolean areSettingsValid() {
321 314
        return true;
322 315
    }
323
    
316

  
324 317
}

Also available in: Unified diff