1 /* 2 * JaLingo, http://jalingo.sourceforge.net/ 3 * 4 * Copyright (c) 2002-2006 Oleksandr Shyshko 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 */ 20 21 package ja.centre.gui.resources; 22 23 import ja.centre.util.assertions.Arguments; 24 import ja.centre.util.assertions.States; 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 28 import javax.swing.*; 29 import java.io.IOException; 30 import java.io.InputStream; 31 import java.net.URL; 32 import java.text.MessageFormat; 33 import java.util.Properties; 34 35 public class Resources { 36 public static final Log LOG = LogFactory.getLog( Resources.class ); 37 38 private static final String SUFFIX_TEXT = ".text"; 39 40 private static final String SUFFIX_KEY = ".key"; 41 42 private static final String SUFFIX_RESOURCE = ".resource"; 43 private static final String SUFFIX_ICON = ".icon"; 44 45 // TODO move this out, may be to some config? 46 public static final String RESOURCES_PACKAGE = "/resources/"; 47 48 private Class aClass; 49 private Properties properties; 50 forProperties( Class aClass )51 public static Resources forProperties( Class aClass ) { 52 return new Resources( aClass ); 53 } asString( Class aClass, String suffix )54 public static String asString( Class aClass, String suffix ) { 55 String name = aClass.getName(); 56 name = "/" + name.replace( ".", "/" ) + suffix; 57 InputStream is = aClass.getResourceAsStream( name ); 58 59 if ( is == null ) { 60 Arguments.doThrow( "Resource file file for class \"" + aClass.getName() + "\" does not exist" ); 61 } 62 63 StringBuilder builder = new StringBuilder(); 64 try { 65 byte[] buffer = new byte[32768]; 66 int read; 67 while ( (read = is.read( buffer )) != -1 ) { 68 builder.append( new String( buffer, 0, read, "UTF-8" ) ); 69 } 70 } catch ( IOException e ) { 71 States.shouldNeverReachHere( e ); 72 } finally { 73 try { 74 is.close(); 75 } catch ( IOException e ) { 76 LOG.error( "Exception occured when tried to close html resource input stream", e ); 77 } 78 } 79 return builder.toString(); 80 } 81 Resources( Class aClass )82 private Resources( Class aClass ) { 83 Arguments.assertNotNull( "aClass", aClass ); 84 85 this.properties = new Properties(); 86 this.aClass = aClass; 87 88 String path = aClass.getName(); 89 90 path = path.replace( ".", "/" ); 91 path = "/" + path + ".properties"; 92 93 InputStream resource = aClass.getResourceAsStream( path ); 94 if ( resource == null ) { 95 Arguments.doThrow( "Properties file for class \"" + aClass.getName() + "\" does not exist" ); 96 } 97 98 try { 99 properties.load( resource ); 100 } catch ( IOException e ) { 101 throw new RuntimeException( e ); 102 } 103 } 104 text( String key )105 public String text( String key ) { 106 return getValue( key + SUFFIX_TEXT ); 107 } text( String key, Object... values )108 public String text( String key, Object... values ) { 109 return MessageFormat.format( text( key ), values ); 110 } url( String key )111 public URL url( String key ) { 112 return getResourceAsUrl( getValue( key + SUFFIX_RESOURCE ) ); 113 } 114 115 // swing icon( String resourcePath )116 public ImageIcon icon( String resourcePath ) { 117 return new ImageIcon( getResourceAsUrl( 118 getValue( resourcePath + SUFFIX_ICON ) ) ); 119 } label( String key )120 public JLabel label( String key ) { 121 JLabel label = new JLabel( text( key ) ); 122 label.setFocusable( false ); 123 return label; 124 } 125 stroke( String key )126 public KeyStroke stroke( String key ) { 127 String fullKey = key + SUFFIX_KEY; 128 129 String value = getValue( fullKey ); 130 KeyStroke keyStroke = KeyStroke.getKeyStroke( value ); 131 132 if ( keyStroke == null ) { 133 Arguments.doThrow( "Incorrectly formatted key \"" 134 + fullKey + "\": \"" + value + "\"" ); 135 } 136 137 return keyStroke; 138 } 139 getResourceAsUrl( String resourcePath )140 private URL getResourceAsUrl( String resourcePath ) { 141 if ( resourcePath.startsWith( "/" ) ) 142 Arguments.doThrow( "Resource path \"" 143 + resourcePath + "\" is invalid. It must not start with \"/\"" ); 144 145 String fullPath = RESOURCES_PACKAGE + resourcePath; 146 URL url = aClass.getResource( fullPath ); 147 148 if ( url == null ) { 149 Arguments.doThrow( "Could not load resource \"" + fullPath + "\" for class \"" 150 + aClass.getName() + "\""); 151 } 152 153 return url; 154 } getValue( String fullKey )155 private String getValue( String fullKey ) { 156 String value = properties.getProperty( fullKey ); 157 if ( value == null ) { 158 Arguments.doThrow( "Could not load value for key \"" + fullKey 159 + "\" for class \"" + aClass.getName() + "\"" ); 160 } 161 return value; 162 } 163 } 164