svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / geometriespalette / GeometriesPaletteExtension.java @ 47430
History | View | Annotate | Download (6.52 KB)
1 | 45739 | jjdelcerro | package org.gvsig.app.extension.geometriespalette; |
---|---|---|---|
2 | |||
3 | 47146 | jjdelcerro | import java.awt.Dimension; |
4 | 46735 | jjdelcerro | import javax.swing.SwingUtilities; |
5 | 45739 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
6 | 47146 | jjdelcerro | import org.gvsig.andami.PluginServices; |
7 | 45739 | jjdelcerro | import org.gvsig.andami.plugins.Extension; |
8 | 47430 | jjdelcerro | import org.gvsig.app.ApplicationLocator; |
9 | import org.gvsig.app.ApplicationManager; |
||
10 | 45739 | jjdelcerro | import org.gvsig.app.addlayer.AddLayerDialog; |
11 | import static org.gvsig.app.extension.AddLayer.createAddLayerDialog; |
||
12 | import org.gvsig.app.gui.WizardPanel; |
||
13 | 47430 | jjdelcerro | import org.gvsig.app.project.documents.Document; |
14 | import org.gvsig.app.project.documents.view.ViewDocument; |
||
15 | import org.gvsig.app.project.documents.view.ViewManager; |
||
16 | 45739 | jjdelcerro | import org.gvsig.fmap.crs.CRSFactory; |
17 | 46735 | jjdelcerro | import org.gvsig.fmap.dal.DALLocator; |
18 | import org.gvsig.fmap.dal.DataManager; |
||
19 | import org.gvsig.fmap.dal.DataStore; |
||
20 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
21 | 45739 | jjdelcerro | import org.gvsig.fmap.mapcontext.MapContext; |
22 | import org.gvsig.fmap.mapcontext.MapContextLocator; |
||
23 | import org.gvsig.fmap.mapcontext.MapContextManager; |
||
24 | import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
||
25 | import org.gvsig.fmap.mapcontrol.MapControl; |
||
26 | import org.gvsig.fmap.mapcontrol.MapControlLocator; |
||
27 | import org.gvsig.fmap.mapcontrol.MapControlManager; |
||
28 | 46735 | jjdelcerro | import org.gvsig.tools.arguments.Arguments; |
29 | 45739 | jjdelcerro | import org.gvsig.tools.dispose.DisposeUtils; |
30 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
31 | 47146 | jjdelcerro | import org.gvsig.tools.swing.api.ToolsSwingUtils; |
32 | 45739 | jjdelcerro | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
33 | |||
34 | /**
|
||
35 | *
|
||
36 | * @author gvSIG Team
|
||
37 | */
|
||
38 | @SuppressWarnings("UseSpecificCatch") |
||
39 | public class GeometriesPaletteExtension extends Extension { |
||
40 | |||
41 | @Override
|
||
42 | public void initialize() { |
||
43 | } |
||
44 | |||
45 | @Override
|
||
46 | public void execute(String actionCommand) { |
||
47 | if (StringUtils.equalsIgnoreCase("tools-open-geometriespalette", actionCommand)) { |
||
48 | doOpenGeometriesPalette(); |
||
49 | } |
||
50 | } |
||
51 | |||
52 | 46735 | jjdelcerro | @Override
|
53 | public void execute(String command, Object[] args) { |
||
54 | Arguments arguments = Arguments.create(args); |
||
55 | |||
56 | if (StringUtils.equalsIgnoreCase("tools-open-geometriespalette", command)) { |
||
57 | String labelattr = (String) arguments.get("label", String.class, null); |
||
58 | String tableName = (String) arguments.get("tableName", String.class, null); |
||
59 | String title = (String) arguments.get("title", String.class, null); |
||
60 | 46770 | jjdelcerro | String filter = (String) arguments.get("filter", String.class, null); |
61 | 46735 | jjdelcerro | if( StringUtils.isBlank(tableName) ) {
|
62 | doOpenGeometriesPalette(); |
||
63 | return;
|
||
64 | } |
||
65 | 46770 | jjdelcerro | doOpenGeometriesPalette(tableName, labelattr, title, filter); |
66 | 46735 | jjdelcerro | } |
67 | } |
||
68 | |||
69 | 45739 | jjdelcerro | @SuppressWarnings("SleepWhileInLoop") |
70 | private void doOpenGeometriesPalette() { |
||
71 | AddLayerDialog addLayerDialog = null;
|
||
72 | |||
73 | 46735 | jjdelcerro | final WindowManager windowManager = ToolsSwingLocator.getWindowManager();
|
74 | 45739 | jjdelcerro | MapContextManager mapContextManager = MapContextLocator.getMapContextManager(); |
75 | MapControlManager mapControlManager = MapControlLocator.getMapControlManager(); |
||
76 | |||
77 | 46735 | jjdelcerro | final MapContext mapContext = mapContextManager.createMapContext();
|
78 | MapControl mapControl = null;
|
||
79 | 45739 | jjdelcerro | try {
|
80 | 47430 | jjdelcerro | ApplicationManager application = ApplicationLocator.getApplicationManager(); |
81 | ViewDocument view = (ViewDocument) application.getCurrentProject().getFirstDocument(ViewDocument.class); |
||
82 | if( view == null || view.getProjection()==null ) { |
||
83 | mapContext.setProjection(CRSFactory.getCRS("EPSG:4326"));
|
||
84 | } else {
|
||
85 | mapContext.setProjection(view.getProjection()); |
||
86 | } |
||
87 | 45739 | jjdelcerro | mapControl = mapControlManager.createJMapControlPanel(mapContext); |
88 | 46735 | jjdelcerro | |
89 | 45739 | jjdelcerro | addLayerDialog = createAddLayerDialog(mapControl, mapContext, null);
|
90 | 47146 | jjdelcerro | Dimension screensize = ToolsSwingUtils.getCurrentScreenSize();
|
91 | ToolsSwingUtils.ensureHeightWitdh( |
||
92 | addLayerDialog, |
||
93 | 3*(screensize.height/4), |
||
94 | 2*(screensize.width/4), |
||
95 | 7*(screensize.height/8), |
||
96 | 3*(screensize.width/4) |
||
97 | ); |
||
98 | 45739 | jjdelcerro | windowManager.showWindow( |
99 | addLayerDialog, |
||
100 | "Select palette layer",
|
||
101 | WindowManager.MODE.DIALOG |
||
102 | ); |
||
103 | |||
104 | if (addLayerDialog.isAccepted()) {
|
||
105 | if (addLayerDialog.getSelectedTab() instanceof WizardPanel) { |
||
106 | WizardPanel panel = (WizardPanel) addLayerDialog.getSelectedTab(); |
||
107 | panel.executeWizard(); |
||
108 | // Las capas se cargan en segundo plano, asi que si tratamos de
|
||
109 | // acceder a ellas ya mismo aun no estan cargadas.
|
||
110 | // No es muy ortodox pero nos esperaremos un poco.
|
||
111 | 46735 | jjdelcerro | Thread task = new Thread(() -> { |
112 | for (int i = 0; i < 30; i++) { |
||
113 | try { Thread.sleep(1000); } catch (Exception ex) { } |
||
114 | if (!mapContext.getLayers().isEmpty()) {
|
||
115 | FLyrVect layer = (FLyrVect) mapContext.getLayers().getFirstActiveVectorLayer(); |
||
116 | SwingUtilities.invokeLater(() -> {
|
||
117 | 47146 | jjdelcerro | GeometriesPalettePanel.showPalette(layer.getFeatureStore(), null, layer.getName(), null); |
118 | 46735 | jjdelcerro | }); |
119 | return;
|
||
120 | } |
||
121 | 45739 | jjdelcerro | } |
122 | 46735 | jjdelcerro | // TODO: Avisar que no se ha cargado ninguna capa ?
|
123 | }, "GeometriesPaleteLoader");
|
||
124 | task.start(); |
||
125 | 45739 | jjdelcerro | } |
126 | } |
||
127 | } catch (Exception ex) { |
||
128 | logger.warn("Can't open palete from layer",ex);
|
||
129 | } finally {
|
||
130 | DisposeUtils.disposeQuietly(addLayerDialog); |
||
131 | DisposeUtils.disposeQuietly(mapControl); |
||
132 | DisposeUtils.disposeQuietly(mapContext); |
||
133 | } |
||
134 | |||
135 | } |
||
136 | |||
137 | 46770 | jjdelcerro | private void doOpenGeometriesPalette(String tableName, String labelattr, String title, String filter) { |
138 | 46735 | jjdelcerro | DataManager dataManager = DALLocator.getDataManager(); |
139 | DataStore store = dataManager.getStoresRepository().getStore(tableName); |
||
140 | if( !(store instanceof FeatureStore) ) { |
||
141 | logger.warn("The table name '"+tableName+"' is not valid."); |
||
142 | return;
|
||
143 | } |
||
144 | FeatureStore featureStore = (FeatureStore) store; |
||
145 | 47146 | jjdelcerro | GeometriesPalettePanel.showPalette(featureStore, labelattr, title, filter); |
146 | 46735 | jjdelcerro | } |
147 | |||
148 | 45739 | jjdelcerro | @Override
|
149 | public boolean isEnabled() { |
||
150 | return true; |
||
151 | } |
||
152 | |||
153 | @Override
|
||
154 | public boolean isVisible() { |
||
155 | return true; |
||
156 | } |
||
157 | |||
158 | } |