Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph / src / org / gvsig / graph / ODMatrixExtension.java @ 29994

History | View | Annotate | Download (7.91 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.BufferedWriter;
45
import java.io.File;
46
import java.io.IOException;
47
import java.util.ArrayList;
48

    
49
import javax.swing.JOptionPane;
50

    
51
import org.gvsig.exceptions.BaseException;
52
import org.gvsig.graph.core.GraphException;
53
import org.gvsig.graph.core.GvFlag;
54
import org.gvsig.graph.core.Network;
55
import org.gvsig.graph.core.NetworkUtils;
56
import org.gvsig.graph.gui.ODMatrixTask;
57
import org.gvsig.graph.gui.OdMatrixControlPanel;
58
import org.gvsig.graph.solvers.OneToManySolver;
59

    
60
import com.iver.andami.PluginServices;
61
import com.iver.andami.messages.NotificationManager;
62
import com.iver.andami.plugins.Extension;
63
import com.iver.andami.ui.mdiManager.IWindow;
64
import com.iver.cit.gvsig.fmap.MapContext;
65
import com.iver.cit.gvsig.fmap.MapControl;
66
import com.iver.cit.gvsig.fmap.layers.FLayer;
67
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
68
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
69
import com.iver.cit.gvsig.project.documents.gui.IDocumentWindow;
70
import com.iver.cit.gvsig.project.documents.view.gui.View;
71

    
72
/**
73
 * @author Francisco Jos? Pe?arrubia (fjp@scolab.es)
74
 *
75
 * ODMatrixControlPanel sets selectedWriter to allow multiple export formats
76
 */
77
public class ODMatrixExtension extends Extension {
78

    
79

    
80

    
81
        private static ArrayList<IODMatrixFileWriter> odMatrixWriters = new ArrayList<IODMatrixFileWriter>();
82

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

    
107
        public void execute(String actionCommand) {
108
                
109
                View v = (View) PluginServices.getMDIManager().getActiveWindow();
110
                MapContext map = v.getMapControl().getMapContext();
111
                SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
112

    
113
                if (actionCommand.equals("ODMATRIX")) {
114
                        while (it.hasNext())
115
                        {
116
                                FLayer aux = it.next();
117
                                if (!aux.isActive())
118
                                        continue;
119
                                Network net = (Network) aux.getProperty("network");
120

    
121
                                if ( net != null)
122
                                {
123
                                        OdMatrixControlPanel ctrlDlg = new OdMatrixControlPanel();
124
                                        try {
125
                                                ctrlDlg.setMapContext(map);
126
                                                PluginServices.getMDIManager().addWindow(ctrlDlg);
127
                                                if (ctrlDlg.isOkPressed()) {
128
                                                        FLyrVect layerOrigins = ctrlDlg.getOriginsLayer();
129
                                                        FLyrVect layerDestinations = ctrlDlg.getDestinationsLayer();
130
                                                        boolean bSameLayer = false;
131
                                                        if (layerOrigins == layerDestinations)
132
                                                                bSameLayer = true;
133
                                                        double tolerance = ctrlDlg.getTolerance();
134
                                                        
135
                                                        GvFlag[] originFlags = NetworkUtils.putFlagsOnNetwork(layerOrigins,
136
                                                                        net, tolerance);
137
                                                        GvFlag[] destinationFlags = null; 
138
                                                        if (bSameLayer)
139
                                                                destinationFlags = originFlags;
140
                                                        else
141
                                                                destinationFlags = NetworkUtils.putFlagsOnNetwork(layerDestinations, net, tolerance);
142
                                                        
143
                                                        File selectedFile = new File(ctrlDlg.getGeneratedFile());
144
                                                        
145
                                                        selectedWriter = odMatrixWriters.get(ctrlDlg.getFileFormat());
146
                                                        
147
                                                        ODMatrixTask task = new ODMatrixTask(net, originFlags, destinationFlags,
148
                                                                        selectedFile, selectedWriter);
149
                                                        PluginServices.cancelableBackgroundExecution(task);
150
                                                        // calculateOdMatrix(net, originFlags, destinationFlags, selectedFile);
151
                                                        
152
                                                        // TODO: ASK THE USER IF HE WANTS TO SAVE FLAGS TO AVOID PUTTING POINTS
153
                                                        // ON NETWORK AGAIN
154
                                                        
155
                                                        return;
156
                                                } // isOkPressed
157
                                        } catch (BaseException e) {
158
                                                e.printStackTrace();
159
                                                if (e.getCode() == GraphException.FLAG_OUT_NETWORK) 
160
                                                        NotificationManager.addError(e.getFormatString(), e);
161
                                        }
162

    
163
                                }
164
                        } 
165
                }
166
                
167

    
168
        }
169

    
170
        /**
171
         * @deprecated Use ODMatrixTask instead (to allow cancel the dialog and see progress
172
         * @param net
173
         * @param originFlags
174
         * @param destinationFlags
175
         * @param generatedFile
176
         */
177
        private void calculateOdMatrix(Network net, GvFlag[] originFlags, GvFlag[] destinationFlags, File generatedFile) {
178
                try {
179
                        selectedWriter.openFile(generatedFile);
180
//                                                output.setByteOrder(ByteOrder.LITTLE_ENDIAN);
181

    
182
                        OneToManySolver solver = new OneToManySolver();
183
                        solver.setNetwork(net);
184
                        solver.putDestinationsOnNetwork(destinationFlags);
185
                        for (int i=0; i < originFlags.length; i++)
186
                        {
187
                                
188
                                solver.setSourceFlag(originFlags[i]);
189
                                long t1 = System.currentTimeMillis();
190
                                
191
                                solver.calculate();
192
                                long t2 = System.currentTimeMillis();
193
                                System.out.println("Punto " + i + " de " + originFlags.length + ". " + (t2-t1) + " msecs.");
194
                                // Escribe el resultado
195
                                // idNodo1 idNodo2 tiempo distancia
196
                                
197
                                for (int j=0; j < destinationFlags.length; j++)
198
                                {
199
                                        selectedWriter.saveDist(i, j, destinationFlags[j].getCost()
200
                                                        , destinationFlags[j].getAccumulatedLength());
201
                                }
202
                                long t3 = System.currentTimeMillis();
203
                                System.out.println("T. de escritura: " + (t3-t2) + " msecs.");
204
                                
205
                        }
206
                        solver.removeDestinationsFromNetwork(destinationFlags);
207
                        selectedWriter.closeFile();
208
                        net.removeFlags();
209
                        JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(),
210
                                        PluginServices.getText(this,"fichero_generado"));
211
                } catch (IOException e) {
212
                        // TODO Auto-generated catch block
213
                        e.printStackTrace();
214
                } catch (GraphException e) {
215
                        // TODO Auto-generated catch block
216
                        e.printStackTrace();
217
                }
218
        }
219

    
220
        
221
        public boolean isEnabled() {
222
                IWindow window = PluginServices.getMDIManager().getActiveWindow();
223
                if (window instanceof View)
224
                {
225
                        View v = (View) window;
226
                MapControl mapCtrl = v.getMapControl();
227
                        MapContext map = mapCtrl.getMapContext();
228
                        
229
                        SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
230
                        while (it.hasNext())
231
                        {
232
                                FLayer aux = it.next();
233
                                if (!aux.isActive())
234
                                        continue;
235
                                Network net = (Network) aux.getProperty("network");
236
                                
237
                                if ( net != null)
238
                                {
239
                                        return true;
240
                                }
241
                        }
242
                }
243
                return false;
244
        }
245

    
246
        public boolean isVisible() {
247
                IWindow f = PluginServices.getMDIManager()
248
                 .getActiveWindow();
249
                if (f == null) {
250
                    return false;
251
                }
252
                if (f instanceof View) {
253
                        return true;
254
                }
255
                return false;
256

    
257
        }
258

    
259
        public static IODMatrixFileWriter getSelectedWriter() {
260
                return selectedWriter;
261
        }
262

    
263
        public static void setSelectedWriter(IODMatrixFileWriter selectedWriter) {
264
                ODMatrixExtension.selectedWriter = selectedWriter;
265
        }
266

    
267

    
268
}
269

    
270