ResourceManager.java revision 13978:1993af50385d
1/* 2 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23package com.sun.swingset3.demos; 24 25import java.net.URL; 26import java.util.MissingResourceException; 27import java.util.ResourceBundle; 28import java.util.logging.Level; 29import java.util.logging.Logger; 30import javax.swing.*; 31 32/** 33 * @author Pavel Porvatov 34 */ 35public class ResourceManager { 36 37 private static final Logger logger = Logger.getLogger(ResourceManager.class.getName()); 38 39 private final Class<?> demoClass; 40 41 // Resource bundle for internationalized and accessible text 42 private ResourceBundle bundle = null; 43 44 public ResourceManager(Class<?> demoClass) { 45 this.demoClass = demoClass; 46 47 String bundleName = demoClass.getPackage().getName() + ".resources." + demoClass.getSimpleName(); 48 49 try { 50 bundle = ResourceBundle.getBundle(bundleName); 51 } catch (MissingResourceException e) { 52 logger.log(Level.SEVERE, "Couldn't load bundle: " + bundleName); 53 } 54 } 55 56 public String getString(String key) { 57 return bundle != null ? bundle.getString(key) : key; 58 } 59 60 public char getMnemonic(String key) { 61 return (getString(key)).charAt(0); 62 } 63 64 public ImageIcon createImageIcon(String filename, String description) { 65 String path = "resources/images/" + filename; 66 67 URL imageURL = demoClass.getResource(path); 68 69 if (imageURL == null) { 70 logger.log(Level.SEVERE, "unable to access image file: " + path); 71 72 return null; 73 } else { 74 return new ImageIcon(imageURL, description); 75 } 76 } 77} 78