Statistics
| Revision:

gvsig-attributeeditor / org.gvsig.attributeeditor / trunk / org.gvsig.attributeeditor / org.gvsig.attributeeditor.mapcontrol / src / main / java / org / gvsig / fmap / mapcontrol / tools / Listeners / AttributeEditorPointListener.java @ 43

History | View | Annotate | Download (8.55 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2014 gvSIG Association
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., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23

    
24
package org.gvsig.fmap.mapcontrol.tools.Listeners;
25

    
26
import java.awt.Dimension;
27
import java.awt.event.ComponentEvent;
28
import java.awt.event.ComponentListener;
29
import java.awt.event.InputEvent;
30
import java.awt.event.MouseEvent;
31

    
32
import javax.swing.JOptionPane;
33
import javax.swing.SwingUtilities;
34

    
35
import org.cresques.cts.IProjection;
36
import org.gvsig.featureform.swing.CreateJFeatureFormException;
37
import org.gvsig.featureform.swing.JFeatureForm;
38
import org.slf4j.Logger;
39
import org.slf4j.LoggerFactory;
40

    
41

    
42
import org.gvsig.fmap.dal.exception.DataException;
43
import org.gvsig.fmap.dal.feature.FeatureQuery;
44
import org.gvsig.fmap.dal.feature.FeatureStore;
45
import org.gvsig.fmap.dal.feature.FeatureType;
46
import org.gvsig.fmap.dal.swing.DALSwingLocator;
47
import org.gvsig.fmap.dal.swing.DataSwingManager;
48
import org.gvsig.fmap.geom.Geometry;
49
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
50
import org.gvsig.fmap.geom.Geometry.TYPES;
51
import org.gvsig.fmap.geom.GeometryLocator;
52
import org.gvsig.fmap.geom.GeometryManager;
53
import org.gvsig.fmap.geom.exception.CreateGeometryException;
54
import org.gvsig.fmap.geom.primitive.Circle;
55
import org.gvsig.fmap.geom.primitive.Point;
56
import org.gvsig.fmap.mapcontext.ViewPort;
57
import org.gvsig.fmap.mapcontext.layers.FLayer;
58
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
59
import org.gvsig.fmap.mapcontext.layers.vectorial.IntersectsGeometryEvaluator;
60
import org.gvsig.fmap.mapcontrol.MapControl;
61
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
62
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent;
63
import org.gvsig.tools.ToolsLocator;
64
import org.gvsig.tools.i18n.I18nManager;
65
import org.gvsig.tools.service.ServiceException;
66
import org.gvsig.tools.swing.api.ToolsSwingLocator;
67
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE;
68

    
69
/**
70
 * @author fdiaz
71
 *
72
 */
73
public class AttributeEditorPointListener extends AbstractPointListener implements ComponentListener{
74

    
75
    private static Logger logger = LoggerFactory.getLogger(AttributeEditorPointListener.class);
76

    
77
    public static final String ATTRIBUTE_EDITOR_TOOL_NAME = "attributeEditorTool";
78
    private static final String ATTRIBUTE_EDITOR_FORM_NAME = "attributeEditorForm";
79
    private MapControl mapControl;
80
    private JFeatureForm form;
81
    private FLyrVect currentLayer = null;
82
    /**
83
     * Radius as tolerance around the selected point, the area will be used to
84
     * look for information.
85
     */
86
    private static int TOL = 7;
87

    
88
    public AttributeEditorPointListener(MapControl mapControl,
89
        JFeatureForm form) {
90
        super();
91
        this.mapControl = mapControl;
92
        this.form = form;
93
    }
94

    
95
    public AttributeEditorPointListener(MapControl mapControl) {
96
        super();
97
        this.mapControl = mapControl;
98
    }
99

    
100
    private FLyrVect getLayer(){
101
        FLayer[] layers = mapControl.getMapContext().getLayers().getActives();
102
        if(layers==null || layers.length!=1){
103
            return null;
104
        }
105
        return (FLyrVect) layers[0];
106

    
107
    }
108

    
109
    public void point(PointEvent e) throws BehaviorException {
110
        MouseEvent mouseEvent = e.getEvent();
111
        if (((mouseEvent.getModifiersEx() & (InputEvent.CTRL_DOWN_MASK
112
            | InputEvent.SHIFT_DOWN_MASK | InputEvent.ALT_DOWN_MASK
113
            | InputEvent.ALT_GRAPH_DOWN_MASK | InputEvent.META_DOWN_MASK)) != 0) || !SwingUtilities.isLeftMouseButton(mouseEvent)) {
114
            return;
115
        }
116

    
117
        I18nManager i18nManager = ToolsLocator.getI18nManager();
118
        String layerName = "";
119
        try {
120
            FLyrVect layer = getLayer();
121
            if (layer == null) {
122
                String msg = i18nManager.getTranslation("There_is_no_one_and_only_one_active_layer");
123
                ToolsSwingLocator.getThreadSafeDialogsManager().messageDialog(
124
                    msg,
125
                    i18nManager.getTranslation("attribute_editor"),
126
                    JOptionPane.INFORMATION_MESSAGE);
127
                return;
128
            }
129
            if (layer != this.currentLayer) {
130
                if (this.currentLayer != null) {
131
                    if (this.form != null) {
132
                        this.form.saveChanges();
133
                    }
134
                }
135
                this.currentLayer = layer;
136
                if (this.form != null) {
137
                    FeatureStore featureStore =
138
                        this.currentLayer.getFeatureStore();
139
                    this.form.bind(featureStore);
140
                }
141
            }
142
            layerName = layer.getName();
143
            ViewPort vp = this.mapControl.getViewPort();
144
            Point point = vp.convertToMapPoint(e.getPoint());
145
            double tolerance = vp.toMapDistance(TOL);
146
            FeatureQuery query = null;
147
            query = queryByPoint(point, tolerance);
148
            if (query != null) {
149
                if (this.form == null) {
150
                    this.form = getForm();
151
                    this.form.showForm(MODE.TOOL);
152
                }
153
                this.form.setQuery(query);
154
            }
155
        } catch (Exception ex) {
156
            logger.warn("Can't show attribute information from point on the layer "+layerName, ex);
157
            String msg = i18nManager.getTranslation("Cant_show_attribute_information_for_layer_%1_on_point",new String[]{layerName});
158
            ToolsSwingLocator.getThreadSafeDialogsManager().messageDialog(
159
                msg,
160
                i18nManager.getTranslation("attribute_editor"),
161
                JOptionPane.WARNING_MESSAGE);
162
        }
163
    }
164

    
165
    private JFeatureForm getForm() throws CreateJFeatureFormException, ServiceException, DataException {
166
        if (this.form == null) {
167
            DataSwingManager swingManager =
168
                DALSwingLocator.getSwingManager();
169
                FeatureStore store = this.currentLayer.getFeatureStore();
170
                this.form = swingManager.createJFeatureForm(store);
171
                this.form.setPreferredSize(new Dimension(300, 200));
172
                this.form.asJComponent().setName(ATTRIBUTE_EDITOR_FORM_NAME);
173
                this.form.asJComponent().addComponentListener(this);
174
        }
175
        return this.form;
176
    }
177

    
178
    private FeatureQuery queryByPoint(Point point, double tol)
179
        throws CreateGeometryException, DataException {
180
        GeometryManager manager = GeometryLocator.getGeometryManager();
181
        Circle circle = (Circle) manager.create(TYPES.CIRCLE, SUBTYPES.GEOM2D);
182
        circle.setPoints(point, tol);
183

    
184
        FeatureStore featureStore = this.currentLayer.getFeatureStore();
185
        FeatureQuery featureQuery = featureStore.createFeatureQuery();
186
        FeatureType featureType = featureStore.getDefaultFeatureType();
187
        String geomName = featureType.getDefaultGeometryAttributeName();
188
        featureQuery.setFeatureType(featureType);
189

    
190
        Geometry query_geo =
191
            this.currentLayer.transformToSourceCRS(circle, true);
192

    
193
        IProjection query_proj = this.currentLayer.getMapContext().getProjection();
194
        if (this.currentLayer.getCoordTrans() != null) {
195
            query_proj = this.currentLayer.getCoordTrans().getPOrig();
196
        }
197

    
198
        IntersectsGeometryEvaluator iee =
199
            new IntersectsGeometryEvaluator(query_geo, query_proj,
200
                featureStore.getDefaultFeatureType(), geomName);
201
        featureQuery.setFilter(iee);
202
        featureQuery.setAttributeNames(null);
203
        return featureQuery;
204
    }
205

    
206
    public void componentResized(ComponentEvent e) {
207
        // TODO Auto-generated method stub
208

    
209
    }
210

    
211
    public void componentMoved(ComponentEvent e) {
212
        // TODO Auto-generated method stub
213

    
214
    }
215

    
216
    public void componentShown(ComponentEvent e) {
217
        // TODO Auto-generated method stub
218

    
219
    }
220

    
221
    public void componentHidden(ComponentEvent e) {
222
        this.form = null;
223
    }
224

    
225
}