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 @ 45839
History | View | Annotate | Download (11.8 KB)
1 |
package org.gvsig.geodb; |
---|---|
2 |
|
3 |
import java.awt.event.ActionEvent; |
4 |
import java.util.ArrayList; |
5 |
import java.util.Arrays; |
6 |
import java.util.List; |
7 |
import java.util.Map; |
8 |
import javax.swing.SwingUtilities; |
9 |
import org.apache.commons.codec.binary.Hex; |
10 |
import org.apache.commons.lang3.StringUtils; |
11 |
import org.gvsig.andami.PluginServices; |
12 |
import org.gvsig.andami.PluginsLocator; |
13 |
import org.gvsig.andami.PluginsManager; |
14 |
import org.gvsig.fmap.dal.DALLocator; |
15 |
import org.gvsig.fmap.dal.DataManager; |
16 |
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 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
21 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
22 |
import org.gvsig.fmap.dal.store.db.DBStoreParameters; |
23 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer; |
24 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
25 |
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 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
32 |
import org.gvsig.fmap.mapcontrol.MapControl; |
33 |
import static org.gvsig.geodb.AbstractWizardDB.LOGGER; |
34 |
import org.gvsig.geodb.vectorialdb.visibility.VisibilityScaleSelector; |
35 |
import org.gvsig.geodb.vectorialdb.visibility.LayerScaleVisibility; |
36 |
import org.gvsig.tools.ToolsLocator; |
37 |
import org.gvsig.tools.dispose.DisposeUtils; |
38 |
import org.gvsig.tools.dynobject.DynObject; |
39 |
import org.gvsig.tools.i18n.I18nManager; |
40 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
41 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
42 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
43 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
44 |
|
45 |
/**
|
46 |
*
|
47 |
* @author gvSIG Team
|
48 |
*/
|
49 |
@SuppressWarnings("UseSpecificCatch") |
50 |
public class WizardDBVectorial extends AbstractWizardDB { |
51 |
|
52 |
public static final int NUMBER_OF_FEATURES_TO_ASK_FOR_VISIBILITY_SCALE = 10000; |
53 |
public static final String LAYERS_SCALE_VISIBILITY_PARAMETER="layersScaleVisibility"; |
54 |
|
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 |
DBStoreParameters params = tableInfo.getParameters(); |
82 |
fixCRS(params); |
83 |
if (groupName == null) { |
84 |
String dbname = params.getDBName();
|
85 |
if (StringUtils.isEmpty(dbname)) {
|
86 |
dbname = "Group";
|
87 |
} |
88 |
String host = params.getHost();
|
89 |
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 |
params.getDataStoreName(), |
99 |
params); |
100 |
} 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 |
checkForVisibilityScale(layer); |
111 |
all_layers.add(layer); |
112 |
} catch (Exception e) { |
113 |
LOGGER.warn("Can't create layer '" + tableInfo.getDocumentName() + "'.", e); |
114 |
} finally {
|
115 |
DisposeUtils.disposeQuietly(store); |
116 |
} |
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 |
|
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 |
|
189 |
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 |
|
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 |
} |
206 |
} |
207 |
} |
208 |
|
209 |
private void askUserForVisibilityScale(FLayer layer) { |
210 |
if (!SwingUtilities.isEventDispatchThread()) { |
211 |
try {
|
212 |
SwingUtilities.invokeAndWait(() -> {askUserForVisibilityScale(layer);});
|
213 |
} catch (Exception ex) { |
214 |
LOGGER.warn("Con show selection wisivility scale dialog.", ex);
|
215 |
} |
216 |
return;
|
217 |
} |
218 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
219 |
WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
220 |
|
221 |
VisibilityScaleSelector visibilityScaleSelector = new VisibilityScaleSelector();
|
222 |
Dialog dialog = winManager.createDialog(
|
223 |
visibilityScaleSelector.asJComponent(), |
224 |
i18n.getTranslation("select_visibility_scale") + " - " + layer.getName(), |
225 |
null,
|
226 |
WindowManager_v2.BUTTONS_OK_CANCEL |
227 |
); |
228 |
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 |
} |
247 |
} |
248 |
}); |
249 |
dialog.show(WindowManager.MODE.DIALOG); |
250 |
} |
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 |
} |