svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.editing.app / org.gvsig.editing.app.mainplugin / src / main / java / org / gvsig / editing / ExploitExtension.java @ 40557
History | View | Annotate | Download (7.03 KB)
1 | 40557 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40557 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
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 | 40557 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * 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 | 40557 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | 40435 | jjdelcerro | *
|
21 | 40557 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.editing; |
||
25 | |||
26 | import java.util.List; |
||
27 | |||
28 | import javax.swing.JOptionPane; |
||
29 | |||
30 | import org.gvsig.andami.IconThemeHelper; |
||
31 | import org.gvsig.andami.PluginServices; |
||
32 | import org.gvsig.andami.plugins.Extension; |
||
33 | import org.gvsig.app.ApplicationLocator; |
||
34 | import org.gvsig.app.project.documents.view.gui.DefaultViewPanel; |
||
35 | import org.gvsig.editing.gui.cad.tools.ExploitCADTool; |
||
36 | import org.gvsig.editing.layers.VectorialLayerEdited; |
||
37 | import org.gvsig.fmap.dal.DALLocator; |
||
38 | import org.gvsig.fmap.dal.DataManager; |
||
39 | import org.gvsig.fmap.dal.exception.DataException; |
||
40 | import org.gvsig.fmap.dal.feature.FeatureSelection; |
||
41 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
42 | import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory; |
||
43 | import org.gvsig.fmap.geom.Geometry; |
||
44 | import org.gvsig.fmap.geom.type.GeometryType; |
||
45 | import org.gvsig.fmap.mapcontrol.MapControl; |
||
46 | import org.gvsig.tools.dispose.DisposableIterator; |
||
47 | import org.slf4j.Logger; |
||
48 | import org.slf4j.LoggerFactory; |
||
49 | |||
50 | |||
51 | /**
|
||
52 | * Extensi?n que gestiona la simplificaci?n de una geometr?a compuesta en otras m?s sencillas.
|
||
53 | *
|
||
54 | * @author Vicente Caballero Navarro
|
||
55 | */
|
||
56 | public class ExploitExtension extends Extension { |
||
57 | |||
58 | private static Logger logger = LoggerFactory.getLogger(ExploitExtension.class); |
||
59 | private DefaultViewPanel view;
|
||
60 | |||
61 | private MapControl mapControl;
|
||
62 | private ExploitCADTool exploit;
|
||
63 | |||
64 | /**
|
||
65 | * @see org.gvsig.andami.plugins.IExtension#initialize()
|
||
66 | */
|
||
67 | public void initialize() { |
||
68 | exploit=new ExploitCADTool();
|
||
69 | CADExtension.addCADTool("_exploit",exploit);
|
||
70 | |||
71 | registerIcons(); |
||
72 | } |
||
73 | |||
74 | private void registerIcons(){ |
||
75 | IconThemeHelper.registerIcon("action", "layer-modify-explode", this); |
||
76 | } |
||
77 | |||
78 | /**
|
||
79 | * @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
|
||
80 | */
|
||
81 | public void execute(String s) { |
||
82 | CADExtension.initFocus(); |
||
83 | if ( "layer-modify-explode".equalsIgnoreCase(s) ) { |
||
84 | CADExtension.setCADTool("_exploit",true); |
||
85 | } |
||
86 | CADExtension.getEditionManager().setMapControl(mapControl); |
||
87 | CADExtension.getCADToolAdapter().configureMenu(); |
||
88 | } |
||
89 | |||
90 | /**
|
||
91 | * @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
||
92 | */
|
||
93 | public boolean isEnabled() { |
||
94 | DisposableIterator iterator = null;
|
||
95 | try {
|
||
96 | if (EditionUtilities.getEditionStatus() ==
|
||
97 | EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) { |
||
98 | |||
99 | view = (DefaultViewPanel) PluginServices.getMDIManager().getActiveWindow(); |
||
100 | mapControl = view.getMapControl(); |
||
101 | IEditionManager em = EditionLocator.getEditionManager(); |
||
102 | if (em.getActiveLayerEdited()==null) { |
||
103 | return false; |
||
104 | } |
||
105 | VectorialLayerEdited vle=(VectorialLayerEdited)em.getActiveLayerEdited(); |
||
106 | // FLyrVect lv=(FLyrVect)vle.getLayer();
|
||
107 | if (((FeatureSelection)vle.getFeatureStore().getSelection()).getSize()<1) { |
||
108 | return false; |
||
109 | } |
||
110 | |||
111 | FeatureStore store = vle.getFeatureStore(); |
||
112 | return canExploit(store);
|
||
113 | |||
114 | /*
|
||
115 | if (!canExploit(store)) {
|
||
116 | return false;
|
||
117 | }
|
||
118 | */
|
||
119 | |||
120 | |||
121 | /*
|
||
122 | * We cannot iterate through selection to get enabled or disbled
|
||
123 | *
|
||
124 | iterator = ((FeatureSelection) vle.getFeatureStore()
|
||
125 | .getSelection()).fastIterator();
|
||
126 | if (iterator.hasNext()){
|
||
127 | Feature feature = (Feature) iterator.next();
|
||
128 | if (feature.getDefaultGeometry()!=null &&
|
||
129 | exploit.isApplicable((feature.getDefaultGeometry()).getType())){
|
||
130 | return true;
|
||
131 | }
|
||
132 | }
|
||
133 | */
|
||
134 | } |
||
135 | } catch (Throwable e) { |
||
136 | logger.info("IsEnabled error. ", e);
|
||
137 | ApplicationLocator.getManager().message("_Unable_to_update_tool_status",
|
||
138 | JOptionPane.ERROR_MESSAGE);
|
||
139 | // NotificationManager.addError(e.getMessage(),e);
|
||
140 | } finally {
|
||
141 | if (iterator != null) { |
||
142 | iterator.dispose(); |
||
143 | } |
||
144 | } |
||
145 | return false; |
||
146 | } |
||
147 | |||
148 | private boolean canExploit(FeatureStore store) { |
||
149 | DataManager dalmanager = DALLocator.getDataManager(); |
||
150 | FeatureStoreProviderFactory factory = |
||
151 | (FeatureStoreProviderFactory) dalmanager.getStoreProviderFactory(store.getProviderName()); |
||
152 | |||
153 | GeometryType geotype = null;
|
||
154 | try {
|
||
155 | geotype = store.getDefaultFeatureType().getDefaultGeometryAttribute().getGeomType(); |
||
156 | } catch (DataException e) {
|
||
157 | logger.info("Cant get geom type.", e);
|
||
158 | return false; |
||
159 | } |
||
160 | |||
161 | |||
162 | if (geotype.getType() == Geometry.TYPES.CURVE
|
||
163 | || geotype.getType() == Geometry.TYPES.MULTICURVE) { |
||
164 | // exploting a curve gives curves, so ok
|
||
165 | return true; |
||
166 | } |
||
167 | |||
168 | if (geotype.getType() == Geometry.TYPES.SURFACE
|
||
169 | || geotype.getType() == Geometry.TYPES.MULTISURFACE) { |
||
170 | |||
171 | // exploting a surface can give curves:
|
||
172 | return allowsCurvesOfSubtype(factory, geotype.getSubType());
|
||
173 | // (factory.allowMultipleGeometryTypes()
|
||
174 | // == FeatureStoreProviderFactory.YES);
|
||
175 | } |
||
176 | |||
177 | if (geotype.isTypeOf(Geometry.TYPES.GEOMETRY)) {
|
||
178 | // if everything is allowed, ok
|
||
179 | return true; |
||
180 | } |
||
181 | |||
182 | // otherwise: (multi)points, etc, not allowed
|
||
183 | return false; |
||
184 | } |
||
185 | |||
186 | /**
|
||
187 | * @param factory
|
||
188 | * @param subType
|
||
189 | * @return whether given factory supports
|
||
190 | * curves of given subtype
|
||
191 | */
|
||
192 | private boolean allowsCurvesOfSubtype( |
||
193 | FeatureStoreProviderFactory fact, |
||
194 | int subt) {
|
||
195 | |||
196 | List arr2_list = fact.getSupportedGeometryTypesSubtypes();
|
||
197 | |||
198 | if (arr2_list == null) { |
||
199 | /*
|
||
200 | * null is a valid value, it means all supported
|
||
201 | */
|
||
202 | return true; |
||
203 | } |
||
204 | int[] item = null; |
||
205 | int sz = arr2_list.size();
|
||
206 | for (int i=0; i<sz; i++) { |
||
207 | item = (int[]) arr2_list.get(i); |
||
208 | if (item != null && item.length == 2 |
||
209 | && item[0] == Geometry.TYPES.CURVE
|
||
210 | && item[1] == subt) {
|
||
211 | // found curves of same subtype (dimensions)
|
||
212 | return true; |
||
213 | } |
||
214 | } |
||
215 | return false; |
||
216 | } |
||
217 | |||
218 | /**
|
||
219 | * @see org.gvsig.andami.plugins.IExtension#isVisible()
|
||
220 | */
|
||
221 | public boolean isVisible() { |
||
222 | if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
|
||
223 | return true; |
||
224 | } |
||
225 | return false; |
||
226 | } |
||
227 | } |