Lines 50-63
Link Here
|
50 |
import java.sql.Blob; |
50 |
import java.sql.Blob; |
51 |
import java.sql.SQLException; |
51 |
import java.sql.SQLException; |
52 |
import java.util.EventObject; |
52 |
import java.util.EventObject; |
|
|
53 |
import java.util.Iterator; |
53 |
import java.util.logging.Level; |
54 |
import java.util.logging.Level; |
54 |
import java.util.logging.Logger; |
55 |
import java.util.logging.Logger; |
|
|
56 |
import javax.imageio.ImageIO; |
57 |
import javax.imageio.ImageReader; |
58 |
import javax.imageio.stream.ImageInputStream; |
55 |
import javax.swing.*; |
59 |
import javax.swing.*; |
56 |
import javax.swing.table.TableCellEditor; |
60 |
import javax.swing.table.TableCellEditor; |
57 |
import org.netbeans.api.progress.ProgressUtils; |
61 |
import org.netbeans.api.progress.ProgressUtils; |
58 |
import org.netbeans.modules.db.dataview.util.FileBackedBlob; |
62 |
import org.netbeans.modules.db.dataview.util.FileBackedBlob; |
59 |
import org.openide.DialogDisplayer; |
63 |
import org.openide.DialogDisplayer; |
60 |
import org.openide.NotifyDescriptor; |
64 |
import org.openide.NotifyDescriptor; |
|
|
65 |
import org.openide.cookies.OpenCookie; |
66 |
import org.openide.filesystems.FileObject; |
67 |
import org.openide.filesystems.FileSystem; |
68 |
import org.openide.filesystems.FileUtil; |
69 |
import org.openide.loaders.DataObject; |
70 |
import org.openide.util.Exceptions; |
61 |
import org.openide.util.NbBundle; |
71 |
import org.openide.util.NbBundle; |
62 |
|
72 |
|
63 |
public class BlobFieldTableCellEditor extends AbstractCellEditor |
73 |
public class BlobFieldTableCellEditor extends AbstractCellEditor |
Lines 72-77
Link Here
|
72 |
protected JPopupMenu popup; |
82 |
protected JPopupMenu popup; |
73 |
protected JTable table; |
83 |
protected JTable table; |
74 |
protected JMenuItem saveContentMenuItem; |
84 |
protected JMenuItem saveContentMenuItem; |
|
|
85 |
protected JMenuItem miOpenImageMenuItem; |
75 |
|
86 |
|
76 |
@SuppressWarnings("LeakingThisInConstructor") |
87 |
@SuppressWarnings("LeakingThisInConstructor") |
77 |
public BlobFieldTableCellEditor() { |
88 |
public BlobFieldTableCellEditor() { |
Lines 98-103
Link Here
|
98 |
}); |
109 |
}); |
99 |
saveContentMenuItem = miLobSaveAction; |
110 |
saveContentMenuItem = miLobSaveAction; |
100 |
popup.add(miLobSaveAction); |
111 |
popup.add(miLobSaveAction); |
|
|
112 |
|
113 |
final JMenuItem miOpenImageAction = new JMenuItem("Open as Image"); |
114 |
miOpenImageAction.addActionListener(new ActionListener() { |
115 |
|
116 |
@Override |
117 |
public void actionPerformed(ActionEvent e) { |
118 |
openAsImage(currentValue); |
119 |
fireEditingCanceled(); |
120 |
} |
121 |
}); |
122 |
miOpenImageMenuItem = miOpenImageAction; |
123 |
popup.add(miOpenImageAction); |
124 |
|
101 |
final JMenuItem miLobLoadAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "loadLob.title")); |
125 |
final JMenuItem miLobLoadAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "loadLob.title")); |
102 |
miLobLoadAction.addActionListener(new ActionListener() { |
126 |
miLobLoadAction.addActionListener(new ActionListener() { |
103 |
|
127 |
|
Lines 136-141
Link Here
|
136 |
currentValue = (java.sql.Blob) value; |
160 |
currentValue = (java.sql.Blob) value; |
137 |
if (currentValue != null) { |
161 |
if (currentValue != null) { |
138 |
saveContentMenuItem.setEnabled(true); |
162 |
saveContentMenuItem.setEnabled(true); |
|
|
163 |
miOpenImageMenuItem.setEnabled(true); |
139 |
try { |
164 |
try { |
140 |
long size = currentValue.length(); |
165 |
long size = currentValue.length(); |
141 |
StringBuilder stringValue = new StringBuilder(); |
166 |
StringBuilder stringValue = new StringBuilder(); |
Lines 154-159
Link Here
|
154 |
} |
179 |
} |
155 |
} else { |
180 |
} else { |
156 |
saveContentMenuItem.setEnabled(false); |
181 |
saveContentMenuItem.setEnabled(false); |
|
|
182 |
miOpenImageMenuItem.setEnabled(false); |
157 |
button.setText("<NULL>"); |
183 |
button.setText("<NULL>"); |
158 |
} |
184 |
} |
159 |
this.table = table; |
185 |
this.table = table; |
Lines 294-297
Link Here
|
294 |
|
320 |
|
295 |
dd.notifyLater(nd); |
321 |
dd.notifyLater(nd); |
296 |
} |
322 |
} |
|
|
323 |
|
324 |
private void openAsImage(Blob b) { |
325 |
if (b == null) { |
326 |
return; |
327 |
} |
328 |
try { |
329 |
ImageInputStream iis = ImageIO.createImageInputStream(b.getBinaryStream()); |
330 |
Iterator<ImageReader> irs = ImageIO.getImageReaders(iis); |
331 |
if (irs.hasNext()) { |
332 |
FileSystem fs = FileUtil.createMemoryFileSystem(); |
333 |
FileObject fob = fs.getRoot().createData(Long.toString(System.currentTimeMillis()), irs.next().getFormatName()); |
334 |
OutputStream os = fob.getOutputStream(); |
335 |
os.write(b.getBytes(1, (int) b.length())); |
336 |
os.close(); |
337 |
DataObject data = DataObject.find(fob); |
338 |
OpenCookie cookie = data.getLookup().lookup(OpenCookie.class); |
339 |
if (cookie != null) { |
340 |
cookie.open(); |
341 |
return; |
342 |
} |
343 |
} |
344 |
displayErrorOpenImage("openImageErrorNotImage.message"); |
345 |
} catch (SQLException ex) { |
346 |
LOG.log(Level.INFO, "SQLException while opening BLOB as file", ex); |
347 |
displayErrorOpenImage("openImageErrorDB.message"); |
348 |
} catch (IOException ex) { |
349 |
LOG.log(Level.INFO, "IOError while opening BLOB as file", ex); |
350 |
} |
351 |
|
352 |
} |
353 |
|
354 |
private void displayErrorOpenImage(String messageProperty) { |
355 |
DialogDisplayer dd = DialogDisplayer.getDefault(); |
356 |
|
357 |
String messageMsg = NbBundle.getMessage(BlobFieldTableCellEditor.class, |
358 |
messageProperty); |
359 |
String titleMsg = NbBundle.getMessage(BlobFieldTableCellEditor.class, |
360 |
"openImageError.title"); |
361 |
|
362 |
NotifyDescriptor nd = new NotifyDescriptor( |
363 |
messageMsg, |
364 |
titleMsg, |
365 |
NotifyDescriptor.OK_CANCEL_OPTION, |
366 |
NotifyDescriptor.WARNING_MESSAGE, |
367 |
new Object[]{NotifyDescriptor.CANCEL_OPTION}, |
368 |
NotifyDescriptor.CANCEL_OPTION); |
369 |
|
370 |
dd.notifyLater(nd); |
371 |
} |
297 |
} |
372 |
} |