/*
* JExportDialog.java
*
* Created on 8. April 2006, 18:41
*
* Copyright (C) 8. April 2006 <Reiner>
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package jexifviewer;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.filechooser.FileFilter;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import shared.cvshelper.Cvs;
import shared.dialog.JModalDialog;
import shared.files.JMyFileFilter;
import shared.files.JPathHelper;
import shared.layout.JGridBagPanel;
import shared.number.JDoubleHelper;
import shared.valueholder.JValueHolder;
/**
*
* @author reiner
*/
@Cvs
(
header = "$Header: /home/reiner/cvs/Java/JExifViewer/src/jexifviewer/JExportDialog.java,v 1.6 2010/04/02 12:12:13 reiner Exp $"
)
public final class JExportDialog extends JModalDialog
{
private JComboBox m_qualityBox;
private JLabel m_qualityLabel;
private JComboBox m_scaleFactorBox;
private JImgView m_imgView;
private JTextField m_sizeField;
private JTextField m_fileField;
private JButton m_fileBrowseButton;
private JCheckBox m_optimizedHufButton;
private JComboBox m_typeBox;
public double m_quality = 0.85f;
public double m_scaleFactor = 1.0;
public File m_file;
public boolean m_bOptimizedHuf;
public String m_mimeType = "image/jpeg";
public JExportDialog(JMainFrame frame, JImgView imgView)
{
super(frame, Main.getString("export_caption"), Main.getString("ok"), Main.getString("cancel"));
m_imgView = imgView;
}
protected void doInit()
{
JGridBagPanel panel = new JGridBagPanel();
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
int row=0;
panel.addComponent(new JLabel(Main.getString("export_label_type")), 0, row, GridBagConstraints.WEST, new Insets(5, 0, 0, 0));
row++;
panel.addComponent(m_typeBox = new JComboBox(), GridBagConstraints.RELATIVE, row, GridBagConstraints.WEST, new Insets(2, 0, 10, 0));
m_typeBox.addItem(new JValueHolder<String>(Main.getString("export_type_jpg"), "image/jpeg"));
m_typeBox.addItem(new JValueHolder<String>(Main.getString("export_type_png"), "image/png"));
m_typeBox.addItem(new JValueHolder<String>(Main.getString("export_type_bmp"), "image/bmp"));
m_typeBox.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
updateTypeData();
if (!m_mimeType.equals("image/jpeg"))
{
m_optimizedHufButton.setVisible(false);
m_qualityLabel.setVisible(false);
m_qualityBox.setVisible(false);
}
else
{
m_optimizedHufButton.setVisible(true);
m_qualityLabel.setVisible(true);
m_qualityBox.setVisible(true);
}
}
});
row++;
panel.addComponent(m_qualityLabel = new JLabel(Main.getString("export_label_quality")), 0, row, GridBagConstraints.WEST, new Insets(5, 0, 0, 0));
row++;
panel.addComponent(m_qualityBox = new JComboBox(), 0, row, GridBagConstraints.WEST, new Insets(2, 0, 0, 0));
m_qualityBox.setEditable(true);
for (double i=0; i<=1.01; i+=.05)
{
m_qualityBox.addItem(String.format("%.2f", i));
}
m_qualityBox.setSelectedItem(String.format("%.2f", m_quality));
panel.addComponent(m_optimizedHufButton = new JCheckBox(Main.getString("export_label_optimizedhuf")), 1, row, GridBagConstraints.WEST, new Insets(2, 10, 0, 0), 1, 1, 1, 0, GridBagConstraints.HORIZONTAL);
m_optimizedHufButton.setSelected(m_bOptimizedHuf);
row++;
panel.addComponent(new JLabel(Main.getString("export_label_file")), 0, row, GridBagConstraints.WEST, new Insets(5, 0, 0, 0));
row++;
panel.addComponent(m_fileField = new JTextField(24), 0, row, GridBagConstraints.WEST, new Insets(2, 0, 0, 0), 2, 1, 1, 0, GridBagConstraints.HORIZONTAL);
panel.addComponent(m_fileBrowseButton = new JButton(Main.getString("export_label_file_browse")), 2, row, GridBagConstraints.WEST, new Insets(2, 5, 0, 0));
final JExportDialog thisDialog = this;
m_fileBrowseButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JFileChooser fileChooser = new JFileChooser(Main.m_settings.getExportDirectory());
fileChooser.setDialogTitle(Main.getString("caption_export"));
FileFilter allFilter = fileChooser.getAcceptAllFileFilter();
fileChooser.setAcceptAllFileFilterUsed(false);
JMyFileFilter fileFilter = new JMyFileFilter();
updateTypeData();
if (m_mimeType.equals("image/jpeg"))
{
fileFilter.setDescription(Main.getString("filefilter_jpg"));
for (String item : Main.m_settings.m_jpgExtensions)
fileFilter.addExtension(item);
}
else if (m_mimeType.equals("image/png"))
{
fileFilter.setDescription(Main.getString("filefilter_png"));
fileFilter.addExtension("png");
}
else if (m_mimeType.equals("image/bmp"))
{
fileFilter.setDescription(Main.getString("filefilter_bmp"));
fileFilter.addExtension("bmp");
}
fileChooser.addChoosableFileFilter(fileFilter);
fileChooser.addChoosableFileFilter(allFilter);
fileChooser.setFileFilter(fileFilter);
if (!m_fileField.getText().isEmpty())
m_file = new File(m_fileField.getText());
else if (m_file != null)
{
StringBuilder strBuilder = new StringBuilder();
strBuilder.append(Main.m_settings.getExportDirectory());
JPathHelper.addSeparator(strBuilder);
strBuilder.append(JPathHelper.getBaseFileName(m_file));
m_file = new File(strBuilder.toString());
}
fileChooser.setSelectedFile(m_file);
if (fileChooser.showSaveDialog(thisDialog) == JFileChooser.APPROVE_OPTION)
{
String str = fileChooser.getSelectedFile().getAbsolutePath();
if (JPathHelper.getFileExtension(str).equals(""))
{
if (m_mimeType.equals("image/jpeg"))
str = str + ".jpg";
else if (m_mimeType.equals("image/png"))
str = str + ".png";
else if (m_mimeType.equals("image/bmp"))
str = str + ".bmp";
}
m_fileField.setText(str);
}
}
});
row++;
panel.addComponent(new JLabel(Main.getString("export_label_scalefactor")), 0, row, GridBagConstraints.WEST, new Insets(5, 0, 0, 0));
row++;
panel.addComponent(m_scaleFactorBox = new JComboBox(), GridBagConstraints.RELATIVE, row, GridBagConstraints.WEST, new Insets(2, 0, 10, 0));
m_scaleFactorBox.setEditable(true);
double[] scales = {1.0/8.0, 1.0/4.0, 1.0/3.0, 1.0/2.0, 2.0/3.0, 3.0/4.0, 7.0/8.0, 1.0, 5.0/4.0, 4.0/3.0, 3.0/2.0, 5.0/3.0, 7.0/4.0, 8.0/4.0};
for (double scale : scales)
m_scaleFactorBox.addItem(String.format("%.3f", scale));
m_scaleFactorBox.setSelectedItem(String.format("%.3f", m_scaleFactor));
m_scaleFactorBox.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
updateSizeField();
}
});
row--;
panel.addComponent(new JLabel(Main.getString("export_label_size")), 1, row, GridBagConstraints.WEST, new Insets(5, 10, 0, 0));
row++;
panel.addComponent(m_sizeField = new JTextField(13), GridBagConstraints.RELATIVE, row, GridBagConstraints.WEST, new Insets(2, 10, 10, 0));
m_sizeField.setEditable(false);
updateSizeField();
if (m_mimeType.equals("image/jpeg"))
m_typeBox.setSelectedIndex(0);
else if (m_mimeType.equals("image/png"))
m_typeBox.setSelectedIndex(1);
else if (m_mimeType.equals("image/bmp"))
m_typeBox.setSelectedIndex(2);
setPreferredSize(new Dimension(550, 320));
setResizable(false);
addCtrlPanel(panel);
}
protected boolean doGetData()
{
boolean flag = false;
try
{
m_quality = JDoubleHelper.parseDouble((String)m_qualityBox.getSelectedItem());
}
catch (NumberFormatException ex)
{}
if (m_quality < 0.0)
m_quality = 0.0;
if (m_quality > 1.0)
m_quality = 1.0;
updateScaleData();
String str = m_fileField.getText();
// if (!str.isEmpty())
if (str.length() > 0)
{
m_file = new File(str);
Main.m_settings.setExportDirectory(JPathHelper.getFolder(m_file));
flag = true;
}
else
{
JOptionPane.showMessageDialog(this, Main.getString("msg_export_nofile"), Main.getMessageBoxCaption(), JOptionPane.ERROR_MESSAGE);
m_fileField.requestFocusInWindow();
}
m_bOptimizedHuf = m_optimizedHufButton.isSelected();
updateTypeData();
return flag;
}
protected void updateScaleData()
{
try
{
m_scaleFactor = JDoubleHelper.parseDouble((String)m_scaleFactorBox.getSelectedItem());
}
catch (NumberFormatException ex)
{}
}
protected void updateTypeData()
{
@SuppressWarnings("unchecked" )
JValueHolder<String> val = (JValueHolder<String>)m_typeBox.getSelectedItem();
m_mimeType = val.getValue();
}
protected void updateSizeField()
{
updateScaleData();
Dimension dim = m_imgView.getImageSize(m_imgView.m_img, null, m_scaleFactor);
m_sizeField.setText(String.format("%d x %d", dim.width, dim.height));
}
}