Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / tools / PointSelectionListener.java @ 45680

History | View | Annotate | Download (7.8 KB)

1 40559 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40559 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 40559 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 40559 jjdelcerro
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20 40435 jjdelcerro
 *
21 40559 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.fmap.mapcontrol.tools;
25
26
import java.awt.Image;
27 45104 omartinez
import java.awt.event.ActionEvent;
28 40435 jjdelcerro
import java.awt.geom.Point2D;
29 45104 omartinez
import java.util.List;
30 40435 jjdelcerro
31
import org.gvsig.fmap.IconThemeHelper;
32
import org.gvsig.fmap.dal.exception.DataException;
33 41175 nbrodin
import org.gvsig.fmap.dal.feature.Feature;
34 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureSelection;
35
import org.gvsig.fmap.dal.feature.FeatureSet;
36
import org.gvsig.fmap.mapcontext.layers.FLayer;
37
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
38
import org.gvsig.fmap.mapcontrol.MapControl;
39
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent;
40
import org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener;
41 45104 omartinez
import org.gvsig.tools.ToolsLocator;
42 41175 nbrodin
import org.gvsig.tools.dispose.DisposableIterator;
43 45104 omartinez
import org.gvsig.tools.i18n.I18nManager;
44
import org.gvsig.tools.swing.api.ToolsSwingLocator;
45
import org.gvsig.tools.swing.api.windowmanager.Dialog;
46
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
47
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
48 40435 jjdelcerro
49
/**
50 45099 omartinez
 * <p>
51
 * Listener that selects all features of the active, and vector layers of the
52
 * associated <code>MapControl</code> that their area intersects with the point
53
 * selected by a single click of any button of the mouse.</p>
54 40435 jjdelcerro
 *
55
 * @author Vicente Caballero Navarro
56
 */
57
public class PointSelectionListener implements PointListener {
58
59 45099 omartinez
    /**
60
     * Reference to the <code>MapControl</code> object that uses.
61
     */
62
    protected MapControl mapCtrl;
63 40435 jjdelcerro
64 45099 omartinez
    /**
65
     * <p>
66
     * Creates a new <code>PointSelectionListener</code> object.</p>
67
     *
68
     * @param mc the <code>MapControl</code> where will be applied the changes
69
     */
70
    public PointSelectionListener(MapControl mc) {
71
        this.mapCtrl = mc;
72
    }
73
74
    public void point(PointEvent event) throws BehaviorException {
75
        try {
76 40435 jjdelcerro
            Point2D p = event.getPoint();
77
            Point2D mapPoint = mapCtrl.getViewPort().toMapPoint((int) p.getX(), (int) p.getY());
78
79
            // Tolerancia de 3 pixels
80 45104 omartinez
            I18nManager i18n = ToolsLocator.getI18nManager();
81 40435 jjdelcerro
            double tol = mapCtrl.getViewPort().toMapDistance(3);
82 41175 nbrodin
            FLayer[] actives = mapCtrl.getMapContext().getLayers().getActives();
83
            for (int i = 0; i < actives.length; i++) {
84 40435 jjdelcerro
                if (actives[i] instanceof FLyrVect) {
85
                    FLyrVect lyrVect = (FLyrVect) actives[i];
86 45099 omartinez
                    FeatureSet newSelection = null;
87
                    try {
88
                        newSelection = lyrVect.queryByPoint(
89
                                mapPoint, tol,
90
                                lyrVect.getFeatureStore().getDefaultFeatureType());
91
                        if (event.getEvent().isControlDown()) {
92
                            FeatureSelection currentSelection = (FeatureSelection) lyrVect.getDataStore().getSelection();
93
                            DisposableIterator it = newSelection.fastIterator();
94
                            while (it.hasNext()) {
95
                                Object obj = it.next();
96
                                if (obj instanceof Feature) {
97
                                    Feature feat = ((Feature) obj);
98
                                    if (currentSelection.isSelected(feat)) {
99
                                        currentSelection.deselect(feat);
100
                                    } else {
101
                                        currentSelection.select(feat);
102
                                    }
103
                                }
104
                            }
105
                        } else {
106
                            lyrVect.getFeatureStore()
107
                                    .setSelection(newSelection);
108
                        }
109 45104 omartinez
                        if (event.getEvent().isShiftDown()) {
110
                            //select features in the feature selection in a table
111
                            FeatureSelection actualSelection = lyrVect.getFeatureStore().getFeatureSelection();
112
                            if (actualSelection.getSize() > 1) {
113
114
                                WindowManager_v2 winmanager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
115
                                PointSelectionListenerUniqueSelectPanel panel = new PointSelectionListenerUniqueSelectPanel();
116
                                panel.put(lyrVect.getFeatureStore(), actualSelection);
117
                                final Dialog dialog = winmanager.createDialog(
118
                                        panel.asJComponent(),
119
                                        i18n.getTranslation("_Features_selector"),
120
                                        null,
121
                                        WindowManager_v2.BUTTONS_OK_CANCEL
122
                                );
123
                                dialog.addActionListener((ActionEvent e) -> {
124
                                    if (dialog.getAction() == WindowManager_v2.BUTTONS_OK) {
125
                                        List<Feature> selectedFeatures = panel.getSelectedFeatures();
126
                                        FeatureSelection newFeatureSelection = null;
127
                                        try {
128
                                            newFeatureSelection = lyrVect.getFeatureStore().createFeatureSelection();
129
                                        if (newFeatureSelection!=null) {
130
                                            for (Feature selectedFeature : selectedFeatures) {
131
                                                newFeatureSelection.select(selectedFeature.getReference());
132
                                            }
133
                                        }
134
                                            lyrVect.getFeatureStore().setSelection(newFeatureSelection);
135
                                        } catch (DataException ex) {
136
137
                                        } finally {
138
139
                                        }
140
                                    }
141
                                });
142
                                dialog.show(WindowManager.MODE.DIALOG);
143
                            }
144
                        }
145 45099 omartinez
                    } finally {
146
                        if (newSelection != null) {
147
                            newSelection.dispose();
148
                        }
149 40435 jjdelcerro
                    }
150
                    mapCtrl.drawMap(false); // FIXME Esto deber?a sobrar (la capa deber?a de ser observada)
151
                }
152
            }
153
154 45099 omartinez
        } catch (DataException e) {
155
            throw new BehaviorException("No se pudo hacer la selecci?n", e);
156
        }
157
    }
158 40435 jjdelcerro
159 45099 omartinez
    public Image getImageCursor() {
160
        return IconThemeHelper.getImage("cursor-select-by-point");
161
    }
162 40435 jjdelcerro
163 45099 omartinez
    public boolean cancelDrawing() {
164
        return false;
165
    }
166 40435 jjdelcerro
167 45099 omartinez
    public void pointDoubleClick(PointEvent event) throws BehaviorException {
168 40435 jjdelcerro
169 45099 omartinez
    }
170 40435 jjdelcerro
}