From: Gerardo H. <ma...@us...> - 2007-02-28 00:02:56
|
Update of /cvsroot/jrman/drafts/src/net/falappa/imageio In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv4501/src/net/falappa/imageio Modified Files: ImageViewerPanelLoadAction.java ImageWriterSpiFileFilter.java Log Message: Finished reformatting code :) Code is almost 100% clean (no more hideous Eclipse formatting, no tabs, no line longer than 80 columns, no CR/LF) Index: ImageWriterSpiFileFilter.java =================================================================== RCS file: /cvsroot/jrman/drafts/src/net/falappa/imageio/ImageWriterSpiFileFilter.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** ImageWriterSpiFileFilter.java 26 Feb 2007 15:25:14 -0000 1.5 --- ImageWriterSpiFileFilter.java 28 Feb 2007 00:02:40 -0000 1.6 *************** *** 36,100 **** */ public class ImageWriterSpiFileFilter extends FileFilter { ! private String description; ! private String[] suffixes; ! private ImageWriterSpi writer; ! private static final ResourceBundle messagesBundle= ! ResourceBundle.getBundle( ! ImageWriterSpiFileFilter.class.getPackage().getName() ! + ".res.ImageSpiFileFilters"); ! /** ! * Create a file filter based on an image writer. ! * @param writer the ImageWriterSpi object the filter is based on ! */ ! public ImageWriterSpiFileFilter(ImageWriterSpi writer) { ! this.writer= writer; ! StringBuffer sb= new StringBuffer(); ! String template= messagesBundle.getString("ImageSpiFileFilter.image_files"); //$NON-NLS-1$ ! sb.append( ! MessageFormat.format( ! template, ! writer.getFormatNames()[0].toUpperCase())); ! suffixes= writer.getFileSuffixes(); ! sb.append(" (*.").append(suffixes[0]); //$NON-NLS-1$ ! for (int i= 1; i < suffixes.length; i++) ! sb.append(";*.").append(suffixes[i]); //$NON-NLS-1$ ! sb.append(')'); ! description= sb.toString(); ! } ! // implements the method of the abstract base class ! public boolean accept(File f) { ! return f.isDirectory() || hasCorrectSuffix(f.getName()); ! } ! // implements the method of the abstract base class ! public String getDescription() { ! return description; ! } ! /** ! * Tests if a string ends with one of the suffixes accepted by the image writer. ! * @param name the string to test ! * @return true if the string passes the test ! */ ! public boolean hasCorrectSuffix(String name) { ! boolean accepted= false; ! for (int i= 0; !accepted && i < suffixes.length; i++) ! accepted= accepted || name.endsWith(suffixes[i]); ! return accepted; ! } ! /** ! * Appends a suffix accepted by the image writer to the file name passed as argument. ! * @param fileName the file name to modify ! * @return a new file name ! */ ! public String addSuffix(String fileName) { ! return fileName + "." + suffixes[0]; //$NON-NLS-1$ ! } ! /** ! * Returns the imagewriter corresponding to the filter ! * @return a ImageWriterSpi object ! */ ! public ImageWriterSpi getImageWriterSpi() { ! return writer; ! } } --- 36,100 ---- */ public class ImageWriterSpiFileFilter extends FileFilter { ! private String description; ! private String[] suffixes; ! private ImageWriterSpi writer; ! private static final ResourceBundle messagesBundle= ! ResourceBundle.getBundle( ! ImageWriterSpiFileFilter.class.getPackage().getName() ! + ".res.ImageSpiFileFilters"); ! /** ! * Create a file filter based on an image writer. ! * @param writer the ImageWriterSpi object the filter is based on ! */ ! public ImageWriterSpiFileFilter(ImageWriterSpi writer) { ! this.writer= writer; ! StringBuffer sb= new StringBuffer(); ! String template= messagesBundle.getString("ImageSpiFileFilter.image_files"); //$NON-NLS-1$ ! sb.append( ! MessageFormat.format( ! template, ! writer.getFormatNames()[0].toUpperCase())); ! suffixes= writer.getFileSuffixes(); ! sb.append(" (*.").append(suffixes[0]); //$NON-NLS-1$ ! for (int i= 1; i < suffixes.length; i++) ! sb.append(";*.").append(suffixes[i]); //$NON-NLS-1$ ! sb.append(')'); ! description= sb.toString(); ! } ! // implements the method of the abstract base class ! public boolean accept(File f) { ! return f.isDirectory() || hasCorrectSuffix(f.getName()); ! } ! // implements the method of the abstract base class ! public String getDescription() { ! return description; ! } ! /** ! * Tests if a string ends with one of the suffixes accepted by the image writer. ! * @param name the string to test ! * @return true if the string passes the test ! */ ! public boolean hasCorrectSuffix(String name) { ! boolean accepted= false; ! for (int i= 0; !accepted && i < suffixes.length; i++) ! accepted= accepted || name.endsWith(suffixes[i]); ! return accepted; ! } ! /** ! * Appends a suffix accepted by the image writer to the file name passed as argument. ! * @param fileName the file name to modify ! * @return a new file name ! */ ! public String addSuffix(String fileName) { ! return fileName + "." + suffixes[0]; //$NON-NLS-1$ ! } ! /** ! * Returns the imagewriter corresponding to the filter ! * @return a ImageWriterSpi object ! */ ! public ImageWriterSpi getImageWriterSpi() { ! return writer; ! } } Index: ImageViewerPanelLoadAction.java =================================================================== RCS file: /cvsroot/jrman/drafts/src/net/falappa/imageio/ImageViewerPanelLoadAction.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ImageViewerPanelLoadAction.java 27 Dec 2006 16:30:18 -0000 1.3 --- ImageViewerPanelLoadAction.java 28 Feb 2007 00:02:40 -0000 1.4 *************** *** 51,135 **** */ public class ImageViewerPanelLoadAction extends AbstractAction { ! private JImageViewerPanel viewerPanel; ! private static final ResourceBundle messagesBundle= ! ResourceBundle.getBundle( ! ImageViewerPanelSaveAction.class.getPackage().getName() ! + ".res.ImageViewerPanelActions"); ! private JFileChooser fc; ! /** ! * Constructs and initializes this object ! * @param viewerPanel the <code>JImageViewerPanel</code> this action is linked to ! */ ! public ImageViewerPanelLoadAction(JImageViewerPanel viewerPanel) { ! super(messagesBundle.getString("ImageViewerPanelLoadAction.Load_2")); //$NON-NLS-1$ ! this.viewerPanel= viewerPanel; ! putValue(SHORT_DESCRIPTION, messagesBundle.getString("ImageViewerPanelLoadAction.Load_image_from_file_3")); //$NON-NLS-1$ ! putValue(SMALL_ICON, UIManager.getIcon("Tree.openIcon")); //$NON-NLS-1$ ! } ! /* (non-Javadoc) ! * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) ! */ ! public void actionPerformed(ActionEvent e) { ! if (fc == null) { ! fc= new JFileChooser(); ! fc.setAcceptAllFileFilterUsed(false); ! fc.setFileSelectionMode(JFileChooser.FILES_ONLY); ! fc.setMultiSelectionEnabled(false); ! fc.setDialogTitle(messagesBundle.getString("ImageViewerPanelLoadAction.Choose_filename_to_load_5")); //$NON-NLS-1$ ! // prepare file filters ! IIORegistry theRegistry= IIORegistry.getDefaultInstance(); ! Iterator it= ! theRegistry.getServiceProviders(ImageReaderSpi.class, false); ! while (it.hasNext()) { ! ImageReaderSpi reader= (ImageReaderSpi)it.next(); ! ImageReaderSpiFileFilter ff= ! new ImageReaderSpiFileFilter(reader); ! fc.addChoosableFileFilter(ff); ! } ! } ! if (fc.showOpenDialog(viewerPanel) == JFileChooser.APPROVE_OPTION) { ! File selectedFile= fc.getSelectedFile(); ! if (selectedFile != null && selectedFile.exists()) { ! //String fileName= selectedFile.getAbsolutePath(); ! ImageReaderSpiFileFilter ff= ! (ImageReaderSpiFileFilter)fc.getFileFilter(); ! readFromFile(selectedFile, ff); ! } ! } ! } ! /** ! * @param selectedFile ! * @param ff ! */ ! private void readFromFile(File selectedFile, ImageReaderSpiFileFilter ff) { ! try { ! ImageInputStream iis= ImageIO.createImageInputStream(selectedFile); ! ImageReader ir= ff.getImageReaderSpi().createReaderInstance(); ! ir.setInput(iis); ! // preparing a destination bufferedimage of type INT_RGB will make ! // scrolling and zooming faster (at least on Win2k) ! int idx= ir.getMinIndex(); ! BufferedImage bufferedImage= ! new BufferedImage( ! ir.getWidth(idx), ! ir.getHeight(idx), ! BufferedImage.TYPE_INT_RGB); ! ImageReadParam irp= ir.getDefaultReadParam(); ! irp.setDestination(bufferedImage); ! bufferedImage= ir.read(idx,irp); ! viewerPanel.setImage(bufferedImage); ! ir.dispose(); ! iis.close(); ! } ! catch (IOException ioe) { ! JOptionPane.showMessageDialog(viewerPanel, messagesBundle.getString("ImageViewerPanelLoadAction.Error_during_image_loading_7"), //$NON-NLS-1$ ! messagesBundle.getString("ImageViewerPanelLoadAction.Error_8"), //$NON-NLS-1$ ! JOptionPane.ERROR_MESSAGE); ! ioe.printStackTrace(); ! } ! } } --- 51,135 ---- */ public class ImageViewerPanelLoadAction extends AbstractAction { ! private JImageViewerPanel viewerPanel; ! private static final ResourceBundle messagesBundle= ! ResourceBundle.getBundle( ! ImageViewerPanelSaveAction.class.getPackage().getName() ! + ".res.ImageViewerPanelActions"); ! private JFileChooser fc; ! /** ! * Constructs and initializes this object ! * @param viewerPanel the <code>JImageViewerPanel</code> this action is linked to ! */ ! public ImageViewerPanelLoadAction(JImageViewerPanel viewerPanel) { ! super(messagesBundle.getString("ImageViewerPanelLoadAction.Load_2")); //$NON-NLS-1$ ! this.viewerPanel= viewerPanel; ! putValue(SHORT_DESCRIPTION, messagesBundle.getString("ImageViewerPanelLoadAction.Load_image_from_file_3")); //$NON-NLS-1$ ! putValue(SMALL_ICON, UIManager.getIcon("Tree.openIcon")); //$NON-NLS-1$ ! } ! /* (non-Javadoc) ! * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) ! */ ! public void actionPerformed(ActionEvent e) { ! if (fc == null) { ! fc= new JFileChooser(); ! fc.setAcceptAllFileFilterUsed(false); ! fc.setFileSelectionMode(JFileChooser.FILES_ONLY); ! fc.setMultiSelectionEnabled(false); ! fc.setDialogTitle(messagesBundle.getString("ImageViewerPanelLoadAction.Choose_filename_to_load_5")); //$NON-NLS-1$ ! // prepare file filters ! IIORegistry theRegistry= IIORegistry.getDefaultInstance(); ! Iterator it= ! theRegistry.getServiceProviders(ImageReaderSpi.class, false); ! while (it.hasNext()) { ! ImageReaderSpi reader= (ImageReaderSpi)it.next(); ! ImageReaderSpiFileFilter ff= ! new ImageReaderSpiFileFilter(reader); ! fc.addChoosableFileFilter(ff); ! } ! } ! if (fc.showOpenDialog(viewerPanel) == JFileChooser.APPROVE_OPTION) { ! File selectedFile= fc.getSelectedFile(); ! if (selectedFile != null && selectedFile.exists()) { ! //String fileName= selectedFile.getAbsolutePath(); ! ImageReaderSpiFileFilter ff= ! (ImageReaderSpiFileFilter)fc.getFileFilter(); ! readFromFile(selectedFile, ff); ! } ! } ! } ! /** ! * @param selectedFile ! * @param ff ! */ ! private void readFromFile(File selectedFile, ImageReaderSpiFileFilter ff) { ! try { ! ImageInputStream iis= ImageIO.createImageInputStream(selectedFile); ! ImageReader ir= ff.getImageReaderSpi().createReaderInstance(); ! ir.setInput(iis); ! // preparing a destination bufferedimage of type INT_RGB will make ! // scrolling and zooming faster (at least on Win2k) ! int idx= ir.getMinIndex(); ! BufferedImage bufferedImage= ! new BufferedImage( ! ir.getWidth(idx), ! ir.getHeight(idx), ! BufferedImage.TYPE_INT_RGB); ! ImageReadParam irp= ir.getDefaultReadParam(); ! irp.setDestination(bufferedImage); ! bufferedImage= ir.read(idx,irp); ! viewerPanel.setImage(bufferedImage); ! ir.dispose(); ! iis.close(); ! } ! catch (IOException ioe) { ! JOptionPane.showMessageDialog(viewerPanel, messagesBundle.getString("ImageViewerPanelLoadAction.Error_during_image_loading_7"), //$NON-NLS-1$ ! messagesBundle.getString("ImageViewerPanelLoadAction.Error_8"), //$NON-NLS-1$ ! JOptionPane.ERROR_MESSAGE); ! ioe.printStackTrace(); ! } ! } } |