001: /*
002: * BlobInfoDialog.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.dialogs;
013:
014: import java.awt.event.ActionEvent;
015: import java.awt.event.ActionListener;
016: import java.io.File;
017: import java.util.List;
018: import javax.swing.ActionMap;
019: import javax.swing.DefaultComboBoxModel;
020: import javax.swing.InputMap;
021: import javax.swing.JComponent;
022: import javax.swing.JDialog;
023: import workbench.gui.WbSwingUtilities;
024: import workbench.gui.actions.EscAction;
025: import workbench.gui.components.BlobHandler;
026: import workbench.gui.components.EncodingPanel;
027: import workbench.gui.components.FlatButton;
028: import workbench.log.LogMgr;
029: import workbench.resource.ResourceMgr;
030: import workbench.resource.Settings;
031: import workbench.util.ExceptionUtil;
032: import workbench.util.FileDialogUtil;
033: import workbench.util.StringUtil;
034: import workbench.util.ToolDefinition;
035:
036: /**
037: *
038: * @author support@sql-workbench.net
039: */
040: public class BlobInfoDialog extends JDialog implements ActionListener {
041: private Object blobValue;
042: private BlobHandler handler;
043: private EscAction escAction;
044: private File uploadFile;
045: private boolean hasTools = false;
046: private boolean setToNull = false;
047:
048: public BlobInfoDialog(java.awt.Frame parent, boolean modal) {
049: super (parent, modal);
050: initComponents();
051: handler = new BlobHandler();
052:
053: getRootPane().setDefaultButton(closeButton);
054: InputMap im = this .getRootPane().getInputMap(
055: JComponent.WHEN_IN_FOCUSED_WINDOW);
056: ActionMap am = this .getRootPane().getActionMap();
057: escAction = new EscAction(this );
058: im.put(escAction.getAccelerator(), escAction.getActionName());
059: am.put(escAction.getActionName(), escAction);
060:
061: String encoding = Settings.getInstance()
062: .getDefaultBlobTextEncoding();
063: encodingPanel.setEncoding(encoding);
064: List<ToolDefinition> tools = Settings.getInstance()
065: .getExternalTools();
066: this .hasTools = (tools != null && tools.size() > 0);
067: this .externalViewer.setEnabled(hasTools);
068: this .externalTools.setEnabled(hasTools);
069: if (hasTools) {
070: DefaultComboBoxModel model = new DefaultComboBoxModel();
071: String name = Settings.getInstance().getLastUsedBlobTool();
072: int toSelect = -1;
073:
074: for (int i = 0; i < tools.size(); i++) {
075: model.addElement(tools.get(i));
076: if (StringUtil
077: .equalString(name, tools.get(i).getName())) {
078: toSelect = i;
079: }
080: }
081:
082: this .externalTools.setModel(model);
083: if (toSelect != -1) {
084: this .externalTools.setSelectedIndex(toSelect);
085: }
086: }
087: WbSwingUtilities.center(this , parent);
088: }
089:
090: public File getUploadedFile() {
091: return uploadFile;
092: }
093:
094: public boolean setToNull() {
095: return setToNull;
096: }
097:
098: public byte[] getNewValue() {
099: return handler.getNewValue();
100: }
101:
102: public void actionPerformed(ActionEvent e) {
103: if (e.getSource() == escAction) {
104: closeWindow();
105: }
106: }
107:
108: private void closeWindow() {
109: setVisible(false);
110: dispose();
111: }
112:
113: public void setBlobValue(Object value) {
114: this .blobValue = value;
115: String lbl = null;
116: if (value instanceof File) {
117: lbl = ResourceMgr.getString("LblFileSize");
118: } else {
119: lbl = ResourceMgr.getString("LblBlobSize");
120: }
121: long len = 0;
122:
123: if (value == null) {
124: lbl = lbl + ": (null)";
125: } else {
126: len = handler.getBlobSize(blobValue);
127: lbl = lbl + ": " + Long.toString(len) + " Byte";
128: }
129: infoLabel.setText(lbl);
130: if (value instanceof File) {
131: infoLabel.setToolTipText(value.toString());
132: } else {
133: infoLabel.setToolTipText(handler.getByteDisplay(len)
134: .toString());
135: }
136: // Show as text is always enabled to allow text editing
137: // for null BLOBs as well.
138:
139: saveAsButton.setEnabled(len > 0);
140: showImageButton.setEnabled(len > 0);
141: showHexButton.setEnabled(len > 0);
142: externalViewer.setEnabled(len > 0 && hasTools);
143: }
144:
145: private void openWithExternalViewer() {
146: try {
147: File f = File.createTempFile("wb$tmp_", ".data");
148: f.deleteOnExit();
149: BlobHandler.saveBlobToFile(this .blobValue, f
150: .getAbsolutePath());
151: ToolDefinition tool = (ToolDefinition) this .externalTools
152: .getSelectedItem();
153: tool.runApplication(f.getAbsolutePath());
154: Settings.getInstance().setLastUsedBlobTool(tool.getName());
155: } catch (Exception e) {
156: LogMgr.logError("BlobInfoDialog.openWithExternalViewer()",
157: "Error running external program", e);
158: String msg = ExceptionUtil.getDisplay(e);
159: WbSwingUtilities.showErrorMessage(this , msg);
160: }
161: }
162:
163: /** This method is called from within the constructor to
164: * initialize the form.
165: * WARNING: Do NOT modify this code. The content of this method is
166: * always regenerated by the Form Editor.
167: */
168: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
169: private void initComponents() {
170: java.awt.GridBagConstraints gridBagConstraints;
171:
172: closeButton = new javax.swing.JButton();
173: jPanel1 = new javax.swing.JPanel();
174: infoLabel = new javax.swing.JLabel();
175: showAsTextButton = new FlatButton();
176: saveAsButton = new FlatButton();
177: encodingPanel = new EncodingPanel(null, false);
178: showImageButton = new FlatButton();
179: uploadButton = new FlatButton();
180: showHexButton = new FlatButton();
181: externalViewer = new FlatButton();
182: externalTools = new javax.swing.JComboBox();
183: setNullButton = new FlatButton();
184:
185: setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
186: setTitle(ResourceMgr.getString("TxtBlobInfo"));
187: setResizable(false);
188: addWindowListener(new java.awt.event.WindowAdapter() {
189: public void windowClosed(java.awt.event.WindowEvent evt) {
190: formWindowClosed(evt);
191: }
192: });
193: getContentPane().setLayout(new java.awt.GridBagLayout());
194:
195: closeButton.setText(ResourceMgr.getString("LblClose"));
196: closeButton.addMouseListener(new java.awt.event.MouseAdapter() {
197: public void mouseClicked(java.awt.event.MouseEvent evt) {
198: closeButtonMouseClicked(evt);
199: }
200: });
201: gridBagConstraints = new java.awt.GridBagConstraints();
202: gridBagConstraints.gridx = 0;
203: gridBagConstraints.gridy = 1;
204: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
205: gridBagConstraints.insets = new java.awt.Insets(12, 0, 10, 0);
206: getContentPane().add(closeButton, gridBagConstraints);
207:
208: jPanel1.setBorder(javax.swing.BorderFactory
209: .createLineBorder(java.awt.Color.lightGray));
210: jPanel1.setLayout(new java.awt.GridBagLayout());
211:
212: infoLabel.setText("jLabel1");
213: gridBagConstraints = new java.awt.GridBagConstraints();
214: gridBagConstraints.gridwidth = 2;
215: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
216: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
217: gridBagConstraints.insets = new java.awt.Insets(15, 10, 0, 8);
218: jPanel1.add(infoLabel, gridBagConstraints);
219:
220: showAsTextButton.setText(ResourceMgr.getString("LblShowAsTxt"));
221: showAsTextButton
222: .addActionListener(new java.awt.event.ActionListener() {
223: public void actionPerformed(
224: java.awt.event.ActionEvent evt) {
225: showAsTextButtonActionPerformed(evt);
226: }
227: });
228: gridBagConstraints = new java.awt.GridBagConstraints();
229: gridBagConstraints.gridx = 0;
230: gridBagConstraints.gridy = 4;
231: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
232: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
233: gridBagConstraints.weightx = 0.5;
234: gridBagConstraints.insets = new java.awt.Insets(9, 10, 0, 5);
235: jPanel1.add(showAsTextButton, gridBagConstraints);
236:
237: saveAsButton.setText(ResourceMgr.getString("MnuTxtFileSaveAs"));
238: saveAsButton
239: .addActionListener(new java.awt.event.ActionListener() {
240: public void actionPerformed(
241: java.awt.event.ActionEvent evt) {
242: saveAsButtonActionPerformed(evt);
243: }
244: });
245: gridBagConstraints = new java.awt.GridBagConstraints();
246: gridBagConstraints.gridx = 0;
247: gridBagConstraints.gridy = 1;
248: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
249: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
250: gridBagConstraints.weightx = 0.5;
251: gridBagConstraints.insets = new java.awt.Insets(9, 10, 2, 5);
252: jPanel1.add(saveAsButton, gridBagConstraints);
253:
254: encodingPanel.setLabelVisible(false);
255: gridBagConstraints = new java.awt.GridBagConstraints();
256: gridBagConstraints.gridx = 1;
257: gridBagConstraints.gridy = 4;
258: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
259: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
260: gridBagConstraints.weightx = 0.5;
261: gridBagConstraints.insets = new java.awt.Insets(9, 8, 0, 10);
262: jPanel1.add(encodingPanel, gridBagConstraints);
263:
264: showImageButton.setText(ResourceMgr.getString("LblShowAsImg"));
265: showImageButton
266: .addActionListener(new java.awt.event.ActionListener() {
267: public void actionPerformed(
268: java.awt.event.ActionEvent evt) {
269: showImageButtonActionPerformed(evt);
270: }
271: });
272: gridBagConstraints = new java.awt.GridBagConstraints();
273: gridBagConstraints.gridx = 0;
274: gridBagConstraints.gridy = 3;
275: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
276: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
277: gridBagConstraints.weightx = 0.5;
278: gridBagConstraints.insets = new java.awt.Insets(16, 10, 2, 5);
279: jPanel1.add(showImageButton, gridBagConstraints);
280:
281: uploadButton.setText(ResourceMgr.getString("LblUploadFile"));
282: uploadButton
283: .addActionListener(new java.awt.event.ActionListener() {
284: public void actionPerformed(
285: java.awt.event.ActionEvent evt) {
286: uploadButtonActionPerformed(evt);
287: }
288: });
289: gridBagConstraints = new java.awt.GridBagConstraints();
290: gridBagConstraints.gridx = 1;
291: gridBagConstraints.gridy = 1;
292: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
293: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
294: gridBagConstraints.insets = new java.awt.Insets(9, 8, 2, 10);
295: jPanel1.add(uploadButton, gridBagConstraints);
296:
297: showHexButton.setText(ResourceMgr.getString("LblShowAsHex"));
298: showHexButton
299: .addActionListener(new java.awt.event.ActionListener() {
300: public void actionPerformed(
301: java.awt.event.ActionEvent evt) {
302: showHexButtonActionPerformed(evt);
303: }
304: });
305: gridBagConstraints = new java.awt.GridBagConstraints();
306: gridBagConstraints.gridx = 1;
307: gridBagConstraints.gridy = 3;
308: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
309: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
310: gridBagConstraints.insets = new java.awt.Insets(16, 8, 2, 10);
311: jPanel1.add(showHexButton, gridBagConstraints);
312:
313: externalViewer
314: .setText(ResourceMgr.getString("LblExternalView"));
315: externalViewer.setToolTipText(ResourceMgr
316: .getDescription("LblExternalView"));
317: externalViewer
318: .addActionListener(new java.awt.event.ActionListener() {
319: public void actionPerformed(
320: java.awt.event.ActionEvent evt) {
321: externalViewerActionPerformed(evt);
322: }
323: });
324: gridBagConstraints = new java.awt.GridBagConstraints();
325: gridBagConstraints.gridx = 0;
326: gridBagConstraints.gridy = 5;
327: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
328: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
329: gridBagConstraints.weightx = 0.5;
330: gridBagConstraints.insets = new java.awt.Insets(7, 10, 11, 5);
331: jPanel1.add(externalViewer, gridBagConstraints);
332:
333: gridBagConstraints = new java.awt.GridBagConstraints();
334: gridBagConstraints.gridx = 1;
335: gridBagConstraints.gridy = 5;
336: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
337: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
338: gridBagConstraints.weightx = 0.5;
339: gridBagConstraints.insets = new java.awt.Insets(7, 8, 11, 10);
340: jPanel1.add(externalTools, gridBagConstraints);
341:
342: setNullButton.setText(ResourceMgr.getString("LblSetNull"));
343: setNullButton
344: .addActionListener(new java.awt.event.ActionListener() {
345: public void actionPerformed(
346: java.awt.event.ActionEvent evt) {
347: setNullButtonActionPerformed(evt);
348: }
349: });
350: gridBagConstraints = new java.awt.GridBagConstraints();
351: gridBagConstraints.gridx = 1;
352: gridBagConstraints.gridy = 2;
353: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
354: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
355: gridBagConstraints.insets = new java.awt.Insets(2, 8, 0, 10);
356: jPanel1.add(setNullButton, gridBagConstraints);
357:
358: gridBagConstraints = new java.awt.GridBagConstraints();
359: gridBagConstraints.gridx = 0;
360: gridBagConstraints.gridy = 0;
361: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
362: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
363: gridBagConstraints.weightx = 1.0;
364: gridBagConstraints.weighty = 1.0;
365: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
366: getContentPane().add(jPanel1, gridBagConstraints);
367:
368: pack();
369: }// </editor-fold>//GEN-END:initComponents
370:
371: private void setNullButtonActionPerformed(
372: java.awt.event.ActionEvent evt)//GEN-FIRST:event_setNullButtonActionPerformed
373: {//GEN-HEADEREND:event_setNullButtonActionPerformed
374: this .setToNull = true;
375: closeWindow();
376: }//GEN-LAST:event_setNullButtonActionPerformed
377:
378: private void showHexButtonActionPerformed(
379: java.awt.event.ActionEvent evt)//GEN-FIRST:event_showHexButtonActionPerformed
380: {//GEN-HEADEREND:event_showHexButtonActionPerformed
381: try {
382: byte[] data = handler.getBlobAsArray(this .blobValue);
383: if (data == null) {
384: WbSwingUtilities.showErrorMessageKey(this ,
385: "MsgBlobNotRetrieved");
386: return;
387: }
388: HexViewer v = new HexViewer(this , ResourceMgr
389: .getString("TxtBlobData"));
390: v.setData(data);
391: v.setVisible(true);
392: } catch (Exception e) {
393: LogMgr.logError(
394: "BlobInfoDialog.showHexButtonActionPerformed()",
395: "Error showing hex data", e);
396: }
397:
398: }//GEN-LAST:event_showHexButtonActionPerformed
399:
400: private void showAsTextButtonActionPerformed(
401: java.awt.event.ActionEvent evt)//GEN-FIRST:event_showAsTextButtonActionPerformed
402: {//GEN-HEADEREND:event_showAsTextButtonActionPerformed
403: if (handler.showBlobAsText(this , this .blobValue, encodingPanel
404: .getEncoding())) {
405: // if the blob has been edited, then clear the upload file in case
406: // there was one.
407: this .uploadFile = null;
408: }
409: }//GEN-LAST:event_showAsTextButtonActionPerformed
410:
411: private void saveAsButtonActionPerformed(
412: java.awt.event.ActionEvent evt)//GEN-FIRST:event_saveAsButtonActionPerformed
413: {//GEN-HEADEREND:event_saveAsButtonActionPerformed
414: long fileSize = -1;
415: try {
416: String file = FileDialogUtil.getBlobFile(this );
417: if (file == null)
418: return;
419:
420: fileSize = BlobHandler.saveBlobToFile(blobValue, file);
421: String msg = ResourceMgr.getString("MsgBlobSaved");
422: File f = new File(file);
423: msg = StringUtil.replace(msg, "%filename%", f
424: .getAbsolutePath());
425: fileSize = f.length();
426: msg = StringUtil.replace(msg, "%filesize%", Long
427: .toString(fileSize));
428: WbSwingUtilities.showMessage(this , msg);
429: } catch (Exception ex) {
430: LogMgr.logError("WbTable.saveBlobContent",
431: "Error when writing data file", ex);
432: String msg = ResourceMgr.getString("ErrBlobSaveError");
433: msg += "\n" + ExceptionUtil.getDisplay(ex);
434: WbSwingUtilities.showErrorMessage(this , msg);
435: }
436: }//GEN-LAST:event_saveAsButtonActionPerformed
437:
438: private void uploadButtonActionPerformed(
439: java.awt.event.ActionEvent evt)//GEN-FIRST:event_uploadButtonActionPerformed
440: {//GEN-HEADEREND:event_uploadButtonActionPerformed
441: String file = FileDialogUtil.getBlobFile(this , false);
442: if (file != null) {
443: this .uploadFile = new File(file);
444: }
445: }//GEN-LAST:event_uploadButtonActionPerformed
446:
447: private void showImageButtonActionPerformed(
448: java.awt.event.ActionEvent evt)//GEN-FIRST:event_showImageButtonActionPerformed
449: {//GEN-HEADEREND:event_showImageButtonActionPerformed
450: this .handler.showBlobAsImage(this , this .blobValue);
451: }//GEN-LAST:event_showImageButtonActionPerformed
452:
453: private void externalViewerActionPerformed(
454: java.awt.event.ActionEvent evt)//GEN-FIRST:event_externalViewerActionPerformed
455: {//GEN-HEADEREND:event_externalViewerActionPerformed
456: openWithExternalViewer();
457: }//GEN-LAST:event_externalViewerActionPerformed
458:
459: private void formWindowClosed(java.awt.event.WindowEvent evt)//GEN-FIRST:event_formWindowClosed
460: {//GEN-HEADEREND:event_formWindowClosed
461: String encoding = encodingPanel.getEncoding();
462: Settings.getInstance().setDefaultBlobTextEncoding(encoding);
463: }//GEN-LAST:event_formWindowClosed
464:
465: private void closeButtonMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_closeButtonMouseClicked
466: {//GEN-HEADEREND:event_closeButtonMouseClicked
467: closeWindow();
468: }//GEN-LAST:event_closeButtonMouseClicked
469:
470: // Variables declaration - do not modify//GEN-BEGIN:variables
471: public javax.swing.JButton closeButton;
472: public workbench.gui.components.EncodingPanel encodingPanel;
473: public javax.swing.JComboBox externalTools;
474: public javax.swing.JButton externalViewer;
475: public javax.swing.JLabel infoLabel;
476: public javax.swing.JPanel jPanel1;
477: public javax.swing.JButton saveAsButton;
478: public javax.swing.JButton setNullButton;
479: public javax.swing.JButton showAsTextButton;
480: public javax.swing.JButton showHexButton;
481: public javax.swing.JButton showImageButton;
482: public javax.swing.JButton uploadButton;
483: // End of variables declaration//GEN-END:variables
484:
485: }
|