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 @ 42896
History | View | Annotate | Download (10.2 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.gvsig.andami.IconThemeHelper; |
36 |
import org.gvsig.andami.PluginServices; |
37 |
import org.gvsig.andami.plugins.Extension; |
38 |
import org.gvsig.app.ApplicationLocator; |
39 |
import org.gvsig.app.ApplicationManager; |
40 |
import org.gvsig.app.addlayer.AddLayerDialog; |
41 |
import org.gvsig.app.gui.WizardPanel; |
42 |
import org.gvsig.app.project.documents.view.ViewDocument; |
43 |
import org.gvsig.app.project.documents.view.gui.IView; |
44 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerAddLayerWizardPanel; |
45 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel; |
46 |
import org.gvsig.fmap.mapcontext.MapContext; |
47 |
import org.gvsig.fmap.mapcontext.ViewPort; |
48 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
49 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
50 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
51 |
import org.gvsig.fmap.mapcontrol.MapControl; |
52 |
import org.gvsig.tools.ToolsLocator; |
53 |
import org.gvsig.tools.dispose.DisposeUtils; |
54 |
import org.gvsig.tools.i18n.I18nManager; |
55 |
import org.slf4j.Logger; |
56 |
import org.slf4j.LoggerFactory; |
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(FilesystemExplorerAddLayerWizardPanel.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 |
@Override
|
148 |
public void execute(String command, Object[] args) { |
149 |
List<File> files = null; |
150 |
if (args != null && args.length >= 1 && args[0] instanceof List) { |
151 |
files = (List<File>) args[0]; |
152 |
} |
153 |
ApplicationManager application = ApplicationLocator.getManager(); |
154 |
|
155 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
156 |
if (view == null) { |
157 |
return;
|
158 |
} |
159 |
ViewDocument document = view.getViewDocument(); |
160 |
|
161 |
MapControl mapControl = view.getMapControl(); |
162 |
this.doAddLayers(mapControl, mapControl.getMapContext(), files);
|
163 |
mapControl.getMapContext().callLegendChanged(); |
164 |
document.setModified(true);
|
165 |
} |
166 |
|
167 |
@Override
|
168 |
public boolean isEnabled() { |
169 |
return true; |
170 |
} |
171 |
|
172 |
/**
|
173 |
* Creates FOpenDialog, and adds file tab, and additional registered tabs
|
174 |
*
|
175 |
* @return FOpenDialog
|
176 |
*/
|
177 |
private AddLayerDialog createFOpenDialog(MapControl mapControl, MapContext mapContext, List<File> files) { |
178 |
ApplicationManager application = ApplicationLocator.getManager(); |
179 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
180 |
AddLayerDialog fopen = new AddLayerDialog();
|
181 |
for (Class<? extends WizardPanel> wpClass : wizardStack) { |
182 |
WizardPanel wp; |
183 |
try {
|
184 |
Object[] params = {}; |
185 |
wp = wpClass.getConstructor() |
186 |
.newInstance(params); |
187 |
application.message( |
188 |
i18nManager.getTranslation("Adding tab...") + wp.getTabName(),
|
189 |
JOptionPane.INFORMATION_MESSAGE
|
190 |
); |
191 |
wp.setMapCtrl(mapControl); |
192 |
wp.setMapContext(mapContext); |
193 |
wp.initWizard(); |
194 |
if (wp instanceof FilesystemExplorerWizardPanel && files != null && !files.isEmpty()) { |
195 |
FilesystemExplorerWizardPanel fswp = (FilesystemExplorerWizardPanel) wp; |
196 |
fswp.addFiles(files); |
197 |
} |
198 |
fopen.addWizardTab(wp.getTabName(), wp); |
199 |
} catch (Exception e) { |
200 |
logger.warn("Can't create layer open dialog.", e);
|
201 |
} |
202 |
} |
203 |
application.message(null,JOptionPane.INFORMATION_MESSAGE); |
204 |
fopen.updateOkButtonState(); |
205 |
return fopen;
|
206 |
} |
207 |
|
208 |
/**
|
209 |
* Adds to mapcontrol all layers selected by user in the specified
|
210 |
* WizardPanel.
|
211 |
*
|
212 |
* @param mapControl MapControl on which we want to load user selected
|
213 |
* layers.
|
214 |
* @param wizardPanel WizardPanel where user selected the layers to load
|
215 |
* @return
|
216 |
*/
|
217 |
// private boolean loadGenericWizardPanelLayers(MapContext mapContext, WizardPanel wp, boolean mapControlAvailable) {
|
218 |
// wp.setMapContext(mapContext);
|
219 |
// wp.executeWizard();
|
220 |
// return true;
|
221 |
// }
|
222 |
/**
|
223 |
* Opens the AddLayer dialog, and adds the selected layers to the provided
|
224 |
* MapContext.
|
225 |
*
|
226 |
* This method is useful when we want to add a layer but we don't have an
|
227 |
* associated View. If a View or a MapControl is available, you will usually
|
228 |
* prefer the {@link #addLayers(MapControl)} method.
|
229 |
*
|
230 |
* @param mapContext The MapContext to add the layers to
|
231 |
* @return <code>true</code> if any layer has been added.
|
232 |
*/
|
233 |
public boolean addLayers(MapContext mapContext) { |
234 |
return doAddLayers(null, mapContext, null); |
235 |
} |
236 |
|
237 |
/**
|
238 |
* Opens the AddLayer dialog, and adds the selected layers to the provided
|
239 |
* mapControl.
|
240 |
*
|
241 |
* @param mapControl The MapControl to add the layers to
|
242 |
*
|
243 |
* @return <code>true</code> if any layer has been added.
|
244 |
*/
|
245 |
public boolean addLayers(MapControl mapControl) { |
246 |
return doAddLayers(mapControl, mapControl.getMapContext(), null); |
247 |
} |
248 |
|
249 |
private boolean doAddLayers(MapControl mapControl, MapContext mapContext, List<File> files) { |
250 |
AddLayerDialog fopen = null;
|
251 |
try {
|
252 |
fopen = createFOpenDialog(mapControl, mapContext, files); |
253 |
PluginServices.getMDIManager().addWindow(fopen); |
254 |
|
255 |
if (fopen.isAccepted()) {
|
256 |
if (fopen.getSelectedTab() instanceof WizardPanel) { |
257 |
WizardPanel wp = (WizardPanel) fopen.getSelectedTab(); |
258 |
wp.execute(); |
259 |
return true; |
260 |
} else {
|
261 |
JOptionPane.showMessageDialog((Component) PluginServices |
262 |
.getMainFrame(), PluginServices.getText(this, "ninguna_capa_seleccionada")); |
263 |
} |
264 |
} |
265 |
return false; |
266 |
} finally {
|
267 |
DisposeUtils.disposeQuietly(fopen); |
268 |
} |
269 |
} |
270 |
|
271 |
@Override
|
272 |
public void initialize() { |
273 |
IconThemeHelper.registerIcon("action", "view-layer-add", this); |
274 |
} |
275 |
} |