/src/net/aerith/misao/toolkit/ImageConversion/ConversionDialog.java

https://github.com/jankotek/Pixy2 · Java · 389 lines · 176 code · 55 blank · 158 comment · 4 complexity · 89ae885360aba6bdad8c6a99e770ef7e MD5 · raw file

  1. /*
  2. * @(#)ConversionDialog.java
  3. *
  4. * Copyright (C) 1997-2007 Seiichi Yoshida
  5. * All rights reserved.
  6. */
  7. package net.aerith.misao.toolkit.ImageConversion;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import javax.swing.*;
  11. import javax.swing.border.*;
  12. import java.io.*;
  13. import java.net.*;
  14. import net.aerith.misao.util.Size;
  15. import net.aerith.misao.io.filechooser.ImageFileFilter;
  16. import net.aerith.misao.image.*;
  17. import net.aerith.misao.image.io.*;
  18. import net.aerith.misao.image.filter.FilterSet;
  19. import net.aerith.misao.gui.dialog.Dialog;
  20. import net.aerith.misao.toolkit.FilterSelection.FilterSelectionDialog;
  21. /**
  22. * The <code>ConversionDialog</code> represents a dialog to set up
  23. * parameters to convert the format of and transform all of the
  24. * images.
  25. *
  26. * @author Seiichi Yoshida (comet@aerith.net)
  27. * @version 2003 August 17
  28. */
  29. public class ConversionDialog extends Dialog {
  30. /**
  31. * The check box to select the input image format.
  32. */
  33. protected JCheckBox check_input_format;
  34. /**
  35. * The combo box to select the input image format.
  36. */
  37. protected JComboBox combo_input_format;
  38. /**
  39. * The list of input image file filters.
  40. */
  41. protected ImageFileFilter[] input_filters;
  42. /**
  43. * The check box to select the output image format.
  44. */
  45. protected JCheckBox check_output_format;
  46. /**
  47. * The combo box to select the output image format.
  48. */
  49. protected JComboBox combo_output_format;
  50. /**
  51. * The list of output image file filters.
  52. */
  53. protected ImageFileFilter[] output_filters;
  54. /**
  55. * The check box to change the file names by default.
  56. */
  57. protected JCheckBox check_change_filename;
  58. /**
  59. * The check box to set the image size or scale.
  60. */
  61. protected JCheckBox check_transform;
  62. /**
  63. * The panel to set the image size or scale.
  64. */
  65. protected TransformationPanel transform_panel;
  66. /**
  67. * The check box to apply the image processing filters.
  68. */
  69. protected JCheckBox check_filter;
  70. /**
  71. * The button to show the dialog to set the image processing
  72. * filters.
  73. */
  74. protected JButton button_filter;
  75. /**
  76. * The set of image processing filters.
  77. */
  78. protected FilterSet filter_set = new FilterSet();
  79. /**
  80. * Constructs a <code>ConversionDialog</code>.
  81. */
  82. public ConversionDialog ( ) {
  83. components = new Object[1];
  84. JTabbedPane tab = new JTabbedPane();
  85. File dummy_file = new File("dummy");
  86. combo_input_format = new JComboBox();
  87. input_filters = ImageFileFilter.getSupportedFilters();
  88. for (int i = 0 ; i < input_filters.length ; i++) {
  89. try {
  90. Format format = input_filters[i].getFormat(dummy_file);
  91. combo_input_format.addItem(format.getName());
  92. } catch ( MalformedURLException exception ) {
  93. System.err.println(exception);
  94. } catch ( UnsupportedFileTypeException exception ) {
  95. System.err.println(exception);
  96. }
  97. }
  98. combo_input_format.setSelectedIndex(0);
  99. check_input_format = new JCheckBox("Read all images in the same format.");
  100. check_input_format.setSelected(true);
  101. check_input_format.addActionListener(new InputFormatListener());
  102. JPanel panel_input_format = new JPanel();
  103. panel_input_format.setLayout(new BoxLayout(panel_input_format, BoxLayout.Y_AXIS));
  104. panel_input_format.add(check_input_format);
  105. panel_input_format.add(combo_input_format);
  106. panel_input_format.setBorder(new TitledBorder("Input Image format"));
  107. combo_output_format = new JComboBox();
  108. output_filters = ImageFileFilter.getBitmapFilters();
  109. for (int i = 0 ; i < output_filters.length ; i++) {
  110. try {
  111. Format format = output_filters[i].getFormat(dummy_file);
  112. combo_output_format.addItem(format.getName());
  113. } catch ( MalformedURLException exception ) {
  114. System.err.println(exception);
  115. } catch ( UnsupportedFileTypeException exception ) {
  116. System.err.println(exception);
  117. }
  118. }
  119. combo_output_format.setSelectedIndex(0);
  120. check_output_format = new JCheckBox("Create all images in the same format.");
  121. check_output_format.setSelected(true);
  122. check_output_format.addActionListener(new OutputFormatListener());
  123. check_change_filename = new JCheckBox("Change output image file names by default.");
  124. check_change_filename.setSelected(true);
  125. JPanel panel_output_format = new JPanel();
  126. panel_output_format.setLayout(new BoxLayout(panel_output_format, BoxLayout.Y_AXIS));
  127. panel_output_format.add(check_output_format);
  128. panel_output_format.add(combo_output_format);
  129. panel_output_format.add(check_change_filename);
  130. panel_output_format.setBorder(new TitledBorder("Output Image format"));
  131. JPanel panel_format= new JPanel();
  132. panel_format.setLayout(new BoxLayout(panel_format, BoxLayout.Y_AXIS));
  133. panel_format.add(panel_input_format);
  134. panel_format.add(panel_output_format);
  135. tab.addTab("Format", panel_format);
  136. check_transform = new JCheckBox("Transform all images in the same size or scale.");
  137. check_transform.setSelected(true);
  138. check_transform.addActionListener(new TransformListener());
  139. transform_panel = new TransformationPanel();
  140. JPanel panel_transform = new JPanel();
  141. panel_transform.setLayout(new BorderLayout());
  142. panel_transform.add(check_transform, BorderLayout.WEST);
  143. panel_transform.add(transform_panel, BorderLayout.SOUTH);
  144. panel_transform.setBorder(new TitledBorder("Image size"));
  145. tab.addTab("Transformation", panel_transform);
  146. check_filter = new JCheckBox("Apply image processing filters to all images.");
  147. check_filter.setSelected(false);
  148. check_filter.addActionListener(new FilterCheckListener());
  149. button_filter = new JButton("Select Filters");
  150. button_filter.setEnabled(false);
  151. button_filter.addActionListener(new FilterButtonListener());
  152. JPanel panel_filter = new JPanel();
  153. panel_filter.setLayout(new BorderLayout());
  154. panel_filter.add(check_filter, BorderLayout.WEST);
  155. panel_filter.add(button_filter, BorderLayout.SOUTH);
  156. tab.addTab("Filter", panel_filter);
  157. components[0] = tab;
  158. }
  159. /**
  160. * Gets the title of the dialog.
  161. * @return the title of the dialog.
  162. */
  163. protected String getTitle ( ) {
  164. return "Image Conversion Setting";
  165. }
  166. /**
  167. * Returns true when to read images in the same format.
  168. * @return true when to read images in the same format.
  169. */
  170. public boolean specifiesInputFormat ( ) {
  171. return check_input_format.isSelected();
  172. }
  173. /**
  174. * Gets the <code>FileFilter</code> for the selected input image
  175. * format.
  176. * @return the <code>FileFilter</code> for the selected input
  177. * image format.
  178. */
  179. public ImageFileFilter getInputFileFilter ( ) {
  180. return input_filters[combo_input_format.getSelectedIndex()];
  181. }
  182. /**
  183. * Returns true when to create images in the same format.
  184. * @return true when to create images in the same format.
  185. */
  186. public boolean specifiesOutputFormat ( ) {
  187. return check_output_format.isSelected();
  188. }
  189. /**
  190. * Gets the <code>FileFilter</code> for the selected output image
  191. * format.
  192. * @return the <code>FileFilter</code> for the selected output
  193. * image format.
  194. */
  195. public ImageFileFilter getOutputFileFilter ( ) {
  196. return output_filters[combo_output_format.getSelectedIndex()];
  197. }
  198. /**
  199. * Returns true when to change all output image file names by
  200. * default.
  201. * @return true when to change all output image file names by
  202. * default.
  203. */
  204. public boolean changesFilenames ( ) {
  205. return check_change_filename.isSelected();
  206. }
  207. /**
  208. * Returns true when to create images in the same size or scale.
  209. * @return true when to create images in the same size or scale.
  210. */
  211. public boolean transformsAll ( ) {
  212. return check_transform.isSelected();
  213. }
  214. /**
  215. * Returns true if the size is selected.
  216. * @return true if the size is selected.
  217. */
  218. public boolean isSize ( ) {
  219. return transform_panel.isSize();
  220. }
  221. /**
  222. * Returns true if the scale is selected.
  223. * @return true if the scale is selected.
  224. */
  225. public boolean isScale ( ) {
  226. return transform_panel.isScale();
  227. }
  228. /**
  229. * Gets the image size.
  230. * @return the image size.
  231. */
  232. public Size getImageSize ( ) {
  233. return transform_panel.getImageSize();
  234. }
  235. /**
  236. * Gets the image scale.
  237. * @return the image scale.
  238. */
  239. public int getScale ( ) {
  240. return transform_panel.getScale();
  241. }
  242. /**
  243. * Returns true when to rescale ST-4/6 Image.
  244. * @return true when to rescale ST-4/6 Image.
  245. */
  246. public boolean rescalesSbig ( ) {
  247. return transform_panel.rescalesSbig();
  248. }
  249. /**
  250. * Returns true when to apply image processing filters to all
  251. * images.
  252. * @return true when to apply image processing filters to all
  253. * images.
  254. */
  255. public boolean appliesImageProcessingFilters ( ) {
  256. return check_filter.isSelected();
  257. }
  258. /**
  259. * Gets the set of image processing filters.
  260. * @return the set of image processing filters.
  261. */
  262. public FilterSet getFilterSet ( ) {
  263. return filter_set;
  264. }
  265. /**
  266. * The <code>InputFormatListener</code> is a listener class of
  267. * menu selection to specify the input format.
  268. */
  269. protected class InputFormatListener implements ActionListener {
  270. /**
  271. * Invoked when one of the menus is selected.
  272. * @param e contains the selected menu item.
  273. */
  274. public void actionPerformed ( ActionEvent e ) {
  275. combo_input_format.setEnabled(check_input_format.isSelected());
  276. }
  277. }
  278. /**
  279. * The <code>OutputFormatListener</code> is a listener class of
  280. * menu selection to specify the output format.
  281. */
  282. protected class OutputFormatListener implements ActionListener {
  283. /**
  284. * Invoked when one of the menus is selected.
  285. * @param e contains the selected menu item.
  286. */
  287. public void actionPerformed ( ActionEvent e ) {
  288. combo_output_format.setEnabled(check_output_format.isSelected());
  289. check_change_filename.setEnabled(check_output_format.isSelected());
  290. }
  291. }
  292. /**
  293. * The <code>TransformListener</code> is a listener class of menu
  294. * selection to transform the images.
  295. */
  296. protected class TransformListener implements ActionListener {
  297. /**
  298. * Invoked when one of the menus is selected.
  299. * @param e contains the selected menu item.
  300. */
  301. public void actionPerformed ( ActionEvent e ) {
  302. transform_panel.setEnabled(check_transform.isSelected());
  303. }
  304. }
  305. /**
  306. * The <code>FilterCheckListener</code> is a listener class of
  307. * menu selection to apply image processing filters.
  308. */
  309. protected class FilterCheckListener implements ActionListener {
  310. /**
  311. * Invoked when one of the menus is selected.
  312. * @param e contains the selected menu item.
  313. */
  314. public void actionPerformed ( ActionEvent e ) {
  315. button_filter.setEnabled(check_filter.isSelected());
  316. }
  317. }
  318. /**
  319. * The <code>FilterButtonListener</code> is a listener class of
  320. * menu selection to select image processing filters.
  321. */
  322. protected class FilterButtonListener implements ActionListener {
  323. /**
  324. * Invoked when one of the menus is selected.
  325. * @param e contains the selected menu item.
  326. */
  327. public void actionPerformed ( ActionEvent e ) {
  328. FilterSelectionDialog dialog = new FilterSelectionDialog(filter_set);
  329. int answer = dialog.show(button_filter);
  330. if (answer == 0)
  331. filter_set = dialog.getFilterSet();
  332. }
  333. }
  334. }