root / org.gvsig.legend.urbanhorizontalsignage / trunk / org.gvsig.legend.urbanhorizontalsignage / org.gvsig.legend.urbanhorizontalsignage.app / org.gvsig.legend.urbanhorizontalsignage.app.mainplugin / src / main / java / org / gvsig / legend / urbanhorizontalsignage / app / UrbanHorizontalSignageExtension.java @ 10194
History | View | Annotate | Download (15.9 KB)
1 |
package org.gvsig.legend.urbanhorizontalsignage.app; |
---|---|
2 |
|
3 |
import java.awt.event.ActionEvent; |
4 |
import java.util.function.Predicate; |
5 |
import javax.swing.Icon; |
6 |
import javax.swing.JOptionPane; |
7 |
import javax.swing.SwingConstants; |
8 |
import javax.swing.event.TreeModelListener; |
9 |
import javax.swing.tree.TreePath; |
10 |
import org.gvsig.andami.IconThemeHelper; |
11 |
import org.gvsig.andami.plugins.Extension; |
12 |
import org.gvsig.app.ApplicationLocator; |
13 |
import org.gvsig.app.ApplicationManager; |
14 |
import org.gvsig.app.project.documents.view.ViewDocument; |
15 |
import org.gvsig.app.project.documents.view.ViewManager; |
16 |
import org.gvsig.fmap.dal.DALLocator; |
17 |
import org.gvsig.fmap.dal.DataManager; |
18 |
import org.gvsig.fmap.dal.DataStore; |
19 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
20 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
21 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
22 |
import org.gvsig.fmap.dal.feature.FeatureType; |
23 |
import org.gvsig.fmap.mapcontext.MapContext; |
24 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
25 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
26 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
27 |
import org.gvsig.fmap.mapcontrol.CompoundStoresTreeModel; |
28 |
import org.gvsig.legend.urbanhorizontalsignage.lib.api.linelegend.UrbanHorizontalSignageLegend; |
29 |
import org.gvsig.legend.urbanhorizontalsignage.lib.api.UrbanHorizontalSignageLocator; |
30 |
import org.gvsig.legend.urbanhorizontalsignage.lib.api.UrbanHorizontalSignageManager; |
31 |
import org.gvsig.legend.urbanhorizontalsignage.lib.api.linelegend.UrbanHorizontalSignageConfig; |
32 |
import org.gvsig.legend.urbanhorizontalsignage.swing.api.ConvertLinesToPolygonsPanel; |
33 |
import org.gvsig.legend.urbanhorizontalsignage.swing.api.UrbanHorizontalSignageSwingLocator; |
34 |
import org.gvsig.legend.urbanhorizontalsignage.swing.api.UrbanHorizontalSignageSwingManager; |
35 |
import org.gvsig.tools.ToolsLocator; |
36 |
import org.gvsig.tools.dispose.DisposeUtils; |
37 |
import org.gvsig.tools.i18n.I18nManager; |
38 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
39 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
40 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
41 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
42 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
43 |
import org.gvsig.tools.util.LabeledValue; |
44 |
import org.slf4j.Logger; |
45 |
import org.slf4j.LoggerFactory; |
46 |
|
47 |
@SuppressWarnings("UseSpecificCatch") |
48 |
public class UrbanHorizontalSignageExtension extends Extension { |
49 |
|
50 |
private static final Logger LOGGER = LoggerFactory.getLogger(UrbanHorizontalSignageExtension.class); |
51 |
|
52 |
@Override
|
53 |
public void initialize() { |
54 |
IconThemeHelper.registerIcon("legend", "legend-overview-vectorial-urbanhorizontalsignage", this); |
55 |
IconThemeHelper.registerIcon("legend", "legend-overview-vectorial-picturemarkersymboltable", this); |
56 |
IconThemeHelper.registerIcon("action", "layer-modify-clipboard-copy-as-polygons", this); |
57 |
|
58 |
} |
59 |
|
60 |
@Override
|
61 |
public void execute(String actionCommand) { |
62 |
ApplicationManager application = ApplicationLocator.getManager(); |
63 |
|
64 |
if ("layer-modify-clipboard-copy-as-polygons".equalsIgnoreCase(actionCommand) ) { |
65 |
ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
66 |
if (viewdoc == null) { |
67 |
return;
|
68 |
} |
69 |
MapContext mapContext = viewdoc.getMapContext(); |
70 |
FLyrVect layer = mapContext.getLayers().getFirstActiveVectorLayer(); |
71 |
if (layer == null) { |
72 |
return;
|
73 |
} |
74 |
copySelecctionAsPolygons(layer); |
75 |
|
76 |
} else if ("tools-urbanSignage-linesToPolygons".equalsIgnoreCase(actionCommand) ) { |
77 |
ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
78 |
if (viewdoc == null) { |
79 |
return;
|
80 |
} |
81 |
MapContext mapContext = viewdoc.getMapContext(); |
82 |
FLyrVect layer = mapContext.getLayers().getFirstActiveVectorLayer(); |
83 |
if (layer == null) { |
84 |
return;
|
85 |
} |
86 |
if( !(layer.getLegend() instanceof UrbanHorizontalSignageLegend) ) { |
87 |
//FIXME mensaje al usuario?
|
88 |
return;
|
89 |
} |
90 |
UrbanHorizontalSignageLegend legend = (UrbanHorizontalSignageLegend) layer.getLegend(); |
91 |
UrbanHorizontalSignageConfig config = legend.getConfig(); // FIXME: clone
|
92 |
FeatureSet lines; |
93 |
try {
|
94 |
lines = layer.getFeatureStore().getFeatureSelection(); |
95 |
} catch(Exception ex) { |
96 |
//FIXME mensaje al usuario?
|
97 |
return;
|
98 |
} |
99 |
convertLinesAsPolygons(config, lines); |
100 |
} |
101 |
|
102 |
} |
103 |
|
104 |
private void copySelecctionAsPolygons(FLyrVect sourceLayer) { |
105 |
// UrbanHorizontalSignageManager uhsManager = UrbanHorizontalSignageLocator.getUrbanHorizontalSignageManager();
|
106 |
|
107 |
UrbanHorizontalSignageConfigForCopyPanel panel = new UrbanHorizontalSignageConfigForCopyPanel();
|
108 |
panel.setFeatureType(sourceLayer.getFeatureStore().getDefaultFeatureTypeQuietly()); |
109 |
UrbanHorizontalSignageLegend legend = (UrbanHorizontalSignageLegend) sourceLayer.getLegend(); |
110 |
panel.put(legend.getConfig()); |
111 |
|
112 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
113 |
WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
114 |
|
115 |
Dialog theDialog = winManager.createDialog(
|
116 |
panel.asJComponent(), |
117 |
i18n.getTranslation("_Copy_as_polygons"),
|
118 |
i18n.getTranslation("_Copy_as_polygons"),
|
119 |
WindowManager_v2.BUTTONS_OK_CANCEL |
120 |
); |
121 |
theDialog.setAutoclose(false);
|
122 |
|
123 |
theDialog.setButtonLabel( |
124 |
WindowManager_v2.BUTTON_OK, |
125 |
i18n.getTranslation("_Accept")
|
126 |
); |
127 |
theDialog.setButtonLabel( |
128 |
WindowManager_v2.BUTTON_CANCEL, |
129 |
i18n.getTranslation("_Cancel")
|
130 |
); |
131 |
|
132 |
theDialog.addActionListener((ActionEvent e) -> {
|
133 |
switch (theDialog.getAction()) {
|
134 |
case WindowManager_v2.BUTTON_OK:
|
135 |
Thread task = new Thread(() -> { |
136 |
panel.fetch(legend.getConfig()); |
137 |
performCopyAsPolygons(sourceLayer); |
138 |
theDialog.asJComponent().setVisible(false);
|
139 |
}, "UHS_CopyAsPolygons");
|
140 |
task.start(); |
141 |
break;
|
142 |
case WindowManager_v2.BUTTON_CANCEL:
|
143 |
theDialog.asJComponent().setVisible(false);
|
144 |
break;
|
145 |
} |
146 |
}); |
147 |
|
148 |
theDialog.show(WindowManager.MODE.DIALOG); |
149 |
} |
150 |
|
151 |
private void performCopyAsPolygons(FLyrVect sourceLayer) { |
152 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
153 |
UrbanHorizontalSignageManager uhsManager = UrbanHorizontalSignageLocator.getUrbanHorizontalSignageManager(); |
154 |
ApplicationManager application = ApplicationLocator.getManager(); |
155 |
DataManager dataManager = DALLocator.getDataManager(); |
156 |
|
157 |
UrbanHorizontalSignageLegend legend = (UrbanHorizontalSignageLegend) sourceLayer.getLegend(); |
158 |
FeatureStore tempTargetStore = null;
|
159 |
FeatureSet tempFeatureSet = null;
|
160 |
try {
|
161 |
|
162 |
FeatureSet sourceFeatureSet = sourceLayer.getFeatureStore().getFeatureSelection(); |
163 |
tempTargetStore = uhsManager.createTemporaryPolygonsFromLines(legend.getConfig(), sourceFeatureSet, null);
|
164 |
tempFeatureSet = tempTargetStore.getFeatureSet(); |
165 |
|
166 |
int n = application.confirmDialog(
|
167 |
"_Number_of_geometries_to_be_copied_to_the_clipboard" + " " + tempFeatureSet.getSize() + "\n" + "_Do_you_want_to_continue", |
168 |
i18n.getTranslation("_Copy_selected_features_as_polygons_to_clipboard"),
|
169 |
JOptionPane.YES_NO_OPTION,
|
170 |
JOptionPane.QUESTION_MESSAGE);
|
171 |
if (n == JOptionPane.YES_OPTION) { |
172 |
dataManager.putFeaturesInClipboard(tempFeatureSet); |
173 |
} |
174 |
|
175 |
} catch (Exception e) { |
176 |
LOGGER.warn("Can't get the selection as JSON.", e);
|
177 |
} finally {
|
178 |
DisposeUtils.disposeQuietly(tempTargetStore); |
179 |
DisposeUtils.disposeQuietly(tempFeatureSet); |
180 |
} |
181 |
|
182 |
} |
183 |
|
184 |
private void convertLinesAsPolygons(UrbanHorizontalSignageConfig config, FeatureSet source) { |
185 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
186 |
WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
187 |
ApplicationManager application = ApplicationLocator.getManager(); |
188 |
UrbanHorizontalSignageManager uhsManager = UrbanHorizontalSignageLocator.getUrbanHorizontalSignageManager(); |
189 |
UrbanHorizontalSignageSwingManager uhsSwingManager = UrbanHorizontalSignageSwingLocator.getUrbanHorizontalSignageSwingManager(); |
190 |
|
191 |
|
192 |
final ConvertLinesToPolygonsPanel panel = uhsSwingManager.createConvertLinesToPolygonsPanel();
|
193 |
panel.setSource(source); |
194 |
// panel.setStores(new StoresWithLayerTypeIcon());
|
195 |
panel.put(config); |
196 |
|
197 |
Dialog theDialog = winManager.createDialog(
|
198 |
panel.asJComponent(), |
199 |
i18n.getTranslation("_Convert_lines_to_polygons"),
|
200 |
i18n.getTranslation("_Convert_Urban_signage_lines_to_polygons"),
|
201 |
WindowManager_v2.BUTTONS_OK_CANCEL |
202 |
); |
203 |
theDialog.setButtonLabel(WindowManager_v2.BUTTON_OK, i18n.getTranslation("_Convert"));
|
204 |
theDialog.setButtonLabel(WindowManager_v2.BUTTON_CANCEL, i18n.getTranslation("_Close"));
|
205 |
theDialog.setAutoclose(false);
|
206 |
|
207 |
theDialog.addActionListener((ActionEvent e) -> {
|
208 |
switch (theDialog.getAction()) {
|
209 |
case WindowManager_v2.BUTTON_OK:
|
210 |
Thread task = new Thread(() -> { |
211 |
try {
|
212 |
theDialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false);
|
213 |
panel.setTaskStatusVisible(true);
|
214 |
uhsManager.convertLinesToPolygons( |
215 |
panel.getConfig(), |
216 |
panel.getSource(), |
217 |
panel.getTargetStore(), |
218 |
panel.getDeleteSourceAtFinish(), |
219 |
panel.getTaskStatus() |
220 |
); |
221 |
theDialog.asJComponent().setVisible(false);
|
222 |
} catch(Exception ex) { |
223 |
LOGGER.warn("Can't convert lines to polygons", ex);
|
224 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
225 |
dialogs.messageDialog( |
226 |
i18n.getTranslation("_Cant_convert_lines_to_polygons") +"\n\n" + ex.getLocalizedMessage(), |
227 |
i18n.getTranslation("_Convert_Urban_signage_lines_to_polygons"),
|
228 |
JOptionPane.WARNING_MESSAGE
|
229 |
); |
230 |
} |
231 |
}, "UHS_ConvertLinesToPolygons");
|
232 |
task.start(); |
233 |
break;
|
234 |
case WindowManager_v2.BUTTON_CANCEL:
|
235 |
theDialog.asJComponent().setVisible(false);
|
236 |
break;
|
237 |
} |
238 |
}); |
239 |
|
240 |
theDialog.show(WindowManager.MODE.WINDOW); |
241 |
} |
242 |
|
243 |
@Override
|
244 |
public boolean isEnabled() { |
245 |
// it's enabled if there is exactly one vector layer in the active view
|
246 |
// and it has a selection
|
247 |
|
248 |
ApplicationManager application = ApplicationLocator.getManager(); |
249 |
ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
250 |
if (viewdoc == null) { |
251 |
return false; |
252 |
} |
253 |
MapContext mapContext = viewdoc.getMapContext(); |
254 |
FLyrVect layer = mapContext.getLayers().getFirstActiveVectorLayer(); |
255 |
if (layer == null) { |
256 |
return false; |
257 |
} |
258 |
if (!layer.isAvailable()) {
|
259 |
// This can happen when opening a persisted project
|
260 |
// and there is a "slow" layer (GeoDB)
|
261 |
return false; |
262 |
} |
263 |
try {
|
264 |
FeatureStore store = layer.getFeatureStore(); |
265 |
if( store == null ) { |
266 |
return false; |
267 |
} |
268 |
FeatureSelection selection = store.getFeatureSelection(); |
269 |
if (selection.isEmpty()) {
|
270 |
return false; |
271 |
} |
272 |
return (layer.getLegend() instanceof UrbanHorizontalSignageLegend); |
273 |
// return !selection.isEmpty();
|
274 |
} catch (Exception ex) { |
275 |
LOGGER.warn("Can't get selection from layer '" + layer.getName() + "'.", ex); |
276 |
return false; |
277 |
} |
278 |
} |
279 |
|
280 |
@Override
|
281 |
public boolean isVisible() { |
282 |
ApplicationManager application = ApplicationLocator.getManager(); |
283 |
return application.getActiveDocument(ViewManager.TYPENAME) != null; |
284 |
} |
285 |
|
286 |
// private static class StoresWithLayerTypeIcon implements CompoundStoresTreeModel {
|
287 |
//
|
288 |
// private final CompoundStoresTreeModel delegated;
|
289 |
//
|
290 |
// public StoresWithLayerTypeIcon() {
|
291 |
// ApplicationManager application = ApplicationLocator.getManager();
|
292 |
// this.delegated = (CompoundStoresTreeModel) application.createProjectStoresTreeModel();
|
293 |
// }
|
294 |
//
|
295 |
// @Override
|
296 |
// public void setFilter(Predicate<DataStore> filter) {
|
297 |
// this.delegated.setFilter(filter);
|
298 |
// }
|
299 |
//
|
300 |
// @Override
|
301 |
// public void addLayers(FLayers layers) {
|
302 |
// this.delegated.addLayers(layers);
|
303 |
// }
|
304 |
//
|
305 |
// @Override
|
306 |
// public Icon getIcon(Object item) {
|
307 |
// Icon icon = this.delegated.getIcon(item);
|
308 |
// if( item instanceof LabeledValue ) {
|
309 |
// item = ((LabeledValue)item).getValue();
|
310 |
// }
|
311 |
// if (item instanceof FeatureStore) {
|
312 |
// try {
|
313 |
// FeatureStore store = (FeatureStore) item;
|
314 |
// FeatureType ft = store.getDefaultFeatureTypeQuietly();
|
315 |
// int type = ft.getDefaultGeometryAttribute().getGeomType().getType();
|
316 |
// Icon icon2 = MapContextLocator.getMapContextManager().getIcon(
|
317 |
// type,
|
318 |
// !store.isFeatureSelectionEmpty()
|
319 |
// );
|
320 |
// if( icon2!=null ) {
|
321 |
// icon = ToolsSwingLocator.getToolsSwingManager().createCompoundIcon(
|
322 |
// SwingConstants.HORIZONTAL, 2, icon, icon2
|
323 |
// );
|
324 |
// }
|
325 |
// } catch(Exception ex) {
|
326 |
// LOGGER.trace("Can't get geometry type from store.", ex);
|
327 |
// }
|
328 |
// }
|
329 |
// return icon;
|
330 |
// }
|
331 |
//
|
332 |
// @Override
|
333 |
// public Object getRoot() {
|
334 |
// return this.delegated.getRoot();
|
335 |
// }
|
336 |
//
|
337 |
// @Override
|
338 |
// public Object getChild(Object parent, int index) {
|
339 |
// return this.delegated.getChild(parent, index);
|
340 |
// }
|
341 |
//
|
342 |
// @Override
|
343 |
// public int getChildCount(Object parent) {
|
344 |
// return this.delegated.getChildCount(parent);
|
345 |
// }
|
346 |
//
|
347 |
// @Override
|
348 |
// public boolean isLeaf(Object node) {
|
349 |
// return this.delegated.isLeaf(node);
|
350 |
// }
|
351 |
//
|
352 |
// @Override
|
353 |
// public void valueForPathChanged(TreePath path, Object newValue) {
|
354 |
// this.delegated.valueForPathChanged(path, newValue);
|
355 |
// }
|
356 |
//
|
357 |
// @Override
|
358 |
// public int getIndexOfChild(Object parent, Object child) {
|
359 |
// return this.delegated.getIndexOfChild(parent, child);
|
360 |
// }
|
361 |
//
|
362 |
// @Override
|
363 |
// public void addTreeModelListener(TreeModelListener l) {
|
364 |
// this.delegated.addTreeModelListener(l);
|
365 |
// }
|
366 |
//
|
367 |
// @Override
|
368 |
// public void removeTreeModelListener(TreeModelListener l) {
|
369 |
// this.delegated.removeTreeModelListener(l);
|
370 |
// }
|
371 |
//
|
372 |
// }
|
373 |
|
374 |
} |