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

COVERAGE SUMMARY FOR SOURCE FILE [OpenFromArchiveDialog.java]

nameclass, %method, %block, %line, %
OpenFromArchiveDialog.java100% (1/1)50%  (5/10)68%  (252/370)69%  (51.8/75)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class OpenFromArchiveDialog100% (1/1)50%  (5/10)68%  (252/370)69%  (51.8/75)
getComicFileNames (): String [] 0%   (0/1)0%   (0/8)0%   (0/1)
getFileNames (): String [] 0%   (0/1)0%   (0/3)0%   (0/1)
getSelectedComicIndex (): int 0%   (0/1)0%   (0/4)0%   (0/1)
isOpenComic (): boolean 0%   (0/1)0%   (0/12)0%   (0/1)
showDialog (): int 0%   (0/1)0%   (0/44)0%   (0/12)
<static initializer> 100% (1/1)80%  (12/15)80%  (0.8/1)
OpenFromArchiveDialog (JFrame, File, ComicMustContainImagesException): void 100% (1/1)83%  (191/231)85%  (35/41)
fileNamesWithoutDirectories (List): List 100% (1/1)90%  (36/40)92%  (12/13)
getDialog (): JDialog 100% (1/1)100% (3/3)100% (1/1)
getFileNamesFrom (ComicMustContainImagesException): String [] 100% (1/1)100% (10/10)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.io.File;
19import java.util.ArrayList;
20import java.util.Arrays;
21import java.util.Iterator;
22import java.util.LinkedList;
23import java.util.List;
24 
25import javax.swing.JCheckBox;
26import javax.swing.JDialog;
27import javax.swing.JFrame;
28import javax.swing.JList;
29import javax.swing.JOptionPane;
30import javax.swing.JScrollPane;
31import javax.swing.ListSelectionModel;
32 
33import net.sf.jomic.comic.ComicMustContainImagesException;
34import net.sf.jomic.tools.FileTools;
35import net.sf.jomic.tools.LocaleTools;
36import net.sf.jomic.tools.NaturalCaseInsensitiveOrderComparator;
37import net.sf.wraplog.Logger;
38 
39/**
40 *  Dialog to chose a file from a comic archive that did not contain any images rather than comic
41 *  archives.
42 *
43 * @author    Thomas Aglassinger
44 */
45public class OpenFromArchiveDialog extends JOptionPane
46{
47    private static final int FILE_LIST_VISIBLE_ROW_COUNT = 7;
48 
49    private File archiveWithoutImagesFile;
50    private List comicFileNames;
51    private JDialog dialog;
52    private JList fileList;
53    private String[] fileNames;
54    private FileTools fileTools;
55    private LocaleTools localeTools;
56    private Logger logger;
57    private JCheckBox openComicCheckBox;
58 
59    public OpenFromArchiveDialog(
60            JFrame parentFrame, File newArchiveWithoutImagesFile, ComicMustContainImagesException error) {
61        super();
62        assert newArchiveWithoutImagesFile != null;
63        assert error != null;
64 
65        logger = Logger.getLogger(OpenFromArchiveDialog.class);
66        fileTools = FileTools.instance();
67        localeTools = LocaleTools.instance();
68 
69        archiveWithoutImagesFile = newArchiveWithoutImagesFile;
70 
71        List fileNameList = fileNamesWithoutDirectories(Arrays.asList(getFileNamesFrom(error)));
72 
73        fileNames = (String[]) (fileNameList.toArray(new String[0]));
74 
75        comicFileNames = new ArrayList();
76 
77        int firstSelectableComic = -1;
78 
79        for (int i = 0; i < fileNames.length; i += 1) {
80            String fileName = fileNames[i];
81 
82            if (fileTools.isComic(fileName)) {
83                if (logger.isDebugEnabled()) {
84                    logger.debug("found comic: " + fileName);
85                }
86                comicFileNames.add(fileName);
87                if (firstSelectableComic == -1) {
88                    firstSelectableComic = i;
89                }
90            }
91        }
92 
93        String title = localeTools.getMessage("dialogs.openComicInArchive.title");
94        String optionExtractContents = localeTools.getMessage("dialogs.openComicInArchive.extractContents");
95        String optionCancel = localeTools.getCancelText();
96        String openSelectedComic = localeTools.getMessage("dialogs.openComicInArchive.openSelectedComic");
97        String[] optionsTexts = new String[]{optionExtractContents, optionCancel};
98 
99        fileList = new JList(fileNames);
100        fileList.setCellRenderer(new FileArchiveListCellRenderer());
101        fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
102        fileList.setSelectedIndex(firstSelectableComic);
103        fileList.setVisibleRowCount(Math.min(FILE_LIST_VISIBLE_ROW_COUNT, fileNames.length));
104 
105        JScrollPane fileListScrollPane = new JScrollPane(fileList);
106 
107        openComicCheckBox = new JCheckBox(openSelectedComic);
108        openComicCheckBox.setEnabled(firstSelectableComic != -1);
109        // TODO: select openComicCheckBox if specified in settings
110        openComicCheckBox.setSelected(true);
111 
112        String messageText = localeTools.getMessage(
113                "dialogs.openComicInArchive.cannotOpenComicInArchive",
114                archiveWithoutImagesFile.getAbsolutePath());
115        Object[] messageComponents = {messageText, fileListScrollPane, openComicCheckBox};
116 
117        setMessage(messageComponents);
118        setMessageType(JOptionPane.WARNING_MESSAGE);
119        setOptions(optionsTexts);
120        setOptionType(JOptionPane.YES_NO_OPTION);
121        dialog = createDialog(parentFrame, title);
122    }
123 
124    /**
125     *  Get name of files containes in archive that represent comics.
126     */
127    public String[] getComicFileNames() {
128        return (String[]) comicFileNames.toArray(new String[0]);
129    }
130 
131    public JDialog getDialog() {
132        return dialog;
133    }
134 
135    /**
136     *  Get file names contained in archive.
137     */
138    public String[] getFileNames() {
139        return fileNames;
140    }
141 
142    public int getSelectedComicIndex() {
143        return fileList.getSelectedIndex();
144    }
145 
146    public boolean isOpenComic() {
147        return openComicCheckBox.isSelected() && (getSelectedComicIndex() != -1);
148    }
149 
150    private String[] getFileNamesFrom(ComicMustContainImagesException error) {
151        String[] result = error.getFileNames();
152 
153        Arrays.sort(result, new NaturalCaseInsensitiveOrderComparator());
154        return result;
155    }
156 
157    public int showDialog() {
158        int result = CLOSED_OPTION;
159 
160        dialog.setVisible(true);
161 
162        Object selectedValue = getValue();
163 
164        if (selectedValue != null) {
165            if (getOptions() == null) {
166                if (selectedValue instanceof Integer) {
167                    result = ((Integer) selectedValue).intValue();
168                }
169            } else {
170                int optionCount = getOptions().length;
171 
172                for (int optionIndex = 0; optionIndex < optionCount; optionIndex += 1) {
173                    if (getOptions()[optionIndex].equals(selectedValue)) {
174                        result = optionIndex;
175                    }
176                }
177            }
178        }
179        return result;
180    }
181 
182    private List fileNamesWithoutDirectories(List someFileNames) {
183        List result = new LinkedList();
184        Iterator rider = someFileNames.iterator();
185 
186        while (rider.hasNext()) {
187            String fileName = (String) rider.next();
188            int fileNameLength = fileName.length();
189 
190            if (fileNameLength > 0) {
191                char lastChar = fileName.charAt(fileNameLength - 1);
192 
193                if (lastChar != File.separatorChar) {
194                    result.add(fileName);
195                }
196            } else {
197                logger.warn("ignoring empty fileName");
198            }
199        }
200        return result;
201    }
202}

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