Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph / src / org / gvsig / graph / gui / MultiInputDlg.java @ 30840

History | View | Annotate | Download (7.6 KB)

1
package org.gvsig.graph.gui;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.Component;
5
import java.awt.Dimension;
6
import java.awt.SystemColor;
7
import java.awt.event.ActionEvent;
8
import java.awt.event.ActionListener;
9
import java.io.File;
10
import java.io.FileInputStream;
11
import java.io.FileNotFoundException;
12
import java.io.FileOutputStream;
13
import java.io.IOException;
14
import java.util.ArrayList;
15
import java.util.Enumeration;
16
import java.util.Properties;
17

    
18
import javax.swing.JButton;
19
import javax.swing.JDialog;
20
import javax.swing.JOptionPane;
21
import javax.swing.JPanel;
22
import javax.swing.JScrollPane;
23
import javax.swing.JTable;
24
import javax.swing.JTextArea;
25
import javax.swing.filechooser.FileFilter;
26
import javax.swing.table.DefaultTableModel;
27

    
28
import org.gvsig.gui.beans.AcceptCancelPanel;
29
import org.gvsig.gui.beans.swing.JFileChooser;
30

    
31
import com.iver.andami.PluginServices;
32

    
33
public class MultiInputDlg extends JDialog {
34
        private boolean canceled = false;
35
        private class MyListener implements ActionListener {
36

    
37
                private MultiInputDlg dlg;
38
                public MyListener(MultiInputDlg dlg)
39
                {
40
                        this.dlg = dlg;
41
                }
42
                public void actionPerformed(ActionEvent e) {
43
                        if (e.getActionCommand().equalsIgnoreCase("OK"))
44
                        {
45
                                dlg.dispose();
46
                        }
47
                        if (e.getActionCommand().equalsIgnoreCase("CANCEL"))
48
                        {
49
                                dlg.canceled = true;
50
                                dlg.dispose();
51
                                
52
                        }                        
53
                }
54
                
55
        }
56

    
57
        private class MyModel extends DefaultTableModel {
58

    
59
                private ArrayList l;
60
                private ArrayList r;
61

    
62
                MyModel(ArrayList leftList, ArrayList rightList) {
63
                        l = leftList;
64
                        r = rightList;
65
                }
66
                
67
                public ArrayList getRigthValues()
68
                {
69
                        return r;
70
                }
71
                public int getColumnCount() {
72
                        return 2;
73
                }
74

    
75
                public int getRowCount() {
76
                        if (l == null) return 0;
77
                        return l.size();
78
                }
79

    
80
                public Object getValueAt(int row, int column) {
81
                        if (column == 0)
82
                                return l.get(row);
83
                        return r.get(row);
84
                }
85

    
86
                public boolean isCellEditable(int row, int column) {
87
                        if (column == 1)
88
                                return true;
89
                        else
90
                                return false;
91
                }
92

    
93
                public void setValueAt(Object aValue, int row, int column) {
94
                        r.set(row, aValue);
95
                }
96
                
97
        }
98
        private ArrayList firstList;
99
        private ArrayList secondList;
100
        
101
        private String msg;
102
        /**
103
         * 
104
         */
105
        private static final long serialVersionUID = 1914037292995440998L;
106
        private JTextArea jLblMsg = null;  //  @jve:decl-index=0:visual-constraint="220,10"
107
        private JScrollPane jScrollPane = null;  //  @jve:decl-index=0:visual-constraint="183,139"
108
        private JTable jTable = null;
109
        private String colName1;
110
        private String colName2;
111
        private JButton btnLoadVelocities;
112
        private JButton btnSaveVelocities;
113

    
114
        public MultiInputDlg(String msg, ArrayList leftValues, ArrayList veloKm) {
115
                firstList = leftValues;
116
                secondList = veloKm;
117
                this.msg = msg;
118
                initialize();
119
        }
120
        /**
121
         * This method initializes this
122
         * 
123
         */
124
        private void initialize() {
125
                jLblMsg = new JTextArea();
126
                jLblMsg.setText(msg);
127
                jLblMsg.setBackground(SystemColor.control);
128
                jLblMsg.setLineWrap(true);
129
                jLblMsg.setPreferredSize(new Dimension(200, 100));
130
                jLblMsg.setFocusable(false);
131
                
132
                BorderLayout layout = new BorderLayout();
133
                layout.setHgap(30);
134
                layout.setVgap(10);
135
                this.getContentPane().setLayout(new BorderLayout());
136
                this.getContentPane().add(jLblMsg, BorderLayout.NORTH);
137
                this.setSize(new Dimension(450, 350));
138
                this.getContentPane().add(getJScrollPane(), BorderLayout.CENTER);
139
                MyListener myListener = new MyListener(this);
140
                AcceptCancelPanel okCancelPanel = new AcceptCancelPanel(myListener, myListener);
141
                btnLoadVelocities = new JButton(PluginServices.getText(this,"load_velocities"));
142
                btnLoadVelocities.addActionListener(new ActionListener() {
143

    
144
                        public void actionPerformed(ActionEvent e) {
145
                                try {
146
                                        loadVelocities();
147
                                } catch (IOException e1) {
148
                                        // TODO Auto-generated catch block
149
                                        e1.printStackTrace();
150
                                        JOptionPane.showMessageDialog(null, e1.getMessage());
151
                                }
152
                        }
153
                        
154
                });
155
                btnSaveVelocities = new JButton(PluginServices.getText(this,"save_velocities"));
156
                btnSaveVelocities.addActionListener(new ActionListener() {
157

    
158
                        public void actionPerformed(ActionEvent e) {
159
                                try {
160
                                        saveVelocities();
161
                                } catch (IOException e1) {
162
                                        // TODO Auto-generated catch block
163
                                        e1.printStackTrace();
164
                                }
165
                        }
166
                        
167
                });
168
                JPanel aux = new JPanel();
169
                aux.add(btnLoadVelocities);
170
                aux.add(btnSaveVelocities);
171
                okCancelPanel.add(aux, BorderLayout.WEST);
172
                this.getContentPane().add(okCancelPanel, BorderLayout.SOUTH);
173
        
174
        }
175

    
176
        protected void saveVelocities() throws IOException {
177
                String curDir = System.getProperty("user.dir");
178

    
179
                JFileChooser fileChooser = new JFileChooser("velocities", new File(curDir));
180
                fileChooser.setFileFilter(new FileFilter() {
181

    
182
                        @Override
183
                        public boolean accept(File f) {
184
                                if (f.isDirectory())
185
                                        return true;
186
                                String path = f.getPath().toLowerCase();
187
                                if (path.endsWith(".properties"))
188
                                        return true;
189
                                return false;
190
                        }
191

    
192
                        @Override
193
                        public String getDescription() {
194
                                return ".properties files";
195
                        }
196
                        
197
                });
198
                int res = fileChooser.showSaveDialog((Component) PluginServices.getMainFrame());
199
                if (res==JFileChooser.APPROVE_OPTION) {
200
                        File f =fileChooser.getSelectedFile();
201
                        if (!f.getPath().toLowerCase().endsWith(".properties"))
202
                                f = new File(f.getPath() + ".properties");
203
                        
204
                        Properties prop = new Properties();
205
                        for (int i=0; i < firstList.size(); i++) {
206
                                prop.setProperty(firstList.get(i) + "", (String) secondList.get(i));
207
                        }
208
                        FileOutputStream fout = new FileOutputStream(f);
209
                        prop.store(fout, "");
210
                }
211
                
212
        }
213
        protected void loadVelocities() throws IOException {
214
                String curDir = System.getProperty("user.dir");
215

    
216
                JFileChooser fileChooser = new JFileChooser("velocities", new File(curDir));
217
                fileChooser.setFileFilter(new FileFilter() {
218

    
219
                        @Override
220
                        public boolean accept(File f) {
221
                                if (f.isDirectory())
222
                                        return true;
223
                                String path = f.getPath().toLowerCase();
224
                                if (path.endsWith(".properties"))
225
                                        return true;
226
                                return false;
227
                        }
228

    
229
                        @Override
230
                        public String getDescription() {
231
                                return ".properties files";
232
                        }
233
                        
234
                });
235
                int res = fileChooser.showOpenDialog((Component) PluginServices.getMainFrame());
236
                if (res==JFileChooser.APPROVE_OPTION) {
237
                        File f =fileChooser.getSelectedFile();
238
                        Properties prop = new Properties();
239
                        FileInputStream fin = new FileInputStream(f);
240
                        prop.load(fin);
241
                        firstList = new ArrayList();
242
                        secondList = new ArrayList();
243
                        Enumeration e = prop.keys();
244
                        while (e.hasMoreElements()) {
245
                                String key = (String) e.nextElement();
246
                                firstList.add(key);
247
                                secondList.add(prop.getProperty(key));
248
                        }
249
                        MyModel myModel = new MyModel(firstList, secondList);
250
                        getJTable().setModel(myModel);
251
                        String col1 = PluginServices.getText(this, "col_arc_type");
252
                        String col2 = PluginServices.getText(this, "col_km_per_hour");
253
                        setColumnNames(col1, col2);
254
                        
255
                }
256
                
257
        }
258
        public ArrayList getRightValues() {
259
                return secondList;
260
        }
261
        /**
262
         * This method initializes jScrollPane        
263
         *         
264
         * @return javax.swing.JScrollPane        
265
         */
266
        private JScrollPane getJScrollPane() {
267
                if (jScrollPane == null) {
268
                        jScrollPane = new JScrollPane();
269
                        jScrollPane.setViewportView(getJTable());
270
                }
271
                return jScrollPane;
272
        }
273
        /**
274
         * This method initializes jTable        
275
         *         
276
         * @return javax.swing.JTable        
277
         */
278
        private JTable getJTable() {
279
                if (jTable == null) {
280
                        jTable = new JTable();
281
                        MyModel myModel = new MyModel(firstList, secondList);
282
                        jTable.setModel(myModel);
283
                }
284
                return jTable;
285
        }
286
        public boolean isCanceled() {
287
                return canceled;
288
        }
289
        public void setColumnNames(String col1, String col2) {
290
                this.colName1 = col1;
291
                this.colName2 = col2;
292
                jTable.getColumnModel().getColumn(0).setHeaderValue(col1);
293
                jTable.getColumnModel().getColumn(1).setHeaderValue(col2);
294
        }
295

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