svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.geodb.app / org.gvsig.geodb.app.mainplugin / src / main / java / org / gvsig / geodb / WizardDBVectorial.java @ 45635
History | View | Annotate | Download (11.7 KB)
1 | 44533 | jjdelcerro | package org.gvsig.geodb; |
---|---|---|---|
2 | |||
3 | 45425 | jjdelcerro | import java.awt.event.ActionEvent; |
4 | 44533 | jjdelcerro | import java.util.ArrayList; |
5 | 45160 | fdiaz | import java.util.Arrays; |
6 | 44533 | jjdelcerro | import java.util.List; |
7 | 45160 | fdiaz | import java.util.Map; |
8 | import javax.swing.SwingUtilities; |
||
9 | import org.apache.commons.codec.binary.Hex; |
||
10 | 44533 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
11 | 45160 | fdiaz | import org.gvsig.andami.PluginServices; |
12 | import org.gvsig.andami.PluginsLocator; |
||
13 | import org.gvsig.andami.PluginsManager; |
||
14 | 44533 | jjdelcerro | import org.gvsig.fmap.dal.DALLocator; |
15 | import org.gvsig.fmap.dal.DataManager; |
||
16 | 45160 | fdiaz | import org.gvsig.fmap.dal.DataStore; |
17 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
18 | import org.gvsig.fmap.dal.exception.DataException; |
||
19 | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
||
20 | 45440 | fdiaz | import org.gvsig.fmap.dal.feature.FeatureSet; |
21 | 44533 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureStore; |
22 | import org.gvsig.fmap.dal.store.db.DBStoreParameters; |
||
23 | 45160 | fdiaz | import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer; |
24 | import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
||
25 | 44533 | jjdelcerro | import org.gvsig.fmap.mapcontext.MapContext; |
26 | import org.gvsig.fmap.mapcontext.MapContextLocator; |
||
27 | import org.gvsig.fmap.mapcontext.MapContextManager; |
||
28 | import org.gvsig.fmap.mapcontext.layers.CancelationException; |
||
29 | import org.gvsig.fmap.mapcontext.layers.FLayer; |
||
30 | import org.gvsig.fmap.mapcontext.layers.FLayers; |
||
31 | 45160 | fdiaz | import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
32 | 44533 | jjdelcerro | import org.gvsig.fmap.mapcontrol.MapControl; |
33 | import static org.gvsig.geodb.AbstractWizardDB.LOGGER; |
||
34 | 45160 | fdiaz | import org.gvsig.geodb.vectorialdb.visibility.VisibilityScaleSelector; |
35 | import org.gvsig.geodb.vectorialdb.visibility.LayerScaleVisibility; |
||
36 | import org.gvsig.tools.ToolsLocator; |
||
37 | 45440 | fdiaz | import org.gvsig.tools.dispose.DisposeUtils; |
38 | 45160 | fdiaz | import org.gvsig.tools.dynobject.DynObject; |
39 | import org.gvsig.tools.i18n.I18nManager; |
||
40 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
41 | 45425 | jjdelcerro | import org.gvsig.tools.swing.api.windowmanager.Dialog; |
42 | 45160 | fdiaz | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
43 | 45425 | jjdelcerro | import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
44 | 44533 | jjdelcerro | |
45 | /**
|
||
46 | *
|
||
47 | 45425 | jjdelcerro | * @author gvSIG Team
|
48 | 44533 | jjdelcerro | */
|
49 | 45425 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
50 | 44533 | jjdelcerro | public class WizardDBVectorial extends AbstractWizardDB { |
51 | 45160 | fdiaz | |
52 | 45404 | omartinez | public static final int NUMBER_OF_FEATURES_TO_ASK_FOR_VISIBILITY_SCALE = 10000; |
53 | 45160 | fdiaz | public static final String LAYERS_SCALE_VISIBILITY_PARAMETER="layersScaleVisibility"; |
54 | 44533 | jjdelcerro | |
55 | public WizardDBVectorial() {
|
||
56 | super();
|
||
57 | } |
||
58 | |||
59 | @Override
|
||
60 | protected boolean requireGeometry() { |
||
61 | return true; |
||
62 | } |
||
63 | |||
64 | @Override
|
||
65 | public Object executeWizard() { |
||
66 | DataManager man = DALLocator.getDataManager(); |
||
67 | MapContextManager mapContextManager = MapContextLocator.getMapContextManager(); |
||
68 | |||
69 | MapControl mapControl = this.getMapCtrl();
|
||
70 | MapContext mapContext = mapControl.getMapContext(); |
||
71 | |||
72 | String groupName = null; |
||
73 | List<FLayer> all_layers = new ArrayList(); |
||
74 | |||
75 | try {
|
||
76 | FLayer layer = null;
|
||
77 | for (TableInfo tableInfo : this.getTablesInformation()) { |
||
78 | if (!tableInfo.isSelected()) {
|
||
79 | continue;
|
||
80 | } |
||
81 | 45634 | fdiaz | DBStoreParameters params = tableInfo.getParameters(); |
82 | fixCRS(params); |
||
83 | 44533 | jjdelcerro | if (groupName == null) { |
84 | 45634 | fdiaz | String dbname = params.getDBName();
|
85 | 44533 | jjdelcerro | if (StringUtils.isEmpty(dbname)) {
|
86 | dbname = "Group";
|
||
87 | } |
||
88 | 45634 | fdiaz | String host = params.getHost();
|
89 | 44533 | jjdelcerro | if (StringUtils.isEmpty(host)) {
|
90 | groupName = dbname; |
||
91 | } else {
|
||
92 | groupName = dbname + " (" + host + ")"; |
||
93 | } |
||
94 | } |
||
95 | FeatureStore store; |
||
96 | try {
|
||
97 | store = (FeatureStore) man.openStore( |
||
98 | 45634 | fdiaz | params.getDataStoreName(), |
99 | params); |
||
100 | 44533 | jjdelcerro | } catch (Exception e) { |
101 | LOGGER.warn("While opening DB store.", e);
|
||
102 | return null; |
||
103 | } |
||
104 | try {
|
||
105 | |||
106 | layer = mapContextManager.createLayer( |
||
107 | tableInfo.getDocumentName(), |
||
108 | store |
||
109 | ); |
||
110 | 45160 | fdiaz | checkForVisibilityScale(layer); |
111 | 44533 | jjdelcerro | all_layers.add(layer); |
112 | } catch (Exception e) { |
||
113 | LOGGER.warn("Can't create layer '" + tableInfo.getDocumentName() + "'.", e); |
||
114 | 45440 | fdiaz | } finally {
|
115 | DisposeUtils.disposeQuietly(store); |
||
116 | 44533 | jjdelcerro | } |
117 | } |
||
118 | |||
119 | if (all_layers.size() > 1) { |
||
120 | FLayers group = new FLayers();
|
||
121 | group.setMapContext(mapContext); |
||
122 | group.setParentLayer(mapContext.getLayers()); |
||
123 | group.setName(groupName); |
||
124 | |||
125 | for (FLayer aLayer : all_layers) {
|
||
126 | group.addLayer(aLayer); |
||
127 | } |
||
128 | if ( group.isOk() ) {
|
||
129 | group.setVisible(true);
|
||
130 | mapContext.beginAtomicEvent(); |
||
131 | try {
|
||
132 | mapControl.addLayer(group); |
||
133 | group.dispose(); |
||
134 | } catch (CancelationException e) {
|
||
135 | LOGGER.warn("Can't add group to mapcontext layers.", e);
|
||
136 | } finally {
|
||
137 | mapContext.endAtomicEvent(); |
||
138 | } |
||
139 | } else {
|
||
140 | fireLayerErrorEvent(layer, mapControl); |
||
141 | } |
||
142 | |||
143 | } else if (all_layers.size() == 1) { |
||
144 | layer = (FLayer) all_layers.get(0);
|
||
145 | if ( layer.isOk() ) {
|
||
146 | layer.setVisible(true);
|
||
147 | mapContext.beginAtomicEvent(); |
||
148 | try {
|
||
149 | mapControl.addLayer(layer); |
||
150 | } catch (CancelationException e) {
|
||
151 | LOGGER.warn("Can't add group to mapcontext layers.", e);
|
||
152 | } finally {
|
||
153 | mapContext.endAtomicEvent(); |
||
154 | } |
||
155 | } else {
|
||
156 | fireLayerErrorEvent(layer, mapControl); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | } finally {
|
||
161 | // Dispose all created layers. If they have been included into
|
||
162 | // a FLayers object, they will have been binded there.
|
||
163 | for (FLayer layer : all_layers) {
|
||
164 | layer.dispose(); |
||
165 | } |
||
166 | } |
||
167 | return null; |
||
168 | } |
||
169 | |||
170 | private void fireLayerErrorEvent(FLayer lyr, MapControl mapControl) { |
||
171 | mapControl.getMapContext().callNewErrorEvent(null);
|
||
172 | } |
||
173 | 45160 | fdiaz | |
174 | |||
175 | private void checkForVisibilityScale(FLayer layer) throws DataException, ValidateDataParametersException { |
||
176 | |||
177 | PluginsManager pluginManager = PluginsLocator.getPluginsManager(); |
||
178 | PluginServices plugin = pluginManager.getPlugin(ExtDB_Spatial.class); |
||
179 | DynObject pluginProperties = plugin.getPluginProperties(); |
||
180 | |||
181 | Map<String, LayerScaleVisibility> layersScaleVisibility = (Map) pluginProperties.getDynValue(LAYERS_SCALE_VISIBILITY_PARAMETER); |
||
182 | String layerKey = buildLayerKey(layer);
|
||
183 | if(layerKey != null) { |
||
184 | LayerScaleVisibility layerScaleVisibility = layersScaleVisibility.get(layerKey); |
||
185 | if(layerScaleVisibility!=null){ |
||
186 | int scaleAboveDenominator = layerScaleVisibility.getScaleAboveDenominator();
|
||
187 | int scaleBelowDenominator = layerScaleVisibility.getScaleBelowDenominator();
|
||
188 | 44533 | jjdelcerro | |
189 | 45160 | fdiaz | layer.setMaxScale(scaleAboveDenominator); |
190 | layer.setMinScale(scaleBelowDenominator); |
||
191 | return;
|
||
192 | } |
||
193 | } |
||
194 | |||
195 | if (layer instanceof FLyrVect) { |
||
196 | FLyrVect lyrVect = (FLyrVect)layer; |
||
197 | FeatureStore store = lyrVect.getFeatureStore(); |
||
198 | 45440 | fdiaz | |
199 | if(store.getExplorer() instanceof JDBCServerExplorer) { |
||
200 | FeatureSet fset = store.getFeatureSet(); |
||
201 | if(fset.size()>NUMBER_OF_FEATURES_TO_ASK_FOR_VISIBILITY_SCALE) {
|
||
202 | askUserForVisibilityScale(layer); |
||
203 | } |
||
204 | DisposeUtils.disposeQuietly(fset); |
||
205 | 45160 | fdiaz | } |
206 | } |
||
207 | } |
||
208 | |||
209 | private void askUserForVisibilityScale(FLayer layer) { |
||
210 | if (!SwingUtilities.isEventDispatchThread()) { |
||
211 | 45425 | jjdelcerro | try {
|
212 | SwingUtilities.invokeAndWait(() -> {askUserForVisibilityScale(layer);});
|
||
213 | } catch (Exception ex) { |
||
214 | LOGGER.warn("Con show selection wisivility scale dialog.", ex);
|
||
215 | } |
||
216 | 45160 | fdiaz | return;
|
217 | } |
||
218 | 45425 | jjdelcerro | I18nManager i18n = ToolsLocator.getI18nManager(); |
219 | WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
||
220 | 45160 | fdiaz | |
221 | VisibilityScaleSelector visibilityScaleSelector = new VisibilityScaleSelector();
|
||
222 | 45425 | jjdelcerro | Dialog dialog = winManager.createDialog(
|
223 | visibilityScaleSelector.asJComponent(), |
||
224 | i18n.getTranslation("select_visibility_scale"),
|
||
225 | null,
|
||
226 | WindowManager_v2.BUTTONS_OK_CANCEL |
||
227 | 45160 | fdiaz | ); |
228 | 45425 | jjdelcerro | dialog.addActionListener((ActionEvent e) -> {
|
229 | if (dialog.getAction()==WindowManager_v2.BUTTON_OK ) {
|
||
230 | layer.setMaxScale(visibilityScaleSelector.getScaleAboveDenominator()); |
||
231 | layer.setMinScale(visibilityScaleSelector.getScaleBelowDenominator()); |
||
232 | if(visibilityScaleSelector.getRememberAnswer()){
|
||
233 | PluginsManager pluginManager = PluginsLocator.getPluginsManager(); |
||
234 | PluginServices plugin = pluginManager.getPlugin(ExtDB_Spatial.class); |
||
235 | DynObject pluginProperties = plugin.getPluginProperties(); |
||
236 | String layerKey = buildLayerKey(layer);
|
||
237 | String layerLabel = buildLayerLabel(layer);
|
||
238 | if(layerKey != null && layerLabel != null){ |
||
239 | Map<String, LayerScaleVisibility> layersScaleVisibility = (Map) pluginProperties.getDynValue(LAYERS_SCALE_VISIBILITY_PARAMETER); |
||
240 | LayerScaleVisibility layerScaleVisibility = new LayerScaleVisibility();
|
||
241 | layerScaleVisibility.setScaleAboveDenominator(visibilityScaleSelector.getScaleAboveDenominator()); |
||
242 | layerScaleVisibility.setScaleBelowDenominator(visibilityScaleSelector.getScaleBelowDenominator()); |
||
243 | layerScaleVisibility.setLabel(layerLabel); |
||
244 | layersScaleVisibility.put(layerKey, layerScaleVisibility); |
||
245 | } |
||
246 | 45160 | fdiaz | } |
247 | } |
||
248 | 45425 | jjdelcerro | }); |
249 | dialog.show(WindowManager.MODE.DIALOG); |
||
250 | 45160 | fdiaz | } |
251 | |||
252 | private String buildLayerKey(FLayer layer) { |
||
253 | if(layer!=null){ |
||
254 | DataStore dataStore = ((FLyrVect)layer).getDataStore(); |
||
255 | if(dataStore != null){ |
||
256 | DataStoreParameters parameters = dataStore.getParameters(); |
||
257 | if(parameters != null){ |
||
258 | return Arrays.toString(Hex.encodeHex(parameters.toByteArray())); |
||
259 | } |
||
260 | } |
||
261 | } |
||
262 | return null; |
||
263 | } |
||
264 | |||
265 | private String buildLayerLabel(FLayer layer) { |
||
266 | |||
267 | if(layer!=null){ |
||
268 | DataStore dataStore = ((FLyrVect)layer).getDataStore(); |
||
269 | if(dataStore != null){ |
||
270 | StringBuilder builder = new StringBuilder(); |
||
271 | DataStoreParameters parameters = dataStore.getParameters(); |
||
272 | if(parameters != null && parameters instanceof DBStoreParameters){ |
||
273 | JDBCStoreParameters jdbcParameters = (JDBCStoreParameters)parameters; |
||
274 | builder.append(jdbcParameters.getDBName()); |
||
275 | builder.append(".");
|
||
276 | builder.append(jdbcParameters.getSchema()); |
||
277 | builder.append(".");
|
||
278 | builder.append(jdbcParameters.getTable()); |
||
279 | return builder.toString();
|
||
280 | } |
||
281 | } |
||
282 | } |
||
283 | return null; |
||
284 | } |
||
285 | 44533 | jjdelcerro | } |