EMMA Coverage Report (generated Tue May 18 22:20:04 CDT 2004)
[all classes][default package]

COVERAGE SUMMARY FOR SOURCE FILE [FileChooserDemo.java]

nameclass, %method, %block, %line, %
FileChooserDemo.java100% (7/7)62%  (18/29)45%  (376/835)49%  (80/162)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class FileChooserDemo$4100% (1/1)50%  (1/2)3%   (7/270)2%   (1/43)
actionPerformed (ActionEvent): void 0%   (0/1)0%   (0/263)0%   (0/42)
FileChooserDemo$4 (FileChooserDemo, String): void 100% (1/1)100% (7/7)100% (1/1)
     
class FileChooserDemo$2100% (1/1)50%  (1/2)27%  (7/26)17%  (1/6)
actionPerformed (ActionEvent): void 0%   (0/1)0%   (0/19)0%   (0/5)
FileChooserDemo$2 (FileChooserDemo, String): void 100% (1/1)100% (7/7)100% (1/1)
     
class FilePreviewer100% (1/1)75%  (3/4)31%  (36/116)41%  (12/29)
loadImage (File): void 0%   (0/1)0%   (0/32)0%   (0/7)
paint (Graphics): void 100% (1/1)16%  (7/45)30%  (3/10)
propertyChange (PropertyChangeEvent): void 100% (1/1)41%  (7/17)50%  (3/6)
FilePreviewer (JFileChooser): void 100% (1/1)100% (22/22)100% (6/6)
     
class FileChooserDemo100% (1/1)47%  (7/15)71%  (222/313)73%  (47/64)
createAboutAction (): Action 0%   (0/1)0%   (0/8)0%   (0/1)
createCancelAction (): Action 0%   (0/1)0%   (0/8)0%   (0/1)
createFindAction (): Action 0%   (0/1)0%   (0/14)0%   (0/2)
createHelpAction (): Action 0%   (0/1)0%   (0/14)0%   (0/2)
createImageButton (Action): JButton 0%   (0/1)0%   (0/16)0%   (0/3)
createOKAction (): Action 0%   (0/1)0%   (0/8)0%   (0/1)
loadImage (String): void 0%   (0/1)0%   (0/9)0%   (0/2)
main (String []): void 0%   (0/1)0%   (0/8)0%   (0/3)
createFileChooser (): JFileChooser 100% (1/1)70%  (14/20)67%  (4/6)
FileChooserDemo (SwingSet2): void 100% (1/1)100% (8/8)100% (3/3)
createButton (Action): JButton 100% (1/1)100% (8/8)100% (2/2)
createCustomFileChooserButton (): JButton 100% (1/1)100% (12/12)100% (2/2)
createFileChooserDemo (): void 100% (1/1)100% (156/156)100% (32/32)
createPlainFileChooserButton (): JButton 100% (1/1)100% (12/12)100% (2/2)
createPreviewFileChooserButton (): JButton 100% (1/1)100% (12/12)100% (2/2)
     
class FileChooserDemo$3100% (1/1)100% (2/2)92%  (69/75)93%  (13/14)
actionPerformed (ActionEvent): void 100% (1/1)91%  (62/68)92%  (12/13)
FileChooserDemo$3 (FileChooserDemo, String): void 100% (1/1)100% (7/7)100% (1/1)
     
class FileChooserDemo$1100% (1/1)100% (2/2)100% (16/16)100% (2/2)
FileChooserDemo$1 (FileChooserDemo): void 100% (1/1)100% (6/6)100% (1/1)
getMaximumSize (): Dimension 100% (1/1)100% (10/10)100% (1/1)
     
class FileChooserDemo$10100% (1/1)100% (2/2)100% (19/19)100% (4/4)
FileChooserDemo$10 (FileChooserDemo, Action): void 100% (1/1)100% (7/7)100% (1/1)
getMaximumSize (): Dimension 100% (1/1)100% (12/12)100% (3/3)

