Lines 39-121
Link Here
|
39 |
* |
39 |
* |
40 |
* Portions Copyrighted 2009-2010 Sun Microsystems, Inc. |
40 |
* Portions Copyrighted 2009-2010 Sun Microsystems, Inc. |
41 |
*/ |
41 |
*/ |
42 |
package org.netbeans.modules.db.dataview.table; |
42 |
package org.netbeans.modules.db.dataview.table.celleditor; |
43 |
|
43 |
|
44 |
import java.awt.BorderLayout; |
|
|
45 |
import java.awt.Component; |
44 |
import java.awt.Component; |
46 |
import java.awt.Dimension; |
|
|
47 |
import java.awt.Font; |
48 |
import java.awt.Insets; |
49 |
import java.awt.event.ActionEvent; |
50 |
import java.awt.event.ActionListener; |
51 |
import java.awt.event.KeyEvent; |
52 |
import java.awt.event.KeyListener; |
53 |
import java.awt.event.MouseEvent; |
45 |
import java.awt.event.MouseEvent; |
54 |
import java.io.File; |
|
|
55 |
import java.io.FileInputStream; |
56 |
import java.io.FileOutputStream; |
57 |
import java.io.IOException; |
58 |
import java.io.InputStream; |
59 |
import java.io.InputStreamReader; |
60 |
import java.io.OutputStreamWriter; |
61 |
import java.io.Reader; |
62 |
import java.io.Writer; |
63 |
import java.nio.charset.Charset; |
64 |
import java.sql.Blob; |
65 |
import java.sql.Clob; |
66 |
import java.sql.SQLException; |
67 |
import java.sql.Timestamp; |
68 |
import java.text.DateFormat; |
69 |
import java.text.ParseException; |
70 |
import java.text.SimpleDateFormat; |
71 |
import java.util.ArrayList; |
72 |
import java.util.Collections; |
73 |
import java.util.Comparator; |
74 |
import java.util.EventObject; |
46 |
import java.util.EventObject; |
75 |
import java.util.List; |
|
|
76 |
import javax.swing.AbstractCellEditor; |
77 |
import javax.swing.Action; |
78 |
import javax.swing.ActionMap; |
79 |
import javax.swing.BorderFactory; |
80 |
import javax.swing.DefaultCellEditor; |
47 |
import javax.swing.DefaultCellEditor; |
81 |
import javax.swing.DefaultComboBoxModel; |
|
|
82 |
import javax.swing.InputMap; |
83 |
import javax.swing.JButton; |
84 |
import javax.swing.JComboBox; |
85 |
import javax.swing.JComponent; |
48 |
import javax.swing.JComponent; |
86 |
import javax.swing.JFileChooser; |
|
|
87 |
import javax.swing.JMenuItem; |
88 |
import javax.swing.JOptionPane; |
89 |
import javax.swing.JPanel; |
90 |
import javax.swing.JPopupMenu; |
91 |
import javax.swing.JScrollPane; |
92 |
import javax.swing.JTable; |
49 |
import javax.swing.JTable; |
93 |
import javax.swing.JTextArea; |
|
|
94 |
import javax.swing.JTextField; |
50 |
import javax.swing.JTextField; |
95 |
import javax.swing.KeyStroke; |
|
|
96 |
import javax.swing.SwingConstants; |
97 |
import javax.swing.SwingUtilities; |
98 |
import javax.swing.UIManager; |
51 |
import javax.swing.UIManager; |
99 |
import javax.swing.table.TableCellEditor; |
|
|
100 |
import org.jdesktop.swingx.JXButton; |
101 |
import org.jdesktop.swingx.JXDatePicker; |
102 |
import org.jdesktop.swingx.JXPanel; |
103 |
import org.jdesktop.swingx.renderer.JRendererCheckBox; |
52 |
import org.jdesktop.swingx.renderer.JRendererCheckBox; |
104 |
import org.netbeans.modules.db.dataview.meta.DBColumn; |
53 |
import org.netbeans.modules.db.dataview.meta.DBColumn; |
105 |
import org.netbeans.modules.db.dataview.util.FileBackedBlob; |
54 |
import org.netbeans.modules.db.dataview.table.ResultSetJXTable; |
106 |
import org.netbeans.modules.db.dataview.util.FileBackedClob; |
|
|
107 |
import org.netbeans.modules.db.dataview.util.DBReadWriteHelper; |
55 |
import org.netbeans.modules.db.dataview.util.DBReadWriteHelper; |
108 |
import org.netbeans.modules.db.dataview.util.DataViewUtils; |
56 |
import org.netbeans.modules.db.dataview.util.DataViewUtils; |
109 |
import org.netbeans.modules.db.dataview.util.JXDateTimePicker; |
|
|
110 |
import org.netbeans.modules.db.dataview.util.TimestampType; |
111 |
import org.openide.awt.StatusDisplayer; |
57 |
import org.openide.awt.StatusDisplayer; |
112 |
import org.openide.util.Exceptions; |
|
|
113 |
import org.openide.util.NbBundle; |
114 |
import org.openide.windows.WindowManager; |
115 |
|
58 |
|
116 |
/** |
|
|
117 |
* @author Ahimanikya Satapathy |
118 |
*/ |
119 |
public class ResultSetTableCellEditor extends DefaultCellEditor { |
59 |
public class ResultSetTableCellEditor extends DefaultCellEditor { |
120 |
|
60 |
|
121 |
protected Object val; |
61 |
protected Object val; |
Lines 203-215
Link Here
|
203 |
checkBox.addActionListener(delegate); |
143 |
checkBox.addActionListener(delegate); |
204 |
} |
144 |
} |
205 |
|
145 |
|
206 |
protected void setEditable(int column, Component c) { |
146 |
protected void setEditable(int column, Component c, boolean celleditable) { |
207 |
assert table != null; |
147 |
assert table != null; |
208 |
DBColumn dbCol = ((ResultSetJXTable) table).getDBColumn(column); |
148 |
DBColumn dbCol = ((ResultSetJXTable) table).getDBColumn(column); |
209 |
if (dbCol.isGenerated()) { |
149 |
if (dbCol.isGenerated()) { |
210 |
editable = false; |
150 |
editable = false; |
211 |
} |
151 |
} |
212 |
if (!((ResultSetJXTable) table).dView.isEditable()) { |
152 |
if (! celleditable) { |
213 |
editable = false; |
153 |
editable = false; |
214 |
} else { |
154 |
} else { |
215 |
editable = dbCol.isEditable(); |
155 |
editable = dbCol.isEditable(); |
Lines 221-876
Link Here
|
221 |
((JComponent) c).setEnabled(editable); |
161 |
((JComponent) c).setEnabled(editable); |
222 |
} |
162 |
} |
223 |
} |
163 |
} |
224 |
} |
|
|
225 |
|
226 |
class BooleanTableCellEditor extends ResultSetTableCellEditor implements TableCellEditor { |
227 |
|
228 |
public BooleanTableCellEditor(JRendererCheckBox cb) { |
229 |
super(cb); |
230 |
cb.setHorizontalAlignment(0); |
231 |
} |
232 |
|
233 |
@Override |
234 |
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { |
235 |
this.table = table; |
236 |
Component c = super.getTableCellEditorComponent(table, value, isSelected, row, column); |
237 |
setEditable(column, c); |
238 |
if (isGtk && c instanceof JComponent) { |
239 |
((JComponent) c).setBorder(BorderFactory.createEmptyBorder()); |
240 |
} |
241 |
return c; |
242 |
} |
243 |
} |
244 |
|
245 |
class StringTableCellEditor extends ResultSetTableCellEditor implements TableCellEditor, ActionListener { |
246 |
|
247 |
private JXButton customEditorButton = new JXButton("..."); |
248 |
private int row, column; |
249 |
|
250 |
public StringTableCellEditor(final JTextField textField) { |
251 |
super(textField); |
252 |
customEditorButton.addActionListener(this); |
253 |
|
254 |
// ui-tweaking |
255 |
customEditorButton.setFocusable(false); |
256 |
customEditorButton.setFocusPainted(false); |
257 |
customEditorButton.setMargin(new Insets(0, 0, 0, 0)); |
258 |
customEditorButton.setPreferredSize(new Dimension(20, 10)); |
259 |
} |
260 |
|
261 |
@Override |
262 |
public Component getTableCellEditorComponent(final JTable table, Object value, boolean isSelected, int row, int column) { |
263 |
this.table = table; |
264 |
final JComponent c = (JComponent) super.getTableCellEditorComponent(table, value, isSelected, row, column); |
265 |
setEditable(column, c); |
266 |
|
267 |
JXPanel panel = new JXPanel(new BorderLayout()) { |
268 |
|
269 |
@Override |
270 |
public void addNotify() { |
271 |
super.addNotify(); |
272 |
c.requestFocus(); |
273 |
} |
274 |
|
275 |
@Override |
276 |
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) { |
277 |
InputMap map = c.getInputMap(condition); |
278 |
ActionMap am = c.getActionMap(); |
279 |
|
280 |
if (map != null && am != null && isEnabled()) { |
281 |
Object binding = map.get(ks); |
282 |
Action action = (binding == null) ? null : am.get(binding); |
283 |
if (action != null) { |
284 |
return SwingUtilities.notifyAction(action, ks, e, c, |
285 |
e.getModifiers()); |
286 |
} |
287 |
} |
288 |
return false; |
289 |
} |
290 |
}; |
291 |
panel.add(c); |
292 |
if (isGtk) { |
293 |
c.setBorder(BorderFactory.createEmptyBorder()); |
294 |
} |
295 |
panel.add(customEditorButton, BorderLayout.EAST); |
296 |
panel.revalidate(); |
297 |
panel.repaint(); |
298 |
|
299 |
this.row = row; |
300 |
this.column = column; |
301 |
return panel; |
302 |
} |
303 |
|
304 |
@Override |
305 |
public final void actionPerformed(ActionEvent e) { |
306 |
assert table != null; |
307 |
super.cancelCellEditing(); |
308 |
editCell(table, row, column); |
309 |
} |
310 |
|
311 |
protected void editCell(JTable table, int row, int column) { |
312 |
JTextArea textArea = new JTextArea(10, 50); |
313 |
Object value = table.getValueAt(row, column); |
314 |
if (value != null) { |
315 |
textArea.setText(value.toString()); |
316 |
textArea.setCaretPosition(0); |
317 |
textArea.setEditable(editable); |
318 |
} |
319 |
JScrollPane pane = new JScrollPane(textArea); |
320 |
Component parent = WindowManager.getDefault().getMainWindow(); |
321 |
|
322 |
if (editable) { |
323 |
int result = JOptionPane.showOptionDialog(parent, pane, table.getColumnName(column), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null); |
324 |
if (result == JOptionPane.OK_OPTION) { |
325 |
table.setValueAt(textArea.getText(), row, column); |
326 |
} |
327 |
} else { |
328 |
JOptionPane.showMessageDialog(parent, pane, table.getColumnName(column), JOptionPane.PLAIN_MESSAGE, null); |
329 |
} |
330 |
} |
331 |
} |
332 |
|
333 |
class DateTimePickerCellEditor extends AbstractCellEditor implements TableCellEditor { |
334 |
|
335 |
private boolean editable = true; |
336 |
private JXDateTimePicker datePicker; |
337 |
private DateFormat dateFormat; |
338 |
private ActionListener pickerActionListener; |
339 |
private boolean ignoreAction; |
340 |
private JTable table; |
341 |
|
342 |
public DateTimePickerCellEditor() { |
343 |
this(new SimpleDateFormat (TimestampType.DEFAULT_FORMAT_PATTERN)); |
344 |
} |
345 |
|
346 |
/** |
347 |
* Instantiates an editor with the given dateFormat. If |
348 |
* null, the datePickers default is used. |
349 |
* |
350 |
* @param dateFormat |
351 |
*/ |
352 |
public DateTimePickerCellEditor(DateFormat dateFormat) { |
353 |
|
354 |
// JW: the copy is used to synchronize .. can |
355 |
// we use something else? |
356 |
this.dateFormat = dateFormat != null ? dateFormat : new SimpleDateFormat (TimestampType.DEFAULT_FORMAT_PATTERN); |
357 |
datePicker = new JXDateTimePicker(); |
358 |
// default border crushes the editor/combo |
359 |
datePicker.getEditor().setBorder( |
360 |
BorderFactory.createEmptyBorder(0, 1, 0, 1)); |
361 |
// should be fixed by j2se 6.0 |
362 |
datePicker.setFont(UIManager.getDefaults().getFont("TextField.font")); |
363 |
if (dateFormat != null) { |
364 |
datePicker.setFormats(dateFormat); |
365 |
} |
366 |
datePicker.addActionListener(getPickerActionListener()); |
367 |
} |
368 |
|
369 |
@Override |
370 |
public Timestamp getCellEditorValue() { |
371 |
return datePicker.getDateTime(); |
372 |
} |
373 |
|
374 |
@Override |
375 |
public boolean isCellEditable(EventObject anEvent) { |
376 |
if (anEvent instanceof MouseEvent) { |
377 |
return ((MouseEvent) anEvent).getClickCount() >= 2; |
378 |
} |
379 |
return super.isCellEditable(anEvent); |
380 |
} |
381 |
|
382 |
@Override |
383 |
public boolean stopCellEditing() { |
384 |
ignoreAction = true; |
385 |
boolean canCommit = commitChange(); |
386 |
ignoreAction = false; |
387 |
if (canCommit) { |
388 |
datePicker.setDateTime(null); |
389 |
return super.stopCellEditing(); |
390 |
} |
391 |
return false; |
392 |
} |
393 |
|
394 |
@Override |
395 |
public Component getTableCellEditorComponent(final JTable table, Object value, |
396 |
boolean isSelected, int row, int column) { |
397 |
this.table = table; |
398 |
ignoreAction = true; |
399 |
datePicker.setDateTime(getValueAsTimestamp(value)); |
400 |
|
401 |
ignoreAction = false; |
402 |
setEditable(column, datePicker); |
403 |
return datePicker; |
404 |
} |
405 |
|
406 |
protected Timestamp getValueAsTimestamp(Object value) { |
407 |
if (isEmpty(value) || DataViewUtils.isSQLConstantString(value)) { |
408 |
return new Timestamp(System.currentTimeMillis()); |
409 |
} |
410 |
|
411 |
if (value instanceof Timestamp) { |
412 |
return (Timestamp) value; |
413 |
} |
414 |
if (value instanceof Long) { |
415 |
return new Timestamp((Long) value); |
416 |
} |
417 |
if (value instanceof String) { |
418 |
try { |
419 |
|
420 |
return new Timestamp(dateFormat.parse((String) value).getTime()); |
421 |
} catch (ParseException e) { |
422 |
//mLogger.log(Level.SEVERE, e.getMessage(), e.getMessage()); |
423 |
} |
424 |
} |
425 |
|
426 |
return new Timestamp(System.currentTimeMillis()); |
427 |
} |
428 |
|
429 |
protected boolean isEmpty(Object value) { |
430 |
return value == null || value instanceof String && ((String) value).length() == 0; |
431 |
} |
432 |
|
433 |
protected boolean commitChange() { |
434 |
try { |
435 |
datePicker.commitEdit(); |
436 |
return true; |
437 |
} catch (ParseException e) { |
438 |
} |
439 |
return false; |
440 |
} |
441 |
|
442 |
public DateFormat[] getFormats() { |
443 |
return datePicker.getFormats(); |
444 |
} |
445 |
|
446 |
public void setFormats(DateFormat... formats) { |
447 |
datePicker.setFormats(formats); |
448 |
} |
449 |
|
450 |
private ActionListener getPickerActionListener() { |
451 |
if (pickerActionListener == null) { |
452 |
pickerActionListener = createPickerActionListener(); |
453 |
} |
454 |
return pickerActionListener; |
455 |
} |
456 |
|
457 |
protected ActionListener createPickerActionListener() { |
458 |
ActionListener l = new ActionListener() { |
459 |
|
460 |
@Override |
461 |
public void actionPerformed(final ActionEvent e) { |
462 |
// avoid duplicate trigger from |
463 |
// commit in stopCellEditing |
464 |
if (ignoreAction) { |
465 |
return; |
466 |
} |
467 |
terminateEdit(e); |
468 |
} |
469 |
|
470 |
private void terminateEdit(final ActionEvent e) { |
471 |
if ((e != null) && (JXDatePicker.COMMIT_KEY.equals(e.getActionCommand()))) { |
472 |
stopCellEditing(); |
473 |
} else { |
474 |
cancelCellEditing(); |
475 |
} |
476 |
} |
477 |
}; |
478 |
return l; |
479 |
} |
480 |
|
481 |
protected void setEditable(int column, JXDateTimePicker c) { |
482 |
assert table != null; |
483 |
DBColumn dbCol = ((ResultSetJXTable) table).getDBColumn(column); |
484 |
if (dbCol.isGenerated()) { |
485 |
editable = false; |
486 |
} else if (!((ResultSetJXTable) table).dView.isEditable()) { |
487 |
editable = false; |
488 |
} else { |
489 |
editable = dbCol.isEditable(); |
490 |
} |
491 |
c.setEditable(editable); |
492 |
} |
493 |
|
494 |
protected void addKeyListener(KeyListener kl) { |
495 |
datePicker.addKeyListener(kl); |
496 |
} |
497 |
} |
498 |
|
499 |
class NumberFieldEditor extends ResultSetTableCellEditor { |
500 |
|
501 |
public NumberFieldEditor(final JTextField textField) { |
502 |
super(textField); |
503 |
((JTextField) getComponent()).setHorizontalAlignment(JTextField.RIGHT); |
504 |
} |
505 |
|
506 |
@Override |
507 |
public Component getTableCellEditorComponent(final JTable table, Object value, boolean isSelected, int row, int column) { |
508 |
this.table = table; |
509 |
Component c = super.getTableCellEditorComponent(table, value, isSelected, row, column); |
510 |
if (isGtk && c instanceof JComponent) { |
511 |
((JComponent) c).setBorder(BorderFactory.createEmptyBorder()); |
512 |
} |
513 |
setEditable(column, c); |
514 |
return c; |
515 |
} |
516 |
} |
517 |
|
518 |
class BlobFieldTableCellEditor extends AbstractCellEditor |
519 |
implements TableCellEditor, |
520 |
ActionListener { |
521 |
|
522 |
protected static final String EDIT = "edit"; |
523 |
protected Blob currentValue; |
524 |
protected JButton button; |
525 |
protected JPopupMenu popup; |
526 |
protected JTable table; |
527 |
|
528 |
public BlobFieldTableCellEditor() { |
529 |
button = new JButton(); |
530 |
button.setActionCommand(EDIT); |
531 |
button.addActionListener(this); |
532 |
button.setContentAreaFilled(false); |
533 |
button.setOpaque(false); |
534 |
button.setBorderPainted(false); |
535 |
button.setRolloverEnabled(false); |
536 |
button.setAlignmentX(0); |
537 |
button.setHorizontalAlignment(SwingConstants.LEFT); |
538 |
button.setFont(new Font(button.getFont().getFamily(), Font.ITALIC, 9)); |
539 |
|
540 |
popup = new JPopupMenu(); |
541 |
final JMenuItem miLobSaveAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "saveLob.title")); |
542 |
miLobSaveAction.addActionListener(new ActionListener() { |
543 |
|
544 |
@Override |
545 |
public void actionPerformed(ActionEvent e) { |
546 |
saveLobToFile(currentValue); |
547 |
fireEditingCanceled(); |
548 |
} |
549 |
}); |
550 |
popup.add(miLobSaveAction); |
551 |
final JMenuItem miLobLoadAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "loadLob.title")); |
552 |
miLobLoadAction.addActionListener(new ActionListener() { |
553 |
|
554 |
@Override |
555 |
public void actionPerformed(ActionEvent e) { |
556 |
Object newValue = loadLobFromFile(); |
557 |
if (newValue != null) { |
558 |
currentValue = (Blob) newValue; |
559 |
} |
560 |
fireEditingStopped(); |
561 |
} |
562 |
}); |
563 |
popup.add(miLobLoadAction); |
564 |
final JMenuItem miLobNullAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "nullLob.title")); |
565 |
miLobNullAction.addActionListener(new ActionListener() { |
566 |
|
567 |
@Override |
568 |
public void actionPerformed(ActionEvent e) { |
569 |
currentValue = null; |
570 |
fireEditingStopped(); |
571 |
} |
572 |
}); |
573 |
popup.add(miLobNullAction); |
574 |
|
575 |
} |
576 |
|
577 |
public void actionPerformed(ActionEvent e) { |
578 |
if (EDIT.equals(e.getActionCommand())) { |
579 |
popup.show(button, 0, button.getHeight()); |
580 |
} |
581 |
} |
582 |
|
583 |
@Override |
584 |
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { |
585 |
currentValue = (java.sql.Blob) value; |
586 |
if (currentValue != null) { |
587 |
try { |
588 |
long size = currentValue.length(); |
589 |
StringBuilder stringValue = new StringBuilder(); |
590 |
stringValue.append("<BLOB "); |
591 |
if (size < 1000) { |
592 |
stringValue.append(String.format("%1$d bytes", size)); |
593 |
} else if (size < 1000000) { |
594 |
stringValue.append(String.format("%1$d kB", size / 1000)); |
595 |
} else { |
596 |
stringValue.append(String.format("%1$d MB", size / 1000000)); |
597 |
} |
598 |
stringValue.append(">"); |
599 |
button.setText(stringValue.toString()); |
600 |
} catch (SQLException ex) { |
601 |
button.setText("<BLOB of unknown size>"); |
602 |
} |
603 |
} else { |
604 |
button.setText("<NULL>"); |
605 |
} |
606 |
this.table = table; |
607 |
return button; |
608 |
} |
609 |
|
610 |
@Override |
611 |
public Object getCellEditorValue() { |
612 |
return currentValue; |
613 |
} |
614 |
|
615 |
@Override |
616 |
public boolean isCellEditable(EventObject anEvent) { |
617 |
if (anEvent instanceof MouseEvent) { |
618 |
return ((MouseEvent) anEvent).getClickCount() >= 2; |
619 |
} |
620 |
return super.isCellEditable(anEvent); |
621 |
} |
622 |
|
623 |
private void saveLobToFile(Blob b) { |
624 |
JFileChooser c = new JFileChooser(); |
625 |
int fileDialogState = c.showSaveDialog(table); |
626 |
if (fileDialogState == JFileChooser.APPROVE_OPTION) { |
627 |
File f = c.getSelectedFile(); |
628 |
InputStream is = null; |
629 |
FileOutputStream fos = null; |
630 |
try { |
631 |
is = b.getBinaryStream(); |
632 |
fos = new FileOutputStream(f); |
633 |
int read = 0; |
634 |
byte[] buffer = new byte[1024]; |
635 |
while((read = is.read(buffer)) > 0) { |
636 |
fos.write(buffer, 0, read); |
637 |
} |
638 |
} catch (IOException ex) { |
639 |
throw new RuntimeException(ex); |
640 |
} catch (SQLException ex) { |
641 |
throw new RuntimeException(ex); |
642 |
} finally { |
643 |
try { |
644 |
if(fos != null) fos.close(); |
645 |
} catch (IOException ex) { |
646 |
Exceptions.printStackTrace(ex); |
647 |
} |
648 |
try { |
649 |
if(is != null) is.close(); |
650 |
} catch (IOException ex) { |
651 |
Exceptions.printStackTrace(ex); |
652 |
} |
653 |
} |
654 |
} |
655 |
} |
656 |
|
657 |
private Blob loadLobFromFile() { |
658 |
JFileChooser c = new JFileChooser(); |
659 |
Blob result = null; |
660 |
int fileDialogState = c.showOpenDialog(table); |
661 |
if (fileDialogState == JFileChooser.APPROVE_OPTION) { |
662 |
File f = c.getSelectedFile(); |
663 |
FileInputStream fis = null; |
664 |
try { |
665 |
fis = new FileInputStream(f); |
666 |
result = new FileBackedBlob(fis); |
667 |
} catch (IOException ex) { |
668 |
throw new RuntimeException(ex); |
669 |
} catch (SQLException ex) { |
670 |
throw new RuntimeException(ex); |
671 |
} finally { |
672 |
try { |
673 |
if(fis != null) fis.close(); |
674 |
} catch (IOException ex) { |
675 |
Exceptions.printStackTrace(ex); |
676 |
} |
677 |
} |
678 |
} |
679 |
return result; |
680 |
} |
681 |
} |
682 |
|
683 |
class ClobFieldTableCellEditor extends AbstractCellEditor |
684 |
implements TableCellEditor, |
685 |
ActionListener { |
686 |
|
687 |
private class CharsetSelector extends JPanel { |
688 |
private JComboBox charsetSelect; |
689 |
|
690 |
CharsetSelector() { |
691 |
List<Charset> charset = new ArrayList<Charset>(Charset.availableCharsets().values()); |
692 |
Collections.sort(charset, new Comparator<Charset>() { |
693 |
@Override |
694 |
public int compare(Charset o1, Charset o2) { |
695 |
return o1.displayName().compareTo(o2.displayName()); |
696 |
} |
697 |
}); |
698 |
charsetSelect = new JComboBox(); |
699 |
charsetSelect.setModel(new DefaultComboBoxModel(charset.toArray())); |
700 |
charsetSelect.setSelectedItem(Charset.defaultCharset()); |
701 |
this.add(charsetSelect); |
702 |
} |
703 |
|
704 |
public Charset getSelectedCharset() { |
705 |
return (Charset) charsetSelect.getSelectedItem(); |
706 |
} |
707 |
|
708 |
public void setSelectedCharset(Charset selectedCharset) { |
709 |
charsetSelect.setSelectedItem(selectedCharset); |
710 |
} |
711 |
} |
712 |
|
713 |
protected static final String EDIT = "edit"; |
714 |
protected Clob currentValue; |
715 |
protected JButton button; |
716 |
protected JPopupMenu popup; |
717 |
protected JTable table; |
718 |
|
719 |
public ClobFieldTableCellEditor() { |
720 |
button = new JButton(); |
721 |
button.setActionCommand(EDIT); |
722 |
button.addActionListener(this); |
723 |
button.setContentAreaFilled(false); |
724 |
button.setOpaque(false); |
725 |
button.setBorderPainted(false); |
726 |
button.setRolloverEnabled(false); |
727 |
button.setAlignmentX(0); |
728 |
button.setHorizontalAlignment(SwingConstants.LEFT); |
729 |
button.setFont(new Font(button.getFont().getFamily(), Font.ITALIC, 9)); |
730 |
|
731 |
popup = new JPopupMenu(); |
732 |
final JMenuItem miLobSaveAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "saveLob.title")); |
733 |
miLobSaveAction.addActionListener(new ActionListener() { |
734 |
|
735 |
@Override |
736 |
public void actionPerformed(ActionEvent e) { |
737 |
saveLobToFile(currentValue); |
738 |
fireEditingCanceled(); |
739 |
} |
740 |
}); |
741 |
popup.add(miLobSaveAction); |
742 |
final JMenuItem miLobLoadAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "loadLob.title")); |
743 |
miLobLoadAction.addActionListener(new ActionListener() { |
744 |
|
745 |
@Override |
746 |
public void actionPerformed(ActionEvent e) { |
747 |
Object newValue = loadLobFromFile(); |
748 |
if (newValue != null) { |
749 |
currentValue = (Clob) newValue; |
750 |
} |
751 |
fireEditingStopped(); |
752 |
} |
753 |
}); |
754 |
popup.add(miLobLoadAction); |
755 |
final JMenuItem miLobNullAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "nullLob.title")); |
756 |
miLobNullAction.addActionListener(new ActionListener() { |
757 |
|
758 |
@Override |
759 |
public void actionPerformed(ActionEvent e) { |
760 |
currentValue = null; |
761 |
fireEditingStopped(); |
762 |
} |
763 |
}); |
764 |
popup.add(miLobNullAction); |
765 |
|
766 |
} |
767 |
|
768 |
public void actionPerformed(ActionEvent e) { |
769 |
if (EDIT.equals(e.getActionCommand())) { |
770 |
popup.show(button, 0, button.getHeight()); |
771 |
} |
772 |
} |
773 |
|
774 |
@Override |
775 |
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { |
776 |
currentValue = (java.sql.Clob) value; |
777 |
if (currentValue != null) { |
778 |
try { |
779 |
long size = currentValue.length(); |
780 |
StringBuilder stringValue = new StringBuilder(); |
781 |
stringValue.append("<CLOB "); |
782 |
if (size < 1000) { |
783 |
stringValue.append(String.format("%1$d Chars", size)); |
784 |
} else if (size < 1000000) { |
785 |
stringValue.append(String.format("%1$d kChars", size / 1000)); |
786 |
} else { |
787 |
stringValue.append(String.format("%1$d MChars", size / 1000000)); |
788 |
} |
789 |
stringValue.append(">"); |
790 |
button.setText(stringValue.toString()); |
791 |
} catch (SQLException ex) { |
792 |
button.setText("<CLOB of unknown size>"); |
793 |
} |
794 |
} else { |
795 |
button.setText("<NULL>"); |
796 |
} |
797 |
this.table = table; |
798 |
return button; |
799 |
} |
800 |
|
801 |
@Override |
802 |
public Object getCellEditorValue() { |
803 |
return currentValue; |
804 |
} |
805 |
|
806 |
@Override |
807 |
public boolean isCellEditable(EventObject anEvent) { |
808 |
if (anEvent instanceof MouseEvent) { |
809 |
return ((MouseEvent) anEvent).getClickCount() >= 2; |
810 |
} |
811 |
return super.isCellEditable(anEvent); |
812 |
} |
813 |
|
814 |
private void saveLobToFile(Clob b) { |
815 |
CharsetSelector charset = new CharsetSelector(); |
816 |
JFileChooser c = new JFileChooser(); |
817 |
c.setAccessory(charset); |
818 |
int fileDialogState = c.showSaveDialog(table); |
819 |
if (fileDialogState == JFileChooser.APPROVE_OPTION) { |
820 |
File f = c.getSelectedFile(); |
821 |
Reader r = null; |
822 |
Writer w = null; |
823 |
try { |
824 |
r = b.getCharacterStream(); |
825 |
w = new OutputStreamWriter(new FileOutputStream(f), charset.getSelectedCharset()); |
826 |
int read = 0; |
827 |
char[] buffer = new char[1024]; |
828 |
while((read = r.read(buffer)) > 0) { |
829 |
w.write(buffer, 0, read); |
830 |
} |
831 |
} catch (IOException ex) { |
832 |
throw new RuntimeException(ex); |
833 |
} catch (SQLException ex) { |
834 |
throw new RuntimeException(ex); |
835 |
} finally { |
836 |
try { |
837 |
if(w != null) w.close(); |
838 |
} catch (IOException ex) { |
839 |
Exceptions.printStackTrace(ex); |
840 |
} |
841 |
try { |
842 |
if(r != null) r.close(); |
843 |
} catch (IOException ex) { |
844 |
Exceptions.printStackTrace(ex); |
845 |
} |
846 |
} |
847 |
} |
848 |
} |
849 |
|
850 |
private Clob loadLobFromFile() { |
851 |
CharsetSelector charset = new CharsetSelector(); |
852 |
JFileChooser c = new JFileChooser(); |
853 |
c.setAccessory(charset); |
854 |
Clob result = null; |
855 |
int fileDialogState = c.showOpenDialog(table); |
856 |
if (fileDialogState == JFileChooser.APPROVE_OPTION) { |
857 |
File f = c.getSelectedFile(); |
858 |
Reader r = null; |
859 |
try { |
860 |
r = new InputStreamReader(new FileInputStream(f), charset.getSelectedCharset()); |
861 |
result = new FileBackedClob(r); |
862 |
} catch (IOException ex) { |
863 |
throw new RuntimeException(ex); |
864 |
} catch (SQLException ex) { |
865 |
throw new RuntimeException(ex); |
866 |
} finally { |
867 |
try { |
868 |
if(r != null) r.close(); |
869 |
} catch (IOException ex) { |
870 |
Exceptions.printStackTrace(ex); |
871 |
} |
872 |
} |
873 |
} |
874 |
return result; |
875 |
} |
876 |
} |
164 |
} |