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 @ 47799
History | View | Annotate | Download (12 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.awt.Dimension; |
27 |
import java.io.File; |
28 |
import java.lang.reflect.InvocationTargetException; |
29 |
import java.util.ArrayList; |
30 |
import java.util.List; |
31 |
import javax.swing.JOptionPane; |
32 |
import org.cresques.cts.ICoordTrans; |
33 |
import org.cresques.cts.IProjection; |
34 |
import org.gvsig.andami.IconThemeHelper; |
35 |
import org.gvsig.andami.PluginServices; |
36 |
import org.gvsig.andami.plugins.Extension; |
37 |
import org.gvsig.app.ApplicationLocator; |
38 |
import org.gvsig.app.ApplicationManager; |
39 |
import org.gvsig.app.addlayer.AddLayerDialog; |
40 |
import org.gvsig.app.gui.WizardPanel; |
41 |
import org.gvsig.app.project.documents.view.ViewDocument; |
42 |
import org.gvsig.app.project.documents.view.gui.IView; |
43 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel; |
44 |
import org.gvsig.fmap.mapcontext.MapContext; |
45 |
import org.gvsig.fmap.mapcontext.ViewPort; |
46 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
47 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
48 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
49 |
import org.gvsig.fmap.mapcontrol.MapControl; |
50 |
import org.gvsig.tools.ToolsLocator; |
51 |
import org.gvsig.tools.arguments.Arguments; |
52 |
import org.gvsig.tools.dispose.DisposeUtils; |
53 |
import org.gvsig.tools.i18n.I18nManager; |
54 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
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(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 |
@Override
|
148 |
public void execute(String command, Object[] args) { |
149 |
Arguments arguments = Arguments.create(args); |
150 |
|
151 |
|
152 |
List<File> files = arguments.getListOf(0,File.class); |
153 |
|
154 |
ApplicationManager application = ApplicationLocator.getManager(); |
155 |
|
156 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
157 |
if (view == null) { |
158 |
return;
|
159 |
} |
160 |
ViewDocument document = view.getViewDocument(); |
161 |
|
162 |
MapControl mapControl = view.getMapControl(); |
163 |
this.doAddLayers(mapControl, mapControl.getMapContext(), files, arguments);
|
164 |
mapControl.getMapContext().callLegendChanged(); |
165 |
document.setModified(true);
|
166 |
} |
167 |
|
168 |
@Override
|
169 |
public boolean isEnabled() { |
170 |
return true; |
171 |
} |
172 |
|
173 |
public static AddLayerDialog createAddLayerDialog(MapControl mapControl, MapContext mapContext, List<File> files) { |
174 |
return createAddLayerDialog(mapControl, mapContext, files, null); |
175 |
} |
176 |
|
177 |
/**
|
178 |
* Creates FOpenDialog, and adds file tab, and additional registered tabs
|
179 |
*
|
180 |
* @param mapControl
|
181 |
* @param mapContext
|
182 |
* @param files
|
183 |
* @param args
|
184 |
* @return FOpenDialog
|
185 |
*/
|
186 |
public static AddLayerDialog createAddLayerDialog(MapControl mapControl, MapContext mapContext, List<File> files, Arguments args) { |
187 |
ApplicationManager application = ApplicationLocator.getManager(); |
188 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
189 |
AddLayerDialog fopen = new AddLayerDialog();
|
190 |
Dimension preferredSize = fopen.getPreferredSize();
|
191 |
String activeTab = null; |
192 |
for (Class<? extends WizardPanel> wpClass : wizardStack) { |
193 |
WizardPanel wp; |
194 |
try {
|
195 |
Object[] params = {}; |
196 |
wp = wpClass.getConstructor() |
197 |
.newInstance(params); |
198 |
application.message( |
199 |
i18nManager.getTranslation("Adding tab...") + wp.getTabName(),
|
200 |
JOptionPane.INFORMATION_MESSAGE
|
201 |
); |
202 |
wp.setMapCtrl(mapControl); |
203 |
wp.setMapContext(mapContext); |
204 |
wp.initWizard(); |
205 |
if (wp instanceof FilesystemExplorerWizardPanel && files != null && !files.isEmpty()) { |
206 |
FilesystemExplorerWizardPanel fswp = (FilesystemExplorerWizardPanel) wp; |
207 |
fswp.addFiles(files); |
208 |
activeTab = wp.getTabName(); |
209 |
} |
210 |
if(wp.addArguments(args)) {
|
211 |
activeTab = wp.getTabName(); |
212 |
} |
213 |
Dimension dim = wp.getPreferredSize();
|
214 |
if( dim.height>preferredSize.height ) {
|
215 |
preferredSize.height = dim.height; |
216 |
} |
217 |
if( dim.width>preferredSize.width ) {
|
218 |
preferredSize.width = dim.width; |
219 |
} |
220 |
fopen.addWizardTab(wp.getTabName(), wp); |
221 |
} catch (Exception e) { |
222 |
logger.warn("Can't create layer open dialog.", e);
|
223 |
} |
224 |
} |
225 |
|
226 |
if(args != null && args.contains("activateTab")){ |
227 |
activeTab = (String)args.get("activateTab"); |
228 |
} |
229 |
|
230 |
fopen.setSelectedTab(activeTab); |
231 |
preferredSize.height += ToolsSwingUtils.rows2px(4);
|
232 |
preferredSize.width += ToolsSwingUtils.cols2px(2);
|
233 |
|
234 |
fopen.setPreferredSize(preferredSize); |
235 |
|
236 |
ToolsSwingUtils.ensureRowsCols(fopen, -1, -1, 30, 100); |
237 |
|
238 |
application.message(null,JOptionPane.INFORMATION_MESSAGE); |
239 |
fopen.updateOkButtonState(); |
240 |
return fopen;
|
241 |
} |
242 |
|
243 |
/**
|
244 |
* Opens the AddLayer dialog, and adds the selected layers to the provided
|
245 |
* MapContext.
|
246 |
*
|
247 |
* This method is useful when we want to add a layer but we don't have an
|
248 |
* associated View. If a View or a MapControl is available, you will usually
|
249 |
* prefer the {@link #addLayers(MapControl)} method.
|
250 |
*
|
251 |
* @param mapContext The MapContext to add the layers to
|
252 |
* @return <code>true</code> if any layer has been added.
|
253 |
*/
|
254 |
public boolean addLayers(MapContext mapContext) { |
255 |
return doAddLayers(null, mapContext, null); |
256 |
} |
257 |
|
258 |
/**
|
259 |
* Opens the AddLayer dialog, and adds the selected layers to the provided
|
260 |
* mapControl.
|
261 |
*
|
262 |
* @param mapControl The MapControl to add the layers to
|
263 |
*
|
264 |
* @return <code>true</code> if any layer has been added.
|
265 |
*/
|
266 |
public boolean addLayers(MapControl mapControl) { |
267 |
return doAddLayers(mapControl, mapControl.getMapContext(), null, false, null); |
268 |
} |
269 |
|
270 |
public boolean addLayers(MapControl mapControl, boolean wait) { |
271 |
return doAddLayers(mapControl, mapControl.getMapContext(), null, wait, null); |
272 |
} |
273 |
|
274 |
private boolean doAddLayers(MapControl mapControl, MapContext mapContext, List<File> files) { |
275 |
return doAddLayers(mapControl, mapContext, files, null); |
276 |
} |
277 |
|
278 |
private boolean doAddLayers(MapControl mapControl, MapContext mapContext, List<File> files, Arguments args) { |
279 |
return doAddLayers(mapControl, mapContext, files, false, args); |
280 |
} |
281 |
|
282 |
private boolean doAddLayers(MapControl mapControl, MapContext mapContext, List<File> files, boolean wait, Arguments args) { |
283 |
AddLayerDialog addLayerDialog = null;
|
284 |
try {
|
285 |
addLayerDialog = createAddLayerDialog(mapControl, mapContext, files, args); |
286 |
Dimension screensize = ToolsSwingUtils.getCurrentScreenSize();
|
287 |
ToolsSwingUtils.ensureHeightWitdh( |
288 |
addLayerDialog, |
289 |
3*(screensize.height/4), |
290 |
2*(screensize.width/4), |
291 |
7*(screensize.height/8), |
292 |
3*(screensize.width/4) |
293 |
); |
294 |
PluginServices.getMDIManager().addWindow(addLayerDialog); |
295 |
|
296 |
if (addLayerDialog.isAccepted()) {
|
297 |
if (addLayerDialog.getSelectedTab() instanceof WizardPanel) { |
298 |
WizardPanel panel = (WizardPanel) addLayerDialog.getSelectedTab(); |
299 |
panel.executeWizard(); |
300 |
if(wait) {
|
301 |
panel.waitTasks(0);
|
302 |
} |
303 |
return true; |
304 |
} else {
|
305 |
JOptionPane.showMessageDialog((Component) PluginServices |
306 |
.getMainFrame(), PluginServices.getText(this, "ninguna_capa_seleccionada")); |
307 |
} |
308 |
} |
309 |
return false; |
310 |
} finally {
|
311 |
DisposeUtils.disposeQuietly(addLayerDialog); |
312 |
} |
313 |
} |
314 |
|
315 |
@Override
|
316 |
public void initialize() { |
317 |
IconThemeHelper.registerIcon("action", "view-layer-add", this); |
318 |
} |
319 |
} |