00001 package guiUtilities;
00002 import javax.swing.*;
00003 import java.awt.*;
00004 import java.awt.event.*;
00005 import java.net.URL;
00006 import java.util.Vector;
00007 import java.util.StringTokenizer;
00008 import java.io.File;
00009
00010 import ProdDatabase.*;
00011
00012 public class ImageViewer extends JFrame
00013 implements ActionListener {
00014 Vector pictures;
00015
00016 JButton previousButton;
00017 JButton nextButton;
00018 JButton saveButton;
00019 JLabel photographLabel;
00020 JLabel captionLabel;
00021 JLabel numberLabel;
00022 int current = 0;
00023
00024
00025 public ImageViewer() {
00026 super("Image Viewer");
00027 JPanel pane = new JPanel();
00028
00029 pictures = new Vector();
00030
00031
00032 numberLabel = new JLabel("Picture " + (current+1) +
00033 " of 0");
00034 numberLabel.setHorizontalAlignment(JLabel.LEFT);
00035 numberLabel.setFont(new Font("SansSerif",Font.BOLD,12));
00036 numberLabel.setForeground(Color.blue);
00037
00038 numberLabel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 5));
00039
00040
00041
00042
00043 captionLabel = new JLabel("First caption");
00044 captionLabel.setFont(new Font("SansSerif",Font.BOLD,18));
00045 captionLabel.setForeground(Color.blue);
00046
00047 captionLabel.setHorizontalAlignment(JLabel.CENTER);
00048 captionLabel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
00049
00050
00051
00052 photographLabel = new JLabel("Loading first image...");
00053 photographLabel.setHorizontalAlignment(JLabel.CENTER);
00054 photographLabel.setVerticalAlignment(JLabel.CENTER);
00055 photographLabel.setVerticalTextPosition(JLabel.CENTER);
00056 photographLabel.setHorizontalTextPosition(JLabel.CENTER);
00057 photographLabel.setBorder(BorderFactory.createCompoundBorder(
00058 BorderFactory.createLoweredBevelBorder(),
00059 BorderFactory.createEmptyBorder(5, 5, 5, 5)
00060 ));
00061 photographLabel.setBorder(BorderFactory.createCompoundBorder(
00062 BorderFactory.createEmptyBorder(0, 0, 10, 0),
00063 photographLabel.getBorder()
00064 ));
00065
00066
00067
00068
00069 previousButton = new JButton("Previous");
00070
00071 previousButton.setActionCommand("previous");
00072 previousButton.addActionListener(this);
00073 previousButton.setEnabled(false);
00074
00075
00076 nextButton = new JButton("Next");
00077 nextButton.setActionCommand("next");
00078 nextButton.addActionListener(this);
00079
00080 saveButton = new JButton("Save Image");
00081 saveButton.setActionCommand("save");
00082 saveButton.addActionListener(this);
00083
00084
00085 pane.setLayout(new BoxLayout(pane,BoxLayout.Y_AXIS));
00086
00087
00088 captionLabel.setAlignmentX(CENTER_ALIGNMENT);
00089 pane.add(captionLabel);
00090 numberLabel.setAlignmentX(CENTER_ALIGNMENT);
00091 pane.add(numberLabel);
00092 photographLabel.setAlignmentX(CENTER_ALIGNMENT);
00093 pane.add(photographLabel);
00094 JPanel buttonPane = new JPanel();
00095 buttonPane.setLayout(new BoxLayout(buttonPane,BoxLayout.X_AXIS));
00096 buttonPane.add(previousButton);
00097 buttonPane.add(Box.createRigidArea(new Dimension(20,0)));
00098 buttonPane.add(nextButton);
00099 buttonPane.add(Box.createHorizontalGlue());
00100 buttonPane.add(saveButton);
00101 pane.add(buttonPane);
00102 setContentPane(pane);
00103
00104 addWindowListener(new java.awt.event.WindowAdapter() {
00105 public void windowClosing(java.awt.event.WindowEvent evt) {
00106 closeDialog(evt);
00107 }
00108 });
00109 }
00110
00111
00112 private void closeDialog(java.awt.event.WindowEvent evt) {
00113 setVisible(false);
00114 dispose();
00115 }
00116
00117 public void actionPerformed(ActionEvent e) {
00118
00119
00120
00121 if (e.getActionCommand().equals("next")) {
00122 current += 1;
00123 if (!previousButton.isEnabled()) previousButton.setEnabled(true);
00124 if (current == pictures.size() - 1) nextButton.setEnabled(false);
00125 updatePhotograph();
00126 } else if(e.getActionCommand().equals("previous")) {
00127 current -= 1;
00128 if (!nextButton.isEnabled()) nextButton.setEnabled(true);
00129 if (current == 0) previousButton.setEnabled(false);
00130 updatePhotograph();
00131 } else {
00132 Photo pic = (Photo)pictures.elementAt(current);
00133 File imageFile;
00134 imageFile = new File(Preferences.PreferencesInterface.getInstance().getPreference(Preferences.PreferencesInterface.SCRATCH_DIR),pic.name);
00135 try {
00136 GeneralUtilities.saveImage(pic.testno,pic.name,imageFile);
00137 JOptionPane.showMessageDialog(null,"Image saved as "+imageFile.toString(),"Image saved",JOptionPane.INFORMATION_MESSAGE);
00138 } catch(Exception e1) {System.out.println("Exception saving image: "+e1.toString());};
00139 }
00140
00141
00142
00143 }
00144
00145
00146 private void updatePhotograph() {
00147 Photo pic = (Photo)pictures.elementAt(current);
00148
00149
00150 captionLabel.setText(pic.headerTitle);
00151 numberLabel.setText("Picture " + (current+1) +
00152 " of " + pictures.size() + ": "+pic.caption);
00153
00154
00155 ImageIcon icon = pic.getIcon();
00156
00157 photographLabel.setToolTipText(pic.caption + ": " +
00158 icon.getIconWidth() + " X " +
00159 icon.getIconHeight());
00160 if(icon!=null) {
00161 photographLabel.setIcon(icon);
00162 photographLabel.setText("");
00163 }
00164 else {
00165 photographLabel.setIcon(null);
00166 photographLabel.setText("Image not available - data corrupted.");
00167 }
00168 }
00169 public void showNewPhotos(Vector listOfImages) {
00170 pictures = listOfImages;
00171 int widthOfWidest = 0;
00172 int heightOfTallest = 0;
00173
00174 for(int i=0;i<pictures.size();i++) {
00175 ImageIcon tempIcon = ((Photo)pictures.elementAt(i)).getIcon();
00176 int iwidth = tempIcon.getIconWidth();
00177 int iheight = tempIcon.getIconHeight();
00178 if(iwidth>widthOfWidest) widthOfWidest = iwidth;
00179 if(iheight>heightOfTallest) heightOfTallest = iheight;
00180 }
00181 Insets i = photographLabel.getInsets();
00182 photographLabel.setPreferredSize(new Dimension(
00183 widthOfWidest+i.left+i.right,
00184 heightOfTallest+i.bottom+i.top));
00185 current=0;
00186 previousButton.setEnabled(false);
00187 if(pictures.size()>1) nextButton.setEnabled(true);
00188 else nextButton.setEnabled(false);
00189 updatePhotograph();
00190 pack();
00191 setVisible(true);
00192 }
00193
00194
00195 }