Coverage details for ui.panel.ImagePanel

LineHitsSource
1 /*******************************************************************************
2  * InternetCafe is a software solution that helps the management of Cybercafes
3  * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4  * Copyright (C) 2006 Guido Angelo Ingenito
5  
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *******************************************************************************/
20 package ui.panel;
21  
22 import java.awt.BorderLayout;
23 import java.awt.Dimension;
24 import java.awt.GridLayout;
25 import java.awt.MediaTracker;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.awt.image.BufferedImage;
29 import java.io.File;
30  
31 import javax.imageio.ImageIO;
32 import javax.swing.ImageIcon;
33 import javax.swing.JButton;
34 import javax.swing.JFileChooser;
35 import javax.swing.JPanel;
36 import javax.swing.JScrollPane;
37  
38 import org.apache.log4j.Logger;
39  
40 import ui.Messages;
41 import ui.util.FileExtensionFilter;
42  
430@SuppressWarnings("serial") //$NON-NLS-1$
440public class ImagePanel extends JPanel {
450 
460    private static final transient Logger logger = Logger
470            .getLogger(ImagePanel.class.getName());
480 
490    private BufferedImage bufferedImage;
500 
510    private String imagePath;
520 
530    private MediaTracker tracker = new MediaTracker(this);
54  
55     private JScrollPane imageScrollPanel;
56  
57     private JPanel buttonPanel;
58  
59     private ImageDrawPanel drawingPanel;
60  
61     private JButton openImageButton;
62  
63     private JButton deleteImageButton;
64  
65     private JButton zoomOriginalImageButton;
66  
67     private JButton zoomBestFitImageButton;
68  
69     private JButton zoomInImageButton;
70  
71     private JButton zoomOutImageButton;
720 
730    private boolean scaleImage = false;
740 
750    public ImagePanel(String imagePath) {
760        this.imagePath = imagePath;
770        initialize();
780    }
79  
800    protected void initialize() {
810        this.setSize(new Dimension(350, 400));
820        this.setPreferredSize(new Dimension(350, 400));
830        this.setMinimumSize(new Dimension(350, 400));
840 
850        this.setLayout(new BorderLayout());
860        this.add(getImageScrollPanel(), BorderLayout.CENTER);
870        this.add(getButtonPanel(), BorderLayout.EAST);
880    }
89  
90     /**
91      * @return Returns the bufferedImage.
92      */
930    protected BufferedImage getBufferedImage() {
940        if (bufferedImage == null) {
950            if (getImagePath() == "" || !new File(getImagePath()).exists()) //$NON-NLS-1$
960                return null;
970            try {
980                if (!scaleImage)
990                    bufferedImage = ImageIO.read(getImageFile());
1000                else {
1010                    tracker.addImage(bufferedImage = ImageIO
1020                            .read(getImageFile()), 0);
1030                    tracker.waitForID(0);
1040                    this.getDrawingPanel().setImage(bufferedImage);
1050                }
1060                this.getDrawingPanel().updateUI();
1070            } catch (Exception ex) {
1080                logger.error(ex.getMessage());
1090                ex.printStackTrace();
1100            }
1110        }
1120        return bufferedImage;
113     }
114  
115     /**
116      * @return Returns the imageFile.
117      */
1180    protected File getImageFile() {
1190        return new File(getImagePath());
120     }
121  
122     /**
123      * @return Returns the imagePath.
124      */
1250    protected String getImagePath() {
1260        if (imagePath == null)
1270            imagePath = ""; //$NON-NLS-1$
1280        return imagePath;
129     }
130  
131     /**
132      * @return Returns the buttonPanel.
133      */
1340    protected JPanel getButtonPanel() {
1350        if (buttonPanel == null) {
1360            buttonPanel = new JPanel();
1370            buttonPanel.setLayout(new GridLayout(6, 1));
1380            buttonPanel.add(getZoomOriginalImageButton(), null);
1390            buttonPanel.add(getZoomBestFitImageButton(), null);
1400            buttonPanel.add(getZoomInImageButton(), null);
1410            buttonPanel.add(getZoomOutImageButton(), null);
1420            buttonPanel.add(getOpenImageButton(), null);
1430            buttonPanel.add(getDeleteImageButton(), null);
1440        }
1450        return buttonPanel;
146     }
147  
148     /**
149      * @return Returns the imageScrollPanel.
150      */
1510    protected JScrollPane getImageScrollPanel() {
1520        if (imageScrollPanel == null) {
1530            imageScrollPanel = new JScrollPane();
1540            imageScrollPanel.setViewportView(getDrawingPanel());
1550            imageScrollPanel
1560                    .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
1570            imageScrollPanel
1580                    .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
1590        }
1600        return imageScrollPanel;
161     }
162  
163     /**
164      * @return Returns the openImageButton.
165      */
1660    public JButton getOpenImageButton() {
1670        if (openImageButton == null) {
1680            openImageButton = new JButton();
1690            openImageButton.setIcon(new ImageIcon(this.getClass().getResource(
1700                    "/icon/22x22/actions/document-open.png"))); //$NON-NLS-1$
1710 
1720            openImageButton.addActionListener(new ActionListener() {
173                 public void actionPerformed(ActionEvent arg0) {
174                     logger.debug("actionPerformed openImageButton"); //$NON-NLS-1$
175                     final JFileChooser chooser = new JFileChooser();
176                     chooser.setDialogTitle(Messages.getString("panel.imagepanel.openimage")); //$NON-NLS-1$
177                     chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
178                     chooser.setApproveButtonText(Messages.getString("button.open")); //$NON-NLS-1$
179                     chooser.setFileFilter(new FileExtensionFilter(Messages.getString("common.filetype.jpg"), //$NON-NLS-1$
180                             Messages.getString("common.file.extension.jpg"))); //$NON-NLS-1$
181                     if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
182                         imagePath = chooser.getSelectedFile().getAbsolutePath();
183                         bufferedImage = null;
184                         getDrawingPanel().setImage(
185                                 bufferedImage = getBufferedImage());
186                         getImageScrollPanel().updateUI();
187  
188                     }
189                 }
190             });
1910        }
1920        return openImageButton;
193     }
194  
195     /**
196      * @return Returns the deleteImageButton.
197      */
1980    protected JButton getDeleteImageButton() {
1990        if (deleteImageButton == null) {
2000            deleteImageButton = new JButton();
2010            deleteImageButton.setIcon(new ImageIcon(this.getClass()
2020                    .getResource("/icon/22x22/actions/edit-delete.png"))); //$NON-NLS-1$
2030            deleteImageButton.addActionListener(new ActionListener() {
204                 public void actionPerformed(ActionEvent arg0) {
205                     logger.debug("actionPerformed deleteImageButton"); //$NON-NLS-1$
206                     imagePath = ""; //$NON-NLS-1$
207                     bufferedImage = null;
208                     getDrawingPanel().setImage(null);
209                     getImageScrollPanel().updateUI();
210                 }
211             });
2120        }
2130        return deleteImageButton;
214     }
215  
216     /**
217      * @return Returns the zoomOriginalImageButton.
218      */
2190    protected JButton getZoomOriginalImageButton() {
2200        if (zoomOriginalImageButton == null) {
2210            zoomOriginalImageButton = new JButton("="); //$NON-NLS-1$
222             // zoomOriginalImageButton.setIcon(new
2230            // ImageIcon(this.getClass().getResource("/icon/22x22/actions/zoom-original.png")));
2240            zoomOriginalImageButton.addActionListener(new ActionListener() {
225                 public void actionPerformed(ActionEvent arg0) {
226                     logger.debug("actionPerformed zoomOriginalImageButton"); //$NON-NLS-1$
227                     getDrawingPanel().originalSize();
228                     getImageScrollPanel().updateUI();
229                 }
230             });
2310        }
2320        return zoomOriginalImageButton;
233     }
234  
235     /**
236      * @return Returns the zoomBestFitImageButton.
237      */
2380    protected JButton getZoomBestFitImageButton() {
2390        if (zoomBestFitImageButton == null) {
2400            zoomBestFitImageButton = new JButton("<>"); //$NON-NLS-1$
241             // zoomBestFitImageButton.setIcon(new
2420            // ImageIcon(this.getClass().getResource("/icon/22x22/actions/zoom-best-fit.png")));
2430            zoomBestFitImageButton.addActionListener(new ActionListener() {
244                 public void actionPerformed(ActionEvent arg0) {
245                     logger.debug("actionPerformed zoomBestFitImageButton"); //$NON-NLS-1$
246                     // int width = (int)
247                     // getImageScrollPanel().getVisibleRect().getWidth();
248                     // int height = (int)
249                     // getImageScrollPanel().getVisibleRect().getHeight();
250                     // getDrawingPanel().setImage(getBufferedImage()..getScaledInstance(width,height,BufferedImage.SCALE_AREA_AVERAGING));
251                     getImageScrollPanel().updateUI();
252                 }
253             });
2540        }
2550        return zoomBestFitImageButton;
256     }
257  
258     /**
259      * @return Returns the zoomInImageButton.
260      */
2610    protected JButton getZoomInImageButton() {
2620        if (zoomInImageButton == null) {
2630            zoomInImageButton = new JButton("+"); //$NON-NLS-1$
264             // zoomInImageButton.setIcon(new
2650            // ImageIcon(this.getClass().getResource("/icon/22x22/actions/zoom-in.png")));
2660            zoomInImageButton.addActionListener(new ActionListener() {
267                 public void actionPerformed(ActionEvent arg0) {
268                     logger.debug("actionPerformed zoomInImageButton"); //$NON-NLS-1$
269                     getDrawingPanel().zoomIn();
270                     getImageScrollPanel().updateUI();
271                 }
272             });
2730        }
2740        return zoomInImageButton;
275     }
276  
277     /**
278      * @return Returns the zoomOutImageButton.
279      */
2800    protected JButton getZoomOutImageButton() {
2810        if (zoomOutImageButton == null) {
2820            zoomOutImageButton = new JButton("-"); //$NON-NLS-1$
283             // zoomOutImageButton.setIcon(new
2840            // ImageIcon(this.getClass().getResource("/icon/22x22/actions/zoom-out.png")));
2850            zoomOutImageButton.addActionListener(new ActionListener() {
286                 public void actionPerformed(ActionEvent arg0) {
287                     logger.debug("actionPerformed zoomOutImageButton"); //$NON-NLS-1$
288                     getDrawingPanel().zoomOut();
289                     getImageScrollPanel().updateUI();
290                 }
291             });
2920        }
2930        return zoomOutImageButton;
294     }
295  
296     /**
297      * @return Returns the drawingPanel.
298      */
2990    protected ImageDrawPanel getDrawingPanel() {
3000        if (drawingPanel == null) {
3010            drawingPanel = new ImageDrawPanel(getBufferedImage(), 10);
3020        }
3030        return drawingPanel;
304     }
305 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.