gvsig-lrs / org.gvsig.lrs / trunk / org.gvsig.lrs / org.gvsig.lrs.app / org.gvsig.lrs.app.mainplugin / src / main / java / org / gvsig / lrs / app / calibrateroute / MeasureCalculatorRouteExtension.java @ 495
History | View | Annotate | Download (6.99 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2015 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.lrs.app.calibrateroute; |
24 |
|
25 |
import java.awt.event.ActionEvent; |
26 |
import java.awt.event.ActionListener; |
27 |
|
28 |
import org.apache.commons.lang3.StringUtils; |
29 |
import org.gvsig.andami.IconThemeHelper; |
30 |
import org.gvsig.andami.PluginServices; |
31 |
import org.gvsig.andami.PluginsLocator; |
32 |
import org.gvsig.andami.PluginsManager; |
33 |
import org.gvsig.andami.plugins.Extension; |
34 |
import org.gvsig.app.ApplicationLocator; |
35 |
import org.gvsig.app.ApplicationManager; |
36 |
import org.gvsig.app.project.documents.view.ViewDocument; |
37 |
import org.gvsig.app.project.documents.view.gui.IView; |
38 |
import org.gvsig.fmap.dal.exception.DataException; |
39 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
40 |
import org.gvsig.fmap.dal.feature.Feature; |
41 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
42 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
43 |
import org.gvsig.fmap.geom.Geometry; |
44 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
45 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
46 |
import org.gvsig.lrs.lib.api.LrsAlgorithmsLocator; |
47 |
import org.gvsig.lrs.lib.api.LrsAlgorithmsManager; |
48 |
import org.gvsig.lrs.lib.api.MeasuresCalculator; |
49 |
import org.gvsig.lrs.lib.api.exceptions.LrsNeededParameterException; |
50 |
import org.gvsig.lrs.swing.api.JLrsAlgorithmParams; |
51 |
import org.gvsig.lrs.swing.api.JLrsLastUsedValues; |
52 |
import org.gvsig.lrs.swing.api.LrsAlgorithmsSwingLocator; |
53 |
import org.gvsig.lrs.swing.api.LrsAlgorithmsSwingManager; |
54 |
import org.gvsig.tools.ToolsLocator; |
55 |
import org.gvsig.tools.dispose.DisposableIterator; |
56 |
import org.gvsig.tools.dynobject.DynObject; |
57 |
import org.gvsig.tools.i18n.I18nManager; |
58 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
59 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
60 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
61 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
62 |
import org.slf4j.Logger; |
63 |
import org.slf4j.LoggerFactory; |
64 |
|
65 |
|
66 |
/**
|
67 |
* @author jbadia
|
68 |
*
|
69 |
*/
|
70 |
public class MeasureCalculatorRouteExtension extends Extension { |
71 |
|
72 |
private static final Logger logger = LoggerFactory.getLogger(MeasureCalculatorRouteExtension.class); |
73 |
|
74 |
/* (non-Javadoc)
|
75 |
* @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
|
76 |
*/
|
77 |
public void execute(String actionCommand) { |
78 |
|
79 |
if (StringUtils.equalsIgnoreCase(actionCommand, "calculate-m-routes")) { |
80 |
IView view = getActiveView(); |
81 |
FLyrVect layer = view.getMapControl().getMapContext().getLayers().getFirstActiveVectorLayer(); |
82 |
|
83 |
|
84 |
LrsAlgorithmsSwingManager manager = LrsAlgorithmsSwingLocator.getLrsAlgorithmsSwingManager(); |
85 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
86 |
|
87 |
PluginsManager pluginsManager = PluginsLocator.getManager(); |
88 |
PluginServices plugin = pluginsManager.getPlugin(this);
|
89 |
DynObject pluginProperties = plugin.getPluginProperties(); |
90 |
|
91 |
|
92 |
|
93 |
JLrsAlgorithmParams panel; |
94 |
try {
|
95 |
JLrsLastUsedValues params = manager.getJLrsLastUsedValues(pluginProperties); |
96 |
panel = manager.createJLrsCalculateMRoutePanel(layer, params); |
97 |
WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
98 |
|
99 |
final Dialog dialog = winManager.createDialog(panel.asJComponent(), |
100 |
i18nManager.getTranslation("m-coordinate-selector"), "", WindowManager_v2.BUTTONS_OK_CANCEL); |
101 |
dialog.addActionListener(new ActionListener() { |
102 |
public void actionPerformed(ActionEvent e) { |
103 |
if( dialog.getAction()==WindowManager_v2.BUTTON_OK) {
|
104 |
LrsAlgorithmsManager lrsAlgMng = LrsAlgorithmsLocator.getLrsAlgorithmsManager(); |
105 |
LrsAlgorithmsSwingManager lrsAlgSwingMng = LrsAlgorithmsSwingLocator.getLrsAlgorithmsSwingManager(); |
106 |
|
107 |
JLrsLastUsedValues prms = lrsAlgSwingMng.getJLrsLastUsedValues(); |
108 |
if(prms != null && !StringUtils.isBlank(prms.getPkInicial()) && !StringUtils.isBlank(prms.getPkFinal())){ |
109 |
|
110 |
FLyrVect lyrVect = (FLyrVect) getActiveView().getMapControl().getMapContext().getLayers().getFirstActiveVectorLayer(); |
111 |
FeatureStore featStore = lyrVect.getFeatureStore(); |
112 |
try {
|
113 |
featStore.edit(FeatureStore.MODE_FULLEDIT); |
114 |
|
115 |
FeatureSelection selection = featStore.getFeatureSelection(); |
116 |
DisposableIterator it = selection.fastIterator(); |
117 |
while(it.hasNext()){
|
118 |
Feature feat = (Feature) it.next(); |
119 |
EditableFeature editfeat = feat.getEditable(); |
120 |
|
121 |
MeasuresCalculator mcalc = lrsAlgMng.createMeasuresCalculator(editfeat.getDefaultGeometry(), false);
|
122 |
mcalc.setFirstMeasure(editfeat.getDouble(prms.getPkInicial())); |
123 |
mcalc.setLastMeasure(editfeat.getDouble(prms.getPkFinal())); |
124 |
mcalc.calculate(); |
125 |
Geometry mgeom = mcalc.getResult(); |
126 |
|
127 |
editfeat.setDefaultGeometry(mgeom); |
128 |
editfeat.validate(EditableFeature.CHECK_RULES_AT_FINISH); |
129 |
featStore.update(editfeat); |
130 |
} |
131 |
|
132 |
featStore.finishEditing(); |
133 |
} catch (DataException e1) {
|
134 |
logger.debug("Error creating the M coordinate: ", e1);
|
135 |
} |
136 |
}else{
|
137 |
|
138 |
} |
139 |
|
140 |
} |
141 |
} |
142 |
}); |
143 |
dialog.show(WindowManager.MODE.WINDOW); |
144 |
} catch (LrsNeededParameterException e1) {
|
145 |
// TODO Auto-generated catch block
|
146 |
e1.printStackTrace(); |
147 |
} |
148 |
|
149 |
} |
150 |
} |
151 |
|
152 |
/* (non-Javadoc)
|
153 |
* @see org.gvsig.andami.plugins.IExtension#initialize()
|
154 |
*/
|
155 |
public void initialize() { |
156 |
registerIcons(); |
157 |
} |
158 |
|
159 |
/* (non-Javadoc)
|
160 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
161 |
*/
|
162 |
public boolean isEnabled() { |
163 |
IView view = getActiveView(); |
164 |
if (view!=null){ |
165 |
ViewDocument viewDocument = view.getViewDocument(); |
166 |
FLayer[] layers = viewDocument.getMapContext().getLayers().getActives();
|
167 |
return layers.length > 0; |
168 |
} |
169 |
|
170 |
return false; |
171 |
|
172 |
} |
173 |
|
174 |
/* (non-Javadoc)
|
175 |
* @see org.gvsig.andami.plugins.IExtension#isVisible()
|
176 |
*/
|
177 |
public boolean isVisible() { |
178 |
ApplicationManager application = ApplicationLocator.getManager(); |
179 |
return application.getActiveComponent(ViewDocument.class) != null; |
180 |
} |
181 |
|
182 |
private void registerIcons() { |
183 |
IconThemeHelper.registerIcon("lrs", "pk_blue", this); |
184 |
} |
185 |
|
186 |
|
187 |
private IView getActiveView() {
|
188 |
ApplicationManager application = ApplicationLocator.getManager(); |
189 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
190 |
return view;
|
191 |
} |
192 |
|
193 |
|
194 |
} |