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