Statistics
| Revision:

root / trunk / extensions / extGraph / src / org / gvsig / graph / ODMatrixExtension.java @ 31735

History | View | Annotate | Download (6.55 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.graph;
42

    
43
import java.awt.Component;
44
import java.io.File;
45
import java.util.ArrayList;
46

    
47
import javax.swing.JOptionPane;
48

    
49
import org.gvsig.exceptions.BaseException;
50
import org.gvsig.graph.core.GraphException;
51
import org.gvsig.graph.core.GvFlag;
52
import org.gvsig.graph.core.Network;
53
import org.gvsig.graph.core.NetworkUtils;
54
import org.gvsig.graph.gui.ODMatrixTask;
55
import org.gvsig.graph.gui.OdMatrixControlPanel;
56

    
57
import com.iver.andami.PluginServices;
58
import com.iver.andami.plugins.Extension;
59
import com.iver.andami.ui.mdiManager.IWindow;
60
import com.iver.cit.gvsig.fmap.MapContext;
61
import com.iver.cit.gvsig.fmap.MapControl;
62
import com.iver.cit.gvsig.fmap.layers.FLayer;
63
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
64
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
65
import com.iver.cit.gvsig.project.documents.view.gui.View;
66

    
67
/**
68
 * @author Francisco Jos? Pe?arrubia (fjp@scolab.es)
69
 *
70
 * ODMatrixControlPanel sets selectedWriter to allow multiple export formats
71
 */
72
public class ODMatrixExtension extends Extension {
73

    
74

    
75

    
76
        private static ArrayList<IODMatrixFileWriter> odMatrixWriters = new ArrayList<IODMatrixFileWriter>();
77

    
78
        private static IODMatrixFileWriter selectedWriter;
79
        
80
        public static void registerOdMatrixFormat(IODMatrixFileWriter w) {
81
                odMatrixWriters.add(w);
82
        }
83
        
84
        public static IODMatrixFileWriter[] getOdMatrixWriters() {
85
                return (IODMatrixFileWriter[]) odMatrixWriters.toArray(new IODMatrixFileWriter[0]);
86
        }
87
        
88
        public void initialize() {
89
                PluginServices.getIconTheme().registerDefault(
90
                                "odmatrix",
91
                                this.getClass().getClassLoader().getResource("images/odmatrix.png")
92
                        );                
93
                
94
                ODMatrixFileWriter4cols f1 = new ODMatrixFileWriter4cols();
95
                ODMatrixFileWriter4cols_minutes_km f2 = new ODMatrixFileWriter4cols_minutes_km();
96
                ODMatrixFileWriterRFormat f3 = new ODMatrixFileWriterRFormat();
97
                registerOdMatrixFormat(f1);
98
                registerOdMatrixFormat(f2);
99
                registerOdMatrixFormat(f3);
100
        }
101

    
102
        public void execute(String actionCommand) {
103
                
104
                View v = (View) PluginServices.getMDIManager().getActiveWindow();
105
                MapContext map = v.getMapControl().getMapContext();
106
                SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
107

    
108
                if (actionCommand.equals("ODMATRIX")) {
109
                        while (it.hasNext())
110
                        {
111
                                FLayer aux = it.next();
112
                                if (!aux.isActive())
113
                                        continue;
114
                                Network net = (Network) aux.getProperty("network");
115

    
116
                                if ( net != null)
117
                                {
118
                                        OdMatrixControlPanel ctrlDlg = new OdMatrixControlPanel();
119
                                        try {
120
                                                ctrlDlg.setMapContext(map);
121
                                                PluginServices.getMDIManager().addWindow(ctrlDlg);
122
                                                if (ctrlDlg.isOkPressed()) {
123
                                                        if (net.getLayer().getISpatialIndex() == null)
124
                                                        {
125
                                                                System.out.println("Calculando ?ndice espacial (QuadTree, que es m?s r?pido)...");
126
                                                                net.getLayer().setISpatialIndex(
127
                                                                                NetworkUtils.createJtsQuadtree(net.getLayer()));
128
                                                                System.out.println("Indice espacial calculado.");
129
                                                        }
130
                                                        FLyrVect layerOrigins = ctrlDlg.getOriginsLayer();
131
                                                        FLyrVect layerDestinations = ctrlDlg.getDestinationsLayer();
132
                                                        boolean bSameLayer = false;
133
                                                        if (layerOrigins == layerDestinations)
134
                                                                bSameLayer = true;
135
                                                        double tolerance = ctrlDlg.getTolerance();
136
                                                        GvFlag[] originFlags = NetworkUtils.putFlagsOnNetwork(layerOrigins,
137
                                                                        net, tolerance);
138
                                                        GvFlag[] destinationFlags = null; 
139
                                                        if (bSameLayer)
140
                                                                destinationFlags = originFlags;
141
                                                        else
142
                                                                destinationFlags = NetworkUtils.putFlagsOnNetwork(layerDestinations, net, tolerance);
143
                                                        
144
                                                        File selectedFile = new File(ctrlDlg.getGeneratedFile());
145
                                                        
146
                                                        
147
                                                        selectedWriter = odMatrixWriters.get(ctrlDlg.getFileFormat());
148
                                                        
149
                                                        ODMatrixTask task = new ODMatrixTask(net, originFlags, destinationFlags,
150
                                                                        selectedFile, selectedWriter);
151
                                                        PluginServices.cancelableBackgroundExecution(task);
152
                                                        // calculateOdMatrix(net, originFlags, destinationFlags, selectedFile);
153
                                                        
154
                                                        // TODO: ASK THE USER IF HE WANTS TO SAVE FLAGS TO AVOID PUTTING POINTS
155
                                                        // ON NETWORK AGAIN
156
                                                        
157
                                                        return;
158
                                                } // isOkPressed
159
                                        } catch (BaseException e) {
160
                                                e.printStackTrace();
161
                                                if (e.getCode() == GraphException.FLAG_OUT_NETWORK) {
162
                                                        JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(), PluginServices.getText(null, "there_are_points_outside_the_tolerance"));
163
//                                                        NotificationManager.addError(e.getFormatString(), e);
164
                                                }
165
                                        }
166

    
167
                                }
168
                        } 
169
                }
170
                
171

    
172
        }
173
        
174
        public boolean isEnabled() {
175
                IWindow window = PluginServices.getMDIManager().getActiveWindow();
176
                if (window instanceof View)
177
                {
178
                        View v = (View) window;
179
                MapControl mapCtrl = v.getMapControl();
180
                        MapContext map = mapCtrl.getMapContext();
181
                        
182
                        SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
183
                        while (it.hasNext())
184
                        {
185
                                FLayer aux = it.next();
186
                                if (!aux.isActive())
187
                                        continue;
188
                                Network net = (Network) aux.getProperty("network");
189
                                
190
                                if ( net != null)
191
                                {
192
                                        return true;
193
                                }
194
                        }
195
                }
196
                return false;
197
        }
198

    
199
        public boolean isVisible() {
200
                IWindow f = PluginServices.getMDIManager()
201
                 .getActiveWindow();
202
                if (f == null) {
203
                    return false;
204
                }
205
                if (f instanceof View) {
206
                        return true;
207
                }
208
                return false;
209

    
210
        }
211

    
212
        public static IODMatrixFileWriter getSelectedWriter() {
213
                return selectedWriter;
214
        }
215

    
216
        public static void setSelectedWriter(IODMatrixFileWriter selectedWriter) {
217
                ODMatrixExtension.selectedWriter = selectedWriter;
218
        }
219

    
220

    
221
}
222

    
223