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
/**
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
 */
24
package org.gvsig.vectorediting.app.mainplugin;
25

    
26
import java.util.ArrayList;
27
import java.util.List;
28
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
public class ServiceExtension extends Extension {
40

    
41
    @Override
42
    public void initialize() {
43
    }
44

    
45
    @Override
46
    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
    @Override
60
    public boolean isEnabled() {
61
        return this.isVisible();
62
    }
63

    
64
    @Override
65
    public boolean isEnabled(String action) {
66
        IView view = getActiveView();
67
        if(view!=null){
68
            FLyrVect activeLayer = getActiveEditingVectLayer(view);
69

    
70
            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
        }
78
        return false;
79
    }
80

    
81
    @Override
82
    public boolean isVisible() {
83
        IView view = getActiveView();
84
        if(view!=null){
85
            FLyrVect activeLayer = getActiveEditingVectLayer(view);
86
            return (activeLayer != null && activeLayer.isEditing());
87
        }
88
        return false;
89
    }
90

    
91
    @Override
92
    public boolean isVisible(String action) {
93
        return this.isVisible();
94
    }
95

    
96
    @Override
97
    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
    private FLyrVect getActiveEditingVectLayer(IView view) {
109
        List<FLyrVect> activeEditingVectLayers = new ArrayList<>();
110
        if (view != null) {
111
            ViewDocument viewDocument = view.getViewDocument();
112
            FLayer[] activeLayers =
113
                viewDocument.getMapContext().getLayers().getActives();
114
            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

    
123
            if ((activeEditingVectLayers.size() == 1)) {
124
                return (FLyrVect) activeEditingVectLayers.get(0);
125
            }
126
        }
127
        return null;
128
    }
129
}