svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / AddLayer.java @ 43152
History | View | Annotate | Download (11.9 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 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 |
*
|
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 |
*
|
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.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.app.extension; |
24 |
|
25 |
import java.awt.Component; |
26 |
import java.io.File; |
27 |
import java.lang.reflect.InvocationTargetException; |
28 |
import java.util.ArrayList; |
29 |
import java.util.List; |
30 |
|
31 |
import javax.swing.JOptionPane; |
32 |
|
33 |
import org.cresques.cts.ICoordTrans; |
34 |
import org.cresques.cts.IProjection; |
35 |
import org.slf4j.Logger; |
36 |
import org.slf4j.LoggerFactory; |
37 |
|
38 |
import org.gvsig.andami.IconThemeHelper; |
39 |
import org.gvsig.andami.PluginServices; |
40 |
import org.gvsig.andami.plugins.Extension; |
41 |
import org.gvsig.app.ApplicationLocator; |
42 |
import org.gvsig.app.ApplicationManager; |
43 |
import org.gvsig.app.addlayer.AddLayerDialog; |
44 |
import org.gvsig.app.gui.WizardPanel; |
45 |
import org.gvsig.app.project.documents.view.ViewDocument; |
46 |
import org.gvsig.app.project.documents.view.gui.IView; |
47 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel; |
48 |
import org.gvsig.fmap.mapcontext.MapContext; |
49 |
import org.gvsig.fmap.mapcontext.ViewPort; |
50 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
51 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
52 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
53 |
import org.gvsig.fmap.mapcontrol.MapControl; |
54 |
import org.gvsig.tools.ToolsLocator; |
55 |
import org.gvsig.tools.dispose.DisposeUtils; |
56 |
import org.gvsig.tools.i18n.I18nManager; |
57 |
|
58 |
/**
|
59 |
* Extensi�n que abre un di�logo para seleccionar la capa o capas que se
|
60 |
* quieren anadir a la vista.
|
61 |
*
|
62 |
*/
|
63 |
public class AddLayer extends Extension { |
64 |
|
65 |
private static final Logger logger = LoggerFactory.getLogger(AddLayer.class); |
66 |
|
67 |
private static ArrayList<Class<? extends WizardPanel>> wizardStack = null; |
68 |
|
69 |
static {
|
70 |
AddLayer.wizardStack = new ArrayList<>(); |
71 |
// Anadimos el panel al wizard de cargar capa.
|
72 |
AddLayer.addWizard(FilesystemExplorerWizardPanel.class); |
73 |
} |
74 |
|
75 |
public static void addWizard(Class<? extends WizardPanel> wpClass) { |
76 |
AddLayer.wizardStack.add(wpClass); |
77 |
} |
78 |
|
79 |
private static WizardPanel getInstance(int i, MapControl mapControl) |
80 |
throws IllegalArgumentException, SecurityException, |
81 |
InstantiationException, IllegalAccessException, |
82 |
InvocationTargetException, NoSuchMethodException { |
83 |
Class<? extends WizardPanel> wpClass = AddLayer.wizardStack.get(i); |
84 |
Object[] params = {}; |
85 |
WizardPanel wp = wpClass.getConstructor() |
86 |
.newInstance(params); |
87 |
wp.setMapCtrl(mapControl); |
88 |
wp.initWizard(); |
89 |
|
90 |
return wp;
|
91 |
} |
92 |
|
93 |
@Override
|
94 |
public boolean isVisible() { |
95 |
ApplicationManager application = ApplicationLocator.getManager(); |
96 |
|
97 |
return application.getActiveComponent(ViewDocument.class) != null; |
98 |
} |
99 |
|
100 |
@Override
|
101 |
public void postInitialize() { |
102 |
super.postInitialize();
|
103 |
} |
104 |
|
105 |
public static void checkProjection(FLayer lyr, ViewPort viewPort) { |
106 |
if (lyr instanceof FLayers) { |
107 |
FLayers layers = (FLayers) lyr; |
108 |
for (int i = 0; i < layers.getLayersCount(); i++) { |
109 |
checkProjection(layers.getLayer(i), viewPort); |
110 |
} |
111 |
} |
112 |
if (lyr instanceof FLyrVect) { |
113 |
FLyrVect lyrVect = (FLyrVect) lyr; |
114 |
IProjection proj = lyr.getProjection(); |
115 |
// Comprobar que la projecci�n es la misma que la vista
|
116 |
if (proj == null) { |
117 |
// SUPONEMOS que la capa est� en la proyecci�n que
|
118 |
// estamos pidiendo (que ya es mucho suponer, ya).
|
119 |
lyrVect.setProjection(viewPort.getProjection()); |
120 |
return;
|
121 |
} |
122 |
int option;
|
123 |
if (proj != viewPort.getProjection()) {
|
124 |
option = JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(), PluginServices |
125 |
.getText(AddLayer.class, "reproyectar_aviso") + "\n" + PluginServices.getText(AddLayer.class, "Capa") + ": " + lyrVect.getName(), PluginServices |
126 |
.getText(AddLayer.class, "reproyectar_pregunta"),
|
127 |
JOptionPane.YES_NO_OPTION);
|
128 |
|
129 |
if (option != JOptionPane.OK_OPTION) { |
130 |
return;
|
131 |
} |
132 |
|
133 |
ICoordTrans ct = proj.getCT(viewPort.getProjection()); |
134 |
lyrVect.setCoordTrans(ct); |
135 |
System.err.println("coordTrans = " + proj.getAbrev() + " " |
136 |
+ viewPort.getProjection().getAbrev()); |
137 |
} |
138 |
} |
139 |
|
140 |
} |
141 |
|
142 |
@Override
|
143 |
public void execute(String actionCommand) { |
144 |
this.execute(actionCommand, null); |
145 |
} |
146 |
|
147 |
public static class ArrayUtils { |
148 |
|
149 |
// FIXME: Use the class in org.gvsig.tools when this is available and remove this.
|
150 |
|
151 |
/**
|
152 |
* Return the element of the array. If the array is null or don't have
|
153 |
* the element indicated return null.
|
154 |
*
|
155 |
* @param array
|
156 |
* @param n
|
157 |
* @return
|
158 |
*/
|
159 |
public static Object get(Object[] array, int n) { |
160 |
if (array == null) { |
161 |
return null; |
162 |
} |
163 |
if (array.length <= n || n < 0) { |
164 |
return null; |
165 |
} |
166 |
return array[n];
|
167 |
} |
168 |
|
169 |
/**
|
170 |
* Return the element of the array if exists and if of the type
|
171 |
* indicated. If the array is null, don't have the element indicated of
|
172 |
* is not of the type required return null.
|
173 |
*
|
174 |
* @param array
|
175 |
* @param n
|
176 |
* @param elementClass
|
177 |
* @return
|
178 |
*/
|
179 |
public static Object get(Object[] array, int n, Class elementClass) { |
180 |
Object o = get(array, n);
|
181 |
if (elementClass.isInstance(o)) {
|
182 |
return o;
|
183 |
} |
184 |
return null; |
185 |
} |
186 |
|
187 |
/**
|
188 |
* Return the element of the array if this is a list of the indiqued
|
189 |
* type. If the array don't have elements, is null, not is a list or the
|
190 |
* elements of the list is not the indicated return null.
|
191 |
*
|
192 |
* @param array
|
193 |
* @param n
|
194 |
* @param elementClass
|
195 |
* @return
|
196 |
*/
|
197 |
public static List getListOf(Object[] array, int n, Class elementClass) { |
198 |
List list = (List) get(array, n, List.class); |
199 |
if (list == null) { |
200 |
return null; |
201 |
} |
202 |
if (list.isEmpty()) {
|
203 |
return null; |
204 |
} |
205 |
if (elementClass.isInstance(list.get(0))) { |
206 |
return list;
|
207 |
} |
208 |
return null; |
209 |
} |
210 |
|
211 |
} |
212 |
|
213 |
@Override
|
214 |
public void execute(String command, Object[] args) { |
215 |
List<File> files = ArrayUtils.getListOf(args,0,File.class); |
216 |
|
217 |
ApplicationManager application = ApplicationLocator.getManager(); |
218 |
|
219 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
220 |
if (view == null) { |
221 |
return;
|
222 |
} |
223 |
ViewDocument document = view.getViewDocument(); |
224 |
|
225 |
MapControl mapControl = view.getMapControl(); |
226 |
this.doAddLayers(mapControl, mapControl.getMapContext(), files);
|
227 |
mapControl.getMapContext().callLegendChanged(); |
228 |
document.setModified(true);
|
229 |
} |
230 |
|
231 |
@Override
|
232 |
public boolean isEnabled() { |
233 |
return true; |
234 |
} |
235 |
|
236 |
/**
|
237 |
* Creates FOpenDialog, and adds file tab, and additional registered tabs
|
238 |
*
|
239 |
* @return FOpenDialog
|
240 |
*/
|
241 |
private AddLayerDialog createFOpenDialog(MapControl mapControl, MapContext mapContext, List<File> files) { |
242 |
ApplicationManager application = ApplicationLocator.getManager(); |
243 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
244 |
AddLayerDialog fopen = new AddLayerDialog();
|
245 |
for (Class<? extends WizardPanel> wpClass : wizardStack) { |
246 |
WizardPanel wp; |
247 |
try {
|
248 |
Object[] params = {}; |
249 |
wp = wpClass.getConstructor() |
250 |
.newInstance(params); |
251 |
application.message( |
252 |
i18nManager.getTranslation("Adding tab...") + wp.getTabName(),
|
253 |
JOptionPane.INFORMATION_MESSAGE
|
254 |
); |
255 |
wp.setMapCtrl(mapControl); |
256 |
wp.setMapContext(mapContext); |
257 |
wp.initWizard(); |
258 |
if (wp instanceof FilesystemExplorerWizardPanel && files != null && !files.isEmpty()) { |
259 |
FilesystemExplorerWizardPanel fswp = (FilesystemExplorerWizardPanel) wp; |
260 |
fswp.addFiles(files); |
261 |
} |
262 |
fopen.addWizardTab(wp.getTabName(), wp); |
263 |
} catch (Exception e) { |
264 |
logger.warn("Can't create layer open dialog.", e);
|
265 |
} |
266 |
} |
267 |
application.message(null,JOptionPane.INFORMATION_MESSAGE); |
268 |
fopen.updateOkButtonState(); |
269 |
return fopen;
|
270 |
} |
271 |
|
272 |
/**
|
273 |
* Adds to mapcontrol all layers selected by user in the specified
|
274 |
* WizardPanel.
|
275 |
*
|
276 |
* @param mapControl MapControl on which we want to load user selected
|
277 |
* layers.
|
278 |
* @param wizardPanel WizardPanel where user selected the layers to load
|
279 |
* @return
|
280 |
*/
|
281 |
// private boolean loadGenericWizardPanelLayers(MapContext mapContext, WizardPanel wp, boolean mapControlAvailable) {
|
282 |
// wp.setMapContext(mapContext);
|
283 |
// wp.executeWizard();
|
284 |
// return true;
|
285 |
// }
|
286 |
/**
|
287 |
* Opens the AddLayer dialog, and adds the selected layers to the provided
|
288 |
* MapContext.
|
289 |
*
|
290 |
* This method is useful when we want to add a layer but we don't have an
|
291 |
* associated View. If a View or a MapControl is available, you will usually
|
292 |
* prefer the {@link #addLayers(MapControl)} method.
|
293 |
*
|
294 |
* @param mapContext The MapContext to add the layers to
|
295 |
* @return <code>true</code> if any layer has been added.
|
296 |
*/
|
297 |
public boolean addLayers(MapContext mapContext) { |
298 |
return doAddLayers(null, mapContext, null); |
299 |
} |
300 |
|
301 |
/**
|
302 |
* Opens the AddLayer dialog, and adds the selected layers to the provided
|
303 |
* mapControl.
|
304 |
*
|
305 |
* @param mapControl The MapControl to add the layers to
|
306 |
*
|
307 |
* @return <code>true</code> if any layer has been added.
|
308 |
*/
|
309 |
public boolean addLayers(MapControl mapControl) { |
310 |
return doAddLayers(mapControl, mapControl.getMapContext(), null); |
311 |
} |
312 |
|
313 |
private boolean doAddLayers(MapControl mapControl, MapContext mapContext, List<File> files) { |
314 |
AddLayerDialog fopen = null;
|
315 |
try {
|
316 |
fopen = createFOpenDialog(mapControl, mapContext, files); |
317 |
PluginServices.getMDIManager().addWindow(fopen); |
318 |
|
319 |
if (fopen.isAccepted()) {
|
320 |
if (fopen.getSelectedTab() instanceof WizardPanel) { |
321 |
WizardPanel wp = (WizardPanel) fopen.getSelectedTab(); |
322 |
wp.execute(); |
323 |
return true; |
324 |
} else {
|
325 |
JOptionPane.showMessageDialog((Component) PluginServices |
326 |
.getMainFrame(), PluginServices.getText(this, "ninguna_capa_seleccionada")); |
327 |
} |
328 |
} |
329 |
return false; |
330 |
} finally {
|
331 |
DisposeUtils.disposeQuietly(fopen); |
332 |
} |
333 |
} |
334 |
|
335 |
@Override
|
336 |
public void initialize() { |
337 |
IconThemeHelper.registerIcon("action", "view-layer-add", this); |
338 |
} |
339 |
} |