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 / vectorialdb / visibility / LayerScaleVisibilityUtils.java @ 46211
History | View | Annotate | Download (8.64 KB)
1 |
package org.gvsig.geodb.vectorialdb.visibility; |
---|---|
2 |
|
3 |
import java.awt.event.ActionEvent; |
4 |
import java.util.HashMap; |
5 |
import java.util.Map; |
6 |
import javax.swing.SwingUtilities; |
7 |
import org.apache.commons.codec.binary.Hex; |
8 |
import org.gvsig.andami.PluginServices; |
9 |
import org.gvsig.andami.PluginsLocator; |
10 |
import org.gvsig.andami.PluginsManager; |
11 |
import org.gvsig.fmap.dal.DataStore; |
12 |
import org.gvsig.fmap.dal.DataStoreParameters; |
13 |
import org.gvsig.fmap.dal.exception.DataException; |
14 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
15 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
16 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
17 |
import org.gvsig.fmap.dal.store.db.DBStoreParameters; |
18 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer; |
19 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
20 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
21 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
22 |
import org.gvsig.geodb.ExtDB_Spatial; |
23 |
import org.gvsig.tools.ToolsLocator; |
24 |
import org.gvsig.tools.dispose.DisposeUtils; |
25 |
import org.gvsig.tools.dynobject.DynObject; |
26 |
import org.gvsig.tools.i18n.I18nManager; |
27 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
28 |
import org.gvsig.tools.swing.api.reminder.DialogReminderManager; |
29 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
30 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
31 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
32 |
import org.slf4j.Logger; |
33 |
import org.slf4j.LoggerFactory; |
34 |
|
35 |
/**
|
36 |
*
|
37 |
* @author gvSIG Team
|
38 |
*/
|
39 |
@SuppressWarnings("UseSpecificCatch") |
40 |
public class LayerScaleVisibilityUtils { |
41 |
|
42 |
private static final Logger LOGGER = LoggerFactory.getLogger(LayerScaleVisibilityUtils.class); |
43 |
|
44 |
public static final int NUMBER_OF_FEATURES_TO_ASK_FOR_VISIBILITY_SCALE = 10000; |
45 |
public static final String LAYERS_SCALE_VISIBILITY_PARAMETER="layersScaleVisibility"; |
46 |
|
47 |
public static void initPluginProperties() { |
48 |
PluginsManager pluginManager = PluginsLocator.getPluginsManager(); |
49 |
PluginServices plugin = pluginManager.getPlugin(ExtDB_Spatial.class); |
50 |
DynObject pluginProperties = plugin.getPluginProperties(); |
51 |
|
52 |
HashMap<Object, Object> layersScaleVisibility = new HashMap<>(); |
53 |
Map<String, LayerScaleVisibility> tmp = (Map) pluginProperties.getDynValue(LAYERS_SCALE_VISIBILITY_PARAMETER); |
54 |
if(tmp != null){ |
55 |
layersScaleVisibility.putAll(tmp); |
56 |
} |
57 |
pluginProperties.setDynValue(LAYERS_SCALE_VISIBILITY_PARAMETER, layersScaleVisibility); |
58 |
} |
59 |
|
60 |
public static void checkForVisibilityScale(FLayer layer) throws DataException, ValidateDataParametersException { |
61 |
checkForVisibilityScale(layer, true);
|
62 |
} |
63 |
|
64 |
public static void checkForVisibilityScale(FLayer layer, boolean checkScale) throws DataException, ValidateDataParametersException { |
65 |
|
66 |
if( layer.getMaxScale()>=0 && layer.getMinScale()>=0 ) { |
67 |
return;
|
68 |
} |
69 |
PluginsManager pluginManager = PluginsLocator.getPluginsManager(); |
70 |
PluginServices plugin = pluginManager.getPlugin(ExtDB_Spatial.class); |
71 |
DynObject pluginProperties = plugin.getPluginProperties(); |
72 |
|
73 |
Map<String, LayerScaleVisibility> layersScaleVisibility = (Map) pluginProperties.getDynValue(LAYERS_SCALE_VISIBILITY_PARAMETER); |
74 |
String layerKey = buildLayerKey(layer);
|
75 |
if (layerKey != null) { |
76 |
LayerScaleVisibility layerScaleVisibility = layersScaleVisibility.get(layerKey); |
77 |
if (layerScaleVisibility != null) { |
78 |
int scaleAboveDenominator = layerScaleVisibility.getScaleAboveDenominator();
|
79 |
int scaleBelowDenominator = layerScaleVisibility.getScaleBelowDenominator();
|
80 |
|
81 |
layer.setMaxScale(scaleAboveDenominator); |
82 |
layer.setMinScale(scaleBelowDenominator); |
83 |
return;
|
84 |
} |
85 |
} |
86 |
if( !checkScale ) {
|
87 |
return;
|
88 |
} |
89 |
DialogReminderManager dialogReminder = ToolsSwingLocator.getDialogReminderManager(); |
90 |
if( dialogReminder.exists("_Do_not_ask_the_scale_again") ) { |
91 |
return;
|
92 |
} |
93 |
if (layer instanceof FLyrVect) { |
94 |
FLyrVect lyrVect = (FLyrVect) layer; |
95 |
FeatureStore store = lyrVect.getFeatureStore(); |
96 |
|
97 |
if (store.getExplorer() instanceof JDBCServerExplorer) { |
98 |
FeatureSet fset = store.getFeatureSet(); |
99 |
if (fset.size() > NUMBER_OF_FEATURES_TO_ASK_FOR_VISIBILITY_SCALE) {
|
100 |
askUserForVisibilityScale(layer); |
101 |
} |
102 |
DisposeUtils.disposeQuietly(fset); |
103 |
} |
104 |
} |
105 |
} |
106 |
|
107 |
private static void askUserForVisibilityScale(FLayer layer) { |
108 |
if (!SwingUtilities.isEventDispatchThread()) { |
109 |
try {
|
110 |
SwingUtilities.invokeAndWait(() -> {
|
111 |
askUserForVisibilityScale(layer); |
112 |
}); |
113 |
} catch (Exception ex) { |
114 |
LOGGER.warn("Con show selection wisivility scale dialog.", ex);
|
115 |
} |
116 |
return;
|
117 |
} |
118 |
DialogReminderManager dialogReminder = ToolsSwingLocator.getDialogReminderManager(); |
119 |
if( dialogReminder.exists("_Do_not_ask_the_scale_again") ) { |
120 |
return;
|
121 |
} |
122 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
123 |
WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
124 |
|
125 |
VisibilityScaleSelector visibilityScaleSelector = new VisibilityScaleSelector();
|
126 |
Dialog dialog = winManager.createDialog(
|
127 |
visibilityScaleSelector.asJComponent(), |
128 |
i18n.getTranslation("select_visibility_scale") + " - " + layer.getName(), |
129 |
null,
|
130 |
WindowManager_v2.BUTTONS_OK_CANCEL |
131 |
); |
132 |
dialog.addActionListener((ActionEvent e) -> {
|
133 |
if (dialog.getAction() == WindowManager_v2.BUTTON_OK) {
|
134 |
layer.setMaxScale(visibilityScaleSelector.getScaleAboveDenominator()); |
135 |
layer.setMinScale(visibilityScaleSelector.getScaleBelowDenominator()); |
136 |
if (visibilityScaleSelector.getRememberAnswer()) {
|
137 |
PluginsManager pluginManager = PluginsLocator.getPluginsManager(); |
138 |
PluginServices plugin = pluginManager.getPlugin(ExtDB_Spatial.class); |
139 |
DynObject pluginProperties = plugin.getPluginProperties(); |
140 |
String layerKey = buildLayerKey(layer);
|
141 |
String layerLabel = buildLayerLabel(layer);
|
142 |
if (layerKey != null && layerLabel != null) { |
143 |
Map<String, LayerScaleVisibility> layersScaleVisibility = (Map) pluginProperties.getDynValue(LAYERS_SCALE_VISIBILITY_PARAMETER); |
144 |
LayerScaleVisibility layerScaleVisibility = new LayerScaleVisibility();
|
145 |
layerScaleVisibility.setScaleAboveDenominator(visibilityScaleSelector.getScaleAboveDenominator()); |
146 |
layerScaleVisibility.setScaleBelowDenominator(visibilityScaleSelector.getScaleBelowDenominator()); |
147 |
layerScaleVisibility.setLabel(layerLabel); |
148 |
layersScaleVisibility.put(layerKey, layerScaleVisibility); |
149 |
} |
150 |
} |
151 |
} |
152 |
if( visibilityScaleSelector.getDoNotAskScaleAgain() ) {
|
153 |
dialogReminder.add("_Do_not_ask_the_scale_again", 1); |
154 |
} |
155 |
}); |
156 |
dialog.show(WindowManager.MODE.DIALOG); |
157 |
} |
158 |
|
159 |
private static String buildLayerKey(FLayer layer) { |
160 |
if(layer!=null){ |
161 |
DataStore dataStore = ((FLyrVect)layer).getDataStore(); |
162 |
if(dataStore != null){ |
163 |
DataStoreParameters parameters = dataStore.getParameters(); |
164 |
if(parameters != null){ |
165 |
return Hex.encodeHexString(parameters.toByteArray());
|
166 |
} |
167 |
} |
168 |
} |
169 |
return null; |
170 |
} |
171 |
|
172 |
private static String buildLayerLabel(FLayer layer) { |
173 |
|
174 |
if(layer!=null){ |
175 |
DataStore dataStore = ((FLyrVect)layer).getDataStore(); |
176 |
if(dataStore != null){ |
177 |
StringBuilder builder = new StringBuilder(); |
178 |
DataStoreParameters parameters = dataStore.getParameters(); |
179 |
if(parameters != null && parameters instanceof DBStoreParameters){ |
180 |
JDBCStoreParameters jdbcParameters = (JDBCStoreParameters)parameters; |
181 |
builder.append(jdbcParameters.getDBName()); |
182 |
builder.append(".");
|
183 |
builder.append(jdbcParameters.getSchema()); |
184 |
builder.append(".");
|
185 |
builder.append(jdbcParameters.getTable()); |
186 |
return builder.toString();
|
187 |
} |
188 |
} |
189 |
} |
190 |
return null; |
191 |
} |
192 |
} |