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