EMMA Coverage Report (generated Sun Apr 20 22:38:01 CEST 2008)
[all classes][net.sf.jomic.ui]

COVERAGE SUMMARY FOR SOURCE FILE [OpenRecentMenu.java]

nameclass, %method, %block, %line, %
OpenRecentMenu.java67%  (2/3)100% (9/9)80%  (253/316)88%  (57.4/65)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class OpenRecentMenu100% (1/1)100% (6/6)82%  (206/250)89%  (48/54)
removeAllMenuItems (): void 100% (1/1)71%  (40/56)88%  (12.3/14)
<static initializer> 100% (1/1)80%  (12/15)80%  (0.8/1)
updateMenuItems (List): void 100% (1/1)83%  (76/92)83%  (17.4/21)
OpenRecentMenu (ActionDelegate): void 100% (1/1)86%  (54/63)96%  (11.5/12)
dispose (): void 100% (1/1)100% (3/3)100% (2/2)
setEnabledUnlessEmpty (): void 100% (1/1)100% (21/21)100% (4/4)
     
class OpenRecentMenu$10%   (0/1)100% (0/0)100% (0/0)100% (0/0)
     
class OpenRecentMenu$ClearOpenRecentMenuListener100% (1/1)100% (3/3)71%  (47/66)85%  (9.4/11)
<static initializer> 100% (1/1)53%  (8/15)53%  (0.5/1)
actionPerformed (ActionEvent): void 100% (1/1)73%  (33/45)84%  (5.9/7)
OpenRecentMenu$ClearOpenRecentMenuListener (OpenRecentMenu): void 100% (1/1)100% (6/6)100% (3/3)

1// Jomic - a viewer for comic book archives.
2// Copyright (C) 2004-2008 Thomas Aglassinger
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16package net.sf.jomic.ui;
17 
18import java.awt.event.ActionEvent;
19import java.awt.event.ActionListener;
20import java.io.File;
21import java.util.ArrayList;
22import java.util.Iterator;
23import java.util.List;
24 
25import javax.swing.JMenu;
26import javax.swing.JMenuItem;
27 
28import net.sf.jomic.tools.ActionDelegate;
29import net.sf.jomic.tools.LocaleTools;
30import net.sf.wraplog.Logger;
31 
32/**
33 *  Menu to collect files recently opened.
34 *
35 * @author    Thomas Aglassinger
36 */
37public class OpenRecentMenu extends JMenu
38{
39    private static final String OPEN_RECENT = "openRecent";
40 
41    private JMenuItem clearRecentFilesItem;
42    private ActionDelegate delegate;
43    private boolean hasFiles;
44    private LocaleTools localeTools;
45    private Logger logger;
46    private List menuItems;
47 
48    OpenRecentMenu(ActionDelegate newDelegate) {
49        super("Open recent");
50        assert newDelegate != null;
51        logger = Logger.getLogger(OpenRecentMenu.class);
52        localeTools = LocaleTools.instance();
53        delegate = newDelegate;
54        menuItems = new ArrayList();
55        setText(localeTools.getMenuItemLabel(LocaleTools.MENU_FILE, OPEN_RECENT));
56        clearRecentFilesItem = localeTools.createMenuItem(LocaleTools.MENU_FILE, OPEN_RECENT + ".clearMenu");
57        clearRecentFilesItem.setActionCommand(Commands.CLEAR_RECENT);
58        clearRecentFilesItem.addActionListener(
59                new ClearOpenRecentMenuListener());
60        setEnabled(false);
61    }
62 
63    /**
64     *  Enable the menu, unless there are no files, in which case it is disabled.
65     */
66    void setEnabledUnlessEmpty() {
67        if (logger.isDebugEnabled()) {
68            logger.debug("setEnabledUnlessEmpty: " + hasFiles);
69        }
70        setEnabled(hasFiles);
71    }
72 
73    public void dispose() {
74        removeAllMenuItems();
75    }
76 
77    void updateMenuItems(final List files) {
78        assert files != null;
79        File[] fileArray = (File[]) files.toArray(new File[0]);
80 
81        removeAllMenuItems();
82 
83        int fileCount = fileArray.length;
84 
85        for (int i = 0; i < fileCount; i += 1) {
86            File file = fileArray[i];
87            JMenuItem newMenuItem = new JMenuItem(file.getName());
88 
89            menuItems.add(newMenuItem);
90 
91            ActionListener listener = new OpenRecentFileActionListener(delegate, file);
92 
93            newMenuItem.setActionCommand(Commands.OPEN_RECENT);
94            newMenuItem.addActionListener(listener);
95            add(newMenuItem);
96        }
97        hasFiles = fileCount > 0;
98        if (hasFiles) {
99            addSeparator();
100            add(clearRecentFilesItem);
101            setEnabled(true);
102        } else {
103            if (logger.isDebugEnabled()) {
104                logger.debug("disabled because there are no files");
105            }
106            setEnabled(false);
107        }
108    }
109 
110    private void removeAllMenuItems() {
111        Iterator rider = menuItems.iterator();
112 
113        while (rider.hasNext()) {
114            JMenuItem oldMenuItem = (JMenuItem) rider.next();
115            ActionListener[] oldListeners = oldMenuItem.getActionListeners();
116            int oldListenersCount = oldListeners.length;
117 
118            if (oldListenersCount > 0) {
119                assert oldListenersCount
120                        == 1 : "menu item must have 0 or 1 ActionListener, but has " + oldListenersCount;
121                oldMenuItem.removeActionListener(oldListeners[0]);
122            } else {
123                logger.warn("oldListenerCount = 0; OutOfMemoryError during menu setup?");
124            }
125        }
126        menuItems.clear();
127        removeAll();
128        hasFiles = false;
129    }
130 
131    /**
132     *  Listener to clear recent files menu.
133     *
134     * @author    Thomas Aglassinger
135     */
136    private final class ClearOpenRecentMenuListener implements ActionListener
137    {
138        private ClearOpenRecentMenuListener() {
139            // Do nothing.
140            super();
141        }
142 
143        public void actionPerformed(final ActionEvent event) {
144            assert event.getSource() == clearRecentFilesItem;
145            assert event.getActionCommand() != null;
146            assert event.getActionCommand().equals(Commands.CLEAR_RECENT);
147            removeAllMenuItems();
148            setEnabled(false);
149            delegate.actionPerformed(event);
150        }
151    }
152}

[all classes][net.sf.jomic.ui]
EMMA 2.0.4217 (C) Vladimir Roubtsov