Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph / src / org / gvsig / graph / gui / OdMatrixControlPanel.java @ 39203

History | View | Annotate | Download (12.1 KB)

1
/*
2
 * OdMatrixControlPanel.java
3
 *
4
 * Created on 20 de octubre de 2008, 13:19
5
 */
6

    
7
package org.gvsig.graph.gui;
8

    
9
import java.awt.Component;
10
import java.awt.Dimension;
11
import java.awt.Font;
12
import java.awt.Rectangle;
13
import java.util.Vector;
14

    
15
import javax.swing.DefaultComboBoxModel;
16
import javax.swing.JFileChooser;
17
import javax.swing.JFrame;
18
import javax.swing.JLabel;
19
import javax.swing.JOptionPane;
20
import javax.swing.JPanel;
21
import javax.swing.JTextArea;
22
import javax.swing.JTextField;
23
import javax.swing.SwingConstants;
24
import javax.swing.UIManager;
25
import javax.swing.UnsupportedLookAndFeelException;
26

    
27
import org.gvsig.exceptions.BaseException;
28
import org.gvsig.fmap.util.LayerListCellRenderer;
29
import org.gvsig.graph.IODMatrixFileWriter;
30
import org.gvsig.graph.ODMatrixExtension;
31

    
32
import com.iver.andami.PluginServices;
33
import com.iver.andami.ui.mdiManager.IWindow;
34
import com.iver.andami.ui.mdiManager.WindowInfo;
35
import com.iver.cit.gvsig.fmap.MapContext;
36
import com.iver.cit.gvsig.fmap.core.FShape;
37
import com.iver.cit.gvsig.fmap.layers.FLayer;
38
import com.iver.cit.gvsig.fmap.layers.FLayers;
39
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
40
import com.iver.cit.gvsig.fmap.layers.LayersIterator;
41

    
42
/**
43
 * 
44
 * @author Fjp
45
 */
