gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.app / org.gvsig.vcsgis.app.mainplugin / src / main / java / org / gvsig / vcsgis / app / addlayer / WizardVCSGisVectorial.java @ 9312
History | View | Annotate | Download (13.1 KB)
1 |
package org.gvsig.vcsgis.app.addlayer; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Comparator; |
5 |
import java.util.List; |
6 |
import java.util.function.Function; |
7 |
import javax.swing.JOptionPane; |
8 |
import javax.swing.SwingUtilities; |
9 |
import org.apache.commons.lang.StringUtils; |
10 |
import org.cresques.cts.IProjection; |
11 |
import org.gvsig.fmap.dal.DALLocator; |
12 |
import org.gvsig.fmap.dal.DataManager; |
13 |
import org.gvsig.fmap.dal.DataStoreParameters; |
14 |
import org.gvsig.fmap.dal.StoresRepository; |
15 |
import org.gvsig.fmap.dal.feature.FeatureType; |
16 |
import org.gvsig.fmap.dal.store.db.DBStoreParameters; |
17 |
import org.gvsig.fmap.geom.Geometry; |
18 |
import org.gvsig.fmap.geom.type.GeometryType; |
19 |
import org.gvsig.fmap.mapcontext.BaseCreateLayerParameters; |
20 |
import org.gvsig.fmap.mapcontext.MapContext; |
21 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
22 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
23 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
24 |
import org.gvsig.fmap.mapcontrol.MapControl; |
25 |
import org.gvsig.geodb.TableInfo; |
26 |
import static org.gvsig.geodb.vectorialdb.visibility.LayerScaleVisibilityUtils.checkForVisibilityScale; |
27 |
import org.gvsig.tools.ToolsLocator; |
28 |
import org.gvsig.tools.i18n.I18nManager; |
29 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
30 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
31 |
import org.gvsig.tools.task.SimpleTaskStatus; |
32 |
import static org.gvsig.vcsgis.app.addlayer.AbstractWizardVCSGis.LOGGER; |
33 |
import org.gvsig.vcsgis.lib.VCSGisEntity; |
34 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace; |
35 |
|
36 |
/**
|
37 |
*
|
38 |
* @author gvSIG Team
|
39 |
*/
|
40 |
@SuppressWarnings("UseSpecificCatch") |
41 |
public class WizardVCSGisVectorial extends AbstractWizardVCSGis { |
42 |
|
43 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
44 |
public WizardVCSGisVectorial() {
|
45 |
super();
|
46 |
this.setVisibleMaintainStructureInToC(true); |
47 |
} |
48 |
|
49 |
@Override
|
50 |
protected boolean requireGeometry() { |
51 |
return true; |
52 |
} |
53 |
|
54 |
@Override
|
55 |
public Object executeWizard() { |
56 |
DataManager man = DALLocator.getDataManager(); |
57 |
MapContextManager mapContextManager = MapContextLocator.getMapContextManager(); |
58 |
|
59 |
MapControl mapControl = this.getMapCtrl();
|
60 |
MapContext mapContext = mapControl.getMapContext(); |
61 |
|
62 |
LayersWithGroup layers = new LayersWithGroup(mapContext);
|
63 |
VCSGisWorkspace workspace = this.getWorkspace();
|
64 |
boolean checkScale = this.getCheckScale(); |
65 |
boolean addToCWhenLoadingAll = this.getAddToCWhenLoadingAll(); |
66 |
|
67 |
for (TableInfo tableInfo : getTablesInformation()) {
|
68 |
if (!tableInfo.isSelected()) {
|
69 |
continue;
|
70 |
} |
71 |
if( ((VCSGisTableInfo)tableInfo).needConnectToModel() ) {
|
72 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
73 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
74 |
int n = dialogs.confirmDialog(
|
75 |
i18n.getTranslation("_Some_of_the_selected_tables_require_connection_to_a_data_model") +
|
76 |
"\n" +
|
77 |
i18n.getTranslation("_It_is_recommended_that_you_connect_to_the_data_model_before_loading_the_tables") +
|
78 |
"\n" +
|
79 |
i18n.getTranslation("_Do_you_want_to_add_the_selected_tables_anyway"),
|
80 |
i18n.getTranslation("_AddLayer"),
|
81 |
JOptionPane.YES_NO_OPTION,
|
82 |
JOptionPane.QUESTION_MESSAGE,
|
83 |
"_Some_of_the_selected_tables_require_connection_to_a_data_model"
|
84 |
); |
85 |
if( n != JOptionPane.YES_OPTION ) { |
86 |
return null; |
87 |
} |
88 |
} |
89 |
} |
90 |
|
91 |
Thread th = new Thread(() -> { |
92 |
SimpleTaskStatus taskstatus = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("Loading layers");
|
93 |
taskstatus.setIndeterminate(); |
94 |
taskstatus.setAutoremove(true);
|
95 |
taskstatus.add(); |
96 |
try {
|
97 |
List<VCSGisTableInfo> tablesInformation = new ArrayList<>(); |
98 |
for (TableInfo tableInfo : getTablesInformation()) {
|
99 |
if (!tableInfo.isSelected()) {
|
100 |
continue;
|
101 |
} |
102 |
tablesInformation.add((VCSGisTableInfo) tableInfo); |
103 |
} |
104 |
tablesInformation.sort(new Comparator<VCSGisTableInfo>() { |
105 |
@Override
|
106 |
public int compare(VCSGisTableInfo o1, VCSGisTableInfo o2) { |
107 |
if( o1==null || o2==null ) { |
108 |
return 0; |
109 |
} |
110 |
VCSGisEntity e1 = o1.getEntity(); |
111 |
VCSGisEntity e2 = o2.getEntity(); |
112 |
if( e1==null || e2==null ) { |
113 |
return 0; |
114 |
} |
115 |
FeatureType ft1 = o1.getFeatureType(); |
116 |
FeatureType ft2 = o2.getFeatureType(); |
117 |
int order1 = (ft1==null)? 0:ft1.getTags().getInt("TOC_Order",0); |
118 |
int order2 = (ft2==null)? 0:ft2.getTags().getInt("TOC_Order",0); |
119 |
String id1 = String.format("%05d/%02d/%s/%s", order1, getLayerOrderWeight(e1), getCategory(e1), e1.getLabelOrName()); |
120 |
String id2 = String.format("%05d/%02d/%s/%s", order2, getLayerOrderWeight(e2), getCategory(e2), e2.getLabelOrName()); |
121 |
return id2.compareTo(id1);
|
122 |
} |
123 |
}); |
124 |
|
125 |
List<String> dataModels = new ArrayList<>(); |
126 |
for (VCSGisTableInfo tableInfo : tablesInformation) {
|
127 |
VCSGisEntity entity = tableInfo.getEntity(); |
128 |
List<String> entityModels = entity.getDataModelsAsList(); |
129 |
if( entityModels!=null ) { |
130 |
for (String entityModel : entityModels) { |
131 |
dataModels.add(entityModel); |
132 |
} |
133 |
} |
134 |
} |
135 |
if( !dataModels.isEmpty() ) {
|
136 |
for (StoresRepository storesRepository : man.getStoresRepository().getSubrepositories() ) {
|
137 |
dataModels.remove(storesRepository.getID()); |
138 |
} |
139 |
for (String dataModel : dataModels) { |
140 |
registerDataModelRepository(workspace, dataModel); |
141 |
} |
142 |
} |
143 |
|
144 |
taskstatus.setRangeOfValues(0, tablesInformation.size());
|
145 |
FLayer layer; |
146 |
for (VCSGisTableInfo tableInfo : tablesInformation) {
|
147 |
if( taskstatus.isCancellationRequested() ) {
|
148 |
taskstatus.cancel(); |
149 |
return;
|
150 |
} |
151 |
taskstatus.message(tableInfo.getEntity().getLabelOrName()); |
152 |
LOGGER.info("Load layer '"+tableInfo.getId()+"'"); |
153 |
DBStoreParameters params = tableInfo.getParameters(); |
154 |
VCSGisEntity entity = tableInfo.getEntity(); |
155 |
List<String> entityModels = entity.getDataModelsAsList(); |
156 |
if( entityModels!=null ) { |
157 |
for (String entityModel : entityModels) { |
158 |
dataModels.add(entityModel); |
159 |
} |
160 |
} |
161 |
String category = this.getCategory(entity); |
162 |
fixCRS(params); |
163 |
|
164 |
if( tableInfo.getVisible()==null ) { |
165 |
tableInfo.setVisible(entity.getCategoryTags(category).getBoolean("visibility", true)); |
166 |
} |
167 |
boolean defaultVisibility = tableInfo.getVisible();
|
168 |
|
169 |
// FeatureStore store;
|
170 |
// try {
|
171 |
// store = (FeatureStore) man.openStore(
|
172 |
// params.getDataStoreName(),
|
173 |
// params
|
174 |
// );
|
175 |
// } catch (Exception e) {
|
176 |
// LOGGER.warn("While opening DB store.", e);
|
177 |
// taskstatus.abort();
|
178 |
// return;
|
179 |
// }
|
180 |
try {
|
181 |
|
182 |
layer = mapContextManager.createLayer( |
183 |
tableInfo.getDocumentName(), |
184 |
new BaseCreateLayerParameters(params, defaultVisibility)
|
185 |
); |
186 |
// layer = mapContextManager.createLayer(
|
187 |
// tableInfo.getDocumentName(),
|
188 |
// store
|
189 |
// );
|
190 |
if( defaultVisibility ) {
|
191 |
checkForVisibilityScale(layer, checkScale); |
192 |
} |
193 |
|
194 |
String tableName = params.getTable();
|
195 |
if (StringUtils.isBlank(tableName)) {
|
196 |
layers.add(null, null, layer); |
197 |
} else {
|
198 |
if (workspace.getWorkspaceEntityByName(tableName) == null) { |
199 |
layers.add(null, tableName, layer);
|
200 |
} else {
|
201 |
layers.add(category, layer.getName(), layer); |
202 |
} |
203 |
} |
204 |
if( !addToCWhenLoadingAll ) {
|
205 |
SwingUtilities.invokeLater(() -> {
|
206 |
layers.updateMapControl(mapControl, maintainStructureInToC(), LayersWithGroup.UPDATE_MODE_SKIP_DUPLICATES); |
207 |
}); |
208 |
} |
209 |
} catch (Exception e) { |
210 |
LOGGER.warn("Can't create layer '" + tableInfo.getDocumentName() + "'.", e); |
211 |
} finally {
|
212 |
// DisposeUtils.disposeQuietly(store);
|
213 |
} |
214 |
taskstatus.incrementCurrentValue(); |
215 |
} |
216 |
if( addToCWhenLoadingAll ) {
|
217 |
SwingUtilities.invokeLater(() -> {
|
218 |
layers.updateMapControl(mapControl, maintainStructureInToC(), false);
|
219 |
layers.disposeLayers(); |
220 |
}); |
221 |
} else {
|
222 |
SwingUtilities.invokeLater(() -> {
|
223 |
layers.disposeLayers(); |
224 |
}); |
225 |
} |
226 |
taskstatus.terminate(); |
227 |
} finally {
|
228 |
// Dispose all created layers. If they have been included into
|
229 |
// a FLayers object, they will have been binded there.
|
230 |
// for (FLayer layer : layers) {
|
231 |
// layer.dispose();
|
232 |
// }
|
233 |
} |
234 |
}, "WizardVCSGisVectorial");
|
235 |
this.startTask(th);
|
236 |
return null; |
237 |
} |
238 |
|
239 |
private void fireLayerErrorEvent(FLayer lyr, MapControl mapControl) { |
240 |
mapControl.getMapContext().callNewErrorEvent(null);
|
241 |
} |
242 |
|
243 |
@Override
|
244 |
protected Function<VCSGisEntity, String> getLabelEntityFormatter() { |
245 |
return (VCSGisEntity entity) -> {
|
246 |
String label = this.getWorkspace().formatEntityLabel(entity); |
247 |
FeatureType type = entity.getFeatureType(); |
248 |
if (type == null) { |
249 |
return label;
|
250 |
} |
251 |
IProjection srs = type.getDefaultSRS(); |
252 |
if (srs == null) { |
253 |
return label;
|
254 |
} |
255 |
MapContext mapContext1 = getMapContext(); |
256 |
if (mapContext1 == null) { |
257 |
return label + " [" + srs.getAbrev() + "]"; |
258 |
} |
259 |
if (mapContext1.getProjection() == null) { |
260 |
return label + " [" + srs.getAbrev() + "]"; |
261 |
} |
262 |
if (StringUtils.equalsIgnoreCase(mapContext1.getProjection().getAbrev(), srs.getAbrev())) {
|
263 |
return label + " [" + srs.getAbrev() + "]"; |
264 |
} |
265 |
return label + " [" + srs.getAbrev() + "!]"; |
266 |
}; |
267 |
} |
268 |
|
269 |
private int getLayerOrderWeight(VCSGisEntity entity) { |
270 |
|
271 |
int type2d = Geometry.TYPES.SURFACE;
|
272 |
try {
|
273 |
GeometryType geomtype = entity.getFeatureType().getDefaultGeometryAttribute().getGeomType(); |
274 |
type2d = simplifyType(geomtype); |
275 |
} catch (Exception e) { |
276 |
LOGGER.warn("While getting geo type.", e);
|
277 |
} |
278 |
switch (type2d) {
|
279 |
case Geometry.TYPES.SURFACE:
|
280 |
return 2; |
281 |
case Geometry.TYPES.CURVE:
|
282 |
return 1; |
283 |
case Geometry.TYPES.POINT:
|
284 |
return 0; |
285 |
default:
|
286 |
// should not reach this
|
287 |
return 3; |
288 |
} |
289 |
|
290 |
} |
291 |
|
292 |
private int simplifyType(GeometryType gt) { |
293 |
if (gt.isTypeOf(Geometry.TYPES.POINT) || gt.isTypeOf(Geometry.TYPES.MULTIPOINT)) {
|
294 |
return Geometry.TYPES.POINT;
|
295 |
|
296 |
} else if (gt.isTypeOf(Geometry.TYPES.CURVE) || gt.isTypeOf(Geometry.TYPES.MULTICURVE)) { |
297 |
return Geometry.TYPES.CURVE;
|
298 |
|
299 |
} else if (gt.isTypeOf(Geometry.TYPES.SURFACE) || gt.isTypeOf(Geometry.TYPES.MULTISURFACE)) { |
300 |
return Geometry.TYPES.SURFACE;
|
301 |
|
302 |
} |
303 |
return Geometry.TYPES.SURFACE;
|
304 |
} |
305 |
} |