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