Revision 42683
trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/java/org/gvsig/app/extension/ShowLayerAsForm.java | ||
---|---|---|
1 | 1 |
package org.gvsig.app.extension; |
2 | 2 |
|
3 | 3 |
import java.awt.Dimension; |
4 |
import java.awt.Image; |
|
4 | 5 |
import java.awt.event.ActionEvent; |
5 | 6 |
import javax.swing.AbstractAction; |
6 | 7 |
import static javax.swing.Action.ACTION_COMMAND_KEY; |
... | ... | |
8 | 9 |
import static javax.swing.Action.SHORT_DESCRIPTION; |
9 | 10 |
import static javax.swing.Action.SMALL_ICON; |
10 | 11 |
import javax.swing.JOptionPane; |
12 |
import org.cresques.cts.IProjection; |
|
13 |
import org.gvsig.andami.IconThemeHelper; |
|
11 | 14 |
import org.gvsig.andami.plugins.Extension; |
12 | 15 |
import org.gvsig.app.ApplicationLocator; |
13 | 16 |
import org.gvsig.app.ApplicationManager; |
14 | 17 |
import org.gvsig.app.project.documents.view.ViewDocument; |
15 | 18 |
import org.gvsig.app.project.documents.view.ViewManager; |
19 |
import org.gvsig.app.project.documents.view.gui.IView; |
|
16 | 20 |
import org.gvsig.featureform.swing.CreateJFeatureFormException; |
17 | 21 |
import org.gvsig.featureform.swing.JFeatureForm; |
18 | 22 |
import org.gvsig.fmap.dal.exception.DataException; |
19 | 23 |
import org.gvsig.fmap.dal.feature.Feature; |
24 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
|
20 | 25 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
26 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
27 |
import org.gvsig.fmap.dal.feature.exception.CreateGeometryException; |
|
21 | 28 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
22 | 29 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
23 | 30 |
import org.gvsig.fmap.geom.Geometry; |
31 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
32 |
import org.gvsig.fmap.geom.GeometryManager; |
|
33 |
import org.gvsig.fmap.geom.primitive.Circle; |
|
34 |
import org.gvsig.fmap.geom.primitive.Point; |
|
24 | 35 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
25 | 36 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
37 |
import org.gvsig.fmap.mapcontext.layers.vectorial.IntersectsGeometryEvaluator; |
|
38 |
import org.gvsig.fmap.mapcontrol.MapControl; |
|
39 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.PointBehavior; |
|
40 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
|
41 |
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent; |
|
42 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener; |
|
26 | 43 |
import org.gvsig.tools.ToolsLocator; |
27 | 44 |
import org.gvsig.tools.i18n.I18nManager; |
28 | 45 |
import org.gvsig.tools.service.ServiceException; |
... | ... | |
38 | 55 |
|
39 | 56 |
@Override |
40 | 57 |
public void initialize() { |
58 |
IconThemeHelper.registerIcon("action", "layer-selectforediting", this); |
|
41 | 59 |
} |
42 | 60 |
|
43 | 61 |
@Override |
... | ... | |
56 | 74 |
FLyrVect vectLayer = (FLyrVect) layer; |
57 | 75 |
JFeatureForm form = this.createform(vectLayer.getFeatureStore()); |
58 | 76 |
form.addAction(new ZoomToCurrentAction(doc, form)); |
77 |
form.addAction(new SelectFeatureInTheViewAction(doc, form, layer)); |
|
59 | 78 |
form.showForm(WindowManager.MODE.WINDOW); |
60 | 79 |
} catch (Exception ex) { |
61 | 80 |
String msg = "Can't show form for layer '" + layer.getName() + "'."; |
... | ... | |
67 | 86 |
} |
68 | 87 |
} |
69 | 88 |
|
89 |
private static class SelectFeatureInTheViewAction extends AbstractAction implements PointListener { |
|
90 |
|
|
91 |
private final JFeatureForm form; |
|
92 |
private final ViewDocument doc; |
|
93 |
private MapControl mapControl = null; |
|
94 |
private String previousTool = null; |
|
95 |
private final FLyrVect layer; |
|
96 |
|
|
97 |
public SelectFeatureInTheViewAction(ViewDocument doc, JFeatureForm form, FLayer layer) { |
|
98 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
|
99 |
IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
|
100 |
|
|
101 |
this.doc = doc; |
|
102 |
this.form = form; |
|
103 |
this.putValue(NAME, null); |
|
104 |
this.putValue(SHORT_DESCRIPTION, i18nManager.getTranslation("_Select_feature_in_the_View")); |
|
105 |
this.putValue(SMALL_ICON, iconTheme.get("layer-selectforediting")); |
|
106 |
this.putValue(ACTION_COMMAND_KEY, "selectInTheView"); |
|
107 |
if( layer instanceof FLyrVect ) { |
|
108 |
this.layer = (FLyrVect) layer; |
|
109 |
this.setEnabled(true); |
|
110 |
} else { |
|
111 |
this.layer = null; |
|
112 |
this.setEnabled(false); |
|
113 |
} |
|
114 |
} |
|
115 |
|
|
116 |
@Override |
|
117 |
public void actionPerformed(ActionEvent ae) { |
|
118 |
ApplicationManager application = ApplicationLocator.getManager(); |
|
119 |
if( this.layer == null ) { |
|
120 |
return; |
|
121 |
} |
|
122 |
if (this.mapControl == null) { |
|
123 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
|
124 |
if (view != null) { |
|
125 |
MapControl mapControl = view.getMapControl(); |
|
126 |
this.previousTool = mapControl.getCurrentTool(); |
|
127 |
mapControl.addBehavior("layer-selectforediting", new PointBehavior(this)); |
|
128 |
mapControl.setTool("layer-selectforediting"); |
|
129 |
this.mapControl = mapControl; |
|
130 |
mapControl.requestFocus(); |
|
131 |
} |
|
132 |
} else { |
|
133 |
this.mapControl.setTool(this.previousTool); |
|
134 |
this.mapControl = null; |
|
135 |
this.previousTool = null; |
|
136 |
} |
|
137 |
} |
|
138 |
|
|
139 |
@Override |
|
140 |
public void point(PointEvent event) throws BehaviorException { |
|
141 |
ApplicationManager application = ApplicationLocator.getManager(); |
|
142 |
try { |
|
143 |
Point point = event.getMapPoint(); |
|
144 |
double tolerance = mapControl.getViewPort().toMapDistance(7); |
|
145 |
GeometryManager manager = GeometryLocator.getGeometryManager(); |
|
146 |
Circle circle = (Circle) manager.create( |
|
147 |
Geometry.TYPES.CIRCLE, |
|
148 |
Geometry.SUBTYPES.GEOM2D |
|
149 |
); |
|
150 |
circle.setPoints(point, tolerance); |
|
151 |
|
|
152 |
FeatureStore featureStore = this.form.getFeatureStore(); |
|
153 |
FeatureType featureType = featureStore.getDefaultFeatureType(); |
|
154 |
FeatureQuery featureQuery = featureStore.createFeatureQuery(); |
|
155 |
String geomName = featureType.getDefaultGeometryAttributeName(); |
|
156 |
featureQuery.setFeatureType(featureType); |
|
157 |
|
|
158 |
Geometry query_geo = this.layer.transformToSourceCRS(circle, true); |
|
159 |
IProjection query_proj = this.mapControl.getMapContext().getProjection(); |
|
160 |
if (this.layer.getCoordTrans() != null) { |
|
161 |
query_proj = this.layer.getCoordTrans().getPOrig(); |
|
162 |
} |
|
163 |
|
|
164 |
IntersectsGeometryEvaluator iee |
|
165 |
= new IntersectsGeometryEvaluator( |
|
166 |
query_geo, |
|
167 |
query_proj, |
|
168 |
featureStore.getDefaultFeatureType(), |
|
169 |
geomName); |
|
170 |
featureQuery.setFilter(iee); |
|
171 |
featureQuery.setAttributeNames(null); |
|
172 |
|
|
173 |
this.form.setQuery(featureQuery); |
|
174 |
|
|
175 |
this.mapControl.setTool(this.previousTool); |
|
176 |
this.mapControl = null; |
|
177 |
this.previousTool = null; |
|
178 |
} catch (Exception e) { |
|
179 |
application.message("Can't filter form.", JOptionPane.WARNING_MESSAGE); |
|
180 |
throw new RuntimeException("Can't create filter", e); |
|
181 |
} |
|
182 |
} |
|
183 |
|
|
184 |
@Override |
|
185 |
public void pointDoubleClick(PointEvent event) throws BehaviorException { |
|
186 |
this.point(event); |
|
187 |
} |
|
188 |
|
|
189 |
@Override |
|
190 |
public Image getImageCursor() { |
|
191 |
return IconThemeHelper.getImage("cursor-info-by-point"); |
|
192 |
} |
|
193 |
|
|
194 |
@Override |
|
195 |
public boolean cancelDrawing() { |
|
196 |
return false; |
|
197 |
} |
|
198 |
|
|
199 |
} |
|
200 |
|
|
70 | 201 |
private static class ZoomToCurrentAction extends AbstractAction { |
71 | 202 |
|
72 | 203 |
private final JFeatureForm form; |
Also available in: Unified diff