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