46
public class OdMatrixControlPanel extends JPanel implements IWindow {
47

    
48
        public static int TIME_SECONDS = 0;
49
        public static int TIME_MINUTES = 1;
50
        public static int LENGTH_METERS = 0;
51
        public static int LENGTH_KILOMETERS = 1;
52
        public static int LENGTH_YARDS = 2;
53
        public static int LENGTH_MILES = 3;
54
        public static int FILE_FORMAT_2_COLS = 0;
55
        public static int FILE_FORMAT_MANY_COLS = 1;
56

    
57
        private MapContext mapContext;
58
        
59
        private DefaultComboBoxModel cboLayerOriginsModel;
60
        private DefaultComboBoxModel cboLayerDestinationsModel;
61

    
62
        private WindowInfo wi;
63
        private boolean okPressed = false;
64

    
65
        /**
66
         * This method initializes txtTolerance        
67
         *         
68
         * @return javax.swing.JTextField        
69
         */
70
        private JTextField getTxtTolerance() {
71
                if (txtTolerance == null) {
72
                        txtTolerance = new JTextField();
73
                        txtTolerance.setBounds(new Rectangle(159, 118, 94, 22));
74
                        txtTolerance.setText("50");
75
                }
76
                return txtTolerance;
77
        }
78

    
79
        /**
80
         * This method initializes txtFormatAreaDescription        
81
         *         
82
         * @return javax.swing.JTextArea        
83
         */
84
        private JTextArea getTxtFormatAreaDescription() {
85
                if (txtFormatAreaDescription == null) {
86
                        txtFormatAreaDescription = new JTextArea();
87
                        txtFormatAreaDescription.setBounds(new Rectangle(16, 55, 438, 53));
88
                        txtFormatAreaDescription.setRows(3);
89
                        txtFormatAreaDescription.setFont(new Font("Arial", Font.PLAIN, 10));
90
                        txtFormatAreaDescription.setLineWrap(true);
91
                        txtFormatAreaDescription.setEditable(false);
92
                }
93
                return txtFormatAreaDescription;
94
        }
95

    
96
        public static void main(String[] args) throws ClassNotFoundException,
97
                        InstantiationException, IllegalAccessException {
98
                try {
99

    
100
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
101
                } catch (UnsupportedLookAndFeelException e) {
102
                        // TODO Auto-generated catch block
103
                        e.printStackTrace();
104
                }
105
                OdMatrixControlPanel panel = new OdMatrixControlPanel();
106
                JFrame test = new JFrame();
107

    
108
                test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
109
                test.add(panel);
110
                test.setBounds(panel.getBounds());
111
                test.setVisible(true);
112
        }
113

    
114
        /** Creates new form OdMatrixControlPanel */
115
        public OdMatrixControlPanel() {
116
                initComponents();
117
        }
118

    
119
        /**
120
         * This method is called from within the constructor to initialize the form.
121
         * WARNING: Do NOT modify this code. The content of this method is always
122
         * regenerated by the Form Editor.
123
         */
124
        @SuppressWarnings("unchecked")
125
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
126
        private void initComponents() {
127

    
128
                jLblToleranceUnits = new JLabel();
129
                jLblToleranceUnits.setBounds(new Rectangle(265, 118, 135, 20));
130
                jLblToleranceUnits.setText(_T("meters"));
131
                jLblTolerance = new JLabel();
132
                jLblTolerance.setBounds(new Rectangle(17, 118, 135, 20));
133
                jLblTolerance.setText(_T("Tolerance") + ":");
134
                jLblTolerance.setHorizontalAlignment(SwingConstants.RIGHT);
135
                this.setLayout(null);
136
                this.setSize(new Dimension(499, 350));
137
                jPanel1 = new javax.swing.JPanel();
138
                cboFileFormat = new javax.swing.JComboBox();
139
                jLabel5 = new javax.swing.JLabel();
140
                jPanel2 = new javax.swing.JPanel();
141
                cboLayerOrigins = new javax.swing.JComboBox();
142
                jLblLayerOrigins = new javax.swing.JLabel();
143
                cboLayerDestinations = new javax.swing.JComboBox();
144
                jLblLayerDestinations = new javax.swing.JLabel();
145
                jLblFile = new javax.swing.JLabel();
146
                txtGeneratedFile = new javax.swing.JTextField();
147
                btnSelectFile = new javax.swing.JButton();
148
                btnOk = new javax.swing.JButton();
149
                btnCancel = new javax.swing.JButton();
150

    
151
                jPanel1.setLayout(null);
152
                jPanel1.setBorder(javax.swing.BorderFactory
153
                                .createTitledBorder(_T("Options")));
154

    
155
                jPanel1.setBounds(new Rectangle(14, 172, 468, 122));
156
                jPanel1.add(cboFileFormat, null);
157
                jPanel1.add(jLabel5, null);
158

    
159
                jPanel1.add(getTxtFormatAreaDescription(), null);
160
                IODMatrixFileWriter[] writers = ODMatrixExtension.getOdMatrixWriters();
161
                String[] formats = new String[writers.length];
162
                for (int i = 0; i < formats.length; i++) {
163
                        formats[i] = _T(writers[i].getFormatDescription());
164
                }
165
                cboFileFormat.setModel(new DefaultComboBoxModel(formats));
166
                cboFileFormat.setSelectedIndex(0);
167

    
168
                cboFileFormat.setBounds(new Rectangle(158, 19, 295, 25));
169
                cboFileFormat.addItemListener(new java.awt.event.ItemListener() {
170
                        public void itemStateChanged(java.awt.event.ItemEvent e) {
171
                                System.out.println(e.getItem());
172
                                
173
                                txtFormatAreaDescription.setText(e.getItem().toString());
174
                        }
175
                });
176
                jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
177
                jLabel5.setBounds(new Rectangle(17, 22, 134, 16));
178
                jLabel5.setText(_T("File_Format") + ":");
179

    
180
                jPanel2.setLayout(null);
181

    
182
                // jPanel2.setLayout(new GridBagLayout(jPanel2, BoxLayout.Y_AXIS));
183
                jPanel2.setBorder(javax.swing.BorderFactory
184
                                .createTitledBorder(_T("Parameters")));
185

    
186
                jPanel2.setBounds(new Rectangle(14, 13, 468, 144));
187

    
188
                cboLayerOrigins.setBounds(new Rectangle(158, 32, 259, 22));
189
                jLblLayerOrigins
190
                                .setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
191
                jLblLayerOrigins.setBounds(new Rectangle(17, 33, 135, 20));
192
                jLblLayerOrigins.setText(_T("Layer_Origins") + ":");
193

    
194
                cboLayerDestinations.setBounds(new Rectangle(158, 61, 259, 22));
195
                jLblLayerDestinations
196
                                .setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
197
                jLblLayerDestinations.setBounds(new Rectangle(17, 59, 135, 20));
198
                jLblLayerDestinations.setText(_T("Layer_Destinations") + ":");
199

    
200
                jLblFile.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
201
                jLblFile.setBounds(new Rectangle(17, 90, 135, 20));
202
                jLblFile.setText(_T("Generated_File") + ":");
203

    
204
                txtGeneratedFile.setText("");
205

    
206
                txtGeneratedFile.setBounds(new Rectangle(159, 90, 259, 22));
207
                btnSelectFile.setText("...");
208

    
209
                btnSelectFile.setBounds(new Rectangle(425, 90, 28, 21));
210
                btnSelectFile.addActionListener(new java.awt.event.ActionListener() {
211
                        public void actionPerformed(java.awt.event.ActionEvent e) {
212
                                JFileChooser dlg = new JFileChooser();
213
                                if (dlg.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION)
214
                                {
215
                                        txtGeneratedFile.setText(dlg.getSelectedFile().getPath());
216
                                }
217
                        }
218
                });
219
                btnOk.setText(_T("OK"));
220
                btnOk.setBounds(new Rectangle(107, 305, 136, 26));
221
                btnOk.addActionListener(new java.awt.event.ActionListener() {
222
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
223
                                btnOkActionPerformed(evt);
224
                        }
225
                });
226

    
227
                btnCancel.setText(_T("Cancel"));
228
                btnCancel.setBounds(new Rectangle(257, 305, 136, 26));
229
                jPanel2.add(jLblLayerOrigins, null);
230
                jPanel2.add(cboLayerOrigins, null);
231
                jPanel2.add(jLblLayerDestinations, null);
232
                jPanel2.add(cboLayerDestinations, null);
233
                jPanel2.add(jLblFile, null);
234
                jPanel2.add(txtGeneratedFile, null);
235
                jPanel2.add(btnSelectFile, null);
236
                jPanel2.add(jLblTolerance, null);
237
                jPanel2.add(getTxtTolerance(), null);
238
                jPanel2.add(jLblToleranceUnits, null);
239
                this.add(jPanel2, null);
240
                this.add(jPanel1, null);
241
                this.add(btnOk, null);
242
                this.add(btnCancel, null);
243
                btnCancel.addActionListener(new java.awt.event.ActionListener() {
244
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
245
                                btnCancelActionPerformed(evt);
246
                        }
247
                });
