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.awt.BorderLayout; |
19 | import java.awt.Dimension; |
20 | import java.awt.GridBagConstraints; |
21 | import java.awt.GridBagLayout; |
22 | import java.awt.event.ActionEvent; |
23 | import java.awt.event.ActionListener; |
24 | import java.awt.event.WindowEvent; |
25 | import java.awt.event.WindowListener; |
26 | |
27 | import javax.swing.BorderFactory; |
28 | import javax.swing.JFrame; |
29 | import javax.swing.JLabel; |
30 | import javax.swing.JPanel; |
31 | import javax.swing.JScrollPane; |
32 | import javax.swing.JTable; |
33 | import javax.swing.Timer; |
34 | |
35 | import net.sf.jomic.comic.ComicCache; |
36 | import net.sf.jomic.common.JomicTools; |
37 | import net.sf.jomic.tools.ArchiveCache; |
38 | import net.sf.jomic.tools.CacheInfo; |
39 | import net.sf.jomic.tools.LocaleTools; |
40 | import net.sf.jomic.tools.SystemTools; |
41 | import net.sf.jomic.tools.UiTools; |
42 | |
43 | /** |
44 | * Frame to show information about environment, relevant version numbers, and supported image |
45 | * formats. |
46 | * |
47 | * @author Thomas Aglassinger |
48 | */ |
49 | public class SystemInfoFrame extends JFrame implements ActionListener, WindowListener |
50 | { |
51 | private static final int INSET = 4; |
52 | private static final String KEY_ARCHIVE_CACHE = "dialogs.systemInformation.archiveCache"; |
53 | private static final String KEY_IMAGE_CACHE = "dialogs.systemInformation.imageCache"; |
54 | private static final String KEY_MEMORY = "dialogs.systemInformation.memory"; |
55 | private static final String KEY_THUMB_CACHE = "dialogs.systemInformation.thumbCache"; |
56 | private static final int MEMORY_INFO_REFRESH_DELAY = 1000; |
57 | private static final int PREFERRED_TABLE_HEIGHT = 150; |
58 | private static final int PREFERRED_TABLE_WIDTH = 500; |
59 | private JLabel archiveCacheInfoLabel; |
60 | private JLabel imageCacheInfoLabel; |
61 | private JomicTools jomicTools; |
62 | private LocaleTools localeTools; |
63 | private JLabel memoryInfoLabel; |
64 | private Timer memoryInfoUpdateTimer; |
65 | private SystemTools systemTools; |
66 | private JLabel thumbCacheInfoLabel; |
67 | private UiTools uiTools; |
68 | |
69 | public SystemInfoFrame() { |
70 | jomicTools = JomicTools.instance(); |
71 | localeTools = LocaleTools.instance(); |
72 | systemTools = SystemTools.instance(); |
73 | uiTools = UiTools.instance(); |
74 | memoryInfoUpdateTimer = new Timer(MEMORY_INFO_REFRESH_DELAY, this); |
75 | memoryInfoUpdateTimer.setInitialDelay(0); |
76 | |
77 | setTitle(localeTools.getMessage("dialogs.systemInformation.title")); |
78 | getContentPane().add(createUI()); |
79 | jomicTools.setIconToJomicLogo(this); |
80 | addWindowListener(this); |
81 | } |
82 | |
83 | public void setVisible(boolean isVisible) { |
84 | if (isVisible) { |
85 | updateMemoryStatus(); |
86 | } |
87 | super.setVisible(isVisible); |
88 | } |
89 | |
90 | private String getCacheDescription(CacheInfo usage) { |
91 | return getCacheDescription(usage.getUsedSize(), usage.getMaxSize(), usage.getEntryCount()); |
92 | } |
93 | |
94 | private String getCacheDescription(long bytesUsed, long bytesAvailable, int entriesUsed) { |
95 | String memoryUsed = localeTools.asByteText(bytesUsed); |
96 | String memoryAvailable = localeTools.asByteText(bytesAvailable); |
97 | String result = localeTools.getMessage( |
98 | "dialogs.systemInformation.xOfYUsedByZEntries", |
99 | new Object[]{memoryUsed, memoryAvailable, new Integer(entriesUsed)}); |
100 | |
101 | return result; |
102 | } |
103 | |
104 | public void actionPerformed(ActionEvent event) { |
105 | updateMemoryStatus(); |
106 | } |
107 | |
108 | public void windowActivated(WindowEvent event) { |
109 | // Do nothing. |
110 | } |
111 | |
112 | public void windowClosed(WindowEvent event) { |
113 | // Do nothing. |
114 | } |
115 | |
116 | public void windowClosing(WindowEvent event) { |
117 | memoryInfoUpdateTimer.stop(); |
118 | } |
119 | |
120 | public void windowDeactivated(WindowEvent event) { |
121 | // Do nothing. |
122 | } |
123 | |
124 | public void windowDeiconified(WindowEvent event) { |
125 | memoryInfoUpdateTimer.restart(); |
126 | } |
127 | |
128 | public void windowIconified(WindowEvent event) { |
129 | memoryInfoUpdateTimer.stop(); |
130 | } |
131 | |
132 | public void windowOpened(WindowEvent event) { |
133 | memoryInfoUpdateTimer.restart(); |
134 | } |
135 | |
136 | private JPanel createImageFormatPanel() { |
137 | JPanel result = new JPanel(); |
138 | |
139 | result.setLayout(new BorderLayout()); |
140 | |
141 | JTable formatTable = new JTable(new ImageFormatTableModel()); |
142 | |
143 | uiTools.initColumnWidths(formatTable); |
144 | // TODO: compute dimension depending on data |
145 | formatTable.setPreferredScrollableViewportSize( |
146 | new Dimension(PREFERRED_TABLE_WIDTH, PREFERRED_TABLE_HEIGHT)); |
147 | result.add(new JLabel( |
148 | localeTools.getMessage("dialogs.systemInformation.supportedImageFormats")), |
149 | BorderLayout.NORTH); |
150 | result.add(new JScrollPane(formatTable), BorderLayout.CENTER); |
151 | return result; |
152 | } |
153 | |
154 | private JPanel createPlatformInfoPanel() { |
155 | JPanel result = new JPanel(); |
156 | String platform = systemTools.getPlatformInfo(); |
157 | String language = systemTools.getLocaleInfo(); |
158 | String javaVersion = systemTools.getJavaInfo(); |
159 | |
160 | String[] keyValues = new String[]{ |
161 | "dialogs.systemInformation.platform", platform, |
162 | "dialogs.systemInformation.language", language, |
163 | "dialogs.systemInformation.javaVersion", javaVersion, |
164 | KEY_MEMORY, "", |
165 | KEY_IMAGE_CACHE, "", |
166 | KEY_THUMB_CACHE, "", |
167 | KEY_ARCHIVE_CACHE, "" |
168 | }; |
169 | GridBagConstraints constraints = new GridBagConstraints(); |
170 | |
171 | constraints.anchor = GridBagConstraints.LINE_START; |
172 | result.setLayout(new GridBagLayout()); |
173 | for (int i = 0; i < keyValues.length; i += 2) { |
174 | String key = keyValues[i]; |
175 | String value = keyValues[i + 1]; |
176 | String message = localeTools.getMessage(key); |
177 | |
178 | constraints.gridx = 0; |
179 | constraints.gridy = i / 2; |
180 | result.add(new JLabel(message), constraints); |
181 | constraints.gridx += 1; |
182 | |
183 | JLabel infoLabel = new JLabel(value); |
184 | |
185 | result.add(infoLabel, constraints); |
186 | if (key.equals(KEY_ARCHIVE_CACHE)) { |
187 | archiveCacheInfoLabel = infoLabel; |
188 | } else if (key.equals(KEY_IMAGE_CACHE)) { |
189 | imageCacheInfoLabel = infoLabel; |
190 | } else if (key.equals(KEY_MEMORY)) { |
191 | memoryInfoLabel = infoLabel; |
192 | } else if (key.equals(KEY_THUMB_CACHE)) { |
193 | thumbCacheInfoLabel = infoLabel; |
194 | } |
195 | } |
196 | |
197 | return result; |
198 | } |
199 | |
200 | private JPanel createUI() { |
201 | JPanel result = new JPanel(); |
202 | |
203 | result.setLayout(new BorderLayout()); |
204 | |
205 | JPanel platformInfoPane = createPlatformInfoPanel(); |
206 | JPanel imageFormatPane = createImageFormatPanel(); |
207 | |
208 | platformInfoPane.setBorder(BorderFactory.createEmptyBorder(2 * INSET, 2 * INSET, INSET, 2 * INSET)); |
209 | imageFormatPane.setBorder(BorderFactory.createEmptyBorder(INSET, 2 * INSET, INSET, 2 * INSET)); |
210 | result.add(platformInfoPane, BorderLayout.NORTH); |
211 | result.add(imageFormatPane, BorderLayout.CENTER); |
212 | return result; |
213 | } |
214 | |
215 | private void updateMemoryStatus() { |
216 | assert archiveCacheInfoLabel != null; |
217 | assert imageCacheInfoLabel != null; |
218 | assert memoryInfoLabel != null; |
219 | assert thumbCacheInfoLabel != null; |
220 | |
221 | ComicCache cache = ComicCache.instance(); |
222 | long freeMemory = Runtime.getRuntime().freeMemory(); |
223 | long availableMemory = Runtime.getRuntime().maxMemory(); |
224 | long usedMemory = availableMemory - freeMemory; |
225 | Object[] options = new Object[]{localeTools.asByteText(usedMemory), localeTools.asByteText(availableMemory)}; |
226 | String memoryStatus = localeTools.getMessage( |
227 | "dialogs.systemInformation.xOfYUsed", options); |
228 | ArchiveCache archiveCache = cache.getArchiveCache(); |
229 | |
230 | memoryInfoLabel.setText(memoryStatus); |
231 | imageCacheInfoLabel.setText(getCacheDescription(cache.getImageCache())); |
232 | thumbCacheInfoLabel.setText(getCacheDescription(cache.getThumbCache())); |
233 | archiveCacheInfoLabel.setText(getCacheDescription(archiveCache)); |
234 | } |
235 | } |