Statistics
| Revision:

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

History | View | Annotate | Download (6.33 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 javax.swing.JOptionPane;
44

    
45
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
47

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

    
65

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

    
76
        private MapControl mapControl;
77
        private ExploitCADTool exploit;
78

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

    
86
                registerIcons();
87
        }
88

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

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

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

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

    
186
        if (geotype.getType() == Geometry.TYPES.SURFACE
187
            || geotype.getType() == Geometry.TYPES.MULTISURFACE) {
188
            
189
            // exploting a surface can give curves, so must allow multiple types
190
            return (factory.allowMultipleGeometryTypes()
191
                == FeatureStoreProviderFactory.YES); 
192
        }
193

    
194
        if (geotype.isTypeOf(Geometry.TYPES.GEOMETRY)) {
195
            // if everything is allowed, ok
196
            return true;
197
        }
198
        
199
        // otherwise: (multi)points, etc, not allowed
200
        return false;
201
        }
202
        /**
203
         * @see org.gvsig.andami.plugins.IExtension#isVisible()
204
         */
205
        public boolean isVisible() {
206
                if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
207
                        return true;
208
                }
209
                return false;
210
        }
211
}