forum

This must be shared: /^1?$|^(11+?)\1+$/ to check for prime numbers

That’s all and checks if a string does not consist of a prime number of “1“, e.g. “111” is prime whereas “1111” is not:

[source:ruby]
/^1?$|^(11+?)\1+$/
[/source]

Although this seems cryptic its rather straight forward: match on either “” or “1” (first part until “|“) or match on a substring of 2 or more “1” with repeatedly fits the whole. For the string “111111” the substring “11” fits in 3 times.

Over all its a very high level description of a primality test. I never considered regular expressions as a mathematical domain language.

More details on Avinash Meetoo’s Blog.

store

Don’t subclass DefautListCellRenderer for Swing’s Nimbus LAF

If you follow the common practice to subclass JLabel to add some formatting to your combobox you will be surprised that this will break the rendering of JComboBoxes in the Nimbus Look Ant Feel.

Examples of bad JLabel-based CellRenderer

Basically the nice glass like look of the combobox will disappear and the old flat look will be restored. This comes due to Nimbus using a custom CellRenderer:

javax.swing.plaf.synth.SynthComboBoxUI$SynthComboBoxRenderer

Fortunately this renderer is based on JLabel, too. This is why you can simply wrap the renderer and add some blinkenlights when needed:

Good example of renderer wrapping the original renderer

The implementation is straight forward:

[source:java]
public class InstanceWithIconCellRendererWrapper implements ListCellRenderer {
private final ListCellRenderer wrapped;
public InstanceWithIconCellRendererWrapper(ListCellRenderer listCellRenderer) {
this.wrapped = listCellRenderer;
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
String displayName = String.valueOf(value); // customize here
Component renderer = wrapped.getListCellRendererComponent(list, displayName, index, isSelected, cellHasFocus);
if (renderer instanceof JLabel) {
Icon icon = new ImageIcon(); // customize here
((JLabel) renderer).setIcon(icon);
}
return renderer;
}
}
[/source]

Use it as follows:


jComboBox.setRenderer(new InstanceWithIconCellRendererWrapper(jComboBox.getRenderer()));

information

Free Burma


Free Burma!

contact

Die! Singletons Die!

Enough is sayed here at Pure Danger Tech. Please let those singletons die, however there is no real singleton with java anyway.

forum

Where is inversion of control in JSR 296 (Swing Application Framework)?

JSR-296 will provide us with nice ressource management. You can get property settings like label texts, colors and other properties from ressource files easily:

ApplicationContext ctxt = ApplicationContext.getInstance();  
ResourceManager mgr = ctxt.getResourceManager();
resource = mgr.getResourceMap(HelloWorld.class);
String helloText = (String) resource.getObject("helloLabel", String.class);

But… what is this ApplicationContext.getInstance() static method call? Where is dependency injection? Look into the API documentation of ApplicationContext: it’s a global service locator. That means it provides a fixed bunch of services which are accessible by a singleton instance. You can of course use aggregation, sub-classing and delegation to extend it’s abilities but dependency injection is the train everyone jumps onto, ain’t it?

There is another issue that puzzles me: where are the interfaces? A framework whithout any interface? It’s even worse, ApplicationContext returns instances of concrete classes, not even abstract classes. So you’ll always get an instance of class RessourceManager. I you like to provide your own, you can only sub-class it, at least, RessourceManager is not final.

Can I have some dependeny injection, please? Picocontainer, Guice or Spring, perhaps? Yes, I can but it would be more nice, if RessourceManager, ActionManager and all other services provided by the framework would be interfaces and I could use any implemention I wish.

By the way, there is some dependency injection in the framework: you can inject property values into any awt component with RessourceManager.injectComponent. I hope the method name is not final yet, because I expected an component to be injected into some other instance, not something to be injected into a component.

store
handbook
Bear