Statistics
| Revision:

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 / CalibrateRouteExtension.java @ 9

History | View | Annotate | Download (9.43 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.Dimension;
26
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
28

    
29
import javax.swing.JOptionPane;
30

    
31
import org.apache.commons.lang3.StringUtils;
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34

    
35
import org.gvsig.andami.IconThemeHelper;
36
import org.gvsig.andami.plugins.Extension;
37
import org.gvsig.app.ApplicationLocator;
38
import org.gvsig.app.ApplicationManager;
39
import org.gvsig.app.project.documents.view.ViewDocument;
40
import org.gvsig.app.project.documents.view.gui.IView;
41
import org.gvsig.fmap.dal.exception.ReadException;
42
import org.gvsig.fmap.geom.Geometry;
43
import org.gvsig.fmap.geom.type.GeometryType;
44
import org.gvsig.fmap.mapcontext.layers.FLayer;
45
import org.gvsig.fmap.mapcontext.layers.FLayers;
46
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
47
import org.gvsig.lrs.lib.api.LrsAlgorithm;
48
import org.gvsig.lrs.lib.api.LrsAlgorithmsLocator;
49
import org.gvsig.lrs.lib.api.LrsAlgorithmsManager;
50
import org.gvsig.lrs.lib.api.LrsCalibrateRouteAlgorithmParams;
51
import org.gvsig.lrs.lib.api.exceptions.LrsGettingParametersException;
52
import org.gvsig.lrs.lib.api.exceptions.LrsNeededParameterException;
53
import org.gvsig.lrs.swing.api.JLrsAlgorithmParams;
54
import org.gvsig.lrs.swing.api.JLrsProgressDialog;
55
import org.gvsig.lrs.swing.api.LrsAlgorithmsSwingLocator;
56
import org.gvsig.lrs.swing.api.LrsAlgorithmsSwingManager;
57
import org.gvsig.lrs.swing.impl.JLrsCalibrateRouteParamsController;
58
import org.gvsig.tools.ToolsLocator;
59
import org.gvsig.tools.i18n.I18nManager;
60
import org.gvsig.tools.observer.Observable;
61
import org.gvsig.tools.observer.Observer;
62
import org.gvsig.tools.swing.api.ToolsSwingLocator;
63
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
64
import org.gvsig.tools.task.SimpleTaskStatus;
65

    
66

    
67
/**
68
 * @author fdiaz
69
 *
70
 */
71
public class CalibrateRouteExtension extends Extension {
72

    
73
    private static final Logger logger = LoggerFactory.getLogger(CalibrateRouteExtension.class);
74

    
75
    /* (non-Javadoc)
76
     * @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
77
     */
78
    public void execute(String actionCommand) {
79
        if (StringUtils.equalsIgnoreCase(actionCommand, "calibrate-routes")) {
80
            IView view = getActiveView();
81
            FLayers layers = view.getMapControl().getMapContext().getLayers();
82

    
83
            final WindowManager winManager = ToolsSwingLocator.getWindowManager();
84

    
85
            final LrsAlgorithmsSwingManager manager = LrsAlgorithmsSwingLocator.getLrsAlgorithmsSwingManager();
86

    
87
            final I18nManager i18nManager = ToolsLocator.getI18nManager();
88

    
89
            JLrsCalibrateRouteParamsController panel;
90
            try {
91
                panel = (JLrsCalibrateRouteParamsController)manager.createJLrsCalibrateRouteAlgorithmParameters(layers, null);
92
            } catch (LrsNeededParameterException e2) {
93
                logger.error("Error creating panel", e2);
94
                JOptionPane.showMessageDialog(
95
                    null,
96
                    new StringBuilder().append(i18nManager.getTranslation("error_creating_panel"))
97
                        .append(":").append(e2.getLocalizedMessage()),
98
                    i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
99
                return;
100
            }
101
            panel.setVisibleAceptCancel(true);
102
            panel.addActionListener(new ActionListener() {
103

    
104
                public void actionPerformed(ActionEvent e) {
105
                    JLrsAlgorithmParams panel = (JLrsAlgorithmParams) e.getSource();
106
                    if (panel.isCanceled()) {
107
                        return;
108
                    }
109
                    LrsCalibrateRouteAlgorithmParams params = null;
110
                    try {
111
                        params = (LrsCalibrateRouteAlgorithmParams)panel.getParams();
112
                    } catch (LrsGettingParametersException e1) {
113
                        logger.error("Error getting parameters", e1);
114
                        JOptionPane.showMessageDialog(
115
                            null,
116
                            new StringBuilder().append(i18nManager.getTranslation("error_getting_parameters"))
117
                                .append(":").append(e1.getLocalizedMessage()),
118
                            i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
119
                        return;
120
                    } catch (LrsNeededParameterException e1) {
121
                        logger.warn("Error getting parameters", e1);
122
                        JOptionPane.showMessageDialog(
123
                            null,
124
                            i18nManager.getTranslation(e1.getMessage()),
125
                            i18nManager.getTranslation("warning"), JOptionPane.WARNING_MESSAGE);
126
                        return;
127
                    }
128
                    LrsAlgorithmsManager algorithmsManager = LrsAlgorithmsLocator.getLrsAlgorithmsManager();
129
                    final LrsAlgorithm algorithm = algorithmsManager.createLrsAlgorithm(params);
130
                    final SimpleTaskStatus taskStatus =
131
                        ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus(i18nManager.getTranslation("progress"));
132

    
133
                    final JLrsProgressDialog progressDialog = manager.createJLrsProgressDialog(taskStatus);
134

    
135
                    winManager.showWindow(progressDialog.asJComponent(), i18nManager.getTranslation("create_routes"),
136
                        WindowManager.MODE.WINDOW);
137

    
138
                    Thread task = new Thread(new Runnable() {
139

    
140
                        public void run() {
141
                            try {
142
                                algorithm.execute(taskStatus);
143
                            } catch (Exception e) {
144
                                logger.error("Error creating routes", e);
145
                                String message = e.getMessage();
146
                                if (e.getCause() != null) {
147
                                    message = e.getCause().getMessage();
148
                                }
149
//                                JOptionPane.showMessageDialog(progressDialog.asJComponent(), message);
150
                                JOptionPane.showMessageDialog(
151
                                    progressDialog.asJComponent(),
152
                                    message,
153
                                    i18nManager.getTranslation("create_routes"),
154
                                    JOptionPane.ERROR_MESSAGE
155
                                );
156
                            }
157
                        }
158
                    });
159
                    task.start();
160

    
161
                }
162
            });
163
            winManager.showWindow(panel.asJComponent(), i18nManager.getTranslation("create_routes"),
164
                WindowManager.MODE.WINDOW);
165
        }
166
    }
167

    
168
    /* (non-Javadoc)
169
     * @see org.gvsig.andami.plugins.IExtension#initialize()
170
     */
171
    public void initialize() {
172
        registerIcons();
173
    }
174

    
175
    /* (non-Javadoc)
176
     * @see org.gvsig.andami.plugins.IExtension#isEnabled()
177
     */
178
    public boolean isEnabled() {
179
        IView view = getActiveView();
180
        FLyrVect activeLayer = getActiveLayer(view);
181
        if (activeLayer != null) {
182
            try {
183
                GeometryType geometryType = activeLayer.getGeometryType();
184
                if (activeLayer != null
185
                    && (geometryType.isTypeOf(Geometry.TYPES.MULTICURVE) || geometryType.isTypeOf(Geometry.TYPES.CURVE))) {
186
                    return true;
187
                }
188
            } catch (ReadException e) {
189
                StringBuilder builder = new StringBuilder("Can't read geometry type from layer ");
190
                logger.warn(builder.append(activeLayer.getName()).toString(), e);
191
            }
192
        }
193
        return false;
194
    }
195

    
196
    /* (non-Javadoc)
197
     * @see org.gvsig.andami.plugins.IExtension#isVisible()
198
     */
199
    public boolean isVisible() {
200
        return true;
201
    }
202

    
203
    private void registerIcons() {
204
        IconThemeHelper.registerIcon("lrs", "pk_blue", this);
205
    }
206

    
207

    
208
    private IView getActiveView() {
209
        ApplicationManager application = ApplicationLocator.getManager();
210
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
211
        return view;
212
    }
213

    
214
    private FLyrVect getActiveLayer(IView vista) {
215
        if (vista != null) {
216
            ViewDocument viewDocument = vista.getViewDocument();
217
            FLayer[] actives =
218
                viewDocument.getMapContext().getLayers().getActives();
219

    
220
            if ((actives.length == 1) && (actives[0] instanceof FLyrVect)) {
221
                return (FLyrVect) actives[0];
222
            }
223
        }
224
        return null;
225
    }
226

    
227
}