svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / ShowLayerAsForm.java @ 44601
History | View | Annotate | Download (9.19 KB)
1 | 42489 | jjdelcerro | package org.gvsig.app.extension; |
---|---|---|---|
2 | |||
3 | import java.awt.Dimension; |
||
4 | 42683 | jjdelcerro | import java.awt.Image; |
5 | 42489 | jjdelcerro | import java.awt.event.ActionEvent; |
6 | import javax.swing.AbstractAction; |
||
7 | import static javax.swing.Action.ACTION_COMMAND_KEY; |
||
8 | import static javax.swing.Action.NAME; |
||
9 | import static javax.swing.Action.SHORT_DESCRIPTION; |
||
10 | import static javax.swing.Action.SMALL_ICON; |
||
11 | import javax.swing.JOptionPane; |
||
12 | 42683 | jjdelcerro | import org.cresques.cts.IProjection; |
13 | import org.gvsig.andami.IconThemeHelper; |
||
14 | 42489 | jjdelcerro | import org.gvsig.andami.plugins.Extension; |
15 | import org.gvsig.app.ApplicationLocator; |
||
16 | import org.gvsig.app.ApplicationManager; |
||
17 | import org.gvsig.app.project.documents.view.ViewDocument; |
||
18 | import org.gvsig.app.project.documents.view.ViewManager; |
||
19 | 42683 | jjdelcerro | import org.gvsig.app.project.documents.view.gui.IView; |
20 | 42489 | jjdelcerro | import org.gvsig.featureform.swing.CreateJFeatureFormException; |
21 | 42775 | jjdelcerro | import org.gvsig.featureform.swing.JFeaturesForm; |
22 | 42489 | jjdelcerro | import org.gvsig.fmap.dal.exception.DataException; |
23 | 42683 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureQuery; |
24 | 42489 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureStore; |
25 | 42683 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureType; |
26 | 42489 | jjdelcerro | import org.gvsig.fmap.dal.swing.DALSwingLocator; |
27 | import org.gvsig.fmap.dal.swing.DataSwingManager; |
||
28 | import org.gvsig.fmap.geom.Geometry; |
||
29 | 42683 | jjdelcerro | import org.gvsig.fmap.geom.GeometryLocator; |
30 | import org.gvsig.fmap.geom.GeometryManager; |
||
31 | import org.gvsig.fmap.geom.primitive.Circle; |
||
32 | import org.gvsig.fmap.geom.primitive.Point; |
||
33 | 42489 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.FLayer; |
34 | import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
||
35 | 43020 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.vectorial.SpatialEvaluatorsFactory; |
36 | 42683 | jjdelcerro | import org.gvsig.fmap.mapcontrol.MapControl; |
37 | import org.gvsig.fmap.mapcontrol.tools.Behavior.PointBehavior; |
||
38 | import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
||
39 | import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent; |
||
40 | import org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener; |
||
41 | 42489 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
42 | 43020 | jjdelcerro | import org.gvsig.tools.evaluator.Evaluator; |
43 | 42489 | jjdelcerro | import org.gvsig.tools.i18n.I18nManager; |
44 | import org.gvsig.tools.service.ServiceException; |
||
45 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
46 | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
||
47 | import org.gvsig.tools.swing.icontheme.IconTheme; |
||
48 | |||
49 | 44281 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
50 | 42489 | jjdelcerro | public class ShowLayerAsForm extends Extension { |
51 | |||
52 | @Override
|
||
53 | public void initialize() { |
||
54 | 44486 | jjdelcerro | IconThemeHelper.registerIcon("action", "layer-show-form", this); |
55 | 44531 | omartinez | IconThemeHelper.registerIcon("action", "layer-selectforediting", this); |
56 | 42489 | jjdelcerro | } |
57 | |||
58 | @Override
|
||
59 | public void execute(String actionCommand) { |
||
60 | ApplicationManager application = ApplicationLocator.getManager(); |
||
61 | if ("layer-show-form".equalsIgnoreCase(actionCommand)) { |
||
62 | ViewDocument doc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
||
63 | if (doc == null) { |
||
64 | return;
|
||
65 | } |
||
66 | FLayer[] layers = doc.getMapContext().getLayers().getActives();
|
||
67 | for (int i = 0; i < layers.length; i++) { |
||
68 | FLayer layer = layers[i]; |
||
69 | if (layer.isAvailable() && layer instanceof FLyrVect) { |
||
70 | try {
|
||
71 | FLyrVect vectLayer = (FLyrVect) layer; |
||
72 | 44437 | jjdelcerro | FeatureStore featureStore = vectLayer.getFeatureStore(); |
73 | FeatureType type = featureStore.getDefaultFeatureType(); |
||
74 | if( type.supportReferences() ) {
|
||
75 | JFeaturesForm form = this.createform(featureStore);
|
||
76 | form.addAction(new SelectFeatureInTheViewAction(doc, form, layer));
|
||
77 | form.showForm(WindowManager.MODE.WINDOW); |
||
78 | } |
||
79 | 42489 | jjdelcerro | } catch (Exception ex) { |
80 | String msg = "Can't show form for layer '" + layer.getName() + "'."; |
||
81 | logger.warn(msg, ex); |
||
82 | application.messageDialog(msg + "\n\n(See the error log for more information)", "Warning", JOptionPane.WARNING_MESSAGE); |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | |||
89 | 42683 | jjdelcerro | private static class SelectFeatureInTheViewAction extends AbstractAction implements PointListener { |
90 | |||
91 | 42775 | jjdelcerro | private final JFeaturesForm form; |
92 | 42683 | jjdelcerro | private final ViewDocument doc; |
93 | private MapControl mapControl = null; |
||
94 | private String previousTool = null; |
||
95 | private final FLyrVect layer; |
||
96 | |||
97 | 42775 | jjdelcerro | public SelectFeatureInTheViewAction(ViewDocument doc, JFeaturesForm form, FLayer layer) {
|
98 | 42683 | jjdelcerro | 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 | 44281 | jjdelcerro | MapControl theMapControl = view.getMapControl(); |
126 | this.previousTool = theMapControl.getCurrentTool();
|
||
127 | theMapControl.addBehavior("layer-selectforediting", new PointBehavior(this)); |
||
128 | theMapControl.setTool("layer-selectforediting");
|
||
129 | this.mapControl = theMapControl;
|
||
130 | theMapControl.requestFocus(); |
||
131 | 42683 | jjdelcerro | } |
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 | featureQuery.setFeatureType(featureType); |
||
156 | |||
157 | Geometry query_geo = this.layer.transformToSourceCRS(circle, true); |
||
158 | IProjection query_proj = this.mapControl.getMapContext().getProjection();
|
||
159 | if (this.layer.getCoordTrans() != null) { |
||
160 | query_proj = this.layer.getCoordTrans().getPOrig();
|
||
161 | } |
||
162 | |||
163 | 43020 | jjdelcerro | Evaluator iee = SpatialEvaluatorsFactory.getInstance().intersects( |
164 | 42683 | jjdelcerro | query_geo, |
165 | query_proj, |
||
166 | 43020 | jjdelcerro | featureStore |
167 | ); |
||
168 | 42683 | jjdelcerro | featureQuery.setFilter(iee); |
169 | featureQuery.setAttributeNames(null);
|
||
170 | |||
171 | this.form.setQuery(featureQuery);
|
||
172 | |||
173 | this.mapControl.setTool(this.previousTool); |
||
174 | this.mapControl = null; |
||
175 | this.previousTool = null; |
||
176 | } catch (Exception e) { |
||
177 | application.message("Can't filter form.", JOptionPane.WARNING_MESSAGE); |
||
178 | throw new RuntimeException("Can't create filter", e); |
||
179 | } |
||
180 | } |
||
181 | |||
182 | @Override
|
||
183 | public void pointDoubleClick(PointEvent event) throws BehaviorException { |
||
184 | this.point(event);
|
||
185 | } |
||
186 | |||
187 | @Override
|
||
188 | public Image getImageCursor() { |
||
189 | return IconThemeHelper.getImage("cursor-info-by-point"); |
||
190 | } |
||
191 | |||
192 | @Override
|
||
193 | public boolean cancelDrawing() { |
||
194 | return false; |
||
195 | } |
||
196 | |||
197 | } |
||
198 | |||
199 | 42775 | jjdelcerro | private JFeaturesForm createform(FeatureStore featureStore) throws CreateJFeatureFormException, ServiceException, DataException { |
200 | 42489 | jjdelcerro | final DataSwingManager swingManager = DALSwingLocator.getSwingManager();
|
201 | 42775 | jjdelcerro | final JFeaturesForm form = swingManager.createJFeaturesForm(featureStore);
|
202 | 42489 | jjdelcerro | form.setPreferredSize(new Dimension(400, 300)); |
203 | return form;
|
||
204 | } |
||
205 | |||
206 | @Override
|
||
207 | public boolean isEnabled() { |
||
208 | return true; |
||
209 | } |
||
210 | |||
211 | @Override
|
||
212 | public boolean isVisible() { |
||
213 | ApplicationManager application = ApplicationLocator.getManager(); |
||
214 | ViewDocument doc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
||
215 | if (doc == null) { |
||
216 | return false; |
||
217 | } |
||
218 | return doc.getMapContext().hasActiveVectorLayers();
|
||
219 | } |
||
220 | } |