Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / ExploitExtension.java @ 38533

History | View | Annotate | Download (7.13 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.editing;
42

    
43
import java.util.List;
44

    
45
import javax.swing.JOptionPane;
46

    
47
import org.slf4j.Logger;
48
import org.slf4j.LoggerFactory;
49

    
50
import org.gvsig.andami.PluginServices;
51
import org.gvsig.andami.plugins.Extension;
52
import org.gvsig.app.ApplicationLocator;
53
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
54
import org.gvsig.editing.gui.cad.tools.ExploitCADTool;
55
import org.gvsig.editing.layers.VectorialLayerEdited;
56
import org.gvsig.fmap.dal.DALLocator;
57
import org.gvsig.fmap.dal.DataManager;
58
import org.gvsig.fmap.dal.exception.DataException;
59
import org.gvsig.fmap.dal.feature.FeatureSelection;
60
import org.gvsig.fmap.dal.feature.FeatureStore;
61
import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory;
62
import org.gvsig.fmap.geom.Geometry;
63
import org.gvsig.fmap.geom.type.GeometryType;
64
import org.gvsig.fmap.mapcontrol.MapControl;
65
import org.gvsig.tools.dispose.DisposableIterator;
66

    
67

    
68
/**
69
 * Extensi?n que gestiona la simplificaci?n de una geometr?a compuesta en otras m?s sencillas.
70
 *
71
 * @author Vicente Caballero Navarro
72
 */
73
public class ExploitExtension extends Extension {
74
    
75
    private static Logger logger = LoggerFactory.getLogger(ExploitExtension.class);
76
        private DefaultViewPanel view;
77

    
78
        private MapControl mapControl;
79
        private ExploitCADTool exploit;
80

    
81
        /**
82
         * @see org.gvsig.andami.plugins.IExtension#initialize()
83
         */
84
        public void initialize() {
85
                exploit=new ExploitCADTool();
86
                CADExtension.addCADTool("_exploit",exploit);
87

    
88
                registerIcons();
89
        }
90

    
91
        private void registerIcons(){
92
                PluginServices.getIconTheme().registerDefault(
93
                                "edition-geometry-exploit",
94
                                this.getClass().getClassLoader().getResource("images/Exploit.png")
95
                        );
96
        }
97

    
98
        /**
99
         * @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
100
         */
101
        public void execute(String s) {
102
                CADExtension.initFocus();
103
                if (s.equals("_exploit")) {
104
                CADExtension.setCADTool(s,true);
105
        }
106
                CADExtension.getEditionManager().setMapControl(mapControl);
107
                CADExtension.getCADToolAdapter().configureMenu();
108
        }
109

    
110
        /**
111
         * @see org.gvsig.andami.plugins.IExtension#isEnabled()
112
         */
113
        public boolean isEnabled() {
114
                DisposableIterator iterator = null;
115
                try {
116
                        if (EditionUtilities.getEditionStatus() ==
117
                            EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
118
                            
119
                                view = (DefaultViewPanel) PluginServices.getMDIManager().getActiveWindow();
120
                                mapControl = view.getMapControl();
121
                                EditionManager em=CADExtension.getEditionManager();
122
                                if (em.getActiveLayerEdited()==null) {
123
                                        return false;
124
                                }
125
                                VectorialLayerEdited vle=(VectorialLayerEdited)em.getActiveLayerEdited();
126
//                                FLyrVect lv=(FLyrVect)vle.getLayer();
127
                                if (((FeatureSelection)vle.getFeatureStore().getSelection()).getSize()<1) {
128
                                        return false;
129
                                }
130
                                
131
                                FeatureStore store = vle.getFeatureStore();
132
                                return canExploit(store);
133
                                
134
                                /*
135
                                if (!canExploit(store)) {
136
                                    return false;
137
                                }
138
                                */
139
                        
140
                                
141
                                /*
142
                                 * We cannot iterate through selection to get enabled or disbled
143
                                 * 
144
                                iterator = ((FeatureSelection) vle.getFeatureStore()
145
                                                .getSelection()).fastIterator();
146
                                if (iterator.hasNext()){
147
                                        Feature feature = (Feature) iterator.next();
148
                                        if (feature.getDefaultGeometry()!=null &&
149
                                            exploit.isApplicable((feature.getDefaultGeometry()).getType())){
150
                                                return true;
151
                                        }
152
                                }
153
                                */
154
                        }
155
                } catch (Throwable e) {
156
                    logger.info("IsEnabled error. ", e);
157
                    ApplicationLocator.getManager().message("_Unable_to_update_tool_status",
158
                        JOptionPane.ERROR_MESSAGE);
159
                        // NotificationManager.addError(e.getMessage(),e);
160
                } finally {
161
                        if (iterator != null) {
162
                                iterator.dispose();
163
                        }
164
                }
165
                return false;
166
        }
167

    
168
        private boolean canExploit(FeatureStore store) {
169
        DataManager dalmanager = DALLocator.getDataManager();
170
        FeatureStoreProviderFactory factory =
171
            (FeatureStoreProviderFactory) dalmanager.getStoreProviderFactory(store.getProviderName());
172
    
173
        GeometryType geotype = null;
174
        try {
175
            geotype = store.getDefaultFeatureType().getDefaultGeometryAttribute().getGeomType();
176
        } catch (DataException e) {
177
            logger.info("Cant get geom type.", e);
178
            return false;
179
        }
180
        
181
        
182
        if (geotype.getType() == Geometry.TYPES.CURVE
183
            || geotype.getType() == Geometry.TYPES.MULTICURVE) {
184
            // exploting a curve gives curves, so ok
185
            return true;
186
        }
187

    
188
        if (geotype.getType() == Geometry.TYPES.SURFACE
189
            || geotype.getType() == Geometry.TYPES.MULTISURFACE) {
190
            
191
            // exploting a surface can give curves:
192
            return allowsCurvesOfSubtype(factory, geotype.getSubType());
193
                // (factory.allowMultipleGeometryTypes()
194
                // == FeatureStoreProviderFactory.YES); 
195
        }
196

    
197
        if (geotype.isTypeOf(Geometry.TYPES.GEOMETRY)) {
198
            // if everything is allowed, ok
199
            return true;
200
        }
201
        
202
        // otherwise: (multi)points, etc, not allowed
203
        return false;
204
        }
205
        
206
        /**
207
     * @param factory
208
     * @param subType
209
     * @return whether given factory supports
210
     * curves of given subtype
211
     */
212
    private boolean allowsCurvesOfSubtype(
213
        FeatureStoreProviderFactory fact,
214
        int subt) {
215
        
216
        List arr2_list = fact.getSupportedGeometryTypesSubtypes();
217
        int[] item = null;
218
        int sz = arr2_list.size();
219
        for (int i=0; i<sz; i++) {
220
            item = (int[]) arr2_list.get(i);
221
            if (item != null && item.length == 2
222
                && item[0] == Geometry.TYPES.CURVE
223
                && item[1] == subt) {
224
                // found curves of same subtype (dimensions)
225
                return true;
226
            }
227
        }
228
        return false;
229
    }
230

    
231
    /**
232
         * @see org.gvsig.andami.plugins.IExtension#isVisible()
233
         */
234
        public boolean isVisible() {
235
                if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
236
                        return true;
237
                }
238
                return false;
239
        }
240
}