1/*
2 * Copyright (c) 2003 Sun Microsystems, Inc. All  Rights Reserved.
3 * 
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 
8 * -Redistributions of source code must retain the above copyright
9 *  notice, this list of conditions and the following disclaimer.
10 * 
11 * -Redistribution in binary form must reproduct the above copyright
12 *  notice, this list of conditions and the following disclaimer in
13 *  the documentation and/or other materials provided with the distribution.
14 * 
15 * Neither the name of Sun Microsystems, Inc. or the names of contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 * 
19 * This software is provided "AS IS," without a warranty of any kind. ALL
20 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
21 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
22 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
23 * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
24 * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
25 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
26 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
27 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
28 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
29 * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
30 * 
31 * You acknowledge that Software is not designed, licensed or intended for
32 * use in the design, construction, operation or maintenance of any nuclear
33 * facility.
34 */
35 
36/*
37 * @(#)FileChooserDemo.java        1.13 03/01/23
38 */
39 
40 
41import javax.swing.*;
42import javax.swing.event.*;
43import javax.swing.text.*;
44import javax.swing.border.*;
45import javax.swing.colorchooser.*;
46import javax.swing.filechooser.*;
47import javax.accessibility.*;
48 
49import java.awt.*;
50import java.awt.event.*;
51import java.beans.*;
52import java.util.*;
53import java.io.*;
54import java.applet.*;
55import java.net.*;
56 
57/**
58 * JFileChooserDemo
59 *
60 * @version 1.1 07/16/99
61 * @author Jeff Dinkins
62 */
63public class FileChooserDemo extends DemoModule {
64    JLabel theImage;
65    Icon jpgIcon; 
66    Icon gifIcon;
67 
68    /**
69     * main method allows us to run as a standalone demo.
70     */
71    public static void main(String[] args) {
72        FileChooserDemo demo = new FileChooserDemo(null);
73        demo.mainImpl();
74    }
75 
76    /**
77     * FileChooserDemo Constructor
78     */
79    public FileChooserDemo(SwingSet2 swingset) {
80        // Set the title for this demo, and an icon used to represent this
81        // demo inside the SwingSet2 app.
82        super(swingset, "FileChooserDemo", "toolbar/JFileChooser.gif");
83        createFileChooserDemo();
84    }
85 
86    public void createFileChooserDemo() {
87        theImage = new JLabel("");
88        jpgIcon = createImageIcon("filechooser/jpgIcon.jpg", "jpg image");
89        gifIcon = createImageIcon("filechooser/gifIcon.gif", "gif image");
90 
91        JPanel demoPanel = getDemoPanel();
92        demoPanel.setLayout(new BoxLayout(demoPanel, BoxLayout.Y_AXIS));
93 
94        JPanel innerPanel = new JPanel();
95        innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.X_AXIS));
96 
97        demoPanel.add(Box.createRigidArea(VGAP20));
98        demoPanel.add(innerPanel);
99        demoPanel.add(Box.createRigidArea(VGAP20));
100 
101        innerPanel.add(Box.createRigidArea(HGAP20));
102 
103        // Create a panel to hold buttons
104        JPanel buttonPanel = new JPanel() {
105            public Dimension getMaximumSize() {
106                return new Dimension(getPreferredSize().width, super.getMaximumSize().height);
107            }
108        };
109        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
110 
111        buttonPanel.add(Box.createRigidArea(VGAP15));
112        buttonPanel.add(createPlainFileChooserButton());
113        buttonPanel.add(Box.createRigidArea(VGAP15));
114        buttonPanel.add(createPreviewFileChooserButton());
115        buttonPanel.add(Box.createRigidArea(VGAP15));
116        buttonPanel.add(createCustomFileChooserButton());
117        buttonPanel.add(Box.createVerticalGlue());
118 
119        // Create a panel to hold the image
120        JPanel imagePanel = new JPanel();
121        imagePanel.setLayout(new BorderLayout());
122        imagePanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
123        JScrollPane scroller = new JScrollPane(theImage);
124        scroller.getVerticalScrollBar().setUnitIncrement(10);
125        scroller.getHorizontalScrollBar().setUnitIncrement(10);
126        imagePanel.add(scroller, BorderLayout.CENTER);
127 
128        // add buttons and image panels to inner panel
129        innerPanel.add(buttonPanel);
130        innerPanel.add(Box.createRigidArea(HGAP30));
131        innerPanel.add(imagePanel);
132        innerPanel.add(Box.createRigidArea(HGAP20));
133    }
134 
135    public JFileChooser createFileChooser() {
136        // create a filechooser
137        JFileChooser fc = new JFileChooser();
138        
139        // set the current directory to be the images directory
140        File swingFile = new File("resources/images/About.jpg");
141        if(swingFile.exists()) {
142            fc.setCurrentDirectory(swingFile);
143            fc.setSelectedFile(swingFile);
144        }
145        
146        return fc;
147    }
148 
149 
150    public JButton createPlainFileChooserButton() {
151        Action a = new AbstractAction(getString("FileChooserDemo.plainbutton")) {
152            public void actionPerformed(ActionEvent e) {
153                JFileChooser fc = createFileChooser();
154 
155                // show the filechooser
156                int result = fc.showOpenDialog(getDemoPanel());
157                
158                // if we selected an image, load the image
159                if(result == JFileChooser.APPROVE_OPTION) {
160                    loadImage(fc.getSelectedFile().getPath());
161                }
162            }
163        };
164        return createButton(a);
165    }
166 
167    public JButton createPreviewFileChooserButton() {
168        Action a = new AbstractAction(getString("FileChooserDemo.previewbutton")) {
169            public void actionPerformed(ActionEvent e) {
170                JFileChooser fc = createFileChooser();
171 
172                // Add filefilter & fileview
173                ExampleFileFilter filter = new ExampleFileFilter(
174                    new String[] {"jpg", "gif"}, getString("FileChooserDemo.filterdescription")
175                );
176                ExampleFileView fileView = new ExampleFileView();
177                fileView.putIcon("jpg", jpgIcon);
178                fileView.putIcon("gif", gifIcon);
179                fc.setFileView(fileView);
180                fc.addChoosableFileFilter(filter);
181                fc.setFileFilter(filter);
182                
183                // add preview accessory
184                fc.setAccessory(new FilePreviewer(fc));
185 
186                // show the filechooser
187                int result = fc.showOpenDialog(getDemoPanel());
188                
189                // if we selected an image, load the image
190                if(result == JFileChooser.APPROVE_OPTION) {
191                    loadImage(fc.getSelectedFile().getPath());
192                }
193            }
194        };
195        return createButton(a);
196    }
197 
198    JDialog dialog;
199    JFileChooser fc;
200 
201    public JButton createCustomFileChooserButton() {
202        Action a = new AbstractAction(getString("FileChooserDemo.custombutton")) {
203            public void actionPerformed(ActionEvent e) {
204                fc = createFileChooser();
205 
206                // Add filefilter & fileview
207                ExampleFileFilter filter = new ExampleFileFilter(
208                    new String[] {"jpg", "gif"}, getString("FileChooserDemo.filterdescription")
209                );
210                ExampleFileView fileView = new ExampleFileView();
211                fileView.putIcon("jpg", jpgIcon);
212                fileView.putIcon("gif", gifIcon);
213                fc.setFileView(fileView);
214                fc.addChoosableFileFilter(filter);
215 
216                // add preview accessory
217                fc.setAccessory(new FilePreviewer(fc));
218 
219                // remove the approve/cancel buttons
220                fc.setControlButtonsAreShown(false);
221 
222                // make custom controls
223                //wokka
224                JPanel custom = new JPanel();
225                custom.setLayout(new BoxLayout(custom, BoxLayout.Y_AXIS));
226                custom.add(Box.createRigidArea(VGAP10));
227                JLabel description = new JLabel(getString("FileChooserDemo.description"));
228                description.setAlignmentX(JLabel.CENTER_ALIGNMENT);
229                custom.add(description);
230                custom.add(Box.createRigidArea(VGAP10));
231                custom.add(fc);
232 
233                Action okAction = createOKAction();
234                fc.addActionListener(okAction);
235 
236                JPanel buttons = new JPanel();
237                buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
238                buttons.add(Box.createRigidArea(HGAP10));
239                buttons.add(createImageButton(createFindAction()));
240                buttons.add(Box.createRigidArea(HGAP10));
241                buttons.add(createButton(createAboutAction()));
242                buttons.add(Box.createRigidArea(HGAP10));
243                buttons.add(createButton(okAction));
244                buttons.add(Box.createRigidArea(HGAP10));
245                buttons.add(createButton(createCancelAction()));
246                buttons.add(Box.createRigidArea(HGAP10));
247                buttons.add(createImageButton(createHelpAction()));
248                buttons.add(Box.createRigidArea(HGAP10));
249 
250                custom.add(buttons);
251                custom.add(Box.createRigidArea(VGAP10));
252                
253                // show the filechooser
254                Frame parent = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, getDemoPanel());
255                dialog = new JDialog(parent, getString("FileChooserDemo.dialogtitle"), true);
256                dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
257                dialog.getContentPane().add(custom, BorderLayout.CENTER);
258                dialog.pack();
259                dialog.setLocationRelativeTo(getDemoPanel());
260                dialog.show();
261            }
262        };
263        return createButton(a);
264    }
265 
266    public Action createAboutAction() {
267        return new AbstractAction(getString("FileChooserDemo.about")) {
268            public void actionPerformed(ActionEvent e) {
269                File file = fc.getSelectedFile();
270                String text;
271                if(file == null) {
272                    text = getString("FileChooserDemo.nofileselected");
273                } else {
274                    text = "<html>" + getString("FileChooserDemo.thefile");
275                    text += "<br><font color=green>" + file.getName() + "</font><br>";
276                    text += getString("FileChooserDemo.isprobably") + "</html>";
277                }
278                JOptionPane.showMessageDialog(getDemoPanel(), text);
279            }
280        };
281    }
282 
283    public Action createOKAction() {
284        return new AbstractAction(getString("FileChooserDemo.ok")) {
285            public void actionPerformed(ActionEvent e) {
286                dialog.dispose();
287                if (!e.getActionCommand().equals(JFileChooser.CANCEL_SELECTION)
288                    && fc.getSelectedFile() != null) {
289 
290                    loadImage(fc.getSelectedFile().getPath());
291                }
292            }
293        };
294    }
295 
296    public Action createCancelAction() {
297        return new AbstractAction(getString("FileChooserDemo.cancel")) {
298            public void actionPerformed(ActionEvent e) {
299                dialog.dispose();
300            }
301        };
302    }
303 
304    public Action createFindAction() {
305        Icon icon = createImageIcon("filechooser/find.gif", getString("FileChooserDemo.find"));
306        return new AbstractAction("", icon) {
307            public void actionPerformed(ActionEvent e) {
308                String result = JOptionPane.showInputDialog(getDemoPanel(), getString("FileChooserDemo.findquestion"));
309                if (result != null) {
310                    JOptionPane.showMessageDialog(getDemoPanel(), getString("FileChooserDemo.findresponse"));
311                }
312            }
313        };
314    }
315 
316    public Action createHelpAction() {
317        Icon icon = createImageIcon("filechooser/help.gif", getString("FileChooserDemo.help"));
318        return new AbstractAction("", icon) {
319            public void actionPerformed(ActionEvent e) {
320                JOptionPane.showMessageDialog(getDemoPanel(), getString("FileChooserDemo.helptext"));
321            }
322        };
323    }
324    
325    class MyImageIcon extends ImageIcon {
326        public MyImageIcon(String filename) {
327            super(filename);
328        };
329        public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
330            g.setColor(Color.white);
331            g.fillRect(0,0, c.getWidth(), c.getHeight());
332            if(getImageObserver() == null) {
333                g.drawImage(
334                    getImage(),
335                    c.getWidth()/2 - getIconWidth()/2,
336                    c.getHeight()/2 - getIconHeight()/2,
337                    c
338                );
339            } else {
340                g.drawImage(
341                    getImage(),
342                    c.getWidth()/2 - getIconWidth()/2,
343                    c.getHeight()/2 - getIconHeight()/2,
344                    getImageObserver()
345                );
346            }
347        }
348    }
349 
350    public void loadImage(String filename) {
351        theImage.setIcon(new MyImageIcon(filename));
352    }
353 
354    public JButton createButton(Action a) {
355        JButton b = new JButton(a) {
356            public Dimension getMaximumSize() {
357                int width = Short.MAX_VALUE;
358                int height = super.getMaximumSize().height;
359                return new Dimension(width, height);
360            }
361        };
362        return b;
363    }
364 
365    public JButton createImageButton(Action a) {
366        JButton b = new JButton(a);
367        b.setMargin(new Insets(0,0,0,0));
368        return b;
369    }
370}
371 
372class FilePreviewer extends JComponent implements PropertyChangeListener {
373    ImageIcon thumbnail = null;
374    
375    public FilePreviewer(JFileChooser fc) {
376        setPreferredSize(new Dimension(100, 50));
377        fc.addPropertyChangeListener(this);
378        setBorder(new BevelBorder(BevelBorder.LOWERED));
379    }
380    
381    public void loadImage(File f) {
382        if (f == null) {
383            thumbnail = null;
384        } else {
385            ImageIcon tmpIcon = new ImageIcon(f.getPath());
386            if(tmpIcon.getIconWidth() > 90) {
387                thumbnail = new ImageIcon(
388                    tmpIcon.getImage().getScaledInstance(90, -1, Image.SCALE_DEFAULT));
389            } else {
390                thumbnail = tmpIcon;
391            }
392        }
393    }
394    
395    public void propertyChange(PropertyChangeEvent e) {
396        String prop = e.getPropertyName();
397        if(prop == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) {
398            if(isShowing()) {
399                loadImage((File) e.getNewValue());
400                repaint();
401            }
402        }
403    }
404    
405    public void paint(Graphics g) {
406        super.paint(g);
407        if(thumbnail != null) {
408            int x = getWidth()/2 - thumbnail.getIconWidth()/2;
409            int y = getHeight()/2 - thumbnail.getIconHeight()/2;
410            if(y < 0) {
411                y = 0;
412            }
413            
414            if(x < 5) {
415                x = 5;
416            }
417            thumbnail.paintIcon(this, g, x, y);
418        }
419    }
420}
421 

[all classes][default package]
EMMA 2.0.4015 (stable) (C) Vladimir Roubtsov