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