📄 resourcemanager.java
字号:
try{
is.close();
}catch(IOException e) {
e.printStackTrace();
}
return o;
}
else {
try{
return PropertiesConverter.load( filePath );
}
catch( PersistenceException pe ) {
Debug.signal( Debug.ERROR, this, ""+pe );
return null;
}
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To save an object to a property file. The filePath MUST be corresponding to an
* external path.
*
* @param o object to save.
* @param fileName a file name that has been appended to the end of a directory path
* provided by this ResourceManager.
*/
public boolean saveObject( Object o, String filePath ) {
try{
PropertiesConverter.save( o, filePath );
return true;
}
catch( PersistenceException pe ) {
Debug.signal( Debug.ERROR, this, pe );
return false;
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To save an object in a binary file
*
* @param o
* @param filePath
* @return
*/
public boolean BackupObject( Object o, String filePath ) {
try{
PropertiesConverter.Backup( o, filePath );
return true;
}
catch( PersistenceException pe ) {
Debug.signal( Debug.ERROR, this, pe );
return false;
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To load text from a file. The filePath MUST be corresponding to a valid resource
* path.
*
* @param fileName a file name that has been appended to the end of a directory path
* provided by this ResourceManager.
*/
public String loadText( String filePath ) {
if( inJar && !isExternal(filePath) ) {
InputStream is = this.getClass().getResourceAsStream( filePath );
if(is==null) return "";
return FileTools.loadTextFromStream( is );
}
else
return FileTools.loadTextFromFile( filePath );
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To save text to a file. The filePath MUST be corresponding to an external path.
*
* @param fileName a file name that has been appended to the end of a directory path
* provided by this ResourceManager.
* @param text text to save.
*/
public boolean saveText( String filePath, String text ) {
return FileTools.saveTextToFile( filePath, text );
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To load properties from a file. The filePath MUST be corresponding to a valid resource
* path.
*
* @param fileName a file name that has been appended to the end of a directory path
* provided by this ResourceManager.
*/
public Properties loadProperties( String filePath ) {
if( inJar && !isExternal(filePath) ) {
InputStream is = this.getClass().getResourceAsStream( filePath );
if(is==null) return new Properties();
return FileTools.loadPropertiesFromStream( is );
}
else
return FileTools.loadPropertiesFile( filePath );
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To save text to a file. The filePath MUST be corresponding to an external path.
*
* @param props properties to save.
* @param fileName a file name that has been appended to the end of a directory path
* provided by this ResourceManager.
* @param header some text to display as header in the file.
*/
public boolean saveProperties( Properties props, String filePath, String header ) {
return FileTools.savePropertiesFile( props, filePath, header );
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To list all the files of a directory ( on one level only ).
* USE THIS METHOD ONLY IF YOU KNOW THAT THE DIRECTORY GIVEN HERE COULD BE
* IN A JAR (if there is a jar, if none we search in the provided base path).
*
* IF you know that the directory is NOT in a JAR then use a simple File.listFiles().
*
* @param dirName directory name (must be a complete path)
* @return all the files of the specified directory (not sub-dirs).
*/
public String[] listFiles( String dirName ) {
return listFiles( dirName, "" );
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To list all the files of a directory ( on one level only ).
* USE THIS METHOD ONLY IF YOU KNOW THAT THE DIRECTORY GIVEN HERE COULD BE
* IN A JAR (if there is a jar, if none we search in the provided base path).
*
* IF you know that the directory is NOT in a JAR then use a simple File.listFiles().
*
* @param dirName directory name (must be a complete path)
* @param ext extension of the files to search, enter "" to get all the files.
* @return the files (not sub-dirs) that have the specified extension.
*/
public String[] listFiles( String dirName, String ext ) {
if( ext==null )
ext ="";
if( inJar && !isExternal(dirName) ) {
return Tools.listFilesInJar( jarName, dirName, ext );
}
else
return FileTools.listFiles( dirName, ext );
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To list all the directories of a directory ( on one level only ).
* USE THIS METHOD ONLY IF YOU KNOW THAT THE DIRECTORY GIVEN HERE COULD BE
* IN A JAR (if there is a jar, if none we search in the provided base path).
*
* IF you know that the directory is NOT in a JAR then use a simple File.listFiles().
*
* @param dirName directory name (must be a complete path)
* @return the sub-dirs of the given dirName (on one level only).
*/
public String[] listDirectories( String dirName ) {
if( inJar && !isExternal(dirName)) {
return Tools.listFilesInJar( jarName, dirName, null );
}
else
return FileTools.listDirs( dirName );
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To get a font resource as a stream.
* @param fontPath font path we'll get as a stream
* @return InputStream on the wanted font, null if the font was not found
*/
public InputStream getFontStream( String fontPath ) {
String fDir = getFontsDir();
if( !fontPath.startsWith(fDir) )
fontPath = fDir+fontPath;
if( inJar )
return this.getClass().getResourceAsStream( fontPath );
else {
try{
return new FileInputStream( fontPath );
}catch( FileNotFoundException fe ) {
Debug.signal(Debug.ERROR, this, fe );
return null;
}
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To get an inputstream on the wanted music.
* @param musicName music name such as "tarvalon.mid"
* @return InputStream on the wanted music, null if the music was not found
*/
public InputStream getMusicStream( String musicName ) {
String mDir = getMusicsDir();
if( !musicName.startsWith(mDir) )
musicName = mDir+musicName;
if( inJar )
return this.getClass().getResourceAsStream( musicName );
else {
try{
return new FileInputStream( musicName );
}catch( FileNotFoundException fe ) {
Debug.signal(Debug.ERROR, this, fe );
return null;
}
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To get an inputstream on the wanted sound.
* @param soundName sound name such as "boing.wav"
* @return InputStream on the wanted sound, null if the sound was not found
*/
public InputStream getSoundStream( String soundName ) {
String sDir = getSoundsDir();
if( !soundName.startsWith(sDir) )
soundName = sDir+soundName;
if( inJar )
return this.getClass().getResourceAsStream( soundName );
else {
try{
return new FileInputStream( soundName );
}catch( FileNotFoundException fe ) {
Debug.signal(Debug.ERROR, this, fe );
return null;
}
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To get an inputstream from a wanted file
* @param filePath complete file path
* @return InputStream on the wanted file, null if the file was not found
*/
public InputStream getFileStream( String filePath ) {
if( inJar )
return this.getClass().getResourceAsStream( filePath );
else {
try{
return new FileInputStream( filePath );
}catch( FileNotFoundException fe ) {
Debug.signal(Debug.ERROR, this, fe );
return null;
}
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Returns the wanted image icon from the base's GUI directory.
*
* @param imageName imageName with or without the complete resource path.
* @return ImageIcon, null if the image was not found.
*/
public ImageIcon getImageIcon( String imageName ) {
String imDir = getGuiImageDir();
if( !imageName.startsWith(imDir) )
imageName = imDir+imageName;
if( inJar ) {
URL url = this.getClass().getResource(imageName);
return new ImageIcon( url );
}
else
return new ImageIcon( imageName );
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Returns the wanted image from the base GUI directory.
*
* @param imageName imageName with or without the complete resource path.
* @return Image, null if the image was not found.
*/
public Image getGuiImage( String imageName ) {
String imDir = getGuiImageDir();
if( !imageName.startsWith(imDir) )
imageName = imDir+imageName;
if( inJar ) {
URL url = this.getClass().getResource(imageName);
return Toolkit.getDefaultToolkit().getImage( url );
}
else
return Toolkit.getDefaultToolkit().getImage( imageName );
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Returns the wanted image from the Image Library directory.
*
* @param imagePath image name from the library with FULL resource path.
* @return Image, null if the image was not found.
*/
public Image getLibraryImage( String imagePath ) {
if( inJar ) {
URL url = this.getClass().getResource(imagePath);
return Toolkit.getDefaultToolkit().getImage( url );
}
else
return Toolkit.getDefaultToolkit().getImage( imagePath );
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To get the path to the OS dependent tranfer script. This script will be used to
* transfer the server-X.cfg.adr file to the central wotlas web server.
* @return full path to the script to use for file transfer. This method uses the system
* property "os.name" to return a path to the right script.
*/
public String getExternalTransferScript() {
if( inClientJar )
return null; // this method is for the server side only
if( Tools.isWindowsOS() )
return DEFAULT_BIN_PATH+File.separator+WIN_BINARY_DIR+File.separator+TRANSFER_SCRIPT_NAME+".bat";
else
return DEFAULT_BIN_PATH+File.separator+UNIX_BINARY_DIR+File.separator+TRANSFER_SCRIPT_NAME+".sh";
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To get the path to the OS dependent scripts.
* @return full path to where the scripts are. This method uses the system
* property "os.name" to return a path to the right script.
*/
public String getExternalScriptsDir() {
if( inClientJar )
return null; // this method is for the server side only
if( Tools.isWindowsOS() )
return DEFAULT_BIN_PATH+File.separator+WIN_BINARY_DIR+File.separator;
else
return DEFAULT_BIN_PATH+File.separator+UNIX_BINARY_DIR+File.separator;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -