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 / ViewControls.java @ 47237
History | View | Annotate | Download (19.3 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.util.ArrayList; |
26 |
import java.util.Collections; |
27 |
import java.util.List; |
28 |
import javax.swing.JOptionPane; |
29 |
import org.gvsig.andami.IconThemeHelper; |
30 |
import org.gvsig.andami.PluginServices; |
31 |
import org.gvsig.andami.plugins.Extension; |
32 |
import org.gvsig.andami.preferences.IPreference; |
33 |
import org.gvsig.andami.preferences.IPreferenceExtension; |
34 |
import org.gvsig.app.ApplicationLocator; |
35 |
import org.gvsig.app.ApplicationManager; |
36 |
import org.gvsig.app.gui.preferencespage.ViewPage; |
37 |
import org.gvsig.app.gui.preferencespage.dal.DALPage; |
38 |
import org.gvsig.app.gui.preferencespage.databaseworkspace.DatabaseWorkspacePage; |
39 |
import org.gvsig.app.gui.preferencespage.dialogreminder.DialogReminderPage; |
40 |
import org.gvsig.app.project.Project; |
41 |
import org.gvsig.app.project.ProjectManager; |
42 |
import org.gvsig.app.project.documents.view.Encuadrator; |
43 |
import org.gvsig.app.project.documents.view.ViewDocument; |
44 |
import org.gvsig.app.project.documents.view.ViewManager; |
45 |
import org.gvsig.app.project.documents.view.gui.ExtentListSelectorModel; |
46 |
import org.gvsig.app.project.documents.view.gui.FPanelExtentSelector; |
47 |
import org.gvsig.app.project.documents.view.gui.FPanelLocConfig; |
48 |
import org.gvsig.app.project.documents.view.gui.IView; |
49 |
import org.gvsig.app.project.documents.view.toc.gui.FPopupMenu; |
50 |
import org.gvsig.fmap.geom.primitive.Envelope; |
51 |
import org.gvsig.fmap.mapcontext.MapContext; |
52 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
53 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
54 |
import org.gvsig.fmap.mapcontrol.MapControl; |
55 |
import org.gvsig.tools.ToolsLocator; |
56 |
import org.gvsig.tools.dataTypes.DataTypes; |
57 |
import org.slf4j.Logger; |
58 |
import org.slf4j.LoggerFactory; |
59 |
|
60 |
/**
|
61 |
* Extensi?n que controla las operaciones b?sicas realizadas sobre la vista.
|
62 |
*
|
63 |
*/
|
64 |
public class ViewControls extends Extension implements IPreferenceExtension { |
65 |
|
66 |
private static final Logger logger = LoggerFactory |
67 |
.getLogger(ViewInvertSelection.class); |
68 |
|
69 |
private static ViewPage viewPropertiesPage = null; |
70 |
|
71 |
public void execute(String s) { |
72 |
execute(s, null);
|
73 |
} |
74 |
|
75 |
public void execute(String command, Object[] args) { |
76 |
ApplicationManager application = ApplicationLocator.getManager(); |
77 |
|
78 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
79 |
if (view == null) { |
80 |
return;
|
81 |
} |
82 |
ViewDocument document = view.getViewDocument(); |
83 |
|
84 |
MapContext mapa = document.getMapContext(); |
85 |
MapControl mapCtrl = view.getMapControl(); |
86 |
|
87 |
if (command.equalsIgnoreCase("view-navigation-zoom-all")) { |
88 |
Envelope all = mapa.getLayers().getFullEnvelope(); |
89 |
if (all != null && !all.isEmpty()) { |
90 |
mapa.getViewPort().setEnvelope(all); |
91 |
document.setModified(true);
|
92 |
} |
93 |
|
94 |
} else if (command.equalsIgnoreCase("view-navigation-frame-manager")) { |
95 |
FPanelExtentSelector l = new FPanelExtentSelector();
|
96 |
|
97 |
ProjectExtension p = (ProjectExtension) PluginServices.getExtension(org.gvsig.app.extension.ProjectExtension.class); |
98 |
Project project = p.getProject(); |
99 |
ExtentListSelectorModel modelo = new ExtentListSelectorModel(project);
|
100 |
project.addPropertyChangeListener(modelo); |
101 |
l.setModel(modelo); |
102 |
l.addSelectionListener(new Encuadrator(project, mapa, view));
|
103 |
document.setModified(true);
|
104 |
PluginServices.getMDIManager().addWindow(l); |
105 |
|
106 |
} else if (command.equalsIgnoreCase("view-locator-setup")) { |
107 |
FPanelLocConfig m_panelLoc = new FPanelLocConfig(view.getMapOverview());
|
108 |
PluginServices.getMDIManager().addCentredWindow(m_panelLoc); |
109 |
document.setModified(true);
|
110 |
|
111 |
} else if (command.equalsIgnoreCase("view-navigation-pan")) { |
112 |
mapCtrl.setTool("pan");
|
113 |
document.setModified(true);
|
114 |
|
115 |
} else if (command.equalsIgnoreCase("view-navigation-zoom-in-topoint")) { |
116 |
mapCtrl.setTool("zoomIn");
|
117 |
document.setModified(true);
|
118 |
|
119 |
} else if (command.equalsIgnoreCase("view-navigation-zoom-out-topoint")) { |
120 |
mapCtrl.setTool("zoomOut");
|
121 |
document.setModified(true);
|
122 |
|
123 |
} else if (command.equalsIgnoreCase("layer-set-visible")) { |
124 |
setVisibles(true, mapa.getLayers());
|
125 |
document.setModified(true);
|
126 |
application.refreshMenusAndToolBars(); |
127 |
|
128 |
} else if (command.equalsIgnoreCase("active-layer-set-hide")) { |
129 |
setActivesVisibles(false, mapa.getLayers());
|
130 |
document.setModified(true);
|
131 |
application.refreshMenusAndToolBars(); |
132 |
|
133 |
} else if (command.equalsIgnoreCase("active-layer-set-visible")) { |
134 |
setActivesVisibles(true, mapa.getLayers());
|
135 |
document.setModified(true);
|
136 |
application.refreshMenusAndToolBars(); |
137 |
|
138 |
} else if (command.equalsIgnoreCase("active-layer-bring-front")) { |
139 |
FLayers layers = mapa.getLayers(); |
140 |
List<FLayer> orderedLayers = bringToFront(layers);
|
141 |
if(orderedLayers != null){ |
142 |
layers.clearLayers(); |
143 |
layers.addAll(orderedLayers); |
144 |
} |
145 |
|
146 |
document.setModified(true);
|
147 |
application.refreshMenusAndToolBars(); |
148 |
|
149 |
} else if (command.equalsIgnoreCase("active-layer-send-back")) { |
150 |
FLayers layers = mapa.getLayers(); |
151 |
List<FLayer> orderedLayers = sendToBack(layers);
|
152 |
if(orderedLayers != null){ |
153 |
layers.clearLayers(); |
154 |
layers.addAll(orderedLayers); |
155 |
} |
156 |
document.setModified(true);
|
157 |
application.refreshMenusAndToolBars(); |
158 |
|
159 |
} else if (command.equalsIgnoreCase("active-layer-move-ahead")) { |
160 |
moveAhead(mapa.getLayers()); |
161 |
document.setModified(true);
|
162 |
application.refreshMenusAndToolBars(); |
163 |
|
164 |
} else if (command.equalsIgnoreCase("active-layer-move-behind")) { |
165 |
moveBehind(mapa.getLayers()); |
166 |
document.setModified(true);
|
167 |
application.refreshMenusAndToolBars(); |
168 |
|
169 |
} else if (command.equalsIgnoreCase("layer-set-hide")) { |
170 |
setVisibles(false, mapa.getLayers());
|
171 |
document.setModified(true);
|
172 |
application.refreshMenusAndToolBars(); |
173 |
|
174 |
} else if (command.equalsIgnoreCase("layer-set-active")) { |
175 |
setActives(true, mapa.getLayers());
|
176 |
document.setModified(true);
|
177 |
application.refreshMenusAndToolBars(); |
178 |
|
179 |
} else if (command.equalsIgnoreCase("layer-set-inactive")) { |
180 |
setActives(false, mapa.getLayers());
|
181 |
document.setModified(true);
|
182 |
application.refreshMenusAndToolBars(); |
183 |
|
184 |
} else if (command.equalsIgnoreCase("group-set-active-all-layers")) { |
185 |
for (FLayer active : mapa.getLayers().getActives()) {
|
186 |
if(active instanceof FLayers){ |
187 |
((FLayers) active).setActive(true, true); |
188 |
} |
189 |
} |
190 |
document.setModified(true);
|
191 |
application.refreshMenusAndToolBars(); |
192 |
|
193 |
} else if (command.equalsIgnoreCase("group-set-inactive-all-layers")) { |
194 |
for (FLayer active : mapa.getLayers().getActives()) {
|
195 |
if(active instanceof FLayers){ |
196 |
((FLayers) active).setActive(false, true); |
197 |
((FLayers) active).setActive(true, false); |
198 |
} |
199 |
} |
200 |
document.setModified(true);
|
201 |
application.refreshMenusAndToolBars(); |
202 |
|
203 |
} else if (command.equalsIgnoreCase("view-navigation-zoom-in-center")) { |
204 |
mapCtrl.zoomIn(); |
205 |
document.setModified(true);
|
206 |
|
207 |
} else if (command.equalsIgnoreCase("view-navigation-zoom-out-center")) { |
208 |
mapCtrl.zoomOut(); |
209 |
document.setModified(true);
|
210 |
|
211 |
} else if (command.equalsIgnoreCase("view-change-scale")) { |
212 |
try {
|
213 |
Long newscale = (Long) ToolsLocator.getDataTypesManager().coerce(DataTypes.LONG, args[0]); |
214 |
long prevscale = mapa.getScaleView();
|
215 |
mapa.setScaleView(newscale); |
216 |
if(prevscale != newscale) {
|
217 |
document.setModified(true);
|
218 |
} |
219 |
} catch (Throwable ex) { |
220 |
logger.info("Can't change scale of view.", ex);
|
221 |
application.message("Can't change scale of view.", JOptionPane.ERROR_MESSAGE); |
222 |
} |
223 |
} |
224 |
} |
225 |
|
226 |
private ViewPage getViewPropertiesPage() {
|
227 |
if (viewPropertiesPage == null) { |
228 |
viewPropertiesPage = new ViewPage();
|
229 |
} |
230 |
return viewPropertiesPage;
|
231 |
} |
232 |
|
233 |
/**
|
234 |
* Pone todas las capas visibles o no visibles.
|
235 |
*
|
236 |
* @param visible true si que quieren poner a visibles.
|
237 |
* @param mapa FMap sobre el que actuar.
|
238 |
*/
|
239 |
private void setVisibles(boolean visible, FLayers layers) { |
240 |
int layerCount = layers.getLayersCount();
|
241 |
for (int i = 0; i < layerCount; i++) { |
242 |
FLayer layer = layers.getLayer(i); |
243 |
layer.setVisible(visible); |
244 |
if (layer instanceof FLayers) { |
245 |
setVisibles(visible, (FLayers) layer); |
246 |
} |
247 |
} |
248 |
} |
249 |
|
250 |
public static void setActivesVisibles(boolean visible, FLayers layers) { |
251 |
int layerCount = layers.getLayersCount();
|
252 |
for (int i = 0; i < layerCount; i++) { |
253 |
FLayer layer = layers.getLayer(i); |
254 |
if (layer.isActive()) {
|
255 |
layer.setVisible(visible); |
256 |
} |
257 |
if (layer instanceof FLayers) { |
258 |
setActivesVisibles(visible, (FLayers) layer); |
259 |
} |
260 |
} |
261 |
} |
262 |
|
263 |
/**
|
264 |
* Pone todas las capas activas o no activas.
|
265 |
*
|
266 |
* @param active true si que quieren poner a activas.
|
267 |
* @param mapa FMap sobre el que actuar.
|
268 |
*/
|
269 |
private void setActives(boolean active, FLayers layers) { |
270 |
int layerCount = layers.getLayersCount();
|
271 |
for (int i = 0; i < layerCount; i++) { |
272 |
FLayer layer = layers.getLayer(i); |
273 |
layer.setActive(active); |
274 |
if (layer instanceof FLayers) { |
275 |
setActives(active, (FLayers) layer); |
276 |
} |
277 |
} |
278 |
} |
279 |
|
280 |
public boolean isVisible() { |
281 |
ApplicationManager application = ApplicationLocator.getManager(); |
282 |
|
283 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
284 |
if (view == null) { |
285 |
return false; |
286 |
} |
287 |
ViewDocument document = view.getViewDocument(); |
288 |
MapContext mapa = document.getMapContext(); |
289 |
return mapa.getLayers().getLayersCount() > 0; |
290 |
} |
291 |
|
292 |
public void initialize() { |
293 |
registerIcons(); |
294 |
} |
295 |
|
296 |
public void postInitialize() { |
297 |
FPopupMenu.registerExtensionPoint(); |
298 |
ProjectManager projectManager = ApplicationLocator.getProjectManager(); |
299 |
ViewManager viewManager = (ViewManager) projectManager.getDocumentManager(ViewManager.TYPENAME); |
300 |
viewManager.addTOCContextAction("view-layer-add");
|
301 |
} |
302 |
|
303 |
private void registerIcons() { |
304 |
IconThemeHelper.registerIcon("action", "view-navigation-zoom-in-topoint", this); |
305 |
IconThemeHelper.registerIcon("action", "view-navigation-zoom-out-topoint", this); |
306 |
IconThemeHelper.registerIcon("action", "view-navigation-zoom-all", this); |
307 |
IconThemeHelper.registerIcon("action", "view-navigation-zoom-in-center", this); |
308 |
IconThemeHelper.registerIcon("action", "view-navigation-zoom-out-center", this); |
309 |
IconThemeHelper.registerIcon("action", "view-navigation-pan", this); |
310 |
IconThemeHelper.registerIcon("action", "view-navigation-frame-manager", this); |
311 |
|
312 |
IconThemeHelper.registerIcon("action", "edit-undo-redo-actions-modify", this); |
313 |
IconThemeHelper.registerIcon("action", "edit-undo-redo-actions-add", this); |
314 |
IconThemeHelper.registerIcon("action", "edit-undo-redo-actions-delete", this); |
315 |
|
316 |
IconThemeHelper.registerIcon("preferences", "dialog-remember-preferences", this); |
317 |
IconThemeHelper.registerIcon("preferences", "dal-preferences", this); |
318 |
IconThemeHelper.registerIcon("preferences", "databaseworkspace-preferences", this); |
319 |
} |
320 |
|
321 |
public boolean isEnabled() { |
322 |
ApplicationManager application = ApplicationLocator.getManager(); |
323 |
|
324 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
325 |
if (view == null) { |
326 |
return false; |
327 |
} |
328 |
ViewDocument document = view.getViewDocument(); |
329 |
MapContext mapa = document.getMapContext(); |
330 |
FLayers layers = mapa.getLayers(); |
331 |
for (int i = 0; i < layers.getLayersCount(); i++) { |
332 |
if (layers.getLayer(i).isAvailable()) {
|
333 |
return true; |
334 |
} |
335 |
} |
336 |
return false; |
337 |
} |
338 |
|
339 |
@Override
|
340 |
public IPreference[] getPreferencesPages() { |
341 |
List<IPreference> prefs = new ArrayList<>(); |
342 |
prefs.add(getViewPropertiesPage()); |
343 |
prefs.add(new DialogReminderPage());
|
344 |
prefs.add(new DALPage());
|
345 |
prefs.add(new DatabaseWorkspacePage());
|
346 |
|
347 |
return prefs.toArray(new IPreference[prefs.size()]); |
348 |
} |
349 |
|
350 |
private List<FLayer> bringToFront(FLayers groupLayers) { |
351 |
List<FLayer> res = new ArrayList<>(); |
352 |
List<FLayer> activeLayers = new ArrayList<>(); |
353 |
List<FLayer> nonActiveLayers = new ArrayList<>(); |
354 |
|
355 |
for (FLayer layer : groupLayers) {
|
356 |
if(layer.isActive()){
|
357 |
activeLayers.add(layer); |
358 |
} else {
|
359 |
nonActiveLayers.add(layer); |
360 |
} |
361 |
if(layer instanceof FLayers){ |
362 |
FLayers group = (FLayers)layer; |
363 |
List<FLayer> layers = bringToFront(group);
|
364 |
if(layers != null){ |
365 |
group.clearLayers(); //NOTE: Don't use clear method because also remove metadata
|
366 |
group.addAll(layers); |
367 |
} |
368 |
} |
369 |
} |
370 |
if(activeLayers.isEmpty() || nonActiveLayers.isEmpty()){
|
371 |
return null; |
372 |
} |
373 |
res.addAll(nonActiveLayers); |
374 |
res.addAll(activeLayers); |
375 |
return res;
|
376 |
} |
377 |
|
378 |
private List<FLayer> sendToBack(FLayers groupLayers) { |
379 |
List<FLayer> res = new ArrayList<>(); |
380 |
List<FLayer> activeLayers = new ArrayList<>(); |
381 |
List<FLayer> nonActiveLayers = new ArrayList<>(); |
382 |
|
383 |
for (FLayer layer : groupLayers) {
|
384 |
if(layer.isActive()){
|
385 |
activeLayers.add(layer); |
386 |
} else {
|
387 |
nonActiveLayers.add(layer); |
388 |
} |
389 |
if(layer instanceof FLayers){ |
390 |
FLayers group = (FLayers)layer; |
391 |
List<FLayer> layers = sendToBack(group);
|
392 |
if(layers != null){ |
393 |
group.clearLayers(); //NOTE: Don't use clear method because also remove metadata
|
394 |
group.addAll(layers); |
395 |
} |
396 |
} |
397 |
} |
398 |
if(activeLayers.isEmpty() || nonActiveLayers.isEmpty()){
|
399 |
return null; |
400 |
} |
401 |
res.addAll(activeLayers); |
402 |
res.addAll(nonActiveLayers); |
403 |
return res;
|
404 |
} |
405 |
|
406 |
private int getPositionInFlayersGroup(FLayers groupLayers, FLayer layer) { |
407 |
for (int i = 0; i < groupLayers.getLayersCount(); i++) { |
408 |
FLayer actualLayer = groupLayers.getLayer(i); |
409 |
if (actualLayer.equals(layer)) {
|
410 |
int correction = groupLayers.getLayersCount() - 1 - i; |
411 |
return correction;
|
412 |
} |
413 |
} |
414 |
return -1; |
415 |
} |
416 |
|
417 |
private int getValidTocPosition(int position, FLayers groupLayers, int move) { |
418 |
int totalSize = groupLayers.getLayersCount();
|
419 |
int newPosition = position + move;
|
420 |
if (newPosition <= 0) { |
421 |
return 0; |
422 |
} |
423 |
if (newPosition >= totalSize) {
|
424 |
return totalSize - 1; |
425 |
} |
426 |
return newPosition;
|
427 |
} |
428 |
|
429 |
private List<FLayer> getFirstLevelActivesFromLayers(FLayers groupLayers, boolean reverse) { |
430 |
ArrayList<FLayer> actives = new ArrayList<>(); |
431 |
for (FLayer layer : groupLayers) {
|
432 |
if (layer.isActive()) {
|
433 |
actives.add(layer); |
434 |
} |
435 |
} |
436 |
if (reverse) {
|
437 |
Collections.reverse(actives);
|
438 |
} |
439 |
return actives;
|
440 |
} |
441 |
|
442 |
private List<FLayers> getFirstLevelGroupsFromLayers(FLayers groupLayers) { |
443 |
ArrayList<FLayers> flayers = new ArrayList<>(); |
444 |
for (FLayer layer : groupLayers) {
|
445 |
if (layer instanceof FLayers) { |
446 |
flayers.add((FLayers) layer); |
447 |
} |
448 |
} |
449 |
Collections.reverse(flayers);
|
450 |
return flayers;
|
451 |
} |
452 |
|
453 |
private void moveAhead(FLayers groupLayers) { |
454 |
List<FLayer> actives = getFirstLevelActivesFromLayers(groupLayers, true); |
455 |
for (FLayer layer : actives) {
|
456 |
FLayers layers = layer.getParentLayer(); |
457 |
int position = this.getPositionInFlayersGroup(layers, layer); |
458 |
if( position < 1 ) { |
459 |
FLayers layersParent = layers.getParentLayer(); |
460 |
if( layersParent!=null ) { |
461 |
int layersPosition = this.getPositionInFlayersGroup(layersParent, layers); |
462 |
layersParent.add(layersParent.size() - layersPosition, layer); |
463 |
layers.remove(layer); |
464 |
} |
465 |
} else {
|
466 |
int newPosition = getValidTocPosition(position, layers, -1); |
467 |
layers.moveTo(position,newPosition); |
468 |
} |
469 |
} |
470 |
List<FLayers> flayersGroup = getFirstLevelGroupsFromLayers(groupLayers);
|
471 |
for (FLayers fLayer : flayersGroup) {
|
472 |
this.moveAhead(fLayer);
|
473 |
} |
474 |
} |
475 |
|
476 |
private void moveBehind(FLayers groupLayers) { |
477 |
List<FLayer> actives = getFirstLevelActivesFromLayers(groupLayers, false); |
478 |
for (FLayer layer : actives) {
|
479 |
FLayers layers = layer.getParentLayer(); |
480 |
int position = this.getPositionInFlayersGroup(layers, layer); |
481 |
if( position >= layers.size()-1 ) { |
482 |
FLayers layersParent = layers.getParentLayer(); |
483 |
if( layersParent!=null ) { |
484 |
int layersPosition = this.getPositionInFlayersGroup(layersParent, layers); |
485 |
layersParent.add(layersParent.size() - layersPosition - 1, layer);
|
486 |
layers.remove(layer); |
487 |
} |
488 |
} else {
|
489 |
int newPosition = getValidTocPosition(position, layers, +1); |
490 |
layers.moveTo(position, newPosition); |
491 |
} |
492 |
} |
493 |
List<FLayers> flayersGroup = getFirstLevelGroupsFromLayers(groupLayers);
|
494 |
for (FLayers fLayer : flayersGroup) {
|
495 |
this.moveBehind(fLayer);
|
496 |
} |
497 |
} |
498 |
} |