
de.xypron.util.IdeText Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ui Show documentation
Show all versions of ui Show documentation
Xypron GUI Components is a class library written in Java. It
facilitates writing Swing applications.
The newest version!
/**
* Copyright 2009, Heinrich Schuchardt
*
* @author Heinrich Schuchardt
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You
* may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package de.xypron.util;
import java.util.HashMap;
import javax.swing.JOptionPane;
/**
* Helper class to retrieve string resources.
*/
public final class IdeText {
private static HashMap resources
= new HashMap();
/**
* Show an error message
* @param cls Class for which the property file is read
* @param key String used as key in properties file.
*/
public static void errorMessage(Class cls, String key) {
JOptionPane.showMessageDialog(null,
getText(cls, key),
getText(IdeText.class,
"IdeComponent.Error"),
JOptionPane.ERROR_MESSAGE);
}
/**
* Show an error message
* @param message message text to display.
*/
public static void errorMessage(String message) {
errorMessage(null, message);
}
/**
* This method returns a language dependent string. The name of the
* resource bundle is obtained by appending ".strings" to the path
* of the class. If the string is not found for the provided class
* its superclasses are searched.
*
* @param cls Class for which the property file is read
* @param key String used as key in properties file.
* @return String
*/
public static String getText(Class cls, String key) {
java.util.ResourceBundle resource;
Class clas;
if (cls == null) {
return key;
}
if (!resources.containsKey(cls)) {
clas = cls;
while (clas != Object.class) {
String path = clas.getPackage().getName() + ".strings";
try {
resource = java.util.ResourceBundle.getBundle(path);
} catch (Exception e) {
resource = null;
}
resources.put(clas, resource);
clas = clas.getSuperclass();
}
}
clas = cls;
while (clas != Object.class) {
try {
resource = resources.get(clas);
if (resource != null) {
return resource.getString(key);
}
} catch (Exception e) {
}
clas = clas.getSuperclass();
}
return key;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy