[Buix-commits] r1264 - in jaxx/trunk/jaxx-runtime-swing: . src/main/java/jaxx/runtime
Author: tchemit Date: 2009-03-12 09:03:21 +0000 (Thu, 12 Mar 2009) New Revision: 1264 Modified: jaxx/trunk/jaxx-runtime-swing/changelog.txt jaxx/trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/SwingUtil.java Log: add some usefull code from ObServe (load Nimbus L&F, load ui configuration) Modified: jaxx/trunk/jaxx-runtime-swing/changelog.txt =================================================================== --- jaxx/trunk/jaxx-runtime-swing/changelog.txt 2009-03-10 22:57:32 UTC (rev 1263) +++ jaxx/trunk/jaxx-runtime-swing/changelog.txt 2009-03-12 09:03:21 UTC (rev 1264) @@ -1,4 +1,5 @@ 1.3 ?? 200903?? + * 20090312 [chemit] - add some usefull code from ObServe (load Nimbus L&F, load ui configuration) * 20090309 [chemit] - in BlockingLayerUI, add a new state 'block' to enable/disable blocking mode with a blockingColor * 20090303 [chemit] - in BlockingLayerUI, add a new state 'useIcon' to enable/disable use of the action icon - add a simple WizardModel Modified: jaxx/trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/SwingUtil.java =================================================================== --- jaxx/trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/SwingUtil.java 2009-03-10 22:57:32 UTC (rev 1263) +++ jaxx/trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/SwingUtil.java 2009-03-12 09:03:21 UTC (rev 1264) @@ -1,11 +1,15 @@ package jaxx.runtime; +import java.awt.Color; import java.awt.Component; import java.awt.Container; +import java.awt.Rectangle; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; +import java.io.IOException; +import java.io.InputStream; import java.util.List; import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; @@ -24,7 +28,11 @@ import java.util.ListIterator; import java.util.Map; import java.util.Map.Entry; +import java.util.Properties; +import javax.swing.ImageIcon; import javax.swing.JComponent; +import javax.swing.UIManager; +import javax.swing.UnsupportedLookAndFeelException; import jaxx.runtime.swing.I18nTableCellRenderer; import jaxx.runtime.swing.Item; import jaxx.runtime.swing.JAXXComboBox; @@ -46,6 +54,8 @@ static private final Log log = LogFactory.getLog(SwingUtil.class); private static Field numReaders; private static Field notifyingListeners; + public static final String ICON_PREFIX = "icon."; + public static final String COLOR_PREFIX = "color."; public static void setText(final JTextComponent c, final String text) { try { @@ -261,4 +271,90 @@ return result; } + + /** + * Centrer un component graphique au center d'un autre component. + * + * <b>Note:</b> si le parent est null, alors on ne fait rien. + * + * @param parent le component parent + * @param component le component à centrer + */ + public static void center(Component parent, Component component) { + if (parent == null) { + return; + } + Rectangle r = parent.getBounds(); + int x = r.x + (r.width - component.getSize().width) / 2; + int y = r.y + (r.height - component.getSize().height) / 2; + component.setLocation(x, y); + } + + /** + * Try to load the Nimbus look and feel. + * <p/> + * @throws UnsupportedLookAndFeelException + * if nimbus is not applicable + * @throws ClassNotFoundException + * @throws InstantiationException + * @throws IllegalAccessException + */ + public static void initNimbusLoookAndFeel() throws UnsupportedLookAndFeelException, ClassNotFoundException, InstantiationException, IllegalAccessException { + + for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(laf.getName())) { + UIManager.setLookAndFeel(laf.getClassName()); + } + } + } + + /** + * Load the ui.properties file and set in {@link UIManager} colors and icons found. + * + * @param defaultUIConfig le path vers le fichier de la config d'ui par défaut (doit etre dansle class-path) + * @param extraUIConfig le path vers une surcharge de la config d'ui (doit etre dans le class-path) + * + * @throws IOException if could not load the ui.properties file + */ + public static void loadUIConfig(String defaultUIConfig, String extraUIConfig) throws IOException { + + Properties p = new Properties(); + log.info("loading default UI config " + defaultUIConfig); + p.load(SwingUtil.class.getResourceAsStream(defaultUIConfig)); + if (log.isDebugEnabled()) { + log.debug(p.toString()); + } + if (extraUIConfig != null) { + InputStream extraStream = SwingUtil.class.getResourceAsStream(extraUIConfig); + if (extraStream == null) { + log.warn("could not find extraUIConfig : " + extraUIConfig); + } else { + log.info("loading extra UI config " + extraUIConfig); + Properties p2 = new Properties(p); + p2.load(extraStream); + if (log.isDebugEnabled()) { + log.debug(p2.toString()); + } + p.putAll(p2); + } + } + for (Entry<Object, Object> entry : p.entrySet()) { + String key = (String) entry.getKey(); + if (key.startsWith(ICON_PREFIX)) { + ImageIcon icon; + try { + icon = createImageIcon((String) entry.getValue()); + UIManager.put(key.substring(ICON_PREFIX.length()), icon); + } catch (Exception e) { + log.warn("could not load icon " + entry.getValue()); + } + continue; + } + if (key.startsWith(COLOR_PREFIX)) { + String value = (String) entry.getValue(); + String[] rgb = value.split(","); + UIManager.put(key.substring(COLOR_PREFIX.length()), new Color(Integer.valueOf(rgb[0]), Integer.valueOf(rgb[1]), Integer.valueOf(rgb[2]))); + } + } + } }
participants (1)
-
tchemit@users.labs.libre-entreprise.org