| 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/>. |
| 16 | package net.sf.jomic.common; |
| 17 | |
| 18 | import java.awt.Image; |
| 19 | import java.awt.Toolkit; |
| 20 | import java.awt.event.ActionEvent; |
| 21 | import java.beans.PropertyChangeEvent; |
| 22 | import java.io.IOException; |
| 23 | import java.net.URL; |
| 24 | import java.text.ParseException; |
| 25 | import java.text.SimpleDateFormat; |
| 26 | import java.util.Date; |
| 27 | |
| 28 | import javax.imageio.ImageIO; |
| 29 | import javax.swing.Icon; |
| 30 | import javax.swing.ImageIcon; |
| 31 | import javax.swing.JFrame; |
| 32 | import javax.swing.JOptionPane; |
| 33 | import javax.swing.SwingUtilities; |
| 34 | |
| 35 | import net.sf.jomic.tools.ErrorTools; |
| 36 | import net.sf.jomic.tools.FileTools; |
| 37 | import net.sf.jomic.tools.LocaleTools; |
| 38 | import net.sf.jomic.tools.StringTools; |
| 39 | import net.sf.wraplog.Level; |
| 40 | import net.sf.wraplog.Logger; |
| 41 | |
| 42 | /** |
| 43 | * Jomic-specific tools. |
| 44 | * |
| 45 | * @author Thomas Aglassinger |
| 46 | */ |
| 47 | public final class JomicTools |
| 48 | { |
| 49 | private static final String LOGO_RESOURCE = "logo.gif"; |
| 50 | private static /*@ spec_public nullable @*/ JomicTools instance; |
| 51 | private Logger logger; |
| 52 | private FileTools fileTools; |
| 53 | private /*@ spec_public nullable @*/ FullScreenCancelabel fullScreenCancelabel; |
| 54 | private ErrorTools errorTools; |
| 55 | private LocaleTools localeTools; |
| 56 | private StringTools stringTools; |
| 57 | |
| 58 | private JomicTools() { |
| 59 | logger = Logger.getLogger(JomicTools.class); |
| 60 | fileTools = FileTools.instance(); |
| 61 | errorTools = ErrorTools.instance(); |
| 62 | stringTools = StringTools.instance(); |
| 63 | localeTools = LocaleTools.instance(); |
| 64 | errorTools.setTitle(JOptionPane.WARNING_MESSAGE, localeTools.getMessage("dialogs.warning.title")); |
| 65 | errorTools.setTitle(JOptionPane.WARNING_MESSAGE, localeTools.getMessage("dialogs.error.title")); |
| 66 | errorTools.setShowStackTraceText(localeTools.getMessage("dialogs.error.showInternalCallStack")); |
| 67 | errorTools.setCannotChangePropertyText(localeTools.getMessage("errors.cannotChangePropertyFromXToY")); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Make a beep, which is mostly useful to give an audible cue that some action |
| 72 | * could not be performed. |
| 73 | */ |
| 74 | public void beep() { |
| 75 | Toolkit.getDefaultToolkit().beep(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Show error message for being unable to process <code>event</code> (which may be null). |
| 80 | */ |
| 81 | public void showError(/*@ nullable @*/ ActionEvent event, Throwable error) { |
| 82 | // TODO: Disable full screen if neccessary. |
| 83 | errorTools.showError(event, error); |
| 84 | } |
| 85 | |
| 86 | public void setFullScreenCancelabel(/*@ nullable @*/ FullScreenCancelabel newFullScreebCancelabel) { |
| 87 | fullScreenCancelabel = newFullScreebCancelabel; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Set icon of <code>frame</code> to the Jomic logo. |
| 92 | * |
| 93 | * @see JFrame#setIconImage(Image) |
| 94 | */ |
| 95 | public void setIconToJomicLogo(JFrame frame) { |
| 96 | try { |
| 97 | Image logoImage = ImageIO.read(getLogoImageUrl()); |
| 98 | |
| 99 | frame.setIconImage(logoImage); |
| 100 | } catch (IOException error) { |
| 101 | // TODO: change error message to something like "cannot read internal resource" |
| 102 | IllegalStateException error2 = new IllegalStateException(localeTools.getMessage( |
| 103 | "errors.cannotFindResourceForLogoImage", LOGO_RESOURCE)); |
| 104 | |
| 105 | error2.initCause(error); |
| 106 | throw error2; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get icon with the Jomic logo. |
| 112 | */ |
| 113 | public Icon getJomicLogo() { |
| 114 | URL imageUrl = getLogoImageUrl(); |
| 115 | Icon result = new ImageIcon(imageUrl); |
| 116 | |
| 117 | return result; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get the date the current version of Jomic was released. |
| 122 | * |
| 123 | * @see Version#DATE |
| 124 | */ |
| 125 | public Date getVersionDate() { |
| 126 | SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd"); |
| 127 | Date result; |
| 128 | |
| 129 | try { |
| 130 | result = dateFormatter.parse(Version.DATE); |
| 131 | } catch (ParseException error) { |
| 132 | Object[] options = new Object[]{Version.class.getName(), Version.DATE}; |
| 133 | String message = localeTools.getMessage("errors.cannotParseVersionDate", options); |
| 134 | IllegalStateException tunneledError = new IllegalStateException(message); |
| 135 | |
| 136 | tunneledError.initCause(error); |
| 137 | throw tunneledError; |
| 138 | } |
| 139 | return result; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Get URL of Jomic logo image. |
| 144 | */ |
| 145 | private URL getLogoImageUrl() { |
| 146 | URL imageUrl = fileTools.getImageResource(LOGO_RESOURCE); |
| 147 | |
| 148 | if (imageUrl == null) { |
| 149 | throw new IllegalStateException(localeTools.getMessage( |
| 150 | "errors.cannotFindResourceForLogoImage", LOGO_RESOURCE)); |
| 151 | } |
| 152 | return imageUrl; |
| 153 | } |
| 154 | |
| 155 | //@ ensures instance != null; |
| 156 | public static synchronized JomicTools instance() { |
| 157 | if (instance == null) { |
| 158 | instance = new JomicTools(); |
| 159 | } |
| 160 | return instance; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Show error message relative to <code>owner</code>, with localized message built from <code>key</code> |
| 165 | * , caused by <code>error</code>. |
| 166 | */ |
| 167 | //@ requires key.length > 0; |
| 168 | public void showError(/*@ nullable @*/ JFrame owner, String key, /*@ nullable @*/ Throwable error) { |
| 169 | showError(owner, key, null, error); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Show error message relative to <code>owner</code>, with localized message built from <code>key</code> |
| 174 | * and <code>option</code>, caused by <code>error</code>. |
| 175 | */ |
| 176 | //@ requires key.length > 0; |
| 177 | public void showError(/*@ nullable @*/ JFrame owner, String key, Object option, |
| 178 | /*@ nullable @*/ Throwable error) { |
| 179 | Object[] options = new Object[]{option}; |
| 180 | |
| 181 | showError(owner, key, options, error); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Show error message relative to <code>owner</code>, with localized message built from <code>key</code> |
| 186 | * and <code>options</code>, caused by <code>error</code>. |
| 187 | */ |
| 188 | //@ requires key.length > 0; |
| 189 | public void showError(/*@ nullable @*/ JFrame owner, String key, Object[] options, |
| 190 | /*@ nullable @*/ Throwable error) { |
| 191 | String message = localeTools.getMessage(key, options); |
| 192 | |
| 193 | showErrorMessage(owner, key, message, error); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Show error message for being unable to change property according to <code>event</code>. |
| 198 | */ |
| 199 | public void showError(PropertyChangeEvent event, Throwable error) { |
| 200 | // TODO: Disable full screen. |
| 201 | errorTools.showError(event, error); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Show warning message relative to <code>owner</code>, with localized message built from |
| 206 | * <code>key</code> and <code>option</code>, caused by <code>error</code>. |
| 207 | */ |
| 208 | //@ requires key.length > 0; |
| 209 | public void showWarning(/*@ nullable @*/ JFrame owner, String key, Object option, |
| 210 | /*@ nullable @*/ Throwable error) { |
| 211 | Object[] options = new Object[]{option}; |
| 212 | |
| 213 | showWarning(owner, key, options, error); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Show warning message relative to <code>owner</code>, with localized message built from |
| 218 | * <code>key</code> and <code>options</code>, caused by <code>error</code>. |
| 219 | */ |
| 220 | //@ requires key.length > 0; |
| 221 | public void showWarning(/*@ nullable @*/ JFrame owner, String key, Object[] options, |
| 222 | /*@ nullable @*/ Throwable error) { |
| 223 | String message = localeTools.getMessage(key, options); |
| 224 | |
| 225 | showWarningMessage(owner, key, message, error); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Show warning message relative to <code>owner</code>, with localized message built from |
| 230 | * <code>key</code>, caused by <code>error</code>. |
| 231 | */ |
| 232 | //@ requires key.length > 0; |
| 233 | public void showWarning(/*@ nullable @*/ JFrame owner, String key, /*@ nullable @*/ Throwable error) { |
| 234 | String message = localeTools.getMessage(key); |
| 235 | |
| 236 | showWarningMessage(owner, key, message, error); |
| 237 | } |
| 238 | |
| 239 | //@ requires key.length > 0; |
| 240 | private void showErrorMessage(/*@ nullable @*/ JFrame owner, String key, String message, |
| 241 | /*@ nullable @*/ Throwable error) { |
| 242 | showMessage(owner, JOptionPane.ERROR_MESSAGE, key, message, error); |
| 243 | } |
| 244 | |
| 245 | //@ requires key.length > 0; |
| 246 | private void showMessage(final /*@ nullable @*/ JFrame owner, final int messageType, String key, String message, |
| 247 | /*@ nullable @*/ final Throwable error) { |
| 248 | int logLevel; |
| 249 | |
| 250 | if (messageType == JOptionPane.WARNING_MESSAGE) { |
| 251 | logLevel = Level.WARN; |
| 252 | } else { |
| 253 | assert messageType == JOptionPane.ERROR_MESSAGE : "messageType=" + messageType; |
| 254 | logLevel = Level.ERROR; |
| 255 | } |
| 256 | |
| 257 | logger.log(logLevel, message + "[key=" + key + "]", error); |
| 258 | |
| 259 | final String errorMessage = stringTools.titled(message); |
| 260 | |
| 261 | if ((fullScreenCancelabel != null) && fullScreenCancelabel.isVisible()) { |
| 262 | SwingUtilities.invokeLater(new Runnable() { |
| 263 | public void run() { |
| 264 | try { |
| 265 | fullScreenCancelabel.performCancelAndWait(); |
| 266 | } catch (Exception fullScreenCancelError) { |
| 267 | logger.warn("cannot cancel full screen properly to show some message", fullScreenCancelError); |
| 268 | } |
| 269 | errorTools.showMessage(owner, messageType, errorMessage, error, true); |
| 270 | } |
| 271 | }); |
| 272 | } else { |
| 273 | errorTools.showMessage(owner, messageType, errorMessage, error, true); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | //@ requires key.length > 0; |
| 278 | private void showWarningMessage(/*@ nullable @*/ JFrame owner, String key, String message, |
| 279 | /*@ nullable @*/ Throwable error) { |
| 280 | showMessage(owner, JOptionPane.WARNING_MESSAGE, key, message, error); |
| 281 | } |
| 282 | } |