svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.snapping.app / org.gvsig.snapping.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / gui / ViewSnappingInfoImpl.java @ 47355
History | View | Annotate | Download (11 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.app.project.documents.view.gui; |
7 |
|
8 |
import java.awt.Dimension; |
9 |
import java.awt.event.ActionEvent; |
10 |
import java.awt.event.MouseAdapter; |
11 |
import java.awt.event.MouseEvent; |
12 |
import java.net.URL; |
13 |
import java.util.ArrayList; |
14 |
import java.util.Collections; |
15 |
import java.util.Comparator; |
16 |
import java.util.HashSet; |
17 |
import java.util.Iterator; |
18 |
import java.util.List; |
19 |
import java.util.Set; |
20 |
import javax.swing.ImageIcon; |
21 |
import javax.swing.ListSelectionModel; |
22 |
import javax.swing.table.AbstractTableModel; |
23 |
import org.apache.commons.io.FilenameUtils; |
24 |
import org.gvsig.andami.PluginServices; |
25 |
import org.gvsig.app.project.documents.view.gui.ViewSnappingInfoImpl.SnappingTableModel.TableRow; |
26 |
import org.gvsig.fmap.mapcontext.MapContext; |
27 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
28 |
import org.gvsig.fmap.mapcontext.layers.SpatialCache; |
29 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
30 |
import org.gvsig.fmap.mapcontrol.MapControlLocator; |
31 |
import org.gvsig.tools.ToolsLocator; |
32 |
import org.gvsig.tools.i18n.I18nManager; |
33 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
34 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
35 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
36 |
import org.slf4j.Logger; |
37 |
import org.slf4j.LoggerFactory; |
38 |
|
39 |
/**
|
40 |
*
|
41 |
* @author jovivas
|
42 |
*/
|
43 |
public class ViewSnappingInfoImpl extends ViewSnappingInfoView { |
44 |
|
45 |
public static class SnappingTableModel extends AbstractTableModel { |
46 |
|
47 |
public class TableRow { |
48 |
|
49 |
private final FLyrVect layer; |
50 |
private boolean selected; |
51 |
private int cacheFeatures; |
52 |
private final boolean contextEnabled; |
53 |
|
54 |
public TableRow(FLyrVect layer, boolean selected, boolean contextEnabled, int cacheFeatures) { |
55 |
this.layer = layer;
|
56 |
this.selected = selected;
|
57 |
this.contextEnabled = contextEnabled;
|
58 |
this.cacheFeatures = cacheFeatures;
|
59 |
} |
60 |
|
61 |
public int getCacheFeatures() { |
62 |
return cacheFeatures;
|
63 |
} |
64 |
|
65 |
public FLyrVect getLayer() {
|
66 |
return layer;
|
67 |
} |
68 |
|
69 |
public boolean isSelected() { |
70 |
return selected;
|
71 |
} |
72 |
|
73 |
private void setSelected(boolean aValue) { |
74 |
SpatialCache spatialCache = this.layer.getSpatialCache();
|
75 |
spatialCache.setEnabled(aValue); |
76 |
this.selected = aValue;
|
77 |
|
78 |
} |
79 |
|
80 |
private void setCacheFeatures(int aValue) { |
81 |
SpatialCache spatialCache = this.layer.getSpatialCache();
|
82 |
spatialCache.setMaxFeatures(aValue); |
83 |
this.cacheFeatures = aValue;
|
84 |
|
85 |
} |
86 |
|
87 |
} |
88 |
private String[] columnNames; |
89 |
|
90 |
private Class<?>[] columnClases = new Class<?>[]{ |
91 |
Boolean.class,
|
92 |
String.class,
|
93 |
Integer.class
|
94 |
}; |
95 |
|
96 |
private List<TableRow> rows; |
97 |
|
98 |
public SnappingTableModel() {
|
99 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
100 |
this.rows = new ArrayList<>(); |
101 |
columnNames = new String[]{ |
102 |
i18n.getTranslation("_selected"),
|
103 |
i18n.getTranslation("_layer_name"),
|
104 |
i18n.getTranslation("_max_number_of_memory_entities")
|
105 |
}; |
106 |
|
107 |
} |
108 |
|
109 |
@Override
|
110 |
public int getRowCount() { |
111 |
return this.rows.size(); |
112 |
|
113 |
} |
114 |
|
115 |
@Override
|
116 |
public int getColumnCount() { |
117 |
return 3; |
118 |
} |
119 |
|
120 |
@Override
|
121 |
public Class<?> getColumnClass(int columnIndex) { |
122 |
return this.columnClases[columnIndex]; |
123 |
} |
124 |
|
125 |
@Override
|
126 |
public String getColumnName(int column) { |
127 |
return this.columnNames[column]; |
128 |
} |
129 |
|
130 |
@Override
|
131 |
public Object getValueAt(int rowIndex, int columnIndex) { |
132 |
TableRow row = this.rows.get(rowIndex);
|
133 |
switch (columnIndex) {
|
134 |
case 0: |
135 |
return row.isSelected();
|
136 |
case 1: |
137 |
if(row.contextEnabled){
|
138 |
return "<html><b>"+row.getLayer().getName()+"</b></html>"; |
139 |
} |
140 |
return row.getLayer().getName();
|
141 |
case 2: |
142 |
return row.getCacheFeatures();
|
143 |
default:
|
144 |
return ""; |
145 |
|
146 |
} |
147 |
} |
148 |
|
149 |
@Override
|
150 |
public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
151 |
TableRow row = this.rows.get(rowIndex);
|
152 |
switch (columnIndex) {
|
153 |
case 0: |
154 |
row.setSelected((boolean) aValue);
|
155 |
break;
|
156 |
case 2: |
157 |
row.setCacheFeatures((int) aValue);
|
158 |
break;
|
159 |
} |
160 |
fireTableRowsUpdated(rowIndex, rowIndex); |
161 |
} |
162 |
|
163 |
@Override
|
164 |
public boolean isCellEditable(int rowIndex, int columnIndex) { |
165 |
switch (columnIndex) {
|
166 |
case 0: |
167 |
return false; |
168 |
case 1: |
169 |
return false; |
170 |
case 2: |
171 |
return true; |
172 |
default:
|
173 |
return false; |
174 |
|
175 |
} |
176 |
} |
177 |
|
178 |
public TableRow getRow(int rowIndex) { |
179 |
TableRow row = this.rows.get(rowIndex);
|
180 |
return row;
|
181 |
} |
182 |
|
183 |
private void addLayer(FLayer layer) { |
184 |
if (layer instanceof FLyrVect) { |
185 |
FLyrVect layerVect = (FLyrVect) layer; |
186 |
SpatialCache spatialCache = layerVect.getSpatialCache(); |
187 |
TableRow row = new TableRow(
|
188 |
layerVect, |
189 |
layerVect.isLayerToSnap(), |
190 |
spatialCache.isContextEnabled(), |
191 |
spatialCache.getMaxFeatures() |
192 |
); |
193 |
rows.add(row); |
194 |
} |
195 |
} |
196 |
|
197 |
} |
198 |
|
199 |
private static final Logger LOGGER = LoggerFactory.getLogger(ViewSnappingInfoImpl.class); |
200 |
private MapContext mapContext;
|
201 |
|
202 |
public ViewSnappingInfoImpl() {
|
203 |
this.initComponents();
|
204 |
} |
205 |
|
206 |
private void initComponents() { |
207 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
208 |
toolsSwingManager.translate(this.lblTolerance);
|
209 |
toolsSwingManager.translate(this.lblPix);
|
210 |
toolsSwingManager.translate(this.lblMaxSnappingLayers);
|
211 |
toolsSwingManager.translate(this.btnCheckAllSnapping);
|
212 |
toolsSwingManager.translate(this.btnUncheckAllSnapping);
|
213 |
|
214 |
SnappingTableModel model = new SnappingTableModel();
|
215 |
this.tableSnapping.setModel(model);
|
216 |
|
217 |
int tolerance = MapControlLocator.getMapControlManager().getTolerance();
|
218 |
this.textFieldTolSnapping.setText(String.valueOf(tolerance)); |
219 |
|
220 |
this.btnCheckAllSnapping.addActionListener((ActionEvent e) -> { |
221 |
doCheckAll(); |
222 |
}); |
223 |
this.btnUncheckAllSnapping.addActionListener((ActionEvent e) -> { |
224 |
doUncheckAll(); |
225 |
}); |
226 |
|
227 |
this.tableSnapping.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); |
228 |
|
229 |
this.tableSnapping.addMouseListener(new MouseAdapter() { |
230 |
@Override
|
231 |
public void mouseClicked(MouseEvent e) { |
232 |
if (e.getModifiers()==MouseEvent.BUTTON1_MASK){ |
233 |
SnappingTableModel model = (SnappingTableModel) tableSnapping.getModel(); |
234 |
int row = tableSnapping.rowAtPoint(e.getPoint());
|
235 |
model.setValueAt((!(boolean) model.getValueAt(row, 0)), row, 0); |
236 |
doUpdateSelectionFromChecks(); |
237 |
}else{
|
238 |
doUpdateChecksFromSelection(); |
239 |
} |
240 |
} |
241 |
}); |
242 |
|
243 |
this.setPreferredSize(new Dimension(450, 400)); |
244 |
|
245 |
} |
246 |
|
247 |
private void doUpdateChecksFromSelection() { |
248 |
SnappingTableModel model = (SnappingTableModel) this.tableSnapping.getModel();
|
249 |
for (int row = 0; row < model.getRowCount(); row++) { |
250 |
model.setValueAt(this.tableSnapping.isCellSelected(row, 0), row, 0); |
251 |
} |
252 |
} |
253 |
|
254 |
private void doUpdateSelectionFromChecks() { |
255 |
SnappingTableModel model = (SnappingTableModel) this.tableSnapping.getModel();
|
256 |
tableSnapping.getSelectionModel().clearSelection(); |
257 |
for (int row = 0; row < model.getRowCount(); row++) { |
258 |
if ((boolean) model.getValueAt(row, 0)){ |
259 |
tableSnapping.getSelectionModel().addSelectionInterval(row, row); |
260 |
} |
261 |
} |
262 |
} |
263 |
|
264 |
private void doUncheckAll() { |
265 |
SnappingTableModel model = (SnappingTableModel) this.tableSnapping.getModel();
|
266 |
for (int row = 0; row < model.getRowCount(); row++) { |
267 |
model.setValueAt(false, row, 0); |
268 |
} |
269 |
doUpdateSelectionFromChecks(); |
270 |
} |
271 |
|
272 |
private void doCheckAll() { |
273 |
SnappingTableModel model = (SnappingTableModel) this.tableSnapping.getModel();
|
274 |
for (int row = 0; row < model.getRowCount(); row++) { |
275 |
model.setValueAt(true, row, 0); |
276 |
} |
277 |
doUpdateSelectionFromChecks(); |
278 |
} |
279 |
|
280 |
@Override
|
281 |
public ImageIcon loadImage(String imageName) { |
282 |
String name = FilenameUtils.getBaseName(imageName);
|
283 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
284 |
if (theme.exists(name)) {
|
285 |
return theme.get(name);
|
286 |
} |
287 |
URL url = this.getClass().getResource(name + ".png"); |
288 |
if (url == null) { |
289 |
return null; |
290 |
} |
291 |
return new ImageIcon(url); |
292 |
} |
293 |
|
294 |
public void applyChanges() { |
295 |
SnappingTableModel model = (SnappingTableModel) this.tableSnapping.getModel();
|
296 |
Set<FLyrVect> layersToSnap = new HashSet<>(); |
297 |
for (int i = 0; i < model.getRowCount(); i++) { |
298 |
TableRow row = model.getRow(i); |
299 |
FLyrVect layer = row.getLayer(); |
300 |
layer.setMaxFeaturesInEditionCache(row.getCacheFeatures()); |
301 |
layer.setLayerToSnap(row.isSelected()); |
302 |
} |
303 |
|
304 |
try {
|
305 |
MapControlLocator.getMapControlManager().setTolerance(Integer.parseInt(this.textFieldTolSnapping.getText())); |
306 |
|
307 |
} catch (Exception e) { |
308 |
LOGGER.error(PluginServices.getText(this, "tolerancia_incorrecta"), e); |
309 |
|
310 |
} |
311 |
this.mapContext.invalidate();
|
312 |
} |
313 |
|
314 |
public void setMapContext(MapContext mapContext){ |
315 |
this.mapContext = mapContext;
|
316 |
|
317 |
List<FLayer>layers = new ArrayList<>(); |
318 |
Iterator<FLayer> layersUnordered = mapContext.deepiterator();
|
319 |
while (layersUnordered.hasNext()) {
|
320 |
layers.add(layersUnordered.next()); |
321 |
} |
322 |
Collections.sort(layers, new Comparator<FLayer>() { |
323 |
@Override
|
324 |
public int compare(FLayer o1, FLayer o2) { |
325 |
return o1.getName().compareToIgnoreCase(o2.getName());
|
326 |
} |
327 |
}); |
328 |
SnappingTableModel model = new SnappingTableModel();
|
329 |
for (FLayer layer : layers) {
|
330 |
model.addLayer(layer); |
331 |
} |
332 |
|
333 |
this.tableSnapping.setModel(model);
|
334 |
|
335 |
|
336 |
} |
337 |
|
338 |
} |