Revision 45947 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/mapcontrol/tools/PointSelectionListenerUniqueSelectPanel.java
PointSelectionListenerUniqueSelectPanel.java | ||
---|---|---|
1 | 1 |
package org.gvsig.fmap.mapcontrol.tools; |
2 | 2 |
|
3 |
import java.util.ArrayList; |
|
3 |
import java.awt.event.ActionEvent; |
|
4 |
import java.awt.event.ActionListener; |
|
5 |
import java.awt.event.ComponentAdapter; |
|
6 |
import java.awt.event.ComponentEvent; |
|
7 |
import java.awt.event.ComponentListener; |
|
8 |
import java.awt.event.KeyAdapter; |
|
9 |
import java.awt.event.KeyEvent; |
|
4 | 10 |
import java.util.List; |
11 |
import java.util.logging.Level; |
|
12 |
import javax.swing.DefaultListModel; |
|
5 | 13 |
import javax.swing.JComponent; |
14 |
import javax.swing.ListModel; |
|
15 |
import javax.swing.ListSelectionModel; |
|
16 |
import javax.swing.event.ChangeEvent; |
|
17 |
import javax.swing.event.ChangeListener; |
|
18 |
import javax.swing.event.ListSelectionEvent; |
|
19 |
import javax.swing.event.ListSelectionListener; |
|
20 |
import org.apache.commons.lang3.tuple.Pair; |
|
6 | 21 |
import org.gvsig.fmap.dal.exception.DataException; |
7 | 22 |
import org.gvsig.fmap.dal.feature.Feature; |
8 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
|
|
9 |
import org.gvsig.fmap.dal.feature.FeatureSet;
|
|
23 |
import org.gvsig.fmap.dal.feature.FeatureReference;
|
|
24 |
import org.gvsig.fmap.dal.feature.FeatureSelection;
|
|
10 | 25 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
11 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
12 | 26 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
13 | 27 |
import org.gvsig.fmap.dal.swing.featuretable.SimpleFeaturesTableModel; |
14 |
import org.gvsig.tools.ToolsLocator; |
|
15 |
import org.gvsig.tools.i18n.I18nManager; |
|
28 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
|
29 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
|
30 |
import org.gvsig.fmap.mapcontrol.MapControl; |
|
31 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior; |
|
16 | 32 |
import org.gvsig.tools.swing.api.Component; |
17 | 33 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
18 | 34 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
35 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
|
36 |
import org.gvsig.tools.util.UnmodifiableBasicList64; |
|
37 |
import org.slf4j.Logger; |
|
38 |
import org.slf4j.LoggerFactory; |
|
19 | 39 |
/** |
20 | 40 |
* |
21 | 41 |
* @author omartinez |
22 | 42 |
*/ |
23 | 43 |
public class PointSelectionListenerUniqueSelectPanel extends PointSelectionListenerUniqueSelect implements Component { |
24 | 44 |
|
45 |
private static final Logger LOGGER = LoggerFactory.getLogger(PointSelectionListenerUniqueSelectPanel.class); |
|
25 | 46 |
SimpleFeaturesTableModel simpleTableModel; |
47 |
ListModel layersModel; |
|
48 |
List<Pair<FLyrVect, UnmodifiableBasicList64<Feature>>> clickedFeatures; |
|
49 |
private FeatureReference lastFeature; |
|
50 |
private FeatureStore lastStore; |
|
51 |
private final boolean toggle; |
|
52 |
private final MapControl mapControl; |
|
53 |
private final ChangeListener mapControlListener; |
|
26 | 54 |
|
27 |
public PointSelectionListenerUniqueSelectPanel() { |
|
55 |
public PointSelectionListenerUniqueSelectPanel(MapControl mapControl, boolean toggle) { |
|
56 |
this.mapControl = mapControl; |
|
57 |
this.toggle = toggle; |
|
58 |
this.mapControlListener = (ChangeEvent e) -> { |
|
59 |
Behavior currentMapTool = mapControl.getCurrentMapTool(); |
|
60 |
boolean found = false; |
|
61 |
if(currentMapTool instanceof CompoundBehavior){ |
|
62 |
CompoundBehavior behaviors = (CompoundBehavior)currentMapTool; |
|
63 |
for (Behavior behavior : behaviors) { |
|
64 |
if(behavior.getListener() instanceof PointSelectionListener) { |
|
65 |
found = true; |
|
66 |
break; |
|
67 |
} |
|
68 |
} |
|
69 |
|
|
70 |
} else if(currentMapTool.getListener() instanceof PointSelectionListener) { |
|
71 |
found = true; |
|
72 |
} |
|
73 |
if(!found){ |
|
74 |
this.setVisible(false); |
|
75 |
} |
|
76 |
}; |
|
28 | 77 |
this.translate(); |
78 |
initComponents(); |
|
29 | 79 |
} |
30 | 80 |
|
31 | 81 |
protected void translate() { |
32 |
I18nManager i18m = ToolsLocator.getI18nManager(); |
|
33 |
this.lblSelect.setText(i18m.getTranslation("_Choose_features_from_selection")); |
|
82 |
ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager(); |
|
83 |
swingManager.translate(this.btnPrevious); |
|
84 |
swingManager.translate(this.btnNext); |
|
34 | 85 |
} |
35 | 86 |
@Override |
36 | 87 |
public JComponent asJComponent() { |
37 | 88 |
return this; |
38 | 89 |
} |
90 |
|
|
91 |
private void initComponents() { |
|
92 |
this.btnPrevious.addActionListener(new ActionListener() { |
|
93 |
@Override |
|
94 |
public void actionPerformed(ActionEvent e) { |
|
95 |
doPrevious(); |
|
96 |
} |
|
97 |
}); |
|
98 |
this.btnNext.addActionListener(new ActionListener() { |
|
99 |
@Override |
|
100 |
public void actionPerformed(ActionEvent e) { |
|
101 |
doNext(); |
|
102 |
} |
|
103 |
}); |
|
104 |
this.lstLayers.addListSelectionListener(new ListSelectionListener() { |
|
105 |
@Override |
|
106 |
public void valueChanged(ListSelectionEvent e) { |
|
107 |
doSelectLayer(); |
|
108 |
} |
|
109 |
}); |
|
110 |
this.tblSelection.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
|
111 |
this.tblSelection.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
|
112 |
@Override |
|
113 |
public void valueChanged(ListSelectionEvent e) { |
|
114 |
if(e.getValueIsAdjusting()){ |
|
115 |
return; |
|
116 |
} |
|
117 |
doSelectFeature(); |
|
118 |
} |
|
119 |
}); |
|
120 |
this.tblSelection.addKeyListener(new KeyAdapter() { |
|
121 |
@Override |
|
122 |
public void keyPressed(KeyEvent e) { |
|
123 |
super.keyPressed(e); |
|
124 |
if(e.getKeyCode() == KeyEvent.VK_UP){ |
|
125 |
doPrevious(); |
|
126 |
e.consume(); |
|
127 |
} else if(e.getKeyCode() == KeyEvent.VK_DOWN){ |
|
128 |
doNext(); |
|
129 |
e.consume(); |
|
130 |
} |
|
131 |
} |
|
132 |
|
|
133 |
}); |
|
134 |
this.mapControl.addChangeToolListener(this.mapControlListener); |
|
135 |
this.addComponentListener(new ComponentAdapter() { |
|
136 |
@Override |
|
137 |
public void componentHidden(ComponentEvent e) { |
|
138 |
mapControl.removeChangeToolListener(mapControlListener); |
|
139 |
} |
|
140 |
}); |
|
141 |
|
|
142 |
ToolsSwingUtils.ensureRowsCols(this, 12, 50, 20, 100); |
|
143 |
|
|
144 |
} |
|
145 |
|
|
146 |
public void put(List<Pair<FLyrVect, UnmodifiableBasicList64<Feature>>> clickedFeatures) { |
|
147 |
this.clickedFeatures = clickedFeatures; |
|
148 |
layersModel = new DefaultListModel(); |
|
149 |
for (Pair<FLyrVect, UnmodifiableBasicList64<Feature>> pair : clickedFeatures) { |
|
150 |
FLyrVect layer = pair.getLeft(); |
|
151 |
((DefaultListModel)layersModel).addElement(layer); |
|
152 |
if(!toggle){ |
|
153 |
try { |
|
154 |
layer.getFeatureStore().getFeatureSelection().deselectAll(); |
|
155 |
} catch (DataException ex) { |
|
156 |
LOGGER.warn("Can't deselect all", ex); |
|
157 |
} |
|
158 |
} |
|
159 |
} |
|
160 |
this.lstLayers.setModel(layersModel); |
|
161 |
this.lstLayers.setSelectedIndex(0);//.getSelectionModel().setSelectionInterval(0, 0); |
|
162 |
doSelectLayer(); |
|
163 |
this.tblSelection.getSelectionModel().setSelectionInterval(0, 0); |
|
164 |
|
|
165 |
} |
|
166 |
|
|
167 |
private void doPrevious() { |
|
168 |
if (this.simpleTableModel != null) { |
|
169 |
int selectedRow = this.tblSelection.getSelectedRow(); |
|
170 |
if (selectedRow <= 0) { |
|
171 |
int selectedLayerIndex = this.lstLayers.getSelectedIndex(); |
|
172 |
if (selectedLayerIndex <= 0) { |
|
173 |
selectedLayerIndex = this.lstLayers.getModel().getSize()-1; |
|
174 |
} else { |
|
175 |
selectedLayerIndex--; |
|
176 |
} |
|
177 |
this.lstLayers.getSelectionModel().setSelectionInterval( |
|
178 |
selectedLayerIndex, |
|
179 |
selectedLayerIndex |
|
180 |
); |
|
181 |
doSelectLayer(); |
|
182 |
selectedRow = this.tblSelection.getModel().getRowCount()-1; |
|
183 |
} else { |
|
184 |
selectedRow--; |
|
185 |
} |
|
186 |
this.tblSelection.getSelectionModel().setSelectionInterval( |
|
187 |
selectedRow, |
|
188 |
selectedRow |
|
189 |
); |
|
190 |
this.tblSelection.scrollRectToVisible(this.tblSelection.getCellRect(selectedRow,0, true)); |
|
191 |
} |
|
192 |
} |
|
39 | 193 |
|
40 |
void put(FeatureStore store, FeatureSet posibleSelection) { |
|
41 |
FeatureType featureType; |
|
42 |
try { |
|
43 |
featureType = store.getDefaultFeatureType(); |
|
44 |
} catch (DataException ex) { |
|
45 |
return; |
|
194 |
private void doNext() { |
|
195 |
if (this.simpleTableModel != null) { |
|
196 |
int selectedRow = this.tblSelection.getSelectedRow(); |
|
197 |
if (selectedRow >= this.tblSelection.getRowCount()-1) { |
|
198 |
int selectedLayerIndex = this.lstLayers.getSelectedIndex(); |
|
199 |
if (selectedLayerIndex >= this.lstLayers.getModel().getSize()-1) { |
|
200 |
selectedLayerIndex = 0; |
|
201 |
} else { |
|
202 |
selectedLayerIndex++; |
|
203 |
} |
|
204 |
this.lstLayers.getSelectionModel().setSelectionInterval(selectedLayerIndex, selectedLayerIndex); |
|
205 |
doSelectLayer(); |
|
206 |
selectedRow = 0; |
|
207 |
} else { |
|
208 |
selectedRow++; |
|
209 |
} |
|
210 |
this.tblSelection.getSelectionModel().setSelectionInterval(selectedRow, selectedRow); |
|
211 |
this.tblSelection.scrollRectToVisible(this.tblSelection.getCellRect(selectedRow,0, true)); |
|
46 | 212 |
} |
47 |
FeatureAttributeDescriptor[] descriptors = featureType.getAttributeDescriptors(); |
|
48 |
List<String> columnNames = new ArrayList<>(); |
|
49 |
for (FeatureAttributeDescriptor descriptor : descriptors) { |
|
50 |
columnNames.add(descriptor.getName()); |
|
213 |
} |
|
214 |
|
|
215 |
private void doSelectLayer() { |
|
216 |
FLyrVect layer = (FLyrVect) lstLayers.getSelectedValue(); |
|
217 |
if (layer != null) { |
|
218 |
|
|
219 |
this.simpleTableModel = (SimpleFeaturesTableModel) DALSwingLocator.getDataSwingManager().createSimpleFeaturesTableModel( |
|
220 |
layer.getFeatureStore().getDefaultFeatureTypeQuietly(), |
|
221 |
null, |
|
222 |
getFeatures(layer).toList()); |
|
223 |
this.simpleTableModel.setCellRenderers(this.tblSelection); |
|
224 |
this.tblSelection.setModel(simpleTableModel); |
|
225 |
ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager(); |
|
226 |
swingManager.createTableColumnAdjuster(tblSelection); |
|
227 |
|
|
51 | 228 |
} |
229 |
} |
|
52 | 230 |
|
53 |
List<Feature> posibleListSelection = new ArrayList<>(); |
|
54 |
for (Feature feature : posibleSelection) { |
|
55 |
posibleListSelection.add(feature); |
|
231 |
private void doSelectFeature() { |
|
232 |
FLyrVect layer = (FLyrVect) lstLayers.getSelectedValue(); |
|
233 |
if(layer == null){ |
|
234 |
return; |
|
56 | 235 |
} |
236 |
int n = this.tblSelection.getSelectedRow(); |
|
237 |
if(n<0){ |
|
238 |
return; |
|
239 |
} |
|
57 | 240 |
|
58 |
this.simpleTableModel = (SimpleFeaturesTableModel) DALSwingLocator.getDataSwingManager().createSimpleFeaturesTableModel(featureType, columnNames, posibleListSelection); |
|
59 |
|
|
60 |
this.tblSelection.setModel(simpleTableModel); |
|
61 |
ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager(); |
|
62 |
swingManager.createTableColumnAdjuster(tblSelection); |
|
241 |
try { |
|
242 |
if(this.toggle){ |
|
243 |
if(lastFeature != null){ |
|
244 |
FeatureSelection selection = lastStore.getFeatureSelection(); |
|
245 |
if(selection.isSelected(lastFeature)){ |
|
246 |
selection.deselect(lastFeature); |
|
247 |
} else { |
|
248 |
selection.select(lastFeature); |
|
249 |
} |
|
250 |
} |
|
251 |
FeatureStore store = layer.getFeatureStore(); |
|
252 |
Feature f = simpleTableModel.getFeatureAt(n); |
|
253 |
FeatureSelection selection = store.getFeatureSelection(); |
|
254 |
if(selection.isSelected(f)){ |
|
255 |
selection.deselect(f); |
|
256 |
} else { |
|
257 |
selection.select(f); |
|
258 |
} |
|
259 |
this.lastFeature = f.getReference(); |
|
260 |
this.lastStore = store; |
|
261 |
|
|
262 |
} else { |
|
263 |
if(lastFeature != null){ |
|
264 |
lastStore.getFeatureSelection().deselect(lastFeature); |
|
265 |
} |
|
266 |
FeatureStore store = layer.getFeatureStore(); |
|
267 |
Feature f = simpleTableModel.getFeatureAt(n); |
|
268 |
store.getFeatureSelection().select(f); |
|
269 |
this.lastFeature = f.getReference(); |
|
270 |
this.lastStore = store; |
|
271 |
} |
|
272 |
} catch (DataException ex) { |
|
273 |
LOGGER.warn("Can't select feature.", ex); |
|
274 |
} |
|
63 | 275 |
} |
64 | 276 |
|
65 |
public List<Feature> getSelectedFeatures() { |
|
66 |
if (this.simpleTableModel!=null) { |
|
67 |
int[] selectedRows = this.tblSelection.getSelectedRows(); |
|
68 |
List<Feature> selectedFeatures = new ArrayList<>(); |
|
69 |
for (int selectedRow : selectedRows) { |
|
70 |
Feature feature = this.simpleTableModel.get(selectedRow); |
|
71 |
selectedFeatures.add(feature); |
|
277 |
private UnmodifiableBasicList64<Feature> getFeatures(FLayer layer) { |
|
278 |
if (clickedFeatures != null) { |
|
279 |
for (Pair<FLyrVect, UnmodifiableBasicList64<Feature>> pair : clickedFeatures) { |
|
280 |
if (pair.getLeft() == layer) { |
|
281 |
return pair.getRight(); |
|
282 |
} |
|
72 | 283 |
} |
73 |
return selectedFeatures; |
|
74 | 284 |
} |
75 | 285 |
return null; |
76 | 286 |
} |
287 |
|
|
288 |
// void put(FeatureStore store, FeatureSet posibleSelection) { |
|
289 |
// FeatureType featureType; |
|
290 |
// try { |
|
291 |
// featureType = store.getDefaultFeatureType(); |
|
292 |
// } catch (DataException ex) { |
|
293 |
// return; |
|
294 |
// } |
|
295 |
// FeatureAttributeDescriptor[] descriptors = featureType.getAttributeDescriptors(); |
|
296 |
// List<String> columnNames = new ArrayList<>(); |
|
297 |
// for (FeatureAttributeDescriptor descriptor : descriptors) { |
|
298 |
// columnNames.add(descriptor.getName()); |
|
299 |
// } |
|
300 |
// |
|
301 |
// List<Feature> posibleListSelection = new ArrayList<>(); |
|
302 |
// for (Feature feature : posibleSelection) { |
|
303 |
// posibleListSelection.add(feature); |
|
304 |
// } |
|
305 |
// |
|
306 |
// this.simpleTableModel = (SimpleFeaturesTableModel) DALSwingLocator.getDataSwingManager().createSimpleFeaturesTableModel(featureType, columnNames, posibleListSelection); |
|
307 |
// |
|
308 |
// this.tblSelection.setModel(simpleTableModel); |
|
309 |
// ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager(); |
|
310 |
// swingManager.createTableColumnAdjuster(tblSelection); |
|
311 |
// } |
|
77 | 312 |
|
313 |
public Feature getSelectedFeature() { |
|
314 |
if (this.simpleTableModel!=null) { |
|
315 |
int selectedRow = this.tblSelection.getSelectedRow(); |
|
316 |
Feature feature = this.simpleTableModel.get(selectedRow); |
|
317 |
return feature; |
|
318 |
} |
|
319 |
return null; |
|
320 |
} |
|
78 | 321 |
|
322 |
|
|
79 | 323 |
|
80 | 324 |
} |
Also available in: Unified diff