/*
* JBatchDialog.java
*
* Created on 23.03.2010, 18:58:55
*
* Copyright (C) 23.03.2010, 18:58:55 <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.event.ActionEvent;
import java.awt.event.ActionListener;
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.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import shared.cvshelper.Cvs;
import shared.dialog.JModalDialog;
import shared.number.JDoubleHelper;
import shared.swing.JSwingHelper;
@Cvs
(
header = "$Header: /home/reiner/cvs/Java/JExifViewer/src/jexifviewer/JBatchDialog.java,v 1.4 2010/04/03 07:59:53 reiner Exp $"
)
public class JBatchDialog extends JModalDialog
{
private JMainFrame m_mainFrame;
private JBatchData m_batchData;
private JTextField m_ctrl_srcFolder;
private JCheckBox m_ctrl_recursive;
private JCheckBox m_ctrl_jpg;
private JCheckBox m_ctrl_png;
private JCheckBox m_ctrl_bmp;
private JComboBox m_ctrl_scale;
private JComboBox m_ctrl_targetFormat;
private JComboBox m_ctrl_targetQuality;
private JCheckBox m_ctrl_optimizedHuffman;
private JTextField m_ctrl_targetFolder;
private JTextField m_ctrl_targetTemplate;
public JBatchDialog(JMainFrame frame, JBatchData batchData)
{
super(frame, Main.getString("batchdialog_caption"), Main.getString("ok"), Main.getString("cancel"), 0);
m_mainFrame = frame;
m_batchData = batchData;
m_batchData.m_bRecursive = Main.m_settings.getBatchRecursive();
m_batchData.m_srcTypes = Main.m_settings.getBatchSrcFormats();
}
protected void doInit()
{
Dimension dim;
JPanel ctrlPanel = new JPanel();
SpringLayout layout = new SpringLayout();
ctrlPanel.setLayout(layout);
JLabel label = new JLabel(Main.getString("batchdialog_src_folder"));
ctrlPanel.add(label);
layout.putConstraint(SpringLayout.WEST, label, 10, SpringLayout.WEST, ctrlPanel);
layout.putConstraint(SpringLayout.NORTH, label, 10, SpringLayout.NORTH, ctrlPanel);
m_ctrl_srcFolder = new JTextField(m_batchData.m_srcFolder, 40);
ctrlPanel.add(m_ctrl_srcFolder);
layout.putConstraint(SpringLayout.WEST, m_ctrl_srcFolder, 0, SpringLayout.WEST, label);
layout.putConstraint(SpringLayout.NORTH, m_ctrl_srcFolder, 5, SpringLayout.SOUTH, label);
JButton button = new JButton(Main.getString("browse"));
ctrlPanel.add(button);
layout.putConstraint(SpringLayout.WEST, button, 10, SpringLayout.EAST, m_ctrl_srcFolder);
layout.putConstraint(SpringLayout.VERTICAL_CENTER, button, 0, SpringLayout.VERTICAL_CENTER, m_ctrl_srcFolder);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
JFileChooser fileChooser = new JFileChooser(m_ctrl_srcFolder.getText());
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setDialogTitle(Main.getString("batchdialog_browse_caption_src_folder"));
if (fileChooser.showOpenDialog(m_mainFrame) == JFileChooser.APPROVE_OPTION)
m_ctrl_srcFolder.setText(m_batchData.m_srcFolder = fileChooser.getSelectedFile().getPath());
}
});
layout.putConstraint(SpringLayout.EAST, ctrlPanel, 10, SpringLayout.EAST, button);
m_ctrl_recursive = new JCheckBox(Main.getString("batchdialog_recursive"), m_batchData.m_bRecursive);
ctrlPanel.add(m_ctrl_recursive);
layout.putConstraint(SpringLayout.WEST, m_ctrl_recursive, 0, SpringLayout.WEST, m_ctrl_srcFolder);
layout.putConstraint(SpringLayout.NORTH, m_ctrl_recursive, 15, SpringLayout.SOUTH, m_ctrl_srcFolder);
m_ctrl_bmp = new JCheckBox(Main.getString("batchdialog_bmp"));
ctrlPanel.add(m_ctrl_bmp);
if ((m_batchData.m_srcTypes & JBatchData.BMP_FORMAT) != 0)
m_ctrl_bmp.setSelected(true);
layout.putConstraint(SpringLayout.EAST, m_ctrl_bmp, 0, SpringLayout.EAST, m_ctrl_srcFolder);
layout.putConstraint(SpringLayout.NORTH, m_ctrl_bmp, 0, SpringLayout.NORTH, m_ctrl_recursive);
m_ctrl_png = new JCheckBox(Main.getString("batchdialog_png"));
ctrlPanel.add(m_ctrl_png);
if ((m_batchData.m_srcTypes & JBatchData.PNG_FORMAT) != 0)
m_ctrl_png.setSelected(true);
layout.putConstraint(SpringLayout.EAST, m_ctrl_png, -10, SpringLayout.WEST, m_ctrl_bmp);
layout.putConstraint(SpringLayout.NORTH, m_ctrl_png, 0, SpringLayout.NORTH, m_ctrl_recursive);
m_ctrl_jpg = new JCheckBox(Main.getString("batchdialog_jpg"));
ctrlPanel.add(m_ctrl_jpg);
if ((m_batchData.m_srcTypes & JBatchData.JPG_FORMAT) != 0)
m_ctrl_jpg.setSelected(true);
layout.putConstraint(SpringLayout.EAST, m_ctrl_jpg, -10, SpringLayout.WEST, m_ctrl_png);
layout.putConstraint(SpringLayout.NORTH, m_ctrl_jpg, 0, SpringLayout.NORTH, m_ctrl_recursive);
JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
ctrlPanel.add(sep);
layout.putConstraint(SpringLayout.WEST, sep, 10, SpringLayout.WEST, ctrlPanel);
layout.putConstraint(SpringLayout.EAST, sep, -10, SpringLayout.EAST, button);
layout.putConstraint(SpringLayout.NORTH, sep, 30, SpringLayout.SOUTH, m_ctrl_recursive);
/* Target stuff */
label = new JLabel(Main.getString("batchdialog_target_folder"));
ctrlPanel.add(label);
layout.putConstraint(SpringLayout.WEST, label, 0, SpringLayout.WEST, m_ctrl_srcFolder);
layout.putConstraint(SpringLayout.NORTH, label, 30, SpringLayout.SOUTH, sep);
m_ctrl_targetFolder = new JTextField(m_batchData.m_targetFolder != null && !m_batchData.m_targetFolder.isEmpty() ? m_batchData.m_targetFolder : m_batchData.m_srcFolder, 40);
ctrlPanel.add(m_ctrl_targetFolder);
layout.putConstraint(SpringLayout.WEST, m_ctrl_targetFolder, 0, SpringLayout.WEST, label);
layout.putConstraint(SpringLayout.EAST, m_ctrl_targetFolder, 0, SpringLayout.EAST, m_ctrl_srcFolder);
layout.putConstraint(SpringLayout.NORTH, m_ctrl_targetFolder, 5, SpringLayout.SOUTH, label);
JButton button_target_browse = new JButton(Main.getString("browse"));
ctrlPanel.add(button_target_browse);
layout.putConstraint(SpringLayout.VERTICAL_CENTER, button_target_browse, 0, SpringLayout.VERTICAL_CENTER, m_ctrl_targetFolder);
layout.putConstraint(SpringLayout.WEST, button_target_browse, 10, SpringLayout.EAST, m_ctrl_targetFolder);
layout.putConstraint(SpringLayout.EAST, button_target_browse, 0, SpringLayout.EAST, button);
button_target_browse.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
JFileChooser fileChooser = new JFileChooser(m_ctrl_targetFolder.getText());
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setDialogTitle(Main.getString("batchdialog_browse_caption_target_folder"));
if (fileChooser.showOpenDialog(m_mainFrame) == JFileChooser.APPROVE_OPTION)
m_ctrl_targetFolder.setText(m_batchData.m_targetFolder = fileChooser.getSelectedFile().getPath());
}
});
label = new JLabel(Main.getString("batchdialog_target_template"));
ctrlPanel.add(label);
layout.putConstraint(SpringLayout.WEST, label, 0, SpringLayout.WEST, m_ctrl_targetFolder);
layout.putConstraint(SpringLayout.NORTH, label, 15, SpringLayout.SOUTH, m_ctrl_targetFolder);
m_ctrl_targetTemplate = new JTextField(m_batchData.m_targetTemplate, 40);
ctrlPanel.add(m_ctrl_targetTemplate);
layout.putConstraint(SpringLayout.WEST, m_ctrl_targetTemplate, 0, SpringLayout.WEST, label);
layout.putConstraint(SpringLayout.EAST, m_ctrl_targetTemplate, 0, SpringLayout.EAST, m_ctrl_targetFolder);
layout.putConstraint(SpringLayout.NORTH, m_ctrl_targetTemplate, 5, SpringLayout.SOUTH, label);
label = new JLabel(Main.getString("batchdialog_target_format"));
ctrlPanel.add(label);
layout.putConstraint(SpringLayout.WEST, label, 0, SpringLayout.WEST, m_ctrl_targetTemplate);
layout.putConstraint(SpringLayout.NORTH, label, 20, SpringLayout.SOUTH, m_ctrl_targetTemplate);
m_ctrl_targetFormat = new JComboBox();
m_ctrl_targetFormat.addItem(Main.getString("batchdialog_select_jpg"));
m_ctrl_targetFormat.addItem(Main.getString("batchdialog_select_png"));
m_ctrl_targetFormat.addItem(Main.getString("batchdialog_select_bmp"));
ctrlPanel.add(m_ctrl_targetFormat);
layout.putConstraint(SpringLayout.WEST, m_ctrl_targetFormat, 0, SpringLayout.WEST, label);
layout.putConstraint(SpringLayout.NORTH, m_ctrl_targetFormat, 5, SpringLayout.SOUTH, label);
final JLabel label_quality = new JLabel(Main.getString("batchdialog_target_quality"));
ctrlPanel.add(label_quality);
layout.putConstraint(SpringLayout.WEST, label_quality, 50, SpringLayout.EAST, m_ctrl_targetFormat);
layout.putConstraint(SpringLayout.NORTH, label_quality, 0, SpringLayout.NORTH, label);
m_ctrl_targetQuality = new JComboBox();
ctrlPanel.add(m_ctrl_targetQuality);
m_ctrl_targetQuality.setEditable(true);
for (double i=0; i<=1.01; i+=.05)
m_ctrl_targetQuality.addItem(String.format("%.2f", i));
m_ctrl_targetQuality.setSelectedItem(String.format("%.2f", m_batchData.m_quality));
layout.putConstraint(SpringLayout.WEST, m_ctrl_targetQuality, 0, SpringLayout.WEST, label_quality);
layout.putConstraint(SpringLayout.NORTH, m_ctrl_targetQuality, 5, SpringLayout.SOUTH, label_quality);
m_ctrl_optimizedHuffman = new JCheckBox(Main.getString("batchdialog_target_optimizedhuffman"), m_batchData.m_bOptimizedHuffmanTable);
ctrlPanel.add(m_ctrl_optimizedHuffman);
layout.putConstraint(SpringLayout.WEST, m_ctrl_optimizedHuffman, 0, SpringLayout.WEST, label_quality);
layout.putConstraint(SpringLayout.NORTH, m_ctrl_optimizedHuffman, 10, SpringLayout.SOUTH, m_ctrl_targetQuality);
final JLabel label_scale = new JLabel(Main.getString("batchdialog_target_scale"));
ctrlPanel.add(label_scale);
layout.putConstraint(SpringLayout.WEST, label_scale, 50, SpringLayout.EAST, m_ctrl_targetQuality);
layout.putConstraint(SpringLayout.NORTH, label_scale, 0, SpringLayout.NORTH, label_quality);
m_ctrl_scale = new JComboBox();
ctrlPanel.add(m_ctrl_scale);
layout.putConstraint(SpringLayout.WEST, m_ctrl_scale, 0, SpringLayout.WEST, label_scale);
layout.putConstraint(SpringLayout.NORTH, m_ctrl_scale, 5, SpringLayout.SOUTH, label_scale);
m_ctrl_scale.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_ctrl_scale.addItem(String.format("%.3f", scale));
m_ctrl_scale.setSelectedItem(String.format("%.3f", m_batchData.m_scaleFactor));
m_ctrl_targetFormat.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
switch (m_ctrl_targetFormat.getSelectedIndex())
{
case 0:
JSwingHelper.enableComponent(label_quality, true);
JSwingHelper.enableComponent(m_ctrl_targetQuality, true);
JSwingHelper.enableComponent(m_ctrl_optimizedHuffman, true);
break;
case 1:
case 2:
JSwingHelper.enableComponent(label_quality, false);
JSwingHelper.enableComponent(m_ctrl_targetQuality, false);
JSwingHelper.enableComponent(m_ctrl_optimizedHuffman, false);
break;
default:
assert (false);
}
}
});
switch (m_batchData.m_targetFormat)
{
case JBatchData.JPG_FORMAT:
m_ctrl_targetFormat.setSelectedIndex(0);
break;
case JBatchData.PNG_FORMAT:
m_ctrl_targetFormat.setSelectedIndex(1);
break;
case JBatchData.BMP_FORMAT:
m_ctrl_targetFormat.setSelectedIndex(2);
break;
default:
assert (false);
}
setPreferredSize(new Dimension(550, 420));
setResizable(false);
addCtrlPanel(ctrlPanel);
}
protected boolean doGetData()
{
m_batchData.m_srcFolder = m_ctrl_srcFolder.getText();
m_batchData.m_bRecursive = m_ctrl_recursive.isSelected();
m_batchData.m_srcTypes = 0;
m_batchData.m_srcTypes |= (m_ctrl_jpg.isSelected() ? JBatchData.JPG_FORMAT : 0);
m_batchData.m_srcTypes |= (m_ctrl_png.isSelected() ? JBatchData.PNG_FORMAT : 0);
m_batchData.m_srcTypes |= (m_ctrl_bmp.isSelected() ? JBatchData.BMP_FORMAT : 0);
if (m_batchData.m_srcTypes == 0)
{
JOptionPane.showMessageDialog(m_mainFrame, Main.getString("batchdialog_msg_mustgive_srcformat"), Main.getMessageBoxCaption(), JOptionPane.WARNING_MESSAGE, Main.m_icon16);
return false;
}
m_batchData.m_targetFolder = m_ctrl_targetFolder.getText();
m_batchData.m_targetTemplate = m_ctrl_targetTemplate.getText();
switch (m_ctrl_targetFormat.getSelectedIndex())
{
case 0:
m_batchData.m_targetFormat = JBatchData.JPG_FORMAT;
break;
case 1:
m_batchData.m_targetFormat = JBatchData.PNG_FORMAT;
break;
case 2:
m_batchData.m_targetFormat = JBatchData.BMP_FORMAT;
break;
default:
assert (false);
}
String str = (String)m_ctrl_targetQuality.getSelectedItem();
if (str != null)
{
try
{
m_batchData.m_quality = JDoubleHelper.parseDouble(str);
}
catch(NumberFormatException ex)
{}
}
m_batchData.m_bOptimizedHuffmanTable = m_ctrl_optimizedHuffman.isSelected();
if ((str = (String)m_ctrl_scale.getSelectedItem()) != null)
{
try
{
m_batchData.m_scaleFactor = JDoubleHelper.parseDouble(str);
}
catch(NumberFormatException ex)
{}
}
Main.m_settings.setBatchRecursive(m_batchData.m_bRecursive);
Main.m_settings.setBatchSrcFormats(m_batchData.m_srcTypes);
Main.m_settings.setBatchTargetFormat(m_batchData.m_targetFormat);
Main.m_settings.setBatchJpgQuality((float)m_batchData.m_quality);
Main.m_settings.setBatchJpgOptHuf(m_batchData.m_bOptimizedHuffmanTable);
Main.m_settings.setBatchScale((float)m_batchData.m_scaleFactor);
return true;
}
}