Statistics
| Revision:

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 @ 44263

History | View | Annotate | Download (10.4 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
import org.gvsig.fmap.dal.feature.Feature;
24 42683 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureQuery;
25 42489 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureStore;
26 42683 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType;
27 42489 jjdelcerro
import org.gvsig.fmap.dal.swing.DALSwingLocator;
28
import org.gvsig.fmap.dal.swing.DataSwingManager;
29
import org.gvsig.fmap.geom.Geometry;
30 42683 jjdelcerro
import org.gvsig.fmap.geom.GeometryLocator;
31
import org.gvsig.fmap.geom.GeometryManager;
32
import org.gvsig.fmap.geom.primitive.Circle;
33
import org.gvsig.fmap.geom.primitive.Point;
34 42489 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.FLayer;
35
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
36 42683 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.vectorial.IntersectsGeometryEvaluator;
37 43020 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.vectorial.SpatialEvaluatorsFactory;
38 42683 jjdelcerro
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;
43 42489 jjdelcerro
import org.gvsig.tools.ToolsLocator;
44 43020 jjdelcerro
import org.gvsig.tools.evaluator.Evaluator;
45 42489 jjdelcerro
import org.gvsig.tools.i18n.I18nManager;
46
import org.gvsig.tools.service.ServiceException;
47
import org.gvsig.tools.swing.api.ToolsSwingLocator;
48
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
49
import org.gvsig.tools.swing.icontheme.IconTheme;
50
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52
53
public class ShowLayerAsForm extends Extension {
54
55
    private static final Logger logger = LoggerFactory.getLogger(ShowLayerAsForm.class);
56
57
    @Override
58
    public void initialize() {
59 42683 jjdelcerro
        IconThemeHelper.registerIcon("action", "layer-selectforediting", this);
60 42489 jjdelcerro
    }
61
62
    @Override
63
    public void execute(String actionCommand) {
64
        ApplicationManager application = ApplicationLocator.getManager();
65
        if ("layer-show-form".equalsIgnoreCase(actionCommand)) {
66
            ViewDocument doc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
67
            if (doc == null) {
68
                return;
69
            }
70
            FLayer[] layers = doc.getMapContext().getLayers().getActives();
71
            for (int i = 0; i < layers.length; i++) {
72
                FLayer layer = layers[i];
73
                if (layer.isAvailable() && layer instanceof FLyrVect) {
74
                    try {
75
                        FLyrVect vectLayer = (FLyrVect) layer;
76 42775 jjdelcerro
                        JFeaturesForm form = this.createform(vectLayer.getFeatureStore());
77 42619 jjdelcerro
                        form.addAction(new ZoomToCurrentAction(doc, form));
78 42683 jjdelcerro
                        form.addAction(new SelectFeatureInTheViewAction(doc, form, layer));
79 42489 jjdelcerro
                        form.showForm(WindowManager.MODE.WINDOW);
80
                    } catch (Exception ex) {
81
                        String msg = "Can't show form for layer '" + layer.getName() + "'.";
82
                        logger.warn(msg, ex);
83
                        application.messageDialog(msg + "\n\n(See the error log for more information)", "Warning", JOptionPane.WARNING_MESSAGE);
84
                    }
85
                }
86
            }
87
        }
88
    }
89
90 42683 jjdelcerro
    private static class SelectFeatureInTheViewAction extends AbstractAction implements PointListener {
91
92 42775 jjdelcerro
        private final JFeaturesForm form;
93 42683 jjdelcerro
        private final ViewDocument doc;
94
        private MapControl mapControl = null;
95
        private String previousTool = null;
96
        private final FLyrVect layer;
97
98 42775 jjdelcerro
        public SelectFeatureInTheViewAction(ViewDocument doc, JFeaturesForm form, FLayer layer) {
99 42683 jjdelcerro
            I18nManager i18nManager = ToolsLocator.getI18nManager();
100
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
101
102
            this.doc = doc;
103
            this.form = form;
104
            this.putValue(NAME, null);
105
            this.putValue(SHORT_DESCRIPTION, i18nManager.getTranslation("_Select_feature_in_the_View"));
106
            this.putValue(SMALL_ICON, iconTheme.get("layer-selectforediting"));
107
            this.putValue(ACTION_COMMAND_KEY, "selectInTheView");
108
            if( layer instanceof FLyrVect ) {
109
                this.layer = (FLyrVect) layer;
110
                this.setEnabled(true);
111
            } else {
112
                this.layer = null;
113
                this.setEnabled(false);
114
            }
115
        }
116
117
        @Override
118
        public void actionPerformed(ActionEvent ae) {
119
            ApplicationManager application = ApplicationLocator.getManager();
120
            if( this.layer == null ) {
121
                return;
122
            }
123
            if (this.mapControl == null) {
124
                IView view = (IView) application.getActiveComponent(ViewDocument.class);
125
                if (view != null) {
126
                    MapControl mapControl = view.getMapControl();
127
                    this.previousTool = mapControl.getCurrentTool();
128
                    mapControl.addBehavior("layer-selectforediting", new PointBehavior(this));
129
                    mapControl.setTool("layer-selectforediting");
130
                    this.mapControl = mapControl;
131
                    mapControl.requestFocus();
132
                }
133
            } else {
134
                this.mapControl.setTool(this.previousTool);
135
                this.mapControl = null;
136
                this.previousTool = null;
137
            }
138
        }
139
140
        @Override
141
        public void point(PointEvent event) throws BehaviorException {
142
            ApplicationManager application = ApplicationLocator.getManager();
143
            try {
144
                Point point = event.getMapPoint();
145
                double tolerance = mapControl.getViewPort().toMapDistance(7);
146
                GeometryManager manager = GeometryLocator.getGeometryManager();
147
                Circle circle = (Circle) manager.create(
148
                        Geometry.TYPES.CIRCLE,
149
                        Geometry.SUBTYPES.GEOM2D
150
                );
151
                circle.setPoints(point, tolerance);
152
153
                FeatureStore featureStore = this.form.getFeatureStore();
154
                FeatureType featureType = featureStore.getDefaultFeatureType();
155
                FeatureQuery featureQuery = featureStore.createFeatureQuery();
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 43020 jjdelcerro
                Evaluator iee = SpatialEvaluatorsFactory.getInstance().intersects(
165 42683 jjdelcerro
                                query_geo,
166
                                query_proj,
167 43020 jjdelcerro
                                featureStore
168
                );
169 42683 jjdelcerro
                featureQuery.setFilter(iee);
170
                featureQuery.setAttributeNames(null);
171
172
                this.form.setQuery(featureQuery);
173
174
                this.mapControl.setTool(this.previousTool);
175
                this.mapControl = null;
176
                this.previousTool = null;
177
            } catch (Exception e) {
178
                application.message("Can't filter form.", JOptionPane.WARNING_MESSAGE);
179
                throw new RuntimeException("Can't create filter", e);
180
            }
181
        }
182
183
        @Override
184
        public void pointDoubleClick(PointEvent event) throws BehaviorException {
185
            this.point(event);
186
        }
187
188
        @Override
189
        public Image getImageCursor() {
190
            return IconThemeHelper.getImage("cursor-info-by-point");
191
        }
192
193
        @Override
194
        public boolean cancelDrawing() {
195
            return false;
196
        }
197
198
    }
199
200 42489 jjdelcerro
    private static class ZoomToCurrentAction extends AbstractAction {
201
202 42775 jjdelcerro
        private final JFeaturesForm form;
203 42489 jjdelcerro
        private final ViewDocument doc;
204 42619 jjdelcerro
205 42775 jjdelcerro
        public ZoomToCurrentAction(ViewDocument doc, JFeaturesForm form) {
206 42489 jjdelcerro
            I18nManager i18nManager = ToolsLocator.getI18nManager();
207
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
208
209
            this.doc = doc;
210
            this.form = form;
211
            this.putValue(NAME, null);
212
            this.putValue(SHORT_DESCRIPTION, i18nManager.getTranslation("_Zoom"));
213
            this.putValue(SMALL_ICON, iconTheme.get("view-navigation-zoom-to-selection"));
214
            this.putValue(ACTION_COMMAND_KEY, "zoomToCurrent");
215
216 42619 jjdelcerro
            this.setEnabled(doc != null);
217 42489 jjdelcerro
        }
218
219
        @Override
220
        public void actionPerformed(ActionEvent ae) {
221
            long index = this.form.getCurrentIndex();
222 42619 jjdelcerro
            if (index < 0) {
223 42489 jjdelcerro
                return;
224
            }
225
            Feature f = this.form.get(index);
226 42619 jjdelcerro
            if (f == null) {
227 42489 jjdelcerro
                return;
228
            }
229
            Geometry g = f.getDefaultGeometry();
230 42619 jjdelcerro
            if (g != null) {
231 42489 jjdelcerro
                doc.getMapContext().getViewPort().setEnvelope(g.getEnvelope());
232
            }
233
        }
234
235
    }
236
237 42775 jjdelcerro
    private JFeaturesForm createform(FeatureStore featureStore) throws CreateJFeatureFormException, ServiceException, DataException {
238 42489 jjdelcerro
        final DataSwingManager swingManager = DALSwingLocator.getSwingManager();
239 42775 jjdelcerro
        final JFeaturesForm form = swingManager.createJFeaturesForm(featureStore);
240 42489 jjdelcerro
        form.setPreferredSize(new Dimension(400, 300));
241
        return form;
242
    }
243
244
    @Override
245
    public boolean isEnabled() {
246
        return true;
247
    }
248
249
    @Override
250
    public boolean isVisible() {
251
        ApplicationManager application = ApplicationLocator.getManager();
252
        ViewDocument doc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
253
        if (doc == null) {
254
            return false;
255
        }
256
        return doc.getMapContext().hasActiveVectorLayers();
257
    }
258
}