001: /*
002: * ImageViewer.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.BorderLayout;
015: import java.awt.Dialog;
016: import java.awt.FlowLayout;
017: import java.awt.Frame;
018: import java.awt.event.ActionEvent;
019: import java.awt.event.ActionListener;
020: import java.awt.event.WindowEvent;
021: import java.awt.event.WindowListener;
022: import java.io.File;
023: import java.sql.Blob;
024: import javax.swing.ActionMap;
025: import javax.swing.InputMap;
026: import javax.swing.JButton;
027: import javax.swing.JComponent;
028: import javax.swing.JDialog;
029: import javax.swing.JPanel;
030: import javax.swing.WindowConstants;
031: import workbench.gui.WbSwingUtilities;
032: import workbench.gui.actions.EscAction;
033: import workbench.log.LogMgr;
034: import workbench.resource.ResourceMgr;
035: import workbench.resource.Settings;
036:
037: /**
038: * @author support@sql-workbench.net
039: */
040: public class ImageViewer extends JDialog implements ActionListener,
041: WindowListener {
042: private ImagePanel panel;
043: private JButton closeButton = new JButton(ResourceMgr
044: .getString("LblClose"));
045: private final String settingsId = "workbench.gui.imageviewer";
046: private EscAction escAction;
047:
048: public ImageViewer(Frame parent, String title) {
049: super (parent, title, true);
050: init();
051: WbSwingUtilities.center(this , parent);
052: }
053:
054: public ImageViewer(Dialog parent, String title) {
055: super (parent, title, true);
056: init();
057: WbSwingUtilities.center(this , parent.getParent());
058: }
059:
060: private void init() {
061: this .getContentPane().setLayout(new BorderLayout());
062: this .setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
063: panel = new ImagePanel();
064: this .getContentPane().add(panel, BorderLayout.CENTER);
065: JPanel buttonPanel = new JPanel();
066: buttonPanel
067: .setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
068: buttonPanel.add(closeButton);
069: closeButton.addActionListener(this );
070: this .getContentPane().add(buttonPanel, BorderLayout.SOUTH);
071: this .addWindowListener(this );
072: if (!Settings.getInstance().restoreWindowSize(this , settingsId)) {
073: setSize(640, 480);
074: }
075:
076: getRootPane().setDefaultButton(closeButton);
077: InputMap im = this .getRootPane().getInputMap(
078: JComponent.WHEN_IN_FOCUSED_WINDOW);
079: ActionMap am = this .getRootPane().getActionMap();
080: escAction = new EscAction(this );
081: escAction.addToInputMap(im, am);
082: }
083:
084: public void setData(Object data) {
085: try {
086: if (data instanceof Blob) {
087: this .panel.setImage((Blob) data);
088: } else if (data instanceof byte[]) {
089: this .panel.setImage((byte[]) data);
090: } else if (data instanceof File) {
091: this .panel.setImage((File) data);
092: }
093: } catch (Exception e) {
094: LogMgr.logError("ImageViewer.setData()",
095: "Error reading image", e);
096: }
097: }
098:
099: public void actionPerformed(ActionEvent e) {
100: this .setVisible(false);
101: this .dispose();
102: }
103:
104: public void windowOpened(WindowEvent e) {
105: }
106:
107: public void windowClosing(WindowEvent e) {
108: }
109:
110: public void windowClosed(WindowEvent e) {
111: Settings.getInstance().storeWindowSize(this , settingsId);
112: }
113:
114: public void windowIconified(WindowEvent e) {
115: }
116:
117: public void windowDeiconified(WindowEvent e) {
118: }
119:
120: public void windowActivated(WindowEvent e) {
121: }
122:
123: public void windowDeactivated(WindowEvent e) {
124: }
125:
126: }
|