root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.gui / src / main / java / es / unex / sextante / gui / algorithm / FixedTableDialog.java @ 338
History | View | Annotate | Download (13.1 KB)
1 |
package es.unex.sextante.gui.algorithm; |
---|---|
2 |
|
3 |
import info.clearthought.layout.TableLayout; |
4 |
import info.clearthought.layout.TableLayoutConstants; |
5 |
|
6 |
import java.awt.BorderLayout; |
7 |
import java.awt.Frame; |
8 |
import java.awt.event.ActionEvent; |
9 |
import java.awt.event.ActionListener; |
10 |
import java.io.BufferedReader; |
11 |
import java.io.BufferedWriter; |
12 |
import java.io.File; |
13 |
import java.io.FileReader; |
14 |
import java.io.FileWriter; |
15 |
import java.io.IOException; |
16 |
import java.io.Writer; |
17 |
import java.util.ArrayList; |
18 |
|
19 |
import javax.swing.JButton; |
20 |
import javax.swing.JFileChooser; |
21 |
import javax.swing.JOptionPane; |
22 |
import javax.swing.JPanel; |
23 |
import javax.swing.JScrollPane; |
24 |
import javax.swing.JSeparator; |
25 |
import javax.swing.JTable; |
26 |
|
27 |
import es.unex.sextante.core.Sextante; |
28 |
import es.unex.sextante.parameters.FixedTableModel; |
29 |
|
30 |
/**
|
31 |
* A dialog to fill a fixed table for a ParameterFixedTable
|
32 |
*
|
33 |
* @author volaya
|
34 |
*
|
35 |
*/
|
36 |
public class FixedTableDialog |
37 |
extends
|
38 |
javax.swing.JDialog { |
39 |
|
40 |
private final FixedTableModel m_TableModel; |
41 |
private final FixedTableModel m_TableModelOrg; |
42 |
private JPanel jPanelMain; |
43 |
private JPanel jPanelAddRemoveRows; |
44 |
private JSeparator jSeparator; |
45 |
private JScrollPane jScrollPane; |
46 |
private JButton jButtonCancel; |
47 |
private JButton jButtonOK; |
48 |
private JButton jButtonRemoveRow; |
49 |
private JButton jButtonAddRow; |
50 |
private JTable jTable; |
51 |
private boolean m_bAccepted; |
52 |
private JPanel jPanelOpenSave; |
53 |
private JButton jButtonOpen; |
54 |
private JButton jButtonSave; |
55 |
|
56 |
|
57 |
/**
|
58 |
* Creates a new dialog
|
59 |
*
|
60 |
* @param window
|
61 |
* the parent window
|
62 |
* @param tableModel
|
63 |
* the table model of the parameter
|
64 |
*/
|
65 |
public FixedTableDialog(final Frame window, |
66 |
final FixedTableModel tableModel) {
|
67 |
|
68 |
|
69 |
super(window, "", true); |
70 |
|
71 |
this.setResizable(false); |
72 |
|
73 |
m_TableModelOrg = tableModel; |
74 |
m_TableModel = FixedTableModel.newInstance(tableModel); |
75 |
|
76 |
initGUI(); |
77 |
} |
78 |
|
79 |
|
80 |
private void initGUI() { |
81 |
try {
|
82 |
{ |
83 |
this.setTitle(Sextante.getText("Fixed_table")); |
84 |
{ |
85 |
jPanelMain = new JPanel(); |
86 |
getContentPane().add(jPanelMain, BorderLayout.CENTER);
|
87 |
final TableLayout jPanelMainLayout = new TableLayout( |
88 |
new double[][] { |
89 |
{ 3.0, TableLayoutConstants.FILL, TableLayoutConstants.FILL, TableLayoutConstants.FILL,
|
90 |
TableLayoutConstants.FILL, 3.0 },
|
91 |
{ 3.0, 75.0, TableLayoutConstants.FILL, 75.0, 5.0, 30.0, 3.0 } }); |
92 |
jPanelMainLayout.setHGap(5);
|
93 |
jPanelMainLayout.setVGap(5);
|
94 |
jPanelMain.setLayout(jPanelMainLayout); |
95 |
jPanelMain.setPreferredSize(new java.awt.Dimension(564, 322)); |
96 |
{ |
97 |
jScrollPane = new JScrollPane(); |
98 |
jPanelMain.add(jScrollPane, "1, 1, 3, 3");
|
99 |
jScrollPane.setPreferredSize(new java.awt.Dimension(432, 256)); |
100 |
{ |
101 |
jTable = new JTable(); |
102 |
jScrollPane.setViewportView(jTable); |
103 |
jTable.setModel(m_TableModel); |
104 |
} |
105 |
} |
106 |
{ |
107 |
jPanelOpenSave = new JPanel(); |
108 |
jPanelMain.add(jPanelOpenSave, "4, 3");
|
109 |
final TableLayout jPanelAddOpenSaveLayout = new TableLayout(new double[][] { |
110 |
{ TableLayoutConstants.MINIMUM, TableLayoutConstants.FILL, TableLayoutConstants.MINIMUM }, |
111 |
{ TableLayoutConstants.MINIMUM, TableLayoutConstants.FILL, TableLayoutConstants.FILL, |
112 |
TableLayoutConstants.MINIMUM } }); |
113 |
jPanelAddOpenSaveLayout.setHGap(5);
|
114 |
jPanelAddOpenSaveLayout.setVGap(5);
|
115 |
jPanelOpenSave.setLayout(jPanelAddOpenSaveLayout); |
116 |
jPanelOpenSave.setPreferredSize(new java.awt.Dimension(116, 56)); |
117 |
} |
118 |
{ |
119 |
jPanelAddRemoveRows = new JPanel(); |
120 |
final TableLayout jPanelAddRemoveRowsLayout = new TableLayout(new double[][] { |
121 |
{ TableLayoutConstants.MINIMUM, TableLayoutConstants.FILL, TableLayoutConstants.MINIMUM }, |
122 |
{ TableLayoutConstants.MINIMUM, TableLayoutConstants.FILL, TableLayoutConstants.FILL, |
123 |
TableLayoutConstants.MINIMUM } }); |
124 |
jPanelAddRemoveRowsLayout.setHGap(5);
|
125 |
jPanelAddRemoveRowsLayout.setVGap(5);
|
126 |
jPanelAddRemoveRows.setLayout(jPanelAddRemoveRowsLayout); |
127 |
jPanelMain.add(jPanelAddRemoveRows, "4, 1");
|
128 |
jPanelAddRemoveRows.setPreferredSize(new java.awt.Dimension(116, 56)); |
129 |
} |
130 |
{ |
131 |
jButtonRemoveRow = new JButton(); |
132 |
jPanelAddRemoveRows.add(jButtonRemoveRow, "1, 2");
|
133 |
jButtonRemoveRow.setText(Sextante.getText("Delete_row"));
|
134 |
jButtonRemoveRow.addActionListener(new ActionListener() { |
135 |
public void actionPerformed(final ActionEvent evt) { |
136 |
removeRow(); |
137 |
} |
138 |
}); |
139 |
if (m_TableModel.isNumberOfRowsFixed()) {
|
140 |
jButtonRemoveRow.setEnabled(false);
|
141 |
jButtonRemoveRow.setSize(100, 22);// |
142 |
} |
143 |
} |
144 |
{ |
145 |
jButtonAddRow = new JButton(); |
146 |
jPanelAddRemoveRows.add(jButtonAddRow, "1, 1");
|
147 |
jButtonAddRow.setText(Sextante.getText("Add_row"));
|
148 |
jButtonAddRow.addActionListener(new ActionListener() { |
149 |
public void actionPerformed(final ActionEvent evt) { |
150 |
addRow(); |
151 |
} |
152 |
}); |
153 |
if (m_TableModel.isNumberOfRowsFixed()) {
|
154 |
jButtonAddRow.setEnabled(false);
|
155 |
jButtonAddRow.setSize(100, 22); |
156 |
} |
157 |
} |
158 |
|
159 |
{ |
160 |
jButtonOpen = new JButton(); |
161 |
jPanelOpenSave.add(jButtonOpen, "1, 2");
|
162 |
jButtonOpen.setText(Sextante.getText("Open"));
|
163 |
jButtonOpen.addActionListener(new ActionListener() { |
164 |
public void actionPerformed(final ActionEvent evt) { |
165 |
open(); |
166 |
} |
167 |
}); |
168 |
} |
169 |
{ |
170 |
jButtonSave = new JButton(); |
171 |
jPanelOpenSave.add(jButtonSave, "1, 1");
|
172 |
jButtonSave.setText(Sextante.getText("Save"));
|
173 |
jButtonSave.addActionListener(new ActionListener() { |
174 |
public void actionPerformed(final ActionEvent evt) { |
175 |
save(); |
176 |
} |
177 |
}); |
178 |
} |
179 |
{ |
180 |
jSeparator = new JSeparator(); |
181 |
jPanelMain.add(jSeparator, "1, 4, 4, 4");
|
182 |
jSeparator.setPreferredSize(new java.awt.Dimension(565, 20)); |
183 |
} |
184 |
{ |
185 |
jButtonOK = new JButton(); |
186 |
jPanelMain.add(jButtonOK, "3, 5");
|
187 |
jButtonOK.setText(Sextante.getText("OK"));
|
188 |
jButtonOK.addActionListener(new ActionListener() { |
189 |
public void actionPerformed(final ActionEvent evt) { |
190 |
btnOKActionPerformed(); |
191 |
} |
192 |
}); |
193 |
} |
194 |
{ |
195 |
jButtonCancel = new JButton(); |
196 |
jPanelMain.add(jButtonCancel, "4, 5");
|
197 |
jButtonCancel.setText(Sextante.getText("Cancel"));
|
198 |
jButtonCancel.addActionListener(new ActionListener() { |
199 |
public void actionPerformed(final ActionEvent evt) { |
200 |
btnCancelActionPerformed(); |
201 |
} |
202 |
}); |
203 |
} |
204 |
} |
205 |
} |
206 |
this.setSize(580, 350); |
207 |
} |
208 |
catch (final Exception e) { |
209 |
Sextante.addErrorToLog(e); |
210 |
} |
211 |
} |
212 |
|
213 |
|
214 |
private void save() { |
215 |
|
216 |
final JFileChooser fc = new JFileChooser(); |
217 |
final GenericFileFilter filter = new GenericFileFilter("csv", "Comma-Separated Values"); |
218 |
|
219 |
fc.setFileFilter(filter); |
220 |
final int returnVal = fc.showSaveDialog(this); |
221 |
|
222 |
if (returnVal == JFileChooser.APPROVE_OPTION) { |
223 |
File file = fc.getSelectedFile();
|
224 |
if(!file.getAbsolutePath().endsWith(".csv")) { |
225 |
file = new File(file.getAbsoluteFile() + ".csv"); |
226 |
} |
227 |
Writer output = null; |
228 |
try {
|
229 |
output = new BufferedWriter(new FileWriter(file)); |
230 |
final int iRows = m_TableModel.getRowCount(); |
231 |
final int iCols = m_TableModel.getColumnCount(); |
232 |
for (int iRow = 0; iRow < iRows; iRow++) { |
233 |
for (int iCol = 0; iCol < iCols; iCol++) { |
234 |
final String s = m_TableModel.getValueAt(iRow, iCol).toString(); |
235 |
output.write(s); |
236 |
if (iCol < (iCols - 1)) { |
237 |
output.write("|");
|
238 |
} |
239 |
} |
240 |
output.write("\n");
|
241 |
} |
242 |
} |
243 |
catch (final IOException e) { |
244 |
Sextante.addErrorToLog(e); |
245 |
} |
246 |
finally {
|
247 |
if (output != null) { |
248 |
try {
|
249 |
output.close(); |
250 |
} |
251 |
catch (final IOException e) { |
252 |
Sextante.addErrorToLog(e); |
253 |
} |
254 |
} |
255 |
} |
256 |
} |
257 |
|
258 |
} |
259 |
|
260 |
|
261 |
private void open() { |
262 |
|
263 |
try {
|
264 |
final JFileChooser fc = new JFileChooser(); |
265 |
final GenericFileFilter filter = new GenericFileFilter("csv", "Comma-Separated Values"); |
266 |
|
267 |
fc.setFileFilter(filter); |
268 |
final int returnVal = fc.showOpenDialog(this); |
269 |
|
270 |
if (returnVal == JFileChooser.APPROVE_OPTION) { |
271 |
final File file = fc.getSelectedFile(); |
272 |
BufferedReader input = null; |
273 |
|
274 |
final int iRows = m_TableModel.getRowCount(); |
275 |
|
276 |
input = new BufferedReader(new FileReader(file)); |
277 |
String sLine = null; |
278 |
int i = 1; |
279 |
while ((sLine = input.readLine()) != null) { |
280 |
if ((i > iRows) && !m_TableModel.isNumberOfRowsFixed()) {
|
281 |
m_TableModel.addRow(); |
282 |
} |
283 |
processLine(sLine, m_TableModel, i - 1);
|
284 |
i++; |
285 |
} |
286 |
input.close(); |
287 |
} |
288 |
} |
289 |
catch (final Exception e) { |
290 |
JOptionPane.showMessageDialog(this, Sextante.getText("Could_not_open_selected_file"), Sextante.getText("Error"), |
291 |
JOptionPane.ERROR_MESSAGE);
|
292 |
} |
293 |
|
294 |
|
295 |
} |
296 |
|
297 |
|
298 |
private void processLine(final String line, |
299 |
final FixedTableModel table,
|
300 |
final int iRow) { |
301 |
|
302 |
try {
|
303 |
final String[] tokens = line.split("\\|"); |
304 |
for (int i = 0; i < tokens.length; i++) { |
305 |
table.setValueAt(tokens[i], iRow, i); |
306 |
} |
307 |
} |
308 |
catch (final Exception e) {} |
309 |
|
310 |
} |
311 |
|
312 |
|
313 |
private void btnOKActionPerformed() { |
314 |
|
315 |
if (checkData()) {
|
316 |
m_TableModelOrg.setAttributes(m_TableModel.getColumnNames(), m_TableModel.getData(), m_TableModel.isNumberOfRowsFixed()); |
317 |
|
318 |
m_bAccepted = true;
|
319 |
dispose(); |
320 |
setVisible(false);
|
321 |
} |
322 |
else {
|
323 |
JOptionPane.showMessageDialog(this, Sextante.getText("Wrong_values_in_table"), Sextante.getText("Error"), |
324 |
JOptionPane.ERROR_MESSAGE);
|
325 |
} |
326 |
|
327 |
} |
328 |
|
329 |
|
330 |
private boolean checkData() { |
331 |
|
332 |
try {
|
333 |
for (int i = 0; i < m_TableModel.getData().length; i++) { |
334 |
final ArrayList list = m_TableModel.getData()[i]; |
335 |
for (int j = 0; j < list.size(); j++) { |
336 |
if (list.get(j).toString().isEmpty()) { //just check that every cell contains something |
337 |
return false; |
338 |
} |
339 |
} |
340 |
} |
341 |
return true; |
342 |
} |
343 |
catch (final Exception e) { |
344 |
return false; |
345 |
} |
346 |
} |
347 |
|
348 |
|
349 |
private void btnCancelActionPerformed() { |
350 |
|
351 |
m_bAccepted = false;
|
352 |
dispose(); |
353 |
setVisible(false);
|
354 |
|
355 |
|
356 |
} |
357 |
|
358 |
|
359 |
private void addRow() { |
360 |
|
361 |
m_TableModel.addRow(); |
362 |
|
363 |
} |
364 |
|
365 |
|
366 |
private void removeRow() { |
367 |
|
368 |
int iRow;
|
369 |
|
370 |
iRow = jTable.getSelectedRow(); |
371 |
if (iRow != -1) { |
372 |
m_TableModel.removeRow(iRow); |
373 |
} |
374 |
|
375 |
} |
376 |
|
377 |
|
378 |
/**
|
379 |
* Returns true if the user accepted the introduced data.
|
380 |
*
|
381 |
* @return true if the user accepted the introduced data.
|
382 |
*/
|
383 |
public boolean accepted() { |
384 |
|
385 |
return m_bAccepted;
|
386 |
|
387 |
} |
388 |
} |