Statistics
| Revision:

gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.app / org.gvsig.vectorediting.app.mainplugin / src / main / java / org / gvsig / vectorediting / app / mainplugin / ServiceExtension.java @ 2109

History | View | Annotate | Download (4.26 KB)

1 159 llmarques
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2014 gvSIG Association
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23 117 llmarques
 */
24
package org.gvsig.vectorediting.app.mainplugin;
25
26 2109 fdiaz
import java.util.ArrayList;
27
import java.util.List;
28 117 llmarques
import org.gvsig.andami.plugins.Extension;
29
import org.gvsig.app.ApplicationLocator;
30
import org.gvsig.app.ApplicationManager;
31
import org.gvsig.app.project.documents.view.ViewDocument;
32
import org.gvsig.app.project.documents.view.gui.IView;
33
import org.gvsig.fmap.mapcontext.layers.FLayer;
34
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
35
import org.gvsig.vectorediting.swing.api.EditingContext;
36
import org.gvsig.vectorediting.swing.api.EditingSwingLocator;
37
import org.gvsig.vectorediting.swing.api.EditingSwingManager;
38
39 245 llmarques
public class ServiceExtension extends Extension {
40 117 llmarques
41 2109 fdiaz
    @Override
42 117 llmarques
    public void initialize() {
43
    }
44
45 2109 fdiaz
    @Override
46 117 llmarques
    public void execute(String actionCommand) {
47
        IView view = getActiveView();
48
        EditingSwingManager swingManager =
49
            EditingSwingLocator.getSwingManager();
50
51
        if (view != null) {
52
            EditingContext editingContext =
53
                swingManager.getEditingContext(view.getMapControl());
54
            editingContext.activateService(actionCommand);
55
        }
56
57
    }
58
59 2109 fdiaz
    @Override
60 117 llmarques
    public boolean isEnabled() {
61
        return this.isVisible();
62
    }
63
64 227 llmarques
    @Override
65 117 llmarques
    public boolean isEnabled(String action) {
66
        IView view = getActiveView();
67 2109 fdiaz
        if(view!=null){
68
            FLyrVect activeLayer = getActiveEditingVectLayer(view);
69 117 llmarques
70 2109 fdiaz
            if (activeLayer != null && activeLayer.isEditing() && action!=null) {
71
                EditingSwingManager swingManager =
72
                    EditingSwingLocator.getSwingManager();
73
                EditingContext editingContext =
74
                    swingManager.getEditingContext(view.getMapControl());
75
                return editingContext.isServiceCompatible(action);
76
            }
77 117 llmarques
        }
78
        return false;
79
    }
80
81 2109 fdiaz
    @Override
82 117 llmarques
    public boolean isVisible() {
83
        IView view = getActiveView();
84 2109 fdiaz
        if(view!=null){
85
            FLyrVect activeLayer = getActiveEditingVectLayer(view);
86
            return (activeLayer != null && activeLayer.isEditing());
87
        }
88
        return false;
89 117 llmarques
    }
90
91 227 llmarques
    @Override
92 117 llmarques
    public boolean isVisible(String action) {
93
        return this.isVisible();
94
    }
95
96 227 llmarques
    @Override
97 117 llmarques
    public boolean canQueryByAction() {
98
        return true;
99
    }
100
101
    private IView getActiveView() {
102
103
        ApplicationManager application = ApplicationLocator.getManager();
104
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
105
        return view;
106
    }
107
108 2109 fdiaz
    private FLyrVect getActiveEditingVectLayer(IView view) {
109
        List<FLyrVect> activeEditingVectLayers = new ArrayList<>();
110 117 llmarques
        if (view != null) {
111
            ViewDocument viewDocument = view.getViewDocument();
112 2109 fdiaz
            FLayer[] activeLayers =
113 117 llmarques
                viewDocument.getMapContext().getLayers().getActives();
114 2109 fdiaz
            for (FLayer activeLayer : activeLayers) {
115
                if(activeLayer instanceof FLyrVect){
116
                    FLyrVect activeVectLayer = (FLyrVect) activeLayer;
117
                    if(activeVectLayer.isEditing()){
118
                        activeEditingVectLayers.add(activeVectLayer);
119
                    }
120
                }
121
            }
122 117 llmarques
123 2109 fdiaz
            if ((activeEditingVectLayers.size() == 1)) {
124
                return (FLyrVect) activeEditingVectLayers.get(0);
125 117 llmarques
            }
126
        }
127
        return null;
128
    }
129
}