GuiTranslator.java
/*
* Copyright (c) 2003-2004 by Woehrmann Softwareentwicklung
*
* Woehrmann Softwareentwicklung (hereinafter referred to as "Developer")
* grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that Licensee does not utilize the software in a manner
* which is disparaging to the Developer.
*
* This software is provided "AS IS," without a warranty of any kind.
* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. THE DEVELOPER SHALL NOT BE LIABLE
* FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL THE DEVELOPER
* BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
* SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
* REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY
* TO USE SOFTWARE, EVEN IF THE DEVELOPER HAS BEEN ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGES.
*
*/
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class GuiTranslator
{ private static Element translationRepository;
/**
* Parse and store the contents of the XML file that contains the
* translated texts.
*
* @param repositoryFileName
* @throws Exception
*/
public static void initialize(String repositoryFileName) throws Exception
{ DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
translationRepository = documentBuilder.parse(repositoryFileName).getDocumentElement();
}
/**
* Translate all texts of those components of a GUObject that are defined
* within the translationRepository.
*
* @param guiIdentifier as ID name of the GUIObject to be translated
* @param gui as the GUIObject to be translated
* @param lang as the destination language
* @throws Exception correlation errors
*/
public static void translateFor(String guiIdentifier, GUIObject gui, String lang) throws Exception
{ if (translationRepository == null)
{ throw new Exception("GuiTranslator not initialized");
}
NodeList list = translationRepository.getChildNodes();
for (int i = 0, n = list.getLength(); i < n; i++)
{ Node guiIDElement = list.item(i);
if (guiIDElement instanceof Element)
{ if (guiIdentifier.equals(guiIDElement.getNodeName()))
{ translateFrom((Element)guiIDElement, gui, lang);
return;
}
}
}
throw new Exception ("No identifier Element found for name: " + guiIdentifier);
}
private static void translateFrom(Element handlerElement, GUIObject gui, String lang) throws Exception
{ NodeList components = handlerElement.getChildNodes();
for (int ci = 0, cn = components.getLength(); ci < cn; ci++)
{ Node componentElement = components.item(ci);
if (componentElement instanceof Element)
{ Component component = gui.getComponent(componentElement.getNodeName());
if (component == null)
{ throw new Exception ("Component not found: " + componentElement.getNodeName());
}
NodeList properties = componentElement.getChildNodes();
for (int pi = 0, pn = properties.getLength(); pi < pn; pi++)
{ Node propertyElement = properties.item(pi);
if (propertyElement instanceof Element)
{ String property = propertyElement.getNodeName();
Attr attr = ((Element)propertyElement).getAttributeNode(lang);
if (attr == null)
{ throw new Exception(
"No translation found for component: " + componentElement.getNodeName()
+ ", property: " + property + ", language: " + lang);
}
setPropertyFor(component, property, attr.getNodeValue());
}
}
}
}
}
private static void setPropertyFor(Component component, String property, String propertyValue) throws Exception
{ if (property.equals("accelerator"))
{ if (component instanceof JMenuItem)
{ if (propertyValue.trim().length() > 0)
{ char accelerator = propertyValue.toUpperCase().charAt(0);
((JMenuItem)component).setAccelerator(KeyStroke.getKeyStroke(
accelerator, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
}
else
{ ((JMenuItem)component).setAccelerator(null);
}
}
else throw propertyUndefinedFor(property, component);
}
else if (property.equals("items"))
{ if (component instanceof JComboBox)
{ JComboBox comboBox = (JComboBox)component;
int index = comboBox.getSelectedIndex();
comboBox.removeAllItems();
StringTokenizer st = new StringTokenizer(propertyValue, "|");
while (st.hasMoreTokens())
{ comboBox.addItem(st.nextToken());
}
if (index >= 0 && index < comboBox.getItemCount())
{ comboBox.setSelectedIndex(index);
}
}
else throw propertyUndefinedFor(property, component);
}
else if (property.equals("mnemonic"))
{ if (component instanceof AbstractButton)
{ if (propertyValue.trim().length() > 0)
{ char mnemonic = propertyValue.toLowerCase().charAt(0);
((AbstractButton)component).setMnemonic(mnemonic);
}
else
{ ((AbstractButton)component).setMnemonic(0);
}
}
else throw propertyUndefinedFor(property, component);
}
else if (property.equals("tabtitle"))
{ Container parent = component.getParent();
if (parent != null && parent instanceof JTabbedPane)
{ JTabbedPane tabbedPane = ((JTabbedPane)parent);
for (int tpi = 0, tpn = tabbedPane.getTabCount(); tpi < tpn; tpi++)
{ if (tabbedPane.getComponentAt(tpi).equals(component))
{ tabbedPane.setTitleAt(tpi, propertyValue);
return;
}
}
}
throw new Exception(
"Component: " + component.getName() + " does not reside in a JTabbedPane."
+ " Unable to set property: tabtitle!");
}
else if (property.equals("text"))
{ if (component instanceof AbstractButton) ((AbstractButton)component).setText(propertyValue);
else if (component instanceof JLabel) ((JLabel)component).setText(propertyValue);
else throw propertyUndefinedFor(property, component);
}
else if (property.equals("title"))
{ if (component instanceof Dialog) ((Dialog)component).setTitle(propertyValue);
else if (component instanceof Frame) ((Frame)component).setTitle(propertyValue);
else throw propertyUndefinedFor(property, component);
}
else if (property.equals("tooltip"))
{ if (component instanceof JComponent)
{ ((JComponent)component).setToolTipText(propertyValue);
}
else throw propertyUndefinedFor(property, component);
}
else
{ throw new Exception("property: " + property + " not defined");
}
}
private static Exception propertyUndefinedFor(String property, Component component)
{ return new Exception(
"Unable to set property: " + property + " for component: " + component.getName());
}
}