248

    
249
        }// </editor-fold>
250

    
251
        private void closeWindow() {
252
                if (PluginServices.getMainFrame() != null) {
253
                        PluginServices.getMDIManager().closeWindow(this);
254
                }
255
        }
256

    
257
        private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {
258
                if (getGeneratedFile().equalsIgnoreCase(""))
259
                {
260
                        JOptionPane.showMessageDialog((Component) PluginServices.getMDIManager().getActiveWindow(),
261
                                        PluginServices.getText(null, "Please_select_a_valid_file"));
262
                        return;
263
                }
264
                try {
265
                        double tol = getTolerance();
266
                } catch (NumberFormatException e) {
267
                        JOptionPane.showMessageDialog((Component) PluginServices.getMDIManager().getActiveWindow(),
268
                                        PluginServices.getText(null, "Please_enter_a_valid_number"));
269
                        return;
270
                }
271
                
272
                okPressed = true;
273
                closeWindow();
274
        }
275

    
276
        private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
277
                closeWindow();
278
        }
279
        
280
        /**
281
         * @return Returns the okPressed.
282
         */
283
        public boolean isOkPressed() {
284
                return okPressed;
285
        }
286

    
287
        // Variables declaration - do not modify
288
        private javax.swing.JButton btnCancel;
289
        private javax.swing.JButton btnOk;
290
        private javax.swing.JButton btnSelectFile;
291
        private javax.swing.JComboBox cboFileFormat;
292
        private javax.swing.JComboBox cboLayerDestinations;
293
        private javax.swing.JComboBox cboLayerOrigins;
294
        private javax.swing.JLabel jLabel5;
295
        private javax.swing.JLabel jLblFile;
296
        private javax.swing.JLabel jLblLayerDestinations;
297
        private javax.swing.JLabel jLblLayerOrigins;
298
        private javax.swing.JPanel jPanel1;
299
        private javax.swing.JPanel jPanel2;
300
        private javax.swing.JTextField txtGeneratedFile;
301
        private JLabel jLblTolerance = null;
302
        private JTextField txtTolerance = null;
303
        private JLabel jLblToleranceUnits = null;
304
        private JTextArea txtFormatAreaDescription = null;
305

    
306
        // End of variables declaration
307

    
308
        public void setMapContext(MapContext mapContext) throws BaseException {
309
                this.mapContext = mapContext;
310
                FLayers layers = mapContext.getLayers();
311
                LayersIterator it = new LayersIterator(layers);
312
                Vector<FLyrVect> arrayLayers = new Vector<FLyrVect>();
313
                while (it.hasNext()) {
314
                        FLayer lyr = it.nextLayer();
315
                        if (!lyr.isAvailable())
316
                                continue;
317
                        if (lyr instanceof FLyrVect) {
318
                                FLyrVect lyrVect = (FLyrVect) lyr;
319
                                if ((lyrVect.getShapeType() == FShape.POINT)
320
                                                || (lyrVect.getShapeType() == FShape.MULTIPOINT))
321
                                        arrayLayers.add(lyrVect);
322
                                        
323
                        }
324
                }
325
                cboLayerOriginsModel = new DefaultComboBoxModel(arrayLayers);
326
                cboLayerDestinationsModel = new DefaultComboBoxModel(arrayLayers);
327
                cboLayerOrigins.setModel(cboLayerOriginsModel);
328
                cboLayerDestinations.setModel(cboLayerDestinationsModel);
329
                cboLayerOrigins.setRenderer(new LayerListCellRenderer());
330
                cboLayerDestinations.setRenderer(new LayerListCellRenderer());
331
                
332
        }
333

    
334
        public int getFileFormat() {
335
                return cboFileFormat.getSelectedIndex();
336
        }
337
        
338
        public String getGeneratedFile() {
339
                return txtGeneratedFile.getText();
340
        }
341
        
342
        public FLyrVect getOriginsLayer() {
343
                return (FLyrVect) cboLayerOrigins.getSelectedItem();
344
        }
345

    
346
        public FLyrVect getDestinationsLayer() {
347
                return (FLyrVect) cboLayerDestinations.getSelectedItem();
348
        }
349

    
350
        public WindowInfo getWindowInfo() {
351
                if (wi == null) {
352
                        wi = new WindowInfo(WindowInfo.MODALDIALOG);
353
                        wi.setWidth((int) this.getPreferredSize().getWidth());
354
                        wi.setHeight((int) this.getPreferredSize().getHeight());
355
                        wi.setTitle(_T("odmatrix_control_panel"));
356
                }
357
                return wi;
358
        }
359
        
360
        public Object getWindowProfile(){
361
                return WindowInfo.DIALOG_PROFILE;
362
        }
363

    
364
        public double getTolerance() {
365
                return Double.parseDouble(txtTolerance.getText());
366
        }
367
        
368
        private String _T(String str) {
369
                return PluginServices.getText(this, str);
370
        }
371

    
372
} // @jve:decl-index=0:visual-constraint="10,10"